gawkinet: Setting Up
1
1 2.5 Setting Up a Service
1 ========================
1
1 The preceding programs behaved as clients that connect to a server
1 somewhere on the Internet and request a particular service. Now we set
1 up such a service to mimic the behavior of the 'daytime' service. Such
1 a server does not know in advance who is going to connect to it over the
1 network. Therefore, we cannot insert a name for the host to connect to
1 in our special file name.
1
1 Start the following program in one window. Notice that the service
1 does not have the name 'daytime', but the number '8888'. From looking
1 at '/etc/services', you know that names like 'daytime' are just
1 mnemonics for predetermined 16-bit integers. Only the system
1 administrator ('root') could enter our new service into '/etc/services'
1 with an appropriate name. Also notice that the service name has to be
1 entered into a different field of the special file name because we are
1 setting up a server, not a client:
1
1 BEGIN {
1 print strftime() |& "/inet/tcp/8888/0/0"
1 close("/inet/tcp/8888/0/0")
1 }
1
1 Now open another window on the same machine. Copy the client program
11 given as the first example (⇒Establishing a TCP Connection TCP
Connecting.) to a new file and edit it, changing the name 'daytime' to
1 '8888'. Then start the modified client. You should get a reply like
1 this:
1
1 Sat Sep 27 19:08:16 CEST 1997
1
1 Both programs explicitly close the connection.
1
1 Now we will intentionally make a mistake to see what happens when the
1 name '8888' (the so-called port) is already used by another service.
1 Start the server program in both windows. The first one works, but the
1 second one complains that it could not open the connection. Each port
1 on a single machine can only be used by one server program at a time.
1 Now terminate the server program and change the name '8888' to 'echo'.
1 After restarting it, the server program does not run any more, and you
1 know why: there is already an 'echo' service running on your machine.
1 But even if this isn't true, you would not get your own 'echo' server
1 running on a Unix machine, because the ports with numbers smaller than
1 1024 ('echo' is at port 7) are reserved for 'root'. On machines running
1 some flavor of Microsoft Windows, there is no restriction that reserves
1 ports 1 to 1024 for a privileged user; hence, you can start an 'echo'
1 server there.
1
1 Turning this short server program into something really useful is
1 simple. Imagine a server that first reads a file name from the client
1 through the network connection, then does something with the file and
1 sends a result back to the client. The server-side processing could be:
1
1 BEGIN {
1 NetService = "/inet/tcp/8888/0/0"
1 NetService |& getline
1 CatPipe = ("cat " $1) # sets $0 and the fields
1 while ((CatPipe | getline) > 0)
1 print $0 |& NetService
1 close(NetService)
1 }
1
1 and we would have a remote copying facility. Such a server reads the
1 name of a file from any client that connects to it and transmits the
1 contents of the named file across the net. The server-side processing
1 could also be the execution of a command that is transmitted across the
1 network. From this example, you can see how simple it is to open up a
1 security hole on your machine. If you allow clients to connect to your
1 machine and execute arbitrary commands, anyone would be free to do 'rm
1 -rf *'.
1