2011-07-08 8 views
0

このコードでは、パケットをキャプチャし、パケットsrcとdstアドレスをhexa形式で出力する前に、inet_ntoaを使用して送信元と宛先のアドレスを表示しようとしています。ここでの問題はinet_ntoa変換の問題

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)); 

出力

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 
+0

ちょっと質問:なぜ[libpcap](http://www.tcpdump.org/)を使用しないのですか? –

答えて

2

は明らかにあなたの構造体であるO/Pに示すようにINET_NTOAのO/pは間違っている、両者が一致していないですIPアドレスを取得するまでに2バイトずつオフになります。私はIPv4プロトコルをチェックして、そのビットはOKと思われます。だから私はstruct macが間違っていると思う。私はstruct macがイーサネットフレームであることを前提としています。その場合は、Ethernet frameの長さが固定されていないため、既に疑いがあります。

また、(Berkeley Packet Filterからこれらを受け取ったと仮定して)、bpfヘッダーから正しくパケットの開始を計算してください(sizeof(struct bpf_header)に依存できません)。

0

あなたのIPパケットは、オフセット16から始まり、struct macをイーサネットヘッダーからコピーした場合、それは14バイトです。パケットに予期しないデータがあるように見えます。