Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111896
  • 博文数量: 27
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-07 00:14
文章分类

全部博文(27)

文章存档

2015年(1)

2014年(20)

2013年(6)

我的朋友

分类: LINUX

2013-08-07 00:30:27

 1

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <pcap.h>
  7. #include <signal.h>
  8. #include <getopt.h>

  9. int main(int argc, char *argv[])
  10. {

  11.     int c;
  12.     char *device = NULL;
  13.     char *fname = NULL;
  14.     char errbuf[PCAP_ERRBUF_SIZE];
  15.     pcap_t *p = NULL;


  16.     while ( (c = getopt(argc, argv, "di:")) != -1)
  17.     {
  18.         switch (c)
  19.         {
  20.             case 'd':
  21.                 {
  22.                     pcap_if_t *alldevsp = NULL;
  23.                     if (pcap_findalldevs(&alldevsp, errbuf) == -1)
  24.                     {
  25.                         fprintf(stderr, "\t #### failed to list the device, err msg: %s\n", strerror(errno));
  26.                         exit(-1);
  27.                     }
  28.                     pcap_if_t *devsp = alldevsp;
  29.                     fprintf(stderr, "List the device as follows:\n");
  30.                     for (int i = 0; devsp; ++i, devsp = devsp->next)
  31.                     {
  32.                         fprintf(stderr, "\t (%d) %s\n", i, devsp->name? devsp->name:"(NULL)");
  33.                     }
  34.                     pcap_freealldevs(alldevsp);
  35.                     exit(0);
  36.                 }
  37.                 break;

  38.             case 'i':
  39.                 {
  40.                         pcap_if_t *alldevsp = NULL;
  41.                         char *endptr;
  42.                     int device_index = strtol(optarg, &endptr, 10);
  43.                     if (endptr[0] != '\0')
  44.                     {
  45.                         device = optarg;
  46.                     }
  47.                     else
  48.                     {
  49.                         if (pcap_findalldevs(&alldevsp, errbuf) == -1)
  50.                         {
  51.                             fprintf(stderr, "\t #### failed to list the device, err msg: %s\n", strerror(errno));
  52.                             exit(1);
  53.                         }
  54.                         pcap_if_t *devsp = alldevsp;
  55.                         for (; device_index > 0 && devsp; devsp = devsp->next, --device_index);
  56.                         if (devsp) device = devsp->name;
  57.                     }
  58.                     if (device)
  59.                     {
  60.                         fprintf(stderr, "device name: %s\n", device);
  61.                         p = pcap_open_live(device, 1600, 1, -1, errbuf);
  62.                         if (p == NULL)
  63.                         {
  64.                             fprintf(stderr, "\t #### failed to open device (%s), err msg:%s\n", device, strerror(errno));
  65.                             exit(1);
  66.                         }
  67.                     }
  68.                     pcap_freealldevs(alldevsp);
  69.                 }
  70.                 break;
  71.         }
  72.     }

  73.     if (argc != optind + 1)
  74.     {
  75.         fprintf(stderr, "You must specifies the file name to save packets\n");
  76.         exit(-1);
  77.     }
  78.     fname = argv[optind];

  79.     pcap_dumper_t *fp = pcap_dump_open(p, fname);
  80.     if (fp == NULL)
  81.     {
  82.         fprintf(stderr, "\t Failed to open savefile name, err msg: %s\n", strerror(errno));
  83.         exit(-1);
  84.     }
  85.         
  86.     pcap_loop(p, -1, pcap_dump, (char*)fp);
  87.     pcap_dump_close(fp);

  88.     exit(0);
  89. }


阅读(929) | 评论(1) | 转发(0) |
1

上一篇:没有了

下一篇:刻录centos 系统安装盘

给主人留下些什么吧!~~

7大爷2013-08-08 08:57:42

不错~