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