Title: Socket Address Structures
1Chapter 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_data14 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_zero8 // always 0 typedef struct
sockaddr_in SOCKADDR_IN
6 Def. of socket address structures (4/6)
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 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 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_lon
g 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 Byte ordering function calls (6/6)
- Byte ordering of SOCKADDR_IN structure
15 Byte ordering function calls (6/6)
16 Function calls to convert IP address (1/4)
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 of socket address
structure SOCKADDR_IN addr // socket system
call g((SOCKADDR )addr, ...) // print IP
address and port number printf("IP addresss,
port numberd\n", inet_ntoa(addr.sin_addr),
ntohs(addr.sin_port))
20 Byte ordering function calls (6/6)
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(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)
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_list0 //
address, for backward compatibility typedef
struct hostent HOSTENT
24 coversion between domain name and IP address(4/6)
HOSTENT
official domain name\0
h_name
h_aliases
alias 1\0
AF_INET
h_addrtype
alias 2\0
4
h_length
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)
// domain name -gt IP address BOOL GetIPAddr(char
name, IN_ADDR addr) HOSTENT ptr
gethostbyname(name) if(ptr NULL)
err_display("gethostbyname()") return
FALSE memcpy(addr, ptr-gth_addr,
ptr-gth_length) return TRUE
26 coversion between domain name and IP address
(6/6)
// IP address -gt 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-gth_name) return TRUE
27 Byte ordering function calls (6/6)