-
[네트워크]주소변환관련 APIProgramming/system programming 2014. 10. 3. 19:33
항상 헷갈리는 주소변환.
참조 : http://blog.naver.com/PostView.nhn?blogId=dkssud2623&logNo=70043775711&redirect=Dlog&widgetTypeCall=true
주소변환관련 API
intelx86은 little endian 방식으로 데이터 처리,저장
network는 big endian 방식으로 데이터 처리
둘이 다르기때문에 Data to Network 는 big endian방식으로 애초에 메모리에 저장하고,
Data from Network는 메모리에 있는 데이터를 little endian으로 변환해서 프로그래머가 처리한다.
long
short
little endian -> big endian
(Host byte order TO Network byte order)
htonl()
htons
big endian -> little endian
(Network byte order TO Host byte order)
ntohl
ntohs
IPv4 인터넷 주소를 조작하기 위한 함수
#include <sys/socket.h>
#include <netinet.h>
#include <arpa/inet.h>
int inet_aton(const char string, struct in_addr *addr)
Dotted-Decimal Notation을 Big-Endian 32비트 값으로 변환
성공 시 0이 아닌 값(true), 실패시 0(false)이 리턴
char * inet_ntoa(struct in_addr *addr)
네트워크 바이트 순서의 32비트 값을 Dotted-Decimal Notation으로 변환
성공 시 변환된 해당 문자열의 포인터를, 실패 시 -1을 리턴
IPv6 인터넷 주소를 조작하기 위한 함수
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
IPv6 문자열 인터넷 주소를 128 비트 이진데이타 주소로 변경하기 위해서 사용되는 함수이다. 이 함수는 프로토콜 범용으로 사용할수 있도록 inet_addr 함수를 확장시킨 것으로 IPv4 와 IPv6 모두의 주소변환에 사용할수 있도록 범용화 되어 있다.
첫번째 argument인 af를 이용해서 프로토콜 종류를 지정할 수 있다.
두번째 argument인 src가 가르키는 인터넷 문자열을 프로토콜 종류에 맞도록 이진데이타로 변경해서 세 번째 argument dst로 복사한다.
const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
위의 함수 inet_pton 과 반대의 일을 한다. 즉 128비트 이진데이타 주소를 문자열 인터넷 주소로 변경한다. 이함수 역시 IPv6 전용의 함수는 아니며 프로토콜 범용으로 사용할수 있도록 inet_ntoa 를 확장한 함수이다
'Programming > system programming' 카테고리의 다른 글
[linux] mmap sys call (0) 2015.11.01 [linux] 디바이스 드라이버 만들기 (0) 2015.06.01 [Linux] shared memory (0) 2013.06.19