PART-THREE

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Slide 1:

SOCKET Sockets are communication points on the same or different computers to exchange data. Sockets are supported by UNIX, Windows, Mac and many other operating systems. Sockets allow communication between two different processes on the same or different machines. In Unix, every I/O actions are done by writing or reading to a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else. To a programmer a socket looks and behaves much like a low level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. UNIX NETWORK PROGRAMMING UNIT-3 1

Slide 2:

The differences between sockets and normal file descriptors occurs in the creation of a socket and through a variety of special operations to control a socket. Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.3BSD. The sockets feature is now available with most current UNIX system releases. UNIX NETWORK PROGRAMMING UNIT-3 2

Slide 3:

Where is Socket used? A Unix Socket is used in client server application frameworks. A server is a process which does some function on request from a client. Most of the application level protocols like FTP make use of Sockets to establish connection between client and server and then for exchanging data. UNIX NETWORK PROGRAMMING UNIT-3 3

Slide 4:

UNIT-3 BERKELEY SOCKETS SOCKETS:- Sockets are a form of an IPC. It provides communication between processes on a single system and also between processes of different systems. There are some preliminary system calls are to be implemented the socket interface, which are listed below: Socket() recvfrom() Bind() sendfrom() Write() read() Listen() close() Accept() Connect() Recv() Send() UNIX NETWORK PROGRAMMING UNIT-3 4

Slide 5:

Sockets can be implemented for connection or connection less transmission of data as follows: Socket():- create endpoint, creates socket(server/client) Bind():- bind address, gives a name to the created socket. Listen():- indicating to receive connection, queue maintaining Accept():- wait until connection is established from the client. Connect():-connection to the server Write(),read(),recv(),send() are for the data transformation. Recvfrom() datagram transfer Sendto() datagram transfer Close() terminate UNIX NETWORK PROGRAMMING UNIT-3 5

Slide 6:

The following diagram shows how socket system calls are implemented for both connection and connection-less transfer. UNIX NETWORK PROGRAMMING UNIT-3 6

Slide 7:

UNIX NETWORK PROGRAMMING UNIT-3 7

Slide 8:

UNIX NETWORK PROGRAMMING UNIT-3 8 SOCKET RECVFROM() SENDTO() RECVFROM() SENDTO() BIND() SOCKET BIND() PROCESS REQUEST BLOCKS UNTIL DATA RECEIVED FROM A CLIENT SERVER CLIENT CONNECTION LESS PROTOCOL Data request Data reply

Slide 9:

In client server implementation, i.e., connection oriented transfer first the server is started, then sometime later a client is started that connects to the server. But in connection less protocol, the system calls are different. The client does not establish a connection with the server. Instead, the client just sends a datagram to the server using the sendto system call, which requires the address of the destination as a parameter. The server does not have to accept a connection from a client. Instead, the server just issues a recvfrom system call that waits until data arrives from some client. The recvfrom returns the network address of the client process, along with the datagram, so the server can send its response to the correct process. UNIX NETWORK PROGRAMMING UNIT-3 9

Slide 10:

How to make client: The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. The two processes each establish their own sockets. The steps involved in establishing a socket on the client side are as follows: Create a socket with the socket () system call. Connect the socket to the address of the server using the connect () system call. Send and receive data. There are a number of ways to do this, but the simplest is to use the read () and write () system calls. UNIX NETWORK PROGRAMMING UNIT-3 10

Slide 11:

How to make a server: The steps involved in establishing a socket on the server side are as follows: Create a socket with the socket () system call. Bind the socket to an address using the bind () system call. For a server socket on the Internet, an address consists of a port number on the host machine. Listen for connections with the listen () system call. Accept a connection with the accept () system call. This call typically blocks until a client connects with the server. Send and receive data using the read () and write () system calls. UNIX NETWORK PROGRAMMING UNIT-3 11

Slide 12:

UNIX DOMAIN PROTOCOLS Unix domain sockets used in 4.3 BSD systems. It supports the protocols called unix domain protocols. These sockets in the unix domain can be used to communicate with processes on the same unix system. These are not true communication protocols. These are limited to 4.3 BSD only. The unix domain protocols provides the following features The ability to pass access right from one process to another {file descriptor} These perform connection oriented and connection less interface. Both can be reliable, since they exist only within the kernel and are not transmitted across external facilities such as a common line between system. There is no encapsulation perform on the unix domain messages. The name space used by the unix domain protocols consist the following parameters UNIX NETWORK PROGRAMMING UNIT-3 12

