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