Я пишу программу захвата пакетов.
Я использую npcap и прикрепляю код, а также вывод при запуске ..
Проблема заключается в том, что напечатанные значения caplen и len очень большие цифры … и иногда слишком малы, иногда равные 20, что невозможно.
Код — это простой пример кода, который поставляется с npcap.
Может кто-то предположить, что здесь идет не так ..?
Удивительно, но захваченный файл при открытии в wireshark имеет точное количество пакетов, и пакеты также читаются.
Так что не удивительно, почему caplen и len показывают неправильные цифры.
Code is :
.#ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
.#define _CRT_SECURE_NO_WARNINGS
.#endif
.#include "pcap.h"
/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main(int argc, char **argv)
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_dumper_t *dumpfile;/* Check command line */
if(argc != 2)
{
printf("usage: %s filename", argv[0]);
return -1;
}
/* Retrieve the device list on the local machine */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
return 0;
}
/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);/* Open the adapter */
if ((adhandle= pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Open the dump file */
dumpfile = pcap_dump_open(adhandle, argv[1]);
if(dumpfile==NULL)
{
fprintf(stderr,"\nError opening output file\n");
return -1;
}
printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
/* At this point, we no longer need the device list. Free it */
pcap_freealldevs(alldevs);
/* start the capture */
pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
pcap_close(adhandle);
return 0;
}
int count = 0;
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
printf ("\nPacket count is %d", count++);
/* save the packet on the dump file */
pcap_dump(dumpfile, header, pkt_data);
printf("\n\nRecieved Packet Size caplen: %zu", header->caplen); /* Length of header */
printf("\nRecieved Packet Size just len: %lu\n", header->len); /* Length of header */
}Output of the program :C:\eclipse\w1\tn\Debug>tn pdump
1. \Device\NPF_{3A8147EC-2368-4216-9603-7641E806F7CE} (Realtek PCIe GBE Family Controller)
2. \Device\NPF_{E39C84EC-CB5D-4300-A5C1-C63EDE0B583C} (Microsoft)
3. \Device\NPF_{2F63E116-DEB4-4FE4-9E27-85DCA9E68C5C} (VMware Virtual Ethernet Adapter)
4. \Device\NPF_{996ACB8F-C0AC-4FF3-8E79-B0C849CC9CF1} (MS NDIS 6.0 LoopBack Driver)
5. \Device\NPF_{619BA8D0-363E-4BCB-8739-4537D3FDAD87} (MS NDIS 6.0 LoopBack Driver)
6. \Device\NPF_{E2FB8B54-0E25-498C-975A-32873AC52060} (VMware Virtual Ethernet Adapter)
7. \Device\NPF_{9B17BD4E-E20D-43A3-A8C6-AC8C3C431182} (Microsoft)
Enter the interface number (1-7):2
listening on Microsoft... Press Ctrl+C to stop...
Packet count is 0
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 1
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 2
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 3
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 4
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 5
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 6
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 7
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 8
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 9
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 10
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 11
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 12
Recieved Packet Size caplen: 1342177300
Recieved Packet Size just len: 3632886444
Packet count is 13
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 14
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 15
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 16
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 17
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 18
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 19
Recieved Packet Size caplen: 1161363476
Recieved Packet Size just len: 4294967295
Packet count is 20
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 6160385
Packet count is 21
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 22
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 23
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 24
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 25
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 26
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 27
Recieved Packet Size caplen: 4278190100
Recieved Packet Size just len: 4294967295
Packet count is 28
Recieved Packet Size caplen: 16711700
Recieved Packet Size just len: 4294967295
Packet count is 29
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295
Packet count is 30
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295
Packet count is 31
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295
Packet count is 32
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 33
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 34
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 35
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 36
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817
Packet count is 37
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 38
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 39
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 40
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 41
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444
Packet count is 42
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444
Packet count is 43
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 44
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 45
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 46
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 47
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 48
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 49
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 50
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 51
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 52
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 53
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 54
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 55
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 6160385
Packet count is 56
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 57
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 58
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 2136866817
Packet count is 59
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817
Packet count is 60
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817
Packet count is 61
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817
Packet count is 62
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 63
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 64
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 65
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 66
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 67
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 68
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 69
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 70
Recieved Packet Size caplen: 1929379860
Recieved Packet Size just len: 3632886444
Packet count is 71
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 72
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 73
Recieved Packet Size caplen: 5111828
Recieved Packet Size just len: 3632886444
Packet count is 74
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 75
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 76
Recieved Packet Size caplen: 5701652
Recieved Packet Size just len: 3632886444
Packet count is 77
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 78
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 79
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 80
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 81
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 82
Recieved Packet Size caplen: 822083604
Recieved Packet Size just len: 3632886444
Packet count is 83
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 84
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 85
Recieved Packet Size caplen: 1448083476
Recieved Packet Size just len: 3632886444
Packet count is 86
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 87
Recieved Packet Size caplen: 1073741844
Recieved Packet Size just len: 3632886444
Packet count is 88
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 89
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 90
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 91
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 92
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 93
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 94
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 95
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 96
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 97
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 98
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 99
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 100
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 101
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 102
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 1970834832
Packet count is 103
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 104
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 105
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 106
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 107
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 108
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 109
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444
Packet count is 110
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 111
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 112
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 113
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 114
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444
Packet count is 115
Recieved Packet Size caplen: 562102292
Recieved Packet Size just len: 1970834832
Packet count is 116
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 117
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 118
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 119
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 120
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 121
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 1970834832
Packet count is 122
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444
Packet count is 123
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 124
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 125
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 126
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832
Packet count is 127
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 128
Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832
Packet count is 129
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 130
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 131
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 132
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 1970834832
Packet count is 133
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 134
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 135
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 136
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295
Packet count is 137
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444
Packet count is 138
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 139
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295
Packet count is 140
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295
Packet count is 141
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 142
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295
Packet count is 143
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295
Packet count is 144
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 145
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 146
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295
Packet count is 147
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 148
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444
Packet count is 149
Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295
Packet count is 150
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 151
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 152
Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295
Packet count is 153
Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295
Packet count is 154
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 155
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 156
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 157
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 158
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 159
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 160
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 161
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 162
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 163
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Packet count is 164
Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295
Беглый взгляд на исходный код libpcap показывает, что caplen
а также len
имеют 32-битный тип без знака, но вы используете %zu
для чего size_t
(который может быть 64 бит).
Попробуй с %u
и посмотреть, если вывод выглядит правильно тогда.
Других решений пока нет …