Slide 13:

A simple association is {protocol, local-address, local-process, foreign-address, foreign-process} For example {unixstr,0,/tmp/log.01528,0,/dev/logfile} The protocol here is unistr, which stands for unix stream, the connection oriented protocol. The local-process is tmp/log.01528, and the foreign process is /dev/logfile. UNIX NETWORK PROGRAMMING UNIT-3 13

Slide 14:

Both the local-address and the foreign-address are zero, since the pathnames on the local host are only address used in this domain. For connection less protocol the protocol is unixdg to specify the datagram protocol and remaining all are same. SOCKET ADDRESSES BSD networking system calls uses a pointer to a socket address structure as an argument. There are various structures which are used in Unix Socket Programming to hold information about the address and port and other information. Most socket functions require a pointer to a socket address structure as an argument. Socket address is defined as structure with address family and protocol specific address as follows in <sys/socket.h> Struct sockaddr { u_short sa_family; /*address family UNIX, INET,XNS*/ char sa_data[14]; /*protocol specific address*/ }; UNIX NETWORK PROGRAMMING UNIT-3 14

Slide 15:

UNIX NETWORK PROGRAMMING UNIT-3 15 The 14 bytes of protocol specific address are interpreted according to the type of address family. There are families which supports these system calls UNIX,INTERNET,XNS. This is a generic socket address structure which will be passed in most of the socket function calls. Here is the description of the member fields: Attribute Values Description sa_family AF_INET AF_UNIX AF_NS AF_IMPLINK This represents an address family. In most of the Internet based applications we use AF_INET. sa_data Protocol Specific Address The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family we will use port number IP address which is represented by sockaddr_in structure defined below.

Slide 16:

UNIX NETWORK PROGRAMMING UNIT-3 16 Second structure that helps you to reference to the socket's elements is as follows: struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };

Slide 17:

UNIX NETWORK PROGRAMMING UNIT-3 17 Attribute Values Description sin_family AF_INET AF_UNIX AF_NS AF_IMPLINK This represents an address family. In most of the Internet based applications we use AF_INET. sin_port Service Port A 16 bit port number in Network Byte Order. sin_addr IP Address A 32 bit IP address in Network Byte Order. sin_zero Not Used You just set this value to NULL as this is not being used.

Slide 18:

UNIX NETWORK PROGRAMMING UNIT-3 18 UNIX FAMILY:- For the unix domain, the following structure is define d in <sys/ un.h > struct sockaddr_in { short sin_family; /*socket unix family: AF_UNIX*/ u-short sin_port; /* 16-bit port number */ struct in_addr sin_addr; /* 32-bit netid/ hostid */ char sin_zero [8]; /* unused*/ }; Mechanism of handling the socket address structures of different sizes The networks system calls work with any of the supported domains, hence, the domain structures are interpreted with the generic structure of the address as follows:- The network system calls take two arguments The address of the generic sockaddr arguments Size of the actual protocol specific structure.

Slide 19:

UNIX NETWORK PROGRAMMING UNIT-3 19 ELEMENTARY SOCKET SYSTEM CALLS The network program is performed through the following socket system calls, which are preliminary and referred as Elementary socket system calls. socket() system call It specifies the type of communication protocol (TCP, UDP, XNS/SPP, etc) It is defined in <sys/ socket.h > Syntax:- int socket(int family, int type, int protocol); Family:- it is one of the form AF_XXXX, AF_UNIX UNIX INTERNET DOMAIN AF_INET INTERNET DOMAIN AF_NS XNS SYSTEM Type:- it specifies the type of the message as SOCK_STREAM stream socket SOCK_RAW raw socket SOCK_DGRAM datagram socket SOCK_SEQPACKET sequenced packet socket Protocol:- the type of the protocol, normally as zero as user applications.

Slide 20:

