Home | History | Annotate | Line # | Download | only in librumpuser
sp_common.c revision 1.7
      1  1.7  pooka /*      $NetBSD: sp_common.c,v 1.7 2010/11/24 14:32:42 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * Common client/server sysproxy routines.  #included.
     30  1.1  pooka  */
     31  1.1  pooka 
     32  1.1  pooka #include <sys/cdefs.h>
     33  1.1  pooka 
     34  1.1  pooka #include <sys/types.h>
     35  1.1  pooka #include <sys/mman.h>
     36  1.4  pooka #include <sys/queue.h>
     37  1.1  pooka #include <sys/socket.h>
     38  1.2  pooka #include <sys/un.h>
     39  1.1  pooka 
     40  1.1  pooka #include <arpa/inet.h>
     41  1.1  pooka #include <netinet/in.h>
     42  1.1  pooka #include <netinet/tcp.h>
     43  1.1  pooka 
     44  1.1  pooka #include <assert.h>
     45  1.1  pooka #include <errno.h>
     46  1.1  pooka #include <fcntl.h>
     47  1.1  pooka #include <poll.h>
     48  1.4  pooka #include <pthread.h>
     49  1.1  pooka #include <stdarg.h>
     50  1.1  pooka #include <stdio.h>
     51  1.1  pooka #include <stdlib.h>
     52  1.1  pooka #include <string.h>
     53  1.1  pooka #include <unistd.h>
     54  1.1  pooka 
     55  1.1  pooka //#define DEBUG
     56  1.1  pooka #ifdef DEBUG
     57  1.1  pooka #define DPRINTF(x) mydprintf x
     58  1.1  pooka static void
     59  1.1  pooka mydprintf(const char *fmt, ...)
     60  1.1  pooka {
     61  1.1  pooka 	va_list ap;
     62  1.1  pooka 
     63  1.1  pooka 	va_start(ap, fmt);
     64  1.1  pooka 	vfprintf(stderr, fmt, ap);
     65  1.1  pooka 	va_end(ap);
     66  1.1  pooka }
     67  1.1  pooka #else
     68  1.1  pooka #define DPRINTF(x)
     69  1.1  pooka #endif
     70  1.1  pooka 
     71  1.1  pooka /*
     72  1.1  pooka  * Bah, I hate writing on-off-wire conversions in C
     73  1.1  pooka  */
     74  1.1  pooka 
     75  1.4  pooka enum { RUMPSP_REQ, RUMPSP_RESP };
     76  1.4  pooka enum { RUMPSP_SYSCALL, RUMPSP_COPYIN, RUMPSP_COPYOUT, RUMPSP_ANONMMAP };
     77  1.1  pooka 
     78  1.1  pooka struct rsp_hdr {
     79  1.1  pooka 	uint64_t rsp_len;
     80  1.1  pooka 	uint64_t rsp_reqno;
     81  1.4  pooka 	uint16_t rsp_class;
     82  1.4  pooka 	uint16_t rsp_type;
     83  1.1  pooka 	/*
     84  1.1  pooka 	 * We want this structure 64bit-aligned for typecast fun,
     85  1.1  pooka 	 * so might as well use the following for something.
     86  1.1  pooka 	 */
     87  1.1  pooka 	uint32_t rsp_sysnum;
     88  1.1  pooka };
     89  1.1  pooka #define HDRSZ sizeof(struct rsp_hdr)
     90  1.1  pooka 
     91  1.1  pooka /*
     92  1.1  pooka  * Data follows the header.  We have two types of structured data.
     93  1.1  pooka  */
     94  1.1  pooka 
     95  1.1  pooka /* copyin/copyout */
     96  1.1  pooka struct rsp_copydata {
     97  1.1  pooka 	size_t rcp_len;
     98  1.1  pooka 	void *rcp_addr;
     99  1.1  pooka 	uint8_t rcp_data[0];
    100  1.1  pooka };
    101  1.1  pooka 
    102  1.1  pooka /* syscall response */
    103  1.1  pooka struct rsp_sysresp {
    104  1.1  pooka 	int rsys_error;
    105  1.1  pooka 	register_t rsys_retval[2];
    106  1.1  pooka };
    107  1.1  pooka 
    108  1.4  pooka struct respwait {
    109  1.4  pooka 	uint64_t rw_reqno;
    110  1.4  pooka 	void *rw_data;
    111  1.4  pooka 	size_t rw_dlen;
    112  1.4  pooka 
    113  1.4  pooka 	pthread_cond_t rw_cv;
    114  1.4  pooka 
    115  1.4  pooka 	TAILQ_ENTRY(respwait) rw_entries;
    116  1.4  pooka };
    117  1.1  pooka 
    118  1.1  pooka struct spclient {
    119  1.1  pooka 	int spc_fd;
    120  1.7  pooka 	int spc_refcnt;
    121  1.7  pooka 	int spc_dying;
    122  1.6  pooka 
    123  1.6  pooka 	struct lwp *spc_mainlwp;
    124  1.6  pooka 	pid_t spc_pid;
    125  1.1  pooka 
    126  1.7  pooka 	struct pollfd *spc_pfd;
    127  1.7  pooka 
    128  1.1  pooka 	struct rsp_hdr spc_hdr;
    129  1.1  pooka 	uint8_t *spc_buf;
    130  1.1  pooka 	size_t spc_off;
    131  1.1  pooka 
    132  1.4  pooka 	pthread_mutex_t spc_mtx;
    133  1.1  pooka 	pthread_cond_t spc_cv;
    134  1.4  pooka 
    135  1.4  pooka 	uint64_t spc_nextreq;
    136  1.4  pooka 	int spc_ostatus, spc_istatus;
    137  1.4  pooka 
    138  1.4  pooka 	TAILQ_HEAD(, respwait) spc_respwait;
    139  1.1  pooka };
    140  1.4  pooka #define SPCSTATUS_FREE 0
    141  1.4  pooka #define SPCSTATUS_BUSY 1
    142  1.4  pooka #define SPCSTATUS_WANTED 2
    143  1.1  pooka 
    144  1.1  pooka typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
    145  1.1  pooka typedef int (*connecthook_fn)(int);
    146  1.1  pooka 
    147  1.4  pooka static int readframe(struct spclient *);
    148  1.4  pooka static void handlereq(struct spclient *);
    149  1.4  pooka 
    150  1.4  pooka static void
    151  1.4  pooka sendlock(struct spclient *spc)
    152  1.4  pooka {
    153  1.4  pooka 
    154  1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    155  1.4  pooka 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    156  1.4  pooka 		spc->spc_ostatus = SPCSTATUS_WANTED;
    157  1.4  pooka 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    158  1.4  pooka 	}
    159  1.4  pooka 	spc->spc_ostatus = SPCSTATUS_BUSY;
    160  1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    161  1.4  pooka }
    162  1.4  pooka 
    163  1.4  pooka static void
    164  1.4  pooka sendunlock(struct spclient *spc)
    165  1.4  pooka {
    166  1.4  pooka 
    167  1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    168  1.4  pooka 	if (spc->spc_ostatus == SPCSTATUS_WANTED)
    169  1.4  pooka 		pthread_cond_broadcast(&spc->spc_cv);
    170  1.4  pooka 	spc->spc_ostatus = SPCSTATUS_FREE;
    171  1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    172  1.4  pooka }
    173  1.1  pooka 
    174  1.1  pooka static int
    175  1.1  pooka dosend(struct spclient *spc, const void *data, size_t dlen)
    176  1.1  pooka {
    177  1.1  pooka 	struct pollfd pfd;
    178  1.1  pooka 	const uint8_t *sdata = data;
    179  1.1  pooka 	ssize_t n;
    180  1.1  pooka 	size_t sent;
    181  1.1  pooka 	int fd = spc->spc_fd;
    182  1.1  pooka 
    183  1.1  pooka 	pfd.fd = fd;
    184  1.1  pooka 	pfd.events = POLLOUT;
    185  1.1  pooka 
    186  1.1  pooka 	for (sent = 0, n = 0; sent < dlen; ) {
    187  1.1  pooka 		if (n) {
    188  1.1  pooka 			if (poll(&pfd, 1, INFTIM) == -1) {
    189  1.1  pooka 				if (errno == EINTR)
    190  1.1  pooka 					continue;
    191  1.1  pooka 				return errno;
    192  1.1  pooka 			}
    193  1.1  pooka 		}
    194  1.1  pooka 
    195  1.3  pooka 		n = send(fd, sdata + sent, dlen - sent, MSG_NOSIGNAL);
    196  1.1  pooka 		if (n == 0) {
    197  1.1  pooka 			return EFAULT;
    198  1.1  pooka 		}
    199  1.1  pooka 		if (n == -1 && errno != EAGAIN) {
    200  1.1  pooka 			return EFAULT;
    201  1.1  pooka 		}
    202  1.1  pooka 		sent += n;
    203  1.1  pooka 	}
    204  1.1  pooka 
    205  1.1  pooka 	return 0;
    206  1.1  pooka }
    207  1.1  pooka 
    208  1.4  pooka static void
    209  1.4  pooka putwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    210  1.4  pooka {
    211  1.4  pooka 
    212  1.4  pooka 	rw->rw_data = NULL;
    213  1.4  pooka 	rw->rw_dlen = 0;
    214  1.4  pooka 	pthread_cond_init(&rw->rw_cv, NULL);
    215  1.4  pooka 
    216  1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    217  1.4  pooka 	rw->rw_reqno = rhdr->rsp_reqno = spc->spc_nextreq++;
    218  1.4  pooka 	TAILQ_INSERT_TAIL(&spc->spc_respwait, rw, rw_entries);
    219  1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    220  1.4  pooka }
    221  1.4  pooka 
    222  1.4  pooka static void
    223  1.4  pooka kickwaiter(struct spclient *spc)
    224  1.4  pooka {
    225  1.4  pooka 	struct respwait *rw;
    226  1.4  pooka 
    227  1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    228  1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries) {
    229  1.4  pooka 		if (rw->rw_reqno == spc->spc_hdr.rsp_reqno)
    230  1.4  pooka 			break;
    231  1.4  pooka 	}
    232  1.4  pooka 	if (rw == NULL) {
    233  1.4  pooka 		printf("PANIC: no waiter\n");
    234  1.6  pooka 		abort();
    235  1.4  pooka 		return;
    236  1.4  pooka 	}
    237  1.4  pooka 	rw->rw_data = spc->spc_buf;
    238  1.4  pooka 	pthread_cond_signal(&rw->rw_cv);
    239  1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    240  1.4  pooka 
    241  1.4  pooka 	spc->spc_buf = NULL;
    242  1.4  pooka 	spc->spc_off = 0;
    243  1.4  pooka }
    244  1.4  pooka 
    245  1.4  pooka static void
    246  1.4  pooka kickall(struct spclient *spc)
    247  1.4  pooka {
    248  1.4  pooka 	struct respwait *rw;
    249  1.4  pooka 
    250  1.4  pooka 	/* DIAGASSERT(mutex_owned(spc_lock)) */
    251  1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries)
    252  1.4  pooka 		pthread_cond_signal(&rw->rw_cv);
    253  1.4  pooka }
    254  1.4  pooka 
    255  1.4  pooka static int
    256  1.4  pooka waitresp(struct spclient *spc, struct respwait *rw)
    257  1.4  pooka {
    258  1.4  pooka 	struct pollfd pfd;
    259  1.4  pooka 	int rv = 0;
    260  1.4  pooka 
    261  1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    262  1.7  pooka 	while (rw->rw_data == NULL && spc->spc_dying == 0) {
    263  1.4  pooka 		/* are we free to receive? */
    264  1.4  pooka 		if (spc->spc_istatus == SPCSTATUS_FREE) {
    265  1.4  pooka 			int gotresp;
    266  1.4  pooka 
    267  1.4  pooka 			spc->spc_istatus = SPCSTATUS_BUSY;
    268  1.4  pooka 			pthread_mutex_unlock(&spc->spc_mtx);
    269  1.4  pooka 
    270  1.4  pooka 			pfd.fd = spc->spc_fd;
    271  1.4  pooka 			pfd.events = POLLIN;
    272  1.4  pooka 
    273  1.4  pooka 			for (gotresp = 0; !gotresp; ) {
    274  1.4  pooka 				while (readframe(spc) < 1)
    275  1.4  pooka 					poll(&pfd, 1, INFTIM);
    276  1.4  pooka 
    277  1.4  pooka 				switch (spc->spc_hdr.rsp_class) {
    278  1.4  pooka 				case RUMPSP_RESP:
    279  1.4  pooka 					kickwaiter(spc);
    280  1.4  pooka 					gotresp = spc->spc_hdr.rsp_reqno ==
    281  1.4  pooka 					    rw->rw_reqno;
    282  1.4  pooka 					break;
    283  1.4  pooka 				case RUMPSP_REQ:
    284  1.4  pooka 					handlereq(spc);
    285  1.4  pooka 					break;
    286  1.4  pooka 				default:
    287  1.4  pooka 					/* panic */
    288  1.4  pooka 					break;
    289  1.4  pooka 				}
    290  1.4  pooka 			}
    291  1.4  pooka 			pthread_mutex_lock(&spc->spc_mtx);
    292  1.4  pooka 			if (spc->spc_istatus == SPCSTATUS_WANTED)
    293  1.4  pooka 				kickall(spc);
    294  1.4  pooka 			spc->spc_istatus = SPCSTATUS_FREE;
    295  1.4  pooka 		} else {
    296  1.4  pooka 			spc->spc_istatus = SPCSTATUS_WANTED;
    297  1.4  pooka 			pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
    298  1.4  pooka 		}
    299  1.4  pooka 	}
    300  1.4  pooka 
    301  1.4  pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    302  1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    303  1.4  pooka 
    304  1.4  pooka 	pthread_cond_destroy(&rw->rw_cv);
    305  1.4  pooka 	return rv;
    306  1.4  pooka }
    307  1.4  pooka 
    308  1.1  pooka static int
    309  1.1  pooka readframe(struct spclient *spc)
    310  1.1  pooka {
    311  1.1  pooka 	int fd = spc->spc_fd;
    312  1.1  pooka 	size_t left;
    313  1.1  pooka 	size_t framelen;
    314  1.1  pooka 	ssize_t n;
    315  1.1  pooka 
    316  1.1  pooka 	/* still reading header? */
    317  1.1  pooka 	if (spc->spc_off < HDRSZ) {
    318  1.1  pooka 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    319  1.1  pooka 		    spc->spc_off));
    320  1.1  pooka 
    321  1.1  pooka 		left = HDRSZ - spc->spc_off;
    322  1.1  pooka 		/*LINTED: cast ok */
    323  1.1  pooka 		n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
    324  1.1  pooka 		if (n == 0) {
    325  1.1  pooka 			return -1;
    326  1.1  pooka 		}
    327  1.1  pooka 		if (n == -1) {
    328  1.1  pooka 			if (errno == EAGAIN)
    329  1.1  pooka 				return 0;
    330  1.1  pooka 			return -1;
    331  1.1  pooka 		}
    332  1.1  pooka 
    333  1.1  pooka 		spc->spc_off += n;
    334  1.1  pooka 		if (spc->spc_off < HDRSZ)
    335  1.1  pooka 			return -1;
    336  1.1  pooka 
    337  1.1  pooka 		/*LINTED*/
    338  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    339  1.1  pooka 
    340  1.1  pooka 		if (framelen < HDRSZ) {
    341  1.1  pooka 			return -1;
    342  1.1  pooka 		} else if (framelen == HDRSZ) {
    343  1.1  pooka 			return 1;
    344  1.1  pooka 		}
    345  1.1  pooka 
    346  1.1  pooka 		spc->spc_buf = malloc(framelen - HDRSZ);
    347  1.1  pooka 		if (spc->spc_buf == NULL) {
    348  1.1  pooka 			return -1;
    349  1.1  pooka 		}
    350  1.1  pooka 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    351  1.1  pooka 
    352  1.1  pooka 		/* "fallthrough" */
    353  1.1  pooka 	} else {
    354  1.1  pooka 		/*LINTED*/
    355  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    356  1.1  pooka 	}
    357  1.1  pooka 
    358  1.1  pooka 	left = framelen - spc->spc_off;
    359  1.1  pooka 
    360  1.1  pooka 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    361  1.1  pooka 	    spc->spc_off, left));
    362  1.1  pooka 
    363  1.1  pooka 	if (left == 0)
    364  1.1  pooka 		return 1;
    365  1.1  pooka 	n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    366  1.1  pooka 	if (n == 0) {
    367  1.1  pooka 		return -1;
    368  1.1  pooka 	}
    369  1.1  pooka 	if (n == -1) {
    370  1.1  pooka 		if (errno == EAGAIN)
    371  1.1  pooka 			return 0;
    372  1.1  pooka 		return -1;
    373  1.1  pooka 	}
    374  1.1  pooka 	spc->spc_off += n;
    375  1.1  pooka 	left -= n;
    376  1.1  pooka 
    377  1.1  pooka 	/* got everything? */
    378  1.1  pooka 	if (left == 0)
    379  1.1  pooka 		return 1;
    380  1.1  pooka 	else
    381  1.1  pooka 		return 0;
    382  1.1  pooka }
    383  1.1  pooka 
    384  1.1  pooka static int
    385  1.1  pooka tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    386  1.1  pooka {
    387  1.1  pooka 	struct sockaddr_in sin;
    388  1.1  pooka 	char buf[64];
    389  1.1  pooka 	const char *p;
    390  1.1  pooka 	size_t l;
    391  1.1  pooka 	int port;
    392  1.1  pooka 
    393  1.1  pooka 	memset(&sin, 0, sizeof(sin));
    394  1.1  pooka 	sin.sin_len = sizeof(sin);
    395  1.1  pooka 	sin.sin_family = AF_INET;
    396  1.1  pooka 
    397  1.1  pooka 	p = strchr(addr, ':');
    398  1.1  pooka 	if (!p) {
    399  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    400  1.1  pooka 		return EINVAL;
    401  1.1  pooka 	}
    402  1.1  pooka 
    403  1.1  pooka 	l = p - addr;
    404  1.1  pooka 	if (l > sizeof(buf)-1) {
    405  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    406  1.1  pooka 		return EINVAL;
    407  1.1  pooka 	}
    408  1.1  pooka 	strncpy(buf, addr, l);
    409  1.1  pooka 	buf[l] = '\0';
    410  1.1  pooka 
    411  1.1  pooka 	/* special INADDR_ANY treatment */
    412  1.1  pooka 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    413  1.1  pooka 		sin.sin_addr.s_addr = INADDR_ANY;
    414  1.1  pooka 	} else {
    415  1.1  pooka 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    416  1.1  pooka 		case 1:
    417  1.1  pooka 			break;
    418  1.1  pooka 		case 0:
    419  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    420  1.1  pooka 			return EINVAL;
    421  1.1  pooka 		case -1:
    422  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    423  1.1  pooka 			return errno;
    424  1.1  pooka 		default:
    425  1.1  pooka 			assert(/*CONSTCOND*/0);
    426  1.1  pooka 			return EINVAL;
    427  1.1  pooka 		}
    428  1.1  pooka 	}
    429  1.1  pooka 
    430  1.1  pooka 	if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
    431  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    432  1.1  pooka 		return EINVAL;
    433  1.1  pooka 	}
    434  1.1  pooka 
    435  1.1  pooka 	/* advance to port number & parse */
    436  1.1  pooka 	p++;
    437  1.1  pooka 	l = strspn(p, "0123456789");
    438  1.1  pooka 	if (l == 0) {
    439  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    440  1.1  pooka 		return EINVAL;
    441  1.1  pooka 	}
    442  1.1  pooka 	strncpy(buf, p, l);
    443  1.1  pooka 	buf[l] = '\0';
    444  1.1  pooka 
    445  1.1  pooka 	if (*(p+l) != '/' && *(p+l) != '\0') {
    446  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    447  1.1  pooka 		return EINVAL;
    448  1.1  pooka 	}
    449  1.1  pooka 
    450  1.1  pooka 	port = atoi(buf);
    451  1.1  pooka 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    452  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    453  1.1  pooka 		return ERANGE;
    454  1.1  pooka 	}
    455  1.1  pooka 	sin.sin_port = htons(port);
    456  1.1  pooka 
    457  1.1  pooka 	*sa = malloc(sizeof(sin));
    458  1.1  pooka 	if (*sa == NULL)
    459  1.1  pooka 		return errno;
    460  1.1  pooka 	memcpy(*sa, &sin, sizeof(sin));
    461  1.1  pooka 	return 0;
    462  1.1  pooka }
    463  1.1  pooka 
    464  1.1  pooka static int
    465  1.1  pooka tcp_connecthook(int s)
    466  1.1  pooka {
    467  1.1  pooka 	int x;
    468  1.1  pooka 
    469  1.1  pooka 	x = 1;
    470  1.1  pooka 	setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    471  1.1  pooka 
    472  1.1  pooka 	return 0;
    473  1.1  pooka }
    474  1.1  pooka 
    475  1.5  pooka /*ARGSUSED*/
    476  1.2  pooka static int
    477  1.2  pooka unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    478  1.2  pooka {
    479  1.2  pooka 	struct sockaddr_un sun;
    480  1.5  pooka 	size_t slen;
    481  1.2  pooka 
    482  1.2  pooka 	if (strlen(addr) > sizeof(sun.sun_path))
    483  1.2  pooka 		return ENAMETOOLONG;
    484  1.2  pooka 
    485  1.2  pooka 	/*
    486  1.2  pooka 	 * The pathname can be all kinds of spaghetti elementals,
    487  1.2  pooka 	 * so meek and obidient we accept everything.
    488  1.2  pooka 	 */
    489  1.2  pooka 	memset(&sun, 0, sizeof(sun));
    490  1.2  pooka 	sun.sun_family = AF_LOCAL;
    491  1.2  pooka 	strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
    492  1.5  pooka 	sun.sun_len = slen = SUN_LEN(&sun);
    493  1.2  pooka 
    494  1.5  pooka 	*sa = malloc(slen);
    495  1.2  pooka 	if (*sa == NULL)
    496  1.2  pooka 		return errno;
    497  1.5  pooka 	memcpy(*sa, &sun, slen);
    498  1.2  pooka 
    499  1.2  pooka 	return 0;
    500  1.2  pooka }
    501  1.2  pooka 
    502  1.1  pooka /*ARGSUSED*/
    503  1.1  pooka static int
    504  1.1  pooka notsupp(void)
    505  1.1  pooka {
    506  1.1  pooka 
    507  1.1  pooka 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    508  1.1  pooka 	return EOPNOTSUPP;
    509  1.1  pooka }
    510  1.1  pooka 
    511  1.1  pooka static int
    512  1.1  pooka success(void)
    513  1.1  pooka {
    514  1.1  pooka 
    515  1.1  pooka 	return 0;
    516  1.1  pooka }
    517  1.1  pooka 
    518  1.1  pooka struct {
    519  1.1  pooka 	const char *id;
    520  1.1  pooka 	int domain;
    521  1.1  pooka 	addrparse_fn ap;
    522  1.1  pooka 	connecthook_fn connhook;
    523  1.1  pooka } parsetab[] = {
    524  1.1  pooka 	{ "tcp", PF_INET, tcp_parse, tcp_connecthook },
    525  1.2  pooka 	{ "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
    526  1.1  pooka 	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
    527  1.1  pooka };
    528  1.1  pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    529  1.1  pooka 
    530  1.1  pooka static int
    531  1.1  pooka parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
    532  1.1  pooka 	int allow_wildcard)
    533  1.1  pooka {
    534  1.1  pooka 	char id[16];
    535  1.1  pooka 	const char *p, *p2;
    536  1.1  pooka 	size_t l;
    537  1.1  pooka 	unsigned i;
    538  1.1  pooka 	int error;
    539  1.1  pooka 
    540  1.1  pooka 	/*
    541  1.1  pooka 	 * Parse the url
    542  1.1  pooka 	 */
    543  1.1  pooka 
    544  1.1  pooka 	p = url;
    545  1.1  pooka 	p2 = strstr(p, "://");
    546  1.1  pooka 	if (!p2) {
    547  1.1  pooka 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    548  1.1  pooka 		return EINVAL;
    549  1.1  pooka 	}
    550  1.1  pooka 	l = p2-p;
    551  1.1  pooka 	if (l > sizeof(id)-1) {
    552  1.1  pooka 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    553  1.1  pooka 		return EINVAL;
    554  1.1  pooka 	}
    555  1.1  pooka 
    556  1.1  pooka 	strncpy(id, p, l);
    557  1.1  pooka 	id[l] = '\0';
    558  1.1  pooka 	p2 += 3; /* beginning of address */
    559  1.1  pooka 
    560  1.1  pooka 	for (i = 0; i < NPARSE; i++) {
    561  1.1  pooka 		if (strcmp(id, parsetab[i].id) == 0) {
    562  1.1  pooka 			error = parsetab[i].ap(p2, sap, allow_wildcard);
    563  1.1  pooka 			if (error)
    564  1.1  pooka 				return error;
    565  1.1  pooka 			break;
    566  1.1  pooka 		}
    567  1.1  pooka 	}
    568  1.1  pooka 	if (i == NPARSE) {
    569  1.1  pooka 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    570  1.1  pooka 		return EINVAL;
    571  1.1  pooka 	}
    572  1.1  pooka 
    573  1.1  pooka 	*idxp = i;
    574  1.1  pooka 	return 0;
    575  1.1  pooka }
    576