Network Programming & Management
Network Programming & Management
UNIT I ELEMENTARY TCP SOCKETS 9 Introduction to Socket Programming Overview of TCP/IP Protocols Introduction to Sockets Socket address Structures Byte ordering functions address conversion functions Elementary TCP Sockets socket, connect, bind, listen, accept, read, write, close functions Iterative Server Concurrent Server. UNIT II APPLICATION DEVELOPMENT 9 TCP Echo Server TCP Echo Client Posix Signal handling Server with multiple clients boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown I/O multiplexing I/O Models select function shutdown function TCP echo Server (with multiplexing) poll function TCP echo Client (with Multiplexing). UNIT III SOCKET OPTIONS, ELEMENTARY UDP SOCKETS 9 Socket options getsocket and setsocket functions generic socket options IP socket options ICMP socket options TCP socket options Elementary UDP sockets UDP echo Server UDP echo Client Multiplexing TCP and UDP sockets Domain name system gethostbyname function Ipv6 support in DNS gethostbyadr function getservbyname and getservbyport functions. UNIT IV ADVANCED SOCKETS 9 Ipv4 and Ipv6 interoperability threaded servers thread creation and termination TCP echo server using threads Mutexes condition variables raw sockets raw socket creation raw socket output raw socket input ping program trace route program. UNIT V SIMPLE NETWORK MANAGEMENT 9 SNMP network management concepts SNMP management information standard MIBs SNMPv1 protocol and Practical issues introduction to RMON, SNMPv2 and SNMPv3.
b) Session established with temporary ports used for two way communication: If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client.
4. What is a protocol ?
A protocol is a set of rules of communication. Protocols are the building blocks of a network architecture. When writing programs that communicate across a computer network, one must first invent a protocol, an agreement on how those programs will communicate.
5. Explain TCP.
TCP is a transport layer protocol used by applications that require guaranteed delivery. Connection oriented: An application requests a connection to destination and uses connection to transfer data. Point-to-point: A TCP connection has two endpoints (no broadcast/multicast). Reliability: TCP guarantees that data will be delivered without loss, duplication or transmission errors.
3
Full duplex: Endpoints can exchange data in both directions simultaneously. Delivering TCP: TCP segments travel in IP datagrams. Internet routers only look at IP header to forward datagrams. Each segment contains a sequence number. Flow Control: Flow control is necessary when a computer in the network transmits data too fast for another computer to receive it .Flow control requires some form of feedback from the receiving peer. This is executed effectively due to the receivers buffer i.e., Window. TCP contains algorithms to estimate the round-trip time (RTT) between a client and server dynamically so that it knows how long to wait for an acknowledgment. For example, the RTT on a LAN can be milliseconds while across a WAN, it can be seconds. Furthermore, TCP continuously estimates the RTT of a given connection, because the RTT is affected by variations in the network traffic.
6. Explain UDP.
The User Datagram Protocol (UDP) provides a connectionless, unreliable transport service. Connectionless means that a communication session between hosts is not established before exchanging data. UDP is often used for communications that use broadcast or multicast Internet Protocol (IP) packets. The UDP connectionless packet delivery service is unreliable because it does not guarantee data packet delivery or send a notification if a packet is not delivered. Because delivery of UDP packets is not guaranteed, applications that use this protocol must supply their own mechanisms for reliability if necessary. Although UDP appears to have some limitations, it is useful in certain situations. Each UDP datagram has a length. The length of a datagram is passed to the receiving application along with the data.
TCP
Reliability: TCP is connection-oriented protocol. When a file or message is send it will get delivered unless connections fails. If connection lost, the server will request the lost part. There is no corruption while transferring a message. Ordered: If you send two messages along a connection, one after the other, you know the first message will get there first. You don't have to worry about data arriving in the wrong order Heavyweight: - when the low level parts of the
UDP
Reliability: UDP is connectionless protocol. When you a send a data or message, you don't know if it'll get there, it could get lost on the way. There may be corruption while transferring a message. Ordered: If you send two messages out, you don't know what order they'll arrive in i.e. not ordered.
TCP "stream" arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together. Streaming: Data is read as a "stream," with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call. Examples: World Wide Web (Apache TCP port 80), e-mail (SMTP TCP port 25 Postfix MTA), File Transfer Protocol (FTP port 21) and Secure Shell (OpenSSH port 22) etc.
tracking connections, etc. It's just fire and forget! This means it's a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets. Datagrams: Packets are sent individually and are guaranteed to be whole if they arrive. One packet per one read call. Examples: Domain Name System (DNS UDP port 53), streaming media applications such as IPTV or movies, Voice over IP (VoIP), Trivial File Transfer Protocol (TFTP) and online multiplayer games etc
3. The server must acknowledge (ACK) the client's SYN and the server must also send its own SYN containing the initial sequence number for the data that the server will send on the connection. The server sends its SYN and the ACK of the client's SYN in a single segment. J is the initial sequence number of client and K is that of server. ACK number is the initial sequence number plus 1. 4. The client must acknowledge the server's SYN. As the minimum number of packets required is 3, it is called three way handshake.
9.
Window scale option: The maximum window that either TCP can advertise to the other TCP is 65535 as the corresponding field in the TCP header occupies 16 bits. But high speed connections (45 Mbps/sec) or long delay paths require larger window which can be set by left shifting (scaling) by 0-14 bits giving rise to one gigabyte. This is effected with SO_RCVBUF socket option. Timestamp option: This option is needed for high speed connections to prevent possible data corruption caused by lost packets that then reappears. The latter two are sometimes called the "RFC 1323 options," as that RFC [Jacobson, Braden, and Borman 1992] specifies the options. They are also called the "long fat pipe options," since a network with either a high bandwidth or a long delay is called a long fat pipe.
When a client wants to contact a server, the client must identify the server with which it wants to communicate. Both TCP and UDP define a group of well-known ports to identify well-known services. Clients, on the other hand, use ephemeral ports, that is, short-lived ports. The Internet Assigned Numbers Authority (IANA) maintains a list of port number assignments.
The port numbers are divided into three ranges: 1. The well-known ports: 0 through 1023. These port numbers are controlled and assigned by the IANA. When possible, the same port is assigned to a given service for TCP, UDP, and SCTP. For example, port 80 is assigned for a Web server, for both TCP and UDP, even though all implementations currently use only TCP. 2. The registered ports: 1024 through 49151. These are not controlled by the IANA, but the IANA registers and lists the uses of these ports as a convenience to the community. When possible, the same port is assigned to a given service for both TCP and UDP. For example, ports 6000 through 6063 are assigned for an X Window server for both protocols, even though all implementations currently use only TCP. 3. The dynamic or private ports: 49152 through 65535. The IANA says nothing about these ports. These are what we call ephemeral ports.
We always pass these structures by reference (ie. we pass a pointer to the structure, not the structure itself ), and we always pass the size of the structure as another argument. When a socket function fills in a structure, the length is also passed by reference, so that its value can be updated by the function. We call these value-result arguments. Always set the structure variables to NULL (i.e. '\0' ) by using memset() or bzero() functions otherwise it may get unexpected junk values in your structure.
/* length of structure (16) */ /* AF_INET */ /* 16-bit TCP/UDP port number */ /* network byte ordered */ /* 32-bit IPv4 address */ /* network byte ordered */ /* unused */
Signed 16 bit integer Unsigned 8 bit integer Signed 32 bit integer Unsigned 32 bit integer Address family of socket address structure Length of socket address,normally uint32_t IPv4 address, normally uint32_t TCP or UDP port normally uint16_t
The POSIX specification requires only three members in the structure: sin_family, sin_addr, and sin_port. Both the IPv4 address and the TCP or UDP port number are always stored in the structure in network byte order. The 32-bit IPv4 address can be accessed in two different ways. For example, if serv is defined as an Internet socket address structure, then serv.sin_addr references the 32-bit IPv4 address as an in_addr structure, while serv.sin_addr.s_addr references the same 32-bit IPv4 address as an in_addr_t (typically an unsigned 32-bit integer).
The sin_zero member is unused, but we always set it to 0 when filling in one of these structures.
sin_family refers to the protocol family and the containd constant values .For eg AF_INET refers to IPv4 protocols.
Generic Socket Address Structure A socket address structure is always passed by reference when passed as an argument to any socket functions. Any socket function that takes one of these pointers as an argument must deal with socket address structures from any of the supported protocol families. A problem arises in how to declare the type of pointer that is passed. The solution chosen was to define a generic socket address structure in the <sys/socket.h> header, which is as shown below :
10
The socket functions are then defined as taking a pointer to the generic socket address structure, as shown here in the ANSI C function prototype for the bind function: int bind(int, struct sockaddr *, socklen_t);
This requires that any calls to these functions must cast the pointer to the protocol-specific socket address structure to be a pointer to a generic socket address structure. For example, struct sockaddr_in serv; /* IPv4 socket address structure */ * fill in serv{} */ bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));
From an application programmer's point of view, the only use of these generic socket address structures is to cast pointers to protocol-specific structures. From the kernel's perspective, another reason for using pointers to generic socket address structures as arguments is that the kernel must take the caller's pointer, cast it to a struct sockaddr *, and then look at the value of sa_family to determine the type of the structure.
IPv6 Socket Address Structure Defined by # include <netinet/in.h> header. The structure is shown below: struct in6_addr { uint8_t
s6_addr[16];
#define
}; SIN6_LEN
struct sockaddr_in6 { uint8_t sin6_len; / sa_family_t sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct in6_addr sin6_addr; uint32_t sin6_scope_id; };
* length of this struct (28) */ /* AF_INET6 */ /* transport layer port# */ /* network byte ordered */ /* flow information, undefined */ /* IPv6 address */ /* network byte ordered */ /* set of interfaces for a scope */
The SIN6_LEN constant must be defined if the system supports the length member for socket address structures. The IPv6 family is AF_INET6, whereas the IPv4 family is AF_INET.
11
The members in this structure are ordered so that if the sockaddr_in6 structure is 64bit aligned, so is the 128-bit sin6_addr member. On some 64-bit processors, data accesses of 64-bit values are optimized if stored on a 64-bit boundary. The sin6_flowinfo member is divided into two fields: o The low-order 20 bits are the flow label o The high-order 12 bits are reserved The sin6_scope_id identifies the scope zone in which a scoped address is meaningful, most commonly an interface index for a link-local address.
New Generic Socket Address Structure A new generic socket address structure was defined as part of the IPv6 sockets API, to overcome some of the shortcomings of the existing struct sockaddr. Unlike the struct sockaddr, the new struct sockaddr_storage is large enough to hold any socket address type supported by the system. The sockaddr_storage structure is defined by including the <netinet /in.h> header, which is as show below :
struct sockaddr_storage { uint8_t ss_len; /* length of this struct (implementation dependent) */ sa_family_t ss_family; /* address family: AF_xxx value */ /* implementation-dependent elements to provide: * a) alignment sufficient to fulfill the alignment requirements of * all socket address types that the system supports. * b) enough storage to hold any type of socket address that the * system supports. */ }; The sockaddr_storage type provides a generic socket address structure that is different from struct sockaddr in two ways: If any socket address structures that the system supports have alignment requirements, the sockaddr_storage provides the strictest alignment requirement. The sockaddr_storage is large enough to contain any socket address structure that the system supports. The sockaddr_storage must be cast or copied to the appropriate socket address structure for the address given in ss_family to access any other fields
12
Two of the socket address structures are fixed-length, while the Unix domain structure and the datalink structure are variable-length. To handle variable-length structures, whenever we pass a pointer to a socket address structure as an argument to one of the socket functions, we pass its length as another argument.
Socket address structure passed from process to kernel Three functions bind, connect, and sendto pass a socket address structure from the process to the kernel.
13
One argument to these three functions is the pointer to the socket address structure and another argument is the integer size of the structure. Since the kernel is passed both the pointer and the size of what the pointer points to, it knows exactly how much data to copy from the process into the kernel.
Socket address structure passed from kernel to process Four functions, accept, recvfrom, getsockname, and getpeername, pass a socket address structure from the kernel to the process. Two of the arguments to these four functions are the pointer to the socket address structure along with a pointer to an integer containing the size of the structure. The reason that the size changes from an integer to be a pointer to an integer is because the size is both a value when the function is called (it tells the kernel the size of the structure so that the kernel does not write past the end of the structure when filling it in) and a result when the function returns (it tells the process how much information the kernel actually stored in the structure). This type of argument is called a value-result argument.
14
Figure :Little-endian byte order and big-endian byte order for a 16-bit integer
The terms "little-endian" and "big-endian" indicate which end of the multibyte value, the little end or the big end, is stored at the starting address of the value.
2.Explain the need for byte ordering functions.What are the four functions used to convert between the 2 byte orders.
There is no standard between the two byte orderings- "little-endian" and "bigendian" and there are systems that use both formats.The byte ordering used by a given system is referred to as the host byte order. Network programmers must deal with these byte ordering differences because networking protocols must specify a network byte order. For example, in a TCP segment, there is a 16-bit port number and a 32-bit IPv4 address. The
15
sending protocol stack and the receiving protocol stack must agree on the order in which the bytes of these multi byte fields will be transmitted. POSIX specification say that certain fields in the socket address structures must be maintained in network byte order. The following four functions to convert between these two byte orders.
from the source to the destination. bcmp ( ) compares two arbitrary byte strings . The return value is zero if the two byte strings are identical; otherwise it is nonzero. The following functions are the ANSI C functions: #include <string.h> void *memset(void *dest, intc, size_tlen); void *memcpy(void *dest, const void *src, size_tnbytes); int memcmp(const void *ptr1, const void *ptr2, size_tnbytes); Returns: 0 if equal, <0 or >0 if unequal (see text) memset sets the specified number of bytes to the value c in the destination. memcpy is similar to bcopy, but the order of the two pointer arguments is swapped. bcopy correctly handles overlapping fields, while the behavior of memcpy is undefined if the source and destination overlap. The ANSI C memmove function must be used when the fields overlap. memcmp compares two arbitrary byte strings and returns 0 if they are identical. If not identical,the return value is either greater than 0 or less than 0, depending on whether the first unequal byte pointed to by ptr1 is greater than or less than the corresponding byte pointed to by ptr2.The comparison is done assuming the two unequal bytes are unsigned chars.
inet_addr( ) : does the same conversion, returning the 32 bit binary network byte ordered value as the return value. Although the IP address (0.0.0.0 through
17
255.255.255.255) are al valid addresses, the functions returns the constant INADDR_NONE on an error. This means the dotted-decimal string 255.255.255.255 cannot be handled by this function since its binary value appears to indicate failure of the function. This is deprecated and the new code should use inet_aton instead
inet_ntoa ( ) : The function inet_ntoa ( ) function converts a 32 bit binary network byte ordered IPv4 address into its corresponding dotted decimal string. The string pointed to by the return value of the function resides in static memory. This function takes structure as its arguments, not a pointer to a structure. (This is rare). inet_pton ( ) and inet_ntop( ) : These two functions are new with the IPv6 and work with both IPv4 and IPv6 addresses. The letter p and n stands for presentation and numeric. Presentation format for an address is often ASCII string and the numeric format is the binary value that goes into a socket address structure.
# include <arpa/inet.h> int inet_pton (int family, const char *strptr, void *addrptr); Returns: 1 if OK, 0 if input not a valid presentation format, -1 on error const char *inet_ntop (int family, cost void *addrptr, char *strptr, size_t len); Returns: pointer to result if OK, NULL on error The family argument for both function is either AF-INET or AF_ INET6. If family is not supported, both functions return an error with errno set to EAFNOSUPPORT. inet_pton ( ) : The first functions tries to convert the string pointed to by strptr, storing the binary results through the pointer addrptr. IF successful, the return value is 1. If the input string is not valid presentation format for the specified family, 0 is returned. inet_ntop( ) : This function does the reverse conversion from numeric (addrptr) to presentation (strptr). The len argument is the size of the destination, to prevent the function from overflowing the callers buffer. To help specify this size, following two definitions are defined by including the <netinet/in.h> header:
#define INET_ADDRSTRLEN 16 /*for IPv4 dotted-decimal */ #define INET6_ADDRSTRLEN 46 /*for IPv6 hex string */
18
If LEN is too small to hold the resulting presentation format including the terminating null, a null pointer is returned and errno is set to ENOSPC. The strptr argument to inet_ntop cannot be a null pointer. The caller must allocate memory for the destination and specify its size. On success this pointer is the return value of the function. Following figure shows the summary of address conversion functions.
#include "unp.h" char *sock_ntop(const struct sockaddr *sockaddr, socklen_t addrlen); Returns: non-null pointer if OK, NULL on error
19
TOPIC : Elementary TCP Sockets socket, connect, bind, listen, accept, read, write,
close functions
2.What are the elementary socket functions required to write a complete TCP client and server.
The following figures shows the Socket functions for elementary TCP client/serversocket(),connect(),bind(),listen(),accept(),read(),write(),close() functions. First server is started, then sometimes later a client is started that connects to the server. The client sends a request to the server, the server processes the request, and the server sends back reply to the client. This continues until the client closes its end of the connection, which sends an end of file notification to the server. The server then closes its end of the connections and either terminates or waits for a new connection.
20
5.What are the different types of errors possible when a TCP socket Initiates a connect() function.
If the client TCP receives no response to its SYN segment, ETIMEDOUT is returned. Some systems provide administrative control over this timeout. If the server's response to the client's SYN is a reset (RST), this indicates that no process is waiting for connections on the server host at the port specified (i.e., the server process is probably not running). This is a hard error and the error ECONNREFUSED is returned to the client as soon as the RST is received. An RST is a type of TCP segment that is sent by TCP when something is wrong. Three conditions that generate an RST are: when a SYN arrives for a port that has no listening server , when TCP wants to abort an existing connection, and when TCP receives a segment for a connection that does not exist.
If the client's SYN elicits an ICMP "destination unreachable" from some intermediate router, this is considered a soft error. The client kernel saves the message but keeps sending SYNs with the same time between each SYN as in the first scenario. If no response is received after some fixed amount of time , the saved ICMP error is returned to the process as either EHOSTUNREACH or ENETUNREACH. It is also possible that the remote system is not reachable by any route in the local system's forwarding table, or that the connect call returns without waiting at all.
bind assigns a protocol address to a socket, and what that protocol address means depends on the protocol. The second argument is a pointer to a protocol-specific address, and the third argument is the size of this address structure. With TCP, calling bind lets us specify a port number, an IP address, both, or neither. Servers bind their well-known port when they start.If a TCP client or server does not do this, the kernel chooses an ephemeral port for the socket when either connect or listen is called. It is normal for a TCP client to let the kernel choose an ephemeral port, unless the application requires a reserved port but it is rare for a TCP server to let the kernel choose an ephemeral port, since servers are known by their well-known port.
#include <sys/socket.h> #int listen (intsockfd, intbacklog); Returns: 0 if OK, -1 on error This function is normally called after both the socket and bind functions and must be called before calling the accept function. The kernel maintains two queues and the backlog argument to the listen function has historically specified the maximum value for the sum of both queues.These queues are : An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which the server is awaiting completion of the TCP three way handshake. These sockets are in the SYN_RECD state. A Completed Connection Queue which contains an entry for each client with whom three handshake has completed. These sockets are in the ESTABLISHED state.
process is put to sleep (assuming the default of a blocking socket). #include <sys/socket.h> int accept (intsockfd, struct sockaddr *cliaddr, socklen_t *addrlen); Returns: non-negative descriptor if OK, -1 on error The cliaddr and addrlen arguments are used to return the protocol address of the connected peer process (the client). addrlen is a value-result argument : before the call, we set the integer value pointed to by *addrlen to the size of the socket address structure pointed to by cliaddr and on return this integer value contains the actual number of bytes stored by the kernel in the socket address structure. If accept is successful, its return value is a brand new descriptor that was automatically created by the kernel. This new descriptor refers to the TCP connection with the client.
array of pointers, and c. Whether the environment of the calling process is passed to the new program or whether a new environment is specified. #include <unistd.h> int execl (const char *pathname, const char arg 0, / (char *) 0 */); int execv (const char *pathname, char *const argv[ ]); int execle (const char *pathname, const char *arg 0, ./ * (char *)0,char *const envp[] */); int execve (const char *pathname, char *const arg [], char *const envp[]); int execlp (const char *filename, const char arg 0, / (char *) 0 */); int execvp (const char *filename, char *const argv[]); These functions return to the caller only if an error occurs. Otherwise control passes to the start of the new program, normally the main function.
The default action of close with a TCP socket is to mark the socket as closed and return to the process immediately. The socket descriptor is no longer usable by the process: It cannot be used as an argument to read or write. But, TCP will try to send any data that is already queued to be sent to the other end, and after this occurs, the normal TCP connection termination sequence takes place
25
For lengthy transactions, a different sort of server is needed the concurrent server, as shown in the figure below. Here, Client A has already established a connection with the server, which has then created a child server process to handle the transaction. This allows the server to process Client B's request without waiting for A's transaction to complete. More than one child server can be started in this way.
********************************************************
26
27