UNIX NETWORK PROGRAMMING UNIT-3 20 The socket system call returns a small integer value, similar to a file descriptor. The association is {protocol, local-addr, local-process, foreign-addr, foreign-process} This system call executed successfully then it returns sockfd otherwise -1. 2. bind() system call It is used to assign a name to an unnamed socket. It is defined in <sys/socket.h> Syntax:- int bind(int sockfd, struct sockaddr *myadr, int addrlen); Sockfd is the socket id which is returned by the socket system call Myaddr is the pointer to sockaddr structure and addrlen is the length of the socket address. It returns the integer value of address. Here first argument is sockfd is already opened socket. The second argument is a pointer to a protocol-specific address and the third argument is size of this address structure. There are three uses of bind. Servers register their well-known address with the system. Both connection-oriented and connection less server need to do this before accepting client request. A client can register a specific address for itself, hence the connection oriented client does not need this system call. A connection less clients get assurance by this unique address. It specifies the values for the local-address and local-process.

Slide 21:

UNIX NETWORK PROGRAMMING UNIT-3 21 3. listen () system call It is executed after socket and bind system calls by the server. It is used by the connection-oriented server only to indicate the willingness to receive connection. Syntax:- int listen(int sockfd, int backlog); Backlog specifies how many connection requests can be queued by the system while it waits for the server to execute the accept() system call. Normally this value is 5. 4. accept () system call:- The server executed the accept() system call to wait until an actual connection from a client is established. Syntax:- int accept(int sockfd, struct sockaddr *peer, int *addrlen); Accept takes the connection request on the queue and creates another socket with the same property of as sockfd. The address of the connected peer process client is return by peer and addrlen arguments property of as sockfd. This system call returns one among the following three types of integer value: 1. An integer value that either error indicator or another new socket descriptor (sockfd). 2. Address of the client process 3. Size of the address All 5-tuples which are required for networking are defined after this system call.

Slide 22:

UNIX NETWORK PROGRAMMING UNIT-3 22 PROTOCOL LOCAL-ADDR LOCAL-PROCESS FOREIGN-ADDR FOREIGN-PROCESS CONNECTION ORIENTED SERVER SOCKET() BIND() LISTEN(), ACCEPT() CONNECTION ORIENTED CLIENT SOCKET() CONNECT() CONNECTION LESS SERVER SOCKET() BIND() RECVFROM() CONNECTION LESS SERVER SOCKET() BIND() SENDTO()

Slide 23:

UNIX NETWORK PROGRAMMING UNIT-3 23 5.connect() system call:- The connection is established with connect() system call between client and server. Connect() system call connects with reference to socket system call. It is executed by the client immediately after the socket() system call. Syntax:- int connect(int sockfd, struct sockaddr *seraddr, int addlen); Sockfd is the value returned by the socket() system call. Seraddr is the pointer to a socket address. Addlen is the size of the address. It has two different functionality for the mode of transmission Connection oriented and connection less Connection oriented:- the connect() system call establishes actual connection between the local system and the foreign system. There is no need to bind. Connection less :- The connection less client can also use the connect system call. Only data grams from this address will be received by the socket. In this approach , there is no need to specify the destination address for every datagram that send and bind() is needed for client also,

Slide 24:

UNIX NETWORK PROGRAMMING UNIT-3 24 6.send () system call :- it is used to data transformation and similar as write system call. Connection is needed. Syntax:- int send(int sockfd, char *buff, int nbytes, int flags); 7. sendto () system call:- used to data transformation and similar as write system call. Syntax:- int sendto(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); 8.recv() system call:- Used to data transformation and similar as read system call. Syntax:- int recv(int sockfd, char *buff, int nbytes, int flags); 9.recvfrom() system call:- used to data transformation and similar as read system call. Syntax:- int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *from, int addrlen); It returns the length of the datagram which is received. Here buff specifies the starting address of the buffer (message array) nbytes specifies the number of bytes in the buffer (number of bytes in the message) flags specifies following constant or zero. the constant are MSG_OOB send or receive out-of-band data MSG_PEEK peek at incoming message (rec or recvfrom) MSG_DONTROUTE bypass routing

Slide 25:

