Sockets Introduction Socket address structures Valueresult arguments Byte
Sockets Introduction • • Socket address structures Value-result arguments Byte ordering and manipulation functions Address conversion functions: inet_aton, inet_addr, inet_ntoa, inet_pton, inet_ntop, sock_ntop • Stream socket I/O functions: readn, writen, readline • File descriptor testing function: isfdtype 1
Comparison of Various Socket Address Structures IPv 4 socketaddr_in{} IPv 6 socketaddr_in 6{} length AF_INET 6 16 -bit port# 32 -bit IP address flow label UNIX socketaddr_un{} length AF_LOCAL (unused) fixed length (16 bytes) 128 -bit IPv 6 address Datalink socketaddr_dl{} length AF_LINK interface index type name len addr len sel len interface name and link-layer address pathname (up to 104 bytes) variable length fixed length (24 bytes) variable length 2
Datatypes Required by Posix. 1 g Datatype int 8_t uint 8_t int 16_t uint 16_t int 32_t sa_family_t socklen_t in_addr_t in_port_t Description Header signed 8 -bit integer <sys/types. h> unsigned 8 -bit integer <sys/types. h> signed 16 -bit integer <sys/types. h> unsigned 16 -bit integer <sys/types. h> signed 32 -bit integer <sys/types. h> address family of socket addr struct <sys/types. h> length of socket addr struct, uint 32_t <sys/types. h> IPv 4 address, normally uint 32_t <sys/types. h> TCP or UDP port, normally uint 16_t <sys/types. h> 3
Socket Address Structures: IPv 4, Generic, IPv 6 struct in_addr { in_addr_t s_addr; struct sockaddr_in { uint 8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; struct sockaddr { uint 8_t sa_len; sa_family; char sa_data[14]; struct in 6_addr { unit 8_t s 6_addr[16]; struct sockaddr_in 6 { uint 8_t sin 6_len; sa_family sin 6_family; in_port_t sin 6_port; uint 32_t sin 6_flowinfo; struct in 6_addr sin 6_addr; /* 32 -bit IPv 4 address, network byte order */ }; /* length of structure */ /* AF_INET */ /* 16 -bit port#, network byte order */ /* 32 -bit IPv 4 address, network byte order */ /* unused */ /* only used to cast pointers */ }; /* address family: AF_xxx value */ /* protocol-specific address */ }; /* 128 -bit IPv 6 address, network byte order */ }; /* length of this struct [24] */ /* AF_INET 6 */ /* port#, network byte order */ /* flow label and priority */ /* IPv 6 adress, network byte order */ }; 4
Value-Result Argument user process int * length user process socket address structure protocol address value kernel e. g. bind, connect, sendto int * length value socket address structure result protocol address kernel e. g. accept, recvfrom, getsockname, getpeername 5
Byte Ordering Functions: converting between the host byte order to the network byte order little-endian byte order: big-endian byte order: (for a 16 -bit integer) address A+1 high-order byte address A low-order byte address A+1 Some machines use the little-endian host byte order while the others use the big-endian. The Internet protocols use the big-endian network byte order. Hence, conversion functions should be added in all cases. #include <netinet/in. h> uint 16_t htons(uint 16_t host 16 bitvalue); returns: value in network byte order uint 32_t htonl(uint 32_t host 32 bitvalue); returns: value in network byte order uint 16_t ntohs(uint 16_t net 16 bitvalue); returns: value in host byte order uint 32_t ntohl(uint 32_t net 32 bitvalue); returns: value in host byte order 6
Byte Manipulation Functions: operating on multibyte fields From 4. 2 BSD: #include <strings. h> void bzero (void *dest, size_t nbytes); void bcopy (const void *src, void *dest, size_t nbytes); int bcmp (const void *ptr 1, const void *ptr 2, size_t nbytes); returns: 0 if equal, nonzero if unequal From ANSI C: #include <string. h> void *memset (void *dest, int c, size_t len); void *memcpy (void *dest, const void *src, size_t nbytes); int memcmp (const void *ptr 1, const void *ptr 2, size_t nbytes); returns: 0 if equal, nonzero if unequal 7
Address Conversion Functions between ASCII strings and network byte ordered binary values For IPv 4 only: ascii and numeric #include <arpa/inet. h> int inet_aton (const char *strptr, struct in_addr *addrptr); returns: 1 if string is valid, 0 on error int_addr_t inet_addr (const char *strptr); returns: 32 -bit binary IPv 4 addr, INADDR_NONE if error char *inet_ntoa (struct in_addr inaddr); returns: pointer to dotted-decimal string For IPv 4 (AF_INET) and IPv 6 (AF_INET 6): presentation and numeric #include <arpa/inet. h> int inet_pton (int family, const char *strptr, void *addrptr); returns: 1 if OK, 0 if invalid presentation, -1 on error const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len); returns: pointer to result if OK, NULL on error INET_ADDRSTRLEN = 16 (for IPv 4), INET 6_ADDRSTRLEN = 46 (for IPv 6 hex string) 8
Address Conversion Functions (cont. ) Protocol independent functions (not standard system functions): #include “unp. h” char *sock_ntop (const struct sockaddr *sockaddr, socklen_t addrlen); returns: nonnull pointer if OK, NULL on error sock_bind_wild, sock_cmp_addr, sock_cmp_port, sock_get_port, sock_ntop_host, sock_set_addr, sock_set_port, sock_set_wild, etc. 9
Stream Socket I/O Functions: preventing callers from having to handle a short count read and write return, before reading or writing requested bytes, when the socket buffer limit is reached in the kernel. When reading from or writing to a stream socket: (need to handle EINTR signal) #include “unp. h” ssize_t readn (int filedes, void *buff, size_t nbytes); returns: #bytes read, -1 on error ssize_t writen (int filedes, const void *buff, size_t nbytes); returns: #bytes written, -1 on error ssize_t readline (int filedes, void *buff, size_t maxlen); returns: #bytes read, -1 on error 10
File Descriptor Testing Function: testing a descriptor of a specific type #include <sys/stat. h> int isfdtype (int sockfd, int fdtype); returns: 1 if descriptor of specified type, 0 if not, -1 on error (to test a socket: fdtype is S_IFSOCK) isfdtype is implemented by calling fstat and testing the returned st_mode value using the S_IFMT macro. 11
- Slides: 11