Home | History | Annotate | Line # | Download | only in librumpuser
sp_common.c revision 1.31
      1  1.31  pooka /*      $NetBSD: sp_common.c,v 1.31 2011/03/08 15:34:37 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4  1.18  pooka  * Copyright (c) 2010, 2011 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.15  pooka #include <sys/syslimits.h>
     40   1.1  pooka 
     41   1.1  pooka #include <arpa/inet.h>
     42   1.1  pooka #include <netinet/in.h>
     43   1.1  pooka #include <netinet/tcp.h>
     44   1.1  pooka 
     45   1.1  pooka #include <assert.h>
     46   1.1  pooka #include <errno.h>
     47   1.1  pooka #include <fcntl.h>
     48  1.13  pooka #include <inttypes.h>
     49   1.1  pooka #include <poll.h>
     50   1.4  pooka #include <pthread.h>
     51   1.1  pooka #include <stdarg.h>
     52  1.11  pooka #include <stddef.h>
     53   1.1  pooka #include <stdio.h>
     54   1.1  pooka #include <stdlib.h>
     55   1.1  pooka #include <string.h>
     56   1.1  pooka #include <unistd.h>
     57   1.1  pooka 
     58   1.1  pooka //#define DEBUG
     59   1.1  pooka #ifdef DEBUG
     60   1.1  pooka #define DPRINTF(x) mydprintf x
     61   1.1  pooka static void
     62   1.1  pooka mydprintf(const char *fmt, ...)
     63   1.1  pooka {
     64   1.1  pooka 	va_list ap;
     65   1.1  pooka 
     66   1.1  pooka 	va_start(ap, fmt);
     67   1.1  pooka 	vfprintf(stderr, fmt, ap);
     68   1.1  pooka 	va_end(ap);
     69   1.1  pooka }
     70   1.1  pooka #else
     71   1.1  pooka #define DPRINTF(x)
     72   1.1  pooka #endif
     73   1.1  pooka 
     74  1.20  pooka #ifndef HOSTOPS
     75  1.20  pooka #define host_poll poll
     76  1.20  pooka #define host_read read
     77  1.31  pooka #define host_sendmsg sendmsg
     78  1.20  pooka #define host_setsockopt setsockopt
     79  1.20  pooka #endif
     80  1.20  pooka 
     81  1.31  pooka #define IOVPUT(_io_, _b_) _io_.iov_base = &_b_; _io_.iov_len = sizeof(_b_);
     82  1.31  pooka #define IOVPUT_WITHSIZE(_io_, _b_, _l_) _io_.iov_base = _b_; _io_.iov_len = _l_;
     83  1.31  pooka #define SENDIOV(_spc_, _iov_) dosend(_spc_, _iov_, __arraycount(_iov_))
     84  1.31  pooka 
     85   1.1  pooka /*
     86   1.1  pooka  * Bah, I hate writing on-off-wire conversions in C
     87   1.1  pooka  */
     88   1.1  pooka 
     89  1.13  pooka enum { RUMPSP_REQ, RUMPSP_RESP, RUMPSP_ERROR };
     90  1.17  pooka enum {	RUMPSP_HANDSHAKE,
     91  1.17  pooka 	RUMPSP_SYSCALL,
     92  1.10  pooka 	RUMPSP_COPYIN, RUMPSP_COPYINSTR,
     93  1.10  pooka 	RUMPSP_COPYOUT, RUMPSP_COPYOUTSTR,
     94  1.18  pooka 	RUMPSP_ANONMMAP,
     95  1.24  pooka 	RUMPSP_PREFORK,
     96  1.24  pooka 	RUMPSP_RAISE };
     97   1.1  pooka 
     98  1.28  pooka enum { HANDSHAKE_GUEST, HANDSHAKE_AUTH, HANDSHAKE_FORK, HANDSHAKE_EXEC };
     99  1.18  pooka 
    100  1.18  pooka #define AUTHLEN 4 /* 128bit fork auth */
    101  1.17  pooka 
    102   1.1  pooka struct rsp_hdr {
    103   1.1  pooka 	uint64_t rsp_len;
    104   1.1  pooka 	uint64_t rsp_reqno;
    105   1.4  pooka 	uint16_t rsp_class;
    106   1.4  pooka 	uint16_t rsp_type;
    107   1.1  pooka 	/*
    108   1.1  pooka 	 * We want this structure 64bit-aligned for typecast fun,
    109   1.1  pooka 	 * so might as well use the following for something.
    110   1.1  pooka 	 */
    111  1.13  pooka 	union {
    112  1.13  pooka 		uint32_t sysnum;
    113  1.13  pooka 		uint32_t error;
    114  1.17  pooka 		uint32_t handshake;
    115  1.24  pooka 		uint32_t signo;
    116  1.13  pooka 	} u;
    117   1.1  pooka };
    118   1.1  pooka #define HDRSZ sizeof(struct rsp_hdr)
    119  1.13  pooka #define rsp_sysnum u.sysnum
    120  1.13  pooka #define rsp_error u.error
    121  1.17  pooka #define rsp_handshake u.handshake
    122  1.24  pooka #define rsp_signo u.signo
    123   1.1  pooka 
    124  1.16  pooka #define MAXBANNER 96
    125  1.16  pooka 
    126   1.1  pooka /*
    127   1.1  pooka  * Data follows the header.  We have two types of structured data.
    128   1.1  pooka  */
    129   1.1  pooka 
    130   1.1  pooka /* copyin/copyout */
    131   1.1  pooka struct rsp_copydata {
    132   1.1  pooka 	size_t rcp_len;
    133   1.1  pooka 	void *rcp_addr;
    134   1.1  pooka 	uint8_t rcp_data[0];
    135   1.1  pooka };
    136   1.1  pooka 
    137   1.1  pooka /* syscall response */
    138   1.1  pooka struct rsp_sysresp {
    139   1.1  pooka 	int rsys_error;
    140   1.1  pooka 	register_t rsys_retval[2];
    141   1.1  pooka };
    142   1.1  pooka 
    143  1.18  pooka struct handshake_fork {
    144  1.18  pooka 	uint32_t rf_auth[4];
    145  1.18  pooka 	int rf_cancel;
    146  1.18  pooka };
    147  1.18  pooka 
    148   1.4  pooka struct respwait {
    149   1.4  pooka 	uint64_t rw_reqno;
    150   1.4  pooka 	void *rw_data;
    151   1.4  pooka 	size_t rw_dlen;
    152  1.21  pooka 	int rw_done;
    153  1.13  pooka 	int rw_error;
    154   1.4  pooka 
    155   1.4  pooka 	pthread_cond_t rw_cv;
    156   1.4  pooka 
    157   1.4  pooka 	TAILQ_ENTRY(respwait) rw_entries;
    158   1.4  pooka };
    159   1.1  pooka 
    160  1.18  pooka struct prefork;
    161   1.1  pooka struct spclient {
    162   1.1  pooka 	int spc_fd;
    163   1.7  pooka 	int spc_refcnt;
    164  1.17  pooka 	int spc_state;
    165   1.6  pooka 
    166  1.11  pooka 	pthread_mutex_t spc_mtx;
    167  1.11  pooka 	pthread_cond_t spc_cv;
    168  1.11  pooka 
    169   1.6  pooka 	struct lwp *spc_mainlwp;
    170   1.6  pooka 	pid_t spc_pid;
    171   1.1  pooka 
    172  1.11  pooka 	TAILQ_HEAD(, respwait) spc_respwait;
    173  1.11  pooka 
    174  1.11  pooka 	/* rest of the fields are zeroed upon disconnect */
    175  1.12  pooka #define SPC_ZEROFF offsetof(struct spclient, spc_pfd)
    176   1.7  pooka 	struct pollfd *spc_pfd;
    177   1.7  pooka 
    178   1.1  pooka 	struct rsp_hdr spc_hdr;
    179   1.1  pooka 	uint8_t *spc_buf;
    180   1.1  pooka 	size_t spc_off;
    181   1.1  pooka 
    182   1.4  pooka 	uint64_t spc_nextreq;
    183  1.25  pooka 	uint64_t spc_syscallreq;
    184  1.26  pooka 	uint64_t spc_generation;
    185   1.4  pooka 	int spc_ostatus, spc_istatus;
    186  1.26  pooka 	int spc_reconnecting;
    187  1.30  pooka 	int spc_inexec;
    188  1.18  pooka 
    189  1.18  pooka 	LIST_HEAD(, prefork) spc_pflist;
    190   1.1  pooka };
    191   1.4  pooka #define SPCSTATUS_FREE 0
    192   1.4  pooka #define SPCSTATUS_BUSY 1
    193   1.4  pooka #define SPCSTATUS_WANTED 2
    194   1.1  pooka 
    195  1.17  pooka #define SPCSTATE_NEW     0
    196  1.17  pooka #define SPCSTATE_RUNNING 1
    197  1.17  pooka #define SPCSTATE_DYING   2
    198  1.17  pooka 
    199   1.1  pooka typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
    200   1.1  pooka typedef int (*connecthook_fn)(int);
    201  1.15  pooka typedef void (*cleanup_fn)(struct sockaddr *);
    202   1.1  pooka 
    203   1.4  pooka static int readframe(struct spclient *);
    204   1.4  pooka static void handlereq(struct spclient *);
    205   1.4  pooka 
    206  1.13  pooka static __inline void
    207  1.13  pooka spcresetbuf(struct spclient *spc)
    208  1.13  pooka {
    209  1.13  pooka 
    210  1.13  pooka 	spc->spc_buf = NULL;
    211  1.13  pooka 	spc->spc_off = 0;
    212  1.13  pooka }
    213  1.13  pooka 
    214  1.13  pooka static __inline void
    215  1.13  pooka spcfreebuf(struct spclient *spc)
    216  1.13  pooka {
    217  1.13  pooka 
    218  1.13  pooka 	free(spc->spc_buf);
    219  1.13  pooka 	spcresetbuf(spc);
    220  1.13  pooka }
    221  1.13  pooka 
    222   1.4  pooka static void
    223  1.12  pooka sendlockl(struct spclient *spc)
    224   1.4  pooka {
    225   1.4  pooka 
    226   1.4  pooka 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    227   1.4  pooka 		spc->spc_ostatus = SPCSTATUS_WANTED;
    228   1.4  pooka 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    229   1.4  pooka 	}
    230   1.4  pooka 	spc->spc_ostatus = SPCSTATUS_BUSY;
    231  1.12  pooka }
    232  1.12  pooka 
    233  1.26  pooka static void __unused
    234  1.12  pooka sendlock(struct spclient *spc)
    235  1.12  pooka {
    236  1.12  pooka 
    237  1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    238  1.12  pooka 	sendlockl(spc);
    239   1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    240   1.4  pooka }
    241   1.4  pooka 
    242   1.4  pooka static void
    243  1.12  pooka sendunlockl(struct spclient *spc)
    244   1.4  pooka {
    245   1.4  pooka 
    246   1.4  pooka 	if (spc->spc_ostatus == SPCSTATUS_WANTED)
    247   1.4  pooka 		pthread_cond_broadcast(&spc->spc_cv);
    248   1.4  pooka 	spc->spc_ostatus = SPCSTATUS_FREE;
    249  1.12  pooka }
    250  1.12  pooka 
    251  1.12  pooka static void
    252  1.12  pooka sendunlock(struct spclient *spc)
    253  1.12  pooka {
    254  1.12  pooka 
    255  1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    256  1.12  pooka 	sendunlockl(spc);
    257   1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    258   1.4  pooka }
    259   1.1  pooka 
    260   1.1  pooka static int
    261  1.31  pooka dosend(struct spclient *spc, struct iovec *iov, size_t iovlen)
    262   1.1  pooka {
    263  1.31  pooka 	struct msghdr msg;
    264   1.1  pooka 	struct pollfd pfd;
    265  1.31  pooka 	ssize_t n = 0;
    266   1.1  pooka 	int fd = spc->spc_fd;
    267   1.1  pooka 
    268   1.1  pooka 	pfd.fd = fd;
    269   1.1  pooka 	pfd.events = POLLOUT;
    270   1.1  pooka 
    271  1.31  pooka 	memset(&msg, 0, sizeof(msg));
    272  1.31  pooka 
    273  1.31  pooka 	for (;;) {
    274  1.31  pooka 		/* not first round?  poll */
    275   1.1  pooka 		if (n) {
    276  1.20  pooka 			if (host_poll(&pfd, 1, INFTIM) == -1) {
    277   1.1  pooka 				if (errno == EINTR)
    278   1.1  pooka 					continue;
    279   1.1  pooka 				return errno;
    280   1.1  pooka 			}
    281   1.1  pooka 		}
    282   1.1  pooka 
    283  1.31  pooka 		msg.msg_iov = iov;
    284  1.31  pooka 		msg.msg_iovlen = iovlen;
    285  1.31  pooka 		n = host_sendmsg(fd, &msg, MSG_NOSIGNAL);
    286  1.10  pooka 		if (n == -1)  {
    287  1.26  pooka 			if (errno == EPIPE)
    288  1.26  pooka 				return ENOTCONN;
    289  1.10  pooka 			if (errno != EAGAIN)
    290  1.18  pooka 				return errno;
    291  1.10  pooka 			continue;
    292   1.1  pooka 		}
    293  1.26  pooka 		if (n == 0) {
    294  1.26  pooka 			return ENOTCONN;
    295  1.26  pooka 		}
    296  1.31  pooka 
    297  1.31  pooka 		/* ok, need to adjust iovec for potential next round */
    298  1.31  pooka 		while (n >= (ssize_t)iov[0].iov_len && iovlen) {
    299  1.31  pooka 			n -= iov[0].iov_len;
    300  1.31  pooka 			iov++;
    301  1.31  pooka 			iovlen--;
    302  1.31  pooka 		}
    303  1.31  pooka 
    304  1.31  pooka 		if (iovlen == 0) {
    305  1.31  pooka 			_DIAGASSERT(n == 0);
    306  1.31  pooka 			break;
    307  1.31  pooka 		} else {
    308  1.31  pooka 			iov[0].iov_base = (uint8_t *)iov[0].iov_base + n;
    309  1.31  pooka 			iov[0].iov_len -= n;
    310  1.31  pooka 		}
    311   1.1  pooka 	}
    312   1.1  pooka 
    313   1.1  pooka 	return 0;
    314   1.1  pooka }
    315   1.1  pooka 
    316   1.4  pooka static void
    317  1.26  pooka doputwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    318   1.4  pooka {
    319   1.4  pooka 
    320   1.4  pooka 	rw->rw_data = NULL;
    321  1.22  pooka 	rw->rw_dlen = rw->rw_done = rw->rw_error = 0;
    322   1.4  pooka 	pthread_cond_init(&rw->rw_cv, NULL);
    323   1.4  pooka 
    324   1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    325   1.4  pooka 	rw->rw_reqno = rhdr->rsp_reqno = spc->spc_nextreq++;
    326   1.4  pooka 	TAILQ_INSERT_TAIL(&spc->spc_respwait, rw, rw_entries);
    327  1.26  pooka }
    328  1.26  pooka 
    329  1.26  pooka static void __unused
    330  1.26  pooka putwait_locked(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    331  1.26  pooka {
    332  1.26  pooka 
    333  1.26  pooka 	doputwait(spc, rw, rhdr);
    334  1.26  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    335  1.26  pooka }
    336  1.26  pooka 
    337  1.26  pooka static void
    338  1.26  pooka putwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    339  1.26  pooka {
    340  1.12  pooka 
    341  1.26  pooka 	doputwait(spc, rw, rhdr);
    342  1.12  pooka 	sendlockl(spc);
    343  1.23  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    344   1.8  pooka }
    345   1.8  pooka 
    346   1.8  pooka static void
    347  1.26  pooka dounputwait(struct spclient *spc, struct respwait *rw)
    348  1.26  pooka {
    349  1.26  pooka 
    350  1.26  pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    351  1.26  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    352  1.26  pooka 	pthread_cond_destroy(&rw->rw_cv);
    353  1.26  pooka 
    354  1.26  pooka }
    355  1.26  pooka 
    356  1.26  pooka static void __unused
    357  1.26  pooka unputwait_locked(struct spclient *spc, struct respwait *rw)
    358  1.26  pooka {
    359  1.26  pooka 
    360  1.26  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    361  1.26  pooka 	dounputwait(spc, rw);
    362  1.26  pooka }
    363  1.26  pooka 
    364  1.26  pooka static void
    365   1.8  pooka unputwait(struct spclient *spc, struct respwait *rw)
    366   1.8  pooka {
    367   1.8  pooka 
    368  1.23  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    369  1.12  pooka 	sendunlockl(spc);
    370  1.12  pooka 
    371  1.26  pooka 	dounputwait(spc, rw);
    372   1.4  pooka }
    373   1.4  pooka 
    374   1.4  pooka static void
    375   1.4  pooka kickwaiter(struct spclient *spc)
    376   1.4  pooka {
    377   1.4  pooka 	struct respwait *rw;
    378  1.22  pooka 	int error = 0;
    379   1.4  pooka 
    380   1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    381   1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries) {
    382   1.4  pooka 		if (rw->rw_reqno == spc->spc_hdr.rsp_reqno)
    383   1.4  pooka 			break;
    384   1.4  pooka 	}
    385   1.4  pooka 	if (rw == NULL) {
    386  1.13  pooka 		DPRINTF(("no waiter found, invalid reqno %" PRIu64 "?\n",
    387  1.13  pooka 		    spc->spc_hdr.rsp_reqno));
    388  1.23  pooka 		pthread_mutex_unlock(&spc->spc_mtx);
    389  1.18  pooka 		spcfreebuf(spc);
    390   1.4  pooka 		return;
    391   1.4  pooka 	}
    392  1.10  pooka 	DPRINTF(("rump_sp: client %p woke up waiter at %p\n", spc, rw));
    393   1.4  pooka 	rw->rw_data = spc->spc_buf;
    394  1.21  pooka 	rw->rw_done = 1;
    395  1.11  pooka 	rw->rw_dlen = (size_t)(spc->spc_off - HDRSZ);
    396  1.13  pooka 	if (spc->spc_hdr.rsp_class == RUMPSP_ERROR) {
    397  1.14  pooka 		error = rw->rw_error = spc->spc_hdr.rsp_error;
    398  1.13  pooka 	}
    399   1.4  pooka 	pthread_cond_signal(&rw->rw_cv);
    400   1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    401   1.4  pooka 
    402  1.14  pooka 	if (error)
    403  1.13  pooka 		spcfreebuf(spc);
    404  1.13  pooka 	else
    405  1.13  pooka 		spcresetbuf(spc);
    406   1.4  pooka }
    407   1.4  pooka 
    408   1.4  pooka static void
    409   1.4  pooka kickall(struct spclient *spc)
    410   1.4  pooka {
    411   1.4  pooka 	struct respwait *rw;
    412   1.4  pooka 
    413   1.4  pooka 	/* DIAGASSERT(mutex_owned(spc_lock)) */
    414   1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries)
    415  1.12  pooka 		pthread_cond_broadcast(&rw->rw_cv);
    416   1.4  pooka }
    417   1.4  pooka 
    418   1.4  pooka static int
    419   1.1  pooka readframe(struct spclient *spc)
    420   1.1  pooka {
    421   1.1  pooka 	int fd = spc->spc_fd;
    422   1.1  pooka 	size_t left;
    423   1.1  pooka 	size_t framelen;
    424   1.1  pooka 	ssize_t n;
    425   1.1  pooka 
    426   1.1  pooka 	/* still reading header? */
    427   1.1  pooka 	if (spc->spc_off < HDRSZ) {
    428   1.1  pooka 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    429   1.1  pooka 		    spc->spc_off));
    430   1.1  pooka 
    431   1.1  pooka 		left = HDRSZ - spc->spc_off;
    432   1.1  pooka 		/*LINTED: cast ok */
    433  1.20  pooka 		n = host_read(fd, (uint8_t*)&spc->spc_hdr + spc->spc_off, left);
    434   1.1  pooka 		if (n == 0) {
    435   1.1  pooka 			return -1;
    436   1.1  pooka 		}
    437   1.1  pooka 		if (n == -1) {
    438   1.1  pooka 			if (errno == EAGAIN)
    439   1.1  pooka 				return 0;
    440   1.1  pooka 			return -1;
    441   1.1  pooka 		}
    442   1.1  pooka 
    443   1.1  pooka 		spc->spc_off += n;
    444  1.29  pooka 		if (spc->spc_off < HDRSZ) {
    445  1.29  pooka 			return 0;
    446  1.29  pooka 		}
    447   1.1  pooka 
    448   1.1  pooka 		/*LINTED*/
    449   1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    450   1.1  pooka 
    451   1.1  pooka 		if (framelen < HDRSZ) {
    452   1.1  pooka 			return -1;
    453   1.1  pooka 		} else if (framelen == HDRSZ) {
    454   1.1  pooka 			return 1;
    455   1.1  pooka 		}
    456   1.1  pooka 
    457   1.1  pooka 		spc->spc_buf = malloc(framelen - HDRSZ);
    458   1.1  pooka 		if (spc->spc_buf == NULL) {
    459   1.1  pooka 			return -1;
    460   1.1  pooka 		}
    461   1.1  pooka 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    462   1.1  pooka 
    463   1.1  pooka 		/* "fallthrough" */
    464   1.1  pooka 	} else {
    465   1.1  pooka 		/*LINTED*/
    466   1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    467   1.1  pooka 	}
    468   1.1  pooka 
    469   1.1  pooka 	left = framelen - spc->spc_off;
    470   1.1  pooka 
    471   1.1  pooka 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    472   1.1  pooka 	    spc->spc_off, left));
    473   1.1  pooka 
    474   1.1  pooka 	if (left == 0)
    475   1.1  pooka 		return 1;
    476  1.20  pooka 	n = host_read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    477   1.1  pooka 	if (n == 0) {
    478   1.1  pooka 		return -1;
    479   1.1  pooka 	}
    480   1.1  pooka 	if (n == -1) {
    481   1.1  pooka 		if (errno == EAGAIN)
    482   1.1  pooka 			return 0;
    483   1.1  pooka 		return -1;
    484   1.1  pooka 	}
    485   1.1  pooka 	spc->spc_off += n;
    486   1.1  pooka 	left -= n;
    487   1.1  pooka 
    488   1.1  pooka 	/* got everything? */
    489   1.1  pooka 	if (left == 0)
    490   1.1  pooka 		return 1;
    491   1.1  pooka 	else
    492   1.1  pooka 		return 0;
    493   1.1  pooka }
    494   1.1  pooka 
    495   1.1  pooka static int
    496   1.1  pooka tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    497   1.1  pooka {
    498   1.1  pooka 	struct sockaddr_in sin;
    499   1.1  pooka 	char buf[64];
    500   1.1  pooka 	const char *p;
    501   1.1  pooka 	size_t l;
    502   1.1  pooka 	int port;
    503   1.1  pooka 
    504   1.1  pooka 	memset(&sin, 0, sizeof(sin));
    505   1.1  pooka 	sin.sin_len = sizeof(sin);
    506   1.1  pooka 	sin.sin_family = AF_INET;
    507   1.1  pooka 
    508   1.1  pooka 	p = strchr(addr, ':');
    509   1.1  pooka 	if (!p) {
    510   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    511   1.1  pooka 		return EINVAL;
    512   1.1  pooka 	}
    513   1.1  pooka 
    514   1.1  pooka 	l = p - addr;
    515   1.1  pooka 	if (l > sizeof(buf)-1) {
    516   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    517   1.1  pooka 		return EINVAL;
    518   1.1  pooka 	}
    519   1.1  pooka 	strncpy(buf, addr, l);
    520   1.1  pooka 	buf[l] = '\0';
    521   1.1  pooka 
    522   1.1  pooka 	/* special INADDR_ANY treatment */
    523   1.1  pooka 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    524   1.1  pooka 		sin.sin_addr.s_addr = INADDR_ANY;
    525   1.1  pooka 	} else {
    526   1.1  pooka 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    527   1.1  pooka 		case 1:
    528   1.1  pooka 			break;
    529   1.1  pooka 		case 0:
    530   1.1  pooka 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    531   1.1  pooka 			return EINVAL;
    532   1.1  pooka 		case -1:
    533   1.1  pooka 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    534   1.1  pooka 			return errno;
    535   1.1  pooka 		default:
    536   1.1  pooka 			assert(/*CONSTCOND*/0);
    537   1.1  pooka 			return EINVAL;
    538   1.1  pooka 		}
    539   1.1  pooka 	}
    540   1.1  pooka 
    541   1.1  pooka 	if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
    542   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    543   1.1  pooka 		return EINVAL;
    544   1.1  pooka 	}
    545   1.1  pooka 
    546   1.1  pooka 	/* advance to port number & parse */
    547   1.1  pooka 	p++;
    548   1.1  pooka 	l = strspn(p, "0123456789");
    549   1.1  pooka 	if (l == 0) {
    550   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    551   1.1  pooka 		return EINVAL;
    552   1.1  pooka 	}
    553   1.1  pooka 	strncpy(buf, p, l);
    554   1.1  pooka 	buf[l] = '\0';
    555   1.1  pooka 
    556   1.1  pooka 	if (*(p+l) != '/' && *(p+l) != '\0') {
    557   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    558   1.1  pooka 		return EINVAL;
    559   1.1  pooka 	}
    560   1.1  pooka 
    561   1.1  pooka 	port = atoi(buf);
    562   1.1  pooka 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    563   1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    564   1.1  pooka 		return ERANGE;
    565   1.1  pooka 	}
    566   1.1  pooka 	sin.sin_port = htons(port);
    567   1.1  pooka 
    568   1.1  pooka 	*sa = malloc(sizeof(sin));
    569   1.1  pooka 	if (*sa == NULL)
    570   1.1  pooka 		return errno;
    571   1.1  pooka 	memcpy(*sa, &sin, sizeof(sin));
    572   1.1  pooka 	return 0;
    573   1.1  pooka }
    574   1.1  pooka 
    575   1.1  pooka static int
    576   1.1  pooka tcp_connecthook(int s)
    577   1.1  pooka {
    578   1.1  pooka 	int x;
    579   1.1  pooka 
    580   1.1  pooka 	x = 1;
    581  1.20  pooka 	host_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    582   1.1  pooka 
    583   1.1  pooka 	return 0;
    584   1.1  pooka }
    585   1.1  pooka 
    586  1.27  pooka static char parsedurl[256];
    587  1.27  pooka 
    588   1.5  pooka /*ARGSUSED*/
    589   1.2  pooka static int
    590   1.2  pooka unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    591   1.2  pooka {
    592   1.2  pooka 	struct sockaddr_un sun;
    593   1.5  pooka 	size_t slen;
    594  1.27  pooka 	int savepath = 0;
    595   1.2  pooka 
    596   1.2  pooka 	if (strlen(addr) > sizeof(sun.sun_path))
    597   1.2  pooka 		return ENAMETOOLONG;
    598   1.2  pooka 
    599   1.2  pooka 	/*
    600   1.2  pooka 	 * The pathname can be all kinds of spaghetti elementals,
    601  1.15  pooka 	 * so meek and obidient we accept everything.  However, use
    602  1.15  pooka 	 * full path for easy cleanup in case someone gives a relative
    603  1.15  pooka 	 * one and the server does a chdir() between now than the
    604  1.15  pooka 	 * cleanup.
    605   1.2  pooka 	 */
    606   1.2  pooka 	memset(&sun, 0, sizeof(sun));
    607   1.2  pooka 	sun.sun_family = AF_LOCAL;
    608  1.15  pooka 	if (*addr != '/') {
    609  1.15  pooka 		char mywd[PATH_MAX];
    610  1.15  pooka 
    611  1.15  pooka 		if (getcwd(mywd, sizeof(mywd)) == NULL) {
    612  1.15  pooka 			fprintf(stderr, "warning: cannot determine cwd, "
    613  1.15  pooka 			    "omitting socket cleanup\n");
    614  1.15  pooka 		} else {
    615  1.15  pooka 			if (strlen(addr) + strlen(mywd) > sizeof(sun.sun_path))
    616  1.15  pooka 				return ENAMETOOLONG;
    617  1.15  pooka 			strlcpy(sun.sun_path, mywd, sizeof(sun.sun_path));
    618  1.15  pooka 			strlcat(sun.sun_path, "/", sizeof(sun.sun_path));
    619  1.27  pooka 			savepath = 1;
    620  1.15  pooka 		}
    621  1.15  pooka 	}
    622  1.15  pooka 	strlcat(sun.sun_path, addr, sizeof(sun.sun_path));
    623  1.15  pooka 	sun.sun_len = SUN_LEN(&sun);
    624  1.15  pooka 	slen = sun.sun_len+1; /* get the 0 too */
    625   1.2  pooka 
    626  1.27  pooka 	if (savepath && *parsedurl == '\0') {
    627  1.27  pooka 		snprintf(parsedurl, sizeof(parsedurl),
    628  1.27  pooka 		    "unix://%s", sun.sun_path);
    629  1.27  pooka 	}
    630  1.27  pooka 
    631   1.5  pooka 	*sa = malloc(slen);
    632   1.2  pooka 	if (*sa == NULL)
    633   1.2  pooka 		return errno;
    634   1.5  pooka 	memcpy(*sa, &sun, slen);
    635   1.2  pooka 
    636   1.2  pooka 	return 0;
    637   1.2  pooka }
    638   1.2  pooka 
    639  1.15  pooka static void
    640  1.15  pooka unix_cleanup(struct sockaddr *sa)
    641  1.15  pooka {
    642  1.15  pooka 	struct sockaddr_un *sun = (void *)sa;
    643  1.15  pooka 
    644  1.15  pooka 	/*
    645  1.15  pooka 	 * cleanup only absolute paths.  see unix_parse() above
    646  1.15  pooka 	 */
    647  1.15  pooka 	if (*sun->sun_path == '/') {
    648  1.15  pooka 		unlink(sun->sun_path);
    649  1.15  pooka 	}
    650  1.15  pooka }
    651  1.15  pooka 
    652   1.1  pooka /*ARGSUSED*/
    653   1.1  pooka static int
    654   1.1  pooka notsupp(void)
    655   1.1  pooka {
    656   1.1  pooka 
    657   1.1  pooka 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    658   1.1  pooka 	return EOPNOTSUPP;
    659   1.1  pooka }
    660   1.1  pooka 
    661   1.1  pooka static int
    662   1.1  pooka success(void)
    663   1.1  pooka {
    664   1.1  pooka 
    665   1.1  pooka 	return 0;
    666   1.1  pooka }
    667   1.1  pooka 
    668   1.1  pooka struct {
    669   1.1  pooka 	const char *id;
    670   1.1  pooka 	int domain;
    671   1.1  pooka 	addrparse_fn ap;
    672   1.1  pooka 	connecthook_fn connhook;
    673  1.15  pooka 	cleanup_fn cleanup;
    674   1.1  pooka } parsetab[] = {
    675  1.15  pooka 	{ "tcp", PF_INET, tcp_parse, tcp_connecthook, (cleanup_fn)success },
    676  1.15  pooka 	{ "unix", PF_LOCAL, unix_parse, (connecthook_fn)success, unix_cleanup },
    677  1.15  pooka 	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success,
    678  1.15  pooka 			    (cleanup_fn)success },
    679   1.1  pooka };
    680   1.1  pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    681   1.1  pooka 
    682   1.1  pooka static int
    683   1.1  pooka parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
    684   1.1  pooka 	int allow_wildcard)
    685   1.1  pooka {
    686   1.1  pooka 	char id[16];
    687   1.1  pooka 	const char *p, *p2;
    688   1.1  pooka 	size_t l;
    689   1.1  pooka 	unsigned i;
    690   1.1  pooka 	int error;
    691   1.1  pooka 
    692   1.1  pooka 	/*
    693   1.1  pooka 	 * Parse the url
    694   1.1  pooka 	 */
    695   1.1  pooka 
    696   1.1  pooka 	p = url;
    697   1.1  pooka 	p2 = strstr(p, "://");
    698   1.1  pooka 	if (!p2) {
    699   1.1  pooka 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    700   1.1  pooka 		return EINVAL;
    701   1.1  pooka 	}
    702   1.1  pooka 	l = p2-p;
    703   1.1  pooka 	if (l > sizeof(id)-1) {
    704   1.1  pooka 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    705   1.1  pooka 		return EINVAL;
    706   1.1  pooka 	}
    707   1.1  pooka 
    708   1.1  pooka 	strncpy(id, p, l);
    709   1.1  pooka 	id[l] = '\0';
    710   1.1  pooka 	p2 += 3; /* beginning of address */
    711   1.1  pooka 
    712   1.1  pooka 	for (i = 0; i < NPARSE; i++) {
    713   1.1  pooka 		if (strcmp(id, parsetab[i].id) == 0) {
    714   1.1  pooka 			error = parsetab[i].ap(p2, sap, allow_wildcard);
    715   1.1  pooka 			if (error)
    716   1.1  pooka 				return error;
    717   1.1  pooka 			break;
    718   1.1  pooka 		}
    719   1.1  pooka 	}
    720   1.1  pooka 	if (i == NPARSE) {
    721   1.1  pooka 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    722   1.1  pooka 		return EINVAL;
    723   1.1  pooka 	}
    724   1.1  pooka 
    725   1.1  pooka 	*idxp = i;
    726   1.1  pooka 	return 0;
    727   1.1  pooka }
    728