UNIX NETWORK PROGRAMMING UNIT-3 25 *to specifies the protocol specific address of where the data is to be sent of length addrlen *from specifies the protocol specific address of who sent this data. All the above four system calls return the length of the data that was written or read as the value of the function. 10.close () system call:- This is the system call to close the socket. Syntax:- int close(int sockfd); In case of connection-oriented, the system must assure that any data within the kernel that still has to be transmitted or acknowledged, is sent. Normally, the system returns from the close(), immediately, but the kernel still tries to send any data already queued. Byte ordering routines:- There are four functions which handles the potential byte order differences between different architectures and network protocols. These routines are called byte ordering routines. 1.u_long htonl(u_long hostlong); convert host-to-network, long integer 2.u_short htons(u_short hostshort); convert host-to-network,short integer 3.u_long ntohl(u_long nettlong); convert network-to-host,long integer 4.u_short ntohs(u_long netshort); convert network-to-host,short integer These functions were designed for the internet protocols These functions are operate on unsigned integer values.

Slide 26:

UNIX NETWORK PROGRAMMING UNIT-3 26 ADVANCED SOCKET SYSTEM CALLS There are some more socket system calls which can be used in various purposes to held the complex network programming. readv(), writev(), sendmsg(), recvmsg(), getpeername(), getsockname(), getsockop(), shutdown() and select(). readv() and writev():- These functions are used to read into or write from buffers . This type of write/read is called scatter read and gather write. writev system call writes the buffers specified by iov[0], iov[1],…..iov[iov count -1]. readv system call fills one buffer before proceeding to the next buffer in the iov array. Syntax:- int writev(int fd, struct iovec iov[], int iovcount); int readv(int fd, struct iovec iov[], int iovcount); These two system calls use the following structure that is defined in <sys/uio.h> Struct iovec { caddr_t iov_base; /*starting address of buffer */ int iov_len; /* size of buffer in bytes */ };

Slide 27:

UNIX NETWORK PROGRAMMING UNIT-3 27 2. sendmsg and recvmsg system call:- These 2 system calls are the most general of all read and write system calls. These are defined in <sys/socket.h> Syntax:- int sendmsg(int sockfd, struct msghdr msg[], int flags); int recvmsg(int sockfd, struct msghdr msg[], int flags); msghdr is the structure consists all the attributes about message to be handled. 3.getpeername() system call:- This system call returns the name of the peer process (client) that is connected to a given socket. It is defined in <sys/socket,h> Syntax:- int getpeername(int sockfd, struct sockaddr *peer, int *addrlen); It returns the name of peer process means, it returns foreign-addr and process elements of the 5-tuple associated with sockfd. The original 4.3 BSD release do not support this system call for unix domain protocols. 4. getsockname() system call:- It returns the name associated with a socket. i.e., it returns the local-addr and local process values of 5-tuple association. Syntax:- int getsockname(int sockfd, struct sockaddr *peer, int *addrlen);

Slide 28:

UNIX NETWORK PROGRAMMING UNIT-3 28 The original 4.3 BSD release do not support this system call for unix domain protocols. 5.getsockopt() and setsockopt() system calls:- These two system calls manipulate the options associated with a socket , such as pass SPP header on input, pass SPP header on output etc., These two system calls apply only to sockets. Syntax:- int getsockopt(int sockfd, int level, int optname, char *optval, int *optlen); int setsockopt(int sockfd, int level, int optname, char *optval, int *optlen); 6.shutdown() system call:- This system call provides to close already opened socket. Syntax:- int shutdown(int sockfd, int howto); Here howto argument 0 no more data can be received on the socket. 1 no more output to be allowed on the socket. 2 both sends and receives to be disallowed. 7.select () system call:- This system call can be used when dealing with multiple descriptiors.

Slide 29:

UNIX NETWORK PROGRAMMING UNIT-3 29 RESERVED PORTS There are two ways for a process to have an Internet port or an XNS port assigned to a socket. Process can request specific port. The process can automatically assign a port. 4.3 BSD support for both XNS, internet of a reserved port. In the internet domain, any TCP or UDP port in the range 1-1023 is reserved an in the XNS domain ports in the range 1-2999 are reserved. A process is not allowed to bind a reserved port unless its effective user ID is zero. 4.3BSD provides a library function that assigns a reserved TCP stream socket to its caller: int rresvport(int *aport); This functions creates an Internet stream socket and binds a reserved port to the socket. The socket descriptor is returned as the value of the function, unless an error occur, in which case -1 is returned. The integer pointed to by aport is the first port number to IPORT_RESERVED-1. It is in the header <netinet/in.h> If this bind fails with an errno, then this function decrements the port number and tries again. If it finally reaches port 512 and finds it already in use, it sets errno to EAGAIN and returns -1. If this function returns successfully, it not only returns the socket as the value of the function, but the port number is also returned in the location pointed to by aport.

