功用
Used to Sniffer packets. You can specify which kind of packets should be handled.
Library
pcap.a
Example
void nami_pcap_example_main(char *dev_name_arg )
{
char *dev_name, errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 net, mask;
pcap_t *pcap_handler;
struct bpf_program pcap_fp;
struct pcap_pkthdr header;
if( dev_name_arg == NULL )
{
/* use pcap_lookupdev to get a default device */
dev_name = pcap_lookupdev(errubuf);
if ( dev_name == NULL ) { /* error handler */ }
} else
{
dev_name = dev_name_arg;
}
/* get the IP and mask of the iface, if net and mask is not required, can skip this step */
if ( pcap_lookupnet( dev, &net, &mask, errbuf ) < 0 ) { /* error handler */ }
/* open a device for capturing */
pcap_handler = pcap_open_live( dev, /* snaplen: snapshot len */ 1518,
/* promisc: specifies if the interface is to be put into promiscuous mode*/ 1,
/* to_ms: the read timeout in milliseconds */ 1000, errbuf );
if ( pcap_handler == NULL ) { /* error handler */ }
if ( pcap_compile( pcap_handler, &pcap_fp, pcap_filter_exp, 0,
PCAP_NETMASK_UNKNOWN) < 0 ) { /* errbuf */ }
if ( pcap_setfilter( pcap_hander, &pcap_fp ) < 0 ) { /* error handler */ }
pcap_loop ( pcap_handler, -1, nami_sniffer_handler, NULL );
return ;
}
void
Reference