int _recvPacket(void *pData) { struct bpf_hdr *hdr; u8 *data; int ret=0; int size; while (lbytes > 0) { hdr = (struct bpf_hdr *)buf; // DEV9_LOG("hdr %d,%d,%d\n", hdr->bh_hdrlen, hdr->bh_caplen, hdr->bh_datalen); // DEV9_LOG("lbytes %d\n", lbytes); data = buf+hdr->bh_hdrlen; size = Packet_WORDALIGN(hdr->bh_hdrlen+hdr->bh_datalen); buf+= size; lbytes-= size; if (_filterPacket(data)) { struct bpf_stat stat; ret = hdr->bh_datalen; memcpy(pData, data, ret); if(PacketGetStats(lpAdapter,&stat)==FALSE){ printf("Warning: unable to get stats from the kernel!\n"); } // printf("_recvPacket %d (tbytes=%d, packets=%d, lost=%d, time=%d)\n", ret, tbytes, stat.bs_recv,stat.bs_drop, timeGetTime()); // printf("%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", data[5], data[4], data[3], data[2], data[1], data[0]); break; } } return ret; }
static int pcap_stats_win32(pcap_t *p, struct pcap_stat *ps) { if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){ snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", pcap_win32strerror()); return -1; } return 0; }
int pcap_stats(pcap_t *p, struct pcap_stat *ps) { #ifdef REMOTE if (p->rmt_clientside) { /* We are on an remote capture */ return pcap_stats_remote(p, ps); } #endif if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){ snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", pcap_win32strerror()); return -1; } return 0; }
/** * Check for newly received packets. Called in the main loop: 'interrupt' mode is not * really supported :( * * @param adapter adapter handle received by a call to init_adapter */ void update_adapter(void *adapter) { struct packet_adapter *pa = (struct packet_adapter*)adapter; struct bpf_stat stat; /* print the capture statistics */ if(PacketGetStats(pa->lpAdapter, &stat) == FALSE) { printf("Warning: unable to get stats from the kernel!\n"); } else { if (pa->bs_drop != stat.bs_drop) { printf("%d packets received.\n%d Packets dropped.\n", stat.bs_recv, stat.bs_drop); pa->bs_drop = stat.bs_drop; } } if ((pa != NULL) && (PacketReceivePacket(pa->lpAdapter, pa->lpPacket, TRUE))) { ProcessPackets(adapter, pa->lpPacket); } }
static int pcap_stats_win32(pcap_t *p, struct pcap_stat *ps) { struct pcap_win *pw = p->priv; struct bpf_stat bstats; char errbuf[PCAP_ERRBUF_SIZE+1]; /* * Try to get statistics. * * (Please note - "struct pcap_stat" is *not* the same as * WinPcap's "struct bpf_stat". It might currently have the * same layout, but let's not cheat. * * Note also that we don't fill in ps_capt, as we might have * been called by code compiled against an earlier version of * WinPcap that didn't have ps_capt, in which case filling it * in would stomp on whatever comes after the structure passed * to us. */ if (!PacketGetStats(pw->adapter, &bstats)) { pcap_win32_err_to_str(GetLastError(), errbuf); pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "PacketGetStats error: %s", errbuf); return (-1); } ps->ps_recv = bstats.bs_recv; ps->ps_drop = bstats.bs_drop; /* * XXX - PacketGetStats() doesn't fill this in, so we just * return 0. */ #if 0 ps->ps_ifdrop = bstats.ps_ifdrop; #else ps->ps_ifdrop = 0; #endif return (0); }
int main() { //define a pointer to an ADAPTER structure LPADAPTER lpAdapter = 0; //define a pointer to a PACKET structure LPPACKET lpPacket; int i; DWORD dwErrorCode; //ascii strings char AdapterName[8192]; // string that contains a list of the network adapters char *temp,*temp1; int AdapterNum=0,Open; ULONG AdapterLength; char buffer[256000]; // buffer to hold the data coming from the driver struct bpf_stat stat; // // Obtain the name of the adapters installed on this machine // printf("Packet.dll test application. Library version:%s\n", PacketGetVersion()); printf("Adapters installed:\n"); i=0; AdapterLength = sizeof(AdapterName); if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){ printf("Unable to retrieve the list of the adapters!\n"); return -1; } temp=AdapterName; temp1=AdapterName; while ((*temp!='\0')||(*(temp-1)!='\0')) { if (*temp=='\0') { memcpy(AdapterList[i],temp1,temp-temp1); temp1=temp+1; i++; } temp++; } AdapterNum=i; for (i=0;i<AdapterNum;i++) printf("\n%d- %s\n",i+1,AdapterList[i]); printf("\n"); do { printf("Select the number of the adapter to open : "); scanf("%d",&Open); if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); } while (Open>AdapterNum); lpAdapter = PacketOpenAdapter(AdapterList[Open-1]); if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) { dwErrorCode=GetLastError(); printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); return -1; } // set the network adapter in promiscuous mode if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){ printf("Warning: unable to set promiscuous mode!\n"); } // set a 512K buffer in the driver if(PacketSetBuff(lpAdapter,512000)==FALSE){ printf("Unable to set the kernel buffer!\n"); return -1; } // set a 1 second read timeout if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){ printf("Warning: unable to set the read tiemout!\n"); } //allocate and initialize a packet structure that will be used to //receive the packets. if((lpPacket = PacketAllocatePacket())==NULL){ printf("\nError: failed to allocate the LPPACKET structure."); return (-1); } PacketInitPacket(lpPacket,(char*)buffer,256000); //main capture loop while(!kbhit()) { // capture the packets if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){ printf("Error: PacketReceivePacket failed"); return (-1); } PrintPackets(lpPacket); } //print the capture statistics if(PacketGetStats(lpAdapter,&stat)==FALSE){ printf("Warning: unable to get stats from the kernel!\n"); } else printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop); PacketFreePacket(lpPacket); // close the adapter and exit PacketCloseAdapter(lpAdapter); return (0); }