Contents |
1. Berkeley Sockets |
struct sockaddr {
u_short sa_family; // Address family: AF_XXX value char sa_data[14]; // Upto 14 bytes of protocol specific address }; |
The contents of the 14 bytes of protocol specific address are interpreted according to the type of address.
For the internet family it is defined in <netinet/in.h>
as:
struct in_addr {
u_long s_addr; // 32 bit netid/hostid }; struct sockaddr_in {
|
For the Xerox NS family it is defined in <sys/ns.h>
as :
union ns_host {
u_char c_host[6]; // Host id address as 6 bytes u_short s_host[3]; // Hostid address as three 16-bit shorts }; union ns_net {
struct ns_addr {
// Combined 12 byte XNS address
struct sockaddr_ns {
|
For the Unix domain it is defined in <sys/un.h> as :
struct sockaddr_un {
short sun_family; // AF_UNIX char sun_path[108]; // pathname }; |
The system calls have to take a struct sockaddr type structure. So before
passing the protocol specific structure it must be typecasted. e.g.
struct sockaddr_in serv_addr;
connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); |
2. Socket System Calls |
int socket(
int family, // Family of protocols int type, // Type of connection int protocol); // Protocol to use |
The family is defined as :
TYPE | DESCRIPTION |
PF_UNIX | Unix internal protocols |
PF_INET | ARPA internet protocols |
TYPE | DESCRIPTION |
SOCK_STREAM | Stream socket |
SOCK_DGRAM | Datagram socket |
SOCK_RAW | Raw socket |
SOCK_RDM | Reliably delivered message socket |
SOCK_SEQPACKET | Sequenced packet socket |
EACCES | Permission to create a socket of the specified type and/or protocol is denied. |
EMFILE | The peer-process descriptor table is full. |
ENOMEM | Insufficient user memory is available. |
ENOSR | There were insufficient STREAMS resources available to complete the operation. |
EPROTONOSUPPORT | The protocol type or the specified protocol is not supported within this domain. |
Syntax:
int socketpair(
int family, // Family of protocol int type, // Type of connection int protocol, // Type of protocol int sockvec[2]); // Array to store the socket descriptors |
The family is PF_UNIX.
The valid types are:
The function returns 0 on success and -1 on error. The errno values are:
EAFNOSUPPORT | The specified address family is not supported on this machine. |
EMFILE | Too many descriptors are in use by this process. |
ENOMEM | There was insufficient user memory for the operation to complete. |
ENOSR | There were insufficient STREAMS resources for the operation to complete. |
EOPNOSUPPORT | The specified protocol does not support creation of socket pairs. |
EPROTONOSUPPORT | The specified protocol is not supported on this machine. |
Syntax:
int bind(
int sockfd, // Socket descriptor struct sockaddr *myaddr, // Pointer to a protocol specific structure int addrlen); // Size of structure |
This function returns 0 on success and -1 on error. The errno values are :
EACCES | Search permission is denied for a component of the path prefix of the pathname in name. |
EIO | An I/O error occurred while making the directory entry or allocating the inode. |
EISDIR | A null pathname was specified. |
ELOOP | Too many symbolic links were encountered in translating the pathname in name. |
ENOENT | A component of the path prefix of the pathname in name does not exist. |
ENOTDIR | A component of the path prefix of the pathname in name is not a directory. |
EROFS | The inode would reside on a read-only file system. |
Syntax:
int connect(
int sockfd, // Socket descriptor struct sockaddr *servaddr, // Pointer to structure with server address int addrlen); // Sizeof address structure |
Stream sockets will use this call once while datagram sockets will use it every time they send a packet.
This function call returns 0 on success and -1 on error. The errno values are:
EACCES | Search permission is denied for a component of the path prefix of the path name in name. |
EADDRINUSE | The address is already in use. |
EADDRNOTAVAIL | The specified address is not available on the remote machine. |
EAFNOSUPPORT | Addresses in the specified address family cannot be used with this socket. |
EALREADY | The socket is non-blocking and a previous connection attempt has not yet been completed. |
EBADF | sockfd is not a valid descriptor. |
ECONNREFUSED | The attempt to connect was forcefully rejected. The calling program should close the socket descriptor, and issue another socket call to obtain a new descriptor before attempting another connect call. |
EINPROGRESS | The socket is non-blocking and the connection cannot be completed immediately. It is possible to select for completion by selecting the socket for writing. However, this is only possible if the socket STREAMS module is the topmost module on the protocol stack with a write service procedure. This will be the normal case. |
EINTR | The connection attempt was interrupted before any data arrived by the delivery of a signal. |
EINVAL | addrlen is not the size of a valid address for the specified address family. |
EIO | An I/O error occurred while reading from or writing to the file system. |
EISCONN | The socket is already connected. |
ELOOP | Too many symbolic links were encountered in translating the pathname in name. |
ENETUNREACH | The network is not reachable from this host. |
ENOENT | A component of the path prefix of the pathname in name does not exist. |
ENOENT | The socket referred to by the pathname in name does not exist. |
ENOSR | There were insufficient STREAMS resources available to complete the operation. |
ETIMEDOUT | Connection establishment timed out |
Syntax:
int listen(
int sockfd, // Socket descriptor int backlog); // No of messages that the system should track |
The backlog parameter cannot be more than 5.
Syntax:
int accept(
int sockfd, // Socket descriptor struct sockaddr *peer, // Address of peer to connect int *addrlen); // Size of address structure |
This call takes the first connection request from the queue and creates another socket with the same properties as sockfd. If there are no pending requests, this call blocks.
This function returns:
Syntax:
int send(
int sockfd, // Socket descriptor const char *buff, // Message buffer int nbytes, // No of bytes to send int flags); // Flags int sendto(
int revc(
int recvfrom(
|
MSG_OOB | Send or receive out-of-band data. |
MSG_PEEK | Peek at incoming message. Used by recv and recvfrom. |
Syntax:
int close(int sockfd); |
If the socket being closed is associated with a protocol that promises reliable delivery, the system must ensure that any data within the kernel that still has to be transmitted or acknowledged, is sent.
3. Byte Ordering Routines |
Syntax:
#include <sys/types.h>
#include <netinet/in.h> ulong htonl(u_long hostlong); u_short htons(u_short hostshort); u_long ntohl(u_long netlong); u_short ntohs(u_short netshort); |
The following table describes the functions:
FUNCTION | DESCRIPTION |
htonl() | Convert host to network, long integer |
htons() | Convert host to network, short integer |
ntohl() | Convert network to host, long integer |
ntohs() | Convert network to host, short integer |
Implicit to these functions are that a short integer occupies 16 bits and a long integer occupies 32 bits.
4. Address Conversion Routines |
Syntax:
#include <netinet/in.h>
#include <arpa/inet.h> unsigned long inet_addr(const char *cp); char *inet_ntoa(const struct in_addr in); |
The first function takes the stringified IP address and returns the 32 bit value.
The second function takes the in_addr structure and returns a pointer to the stringified IP address form.
5. Advanced Socket System Calls |
Syntax:
#include <sys/types.h>
#include <sys/uio.h> int writev( int fd, struct iovec iov[], int iovcount ); int readv ( int fd, struct iovec iov[], int iovcount ); |
The structure iovec is defined as:
struct iovec {
caddr_t iov_base; // Starting address of buffer int iovlen; // Size of buffer in bytes }; |
Each element of the iovec array specifies a base address of a buffer and the amount of data to be written or read from it. The parameter iovcount specifies the number of separate buffers. In case of a datagram system, each write will generate separate datagrams but if combined in one, a writev will generate only one.
Syntax:
int sendmsg( int sockfd, struct msghdr msg[], int
flags );
int recvmsg( int sockfd, struct msghdr msg[], int flags ); |
The structure msghdr is defined as:
struct msghdr{
caddr_t msg_name; // Optional address int msg_namelen; // Size of address struct iovec *msg_iov; // Scatter or gather array int msg_iovlen; // Elements in msg_iov caddr_t msg_accrights; // Access rights sent or received int msg_accrightslen; // Length of the access rights buffer }; |
The fields msg_name and msg_namelen are not used for connected processes.
Syntax:
int getpeername( int sockfd, struct sockaddr *peer, int *addrlen ); |
This function returns the foreign address and the foreign process elements of the 5 tuple association of sockfd.
Syntax:
int getsockname( int sockfd, struct sockaddr *peer, int *addrlen ); |
This call returns the local address and the local process of the association.
Syntax:
int shutdown( int sockfd, int howto ); |
The control is defined using different values of howto. They are:
VALUE | FUNCTION |
0 | No more data can be received on the socket |
1 | No more data can be sent through the socket |
2 | Both sends and receives are disallowed |
6. Reserved Ports |
Syntax:
int rresvport( int *aport ); |
The port scenario is shown below:
Internet | XNS | |
Reserved Ports | 1-1023 | 1-2999 |
Ports automatically assigned by the system | 1024-5000 | 3000-65535 |
Ports assigned by rresvport | 512-1023 | NA |