관리 메뉴

kisoo

Packet 구조 및 구조체 본문

01.About Programming /1.Network Lab

Packet 구조 및 구조체

JamesK78 2010. 11. 17. 13:28

Packet 구조 구조체

 

0

 

 

 

 

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

24

 

 

 

 

 

 

32

~47

Ethernet
Header
(14 Byte)

Destination MAC Address(6 Byte)

Source MAC Address(6 Byte)

TYPE(2 Byte)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

IP
Header
(20 Byte)

Version

Header
Length

Type Of
Service

Total Length

 

 

 

 

Identification

Flag

Fragment Offset

 

 

 

 

Time To Live

Protocol

Header Checksum

 

 

 

 

Source IP Address

 

 

 

 

Destination IP Address

 

 

 

 

TCP
Header
(20 Byte)

Source Port

Destination Port

 

 

 

 

Sequence Number

 

 

 

 

Acknowledgement Number

 

 

 

 

Data
Offset

 

 

 

 

 

 

Flags

Window

 

 

 

 

Checksum

Urgent Pointer

 

 

 

 

Application
(0~1460)

DATA

 

 

 

 

Packet Format

 

BSD 계열

POSIX Standard

C 언어

설명

바이트

char

int8_t

char

부호가 붙은 정수

1byte

short

int16_t

short

부호가 붙은 정수

2byte

int

int32_t

int

부호가 붙은 정수

4byte

u_char

u_int8_t

unsigned char

부호가 붙지 않은 정수

1byte

u_short

u_int16_t

unsigned short

부호가 붙지 않은 정수

2byte

u_int

u_int32_t

unsigned int

부호가 붙지 않은 정수

4byte

BSD, POSIX, C언어 Data type 정의

 

/* Ethernet Header Structure */

#define  ETHER_ADDR_LEN 6

 

struct     ether_header {

             u_char    ether_dhost[ETHER_ADDR_LEN];

             u_char    ether_shost[ETHER_ADDR_LEN];

             u_short ether_type;

};

TYPE Field

#define  ETHERTYPE_IP     0x0800   /* IP Protocol */

#define  ETHERTYPE_ARP   0x0806   /* Address Resolution Protocol */

#define ETHERTYPE_REVARP          0x8035   /* reverse Address Resolution Protocol */

 

 

 

 

ARP & RARP ( Address Resolution Protocol & reverse Address Resolution Protocol )

ARP Packet = arphdr 구조체 + ether_arp 구조체

 

0

 

 

 

 

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

 

 

32

47

ether_arp

hardware address format
arp_hrd

protocol address format
arp_pro

a
r
p
h
d
r

 

 

 

hardware length
arp_hln

protocol length
arp_pln

operation
arp_op

 

 

 

sender hardware address(MAC Address)
arp_sha

 

 

sender protocol address(IP Address)
arp_spa

 

 

 

 

target hardware address(MAC Address)
arp_tha

 

 

target protocol address(IP Address)
arp_tpa

 

 

 

 

ARP Packet Format

 

/* ARP packet structure */

struct     arphdr {

             u_short   ar_hrd;    /* format of hardware address */

             u_short   ar_pro;    /* format of protocol address */

             u_char    ar_hln;    /* length of hardware address */

             u_char    ar_pln;    /* length of protocol address */

             u_short   ar_op;     /* one of: */

};

struct     ether_arp {

             struct arphdr ea_hdr;                                     /* ARPfixed-size header */

             u_char    arp_sha[ETHER_ADDR_LEN];             /* sender hardware address */

u_char    arp_spa[4];                        /* sender protocol address */

             u_char    arp_tha[ETHER_ADDR_LEN];             /* target hardware address */

             u_char    arp_tpa[4];                        /* target hardware address */

};

 

#define arp_hrd   ea_hdr.ar_hrd

#define  arp_pro   ea_hdr.ar_pro

#define  arp_hln   ea_hdr.ar_hlen

#define  arp_pln   ea_hdr.ar_pln

#define  arp_op    ea_hdr.ar_op

 

 

 

 

 

 

 

 

 

IP Protocol

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

IP
Header
(20 Byte)

Version

Header
Length

Type Of
Service

Total Length

Identification

Flag

Fragment Offset

Time To Live

Protocol

Header Checksum

Source IP Address

Destination IP Address

IP header Format

 

/* IP header Structure */

