Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4252923
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: Mysql/postgreSQL

2011-08-13 12:40:36

1. libpcap 的下载



  解压: ywx@ywx:~/yu$ tar zxfv libpcap-1.1.1.tar.gz

2. ./configure 配置文件
   make
   make install
  注意在./configure 过程中有错误,安装
    apt-get install flex

  默认安装在了/usr/下
  最后安装在了 /usr/local/lib/libpcap.so


3. 代码编写:

  代码附件:
 testlibcap.rar  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pcap.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>

  8. int main(int argc, char *argv[])
  9. {
  10.     char *dev;
  11.     char *net;
  12.     char *mask;
  13.     int ret;
  14.     char errbuf[PCAP_ERRBUF_SIZE];
  15.     bpf_u_int32 netp;
  16.     bpf_u_int32 maskp;
  17.     struct in_addr addr;
  18.     dev = pcap_lookupdev(errbuf);
  19.     if(dev == NULL)
  20.     {
  21.         printf("%s\n",errbuf);
  22.         exit(1);
  23.     }
  24.     printf("device name is :%s\n",dev);
  25.     ret =pcap_lookupnet(dev, &netp, &maskp, errbuf);
  26.     if(ret == -1)
  27.     {
  28.         printf("%s\n",errbuf);
  29.         exit(1);
  30.     }
  31.     addr.s_addr =netp;
  32.     net = inet_ntoa(addr);
  33.     if(net == NULL)
  34.     {
  35.         perror("inet_ntoa");
  36.         exit(1);
  37.     }

  38.     printf("IP address:%s\n",net);
  39.     addr.s_addr = maskp;
  40.     mask = inet_ntoa(addr);
  41.     if(mask ==NULL)
  42.     {
  43.         perror("inet_ntoa");
  44.         exit(1);
  45.     }
  46.     printf("wangluo yanma:%s\n",mask);

  47.     exit(EXIT_SUCCESS);

  48. }



4.编译运行
  1. 应该需要设置环境变量,
  2. root@ywx:/home/ywx/yu/libpcap# ln -s /usr/local/lib/libpcap.so.1 /usr/lib/

  3. root@ywx:/home/ywx/yu/libpcap# gcc -o testlibcap testlibcap.c -lpcap
  4. root@ywx:/home/ywx/yu/libpcap# ./testlibcap
  5. device name is :eth0
  6. IP address:192.168.244.0
  7. wangluo yanma:255.255.255.0


阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~