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