struct     ip {

#if          BYTE_ORDER == LITTLE_ENDIAN

             u_int      ip_hl:4,                /* header length */

                           ip_v:4;                 /* version */

#endif

#if          BYTE_ORDER== BIG_ENDIAN

             u_int      ip_v:4,                 /* version */

                           ip_hl:4;                 /* header length */

#endif

             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 address */

             struct     in_addr   in_dst;    /* destination address */

};

/* IP Header Fragment Offset */

#define IP_RF       0x8000   /* reserved fragment flag */

#define IP_DF       0x4000   /* don’t fragment flag */

#define IP_MF      0x2000   /* more fragment flag */

#define IP_OFFMASK          0x1fff     /* mask for fragmenting bits */

 

/* Protocol number */

번호

프로토콜 이름

               

1

ICMP

Internet Control Management Protocol

2

IGMP

Internet Group Management Protocol

4

IP

IP in IP (encapsulation)

6

TCP

Transmission Control Protocol

8

EGP

Exterior Gateway Protocol

17

UDP

User Datagram Protocol

41

IPv6

IP version 6

46

RSVP

Resource ReSerVation Protocol

89

OSPF

Open Shortest Path First

ICMP Protocol

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

ICMP
에코 응답

에코 요구

Header

Type
icmp_type

Code
icmp_code

Checksum
icmp_cksum

Identification
icmp_id

Sequence Number
icmp_seq

icmp_data

ICMP echo 응답, echo 요구 header Format

 

/* ICMP 에코 요구, 에코 응답 패킷 구조체

struct     icmp {

             u_char    icmp_type;                         /* type of message */

             u_char    icmp_code;           /* type sub code */

             u_short   icmp_cksum;         /* ones complement cksum of struct */

             n_short   icmp_id;

             n_short   icmp_seq;

             char        icmp_date[1];

};

/*

icmp_type 값은 8이며 icmp_code 값은 0 포함된다.

*/

 

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

ICMP
도달 불능
Header

Type
icmp_type

Code
icmp_code

Checksum
icmp_cksum

미사용
icmp_pmvoid

다음의 MTU
icmp_nextmtu

Icmp_data

ICMP 도달 불능 header Format

 

/* ICMP 도달 불능 패킷 구조체

struct     icmp {

             u_char    icmp_type;                         /* type of message */

             u_char    icmp_code;           /* type sub code */

             u_short   icmp_cksum;         /* ones complement cksum of struct */

             n_short   icmp_pmvoid;

             n_short   icmp_nextmtu;

             char        icmp_date[1];

};

/*

icmp_type 값은 3

icmp_code

#define  ICMP_UNREACH_NET                        0            /* 네트워크 도달 불능 */

#define  ICMP_UNREACH_HOST        1            /* 호스트 도달 불능 */

#define  ICMP_UNREACH_PROTOCOL 2            /* 프로토콜 도달 불능 */

#define  ICMP_UNREACH_PORT         3            /* 포트 도달 불능 */

#define  ICMP_UNREACH_NEEDFRAG  4            /* 프래크먼트가 필요하거나 DF 세트되어 있다. */

#define  ICMP_UNREACH_SRCFAIL     5            /* 소스 라우팅 실패 */

*/

 

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

ICMP
Redirect
Header

Type
icmp_type

Code
icmp_code

Checksum
icmp_cksum

Gateway Internet Address
icmp_gwaddr

icmp_data

ICMP Redirect header Format

 

/* ICMP Redirect 패킷 구조체

struct     icmp {

             u_char    icmp_type;                         /* type of message */

             u_char    icmp_code;           /* type sub code */

             u_short   icmp_cksum;         /* ones complement cksum of struct */

             struct     in_addr   icmp_gwaddr

             char        icmp_date[1];

};

/*

icmp_type 값은 5

icmp_code

#define  ICMP_REDIRECT_NET          0            /* 네트워크 어드레스 리다이렉트 */

#define  ICMP_REDIRECT_HOST        1            /* 호스트 어드레스 리다이렉트 */

#define  ICMP_REDIRECT_TOSNET    2            /* 네트워크와 TOS 대한 리다이렉트 */

#define  ICMP_REDIRECT_TOSHOST  3            /* 포트 어드레스와 TOS 대한 리다이렉트 */

*/

 

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

ICMP
시간 초과
Header

Type
icmp_type

