0% found this document useful (0 votes)
3 views4 pages

ANP- 9. Socket Programming in UNIX (structures and functions)

The document provides an overview of socket programming in UNIX, focusing on socket address structures, byte ordering functions, and address conversion functions. It explains the generic socket address structure and specific structures for IPv4, including how to manipulate and convert byte orders. Additionally, it discusses functions for checking socket descriptors and converting between ASCII strings and network byte ordered values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

ANP- 9. Socket Programming in UNIX (structures and functions)

The document provides an overview of socket programming in UNIX, focusing on socket address structures, byte ordering functions, and address conversion functions. It explains the generic socket address structure and specific structures for IPv4, including how to manipulate and convert byte orders. Additionally, it discusses functions for checking socket descriptors and converting between ASCII strings and network byte ordered values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/21/2017

Topics
• Socket address structures
Socket Programming in UNIX • Byte ordering functions (conversion between
(Basic Structures and auxiliary host and network order)
functions) • Byte manipulation functions
Harshad B. Prajapati • Address conversion functions
Associate Professor
Information Technology Department,
Dharmsinh Desai University, Nadiad

Socket address structures Socket address structures


• a pointer to a socket address structure as an Generic Socket address structure.
argument in most socket functions. • Type of argument?
– With ANSI C, solution is simple. Use void*
• Each supported protocol suite defines its own – Socket functions exists before ANSI C. Solution chosen in 1982 was to
socket address structure. define a generic socket address structure.
• sockaddr (Page 60, Stevens Vol.1)
– The names of these structures begin with
#include <sys/socket.h>
sockaddr_ and end with a unique suffix for each
struct sockaddr {
protocol suite. uint8_t sa_len;
sa_family_t sa_family; /* (unsigned short) address family, AF_xxx
or PF_xxx*/
char sa_data[14]; /* 14 bytes of protocol-specific address*/
};

Socket address structures Socket address structures


• IPv4 socket address structure • sockaddr_in (Page 58, Stevens Vol.1)
– commonly called an "Internet socket address structure,
– It is named sockaddr_in #include <netinet/in.h>
– defined in the <netinet/in.h> header struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family; /* (unsigned short) Address family, AF_INET */
• in_addr (Page 58, Stevens Vol.1)
in_port_t sin_port; /* (unsigned short int) Port number */
#include <netinet/in.h> struct in_addr sin_addr; /* Internet address unsigned char*/
struct in_addr { sin_zero[8]; /* Same size as struct sockaddr*/
in_addr_t s_addr; /* that's a 32-bit int (4 bytes) (uint32_t) */ };
};

1
7/21/2017

Socket address structures Socket address structures


• Both the IPv4 address and the TCP or UDP port • The sin_zero member is unused,
number are always stored in the structure in network – we always set it to 0 when filling in one of these structures.
– By convention, we always set the entire structure to 0 before filling it
byte order. in, not just the sin_zero member.
• The 32-bit IPv4 address can be accessed in two
different ways:
– For example,
struct sockaddr_in serv;
(1) serv.sin_addr references the 32-bit IPv4 address as an
in_addr structure,
(2) while serv.sin_addr.s_addr references the same 32-bit
IPv4 address as an in_addr_t (typically an unsigned 32-bit
integer).

Socket address structures Value-result arguments


How to use generic socket address structure? • Value Argument
• E.g., in • Functions: bind, connect, and sendto pass a socket address structure from
the process to the kernel.
int bind(int, struct sockaddr *, socklen_t);
Ex.
struct sockaddr_in serv; /* fill in serv{} */ connect (sockfd, (struct sockaddr *)
struct sockaddr_in serv; /* IPv4 socket address structure */ &serv, sizeof(serv));
/* fill in serv{} */
bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));

Value-result arguments Byte Ordering functions


• Value-result Argument • See byte ordering in two different formats (little endian and
• Functions: accept, recvfrom, getsockname, and getpeername pass a big endian). Any computer system uses one of these. (it is
socket address structure from the kernel to the process. specific to hardware architecture)
struct sockaddr_un cli; /* Unix domain */
socklen_t len; len = sizeof(cli); /* len is a value */
getpeername(unixfd, (struct sockaddr *) &cli, &len); /* len may have changed
*/

