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