Code
icmp_code

Checksum
icmp_cksum

미사용
icmp_void

icmp_data

ICMP 시간 초과 Format

 

/* ICMP 시간 초과 패킷 구조체

struct     icmp {

             u_char    icmp_type;                         /* type of message */

             u_char    icmp_code;           /* type sub code */

             u_short   icmp_cksum;         /* ones complement cksum of struct */

             n_int      icmp_void;

char        icmp_date[1];

};

/*

icmp_type 값은 11

icmp_code                         0 : TTL 0으로 IP 데이터 그램의 폐기하는

                           1 : 프래그먼트의 리어셈블이 타임아웃되어 폐기하는

*/

 

 

TCP Protocol

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

TCP
Header
(20 Byte)

Source Port
th_sport

Destination Port
th_dport

Sequence Number
th_seq

Acknowledgement Number
th_ack

DataOffset
th_off

 

 

 

 

 

 

Flags
th_flags

Window
th_win

Checksum
th_sum

Urgent Pointer
th_urp

TCP header Format

 

/* TCP header structure */

struct     tcphdr {

             u_short   th_sport; /* source port */

             u_short   th_dport;             /* destination port */

             tcp_seq  th_seq;   /* sequence number */

             tcp_seq  th_ack;   /* acknowledgement number */

#if          BYTE_ORDER        ==         LITTLE_ENDIAN

             u_int      th_x2:4,  /* (unused) */

                           th_off:4; /* data offset */

#endif

#if          BYTE_ORDER        ==         BIG_ENDIAN

             u_int      th_off:4, /* data offset */

                           th_x2:4;  /* (unused) */

#endif

             u_char    th_flags;  /* (unused) */

             u_short   th_win;   /* window */

             u_short   th_sum;  /* checksum */

             u_short   th_urp;   /* urgent pointer */

};

/*

시퀀스번호(th_seq) 통신한 데이터의 위치를 표현(옥텟 단위)

확인 응답 번호(th_ack) 다음에 송신한 시퀀스 번호를 표현

 

th_flags 하위 6비트는 제이 플래그(control flag)

#define  TH_FIN   0x01

#define  TH_SYN  0x02

#define  TH_RST  0x04

#define  TH_PUSH             0x08

#define  TH_ACK  0x10

#define  TH_URF  0x20

 

윈도 사이즈(th_win) 데이터를 수신하는 측에 있는 수신 버퍼의 영역 크기를 알려주는데 사용한다.

체크섬(th_sum) TCP 헤더나 데이터가 파손 여부를 보증하기 위한 필드

*/

 

 

 

Control Flag

 

Control flag

비트가 1일때의 의미

URG(Urgent Flag)

이동하고 있는 데이터 중에 긴급히 처리해야 하는 것이 포함되어 있는지 아닌지를 의미한다.긴급히 처리해야 하는 데이터는 긴급 포인터의 필드를 나타낸다.

ACK(Acknowledgement Flag)

확인 응답필드가 유효한 것을 의미한다. 가장 최초의 SYN 세그먼트 이외는 반드시 [1] 되어 있다.

PSH(Push Flag)

송신한 데이터를 버퍼링하지 않게 신속히 애플리케이션에 전해 주도록 지원한다.

RST(Reset Flag)

커넥션을 강제적으로 차단하는 것을 의미한다.

SYN(Synchronize Flag)

커넥션 연결 요구를 의미한다. 시퀀스 번호 필드값을 시퀀스 번호의 초기값으로 한다.

FIN(Fin Flag)

커넥션 차단 요구를 의미한다. 통신의 마지막 세그먼트에 있는 것을 의미하고 세그먼트 이외에는 데이터 세그먼트가 송신되지 않는다.

 

UDP Protocol

 

 

0

 

 

 

4

 

 

 

8

 

 

 

 

 

 

 

16

 

 

 

 

 

 

 

 

 

 

 

 

31

UDP
Diagram

Source Port
uh_sport

Destination Port
uh_dport

Length
uh_ulen

Checksum
uh_sum

UDP datagram Format

 

/* Udp header structure */

struct     udphdr {

             u_short   uh_sport;             /* source port */

             u_short   uh_dport;             /* destination port */

             u_short   uh_ulen; /* udp length */

             u_short   uh_sum;  /* udp checksum */

};

 

 

 

출처 - 네이트 통


Comments