用winpcap截获的数据包,请问怎样取出包中的目标IP地址?
最好能提供源程序
谢谢

解决方案 »

  1.   

    照着testApp的例子,先PacketReceivePacket,然后可以得到IP头,照着IP结构分析如下:
    typedef struct _IP_HEADER
    {
    #if defined(__LITTLE_ENDIAN_BITFIELD)
    __u8         ihl:4,
                version:4;
    #elif defined (__BIG_ENDIAN_BITFIELD)
    __u8         version:4,
                   ihl:4;
    #endif
        __u8            tos;
        __u16           tot_len;
        __u16           id;
        __u16           frag_off;
        __u8            ttl;
        __u8            protocol;
        __u16           check;
        __u32           saddr;
        __u32           daddr;
    }               IP_HEADER;
    取得daddr即为包中的目标IP地址
      

  2.   

    定义一个ip报文头的结构:
    struct ip
      {
        unsigned int ip_hl:4;               /* header length */
        unsigned int ip_v:4;                /* version */
        u_char ip_tos;                    /* type of service */
        u_short ip_len;                     /* total length */
        u_short ip_id;                      /* identification */
        u_short ip_off;                     /* fragment offset field */
    #define IP_RF 0x8000                    /* reserved fragment flag */
    #define IP_DF 0x4000                    /* dont fragment flag */
    #define IP_MF 0x2000                    /* more fragments flag */
    #define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
        u_char ip_ttl;                    /* time to live */
        u_char ip_p;                      /* protocol */
        u_short ip_sum;                     /* checksum */
        struct in_addr ip_src, ip_dst;      /* source and dest address */
      };用这个函数可以显示报文的ip地址:
    bp是用winpcap截到的报文,length是报文长度:void
    print_ip(unsigned char *bp, u_short length)
    {
      struct ip *iph;
      u_int ip_off, ip_hl, ip_len;  iph = (struct ip *)bp;  if (length < IP_H) {
        printf("truncated-ip %d", length);
        return;
      }
      ip_hl = iph->ip_hl * 4;
      //ip_len = ntohs(iph->ip_len);
      ip_len = iph->ip_len;  if (length < ip_len) {
        printf("truncated-ip - %d bytes missing!", ip_len - length);
        return;
      }
      ip_off = ntohs(iph->ip_off);  /* Handle first fragment. */
      if ((ip_off & IP_OFFMASK) == 0) {
        switch (iph->ip_p) {    default:
          printf("%s > %s:", inet_ntoa(iph->ip_src),
         inet_ntoa(iph->ip_dst));
          printf(" ip-proto-%d %d", iph->ip_p, ip_len);
          break;
        }
      }
      /* Handle more frags. */
      if (ip_off & (IP_MF|IP_OFFMASK)) {
        if (ip_off & IP_OFFMASK)
          printf("%s > %s:", inet_ntoa(iph->ip_src),
         inet_ntoa(iph->ip_dst.s_addr));
        
        printf(" (frag %d:%d@%d%s)", ntohs(iph->ip_id), ip_len - ip_hl,
       (ip_off & IP_OFFMASK) << 3, (ip_off & IP_MF) ? "+" : "");
      }
      /* Handle don't frags. */
      else if (ip_off & IP_DF) printf(" (DF)");
        
      if (iph->ip_tos) printf(" [tos 0x%x]", (int)iph->ip_tos);
      if (iph->ip_ttl <= 1) printf(" [ttl %d]", (int)iph->ip_ttl);
    }
      

  3.   

    比如包在const char * p中前面14是以太头。p[12]*0x100 + p[13] == 0x0800时是IP包
    然后
    UINT sip     = *(UINT *)&p[12 + 14];
    UINT dip     = *(UINT *)&p[16 + 14];找本《TCP/IP协议》看看就行了