int main(){ char *dev; int ret; char errbuf[PCAP_ERRBUF_SIZE]; bpf_u_int32 netp; bpf_u_int32 maskp; pcap_t* descr; dev = pcap_lookupdev(errbuf); if(dev == NULL){ printf("%s\n",errbuf); exit(1); } ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); if(ret == -1){ printf("%s\n",errbuf); exit(1); } descr = pcap_open_live(dev,BUFSIZ,1,1,errbuf); if(descr == NULL){ printf("%s\n",errbuf); exit(1); } pcap_loop(descr,-1,find,NULL); return 0; }
int main(int argc, char *argv[]) { char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *pd; if(!(dev = pcap_lookupdev(errbuf))) { perror(errbuf); exit(1); } if((pd = pcap_open_live(dev, PCAP_SNAPSHOT, 1, PCAP_TIMEOUT, errbuf)) == NULL) { perror(errbuf); exit(1); } if(pcap_loop(pd, -1, packet_view, 0) < 0) { perror(pcap_geterr(pd)); exit(1); } pcap_close(pd); return 0; }
void startSniffer(char *filter) { struct in_addr in; if (dev == NULL) if ((dev = pcap_lookupdev(errbuf)) == NULL) { fprintf(stderr, "startSniffer: couldn't find a device to sniff: %s\n", errbuf); exit(1); } printf("\n\n%s starting: listening on device: %s\n", VERSION, dev); if ((pd = pcap_open_live(dev, SNAPLEN, promisc, readtimeout, errbuf)) == NULL) { fprintf(stderr, "startSniffer: pcap_open_live failed: %s\n", errbuf); exit(1); } pcap_lookupnet(dev, &netp, &maskp, errbuf); in.s_addr = netp; printf("%s (%s/", dev, inet_ntoa(in)); in.s_addr = maskp; printf("%s) opened successfully in %spromiscuous mode\n", inet_ntoa(in), (promisc ? "" : "non-")); if (filter != NULL) { pcap_compile(pd, &fprog, filter, 0, netp); if ((pcap_setfilter(pd, &fprog)) == -1) { fprintf(stderr, "startSniffer: pcap_setfilter: cannot set filter\n"); exit(1); } pcap_freecode(&fprog); } }
int main(void) { char *device=NULL; pcap_if_t *alldevsp; char errbuf[PCAP_ERRBUF_SIZE]; device = pcap_lookupdev(errbuf); printf("device %s\n",device); if (pcap_findalldevs (&alldevsp, errbuf) < 0) { fprintf (stderr, "Error %s", errbuf); exit (1); } while (alldevsp != NULL) { printf ("%s\n", alldevsp->name); alldevsp = alldevsp->next; } return 1; }
int main(void) { printf("I'm a sniffer, running...\n"); pcap_t *sniffer_handle = NULL; char errbuf[PCAP_ERRBUF_SIZE], *device = NULL; int count = 0; memset(errbuf, 0, PCAP_ERRBUF_SIZE); device = pcap_lookupdev(errbuf); if (device == NULL) err_die(errbuf); printf("Opening device %s\n", device); sniffer_handle = pcap_open_live(device, 2048, 1, 512, errbuf); if (sniffer_handle == NULL) err_die(errbuf); if (pcap_datalink(sniffer_handle) != DLT_EN10MB) { printf("不支持Ethernet headers\n"); } pcap_loop(sniffer_handle, -1, process_packet, (u_char *)&count); return 0; }
/* Find and prepare ethernet device for capturing */ pcap_t *prepare_capture(char *interface, int promisc, char *capfilter) { char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *pcap_hnd; char *dev = NULL; bpf_u_int32 net, mask; struct bpf_program filter; /* Starting live capture, so find and open network device */ if (!interface) { dev = pcap_lookupdev(errbuf); if (dev == NULL) LOG_DIE("Cannot find a valid capture device: %s", errbuf); } else { dev = interface; } if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) net = 0; pcap_hnd = pcap_open_live(dev, BUFSIZ, promisc, 0, errbuf); if (pcap_hnd == NULL) LOG_DIE("Cannot open live capture on '%s': %s", dev, errbuf); set_link_header_offset(pcap_datalink(pcap_hnd)); /* Compile capture filter and apply to handle */ if (pcap_compile(pcap_hnd, &filter, capfilter, 0, net) == -1) LOG_DIE("Cannot compile capture filter '%s': %s", capfilter, pcap_geterr(pcap_hnd)); if (pcap_setfilter(pcap_hnd, &filter) == -1) LOG_DIE("Cannot apply capture filter: %s", pcap_geterr(pcap_hnd)); pcap_freecode(&filter); return pcap_hnd; }
int main() { char *dev,errbuf[PCAP_ERRBUF_SIZE]; pcap_t * handle; struct pcap_pkthdr header; const u_char *packet; struct bpf_program fp; char filter_exp[]="dst host www.google.com"; bpf_u_int32 mask,net; dev=pcap_lookupdev(errbuf); pcap_lookupnet(dev,&net,&mask,errbuf); handle=pcap_open_live(dev,BUFSIZ,0,-1,errbuf); //pcap_compile(handle,&fp,filter_exp,0,net); //pcap_setfilter(handle,&fp); packet=pcap_next(handle,&header); printf("%d\n",header.len); pcap_loop(handle,-1,callback,NULL); pcap_close(handle); return 0; }
/* * Class: com_me_lodea_jcap_JCapSession * Method: getDefaultInterface * Signature: ()Ljava/net/NetworkInterface; */ JNIEXPORT jobject JNICALL Java_com_me_lodea_jcap_JCapSession_getDefaultInterface (JNIEnv* const env, const jclass clazz) { char errorbuf[PCAP_ERRBUF_SIZE]; const char* dev; jclass ifaceClass; jmethodID factory; jstring ifaceName; dev = pcap_lookupdev(errorbuf); if (dev == NULL) { throwJCapException(env, errorbuf); return NULL; } ifaceClass = (*env)->FindClass(env, "java/net/NetworkInterface"); if (ifaceClass == NULL) { return NULL; } factory = (*env)->GetStaticMethodID(env, ifaceClass, "getByName", "(Ljava/lang/String;)Ljava/net/NetworkInterface;"); if (factory == NULL) { return NULL; } ifaceName = (*env)->NewStringUTF(env, dev); if (ifaceName == NULL) { return NULL; } return (*env)->CallStaticObjectMethod(env, ifaceClass, factory, ifaceName); }
/* * PCap initialize */ int pcap_initialize(char *pcap_errbuf, u_int32_t *net, u_int32_t *mask) { char *dev; // find a device if not specified if(!progopt.device_set()) { dev = pcap_lookupdev(pcap_errbuf); if (dev == NULL) { return 0; } } else { dev = progopt.device; } // get network number and mask associated with capture device if(pcap_lookupnet(dev, net, mask, pcap_errbuf) == -1) { return 0; } // open capture device handle = pcap_open_live(dev, PKT_LEN, 1, progopt.time, pcap_errbuf); if (handle == NULL) { return 0; } return 1; }
void capture_package( char * dst_ip_str ) { pcap_t* pcap_handle; char error_content[PCAP_ERRBUF_SIZE]; char *net_interface; struct bpf_program bpf_filter; /* "" indicates capture all packet*/ char bpf_filter_string[] = "tcp"; bpf_u_int32 net_mask; bpf_u_int32 net_ip; /* get network interface */ net_interface = pcap_lookupdev( error_content ); if(net_interface == NULL){ fprintf(stderr, "Couldn't find default device: %s\n", error_content); exit(1); } printf("Device: %s\n", net_interface); /* get network addr, mask */ if( pcap_lookupnet( net_interface, &net_ip, &net_mask, error_content ) == -1){ fprintf(stderr, "Couldn't get netmask for device %s\n", net_interface); exit(1); } /* open network interface */ pcap_handle = pcap_open_live( net_interface, BUFSIZ, 1, 0, error_content ); if(pcap_handle == NULL){ fprintf(stderr, "Couldn't open device %s: %s\n", net_interface, error_content); exit(1); } //sprintf(bpf_filter_string, "src %s and dst %s tcp", net_ip, dst_ip_str); /* compile the filter */ if( pcap_compile( pcap_handle, &bpf_filter, bpf_filter_string, 0, net_ip ) == -1){ fprintf(stderr, "couldn't parse filter: %s: %s\n", bpf_filter_string, pcap_geterr(pcap_handle)); exit(1); } /* set the filter */ if( pcap_setfilter( pcap_handle, &bpf_filter ) == -1 ){ fprintf(stderr, "couldn't install filter: %s: %s\n", bpf_filter_string, pcap_geterr(pcap_handle)); exit(1); } //if( pcap_datalink( pcap_handle ) != DLT_EN10MB ) //return link layer type // return; /* register the call back function, capture the packet in loop then, callback function analysis the packet */ pcap_loop( pcap_handle, -1, tcp_protocol_packet_callback, NULL ); pcap_close( pcap_handle ); }
int main() { struct pcap_pkthdr header; const u_char *packet; char errbuf[PCAP_ERRBUF_SIZE]; char *device; pcap_t *pcap_handle; int i; device = pcap_lookupdev(errbuf); if(device == NULL) pcap_fatal("pcap_lookupdev", errbuf); printf("Sniffing on device %s\n", device); pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf); if(pcap_handle == NULL) pcap_fatal("pcap_open_live", errbuf); for(i=0; i < 3; i++) { packet = pcap_next(pcap_handle, &header); printf("Got a %d byte packet\n", header.len); dump(packet, header.len); } pcap_close(pcap_handle); }
int main(int argc,char *argv[]){ char *dev, errbuf[PCAP_ERRBUF_SIZE]; int i = 0; struct bpf_program filter; char filter_app[] = "src host 192.168.8.144 && arp "; bpf_u_int32 mask; bpf_u_int32 net; pcap_t *handle = NULL; pcap_dumper_t *pcap_dumper = NULL; dev = pcap_lookupdev(errbuf); if(dev == NULL){ fprintf(stderr,"couldn't find default device: %s\n",errbuf); return(2); } prjntf("Device: %s\n",dev); pcap_lookupnet(dev,&net,&mask,errbuf); handle = pcap_open_ljve(dev,BUFSIZ,1,0,errbuf); pcap_compjle(handle,&filter,filter_app,0,net); pcap_setfjlter(handle,&filter); pcap_dumper = pcap_dump_open(handle,"ljbcaptest1.pcap"); prjntf("%d*******\n",i); j = pcap_loop(handle,10,pcap_dump,(u_char *)pcap_dumper); pcap_dump_flush(pcap_dumper); pcap_dump_close(pcap_dumper); prjntf("%d*******\n",i); pcap_close(handle); return(0); }
/* * display the interface selection for bridged sniffing */ static void curses_bridged_sniff(void) { wdg_t *in; char err[PCAP_ERRBUF_SIZE]; DEBUG_MSG("curses_bridged_sniff"); /* if the user has not specified an interface, get the first one */ if (GBL_OPTIONS->iface == NULL) { SAFE_CALLOC(GBL_OPTIONS->iface, IFACE_LEN, sizeof(char)); strncpy(GBL_OPTIONS->iface, pcap_lookupdev(err), IFACE_LEN - 1); } SAFE_CALLOC(GBL_OPTIONS->iface_bridge, IFACE_LEN, sizeof(char)); wdg_create_object(&in, WDG_INPUT, WDG_OBJ_WANT_FOCUS | WDG_OBJ_FOCUS_MODAL); wdg_set_color(in, WDG_COLOR_SCREEN, EC_COLOR); wdg_set_color(in, WDG_COLOR_WINDOW, EC_COLOR); wdg_set_color(in, WDG_COLOR_FOCUS, EC_COLOR_FOCUS); wdg_set_color(in, WDG_COLOR_TITLE, EC_COLOR_MENU); wdg_input_size(in, strlen("Second network interface :") + IFACE_LEN, 4); wdg_input_add(in, 1, 1, "First network interface :", GBL_OPTIONS->iface, IFACE_LEN, 1); wdg_input_add(in, 1, 2, "Second network interface :", GBL_OPTIONS->iface_bridge, IFACE_LEN, 1); wdg_input_set_callback(in, bridged_sniff); wdg_draw_object(in); wdg_set_focus(in); }
void capture(char *dev) { pcap_t *pcap; char errbuf[PCAP_ERRBUF_SIZE]; struct pcap_pkthdr header; /* The header that pcap gives us */ const u_char *packet; /* The actual packet */ if(NULL == dev) { dev = pcap_lookupdev(errbuf); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", errbuf); exit(1); } } pcap = pcap_create(dev, errbuf); pcap_set_rfmon(pcap, 1); pcap_set_promisc(pcap, 1); pcap_set_buffer_size(pcap, 1 * 1024 * 1024); pcap_set_timeout(pcap, 1); pcap_set_snaplen(pcap, 16384); pcap_activate(pcap); if(DLT_IEEE802_11_RADIO == pcap_datalink(pcap)) { pcap_loop(pcap, 0, got_packet, 0); } else { fprintf(stderr, "Could not initialize a IEEE802_11_RADIO packet capture for interface %s\n", dev); } }
/* main(): Main function. Opens network interface and calls pcap_loop() */ int main(int argc, char *argv[] ){ int i=0, count=0; pcap_t *descr = NULL; char errbuf[PCAP_ERRBUF_SIZE], *device=NULL; memset(errbuf,0,PCAP_ERRBUF_SIZE); if( argc > 1){ /* If user supplied interface name, use it. */ device = argv[1]; } else{ /* Get the name of the first device suitable for capture */ if ( (device = pcap_lookupdev(errbuf)) == NULL){ fprintf(stderr, "ERROR: %s\n", errbuf); exit(1); } } printf("Opening device %s\n", device); /* Open device in promiscuous mode */ if ( (descr = pcap_open_live(device, MAXBYTES2CAPTURE, 1, 512, errbuf)) == NULL){ fprintf(stderr, "ERROR: %s\n", errbuf); exit(1); } /* Loop forever & call processPacket() for every received packet*/ if ( pcap_loop(descr, -1, processPacket, (u_char *)&count) == -1){ fprintf(stderr, "ERROR: %s\n", pcap_geterr(descr) ); exit(1); } return 0; }
int handler::init_dev(const char *dev,const char *code) { char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; if (dev == 0 || strlen(dev) == 0){ dev = pcap_lookupdev(errbuf); } handle = pcap_open_live(dev, BUFSIZ, false, 0, errbuf); if (handle == 0){ std::cout << "open dev error:" << std::endl; std::cout << errbuf << std::endl; return -1; } // 设置过滤条件 // bpf_u_int32 mask; // bpf_u_int32 net; // int rc = pcap_lookupnet(dev, &net, &mask, errbuf); // if (rc == -1){ // std::cout << "pcap lookup error:" << std::endl; // std::cout << errbuf << std::endl; // return -1; // } struct bpf_program fcode; pcap_compile(handle, &fcode, code, 1,0 /*mask*/); pcap_setfilter(handle, &fcode); packet::getInstance()->init(dev); return 0; }
int my_pcap(){ char *devname; char errbuf[PCAP_ERRBUF_SIZE]; devname=pcap_lookupdev(errbuf); printf("opening device %s\n",devname); int snaplen=1518; /// open & live dev to cap packet // param1: dev name // param2: lenght of snap packet // param3: is MISC mode,0 not,other yes // this is determinated by network device setting first // param4: error buffer // return: ptr to pcap_t pcap_t *p_pcap=NULL; p_pcap = pcap_open_live(devname,snaplen,0,10,errbuf); if(p_pcap==NULL){ printf("[error]: %s\n",errbuf); exit(-1); } /// loop tp pcap // param1: ptr to pcap_t // param2: cnt num packet to capture // -1,only ocurr error to exit // param3: handler packet function ptr // param4: user? int cnt; pcap_loop(p_pcap,PCAP_NUM,my_pcap_handler,(u_char*)&cnt); return 0; }
int main(int argc, char** argv) { int i; char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *descr; const u_char *packet; struct pcap_pkthdr hdr; struct ether_header *epthr; struct bpf_program fp; bpf_u_int32 maskp; bpf_u_int32 netp; dev = pcap_lookupdev(errbuf); printf("%s", dev); fflush(stdout); pcap_lookupnet(dev, &netp, &maskp, errbuf); descr = pcap_open_live(dev, 100, 0, -1, errbuf); //ok descr = pcap_open_live("any", 100, 0, -1, errbuf); //tcp header error pcap_compile(descr, &fp, argv[1], 0, netp); pcap_setfilter(descr, &fp); pcap_loop(descr, -1, my_callback, "kkk"); fprintf(stdout, "aoaoa"); return 0; }
static captureObject * newcaptureObjectDevice(char *device, int snaplen, int promisc, int timeout) { char errbuf[PCAP_ERRBUF_SIZE]; captureObject *self; self = PyObject_New(captureObject, &capture_Type); if (self == NULL) return NULL; if (! device) { device = pcap_lookupdev(errbuf); if (! device) { PyErr_SetString(ErrorObject, errbuf); return NULL; } } self->device = PyMem_New(char, strlen(device)); strcpy(self->device, device); self->filename = NULL; self->pcap = pcap_open_live(device, snaplen, promisc, timeout, errbuf); if (! self->pcap) { PyMem_Free(self->device); PyErr_SetString(ErrorObject, errbuf); return NULL; } return self; }
int main(int argc,char **argv) { int i; char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr; const u_char *packet; struct pcap_pkthdr hdr; /* pcap.h */ struct ether_header *eptr; /* net/ethernet.h */ if(argc != 2) { fprintf(stdout,"Usage: %s numpackets\n",argv[0]); return 0; } /* grab a device to peak into¡ */ dev = pcap_lookupdev(errbuf); if(dev == NULL) { printf("%s\n", errbuf); exit(1); } /* open device for reading */ descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf); if(descr == NULL) { printf("pcap_open_live(): %s\n",errbuf); exit(1); } /* allright here we call pcap_loop(..) and pass in our callback function */ /* int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)*/ pcap_loop(descr,atoi(argv[1]),my_callback,NULL); fprintf(stdout,"\nDone processing packets¡ wheew!\n"); return 0; }
char * pcap_ex_lookupdev(char *ebuf) { #ifdef _WIN32 pcap_if_t *pifs, *pif; struct pcap_addr *pa; char *name = NULL; // Get all available devices. if (_pcap_ex_findalldevs(&pifs, ebuf) == -1) { return NULL; } // Get first not 0.0.0.0 or 127.0.0.1 device for (pif = pifs; pif != NULL; pif = pif->next) { for (pa = pif->addresses; pa != NULL; pa = pa->next) { struct sockaddr_in *addrStruct = (struct sockaddr_in *)pa->addr; u_long addr = addrStruct->sin_addr.S_un.S_addr; if (addrStruct->sin_family == AF_INET && addr != 0 && // 0.0.0.0 addr != 0x100007f // 127.0.0.1 ) { name = pif->name; break; } } } pcap_freealldevs(pifs); return (name); #else return (pcap_lookupdev(ebuf)); #endif }
int main() { char errBuf[PCAP_ERRBUF_SIZE], * devStr; /* get a device */ devStr = pcap_lookupdev(errBuf); if(devStr) { printf("success: device: %s\n", devStr); } else { printf("error: %s\n", errBuf); exit(1); } /* open a device, wait until a packet arrives */ pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf); if(!device) { printf("error: pcap_open_live(): %s\n", errBuf); exit(1); } /* wait loop forever */ int id = 0; pcap_loop(device, -1, getPacket, (u_char*)&id); pcap_close(device); return 0; }
pcap_t * pcap_open(char *device) { char ebuf[PCAP_ERRBUF_SIZE]; pcap_t *pcap; if (device == NULL) { if ((device = pcap_lookupdev(ebuf)) == NULL) return (NULL); } if ((pcap = pcap_open_live(device, 31337, 0, 10, ebuf)) == NULL) return (NULL); #ifdef BSD { int n = 1; if (ioctl(pcap_fileno(pcap), BIOCIMMEDIATE, &n) < 0) { pcap_close(pcap); return (NULL); } } #endif return (pcap); }
void liveSniffing(char* interface, u_char* filter){ char *dev = NULL; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *handle; /* check for capture device name on command-line */ if (interface == NULL) { /* find a capture device if not specified on command-line */ dev = pcap_lookupdev(errbuf); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", errbuf); exit(EXIT_FAILURE); } } else { dev = interface; } /* open capture device */ handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf); if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); exit(EXIT_FAILURE); } /* now we can set our callback function */ pcap_loop(handle, -1, got_packet, filter); /* cleanup */ pcap_close(handle); }
int main(int argc, char **argv) { char *devname; char ebuf[PCAP_ERRBUF_SIZE]; char llstr[6 * 3]; const u_char *pload; pcap_t *dev; struct pcap_pkthdr hdr; struct ethhdr *machdr; #if 0 printf("%zu\n", sizeof(*machdr)); return 0; #endif int c = get_program_options(argc, argv, NULL); printf("get_program_options: %d\n", c); return 0; devname = pcap_lookupdev(ebuf); printf("errbuf: %s: %s\n", ebuf, devname); dev = pcap_open_live("enp0s25", PCAP_ERRBUF_SIZE, 1, 2000, ebuf); printf("dev: %p\n", dev); pload = pcap_next(dev, &hdr); ebuf[PCAP_ERRBUF_SIZE - 1] = '\0'; printf("ploadptr: %p, hdrlen: %u\n", pload, hdr.len); machdr = eth_hdr(pload); printf("mac header: src: %s\n", mac_str(machdr->src, llstr)); printf("mac header: dst: %s\n", mac_str(machdr->dst, llstr)); return 0; }
int main(int argc, char **argv) { char *dev; /* name of the device to use */ char *net; /* dot notation of the network address */ char *mask;/* dot notation of the network mask */ int ret; /* return code */ char errbuf[PCAP_ERRBUF_SIZE]; bpf_u_int32 netp; /* ip */ bpf_u_int32 maskp;/* subnet mask */ struct in_addr addr; /* ask pcap to find a valid device for use to sniff on */ dev = pcap_lookupdev(errbuf); /* error checking */ if(dev == NULL) { printf("%s\n",errbuf); exit(1); } /* print out device name */ printf("DEV: %s\n",dev); /* ask pcap for the network address and mask of the device */ ret = pcap_lookupnet(dev,&netp,&maskp,errbuf); if(ret == -1) { printf("%s\n",errbuf); exit(1); } /* get the network address in a human readable form */ addr.s_addr = netp; net = inet_ntoa(addr); if(net == NULL)/* thanks Scott :-P */ { perror("inet_ntoa"); exit(1); } printf("NET: %s\n",net); /* do the same as above for the device's mask */ addr.s_addr = maskp; mask = inet_ntoa(addr); if(mask == NULL) { perror("inet_ntoa"); exit(1); } printf("MASK: %s\n",mask); return 0; }
int main(int argc,char**argv){ char *dev; int i,c; char errbuf[PCAP_ERRBUF_SIZE]; bpf_u_int32 mask, net; pcap_t *pd; tcpNumber = udpNumber = tcpSize = udpSize = totalPacket = 0; signal(SIGINT,termSignal); while( (c = getopt(argc,argv,"i:r:")) != -1 ){ switch(c){ case 'i': dev = optarg; break; case 'r': pd = pcap_open_offline(optarg,errbuf); break; } } // Read from device if(pd == NULL){ // Get device if(dev == NULL){ if ( (dev = pcap_lookupdev(errbuf)) == NULL) fprintf(stderr, "pcap_lookup: %s\n", errbuf); } printf("device = %s\n",dev); // Open dev to capture packet. pd = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf); if( pd == NULL ){ fprintf(stderr, "pcap_open_live: %s\n", errbuf ); } // Get ip and mask from dev if( pcap_lookupnet(dev, &net, &mask, errbuf) == -1 ){ fprintf(stderr, "pcap_lookupnet: %s\n", errbuf); net = 0; mask = 0; } pcap_loop(pd, 0, pcap_handlers, NULL); } // Read from pcap file else{ pcap_loop(pd, 0, pcap_handlers, NULL); printf("Captured packets: %d\n",totalPacket); printf("Total connection: %d\n", connectionNumber); printf("type\t\t\tconnection\t\t\tpackets\t AVG.size\n"); for( i = 0; i < connectionNumber; i++ ){ printf("%-6s%-15s:%-5d -> %-15s:%-5d\t%d\t %d\n",connection[i].type, connection[i].src, connection[i].srcport,connection[i].dst,connection[i].dstport,connection[i].packetnumber,connection[i].averageSize); } } }
void main(int argc, char **argv) { L = MakeEmpty( NULL ); /* Initialize the packets buffer */ P = Header( L ); /* threads handling variables */ struct thread_list threads; char *dev = NULL; /* capture device name */ char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */ /* check for capture device name on command-line */ if (argc == 2) { dev = argv[1]; } else if (argc > 2) { fprintf(stderr, "error: unrecognized command-line options\n\n"); print_app_usage(); exit(EXIT_FAILURE); } else { /* find a capture device if not specified on command-line */ dev = pcap_lookupdev(errbuf); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", errbuf); exit(EXIT_FAILURE); } } /* Time to split into two threads : One for capturing and one for injection */ if ( ( pthread_create ( &threads.capture_thread, NULL, packet_capture,dev ) ) != 0 ) { printf ( "\nError: Capture thread creation.\n" ); exit ( 1 ); } if ( ( pthread_create ( &threads.inject_thread, NULL, packet_inject, dev ) ) != 0 ) { printf ( "\nError: Inject thread creation.\n" ); exit ( 1 ); } pthread_join ( threads.capture_thread, NULL ); pthread_join ( threads.inject_thread, NULL ); PRINT_DEBUG("\nCapture complete.\n"); PRINT_DEBUG("\nINJECTION complete.\n"); return; }
inline std::string find_default_device_name() { auto buf = error_buffer{}; auto device_name = std::string{pcap_lookupdev(buf.data())}; if (device_name.empty()) { throw error{"Couldn't find default device\n" + error_string(buf)}; } return device_name; }
static char *null_PCAP_LOOKUPDEV(char *errbuf) { #ifdef STATICPCAP return pcap_lookupdev(errbuf); #endif seterr(errbuf, "libpcap not loaded"); return ""; }