'pcap'에 해당되는 글 1건

  1. 2009.09.16 ether_header 구조체와 iphdr 구조체를 이용한 패킷 분석
2009. 9. 16. 19:39

ether_header 구조체와 iphdr 구조체를 이용한 패킷 분석

이전 포스팅에서는 바이트별로 직접 골라냈었는데(-ㅅ-;) 이번엔 라이브러리에 있는 구조체를 활용해서 더 쉽게 패킷을 분석해보자.


ether_header 구조체

/usr/include/net/ethernet.h 에 위치한다. (gcc의 버전에 따라 다를 수 있다.)

/* 10Mb/s ethernet header */
struct ether_header
{
    u_int8_t  ether_dhost[ETH_ALEN];        /* destination eth addr */
    u_int8_t  ether_shost[ETH_ALEN];        /* source ether addr    */
    u_int16_t ether_type;                          /* packet type ID field */
} __attribute__ ((__packed__));



iphdr 구조체

/usr/include/netinet/ip.h 에 위치한다. (역시 gcc의 버전에 따라 다를 수 있다.)

   struct iphdr
      {
   #if __BYTE_ORDER == __LITTLE_ENDIAN
        unsigned int ihl:4;                             /* header length */
        unsigned int version:4;                       /* version */
   #elif __BYTE_ORDER == __BIG_ENDIAN
        unsigned int version:4;                       /* version */
        unsigned int ihl:4;                             /* header length */
   #else
   # error "Please fix <bits/endian.h>"
   #endif
        u_int8_t tos;                                     /* type of service */
        u_int16_t tot_len;                               /* total length */
        u_int16_t id;                                     /* identification */
        u_int16_t frag_off;                              /* fragment offset field */
        u_int8_t ttl;                                       /* time to live */
        u_int8_t protocol;                              /* protocol */
        u_int16_t check;                               /* checksum */
        u_int32_t saddr;                               /* source address */
        u_int32_t daddr;                               /* dest address */
        /*The options start here. */
      };


소스 코드



실행 화면