/* Close up shop. */ void network_pcap_close(void) { pcap_t *pc; if (pcap != NULL) { pclog("Closing WinPcap\n"); /* Tell the polling thread to shut down. */ pc = pcap; pcap = NULL; #if 1 /* Terminate the polling thread. */ if (poll_tid != NULL) { thread_kill(poll_tid); poll_tid = NULL; } #else /* Wait for the polling thread to shut down. */ while (poll_tid != NULL) ; #endif /* OK, now shut down WinPcap itself. */ f_pcap_close(pc); /* Unload the DLL if possible. */ if (pcap_handle != NULL) { dynld_close(pcap_handle); pcap_handle = NULL; } } poll_rx = NULL; poll_arg = NULL; }
/* Capture packets from the network, and print them. */ static int start_cap(char *dev) { char temp[PCAP_ERRBUF_SIZE]; struct pcap_pkthdr *hdr; const unsigned char *pkt; struct tm *ltime; time_t now; pcap_t *pcap; int rc; /* Open the device for reading from it. */ pcap = f_pcap_open_live(dev, 1518, /* MTU */ 1, /* promisc mode */ 10, /* timeout */ temp); if (pcap == NULL) { fprintf(stderr, "Pcap: open_live(%s): %s\n", dev, temp); return(2); } printf("Listening on '%s'..\n", dev); for (;;) { rc = f_pcap_next_ex(pcap, &hdr, &pkt); if (rc < 0) break; /* Did we time out? */ if (rc == 0) continue; /* Convert the timestamp to readable format. */ now = hdr->ts.tv_sec; ltime = localtime(&now); strftime(temp, sizeof(temp), "%H:%M:%S", ltime); /* Process and print the packet. */ printf("\n<< %s,%.6ld len=%u\n", temp, hdr->ts.tv_usec, hdr->len); rc = eth_prhdr((unsigned char *)pkt); hex_dump((unsigned char *)pkt+rc, hdr->len-rc); } /* All done, close up. */ f_pcap_close(pcap); return(0); }