Ubuntu下GCC之libpcap库数据库通信协议解析基础框架(7)

printf("-------Ethernet Protocol (Link Layer)---------\n");
 /* get ethernet header */   
 ethernet = (struct db_ethernet*)(packet);
 printf("Mac Source Address is:\n");
 mac_string=(u_char*)ethernet->ether_shost;
 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
 printf("Mac Destination Address is:\n");
 mac_string=(u_char*)ethernet->ether_dhost;
 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
 ethernet_type=ntohs(ethernet ->ether_type);
        switch(ethernet_type)
 {
 case 0x0800: got_ip_package(args,header,packet);break;
 case 0x0806: printf("The network layer is ARP protocol\n");break;
 case 0x0835: printf("The network layer is RARP protocol\n");break;
 default:break;
 }
return;
}
/*function implement end*/

/*main function,entrance for system*/
int main(int argc, char **argv)
{

char *dev = NULL;   /* capture device name */
 char errbuf[PCAP_ERRBUF_SIZE];  /* error buffer */
 pcap_t *handle;    /* packet capture handle */
 char filter_exp[] = "ip";
 //char filter_exp[] = "ip host 127.0.0.1";/*port 3306*//* filter expression [3] */
 struct bpf_program fp;   /* compiled filter program (expression) */
 bpf_u_int32 mask;   /* subnet mask */
 bpf_u_int32 net;   /* ip */
 int num_packets = -1;   /* number of packets to capture ,-1 is loop*/

print_app_banner();

/* 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);
  }
 }
 
 /* get network number and mask associated with capture device */
 if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
  fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
      dev, errbuf);
  net = 0;
  mask = 0;
 }

/* print capture info */
 printf("Device: %s\n", dev);
 printf("Number of packets: %d\n", num_packets);
 printf("Filter expression: %s\n", filter_exp);

#ifdef OFFLINE
 handle=pcap_open_offline("tns.pcap",errbuf);//read  offline  file
#else
 /* open capture device */
 handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
#endif
 if (handle == NULL) {
  fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
  exit(EXIT_FAILURE);
 }

/* make sure we're capturing on an Ethernet device [2] */
 if (pcap_datalink(handle) != DLT_EN10MB) {
  fprintf(stderr, "%s is not an Ethernet\n", dev);
  exit(EXIT_FAILURE);
 }

/* compile the filter expression */
 if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
  fprintf(stderr, "Couldn't parse filter %s: %s\n",
      filter_exp, pcap_geterr(handle));
  exit(EXIT_FAILURE);
 }

/* apply the compiled filter */
 if (pcap_setfilter(handle, &fp) == -1) {
  fprintf(stderr, "Couldn't install filter %s: %s\n",
      filter_exp, pcap_geterr(handle));
  exit(EXIT_FAILURE);
 }

/* now we can set our callback function */
 pcap_loop(handle, num_packets, got_ethernet_packet, NULL);

/* cleanup */
 pcap_freecode(&fp);
 pcap_close(handle);

printf("\nCapture complete.\n");

return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ppjzz.html