DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with sockets

Inetd

One of the daemons provided with the UNIX system is inetd, the so called ``Internet super-server.'' inetd is invoked at boot time by the Service Access Controller, and determines the services for which it is to listen from the file /etc/inetd.conf. Once this information has been read and a pristine environment created, inetd proceeds to create one socket for each service it is to listen for, binding the appropriate port number to each socket.

inetd then performs a select on all these sockets for read availability, waiting for somebody wishing a connection to the service corresponding to that socket. inetd then performs an accept on the socket in question, forks, dups the new socket to file descriptors 0 and 1 (standard input and standard output), closes other open file descriptors, and execs the appropriate server.

Servers making use of inetd are considerably simplified, as inetd takes care of most of the communication work required in establishing a connection. The server invoked by inetd expects the socket connected to its client to be on file descriptors 0 and 1, and may immediately do operations such as read, write, send, or recv. Indeed, servers may use buffered I/O as provided by the stdio conventions, as long as they remember to use fflush when appropriate.

One call that may be of interest to individuals writing servers to be invoked by inetd is the getpeername call, which returns the address of the peer (process) connected on the other end of the socket. For example, to log the Internet address in ``dot notation'' (for example, ``128.32.0.4'') of a client connected to a server under inetd, the following code might be used:

   struct sockaddr_in name;
   int namelen = sizeof(name);
    ...
   if (getpeername(0,
   	(struct sockaddr *)&name, &namelen) < 0) {
   	syslog(LOG_ERR, "getpeername: %m");
   	exit(1);
   } else
   	syslog(LOG_INFO, "Connection from %s",
   		inet_ntoa(name.sin_addr));
    ...
While the getpeername call is especially useful when writing programs to run with inetd, it can be used under other circumstances.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004