A number of Australian employees of Hewlett-Packard are facing the loss of their jobs as the global computer giant looks to slash its worldwide workforce by up to 30,000.
read more
David M Williams
Monday, 20 August 2007 20:18
Socket handling
This daemon, like any other, is designed to run in the background, and respond to network connections over TCP/IP. We can achieve this using the best known TCP/IP handling interface, Berkeley sockets. Our main() routine in dwserv.cpp sets it all up.
First, as we are going to be creating accounts, the server must be run as the super-user. However, this need not cause any fear, because our server is both robust and secure. And, of course, it is open source and can thus be inspected and enhanced.
We then establish a server socket, bound to the port we have chosen to use, as specified by the port variable above.
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (port);
if ((ppe = getprotobyname ("tcp")) == 0)
errexit ("Can't find tcp: %s\n", strerror (errno));
if ((sock = socket (PF_INET, SOCK_STREAM, ppe->p_proto)) < 0)
errexit ("Can't create socket: %s\n", strerror (errno));
if (bind (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)
errexit ("Can't bind to port: %s\n", strerror (errno));
if (listen (sock, qlen) < 0)
errexit ("Can't listen on port: %s\n", strerror (errno));
We set the server to run in the background by using the fork system call to create a second process, and by disconnecting the new process from the controlling tty. The original process is terminated.
if (pid) // Non-zero is parent.
{
printf ("Server pid is %d.\n", pid);
if (strlen (FileName) > 0)
log ("Server pid is %d.\n\n", pid);
exit (0);
}
fd = open ("/dev/tty", O_RDWR);
ioctl (fd, TIOCNOTTY, 0);
close (fd);
The server now simply sits passively in the background listening forever for network connections.
If an incoming request is made, then a new, slave, socket is created to process it. This is hived off into a child process. This permits the server socket and process to continue accepting more connections because it has offloaded the processing. This gives the appearance of allowing multiple simultaneous connections.
strcpy (Remote, IPtoAddress (fsin.sin_addr));
log ("%s: connect from %s\n", CurrentDateTime (nowtime), Remote);
The process method, called above, deciphers the command received from the client and calls the appropriate method to deal with it - such as CreateAccount. Any error messages are returned to the client through the same socket.
Think again. Most businesses only have PART of a DR plan - and this spells business disaster in the event of an IT disaster.
Download The Seven Sins of Disaster Recovery White Paper now and find out how you can prevent this happening to you.