File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / hping2 / libpcap_stuff.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:11:37 2012 UTC (12 years, 4 months ago) by misho
Branches: hping2, MAIN
CVS tags: v2_0_0rc3p7, v2_0_0rc3p5, v2_0_0rc3p4, v2_0_0rc3p0, v2_0_0rc3, HEAD
hping2

    1: /* 
    2:  * $smu-mark$ 
    3:  * $name: libpcap_stuff.c$ 
    4:  * $author: Salvatore Sanfilippo <antirez@invece.org>$ 
    5:  * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$ 
    6:  * $license: This software is under GPL version 2 of license$ 
    7:  * $date: Fri Nov  5 11:55:48 MET 1999$ 
    8:  * $rev: 8$ 
    9:  */ 
   10: 
   11: #include "hping2.h"
   12: 
   13: /* This is not be compiled if the target is linux without libpcap */
   14: #if (!defined OSTYPE_LINUX) || (defined FORCE_LIBPCAP)
   15: #include <stdio.h>
   16: #include <string.h>
   17: #include <stdlib.h>
   18: #include <sys/ioctl.h>
   19: #include <pcap.h>
   20: #include <net/bpf.h>
   21: 
   22: #include "globals.h"
   23: 
   24: int open_pcap()
   25: {
   26: 	int on;
   27: 
   28: 	on = 1; /* no warning if BIOCIMMEDIATE will not be compiled */
   29: 	if (opt_debug)
   30: 		printf("DEBUG: pcap_open_live(%s, 99999, 0, 1, %p)\n",
   31: 			ifname, errbuf);
   32: 
   33: 	pcapfp = pcap_open_live(ifname, 99999, 0, 1, errbuf);
   34: 	if (pcapfp == NULL) {
   35: 		printf("[open_pcap] pcap_open_live: %s\n", errbuf);
   36: 		return -1;
   37: 	}
   38: #if (!defined OSTYPE_LINUX) && (!defined __sun__)
   39: 	/* Return the packets to userspace as fast as possible */
   40: 	if (ioctl(pcap_fileno(pcapfp), BIOCIMMEDIATE, &on) == -1)
   41: 		perror("[open_pcap] ioctl(... BIOCIMMEDIATE ...)");
   42: #endif
   43: 	return 0;
   44: }
   45: 
   46: int close_pcap()
   47: {
   48: 	pcap_close(pcapfp);
   49: 	return 0;
   50: }
   51: 
   52: int pcap_recv(char *packet, unsigned int size)
   53: {
   54:         char *p = NULL;
   55:         int pcapsize;
   56: 
   57: 	if (opt_debug)
   58: 		printf("DEBUG: under pcap_recv()\n");
   59: 
   60:         while(p == NULL) {
   61:                 p = (unsigned char*) pcap_next(pcapfp, &hdr);
   62: 		if (p == NULL && opt_debug)
   63: 			printf("DEBUG: [pcap_recv] p = NULL\n");
   64: 	}
   65: 
   66:         pcapsize = hdr.caplen;
   67: 
   68:         if (pcapsize < size)
   69:                 size = pcapsize;
   70: 
   71:         memcpy(packet, p, pcapsize);
   72: 
   73:         return pcapsize;
   74: }
   75: #endif /* ! OSTYPE_LINUX || FORCE_LIBPCAP */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>