Slide 30:

UNIX NETWORK PROGRAMMING UNIT-3 30 Internet XNS Reserved ports 1-1023 1-2999 Ports automatically assigned by system 1024-5000 3000-65535 Ports assigned by rresvport () 512-1023

Slide 31:

UNIX NETWORK PROGRAMMING UNIT-3 31 SOCKET OPTIONS:- The purpose of socket options are to manipulate data in a socket. i.e., performed operations into a socket. There are 3 types of socket options. Set or get socket option Function control socket option I/O control socket option. Set or get socket option:- To get or set option into a socket using two system calls setsockopt() and getsockopt() Setsocktopt ():- to set option into a socket. Syntax:- int setsockopt(int sockfd, int level, int optname, char *optval, int *optlen); sockfd:- to open a socket. level:- in which level communicate two process. optval:- specifies the size of the size set by the system on return to specify the amount of data stored into the optval variable. optlen:- it represented the length of option value. Getsockopt():- this system call is used to retrieve the option of the socket. Syntax:- int getsockopt(int sockfd, int level, int optname, char *optval, int *optlen);

Slide 32:

UNIX NETWORK PROGRAMMING UNIT-3 32 The above system call executed successfully it returns zero otherwise -1. Function control system call:- The fctnl system call is used to change the properties of a file that is already open. #include<fctnl.h> Syntax:- int fnctl(int sockfd, int cmd, int arg); sockfd is used to open a socket . cmd specifies the operation to be performed cmd values are F_SETOWN, F_GETOWN,F_SETFL and F_GETFL F_SETOWN command sets either the process ID or the process group ID to receive the SIGIO signal for the socket associated with fd. Every socket has an associated process group number, similar to the terminal process gropu number. For socket, it process group number is initialized to zero and can be set by calling fctnl with the F_SETOWN command. The arg value for the F_SETOWN command can be either a positive integer, specifying a process ID, or a negative integer, specifying a process group ID. The F_GETOWN command returns , as the return value from the fctnl system call, either the process ID or the process group ID associated with a socket. A file flag bits are set and examined with the F_SETFL and F_GETFL commands.

Slide 33:

UNIX NETWORK PROGRAMMING UNIT-3 33 F_SETFL set the status of the flag for the file as the value of the system call. F_GETFL return the status of the flag for this file as the value of the system call. The arg values that can be used with the F_GETFL and F_SETFL commands. Arg Meaning FAPPEND Append on each write FASYNC Signal process group when data ready FCREAT Create if nonexistent FEXCL Error if already created FINDELAY Nonblocking I/O FTRUNC Truncate to zero length

Slide 34:

UNIX NETWORK PROGRAMMING UNIT-3 34 I/O CONTROL SOCKET OPTION:- IO control socket option provided by ioctl system call. This system call is also used to change the behaviour of an open file. It is provided device operations. #include<sys/ioctl.h> SYNTAX: int ioctl(int sockfd, unsigned long request, char *arg); This system call performs a variety of control functions on terminals, devices, sockets and streams. It contains the definitions for request for all possible operations. The main difference between the fctnl and ioctl is that the former is intended for any open file, while the latter is intended for device-specific operations. It contain 4 kinds of operations File operations Socket operations Routing operations Interface operations

Slide 35:

UNIX NETWORK PROGRAMMING UNIT-3 35 ASYNCHRONOUS I/O:- An asynchronous can be defines as data transfer between two different process with different speed in different clock cycles. Asynchronous I/O allows the process to tell the kernel to notify it when specified file descriptor is ready for I/O. It is also called signal-driven I/O. The notification from the kernel to the user process takes place with a signal, the SIGIO signal. To do asynchronous I/O, a process must perform the following steps. 1. Process performed I/O operations for generate file descriptor and intimated to the kernel. 2. When kernel identified fd for I/O operations, kernel generated signal SIGIO. 3. Process performed asynchronous I/O by using fctnl() or ioctl(). 4. Set F_SETOWN in cmd argument provided either specific process or multiple process for communication and argument represented as FASYNC() is provided for asynchronous data transmission.

