简体   繁体   English

inet_ntoa 转换问题

[英]inet_ntoa conversion problem

In this snippet of code i capture the packet and i am trying to display the source and destination address by using inet_ntoa, even before that i am printing the packet src and dst address in hexa format.在这段代码中,我捕获了数据包,并尝试使用 inet_ntoa 显示源地址和目标地址,甚至在此之前我以十六进制格式打印数据包 src 和 dst 地址。 The problem here is both do not match, the o/p of inet_ntoa is wrong as shown in o/p这里的问题是两者都不匹配,inet_ntoa的o/p错误如o/p所示

  the src ip address should be 172.28.6.87 but inet_ntoa shows 86.212.172.28
  the src ip address should be 172.28.6.110 but inet_ntoa shows 6.87.172.28


  char *ptr = NULL;
  ptr_fltr = (struct packet_filter*)(packet); 
  memcpy(out_data,packet,50);
  printf("\n");
  for(i= 28;i<36;i++)
  printf("%#x\t",out_data[i]);
  printf("*******************************************************************\n");
  printf("---------------------Received Packet Info--------------------\n");
  ptr = inet_ntoa(ptr_fltr->ip.ip_src);
  printf("Source Ip Addr :%s\n",ptr);

here这里

 struct packet_filter
  {
    struct mac_filter mac;
    struct ip_filter ip;
    union {
            struct udp_filter proto;
    }protocol;
  }__attribute__((packed));


 struct ip_filter
 {
    u_char ip_vhl;
    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 */
    u_char ip_ttl; /* time to live */
    u_char ip_p; /* protocol */
    u_short ip_sum; /* checksum */
    struct in_addr ip_src; /* source and dest address */
    struct in_addr ip_dst; /* source and dest address */
 }__attribute__((packed));

output output

  0xac    0x1c    0x6 0x57    0xac    0x1c    0x6 0x6e         
  ************************************************************
  --------------------Received Packet Info--------------------
  Source Ip Addr :86.212.172.28
  Destination Ip Addr :6.87.172.28

Clearly your struct is off by two bytes by the time you get to the IP addresses.显然,当您到达 IP 地址时,您的结构已关闭两个字节。 I've checked against the IPv4 protocol and that bit looks OK.我已经检查了 IPv4 协议,该位看起来不错。 So I suspect the struct mac is wrong.所以我怀疑struct mac是错误的。 I presume struct mac is meant to be an ethernet frame.我认为struct mac是一个以太网框架。 If so, it's already a bit suspicious because an Ethernet frame is not of a fixed length.如果是这样,那已经有点可疑了,因为以太网帧不是固定长度的。

Also, (assuming you are getting these from the Berkeley Packet Filter) make sure you calculate the start of the packet correctly from the bpf header (you can't rely on sizeof(struct bpf_header) ).此外,(假设您从 Berkeley Packet Filter 获取这些信息)确保您从 bpf header 正确计算数据包的开始(您不能依赖sizeof(struct bpf_header) )。

Your IP packet starts at offset 16, and if you have copied struct mac from ethernet header it is 14 bytes long.您的 IP 数据包从偏移量 16 开始,如果您从以太网 header 复制了struct mac ,它的长度为 14 个字节。 Looks like there is some unexpected data in packet.数据包中似乎有一些意外数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM