Presentation is loading. Please wait.

Presentation is loading. Please wait.

인터넷 주소변환 School of Electronics and Information. Kyung Hee University.

Similar presentations


Presentation on theme: "인터넷 주소변환 School of Electronics and Information. Kyung Hee University."— Presentation transcript:

1 인터넷 주소변환 School of Electronics and Information. Kyung Hee University.
Dae Sun Kim

2 Summary 1주차 2주차 Linux 기본 명령어, VI 편집기 네트워크란? 인터넷이란? Client / Server 모델
네트워크란 End-System 들을 연결하는 하나의 System을 의미한다. 인터넷이란? 멀리 떨어진 둘 이상의 네트워크가 연결되어 이뤄지는 거대한 네트워크를 의미한다. Client / Server 모델 Iterative Server Concurrent Server

3 Summary 네트워크 프로그래밍 이란? Server socket 구현 네트워크로 연결되어 있는 두 호스트간의 데이터 송수신.
파일 입출력과의 차이점은 데이터를 주고 받는 대상에 있다. Server socket 구현 Telephone Server Socket 전화기 구입 전화번호 할당 케이블에 연결 수화기 든다! 소켓 생성(socket) IP 주소 할당.(bind) 연결 요청 대기 상태 (listen) 연결 수락!(accept)

4 Summary Program 5 7 File Descriptor Operating System
리눅스에서는 모든 것을 파일로 관리한다.(파일, 소켓, 표준입력, 표준출력) 파일을 관리하기 위해서 모든 파일(파일, 소켓 표준 입력, 표준 출력)에 파일 디스크립터를 할당 해 준다. Operating System Program 파일 A 5 소켓 B 소켓 C 7 파일 D

5 Summary 3주차 프로토콜 이란? 소켓의 생성 Port란? CASE Domain Type Protocol 1 PF_INET
컴퓨터(호스트) 상호간의 대화에 필요한 통신 규약 소켓의 생성 int socket (int domain, int type, int protocol) 연결지향형 소켓, 비연결지향형 소켓 Port란? 호스트 내에서 실행되고 있는 프로세스(Process)를 구분 짓기 위해 16비트의 논리적 할당 Well-known ports : 0 ~ 1023 CASE Domain Type Protocol 1 PF_INET SOCK_STREAM IPPROTO_TCP(0) 2 SOCK_DGRAM IPPROTO_UDP(0)

6 Summary IPv4 주소체계를 나타내는 구조체 struct sockaddr_in {
sa_family_t sin_family; /* 주소 체계(AF_INET) */ uint16_t sin_port; /* port 정보 */ struct in_addr sin_addr; /* 32 비트 IP 주소정보 */ char sin_zero[8]; /* 사용되지 않음 */ }; struct in_addr { uint32_t s_addr; /* 32비트 IP 주소정보 */ };

7 Summary Big-Endian Little-Endian 0x1234 0x1234 네트워크 바이트 순서 0x12 0x34
12 번지 0x34 0x12 13 번지 0x1234 0x1234

8 Summary 바이트 순서 변환 함수 unsigned short htons(unsigned short);
unsigned short ntohs(unsigned short); unsigend long htonl(unsigned long); unsigned long ntohl(unsigned long); ‘h’ : host byte order ‘n’ : network byte order ‘s’ : short (16 bit) ‘l’ : long (32 bit)

9 Summary FROM Dotted-Decimal Notation TO Big-Endian 32 비트 정수형 데이터 (Network) FROM Big-Endian 32 비트 정수형 데이터 (Network) TO Dotted-Decimal Notation unsigned long inet_addr(const char * string) char * inet_ntoa(struct in_addr addr)

10 인터넷 주소 초기화 Template 1: struct sockaddr_in addr;
     2: char *serv_ip=" ";    3: char *serv_port="9190";    4: memset(&addr, 0, sizeof(addr_len));   5: addr.sin_family = AF_INET;   6: addr.sin_addr.s_addr = inet_addr(serv_ip);   7: addr.sin_port = htons(atoi(serv_port));

11 인터넷 주소 초기화 Template 1: struct sockaddr_in addr;
2: char *serv_port="9190"; 3: memset(&addr, 0, sizeof(addr_len)); 4: addr.sin_family = AF_INET; 5: addr.sin_addr.s_addr = htons(INADDR_ANY); 6: addr.sin_port = htons(atoi(serv_port));

12 Port 번호 확인 #include <sys/types.h> #include <sys/socket.h>
getservbyname()은 well-known 포트를 사용하는 TCP/IP 응용 프로그램의 포트번호를 알아내는 함수이다 서비스 이름과 프로토콜을 인자로 하여 호출하면 서비스 관련 각종 정보를 포함하고 있는 servent라는 구조체의 포인터를 리턴한다. #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> struct servent * getservbyname (string service, string protocol)

13 프로그램 예제 확인 byte_order.c struct servent { char *s_name; /* 서비스 이름 */
char **s_aliases; /* 별명 목록 */ int s_port; /* 포트번호 */ char *s_proto; /* 사용하는 프로토콜 */ };

14 IP 주소 변환 gethostbyname() inet_addr() inet_pton() inet_ntoa()
도메인 네임 IP 주소(binary) 10진 표기법 gethostbyname() inet_addr() inet_pton() inet_ntoa() Inet_ntop() gethostbyaddr()

15 프로그램 예제 확인 ascii_ip.c

16 도메인 주소변환 #include <sys/types.h> #include <sys/socket.h>
#include <netdb.h> struct hostent *gethostbyname(const char *hname) 도메인 네임으로부터 IP 주소를 얻는 함수 struct hostent *gethostbyaddr(const char *in_addr, int len, int family) IP 주소로부터 도메인 네임을 얻는 함수

17 struct hostent { char *h_name; /* 호스트 이름 */
char **h_aliases; /* 호스트 별명들 */ int h_addrtype; /* 호스트 주소의 종류 */ int h_length; /* 주소의 크기 */ char **h_addr_list; /* IP 주소 리스트 */ }; #define h_addr haddr_list[0]; /* 첫 번째(대표) 주소 */

18 주소 형태(IPv4 : AF_INET , IPv6 : AF_INET6)
Member Description h_name 호스트의 공식적 이름 h_aliases 호스트의 별명을 배열의 포인터로 반환 h_addrtype 주소 형태(IPv4 : AF_INET , IPv6 : AF_INET6) h_length 주소길이 (IPv4 : 4, IPv6 : 16) h_addr_list IP 주소를 배열의 포인터로 반환 (IPv4 : 각 포인터는 4 바이트 IP 주소를 가리킨다.

19 프로그램 예제 확인 get_hostent.c , get_host_byaddr.c


Download ppt "인터넷 주소변환 School of Electronics and Information. Kyung Hee University."

Similar presentations


Ads by Google