查看: 10648|回复: 2

ICMP报头分析问题!急!

[复制链接]
发表于 2006-7-1 10:13:11 | 显示全部楼层 |阅读模式
在ping程序的ICMP报头解析过程中:void CPing:ecodeICMPHeader(char *buf, int bytes, SOCKADDR_IN *from)
{
   IpHeader       *iphdr = NULL;
    IcmpHeader     *icmphdr = NULL;
    unsigned short  iphdrlen;
    DWORD           tick;
    static   int    icmpcount = 0;

    iphdr = (IpHeader *)buf;
        // Number of 32-bit words * 4 = bytes
    iphdrlen = iphdr->h_len * 4;
tick = GetTickCount();

    if ((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
        DecodeIPOptions(buf, bytes);

        CString temp;
    if (bytes  < iphdrlen + ICMP_MIN)
    {
                temp.Format("Too few bytes from %s \r\n",inet_ntoa(from->sin_addr));
                m_dlg->m_result+=temp;
                m_dlg->SetDlgItemText(IDC_EDIT2,m_dlg->m_result);
    }
    icmphdr = (IcmpHeader*)(buf + iphdrlen);
这里 iphdrlen = iphdr->h_len * 4;和  icmphdr = (IcmpHeader*)(buf + iphdrlen);
这两条语句是什么意思啊?
我看了很长时间也不明白,请各位高手指教!
回复

使用道具 举报

发表于 2006-7-2 17:50:55 | 显示全部楼层
iphdrlen = iphdr->h_len * 4 是因为IP头中IP头长度中所包含的数据的是以32位为一个单位
就是说iphdr->h_len=1,则是32位,即为4个字节。
所以,要获得IP头长度的字节数,则需*4
后面一个我不知道
回复

使用道具 举报

 楼主| 发表于 2006-7-4 19:52:00 | 显示全部楼层
呵呵,先要谢谢楼上说的,但我还是没有明白。IP头是这样定义的
typedef struct _IPHEADER {
        unsigned char  header_len:4;
        unsigned char  version:4;   
        unsigned char  tos;            // type of service
        unsigned short total_len;      // length of the packet
        unsigned short ident;          // unique identifier
        unsigned short flags;         
        unsigned char  ttl;            
        unsigned char  proto;          // protocol ( IP , TCP, UDP etc)
        unsigned short checksum;      
        unsigned int   sourceIP;
        unsigned int   destIP;

}IPHEADER;
  unsigned char   这个不是占一个字节吗?
怎么又是32位,四个字节呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | CSNA会员注册

本版积分规则

快速回复 返回顶部 返回列表