1 / 27

Chapter 03. Socket Address Structures

Chapter 03. Socket Address Structures. Goal. Socket address structures Byte ordering IP address conversion Domain name conversion. Def. of socket address structures (1/6). Socket address structures Sockets use the socket address structure to pass and to receive addresses

thor
Download Presentation

Chapter 03. Socket Address Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 03. Socket Address Structures

  2. Goal • Socket address structures • Byte ordering • IP address conversion • Domain name conversion

  3. Def. of socket address structures (1/6) • Socket address structures • Sockets use the socket address structure to pass and to receive addresses • Several types depending on address family • example) TCP/IP SOCKADDR_IN, IrDA SOCKADDR_IRDA • Basic type is SOCKADDR structure

  4. Def. of socket address structures (2/6) • SOCKADDR structure • sa_family • 16-bit integer value identifying the protocol family being used e.g.) TCP/IP  AF_INET • sa_data • Address information used in the protocol family e.g.) TCP/IP  IP address and port number struct sockaddr { u_short sa_family; char sa_data[14]; }; typedef struct sockaddr SOCKADDR;

  5. Def. of socket address structures (3/6) • SOCKADDR_IN structure • sin_addr • 32-bit in_addr structure struct sockaddr_in { short sin_family; // AF_INET u_short sin_port; // port number struct in_addr sin_addr; // IP address char sin_zero[8]; // always 0 }; typedef struct sockaddr_in SOCKADDR_IN;

  6. Def. of socket address structures (4/6) • IN_ADDR structure struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; #define s_addr S_un.S_addr }; typedef struct in_addr IN_ADDR;

  7. Def. of socket address structures (5/6) • comparison of socket address structures SOCKADDR{} SOCKADDR_IN{} SOCKADDR_IRDA{} sa_family (2) sin_family (2) irdaAddressFamily (2) sa_data (14) sin_port (2) irdaDeviceID (4) sin_addr (4) irdaServiceName (25) sin_zero (8)

  8. Def. of socket address structures (6/6) • Use of socket address structure • example1) • Example 2) SOCKADDR_IN addr1; // initialize socket address structure ... f((SOCKADDR *)&addr1, ...); SOCKADDR_IN addr2; g((SOCKADDR *)&addr2, ...); // use socket address structure ...

  9. 0x1000 0x1001 0x1002 0x1003 Big-endian    0x12 0x34 0x56 0x78    Little-endian    0x78 0x56 0x34 0x12    Byte ordering function calls (1/6) • byte ordering • Two ways to store data in their memories • big-endian: the high-order octet is stored in the lowest memory location • little-endian: the high-order octet is stored in the highest memory location

  10. router End system router End system ? (a) ? (b) (c) IP address Port number ? data Byte ordering function calls (2/6) • What if byte ordering is not considered in the below?

  11. Byte ordering function calls (3/6) • Cases that byte ordering must be considered in network applications • Address information for protocol • (a) IP address  big-endian • (b) port number  big-endian • Data that applications send and receive • (c) big-endian or little-endian Ref. network byte ordering : big-endian  host byte ordering : byte ordering that system uses

  12. Byte ordering function calls (4/6) • Byte ordering functions(unix compatible) • Byte ordering functions(Winsock extended) u_short htons (u_short hostshort); // host-to-network-short u_long htonl (u_long hostlong); // host-to-network-long u_short ntohs (u_short netshort); // network-to-host-short u_long ntohl (u_long netlong); // network-to-host-long int WSAHtons (SOCKET s, u_short hostshort, u_short* lpnetshort); int WSAHtonl (SOCKET s, u_long hostlong, u_long* lpnetlong); int WSANtohs (SOCKET s, u_short netshort, u_short* lphostshort); int WSANtohl (SOCKET s, u_long netlong, u_long* lphostlong);

  13. Byte ordering function calls (5/6) • Use of byte ordering functions Data to pass Use data in application hton*( ) ntoh*( ) Data from socket call Socket system call Socket system call

  14. SOCKADDR_IN{} sin_family (2) Host byte ordering sin_port (2) sin_addr (4) Network byte ordering sin_zero (8) Byte ordering function calls (6/6) • Byte ordering of SOCKADDR_IN structure

  15. Byte ordering function calls (6/6) • ByteOrder.cpp

  16. Function calls to convert IP address (1/4) • Example.

  17. Function calls to convert IP address(2/4) • IP address coversion function calls • Converts an IP address in ASCII dotted-decimal format to 32-bit binary format (network byte ordering) • Converts 32-bit binary format (network byte ordering) to an IP address in ASCII dotted-decimal format unsigned long inet_addr (const char* cp); char* inet_ntoa (struct in_addr in); // network-to-ascii

  18. Function calls to convert IP address(3/4) • Use ① of byte ordering and IP address conversion function calls • Case that address information is passed to socket system call after socket address structure initialization (usually sender side) • f( ) is socket system call // initialize socket address structure SOCKADDR_IN addr; ZeroMemory(&addr, sizeof(addr)); // initialize to 0 addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("147.46.114.70"); addr.sin_port = htons(9010); // socket system call f((SOCKADDR *)&addr, ...);

  19. Function calls to convert IP address (4/4) • Use ② of byte ordering and IP address conversion function calls • Case that application prints address information after socket system call (usually receiver side) • g( ) is socket system call // declaration ofsocket address structure SOCKADDR_IN addr; // socket system call g((SOCKADDR *)&addr, ...); // print IP address and port number printf("IP address=%s, port number=%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));

  20. Byte ordering function calls (6/6) • IPAddr.cpp

  21. coversion between domain name and IP address (1/6) • Example: Domain name  IP address

  22. coversion between domain name and IP address (2/6) • Domain name  IP address /* domain name  IP address(network byte ordering) */ struct hostent* gethostbyname ( const char* name // domain name ); /* address(network byte ordering)domain name*/ struct hostent* gethostbyaddr ( const char* addr, // network byte ordered IP address int len, // length of IP address(e.g.: 4) int type // address family(e.g.: AF_INET) );

  23. coversion between domain name and IP address(3/6) • hostent struct struct hostent { char * h_name; // official name of host char ** h_aliases; // alias list short h_addrtype; // host address type short h_length; // length of address char ** h_addr_list; // list of addresses #define h_addr h_addr_list[0] // address, for backward compatibility }; typedef struct hostent HOSTENT;

  24. coversion between domain name and IP address(4/6) • hostent struct (cont’d) HOSTENT{} h_name official domain name\0 h_aliases alias #1\0 h_addrtype AF_INET alias #2\0 h_length 4 NULL h_addr_list IN_ADDR{} IP address #1 IP address #2 NULL h_length = 4

  25. coversion between domain name and IP address(5/6) • User defined function ① // domain name -> IP address BOOL GetIPAddr(char *name, IN_ADDR *addr) { HOSTENT *ptr = gethostbyname(name); if(ptr == NULL){ err_display("gethostbyname()"); return FALSE; } memcpy(addr, ptr->h_addr, ptr->h_length); return TRUE; }

  26. coversion between domain name and IP address (6/6) • User defined function ② // IP address -> domain name BOOL GetDomainName(IN_ADDR addr, char *name) { HOSTENT *ptr = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); if(ptr == NULL){ err_display("gethostbyaddr()"); return FALSE; } strcpy(name, ptr->h_name); return TRUE; }

  27. Byte ordering function calls (6/6) • INameResolution.cpp

More Related