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