*一:int pcap_findalldevs(pcap_if_t , char )
说明:用来获得网卡的列表
参数:指向pcap_if_t类型的列表的指针的指针; char型指针,当打开列表错误时返回错误信息
返回值:为int型,当显示列表失败时返回-1
pcap_if结构体如下:
struct pcap_if
{
struct pcap_if *next;
char *name;
char *description;
struct pcap_addr *addresses; //pcap_addr 结构体
bpf_u_int32 flags;
};
pcap_addr 结构体如下:
struct pcap_addr
{
struct pcap_addr *next;
struct sockaddr *addr;
struct sockaddr *netmask;
struct sockaddr *broadaddr;
struct sockaddr *dstaddr;
};
事例:
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[64];
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
for(d=alldevs;d;d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
*二:void pcap_freealldevs(pcap_if_t )
说明:与int pcap_findalldevs(pcap_if_t **, char *)配套使用,当不再需要网卡列表时,用此函数free释放空间
参数:打开网卡列表时申请的pcap_if_t型的指针
事例: pcap_freealldevs(alldevs);
**三:pcap_t pcap_open_live(const char * device, int snaplen, int promisc, int to_ms, char ebuf )
说明:被用来得到一个包抓取得描述符
参数:
device是一个指出要抓取的网络设备的字符串。
snaplen指明最大可抓取的字节长度。
promisc置位表明该接口要被设置成混杂模式。
to_ms以毫秒为单位设置超时时间。当在超时时间内网卡上没有数据到来时对网卡的读操作将返回(如 pcap_dispatch() or pcap_next_ex()等函数)。
ebuf被用来存放当pcap_open_live()调用失败时,返回的错误字符串。
返回值:pcap_t型的指针,供pcap_dispatch() or pcap_next_ex()等函数调用。
pcap_t的结构体:
struct pcap {
#ifdef WIN32
ADAPTER *adapter;
LPPACKET Packet;
int timeout;
int nonblock;
#else
int fd;
#endif
int snapshot;
int linktype;
int tzoff;
int offset;
struct pcap_sf sf;
struct pcap_md md;
int bufsize;
u_char *buffer;
u_char *bp;
int cc; //Place holder for pcap_next().
u_char *pkt; //Placeholder for filter code if bpf not in kernel.
struct bpf_program fcode;
char errbuf[PCAP_ERRBUF_SIZE + 1];
int dlt_count;
int *dlt_list;
#ifdef REMOTE
int rmt_clientside;
SOCKET rmt_sockctrl; //!< socket ID of the socket used for the control connection
SOCKET rmt_sockdata; //!< socket ID of the socket used for the data connection
pthread_t rmt_threaddata; //!< handle to the receiving thread, we need to kill it in case of 'pcap_clos()'
int rmt_flags; //!< we have to save flags, since they are passed by the pcap_open_live(), but they are used by the pcap_start capture()
int rmt_capstarted; //!< 'true' if the capture is already started (needed to knoe if we have to call the pcap_startcapture()
struct pcap_pkthdr pcap_header; //!< In Linux, you have to copy the packet headers another time before giving them to the user
#endif
};
**四: int pcap_loop(pcap_t p, int cnt, pcap_handler callback, u_char user);
说明:捕获数据包;不会响应pcap_open_live()中设置的超时时间
参数:
p是由pcap_open_live()返回的所打开网卡的指针
cnt用于设置所捕获数据包的个数
packet_handler是与void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)配合使用的一个参数。回调函数。
user值一般为NULL
*五:int pcap_dispatch(pcap_t * p, int cnt, pcap_handler, u_char user)
说明:捕获数据包。可以不被阻塞
参数:与pcap_loop()相同
**六:int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, u_char pkt_data);
说明:捕获数据包,与pcap_ dispatch() pcap_loop()很相似。pcap_next_ex()允许直接调用来接收包,它的参数和pcap_loop()相同:有一个网卡描述副,和两个指针,这两个指针会被初始化并返回给用户,一个是pcap_pkthdr结构,另一个是接收数据的缓冲区。
参数:
p是由pcap_open_live()返回的所打开网卡的指针
pcap_pkthdr型的结构体,存储时间,包的长度
pkt_data存储数据包的内容,为一个char型数组
结构体:
struct pcap_pkthdr
{
struct timeval ts;
bpf_u_int32 caplen;
bpf_u_int32 len;
};
返回值:当等于1时成功;等于0时超时;等于-1时说明发生错误,错误信息用pcap_geterr(adhandle)获得
**七:int pcap_compile(pcap_t *p, struct bpf_program fp, char str, int optimize,bpf_u_int32 netmask)
说明:编译一个过滤设备,它通过一个高层的boolean型变量和字串产生一系列的能够被底层驱动所解释的二进制编码。boolean表示语法能够在这个文件的过滤表示语法中找到。与pcap_setfilter()配合使用。
参数:
p是打开网卡时返回的网卡指针
fp用于与pcap_setfilter()传递过滤信息。
str是一个字符串。
optimize指明是否需要优化编译结果。
netmask子网掩码
返回值:发生错误是返回-1
*八:int pcap_setfilter ( pcap_t p, struct bpf_program * fp )
说明:pcap_setfilter() 用来联系一个在内核驱动上过滤的过滤器,这时所有网络数据包都将流经过滤器,并拷贝到应用程序中。
参数:
p是打开网卡时返回的网卡指针
fp是pcap_compile()传递过来的参数
返回值: 错误时返回-1
**九:int pcap_sendpacket(pcap_t p, u_char buf, int size)
说明:手工发送一个数据包了。这个函数需要的参数:一个装有要发送数据的缓冲区,要发送的长度,和一个适配器。注意缓冲区中的数据将不被内核协议处理,只是作为最原始的数据流被发送,所以我门必须填充好正确的协议头以便正确的将数据发送。
参数:
p是打开网卡时返回的网卡指针
buf是发送数据包的内容缓冲区首地址
size是发送数据包的大小
十:发送数据包有关的几个函数
1,pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
说明:给数据包队列分配空间
参数:memsize队列缓冲区的大小
返回值:pcap_send_queue指针
结构体:
struct pcap_send_queue
{
u_int maxlen;
u_int len; //< Current size of the queue, in bytes.
char *buffer; //< Buffer containing the packets to be sent.
};
2,int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
说明:填充队列
参数:
queue 是由pcap_sendqueue_alloc()返回的指针
pkt_header是数据包头
pkt_data是数据包内容缓冲区指针
返回值:错误是返回-1
3,u_int pcap_sendqueue_transmit(pcap_t p, pcap_send_queue queue, int sync)
说明:发送队列数据
参数:
pcap_t是 pcap_open_live()函数打开的网卡指针
queue 是由pcap_sendqueue_alloc()返回的指针
sync是同步设置。如果非零,那么发送将是同步的,这将站用很大的CPU资源,因为发生在内核驱动的同步发送是通过"brute force" loops的,但是一般情况下能够精确到微秒。
返回值:错误返回是-1
4,void pcap_sendqueue_destroy(pcap_send_queue* queue);
说明:释放队列
参数:queue 是由pcap_sendqueue_alloc()返回的指针