截获数据包并打印时间和数据包长度。
/* Callback function invoked by libpcap for every incoming packet */
// 定义回调函数,一定要符合接口,这个是麻烦的地方啊。。。
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;
/* convert the timestamp to readable format */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
// strftime是win32的API?或者Linux、Windows下都有这个函数?
// 把ltime里的时间,格式化后,输到字符数组中?
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
// 前面是标准时间,后面是毫秒
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
以下是windows定义的时间结构:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
posted on 2009-09-22 01:59
thinke365 阅读(205)
评论(0) 编辑 收藏 引用 所属分类:
Winpcap