我最近开始使用libpcap & linux专用库和头文件(如netinet/tcp.h)编写数据包嗅探器。 问题是:当我使用TH_OFF(tcp)* 4获取tcp头时,其值通常小于20个字节。好吧,我知道,这是畸形的,但Wireshark显示其他值(20 <)。旗帜也会发生同样的情况。tcp header&flags
下面是代码:
struct nread_tcp {
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
u_short th_seq; /* sequence number */
u_short th_ack; /* acknowledgement number */
#if BYTE_ORDER == LITTLE_ENDIAN
u_int th_x2:4, /* (unused) */
th_off:4; /* data offset */
#endif
#if BYTE_ORDER == BIG_ENDIAN
u_int th_off:4, /* data offset */
th_x2:4; /* (unused) */
#endif
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_NS 0x100
#define TH_RS 0xE00
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
u_char th_offx2; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
};
const struct nread_tcp* tcp = (struct nread_tcp*)(pachet + sizeof(struct ether_header) + sizeof(struct crt_ip));
...
int hlen = TH_OFF(tcp)*4;
char * tmp = new char[strlen("000000000000")+1];
strcpy(tmp,"000000000000");
if (tcp->th_flags & TH_NS){
tmp[3] = '1';
}
else
tmp[3] = '0';
if (tcp->th_flags & TH_ECE){
tmp[4] = '1';
}
else
tmp[4] = '0';
if (tcp->th_flags & TH_CWR){
tmp[5] = '1';
}
else
tmp[5] = '0';
if (tcp->th_flags & TH_URG){
tmp[6] = '1';
}
else
tmp[6] = '0';
if (tcp->th_flags & TH_ACK){
tmp[7] = '1';
}
else
tmp[7] = '0';
if (tcp->th_flags & TH_PUSH){
tmp[8] = '1';
}
else
tmp[8] = '0';
if (tcp->th_flags & TH_RST){
tmp[9] = '1';
}
else
tmp[9] = '0';
if (tcp->th_flags & TH_SYN){
tmp[10] = '1';
}
else
tmp[10] = '0';
if (tcp->th_flags & TH_FIN){
tmp[11] = '1';
}
else
tmp[11] = '0';
我GOOGLE了它颇有几分都没有找到有用的东西。也许这是另一个begginer的错误,但我无法弄清楚。 在此先感谢。
再次,谢谢你的提示/想法。
所有包括处理链路层头的pcap处理函数都是正确的。我的应用程序是用Qt 4.7.4编写的。让我给你更多关于我的逻辑的信息。也许我详细分析了以太网帧头,IP头,UDP头,ARP/RARP头和ICMP头。 至于逻辑,它是这样的: 我有一个线程女巫侦听
-pcap_loop(captureHandler,-1,packetHandler,(unsigned char *)dumpfile); packetHandler位于一个名为“engine.cpp”的单独的.cpp中。在那个.cpp中,我有数据包解释器女巫,根据协议类型,实例化正确的类(tcpPacketHandler for tcp protocol)。 我的tcp结构在engine.h中声明(不是tcpPacketHandler头文件)。 tcp结构是在我最初的问题中发布的。 的tcpPacketHandler构造是这样的:
tcpPacketHandler::tcpPacketHandler(u_char *arg, const pcap_pkthdr * header,const u_char * pachet)
{
(void)arg;
(void)header;
const struct nread_tcp* tcp = (struct nread_tcp*)(pachet + sizeof(struct ether_header) + sizeof(struct crt_ip));
//fprintf(stdout,"\nSEQUENCE NUMBER %u \nACK %u \nWINDOW %u\nURGENT POINTER %u\n", tcp->th_seq, tcp->th_ack, tcp->th_win,tcp->th_urp);
//qDebug() <<sizeof(struct ether_header)<< " "<< sizeof(struct crt_ip);
this->seqNr = ntohs(tcp->th_seq);
this->ackNr = ntohs(tcp->th_ack);
this->window = ntohs(tcp->th_win);
this->urgentPointer = ntohs(tcp->th_urp);
this->portSursa = ntohs(tcp->th_sport);
this->portDestinatie = ntohs(tcp->th_dport);
this->checksum = ntohs(tcp->th_sum);
char * tmp = new char[strlen("000000000000")+1];
strcpy(tmp,"000000000000");
if (tcp->th_flags & TH_NS){
tmp[3] = '1';
}
else
tmp[3] = '0';
if (tcp->th_flags & TH_ECE){
tmp[4] = '1';
}
else
tmp[4] = '0';
if (tcp->th_flags & TH_CWR){
tmp[5] = '1';
}
else
tmp[5] = '0';
if (tcp->th_flags & TH_URG){
tmp[6] = '1';
}
else
tmp[6] = '0';
if (tcp->th_flags & TH_ACK){
tmp[7] = '1';
}
else
tmp[7] = '0';
if (tcp->th_flags & TH_PUSH){
tmp[8] = '1';
}
else
tmp[8] = '0';
if (tcp->th_flags & TH_RST){
tmp[9] = '1';
}
else
tmp[9] = '0';
if (tcp->th_flags & TH_SYN){
tmp[10] = '1';
}
else
tmp[10] = '0';
if (tcp->th_flags & TH_FIN){
tmp[11] = '1';
}
else
tmp[11] = '0';
this->hdrLen = TH_OFF(tcp)*4;
// qDebug() << this->hdrLen;
this->flags = new char[13];
strcpy(this->flags,tmp);
if(tmp)
{
delete [] tmp;
tmp = NULL;
}
}
所有的TCP报头字段显示正确,直到我打在TCP报头数据的其余部分的“数据偏移字段”
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
从这点不符合Wireshark结果(数据偏移量,标志和有效载荷)。
也许问题是nread_tcp结构声明在多个数据包共享的文件中,并且在短时间间隔内捕获大量数据包时指针出错。 我真的没有线索。 我在Google上找到的每一件东西都使用这个结构,但我不知道t know what
是我的代码中的问题。 作为第二个问题:packetHandler函数(来自pcap_loop函数)是否可以声明为类的成员? (因为我注意到它没有类型,当我试图让它成为类成员时,编译器给我一个错误)。
在此先感谢。
谢谢你的提示,但不幸的是,结果是一样的:( – user1300795 2012-03-29 18:14:25
如果你调用'pcap_datalink()'调用'pcap_open_live()','pcap_open_offline(后)'或'pcap_activate()',它会返回什么值,并且您是否正确处理了该类型的链接层头标头,如[tcpdump.org链接层头页面]中所述(http:// www .tcpdump.org/linktypes.html)? – 2012-03-30 03:42:38