Wednesday, July 1, 2015

Berkley Sockets Refresher

Berkley Sockets is a computing library for internet sockets, unix domain sockets and inter process communication.
This list is a summary of functions or methods provided by the Berkeley sockets API library:
  • socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it.
  • bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number and IP address.
  • listen() is used on the server side, and causes a bound TCP socket to enter listening state.
  • connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP connection.
  • accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection.
  • send() and recv(), or write() and read(), or sendto() and recvfrom(), are used for sending and receiving data to/from a remote socket.
  • close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.
  • gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only.
  • select() is used to pend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors.
  • poll() is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read from or if an error occurred.
  • getsockopt() is used to retrieve the current value of a particular socket option for the specified socket.
  • setsockopt() is used to set a particular socket option for the specified socket.
socket()
socket() creates an endpoint for communication and returns a file descriptor for the socket. socket() takes three arguments:
  • domain, which specifies the protocol family of the created socket. For example:
    • AF_INET for network protocol IPv4 or
    • AF_INET6 for IPv6.
    • AF_UNIX for local socket (using a file).
  • type, one of:
    • SOCK_STREAM (reliable stream-oriented service or Stream Sockets)
    • SOCK_DGRAM (datagram service or Datagram Sockets)
    • SOCK_SEQPACKET (reliable sequenced packet service), or
    • SOCK_RAW (raw protocols atop the network layer).
  • protocol specifying the actual transport protocol to use. The most common are IPPROTO_TCPIPPROTO_SCTPIPPROTO_UDPIPPROTO_DCCP. These protocols are specified in . The value 0 may be used to select a default protocol from the selected domain and type.
The function returns -1 if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.
Prototype
int socket(int domain, int type, int protocol);

bind()

bind() assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connections to other hosts. bind() takes three arguments:
  • sockfd, a descriptor representing the socket to perform the bind on.
  • my_addr, a pointer to a sockaddr structure representing the address to bind to.
  • addrlen, a socklen_t field specifying the size of the sockaddr structure.
Bind() returns 0 on success and -1 if an error occurs.
Prototype
int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);

listen()[edit]

After a socket has been associated with an address, listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAMSOCK_SEQPACKET). listen() requires two arguments:
  • sockfd, a valid socket descriptor.
  • backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value.
Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned.
Prototype
int listen(int sockfd, int backlog);

accept()[edit]

When an application is listening for stream-oriented connections from other hosts, it is notified of such events (cf. select() function) and must initialize the connection using theaccept() function. The accept() function creates a new socket for each connection and removes the connection from the listen queue. It takes the following arguments:
  • sockfd, the descriptor of the listening socket that has the connection queued.
  • cliaddr, a pointer to a sockaddr structure to receive the client's address information.
  • addrlen, a pointer to a socklen_t location that specifies the size of the client address structure passed to accept(). When accept() returns, this location indicates how many bytes of the structure were actually used.
The accept() function returns the new socket descriptor for the accepted connection, or -1 if an error occurs. All further communication with the remote host now occurs via this new socket.
Datagram sockets do not require processing by accept() since the receiver may immediately respond to the request using the listening socket.
Prototype
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

connect()[edit]

The connect() system call connects a socket, identified by its file descriptor, to a remote host specified by that host's address in the argument list.
Certain types of sockets are connectionless, most commonly user datagram protocol sockets. For these sockets, connect takes on a special meaning: the default target for sending and receiving data gets set to the given address, allowing the use of functions such as send() and recv() on connectionless sockets.
connect() returns an integer representing the error code: 0 represents success, while -1 represents an error. Historically, in the BSD-derived systems, the state of a socket descriptor is undefined if the call to connect() fails (as it is specified in the Single Unix Specification), thus, portable applications should close the socket descriptor immediately and obtain a new descriptor with socket(), in the case the call to connect() fails.[3]
Prototype
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

gethostbyname() and gethostbyaddr()[edit]

The gethostbyname() and gethostbyaddr() functions are used to resolve host names and addresses in the domain name system or the local host's other resolver mechanisms (e.g., /etc/hosts lookup). They return a pointer to an object of type struct hostent, which describes an Internet Protocol host. The functions take the following arguments:
  • name specifies the name of the host. For example: www.wikipedia.org
  • addr specifies a pointer to a struct in_addr containing the address of the host.
  • len specifies the length, in bytes, of addr.
  • type specifies the address family type (e.g., AF_INET) of the host address.
The functions return a NULL pointer in case of error, in which case the external integer h_errno may be checked to see whether this is a temporary failure or an invalid or unknown host. Otherwise a valid struct hostent * is returned.
These functions are not strictly a component of the BSD socket API, but are often used in conjunction with the API functions. Furthermore, these functions are now considered legacy interfaces for querying the domain name system. New functions that are completely protocol-agnostic (supporting IPv6) have been defined. These new function aregetaddrinfo() and getnameinfo(), and are based on a new addrinfo data structure.
Prototypes
struct hostent *gethostbyname(const char *name);
struct hostent *gethostbyaddr(const void *addr, int len, int type);
The following lists a sampling of protocol families (preceded by the standard symbolic identifier) defined in a modern Linux or BSD implementation:
PF_LOCAL, PF_UNIX, PF_FILE
                Local to host (pipes and file-domain)
PF_INET         IP protocol family
PF_AX25         Amateur Radio AX.25
PF_IPX          Novell Internet Protocol
PF_APPLETALK    Appletalk DDP
PF_NETROM       Amateur radio NetROM
PF_BRIDGE       Multiprotocol bridge
PF_ATMPVC       ATM PVCs
PF_X25          Reserved for X.25 project
PF_INET6        IP version 6
PF_ROSE         Amateur Radio X.25 PLP
PF_DECnet       Reserved for DECnet project
PF_NETBEUI      Reserved for 802.2LLC project
PF_SECURITY     Security callback pseudo AF
PF_KEY          PF_KEY key management API
PF_NETLINK, PF_ROUTE
                routing API
PF_PACKET       Packet family
PF_ASH          Ash
PF_ECONET       Acorn Econet
PF_ATMSVC       ATM SVCs
PF_SNA          Linux SNA Project
PF_IRDA         IrDA sockets
PF_PPPOX        PPP over X sockets
PF_WANPIPE      Wanpipe API sockets
PF_BLUETOOTH    Bluetooth sockets
Sources: 
https://en.wikipedia.org/wiki/Berkeley_sockets
http://wiki.treck.com/Introduction_to_BSD_Sockets

No comments:

Post a Comment