c - pcap_loop() ends right afterI ping a host -


i trying make packet sniffer, encountering problems. using linux (debian) , pcap library.

so, have set pcap filter "host www.google.com". when run program, waits, , if ping www.google.com, quits (just if pressed ctrl+c), , don't know why.

if replace callback function's body with:

void got_packet(....) {     static int 1 = 1;     printf("%i ", i);     i++; } 

then program works fine (it counts number of packets). if replace below code, said earlier. appreciated, thanks. (summary below code: handle_ethernet() supposed print ethernet header. got_packet() calls handle_ethernet() )

#include<stdio.h> #include<stdlib.h> #include<pcap.h> #include<sys/socket.h> #include<errno.h> #include<netinet/in.h> #include<arpa/inet.h> #include<netinet/if_ether.h>  int handle_ethernet(u_char* args, const struct pcap_pkthdr* pkthdr,             const u_char* packet) {     struct ether_header* eptr;     eptr = (struct ether_header*)packet;     printf("source ethernet header: %s\n",         ether_ntoa((const struct ether_addr*)eptr->ether_shost));     printf("destination ethernet header: %s\n",          ether_ntoa((const struct ether_addr*)eptr->ether_dhost));     fflush(stdout);      if(ntohs(eptr->ether_type) == ethertype_ip) {         printf(" [ip]\n");     } else if(ntohs(eptr->ether_type) == ethertype_arp) {         printf(" [arp]\n");     } else {         printf(" [?}\n");     }      return eptr->ether_type;  }  void got_packet(u_char* args, const struct pcap_pkhdr* hdr,          const u_char* packet) {     struct ether_header* eptr;     eptr = (struct ether_header*)packet;     printf("source: %s\n", ether_ntoa((const struct ether_addr*)eptr->ether_shost));     printf("dest: %s\n", ether_ntoa((const struct ether_addr*)eptr->ether_dhost));     fflush(stdout);   //  static int = 1; //  printf("%i ", i); //  fflush(stdout); //  i++; }  int main() {     pcap_if_t* alldevs;     pcap_if_t* d;     char errbuf[256];     char input_dev[5];     pcap_t* descr;       struct bpf_program fp; // here keep compiled filter expr     int mask, network;     char filter_exp[] = "host www.google.com";     struct pcap_pkthdr hdr;     struct ether_header* eptr;     const u_char* packet;       if(pcap_findalldevs(&alldevs, errbuf) == -1) {         printf("error while finding devs.\n");         exit(1);     }      d = alldevs;     while(d != null) {         printf("%s\n", d->name);         d = d->next;     }      scanf("%s", input_dev);     printf("%s\n", input_dev);      /* ip , mask */     if(pcap_lookupnet(input_dev, &network, &mask, errbuf) == -1) {         printf("pcap_lookupnet error: %s.\n", errbuf);         exit(1);     }      /* opening device sniffing */     descr = pcap_open_live(input_dev, bufsiz, 1, -1, errbuf);      /* determine type of link-layer headers device provides */     if(pcap_datalink(descr) != dlt_en10mb) {         fprintf(stderr, "device %s doesn't provide ethernet headers\n",             input_dev);         exit(1);     }     /* optional, guess */      /* compile filter */     if(pcap_compile(descr, &fp, filter_exp, 0, network) == -1) {         printf("error while compiling filter.\n");         exit(1);     }      if(pcap_setfilter(descr, &fp) == -1) {         printf("pcap_setfilter error.\n");         exit(1);     }      /* capture loop */     pcap_loop(descr, 1000, got_packet, null);      pcap_close(descr);      return 0; } 


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -