C.3 sock Program
Stevens' sock program first appeared in
TCPv1, where it was frequently used to generate special case
conditions, most of which were then examined in the text using
tcpdump. The handy thing about the program is that it
generates so many different scenarios, saving us from having to
write special test programs.
We do not show the source code for the program
in this text (it is over 2,000 lines of C), but the source code is
freely available (see the Preface).
The program operates in one of four modes, and
each mode can use either TCP or UDP:
-
Standard input, standard output client
(Figure C.1):
In the client mode, everything read from
standard input is written to the network, and everything received
from the network is written to standard output. The server's IP
address and port must be specified, and in the case of TCP, an
active open is performed.
-
Standard input, standard output server鈥擳his mode
is similar to the previous mode, except the program binds a
well-known port to its socket, and in the case of TCP, performs a
passive open.
-
Source client (Figure C.2):
The program performs a fixed number of writes to
a network of some specified size.
-
Sink server (Figure C.3):
The program performs a fixed number of reads
from a network.
These four operating modes correspond to the
following four commands:
sock [options] hostname service
|
sock [options] -s [hostname] service
|
sock [options] -i hostname service
|
sock [options] -is [hostname] service
|
where hostname
is a hostname or IP address and service is a service name or port number. In
the two server modes, the wildcard address is bound, unless the
optional hostname is
specified.
About 40 command-line options can also be
specified, and these drive the optional features of the program. We
will not detail these options here, but many of the socket options
described in Chapter 7 can be set. Executing the
program without any arguments prints a summary of the options.
-b n bind n as client's local port number
-c convert newline to CR/LF & vice versa
-f a.b.c.d.p foreign IP address = a.b.c.d, foreign port # = p
-g a.b.c.d loose source route
-h issue TCP half-close on standard input EOF
-i "source" data to socket, "sink" data from socket (w/-s)
-j a.b.c.d join multicast group
-k write or writev in chunks
-l a.b.c.d.p client's local IP address = a.b.c.d, local port # = p
-n n # buffers to write for "source" client (default 1024)
-o do NOT connect UDP client
-p n # ms to pause before each read or write (source/sink)
-q n size of listen queue for TCP server (default 5)
-r n # bytes per read() for "sink" server (default 1024)
-s operate as server instead of client
-t n set multicast ttl
-u use UDP instead of TCP
-v verbose
-w n # bytes per write() for "source" client (default 1024)
-x n # ms for SO_RCVTIMEO (receive timeout)
-y n # ms for SO_SNDTIMEO (send timeout)
-A SO_REUSEADDR option
-B SO_BROADCAST option
-C set terminal to cbreak mode
-D SO_DEBUG option
-E IP_RECVDSTADDR option
-F fork after connection accepted (TCP concurrent server)
-G a.b.c.d strict source route
-H n IP_TOS option (16=min del, 8=max thru, 4=max rel, 2=min cost)
-I SIGIO signal
-J n IP_TTL option
-K SO_KEEPALIVE option
-L n SO_LINGER option, n = linger time
-N TCP_NODELAY option
-O n # ms to pause after listen, but before first accept
-P n # ms to pause before first read or write (source/sink)
-Q n # ms to pause after receiving FIN, but before close
-R n SO_RCVBUF option
-S n SO_SNDBUF option
-T SO_REUSEPORT option
-U n enter urgent mode before write number n (source only)
-V use writev() instead of write(); enables -k too
-W ignore write errors for sink client
-X n TCP_MAXSEG option (set MSS)
-Y SO_DONTROUTE option
-Z MSG_PEEK
|