Slide 36:

UNIX NETWORK PROGRAMMING UNIT-3 36 INPUT OUTPUT MULTIPLEXING Multiplexing means to combine many into one. Incase, a process that reads input from more that one source. For example:- 4.3 BSD line print spooler, which can read two programs on to print spooler. Here lpr command creates three files in the spooling directory, multiplexes and demultiplexes as follows: It opens two sockets, A unix stream socket to receive print requests from processes on the same host. A TCP socket to receive print requests from processes on other hosts. Here, it does not know when requests will arrive on the two sockets, so it can start a read on one socket and it can block the other. The multiplexing of different I/O channels can be handled by few techniques. Polling technique: The two sockets can be set without blocking process. The process has to execute a loop that reads from each socket, and if nothing is available to read, write some amount of time before trying the reads again. This is called polling. In this technique, the software polls each socket at some interval and if no data is available to read, the process waits by calling the sleep function. Polling can waste computer resources, since most of the time there is no work done.

Slide 37:

UNIX NETWORK PROGRAMMING UNIT-3 37 Spawn the processes:- The process can fork one child process to handle each I/O channel. Here 2 processes are spawned by fork() system call, one to read from the Unix stream socket and one to read from the TCP socket. Each child process calls read and blocks until data is available. When child returns from its read() it passes the data to the parent process using some form of IPC (pipes, fifos, msgqueues). Asynchronous I/O technique:- SIGIO signal handles the multiplexing but if more than one descriptor has been enabled for asynchronous I/O , the SIGIO signal doesn’t tell which descriptor is ready for I/O. This technique is only available for terminals and sockets under 4.3 BSD. Select system call This system call for 4.3 BSD, allows the user process to instruct the kernel to wait for any one of multiple events to occur and to wake up the process only when one of these events occur.

Slide 38:

UNIX NETWORK PROGRAMMING UNIT-3 38 Out of band data Passing of data from one process to the another process in priority way is called out of band data. With out of band , the data is send before any other data which data which is already in the buffer of sending side. Out of band data is defined only for stream sockets. Similarly, at receiving end to pass this data to its user process ahead of any data that has buffered. This out of band data is supported in only TCP and SPP. TCP and SPP treat out of band data differently. SPP only allows a single byte of out of band data where as TCP has no restrictions. To send an out of band data message three system calls can be used. Send(), sendto() or sendmsg(). To receive an out of band message three system calls can be used. Recv(), recvfrom() or recvmsg(). There are exist several possibilities while receiving the out of band data at other end. These can be handled by TCP or SPP as follows:

Slide 39:

UNIX NETWORK PROGRAMMING UNIT-3 39 TCP OUT OF BAND DATA TCP sends the notification that out of band data exist before sending the out of band data itself. If the process executes one of the three receive system calls. If the out of band data stream. This data byte can only be read by specifying the MSG_OOB flag to the receiving system calls. If the out of band data is not received in line, it is possible to lose some intermediate out of band data when the out of band data arrives faster than it is processes by the user. SPP OUT OF BAND DATA SPP handles the out of band data as follows: The out of band data byte is received along with a special flag specifying that the byte contains out of band data. If the socket has a process group, the SIGURG( signal urgent) signal is generated for the process group of the socket. The process can be read the out of band data byte by calling any one of three receive system calls, specifying the MSG_OOB flag. Though SPP only single byte stream is returned. If there is no out of band data then error of EINVAL is returned by these three system calls. The byte in the normal data stream is remembered, regardless whether the process reads the out of band data or not. The process continues reading data from the socket, since the system remembers the position of the out of band data byte in the data stream.

Slide 40:

UNIX NETWORK PROGRAMMING UNIT-3 40 SOCKET AND SIGNALS There is some relation between sockets and signals, the signals are generated for a sockets. There are three signals that can be generated for a socket. SIGIO This signal indicates that a socket is ready for asynchronous I/O. The signal is sent to the process group of the socket. This process group is established by calling ioctl or fctnl. This signal is sent to the process group only if the process has enabled asynchronous I/O on the socket. SIGURG This signal indicates that an urgent condition is present on a socket. An urgent condition is either the arrival or out of band data on the socket or the presence of control status information to be read from the master side of the pseudo terminal that has been put into packet mode. The signal is sent to the process group of the signal. SIGPIPE This signal indicates that we can no longer write to a socket, pipe or FIFO. This signal is sent only to the process associated with the socket. No process group of the socket is used for this signal.

