12/6/2007
Contents
Multiprotocol/Multiservice Servers
INTRODUCTION
Introduction Multi-protocol server Example of Multi-protocol server Multi-service server Example of multi-service server Super servers
One server per protocol
Last time: Concurrent Connection-Oriented server - echo server - that supports multiple clients at the same time using one single process. Today: Extension to more complicated servers:
A multiprotocol server consists of a single process that uses I/O multiplexing to handle communication over either UDP or TCP. The server initially opens two sockets
one that uses a connectionless transport (UDP) one that uses a connection-oriented transport (TCP).
Advantage: Ease of control Disadvantages:
.
=
Because many services can be accessed through either UDP or TCP, each service can require two servers.
The cost to maintain consistency
TCP server
UDP server
Overhead of replication
Single process server accommodating multiple transport protocols and also multiple services.
Multiprotocol Server Design
UDP and TCP servers should both contain the same code needed to perform the computation
socket for UDP requests
socket for TCP requests
socket for a TCP connection
Algorithm
The server then uses I/O multiplexing to wait for one of the sockets to become ready : If the TCP socket becomes ready, a client has requested a TCP connection.
The server uses accept to obtain the new connection, and then communicates with the client over that connection.
If the UDP socket becomes ready, a client has sent a request in the form of a UDP datagram.
The server uses recvfrom to read the request and record the sender's endpoint address
1
12/6/2007
Process Structure of a sequential, multiprotocol server server
Outline of a Multiprotocol Server 1.The multiprotocol server consists of a single process
At any time, the server has at most three sockets open:
2.The process opens master sockets for both UDP and TCP
•one for UDP requests, •one for TCP connection requests, socket for UDP requests
socket for TCP requests
socket for a TCP connection
•a temporary one for an individual TCP connection
3.It uses select to wait for either or both of them to become ready
Example – Multiprotocol DAYTIME Server
5.If the UDP socket becomes ready, the server reads the request and responds
/* main- iterative server for DAYTIME service */ int main(int argc, *argv[]) { char *service = “daytime”; /*service name or port number */ struct sockaddr_in fsin; /* the from address of a client */ int alen; /* length of a client’s address */ char buf[LINELEN+1]; /* line buffer */ int tsock; /* TCP master socket */ int usock; /* UDP socket */ fd_set rfds; /* read file descriptor set */
/* daytimed.c – main : DAYTIME service for TCP and UDP */ /* include header files here */ #define QLEN 5 /*max. connection queue length */ #define LINELEN 128 extern int errno; int daytime(char buf[]) int errexit (const char *format, …); int passiveTCP (const char *service, int qlen); int passiveUDP (const char *service);
tsock = passiveTCP (service, QLEN); usock = passiveUDP (service);
4.If the TCP socket becomes ready, the server accepts the new connection and handles requests using it
/* check arguments - not detailed here*/
if ( FD_ISSET (tsock, &rfds)) { int ssock;
FD_ZERO (&rfds);
alen = sizeof (fsin); ssock = accept(tsock,(struct sockaddr *)&fsin, &alen); if ( ssock < 0) errexit (“accept: %s\n, strerror (errno)); daytime(buf); (void) write (ssock, buf, strlen(buf)); (void) close (ssock);
while(1) { FD_SET (tsock, &rfds); FD_SET (usock, &rfds); if ( select (FD_SETSIZE, &rfds, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0) < 0) errexit (“select: %s\n”, strerror(errno)); }
2
12/6/2007
/* Daytime – fill the given buffer with the time of day */ if ( FD_ISSET (usock, &rfds)) { alen = sizeof (fsin); if ( recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0 ) errexit (“recvfrom: %s\n, strerror (errno)); daytime(buf); (void) sendto (usock, buf, strlen(buf),0, (struct sockaddr *)&fsin, alen); }
int daytime ( char buf[]) { char *ctime(); time_t now; (void) time (&now); sprintf (buf, “%s”, ctime(&now)); }
}
Concept of Shared Code
“A multi-protocol server design permits the designer to create a single procedure that responds to requests for a given service and to call that procedure regardless of whether requests arrive via UDP or TCP”
Contents
Motivation for a Multiservice Server
Same as for multiprotocol server Also reduces the total code required
Introduction Multi-protocol server Example of Multi-protocol server Multi-service server Example of multi-service server Super servers
A Sequential Connectionless, Multi-service Server Mapping table
server
Master sockets (one for each service being offered)
Server application process
Operating system
3
12/6/2007
A Sequential Connectionless, Multi-service Server (Continued.) Algorithm 1. 2. 3. 4. 5. 6. 7.
A Sequential Connection-Oriented, Multiservice Server Mapping table
The server opens a set of UDP sockets and binds each to a well-known port. Keep a table to map sockets to services. Call select to wait for a datagram to arrive. When a datagram arrives, get the descriptor. Look up the mapping table, find the right procedure. compute a response and send a reply. Go back to step 3.
Master sockets (one for each service offered)
A Sequential Connection-Oriented, Multiservice Server (Continued.)
6.
Create a socket for each service Keep a table to map sockets to services. Call select to wait for an incoming connection request. Call accept to create a slave socket. Through the slave socket, interact with a client, and then close it. Go back to step 3.
A Concurrent, Connection-Oriented, Multi-service Server (Continued.)
Socket for one individual connection
Operating system
A Concurrent, Connection-Oriented, Multi-service Server
Algorithm 1. 2. 3. 4. 5.
Server application process
server
Implementations
The Master-slave Implementation
A Single-Process Implementation.
Multi-thread Implementation
A Concurrent, Connection-Oriented, Multiservice Server (Continued.) Mapping table
A Master-Slave Implementation 1. 2. 3. 4.
5.
Create a socket for each service. Call select to wait for any connection request. If one of the master sockets becomes ready, call accept to open a connection. The master process closes the connection as soon as it has created a slave process; The connection remains open in the slave process. The slave process communicates with the client over the connection.
Server application process
server
slave1
…
Master sockets (one for each service offered)
slaven
…
Socket for each individual connection
Operating system
4
12/6/2007
A Single-Process Implementation 1. 2. 3. 4. 5.
Algorithm Create a socket and bind to the well-known port for the service. Add this socket to a list. Call select to wait for I/O on existing sockets. If original socket is ready, call accept to obtain the next connection, and add the new socket to the list. If a socket other than the original is ready, call read to obtain the next request, compute a response, and then send a reply. Go back to step 2.
A Single-Process Implementation (Continued.) Mapping table
Master sockets (one for each service offered)
Multiservice server - superd.c /* superd.c – main, doTCP*/ /* include header files here */ #define UDP_SERV 0 #define TCP_SERV 1 #define NOSOCK -1 extern int errno; struct service { char *sv_name; char sv_useTCP; int sv_sock; void (*sv_func) (int); }
#define QLEN 5 #define LINELEN 128 extern u_short portbase; /*from passivesock() */ /* main - Super-server main program */ int main(int argc, *argv[]) { struct service *psv; /* service table pointer */ fd_set afds, rfds; /* read file descriptors */
Server application process
server
Socket for each individual connection
Operating system
int TCPechod(int), TCPchargend(int), TCPdaytimed(int), TCPtimed(int); int errexit (const char *format, …); int passiveTCP (const char *service, int qlen); int passiveUDP (const char *service); void doTCP( struct service *psv); struct service svent[ ] = { {“echo”, TCP_SERV, NOSOCK, TCPechod}, {“chargen”, TCP_SERV, NOSOCK, TCPchargend}, {“daytime”, TCP_SERV, NOSOCK, TCPdaytimed}, {“time”, TCP_SERV, NOSOCK, TCPtimed}, {0,0,0,0}, };
switch (argc) { case 1: break; case 2: portbase = (u_short) atoi(argv[1]); break; default: errexit(“usage: superd [portbase]\n”); }
5
12/6/2007
while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if ( select (FD_SETSIZE, &rfds, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0) < 0) errexit (“select: %s\n”, strerror(errno));
FD_ZERO (&afds); for (psv = &svent[0]; psv->sv_name; ++psv) { if (psv->sv_useTCP) psv->sv_sock = passiveTCP(psv->sv_name, QLEN); else psv->sv_sock = passiveUDP(psv->sv_name); FD_SET(psv->sv_sock, &afds); }
for (psv=&svent[0];psv->sv_name; ++psv){ if (FD_ISSET(psv->sv_sock, &rfds)){ if (psv->sv_useTCP) doTCP(psv); else psv->sv_func(psv->sv_sock); } } }
Contents
Introduction Multi-protocol server Example of Multi-protocol server Multi-service server Example of multi-service server Super servers
/* doTCP - handle a TCP service connection request */ void doTCP ( struct service *psv) { struct sockaddr_in fsin; /* request from addr. */ int alen; /* from-address length */ int ssock; alen = sizeof( fsin); ssock = accept(psv->sv_sock, (struct sockaddr *) &fsin, &alen); if (ssock == NOSOCK) errexit (“accept: %s\n”, strerror(errno)); if (_beginthread((void (*)(void *)) psv->sv_func, 0, (void *) ssock) ==(unsigned long) -1) errexit (“_beginthread: %s\n”, strerror(errno)); }
A Multiservice, Multiprotocol Server -- A Super Server
Motivation:
Offer many services without using excessive resources (entries in the process table and swap space) Reduces overhead without eliminating the functionality
6
12/6/2007
Calling separate programs from a multiservice server
A Multiservice, Multiprotocol Server
Operates much the same as a conventional multiservice server.
Initiates both UDP and TCP sockets or either one for the same service.
Calls select to wait for any socket to become ready.
If a UDP socket becomes ready, the server reads the datagram, computes a response, and sends a reply.
If a TCP socket becomes ready, the server calls accept to create a new connection to do communication by making it either iterative or concurrent.
Disadvantage of discussed designs is the lack of flexibility
Calling separate programs from a multiservice server (Continued.)
Server application process
slave1
slaven
prog1
progn
execve used
…
Super servers are configurable – the set of services that the server handles can be changed without recompiling source code. Static configuration:
…
Socket for each individual connection
Operating system
BSD UNIX Super Server: inetd
Super server example: BSD UNIX program inetd
supplies many of the small TCP/IP services that network managers use for testing Uses a configuration file (text file) to make addition of new services easier
/etc/inetd.conf
Specified by a configuration file when server process begins execution
Dynamic configuration:
Master sockets (one for each service offered)
Using independently compiled program to handle each service Process call execve can help
Server Configuration
fork used server
We must terminate the server and recompile sources
We should break a large multiservice server into independent components
Mapping table
Changing the code for any single service requires recompilation of the entire multiservice server
Occurs while super server is running. The server reads the modified configuration file and changed the services it offers.
inetd
inetd reads a configuration file that lists all the services it should handle.
inetd creates a socket for each listed service, and adds the socket to a fd_set given to select()
Dynamically configurable
7
12/6/2007
Fields of an entry in an inetd configuration file Field
Meaning
Service name
Name of a service to be offered
Socket Type
Type of socket to use
Protocol
Name of protocol to be used with service
Wait status
Wait (iterative) or nowait (concurrent)
Userid
Login id. (in UNIX root has absolute privilege)
Server program Name of service program to execute or internal to use the version of the code compiled into inetd
Arguments
Some versions of Unix provide a service very similar to inetd called xinetd.
# comments start echo stream echo dgram chargen stream chargen dgram ftp stream telnet stream finger stream # Authentication auth stream -l -e -o # TFTP tftp dgram /tftpboot
with # tcp nowait root internal udp wait root internal tcp nowait root internal udp wait root internal tcp nowait root /usr/sbin/ftpd ftpd -l tcp nowait root /usr/sbin/telnetd telnetd tcp nowait root /usr/sbin/fingerd fingerd tcp
nowait
nobody
udp
wait
root
/usr/sbin/in.identd in.identd
/usr/sbin/tftpd tftpd -s
0 or more arguments to be passed
xinetd
Example of /etc/inetd.conf
Summary A multiprotocol server is: a single-process server that can handle multiple transport protocols
configuration scheme is different basic idea (functionality) is the same…
A multiservice server is: a single-process server that can handle multiple services
Both of them do :
Reduce overhead Save system resources Make maintenance easier
8