Linux环境下libpcap库源代码分析

linux环境下libpcap 源代码分析

韩大卫@吉林师范大学


libpcap 源代码官方下载地址:
git clone https://github.com/the-tcpdump-group/libpcap.git

tcpdumpm源代码官方下载地址:
git clone git://bpf.tcpdump.org/tcpdump

tcpdump.c使用libpcap里的pcap_open_livepcap_loop 完成两个最关键的动作:获取捕获报文的接口,和捕获报文并将报文交给callback(关于tcpdump源代码的构架,请参考作者的tcpdump源代码分析)
 
现结合libpcap源代码分析pcap_open_livepcap_loop的实现机制,并进入linux内核,展示linux内核对这两个API的响应动作。

tcpdump.cpcap_open_live的使用是:

pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); 

pcap_open_live定义如下:

pcap_t *pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf)

source 	为指定的网络接口。
snaplen 	为最大报文长度。
Promisc 	是否将设备设置为混杂模式。
to_ms 	超时时间。
errbuf 	为错误信息描述字符。

返回值为cap_t类型的指针,pcap_t 定义是:

typedef struct pcap pcap_t;
struct pcap {
/*typedef int (*read_op_t)(pcap_t *, int cnt, pcap_handler, u_char *);
read_op为从网络接口读取报文的函数指针,待其得到赋值后,调用实现函数*/
    read_op_t read_op;
 
//从文件里读取报文的函数指针
    int (*next_packet_op)(pcap_t *, struct pcap_pkthdr *, u_char **);
//文件描述符,socket
    int fd;
    int selectable_fd;   
    int bufsize;    //read缓冲区大小
    u_char *buffer; //read缓冲区指针
    u_char *bp;
    int cc;
...
    int snapshot;
    int linktype;       /* Network linktype */
    int linktype_ext;      
    int tzoff;      /* timezone offset */
    int offset;     /* offset for proper alignment */
    int activated;      /* true if the capture is really started */
    int oldstyle;       /* if we're opening with pcap_open_live() */
    struct pcap_opt opt; 
    u_char *pkt;
...
   //激活函数,激活函数在得到调用后,会建立起与底层IPCsocket
    activate_op_t activate_op;
...
};

pcap_t *
pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf){   
    pcap_t *p;
    int status;
   //创建捕获报文的接口句柄
    p = pcap_create(source, errbuf);

    if (p == NULL)
        return (NULL);
    //设置最大报文长度
    status = pcap_set_snaplen(p, snaplen);
    if (status < 0)
        goto fail;
	//将设备设为混杂模式
    status = pcap_set_promisc(p, promisc);
    if (status < 0)
        goto fail;
	//设置超时时间
    status = pcap_set_timeout(p, to_ms);
    if (status < 0)
        goto fail;
    p->oldstyle = 1;
	//pcap_avtivate调用pcap_tactivate_op, 建立起与底层IPC通道
    status = pcap_activate(p);

    if (status < 0)
        goto fail;
    return (p);
...
}

pcap_t *pcap_create(const char *source, char *errbuf){   
    size_t i;
    int is_theirs;
    pcap_t *p;

    if (source == NULL)
        source = "any";

	//capture_source_types数组里寻找是否有特定API集合的接口对应source
    for (i = 0; capture_source_types[i].create_op != NULL; i++) {
        is_theirs = 0;
        p = capture_source_types[i].create_op(source, errbuf, &is_theirs);
        if (is_theirs) {
                return (p);
        }
    }

    //如果没有, 那么就将source作为普通网络接口
    return (pcap_create_interface(source, errbuf));
}
pcap_create_interface() 函数在libpcap下有多个实现,可由编译宏来指定特定的pcap_create_interface来初始化read_op等函数指针。linux环境里默认是libpcap/pcap-linux.c中的 pcap_create_interface():

pcap_t *
pcap_create_interface(const char *device, char *ebuf)
{  
    pcap_t *handle;
    
/*可将 pcap_create_common看做pcap_t结构的构造函数,初始化一个pcap_t*/
    handle = 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
libpcap是一个用于网络数据包捕获的。根据引用\[1\]和引用\[2\]的内容,安装libpcapLinux上需要执行以下步骤: 1. 安装GCC编译器:如果系统中没有安装GCC,可以使用以下命令安装:`yum -y install gcc-c++`。 2. 安装flex:如果系统中没有安装flex,可以使用以下命令安装:`yum -y install flex`。没有安装flex会导致在安装libpcap时出现"Your operating system’s lex is insufficient to compile libpcap"错误。 3. 安装bison:如果已经安装了flex,需要搭配安装bison,可以使用以下命令安装:`yum -y install bison`。如果不安装bison,会出现"don’t have both flex and bison;reverting to lex/yacc"错误。 4. 下载libpcap源码包:从libpcap官方链接下载压缩包,然后解压。 5. 进入解压后的libpcap目录,执行以下命令进行安装: ``` ./configure make make install ``` 6. 编译使用libpcap的程序时,需要添加`-lpcap`参数,例如:`gcc -o device libpcap -lpcap`。如果没有添加`-lpcap`参数,会出现"pcap_lookupdev 未定义的引用"错误。 如果在运行libpcap时出现"error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory"错误,可以根据引用\[1\]中的建议,将libpcap.so.1软链接拷贝到`/usr/lib`目录下,使用以下命令: ``` sudo cp libpcap.so.1 /usr/lib/ ``` 这样就完成了libpcapLinux上的安装和使用。 #### 引用[.reference_title] - *1* [Linux安装libpcap(pcap.h)(以Ubuntu 18.04为例)](https://blog.csdn.net/qq_30902647/article/details/86776612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [linuxlibpcap的安装和简单使用](https://blog.csdn.net/lqw198421/article/details/113268051)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值