Slide 41:

UNIX NETWORK PROGRAMMING UNIT-3 41 The "internet super server", or inetd , is available on all Unix(like) systems, providing many of the basic network services available. The relationship between the daemon and several of the config files in the /etc/ directory. What is inetd? In traditional Unix scenarios, one server (daemon) process watches for connections on a particular port, and handles incoming requests. Now if a machine offers many services, many daemon processes would be needed, mostly running idle but still wasting resources like memory. The internet super server, inetd, is an approach to this problem. It listens on a number of ports, and when it receives a request it then determines which program to run to handle the request and starts an instance of that program.

Slide 42:

UNIX NETWORK PROGRAMMING UNIT-3 42 INTERNET SUPERSERVER The 4.3 BSD system or other server system can have may daemons just waiting for a request to arrive i.e., there could be an internet FTP daemon, an internet TELNET daemon and internet TFTP daemon, a remote login daemon, a remote shell daemon and so on. All these services must had a process associated with it. In other server system, the servers system is waiting for a request from client, when the request came, server creates a child process and assign the job to the child process and again server goes in wait state. (concurrent server) In 4.3 BSD system, all these can be handled in simple way by providing an internet superserver. It is done by inetd process. This daemon handles only TCP/UDP but not XNS,Unix domain protocols. The purpose of inetd process is single process, provided service with multiple process it reduced number of process in a single system i.e., reduced of potential service. inetd provides two features It allows a single process to waiting to service multiple connections requests. By this feature, the number of processes are reduced It simplifies the writing of the daemon processes to handle the requests, all the start-up details( boot, create socket, bind the servers well known address of the socket, wait for connection , fork).All the tasks are done by inetd, before the actual server is invoked.

Slide 43:

UNIX NETWORK PROGRAMMING UNIT-3 43 Configuring inetd - /etc/inetd.conf The operation of inetd is controlled by its own config file, named /etc/inetd.conf, The inetd.conf file basically provides enabling and mapping of services the systems administrator would like to have multiplexed through inetd , indicating which program should be started for incoming requests on which port. inetd configuration file:- The inetd process establishes itself as a daemon. It the reads the file /etc/inetd.cong (configuration file) to initialize itself. This is a text file, it specifies the services that the super server is to listen and do the services. Each line contains the fields as followed, Service-name must be in /etc/services Socket-type stream or datagram Protocol must be in /etc/protocols (tcp/udp) Wait-flag wait or nowait Login-name from /etc/passwd Server-program full pathname to exec Server-program-arg maximum of 5 arguments

Slide 44:

UNIX NETWORK PROGRAMMING UNIT-3 44 service-name: The service name indicates the port inetd should listen on. It is either a decimal number, or a name matching a service name given in /etc/services . socket-type: The communications socket type, the different types are "stream" for a TCP stream, "dgram" for an UDP service, "raw" for a raw socket, "rdm" for reliably delivered message and "seqpacket" for a sequenced packet socket. The most common socket types are "stream" and "dgram". server-program This field is the full path of the program that gets started. program-arguments This field contains the argument vector argv[] of the program started, including the program name and additional arguments the systems administrator may need to specify for the server program that is started. For example ftp,stream,tcp,nowait,root,/etc/telnetd,telnetd The actual name of the server is always passed as the first argument to a program when it is executed.

Slide 45:

UNIX NETWORK PROGRAMMING UNIT-2 45 Bind() SOCKET Listen() Select() Accept() Fork() Close connected socket (if stream socket) Close all files other than socket Dup socket to fds 0, 1 and 2 Setgid () and setuid () Exec() server Parent child

Slide 46:

