安装库
apt-get install libpcap-dev
获取网络号(ip地址)和掩码
- int pcap_lookupnet(char* device,bpf_u_int32 *netp,bpf_u_int32 *maskp,char *errbuf);
- 功能:获取指定网卡的ip地址,子网掩码
- 参数:device:网络设备名,为第一步获取的网络接口字符串,也可以人为指定,如“eth0”;
- netp:存放ip地址的指针,buf_u_int32为32位无符号整型
- maskp:存放子网掩码的指针
- errbuf:存放出错信息
- 返回值:成功返回0,失败返回1
函数名称:pcap_t *pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
函数功能:获得用于捕获网络数据包的数据包捕获描述字。
参数说明:device 参数为指定打开的网络设备名。snaplen参数定义捕获数据的最大字节数。promisc指定是否将网络接口置于混杂模式。to_ms参数指*定超时时间(毫秒)。ebuf参数则仅在pcap_open_live()函数出错返回NULL时用于传递错误消息。
https://stackoverflow.com/questions/20003690/what-is-pcap-pcap-t-errbufpcap-errbuf-size
https://baike.baidu.com/item/pcap/6226076