簡體   English   中英

inet_ntoa 轉換問題

[英]inet_ntoa conversion problem

在這段代碼中,我捕獲了數據包,並嘗試使用 inet_ntoa 顯示源地址和目標地址,甚至在此之前我以十六進制格式打印數據包 src 和 dst 地址。 這里的問題是兩者都不匹配,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);

這里

 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

  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

顯然,當您到達 IP 地址時,您的結構已關閉兩個字節。 我已經檢查了 IPv4 協議,該位看起來不錯。 所以我懷疑struct mac是錯誤的。 我認為struct mac是一個以太網框架。 如果是這樣,那已經有點可疑了,因為以太網幀不是固定長度的。

此外,(假設您從 Berkeley Packet Filter 獲取這些信息)確保您從 bpf header 正確計算數據包的開始(您不能依賴sizeof(struct bpf_header) )。

您的 IP 數據包從偏移量 16 開始,如果您從以太網 header 復制了struct mac ,它的長度為 14 個字節。 數據包中似乎有一些意外數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM