Tcpserver and Tcpclient
The Tcpserver program (part of the uscpi-tcp package) is a replacement for the standard Unix inetd utility as well as easy “roll-your own” command-line based utility to make your console program run over a network connection.
Replacing INETD
If you run qmail or anything that intense that gets a lot of SMTP or other network connections, just run TCPSERVER. It greatly increases throughput on the system. INETD is sooo slooooow.
To run Tcpserver, the system starts it up with a command like this:
tcpserver -c 100 -u 82 -g 810 smtp $QMAIL_HOME/bin/qmail-smtpd &
The “c” is the maximum number of connections to run, “u” is the uid (numerical user id) to run the client as, “g” is the numeric group id (gid), “smtp’ refers to the SMTP port number (25), and the qmail-smtpd is the program that gets spawned with the network connection plumbing to communicate with the client.
Tcpserver spawns a seperate instance for each incoming connection. When the connection/transaction ends, the program ends as well, so no state or variable information is kept across invokations, unless you provide a method for doing so.
Tcprules
You can tell TCPSERVER which connection to allow and deny using the tcprules program. You can restrict access according to IP address (or addresses starting with a value (”127.0” means any IP address beginning with that string. In addition, you can specify on the rule to set certain environment variables that are passed into the client program. This is useful if you want to warn the client that the connection is special in some way, or to set up a default for that IP range.
The tcprules program actually just reads the rule file, of the form
127.0.0.1:allow,RELAYCLIENT="",TCPLOCALHOST="movie.edu"
and creates a cdb file (a file internally hashed to create a fasy lookup to the record). that is used by the tcpserver program.
Tcpclient
You can write a simple client for a TCPCLIENT service.
For starters, write a Perl program which writes to the network connection via file handle #7 and reads to the network via file handle #6. The TCPCLIENT command does all the plumbing.
#!/usr/bin/perl use IO::Handle; $nr = new IO::Handle; $nw = new IO::Handle; $nr->fdopen(6, "r"); $nw->fdopen(7, "w"); $nw->print("GET /\n"); $nw->flush(); print "READING...\n"; print $nr->getlines();
Tcpserver
Now lets create a simple TCPSERVER application. It reads the network connection as it was STDIN and STDOUT. So, this is gonna be so easy!
#!/usr/bin/perl $req = <STDIN>; print `ls -l`;
Tough, eh? It waits for any command (terminated with \n) and then dumps out its current directory listing to the client. Sure, its not a secure thing, but its easy to demonstrate!
To run the 2 programs you first start the server, then run the client
$ tcpserver 127.0.0.1 10003 myserver & [1] 2216 $ tcpclient 127.0.0.1 10003 myclient READING... total 240 -rwxrwxr-x 1 allen allen 79 Dec 2 10:58 myserver -rwxrwxr-x 1 allen allen 100 Nov 29 09:32 myclient $ kill 2216
Neat!