본문 바로가기
코스웨어/15년 스마트컨트롤러

20150522 - 14번 - 박제혁 - IP 프로토콜

by 알 수 없는 사용자 2015. 5. 24.
728x90
반응형


IP 프로토콜


--- struct ip 구조체 (/usr/include/netinet/ip.h) ---

struct ip { #if BYTE_ORDER == LITTLE_ENDIAN u_char ip_hl:4, /* header length */         ip_v:4; /* version */ #endif #if BYTE_ORDER == BIG_ENDIAN u_char ip_v:4, /* version */         ip_hl:4; /* header length */ #endif u_char ip_tos; /* type of service */ short     ip_len; /* total length */ u_short ip_id; /* identification */ short     ip_off; /* fragment offset field */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ u_char ip_ttl; /* time to live */ u_char ip_p; /* protocol */ u_short ip_sum; /* checksum */ struct in_addr ip_src,ip_dst; /* source and dest address */ };


--- 사용법 ----


#include <netinet/ip.h>


const unsigned char *ucpData = 캡쳐한 패킷의 제일 앞 주소;


struct ip *v = ucpData + sizeof(struct ether_header);

// v는 캡쳐한 패킷의 주소에다가 이더넷 헤더길이 만큼 더한 주소값을 받음

// 즉 ip 구조체의 제일 앞을 가리키게 됨


printf("IP Version : %d \n", v->ip_v);

//IP Version


printf("Header Length : %d \n", v->ip_hl * 4);

//IP Header 크기


printf("Type of Service : 0x%02X \n", v->ip_tos);

//Type of Service 요구되는 서비스 품질

//최소 지연, 최대 처리량, 최대 신뢰성, 최소 비용을 나타내는 필드


printf("Total Length : %d byte \n", ntohs(v->ip_len));

//Total Length

//IP 헤더 및 데이터를 포함한 IP 패킷 전체의 길이

//최대값은 65,535 이지만 MTU에서 1500으로 제한


printf("Src IP : %s \n", inet_ntoa(v->ip_src));

//송신 IP주소


printf("Dst IP : %s \n", inet_ntoa(v->ip_dst));

//수신 IP주소



--- 참고 ---


1. inet_ntoa 함수


함수원형 : char * inet_ntoa(struct in_addr);


캡쳐한 패킷을 보면 src ip, dst ip들은 C0 A8 00 73 이런 식으로 저장되어 있다.


inet_ntoa 함수는 이 값들을  192.168.0.115 이렇게 변환시킨 후 이 문자열의 첫번째 주소값을 리턴한다.


728x90