Home | History | Annotate | Line # | Download | only in tftp-proxy
tftp-proxy.c revision 1.1.1.1
      1  1.1.1.1  martti /*	$NetBSD: tftp-proxy.c,v 1.1.1.1 2009/12/01 07:03:10 martti Exp $	*/
      2  1.1.1.1  martti /* $OpenBSD: tftp-proxy.c,v 1.2 2006/12/20 03:33:38 joel Exp $
      3  1.1.1.1  martti  *
      4  1.1.1.1  martti  * Copyright (c) 2005 DLS Internet Services
      5  1.1.1.1  martti  * Copyright (c) 2004, 2005 Camiel Dobbelaar, <cd (at) sentia.nl>
      6  1.1.1.1  martti  *
      7  1.1.1.1  martti  * Redistribution and use in source and binary forms, with or without
      8  1.1.1.1  martti  * modification, are permitted provided that the following conditions
      9  1.1.1.1  martti  * are met:
     10  1.1.1.1  martti  *
     11  1.1.1.1  martti  * 1. Redistributions of source code must retain the above copyright
     12  1.1.1.1  martti  *    notice, this list of conditions and the following disclaimer.
     13  1.1.1.1  martti  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.1.1  martti  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.1.1  martti  *    documentation and/or other materials provided with the distribution.
     16  1.1.1.1  martti  * 3. The name of the author may not be used to endorse or promote products
     17  1.1.1.1  martti  *    derived from this software without specific prior written permission.
     18  1.1.1.1  martti  *
     19  1.1.1.1  martti  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1.1.1  martti  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1.1.1  martti  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1.1.1  martti  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1.1.1  martti  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1.1.1  martti  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1.1.1  martti  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1.1.1  martti  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1.1.1  martti  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1.1.1  martti  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1.1.1  martti  */
     30  1.1.1.1  martti 
     31  1.1.1.1  martti #include <sys/types.h>
     32  1.1.1.1  martti #include <sys/ioctl.h>
     33  1.1.1.1  martti #include <sys/uio.h>
     34  1.1.1.1  martti #include <unistd.h>
     35  1.1.1.1  martti 
     36  1.1.1.1  martti #include <netinet/in.h>
     37  1.1.1.1  martti #include <arpa/inet.h>
     38  1.1.1.1  martti #include <arpa/tftp.h>
     39  1.1.1.1  martti #include <sys/socket.h>
     40  1.1.1.1  martti #include <net/if.h>
     41  1.1.1.1  martti #include <net/pfvar.h>
     42  1.1.1.1  martti 
     43  1.1.1.1  martti #include <errno.h>
     44  1.1.1.1  martti #include <pwd.h>
     45  1.1.1.1  martti #include <stdio.h>
     46  1.1.1.1  martti #include <syslog.h>
     47  1.1.1.1  martti #include <string.h>
     48  1.1.1.1  martti #include <stdlib.h>
     49  1.1.1.1  martti 
     50  1.1.1.1  martti #include "filter.h"
     51  1.1.1.1  martti 
     52  1.1.1.1  martti #define CHROOT_DIR	"/var/empty"
     53  1.1.1.1  martti #define NOPRIV_USER	"proxy"
     54  1.1.1.1  martti 
     55  1.1.1.1  martti #define PF_NAT_PROXY_PORT_LOW	50001
     56  1.1.1.1  martti #define PF_NAT_PROXY_PORT_HIGH	65535
     57  1.1.1.1  martti 
     58  1.1.1.1  martti #define DEFTRANSWAIT	2
     59  1.1.1.1  martti #define NTOP_BUFS	4
     60  1.1.1.1  martti #define PKTSIZE		SEGSIZE+4
     61  1.1.1.1  martti 
     62  1.1.1.1  martti const char *opcode(int);
     63  1.1.1.1  martti const char *sock_ntop(struct sockaddr *);
     64  1.1.1.1  martti u_int16_t pick_proxy_port(void);
     65  1.1.1.1  martti static void usage(void);
     66  1.1.1.1  martti 
     67  1.1.1.1  martti extern	char *__progname;
     68  1.1.1.1  martti char	ntop_buf[NTOP_BUFS][INET6_ADDRSTRLEN];
     69  1.1.1.1  martti int	verbose = 0;
     70  1.1.1.1  martti 
     71  1.1.1.1  martti int
     72  1.1.1.1  martti main(int argc, char *argv[])
     73  1.1.1.1  martti {
     74  1.1.1.1  martti 	int c, fd = 0, on = 1, out_fd = 0, peer, reqsize = 0;
     75  1.1.1.1  martti 	int transwait = DEFTRANSWAIT;
     76  1.1.1.1  martti 	char *p;
     77  1.1.1.1  martti 	struct tftphdr *tp;
     78  1.1.1.1  martti 	struct passwd *pw;
     79  1.1.1.1  martti 
     80  1.1.1.1  martti 	char cbuf[CMSG_SPACE(sizeof(struct sockaddr_storage))];
     81  1.1.1.1  martti 	char req[PKTSIZE];
     82  1.1.1.1  martti 	struct cmsghdr *cmsg;
     83  1.1.1.1  martti 	struct msghdr msg;
     84  1.1.1.1  martti 	struct iovec iov;
     85  1.1.1.1  martti 
     86  1.1.1.1  martti 	struct sockaddr_storage from, proxy, server, proxy_to_server, s_in;
     87  1.1.1.1  martti 	struct sockaddr_in sock_out;
     88  1.1.1.1  martti 	socklen_t j;
     89  1.1.1.1  martti 	in_port_t bindport;
     90  1.1.1.1  martti 
     91  1.1.1.1  martti 	openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
     92  1.1.1.1  martti 
     93  1.1.1.1  martti 	while ((c = getopt(argc, argv, "vw:")) != -1)
     94  1.1.1.1  martti 		switch (c) {
     95  1.1.1.1  martti 		case 'v':
     96  1.1.1.1  martti 			verbose++;
     97  1.1.1.1  martti 			break;
     98  1.1.1.1  martti 		case 'w':
     99  1.1.1.1  martti 			transwait = strtoll(optarg, &p, 10);
    100  1.1.1.1  martti 			if (transwait < 1) {
    101  1.1.1.1  martti 				syslog(LOG_ERR, "invalid -w value");
    102  1.1.1.1  martti 				exit(1);
    103  1.1.1.1  martti 			}
    104  1.1.1.1  martti 			break;
    105  1.1.1.1  martti 		default:
    106  1.1.1.1  martti 			usage();
    107  1.1.1.1  martti 			break;
    108  1.1.1.1  martti 		}
    109  1.1.1.1  martti 
    110  1.1.1.1  martti 	/* open /dev/pf */
    111  1.1.1.1  martti 	init_filter(NULL, verbose);
    112  1.1.1.1  martti 
    113  1.1.1.1  martti 	tzset();
    114  1.1.1.1  martti 
    115  1.1.1.1  martti 	pw = getpwnam(NOPRIV_USER);
    116  1.1.1.1  martti 	if (!pw) {
    117  1.1.1.1  martti 		syslog(LOG_ERR, "no such user %s: %m", NOPRIV_USER);
    118  1.1.1.1  martti 		exit(1);
    119  1.1.1.1  martti 	}
    120  1.1.1.1  martti 	if (chroot(CHROOT_DIR) || chdir("/")) {
    121  1.1.1.1  martti 		syslog(LOG_ERR, "chroot %s: %m", CHROOT_DIR);
    122  1.1.1.1  martti 		exit(1);
    123  1.1.1.1  martti 	}
    124  1.1.1.1  martti 	if (setgroups(1, &pw->pw_gid) ||
    125  1.1.1.1  martti 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
    126  1.1.1.1  martti 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) {
    127  1.1.1.1  martti 		syslog(LOG_ERR, "can't revoke privs: %m");
    128  1.1.1.1  martti 		exit(1);
    129  1.1.1.1  martti 	}
    130  1.1.1.1  martti 
    131  1.1.1.1  martti 	/* non-blocking io */
    132  1.1.1.1  martti 	if (ioctl(fd, FIONBIO, &on) < 0) {
    133  1.1.1.1  martti 		syslog(LOG_ERR, "ioctl(FIONBIO): %m");
    134  1.1.1.1  martti 		exit(1);
    135  1.1.1.1  martti 	}
    136  1.1.1.1  martti 
    137  1.1.1.1  martti 	if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on)) == -1) {
    138  1.1.1.1  martti 		syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m");
    139  1.1.1.1  martti 		exit(1);
    140  1.1.1.1  martti 	}
    141  1.1.1.1  martti 
    142  1.1.1.1  martti 	j = sizeof(s_in);
    143  1.1.1.1  martti 	if (getsockname(fd, (struct sockaddr *)&s_in, &j) == -1) {
    144  1.1.1.1  martti 		syslog(LOG_ERR, "getsockname: %m");
    145  1.1.1.1  martti 		exit(1);
    146  1.1.1.1  martti 	}
    147  1.1.1.1  martti 
    148  1.1.1.1  martti 	bindport = ((struct sockaddr_in *)&s_in)->sin_port;
    149  1.1.1.1  martti 
    150  1.1.1.1  martti 	/* req will be pushed back out at the end, unchanged */
    151  1.1.1.1  martti 	j = sizeof(from);
    152  1.1.1.1  martti 	if ((reqsize = recvfrom(fd, req, sizeof(req), MSG_PEEK,
    153  1.1.1.1  martti 	    (struct sockaddr *)&from, &j)) < 0) {
    154  1.1.1.1  martti 		syslog(LOG_ERR, "recvfrom: %m");
    155  1.1.1.1  martti 		exit(1);
    156  1.1.1.1  martti 	}
    157  1.1.1.1  martti 
    158  1.1.1.1  martti 	bzero(&msg, sizeof(msg));
    159  1.1.1.1  martti 	iov.iov_base = req;
    160  1.1.1.1  martti 	iov.iov_len = sizeof(req);
    161  1.1.1.1  martti 	msg.msg_name = &from;
    162  1.1.1.1  martti 	msg.msg_namelen = sizeof(from);
    163  1.1.1.1  martti 	msg.msg_iov = &iov;
    164  1.1.1.1  martti 	msg.msg_iovlen = 1;
    165  1.1.1.1  martti 	msg.msg_control = cbuf;
    166  1.1.1.1  martti 	msg.msg_controllen = CMSG_LEN(sizeof(struct sockaddr_storage));
    167  1.1.1.1  martti 
    168  1.1.1.1  martti 	if (recvmsg(fd, &msg, 0) < 0) {
    169  1.1.1.1  martti 		syslog(LOG_ERR, "recvmsg: %m");
    170  1.1.1.1  martti 		exit(1);
    171  1.1.1.1  martti 	}
    172  1.1.1.1  martti 
    173  1.1.1.1  martti 	close(fd);
    174  1.1.1.1  martti 	close(1);
    175  1.1.1.1  martti 
    176  1.1.1.1  martti 	peer = socket(from.ss_family, SOCK_DGRAM, 0);
    177  1.1.1.1  martti 	if (peer < 0) {
    178  1.1.1.1  martti 		syslog(LOG_ERR, "socket: %m");
    179  1.1.1.1  martti 		exit(1);
    180  1.1.1.1  martti 	}
    181  1.1.1.1  martti 	memset(&s_in, 0, sizeof(s_in));
    182  1.1.1.1  martti 	s_in.ss_family = from.ss_family;
    183  1.1.1.1  martti 	s_in.ss_len = from.ss_len;
    184  1.1.1.1  martti 
    185  1.1.1.1  martti 	/* get local address if possible */
    186  1.1.1.1  martti 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
    187  1.1.1.1  martti 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    188  1.1.1.1  martti 		if (cmsg->cmsg_level == IPPROTO_IP &&
    189  1.1.1.1  martti 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
    190  1.1.1.1  martti 			memcpy(&((struct sockaddr_in *)&s_in)->sin_addr,
    191  1.1.1.1  martti 			    CMSG_DATA(cmsg), sizeof(struct in_addr));
    192  1.1.1.1  martti 			break;
    193  1.1.1.1  martti 		}
    194  1.1.1.1  martti 	}
    195  1.1.1.1  martti 
    196  1.1.1.1  martti 	if (bind(peer, (struct sockaddr *)&s_in, s_in.ss_len) < 0) {
    197  1.1.1.1  martti 		syslog(LOG_ERR, "bind: %m");
    198  1.1.1.1  martti 		exit(1);
    199  1.1.1.1  martti 	}
    200  1.1.1.1  martti 	if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
    201  1.1.1.1  martti 		syslog(LOG_ERR, "connect: %m");
    202  1.1.1.1  martti 		exit(1);
    203  1.1.1.1  martti 	}
    204  1.1.1.1  martti 
    205  1.1.1.1  martti 	tp = (struct tftphdr *)req;
    206  1.1.1.1  martti 	if (!(ntohs(tp->th_opcode) == RRQ || ntohs(tp->th_opcode) == WRQ)) {
    207  1.1.1.1  martti 		/* not a tftp request, bail */
    208  1.1.1.1  martti 		if (verbose) {
    209  1.1.1.1  martti 			syslog(LOG_WARNING, "not a valid tftp request");
    210  1.1.1.1  martti 			exit(1);
    211  1.1.1.1  martti 		} else
    212  1.1.1.1  martti 			/* exit 0 so inetd doesn't log anything */
    213  1.1.1.1  martti 			exit(0);
    214  1.1.1.1  martti 	}
    215  1.1.1.1  martti 
    216  1.1.1.1  martti 	j = sizeof(struct sockaddr_storage);
    217  1.1.1.1  martti 	if (getsockname(fd, (struct sockaddr *)&proxy, &j) == -1) {
    218  1.1.1.1  martti 		syslog(LOG_ERR, "getsockname: %m");
    219  1.1.1.1  martti 		exit(1);
    220  1.1.1.1  martti 	}
    221  1.1.1.1  martti 
    222  1.1.1.1  martti 	((struct sockaddr_in *)&proxy)->sin_port = bindport;
    223  1.1.1.1  martti 
    224  1.1.1.1  martti 	/* find the un-rdr'd server and port the client wanted */
    225  1.1.1.1  martti 	if (server_lookup((struct sockaddr *)&from,
    226  1.1.1.1  martti 	    (struct sockaddr *)&proxy, (struct sockaddr *)&server,
    227  1.1.1.1  martti 	    IPPROTO_UDP) != 0) {
    228  1.1.1.1  martti 		syslog(LOG_ERR, "pf connection lookup failed (no rdr?)");
    229  1.1.1.1  martti 		exit(1);
    230  1.1.1.1  martti 	}
    231  1.1.1.1  martti 
    232  1.1.1.1  martti 	/* establish a new outbound connection to the remote server */
    233  1.1.1.1  martti 	if ((out_fd = socket(((struct sockaddr *)&from)->sa_family,
    234  1.1.1.1  martti 	    SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    235  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't create new socket");
    236  1.1.1.1  martti 		exit(1);
    237  1.1.1.1  martti 	}
    238  1.1.1.1  martti 
    239  1.1.1.1  martti 	bzero((char *)&sock_out, sizeof(sock_out));
    240  1.1.1.1  martti 	sock_out.sin_family = from.ss_family;
    241  1.1.1.1  martti 	sock_out.sin_port = htons(pick_proxy_port());
    242  1.1.1.1  martti 	if (bind(out_fd, (struct sockaddr *)&sock_out, sizeof(sock_out)) < 0) {
    243  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't bind to new socket: %m");
    244  1.1.1.1  martti 		exit(1);
    245  1.1.1.1  martti 	}
    246  1.1.1.1  martti 
    247  1.1.1.1  martti 	if (connect(out_fd, (struct sockaddr *)&server,
    248  1.1.1.1  martti 	    ((struct sockaddr *)&server)->sa_len) < 0 && errno != EINPROGRESS) {
    249  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't connect to remote server: %m");
    250  1.1.1.1  martti 		exit(1);
    251  1.1.1.1  martti 	}
    252  1.1.1.1  martti 
    253  1.1.1.1  martti 	j = sizeof(struct sockaddr_storage);
    254  1.1.1.1  martti 	if ((getsockname(out_fd, (struct sockaddr *)&proxy_to_server,
    255  1.1.1.1  martti 	    &j)) < 0) {
    256  1.1.1.1  martti 		syslog(LOG_ERR, "getsockname: %m");
    257  1.1.1.1  martti 		exit(1);
    258  1.1.1.1  martti 	}
    259  1.1.1.1  martti 
    260  1.1.1.1  martti 	if (verbose)
    261  1.1.1.1  martti 		syslog(LOG_INFO, "%s:%d -> %s:%d/%s:%d -> %s:%d \"%s %s\"",
    262  1.1.1.1  martti 			sock_ntop((struct sockaddr *)&from),
    263  1.1.1.1  martti 			ntohs(((struct sockaddr_in *)&from)->sin_port),
    264  1.1.1.1  martti 			sock_ntop((struct sockaddr *)&proxy),
    265  1.1.1.1  martti 			ntohs(((struct sockaddr_in *)&proxy)->sin_port),
    266  1.1.1.1  martti 			sock_ntop((struct sockaddr *)&proxy_to_server),
    267  1.1.1.1  martti 			ntohs(((struct sockaddr_in *)&proxy_to_server)->sin_port),
    268  1.1.1.1  martti 			sock_ntop((struct sockaddr *)&server),
    269  1.1.1.1  martti 			ntohs(((struct sockaddr_in *)&server)->sin_port),
    270  1.1.1.1  martti 			opcode(ntohs(tp->th_opcode)),
    271  1.1.1.1  martti 			tp->th_stuff);
    272  1.1.1.1  martti 
    273  1.1.1.1  martti 	/* get ready to add rdr and pass rules */
    274  1.1.1.1  martti 	if (prepare_commit(1) == -1) {
    275  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't prepare pf commit");
    276  1.1.1.1  martti 		exit(1);
    277  1.1.1.1  martti 	}
    278  1.1.1.1  martti 
    279  1.1.1.1  martti 	/* rdr from server to us on our random port -> client on its port */
    280  1.1.1.1  martti 	if (add_rdr(1, (struct sockaddr *)&server,
    281  1.1.1.1  martti 	    (struct sockaddr *)&proxy_to_server, ntohs(sock_out.sin_port),
    282  1.1.1.1  martti 	    (struct sockaddr *)&from,
    283  1.1.1.1  martti 	    ntohs(((struct sockaddr_in *)&from)->sin_port),
    284  1.1.1.1  martti 	    IPPROTO_UDP) == -1) {
    285  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't add rdr");
    286  1.1.1.1  martti 		exit(1);
    287  1.1.1.1  martti 	}
    288  1.1.1.1  martti 
    289  1.1.1.1  martti 	/* explicitly allow the packets to return back to the client (which pf
    290  1.1.1.1  martti 	 * will see post-rdr) */
    291  1.1.1.1  martti 	if (add_filter(1, PF_IN, (struct sockaddr *)&server,
    292  1.1.1.1  martti 	    (struct sockaddr *)&from,
    293  1.1.1.1  martti 	    ntohs(((struct sockaddr_in *)&from)->sin_port),
    294  1.1.1.1  martti 	    IPPROTO_UDP) == -1) {
    295  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't add pass in");
    296  1.1.1.1  martti 		exit(1);
    297  1.1.1.1  martti 	}
    298  1.1.1.1  martti 	if (add_filter(1, PF_OUT, (struct sockaddr *)&server,
    299  1.1.1.1  martti 	    (struct sockaddr *)&from,
    300  1.1.1.1  martti 	    ntohs(((struct sockaddr_in *)&from)->sin_port),
    301  1.1.1.1  martti 	    IPPROTO_UDP) == -1) {
    302  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't add pass out");
    303  1.1.1.1  martti 		exit(1);
    304  1.1.1.1  martti 	}
    305  1.1.1.1  martti 
    306  1.1.1.1  martti 	/* and just in case, to pass out from us to the server */
    307  1.1.1.1  martti 	if (add_filter(1, PF_OUT, (struct sockaddr *)&proxy_to_server,
    308  1.1.1.1  martti 	    (struct sockaddr *)&server,
    309  1.1.1.1  martti 	    ntohs(((struct sockaddr_in *)&server)->sin_port),
    310  1.1.1.1  martti 	    IPPROTO_UDP) == -1) {
    311  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't add pass out");
    312  1.1.1.1  martti 		exit(1);
    313  1.1.1.1  martti 	}
    314  1.1.1.1  martti 
    315  1.1.1.1  martti 	if (do_commit() == -1) {
    316  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't commit pf rules");
    317  1.1.1.1  martti 		exit(1);
    318  1.1.1.1  martti 	}
    319  1.1.1.1  martti 
    320  1.1.1.1  martti 	/* forward the initial tftp request and start the insanity */
    321  1.1.1.1  martti 	if (send(out_fd, tp, reqsize, 0) < 0) {
    322  1.1.1.1  martti 		syslog(LOG_ERR, "couldn't forward tftp packet: %m");
    323  1.1.1.1  martti 		exit(1);
    324  1.1.1.1  martti 	}
    325  1.1.1.1  martti 
    326  1.1.1.1  martti 	/* allow the transfer to start to establish a state */
    327  1.1.1.1  martti 	sleep(transwait);
    328  1.1.1.1  martti 
    329  1.1.1.1  martti 	/* delete our rdr rule and clean up */
    330  1.1.1.1  martti 	prepare_commit(1);
    331  1.1.1.1  martti 	do_commit();
    332  1.1.1.1  martti 
    333  1.1.1.1  martti 	return(0);
    334  1.1.1.1  martti }
    335  1.1.1.1  martti 
    336  1.1.1.1  martti const char *
    337  1.1.1.1  martti opcode(int code)
    338  1.1.1.1  martti {
    339  1.1.1.1  martti 	static char str[6];
    340  1.1.1.1  martti 
    341  1.1.1.1  martti 	switch (code) {
    342  1.1.1.1  martti 	case 1:
    343  1.1.1.1  martti 		(void)snprintf(str, sizeof(str), "RRQ");
    344  1.1.1.1  martti 		break;
    345  1.1.1.1  martti 	case 2:
    346  1.1.1.1  martti 		(void)snprintf(str, sizeof(str), "WRQ");
    347  1.1.1.1  martti 		break;
    348  1.1.1.1  martti 	default:
    349  1.1.1.1  martti 		(void)snprintf(str, sizeof(str), "(%d)", code);
    350  1.1.1.1  martti 		break;
    351  1.1.1.1  martti 	}
    352  1.1.1.1  martti 
    353  1.1.1.1  martti 	return (str);
    354  1.1.1.1  martti }
    355  1.1.1.1  martti 
    356  1.1.1.1  martti const char *
    357  1.1.1.1  martti sock_ntop(struct sockaddr *sa)
    358  1.1.1.1  martti {
    359  1.1.1.1  martti 	static int n = 0;
    360  1.1.1.1  martti 
    361  1.1.1.1  martti 	/* Cycle to next buffer. */
    362  1.1.1.1  martti 	n = (n + 1) % NTOP_BUFS;
    363  1.1.1.1  martti 	ntop_buf[n][0] = '\0';
    364  1.1.1.1  martti 
    365  1.1.1.1  martti 	if (sa->sa_family == AF_INET) {
    366  1.1.1.1  martti 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    367  1.1.1.1  martti 
    368  1.1.1.1  martti 		return (inet_ntop(AF_INET, &sin->sin_addr, ntop_buf[n],
    369  1.1.1.1  martti 		    sizeof ntop_buf[0]));
    370  1.1.1.1  martti 	}
    371  1.1.1.1  martti 
    372  1.1.1.1  martti 	if (sa->sa_family == AF_INET6) {
    373  1.1.1.1  martti 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    374  1.1.1.1  martti 
    375  1.1.1.1  martti 		return (inet_ntop(AF_INET6, &sin6->sin6_addr, ntop_buf[n],
    376  1.1.1.1  martti 		    sizeof ntop_buf[0]));
    377  1.1.1.1  martti 	}
    378  1.1.1.1  martti 
    379  1.1.1.1  martti 	return (NULL);
    380  1.1.1.1  martti }
    381  1.1.1.1  martti 
    382  1.1.1.1  martti u_int16_t
    383  1.1.1.1  martti pick_proxy_port(void)
    384  1.1.1.1  martti {
    385  1.1.1.1  martti 	return (IPPORT_HIFIRSTAUTO + (arc4random() %
    386  1.1.1.1  martti 	    (IPPORT_HILASTAUTO - IPPORT_HIFIRSTAUTO)));
    387  1.1.1.1  martti }
    388  1.1.1.1  martti 
    389  1.1.1.1  martti static void
    390  1.1.1.1  martti usage(void)
    391  1.1.1.1  martti {
    392  1.1.1.1  martti 	syslog(LOG_ERR, "usage: %s [-v] [-w transwait]", __progname);
    393  1.1.1.1  martti 	exit(1);
    394  1.1.1.1  martti }
    395