Beispiel #1
0
/* 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);
}
Beispiel #2
0
/* Initialize WinPcap for us. */
int
network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
{
    char temp[PCAP_ERRBUF_SIZE];
    char filter_exp[255];
    struct bpf_program fp;
    char *dev;

    /* Did we already load the DLL? */
    if (pcap_handle == NULL) return(-1);

#if 1
    /* Get the value of our capture interface. */
    dev = network_pcap;
    if (dev == NULL) {
	pclog(" PCap device is a null pointer!\n");
	return(-1);
    }
    if ((dev[0] == '\0') || !strcmp(dev, "none")) {
	pclog(" No network device configured!\n");
	return(-1);
    }
    pclog(" Network interface: '%s'\n", dev);
#endif

    strcpy(temp, f_pcap_lib_version());
    dev = strchr(temp, '(');
    if (dev != NULL) *(dev-1) = '\0';
    pclog("PCAP: initializing, %s\n", temp);

#if 0
    /* Get the value of our capture interface. */
    dev = network_pcap;
    if ((dev[0] == '\0') || !strcmp(dev, "none")) {
	pclog(" No network device configured!\n");
	return(-1);
    }
    pclog(" Network interface: '%s'\n", dev);
#else
    dev = network_pcap;
#endif

    pcap = f_pcap_open_live(dev,		/* interface name */
			   1518,	/* maximum packet size */
			   1,		/* promiscuous mode? */
			   10,		/* timeout in msec */
			   temp);	/* error buffer */
    if (pcap == NULL) {
	pclog(" Unable to open device: %s!\n", temp);
	return(-1);
    }

    /* Create a MAC address based packet filter. */
    pclog(" Installing packet filter for MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
			mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    sprintf(filter_exp,
	"( ((ether dst ff:ff:ff:ff:ff:ff) or (ether dst %02x:%02x:%02x:%02x:%02x:%02x)) and not (ether src %02x:%02x:%02x:%02x:%02x:%02x) )",
	mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
	mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    if (f_pcap_compile(pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
	if (f_pcap_setfilter(pcap, &fp) == -1)
		pclog(" Error installing filter (%s) !\n", filter_exp);
    } else {
	pclog(" Could not compile filter (%s) !\n", filter_exp);
    }

    /* Save the callback info. */
    poll_rx = func;
    poll_arg = arg;

    pclog(" Starting thread..\n");
    poll_tid = thread_create(poll_thread, mac);

    return(0);
}