常用函数和结构体
/* * Item in a list of interfaces. */
struct pcap_if {
struct pcap_if *next;
char *name; /* name to hand to "pcap_open_live()" */
char *description; /* textual description of interface, or NULL */
struct pcap_addr *addresses;
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
};
typedef struct pcap_if pcap_if_t;
int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf);
void pcap_freealldevs(pcap_if_t *);
/* * Representation of an interface address. */
struct pcap_addr {
struct pcap_addr *next;
struct sockaddr *addr; /* address */
struct sockaddr *netmask; /* netmask for that address */
struct sockaddr *broadaddr; /* broadcast address for that address */
struct sockaddr *dstaddr; /* P2P destination address for that address */
};
typedef struct pcap_addr pcap_addr_t;
struct sockaddr_in {
short sin_family; //协议族 u_short sin_port; //协议端口 struct in_addr sin_addr; // char sin_zero[8];
};
struct sockaddr {
u_short sa_family;
char sa_data[14];
}; //通用的socket地址
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
#define s_addr S_un.S_addr
#define s_host S_un.S_un_b.s_b2
#define s_net S_un.S_un_b.s_b1
#define s_imp S_un.S_un_w.s_w2
#define s_impno S_un.S_un_b.s_b4
#define s_lh S_un.S_un_b.s_b3
};
如上所示,上面定义了三种结构体:sockaddr_in、sockaddr和in_addr。简单说一下这三个结构体,sockaddr是通用的socket地址,而sockaddr_in是具体到internet的socket地址,两者之间可以进行类型转换。而in_addr结构体就是32位IP地址。因为这些其实都是socket编程中的知识,所以只是稍微提一下,感兴趣的可以细心研究一下。
pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf);
int pcap_loop ( pcap_t * p,
int cnt,
pcap_handler callback,
u_char * user
)
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
const u_char *);
int pcap_next_ex ( pcap_t * p,
struct pcap_pkthdr ** pkt_header,
const u_char ** pkt_data
)
/* * Generic per-packet information, as supplied by libpcap. * * The time stamp can and should be a "struct timeval", regardless of * whether your system supports 32-bit tv_sec in "struct timeval", * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit * and 64-bit applications. The on-disk format of savefiles uses 32-bit * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit * and 64-bit versions of libpcap, even if they're on the same platform, * should supply the appropriate version of "struct timeval", even if * that's not what the underlying packet capture mechanism supplies. */
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
struct timeval
{
time_t tv_sec; /*seconds, 秒*/
suseconds tv_usec; /*microseconds, 微秒*/
}
timeval表示一个时间点,用秒、微秒来定义时间点。
int pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
bpf_u_int32);
/* * Structure for "pcap_compile()", "pcap_setfilter()", etc.. */
struct bpf_program {
u_int bf_len;
struct bpf_insn *bf_insns;
};
/* * The instruction data structure. */
struct bpf_insn {
u_short code;
u_char jt;
u_char jf;
bpf_u_int32 k;
};
int pcap_setfilter(pcap_t *, struct bpf_program *);
typedef struct pcap pcap_t;
int pcap_datalink(pcap_t *);
pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
typedef struct pcap_dumper pcap_dumper_t;
void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
int pcap_createsrcstr(char *source, int type, const char *host, const char *port, const char *name, char *errbuf);
int pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks);
int pcap_sendpacket(pcap_t *, const u_char *, int);
17. pcap_sendqueue_alloc() 创建发送队列
pcap_sendqueue_queue()向队列加入要发送的数据包
pcap_sendqueue_transmit()发送数据队列
pcap_sendqueue_destroy() 释放数据队列
pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);
void pcap_sendqueue_destroy(pcap_send_queue* queue);
18. pcap_send_queue结构体,发送数据报队列的数据结构
/*! \brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit(). */
struct pcap_send_queue
{
u_int maxlen; ///< Maximum size of the the queue, in bytes. This variable contains the size of the buffer field. u_int len; ///< Current size of the queue, in bytes. char *buffer; ///< Buffer containing the packets to be sent. };
typedef struct pcap_send_queue pcap_send_queue;
19. pcap_setmode() 设置适配器工作模式
int pcap_setmode(pcap_t *p, int mode);
常用字段含义
PCAP_ERRBUF_SIZE libpcap错误信息缓冲的大小,值为256
PCAP_BUF_SIZE 最大缓冲大小,值为1024
PCAP_SRC_IF_STRING rpcap源字符串格式,值为“rpcap://”
PCAP_IF_LOOPBACK 用来判断回环地址,值为0x00000001
PCAP_OPENFLAG_PROMISCUOUS 混杂模式,值为1
DLT_EN10MB 链路层为以太网