1 /* -*- c-basic-offset: 8; -*-
3 * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
4 * Permission to use or modify this software and its documentation only for
5 * educational purposes and without fee is hereby granted, provided that
6 * the above copyright notice appear in all copies. The author makes no
7 * representations about the suitability of this software for any purpose.
8 * It is provided "as is" without express or implied warranty.
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
23 char *host
; /* hostname or dotted-decimal string */
26 /* DefinE global variables */
27 int bindport
; /* 0 or TCP or UDP port number to bind */
28 /* set by -b or -l options */
29 int broadcast
; /* SO_BROADCAST */
30 int cbreak
; /* set terminal to cbreak mode */
31 int chunkwrite
; /* write in small chunks; not all-at-once */
32 int client
= 1; /* acting as client is the default */
33 int connectudp
= 1; /* connect UDP client */
34 int crlf
; /* convert newline to CR/LF & vice versa */
35 int debug
; /* SO_DEBUG */
36 int dofork
; /* concurrent server, do a fork() */
37 int dontroute
; /* SO_DONTROUTE */
38 char foreignip
[32]; /* foreign IP address, dotted-decimal string */
39 int foreignport
; /* foreign port number */
40 int halfclose
; /* TCP half close option */
41 int ignorewerr
; /* true if write() errors should be ignored */
42 int iptos
= -1; /* IP_TOS opton */
43 int ipttl
= -1; /* IP_TTL opton */
44 char joinip
[32]; /* multicast IP address, dotted-decimal string */
45 int keepalive
; /* SO_KEEPALIVE */
46 long linger
= -1; /* 0 or positive turns on option */
47 int listenq
= 5; /* listen queue for TCP Server */
48 char localip
[32]; /* local IP address, dotted-decimal string */
49 int maxseg
; /* TCP_MAXSEG */
50 int mcastttl
; /* multicast TTL */
51 int msgpeek
; /* MSG_PEEK */
52 int nodelay
; /* TCP_NODELAY (Nagle algorithm) */
53 int nbuf
= 1024; /* number of buffers to write (sink mode) */
54 int onesbcast
; /* set IP_ONESBCAST for 255.255.255.255 bcasts */
55 int pauseclose
; /* #ms to sleep after recv FIN, before close */
56 int pauseinit
; /* #ms to sleep before first read */
57 int pauselisten
; /* #ms to sleep after listen() */
58 int pauserw
; /* #ms to sleep before each read or write */
59 int reuseaddr
; /* SO_REUSEADDR */
60 int reuseport
; /* SO_REUSEPORT */
61 int readlen
= 1024; /* default read length for socket */
62 int writelen
= 1024; /* default write length for socket */
63 int recvdstaddr
; /* IP_RECVDSTADDR option */
64 int rcvbuflen
; /* size for SO_RCVBUF */
65 int sndbuflen
; /* size for SO_SNDBUF */
66 long rcvtimeo
; /* SO_RCVTIMEO */
67 long sndtimeo
; /* SO_SNDTIMEO */
68 int sroute_cnt
; /* count of #IP addresses in route */
69 char *rbuf
; /* pointer that is malloc'ed */
70 char *wbuf
; /* pointer that is malloc'ed */
71 int server
; /* to act as server requires -s option */
72 int sigio
; /* send SIGIO */
73 int sourcesink
; /* source/sink mode */
74 int udp
; /* use UDP instead of TCP */
75 int urgwrite
; /* write urgent byte after this write */
76 int verbose
; /* each -v increments this by 1 */
77 int usewritev
; /* use writev() instead of write() */
79 struct sockaddr_in cliaddr
, servaddr
;
81 static void usage(const char *);
84 main(int argc
, char *argv
[])
92 opterr
= 0; /* don't want getopt() writing to stderr */
93 while ( (c
= getopt(argc
, argv
, "2b:cf:g:hij:kl:n:op:q:r:st:uvw:x:y:ABCDEFG:H:IJ:KL:NO:P:Q:R:S:TU:VWX:YZ")) != -1) {
96 case '2': /* use 255.255.255.255 as broadcast address */
102 bindport
= atoi(optarg
);
105 case 'c': /* convert newline to CR/LF & vice versa */
109 case 'f': /* foreign IP address and port#: a.b.c.d.p */
110 if ( (ptr
= strrchr(optarg
, '.')) == NULL
)
111 usage("invalid -f option");
113 *ptr
++ = 0; /* null replaces final period */
114 foreignport
= atoi(ptr
); /* port number */
115 strcpy(foreignip
, optarg
); /* save dotted-decimal IP */
118 case 'g': /* loose source route */
119 sroute_doopt(0, optarg
);
122 case 'h': /* TCP half-close option */
126 case 'i': /* source/sink option */
130 #ifdef IP_ADD_MEMBERSHIP
131 case 'j': /* join multicast group a.b.c.d */
132 strcpy(joinip
, optarg
); /* save dotted-decimal IP */
136 case 'k': /* chunk-write option */
140 case 'l': /* local IP address and port#: a.b.c.d.p */
141 if ( (ptr
= strrchr(optarg
, '.')) == NULL
)
142 usage("invalid -l option");
144 *ptr
++ = 0; /* null replaces final period */
145 bindport
= atoi(ptr
); /* port number */
146 strcpy(localip
, optarg
); /* save dotted-decimal IP */
149 case 'n': /* number of buffers to write */
153 case 'o': /* do not connect UDP client */
157 case 'p': /* pause before each read or write */
158 pauserw
= atoi(optarg
);
161 case 'q': /* listen queue for TCP server */
162 listenq
= atoi(optarg
);
165 case 'r': /* read() length */
166 readlen
= atoi(optarg
);
169 case 's': /* server */
174 #ifdef IP_MULTICAST_TTL
175 case 't': /* IP_MULTICAST_TTL */
176 mcastttl
= atoi(optarg
);
180 case 'u': /* use UDP instead of TCP */
184 case 'v': /* output what's going on */
188 case 'w': /* write() length */
189 writelen
= atoi(optarg
);
192 case 'x': /* SO_RCVTIMEO socket option */
193 rcvtimeo
= atol(optarg
);
196 case 'y': /* SO_SNDTIMEO socket option */
197 sndtimeo
= atol(optarg
);
200 case 'A': /* SO_REUSEADDR socket option */
204 case 'B': /* SO_BROADCAST socket option */
208 case 'C': /* set standard input to cbreak mode */
212 case 'D': /* SO_DEBUG socket option */
216 case 'E': /* IP_RECVDSTADDR socket option */
220 case 'F': /* concurrent server, do a fork() */
224 case 'G': /* strict source route */
225 sroute_doopt(1, optarg
);
229 case 'H': /* IP_TOS socket option */
230 iptos
= atoi(optarg
);
234 case 'I': /* SIGIO signal */
239 case 'J': /* IP_TTL socket option */
240 ipttl
= atoi(optarg
);
244 case 'K': /* SO_KEEPALIVE socket option */
248 case 'L': /* SO_LINGER socket option */
249 linger
= atol(optarg
);
252 case 'N': /* SO_NODELAY socket option */
256 case 'O': /* pause before listen(), before first accept() */
257 pauselisten
= atoi(optarg
);
260 case 'P': /* pause before first read() */
261 pauseinit
= atoi(optarg
);
264 case 'Q': /* pause after receiving FIN, but before close() */
265 pauseclose
= atoi(optarg
);
268 case 'R': /* SO_RCVBUF socket option */
269 rcvbuflen
= atoi(optarg
);
272 case 'S': /* SO_SNDBUF socket option */
273 sndbuflen
= atoi(optarg
);
277 case 'T': /* SO_REUSEPORT socket option */
282 case 'U': /* when to write urgent byte */
283 urgwrite
= atoi(optarg
);
286 case 'V': /* use writev() instead of write() */
288 chunkwrite
= 1; /* implies this option too */
291 case 'W': /* ignore write errors */
295 case 'X': /* TCP maximum segment size option */
296 maxseg
= atoi(optarg
);
299 case 'Y': /* SO_DONTROUTE socket option */
303 case 'Z': /* MSG_PEEK option */
308 usage("unrecognized option");
312 /* check for options that don't make sense */
313 if (udp
&& halfclose
)
314 usage("can't specify -h and -u");
316 usage("can't specify -D and -u");
317 if (udp
&& linger
>= 0)
318 usage("can't specify -L and -u");
320 usage("can't specify -N and -u");
322 if (udp
== 0 && broadcast
)
323 usage("can't specify -B with TCP");
325 if (udp
== 0 && foreignip
[0] != 0)
326 usage("can't specify -f with TCP");
329 if (optind
!= argc
-2)
330 usage("missing <hostname> and/or <port>");
332 port
= argv
[optind
+1];
335 /* If server specifies host and port, then local address is
336 bound to the "host" argument, instead of being wildcarded. */
337 if (optind
== argc
-2) {
339 port
= argv
[optind
+1];
340 } else if (optind
== argc
-1) {
344 usage("missing <port>");
348 fd
= cliopen(host
, port
);
350 fd
= servopen(host
, port
);
352 if (sourcesink
) { /* ignore stdin/stdout */
365 } else { /* copy stdin/stdout to/from socket */
376 usage(const char *msg
)
379 "usage: sock [ options ] <host> <port> (for client; default)\n"
380 " sock [ options ] -s [ <IPaddr> ] <port> (for server)\n"
381 " sock [ options ] -i <host> <port> (for \"source\" client)\n"
382 " sock [ options ] -i -s [ <IPaddr> ] <port> (for \"sink\" server)\n"
383 "options: -b n bind n as client's local port number\n"
384 " -c convert newline to CR/LF & vice versa\n"
385 " -f a.b.c.d.p foreign IP address = a.b.c.d, foreign port# = p\n"
386 " -g a.b.c.d loose source route\n"
387 " -h issue TCP half close on standard input EOF\n"
388 " -i \"source\" data to socket, \"sink\" data from socket (w/-s)\n"
389 #ifdef IP_ADD_MEMBERSHIP
390 " -j a.b.c.d join multicast group\n"
392 " -k write or writev in chunks\n"
393 " -l a.b.c.d.p client's local IP address = a.b.c.d, local port# = p\n"
394 " -n n #buffers to write for \"source\" client (default 1024)\n"
395 " -o do NOT connect UDP client\n"
396 " -p n #ms to pause before each read or write (source/sink)\n"
397 " -q n size of listen queue for TCP server (default 5)\n"
398 " -r n #bytes per read() for \"sink\" server (default 1024)\n"
399 " -s operate as server instead of client\n"
400 #ifdef IP_MULTICAST_TTL
401 " -t n set multicast ttl\n"
403 " -u use UDP instead of TCP\n"
405 " -w n #bytes per write() for \"source\" client (default 1024)\n"
406 " -x n #ms for SO_RCVTIMEO (receive timeout)\n"
407 " -y n #ms for SO_SNDTIMEO (send timeout)\n"
408 " -A SO_REUSEADDR option\n"
409 " -B SO_BROADCAST option\n"
410 " -C set terminal to cbreak mode\n"
411 " -D SO_DEBUG option\n"
412 " -E IP_RECVDSTADDR option\n"
413 " -F fork after connection accepted (TCP concurrent server)\n"
414 " -G a.b.c.d strict source route\n"
416 " -H n IP_TOS option (16=min del, 8=max thru, 4=max rel, 2=min$)\n"
420 " -J n IP_TTL option\n"
422 " -K SO_KEEPALIVE option\n"
423 " -L n SO_LINGER option, n = linger time\n"
424 " -N TCP_NODELAY option\n"
425 " -O n #ms to pause after listen, but before first accept\n"
426 " -P n #ms to pause before first read or write (source/sink)\n"
427 " -Q n #ms to pause after receiving FIN, but before close\n"
428 " -R n SO_RCVBUF option\n"
429 " -S n SO_SNDBUF option\n"
431 " -T SO_REUSEPORT option\n"
433 " -U n enter urgent mode before write number n (source only)\n"
434 " -V use writev() instead of write(); enables -k too\n"
435 " -W ignore write errors for sink client\n"
436 " -X n TCP_MAXSEG option (set MSS)\n"
437 " -Y SO_DONTROUTE option\n"
440 " -2 IP_ONESBCAST option (255.255.255.255 for broadcast\n"