UNIX NETWORK PROGRAMMING UNIT-3 46 A complete scenario of inetd daemon Step 1: it read the /etc/inetd.conf file and creates a socket of the type (stream/datagram) for all the services specified in the file. Step 2: for each socket, bind is executed for every socket, specifying the well known address for the server this TCP or UDP port number is obtained by looking up the service name field from the configuration file in the /etc/services file.. Step 3: listen() is executed for stream sockets specifying a willingness to receive connections on the socket and the queue length for incoming connections. This step is not done for the data gram sockets. Step 4: a select() is executed to wait for the first socket to become ready for reading. That a stream socket is considered ready for reading when a connection request arrives for that socket. A data gram socket is ready for reading when a data gram arrives. At this point the inetd daemon just waits for the select system call to return. Step 5: when a socket is ready for reading, if it is a stream socket, an accept system call is executed to accept the connection.

Slide 47:

UNIX NETWORK PROGRAMMING UNIT-3 47 Step 6: The inetd daemon forks and the child process handles the services request. The child closes all the file descriptors other than the socket descriptors that it is handling and then calls the dup2 to cause the socket to be duplicated on file descriptors 0, 1 and 2. The original socket descriptor is closed. The only file descriptors that are open in the child are 0, 1 and 2. it then call the getpwname to get the password file entry for the login name that is specified in the /etc/inetd.conf file. If this entry does not have a user ID of zero then the child becomes a specified user by executing setpid() and setgid() system calls. The child process now does an exec to execute the appropriate server-program to handle the request, passing the arguments that are specified in the configuration file. Step 7: if the socket is stream socket, the parent process must close the connected socket. The parent goes back and executed the select() system call again, waiting for the next socket to become ready for reading. Special case:- If another request arrives for the same server, control is returned to the parent process as soon as it executes the select(). The steps 1-7 are executed again and another child process handles this new service request.

Slide 48:

UNIX NETWORK PROGRAMMING UNIT-2 48 By using etc/inetd.config startup file provided name for each socket. Inetd process identified requests from clients by using listen() . This system call contain backlog i.e. ready queue from various clients. Select () ready for fd from multiple fds, that fd can be accepted by inetd process with executed accept system call. That process executed fork(), when executed fork() created child process. Child process close all file descriptors other than parent socket fd. Child process executed dup() for communication with other process generated fds 0, 1 and 2. Child process set userid and groupid executed server program exec() provided communication with other process. Finally parent process close socket fd and control goes to select(). The same procedure repeated again until backlog is empty.

Slide 49:

UNIX NETWORK PROGRAMMING UNIT-3 49 Socket implementation within the unix kernel Socket are implemented in unix kernel as in 4.3 BSD systems. All the socket system calls, algorithms and code to support the communication protocols (TCP, UDP. IDP etc.,) are completely exists within the kernel. The actual kernel implementation separates the network system into three layers. User process Socket system call interface Communication protocol ( tcp / udp / ip ) Data link device driver ( ethernet )

Slide 50:

UNIX NETWORK PROGRAMMING UNIT-3 50 Sockpair() system call This system call is implemented only for the unix domain int socketpair(int family, int type, int protocol, int sockvec[2]); This returns two descriptors, Sockvec[0], and sockvec[1],that are unnamed and connected. This system call is similar to the pipe system call, but socketpair returns a pair of socket descriptors, not file descriptors. These are bidirectional, connection-oriented, unix domain sockets stream pipes. Stream pipes:- A stream pipe is similar to a pipe, but a stream pipe is bidirectional. Stream pipes are used in both 4.3 BSD and in the system V release 3 for passing file descriptors between processes. Stream pipes are created by spipe(), the purpose of stream pipes are passing file descriptors between 2 different systems. spipe contains two versions unnamed_spipe named_spipe

Slide 51:

UNIX NETWORK PROGRAMMING UNIT-3 51 PASSING FILE DESCRIPTORS PROCESS TABLE ENTRY Fd0 Fd1 Fd2 Current file position / i -node ptr PROCESS TABLE ENTRY Fd0 Fd1 Fd2 i -node information Sharing a file table entry between 2 processes

Slide 52:

UNIX NETWORK PROGRAMMING UNIT-3 52 When we want to pass a file from one process to another process, sending process open a file descriptor and generate id for system table. System table is a common area sharing both sending and receiving process. In the above diagram, sending process open file descriptor 3 for file and send file to a system table. Receiving process open file descriptor 2, receive from file to system table, here sending and receiving process fds are not same i.e., sending and receiving fds are may or may not be same. To pass a file from one process to another process, two process must be connected unix domain socket. The connection must be either stream socket or datagram socket. Send message file by sendmsg() and receive message by recvmsg().