2
7/21/2017

Byte Ordering functions Byte Manipulation functions


• Host byte order: byte order used by a given system is called host byte • Two sets of byte manipulation functions
order. – Provided by BSD (berkely derived, starts with b)
• Network byte order: byte order used for network programs by networking – ANSI C (starts with mem)
protocols (E.g. for TCP, kernel) is network byte order.
• We need conversion functions for short and long data types.
#include <strings.h>
#include <netinet/in.h>
void bzero(void * dest , size_t nbytes ); /* we use this function to initialize
Conversion to network byte order. l for long (4 bytes) and s for short (2 bytes) address structure with 0 value in all its bytes.*/
uint16_t htons(uint16_t host16bitvalue ) ; void bcopy(const void * src , void * dest , size_t nbytes );
uint32_t htonl (uint32_t host32bitvalue ) ; int bcmp(const void * ptr1 , const void * ptr2 , size_t nbytes );

Conversion to host byte order. l for long (4 bytes) and s for short (2 bytes) Returns: 0 if equal, nonzero if unequal
uint16_t ntohs(uint16_t net16bitvalue ) ;
uint32_t ntohl (uint32_t net32bitvalue ) ;

Byte Manipulation functions Address conversion functions


• ANSI C functions: • These functions convert Internet addresses between ASCII strings (what
humans prefer to use- dotted -decimal string,e.g., "206.168.112.96“ and
#include <string.h> network byte ordered binary values (values that are stored in socket
address structures, 32 bytes values).
void *memset(void * dest , int c , size_t len );
• The memset function sets the specified number of bytes to the value c in
the destination • two groups of address conversion functions.
void * memcpy (void * dest , const void * src , size_t nbytes ); #include <arpa/inet.h>
• memcpy is similar to bcopy , but the order of the two pointer arguments is int inet_aton(const char * strptr , struct in_addr * addrptr );
swapped. • Returns: 1 if string was valid, 0 on error
• bcopy correctly handles overlapping fields, while the behavior of memcpy char *inet_ntoa(struct in_addr inaddr );
is undefined if the source and destination overlap • Returns: pointer to dotted-decimal string
int memcmp (const void * ptr1 , const void * ptr2 , size_t nbytes ); in_addr_t inet_addr(const char * strptr );
• memcmp compares two arbitrary byte strings and returns 0 if they are • Returns: 32-bit binary network byte ordered IPv4 address; INADDR_NONE
identical. Otherwise, result is whether first unequal byte pointed to by if error
ptr1 is greater than or less than the corresponding byte pointed to by ptr2
.

Address conversion functions Address conversion functions


• The newer functions, inet_pton and inet_ntop , handle both IPv4 and IPv6 • Example
addresses. • Even if your system does not yet include support for IPv6, you can start
using these newer functions by replacing calls of the form
#include <arpa/inet.h>
int inet_pton(int family , const char * strptr , void * addrptr ); servaddr.sin_addr.s_addr = inet_addr(cp);
• Returns: 1 if OK, 0 if input not a valid presentation format, -1 on error with
• The family argument for both functions is either AF_INET or AF_INET6 . inet_pton(AF_INET, cp, &servaddr.sin_addr);
const char *inet_ntop(int family , const void * addrptr , char * strptr , size_t
len ); and replacing calls of the form
• Returns: pointer to result if OK, NULL on error. ptr = inet_ntoa(foo.sin_addr);
• The len argument is the size of the destination, to prevent the function With
from overflowing the caller's buffer. char str[INET_ADDRSTRLEN];
• The strptr argument to inet_ntop cannot be a null pointer. The caller must ptr = inet_ntop(AF_INET, &servaddr.sin_addr, str, sizeof(str));
allocate memory for the destination and specify its size. On success, this
pointer is the return value of the function. (See example.)

3
7/21/2017

Checking whether file descriptor is


a socket descriptor
• Use isfdtype function

#include <sys/stat.h>
int isfdtype(int sockfd, int fdtype);
Returns: 1 if sockfd is of specified fdtype, 0 if not, -1 on error.

To test for a socket descriptor, use value S_IFSOCK for fdtype.

You might also like