请教一个libpcap抓ip包tcp包的问题

ouyadi 2012-11-21 08:11:33
我需要做一个抓包器,代码如下,我的主机ip地址是192.168.1.110,可是结果却是
sudo ./sniffall 0
157.55.56.161 192.168.1.105
192.168.1.105 157.55.56.161
157.55.56.161 192.168.1.105
192.168.1.105 157.55.56.161
157.55.56.161 192.168.1.105

这个不对吧?之前我尝试过用netinet/ip.h和netinet/ether.h下面的struct ip和struct ether来算offset,可是结果的ip更奇怪了,大家看看是哪里出了问题,谢谢!


#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include <netinet/ip.h>
//#include <netinet/if_ether.h>
//#include <netinet/ether.h>

struct ether_header
{
u_int8_t ether_dhost[6];
u_int8_t ether_shost[6];
u_int16_t ether_type;
};

typedef u_int32_t in_addr_t;

struct ip_header
{
#ifdef WORDS_BIGENDIAN
u_int8_t ip_version:4;
u_int8_t ip_header_length:4;
#else
u_int8_t ip_header_length:4;
u_int8_t ip_version:4;
#endif
u_int8_t ip_tos;
u_int16_t ip_length;
u_int16_t ip_id;
u_int16_t ip_off;
u_int8_t ip_ttl;
u_int8_t ip_protocol;
u_int16_t ip_checksum;
struct in_addr ip_source_address;
struct in_addr ip_destination_address;
};


void getPacket(u_char *args, const struct pcap_pkthdr *pkthdr, const u_char *packet){
struct ip_header *ip;
ip = (struct ip_header*)(packet+sizeof(struct ether_header));
char* src = inet_ntoa(ip->ip_source_address);
printf("%s ",src);
char* dst = inet_ntoa(ip->ip_destination_address);
printf(" %s\n", dst);

}
...全文
116 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
科比布莱恩特 2012-11-23
  • 打赏
  • 举报
回复
引用 楼主 ouyadi 的回复:
我需要做一个抓包器,代码如下,我的主机ip地址是192.168.1.110,可是结果却是 sudo ./sniffall 0 157.55.56.161 192.168.1.105 192.168.1.105 157.55.56.161 157.55.56.161 192.168.1.105 192.168.1.105 157.55.56.161 15……
没用过这个库的帮顶~

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