2016-11-20 7 views
-1

私は現在、データパケットをいくつかのヘッダに分解しています。ここでARPパケットのサイズを取得

は私の現在のコードです:

void analyse(struct pcap_pkthdr *header, const unsigned char *packet, int verbose) { 


    // Define headers and payload 
    const struct ether_header *ethernet = NULL; 
    const struct ether_arp *arp = NULL; 
    const struct ip *ip = NULL; 
    const struct tcphdr *tcp = NULL; 
    const char *payload = NULL; 

    /* Ethernet header is the first data block of packet **/ 
    ethernet = (struct ether_header*) packet; 

    // ARP packet following 
    if(ntohs(ethernet->ether_type) == ETHERTYPE_ARP) { 

     arp = (struct ether_arp*) (packet + ETH_HLEN); 

     // If the operation performed by the sender is a reply, we increment the ARP Response Counter 
     if(ntohs(arp->ea_hdr.ar_op) == 2) { 

      arpResponsesCounter++; 

     } 

    } else { // IP packet following 

     ip = (struct ip*) (packet + ETH_HLEN); 

    } 

    // ARP header and IP header don't have the same size 
    if(arp == NULL) { 

     u_int shift_size = (ip->ip_hl)*4; 

    } else { 


    } 


} 

http://unix.superglobalmegacorp.com/BSD4.4/newsrc/netinet/ip.h.htmlhttp://unix.superglobalmegacorp.com/Net2/newsrc/netinet/if_ether.h.htmlによると、IPヘッダのサイズは(ip->ip_hl)*4;によって与えられているが、私はARPヘッダのサイズを取得する方法を見つけ出すことはできません。 TCPヘッダポインタを正しく定義する必要があります。

ありがとうございました

+1

ARPはTCPとは関係ありません。 ARPパケットの中にTCPセグメントを見つけることはできません。 –

答えて

0

私はあなたが混乱していると思います。 ARPパケットは、ARPヘッダーであるです。 ARPはそれ自体のプロトコルであり、IPのやり方でパケットにペイロードとして他のプロトコルを含んでいません。これは、ICMPがネットワーク層プロトコルであるように、適切にリンク層プロトコルです。どちらもトップレイヤプロトコルであり、どちらも他のプロトコルを持ちません。

ネットワークのレイヤ2およびレイヤ3アドレスのサイズ(イーサネットの場合は48ビット、IPv4の場合は32ビット)を知っていれば、ネットワーク上のARPパケットのサイズを判断できます。

Hardware Type is two octets 
Protocol Type is two octets 
Hardware Address Length is one octet 
Protocol Address Length is one octet 
Operation is two octets 
Sender Hardware Address is Hardware Address Length octets 
Sender Protocol Address is Protocol Address Length octets 
Destination Hardware Address is Hardware Address Length octets 
Destination Protocol Address is Protocol Address Length octets 

基本的に、ハードウェアアドレスの長さの2倍に加え、プロトコルアドレス長の2倍の8オクテットがあります。

イーサネット上のIPv4の場合、これは(8 + (2 * 6) + (2 * 4)) = 28を意味します。

+0

しかし、イーサネット、IPとARPの関係はどうですか? 'の#define \t ETHERTYPE_IP \t 0x0800で\t \t/* IPプロトコル* /' :このヘッダーファイルhttp://unix.superglobalmegacorp.com/Net2/newsrc/netinet/if_ether.h.html二つ定義があるため'#define ETHERTYPE_ARP \t 0x0806 \t \t/* Addr。 私は少し混乱しています –

+0

宛先のレイヤ2アドレスを必要とするレイヤ2プロトコルは、宛先ホストからレイヤ2宛先アドレスを取得するためにARPを使用します。イーサネットはARPを使用して宛先MACアドレスを取得するため、ARPパケットはイーサネットフレームのペイロードになります。ネットワークスタックは、ARPを使用するときに開始され、停止します。 –

関連する問題