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 이렇게 변환시킨 후 이 문자열의 첫번째 주소값을 리턴한다.
'코스웨어 > 15년 스마트컨트롤러' 카테고리의 다른 글
고전 자료(?) ... (4) | 2015.05.28 |
---|---|
20150528 - 20번 엄민웅 C#, C++(클래스 생성자 소멸자) (8) | 2015.05.28 |
20150527 - 19번 - 안향진 - C#,C++ (6) | 2015.05.28 |
20150526 - 18번 - 안해운 - C++ (6) | 2015.05.26 |
20150522 packet analyzer 소스코드 (1) | 2015.05.22 |
20150221 업무일지 - 16번 박태인 - 서보모터,Timer/Counter & network protocol, MAC Address (4) | 2015.05.22 |
20150520_일일업무일지_14번_박서연 - 카운터/타이머, pcap, 도서관리프로그램 (5) | 2015.05.20 |
2015.05.20 Linux Hex_view Source (0) | 2015.05.20 |