| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: sp_common.c,v 1.1 2010/11/04 20:54:07 pooka Exp $ */ | | 1 | /* $NetBSD: sp_common.c,v 1.2 2010/11/05 14:23:45 pooka Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2010 Antti Kantee. All Rights Reserved. | | 4 | * Copyright (c) 2010 Antti Kantee. All Rights Reserved. |
5 | * | | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | | 7 | * modification, are permitted provided that the following conditions |
8 | * are met: | | 8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright | | 9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright | | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the | | 12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. | | 13 | * documentation and/or other materials provided with the distribution. |
14 | * | | 14 | * |
| @@ -24,26 +24,27 @@ | | | @@ -24,26 +24,27 @@ |
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | * SUCH DAMAGE. | | 25 | * SUCH DAMAGE. |
26 | */ | | 26 | */ |
27 | | | 27 | |
28 | /* | | 28 | /* |
29 | * Common client/server sysproxy routines. #included. | | 29 | * Common client/server sysproxy routines. #included. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | | | 33 | |
34 | #include <sys/types.h> | | 34 | #include <sys/types.h> |
35 | #include <sys/mman.h> | | 35 | #include <sys/mman.h> |
36 | #include <sys/socket.h> | | 36 | #include <sys/socket.h> |
| | | 37 | #include <sys/un.h> |
37 | | | 38 | |
38 | #include <arpa/inet.h> | | 39 | #include <arpa/inet.h> |
39 | #include <netinet/in.h> | | 40 | #include <netinet/in.h> |
40 | #include <netinet/tcp.h> | | 41 | #include <netinet/tcp.h> |
41 | | | 42 | |
42 | #include <assert.h> | | 43 | #include <assert.h> |
43 | #include <errno.h> | | 44 | #include <errno.h> |
44 | #include <fcntl.h> | | 45 | #include <fcntl.h> |
45 | #include <poll.h> | | 46 | #include <poll.h> |
46 | #include <stdarg.h> | | 47 | #include <stdarg.h> |
47 | #include <stdio.h> | | 48 | #include <stdio.h> |
48 | #include <stdlib.h> | | 49 | #include <stdlib.h> |
49 | #include <string.h> | | 50 | #include <string.h> |
| @@ -319,50 +320,75 @@ tcp_parse(const char *addr, struct socka | | | @@ -319,50 +320,75 @@ tcp_parse(const char *addr, struct socka |
319 | } | | 320 | } |
320 | | | 321 | |
321 | static int | | 322 | static int |
322 | tcp_connecthook(int s) | | 323 | tcp_connecthook(int s) |
323 | { | | 324 | { |
324 | int x; | | 325 | int x; |
325 | | | 326 | |
326 | x = 1; | | 327 | x = 1; |
327 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x)); | | 328 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x)); |
328 | | | 329 | |
329 | return 0; | | 330 | return 0; |
330 | } | | 331 | } |
331 | | | 332 | |
| | | 333 | static int |
| | | 334 | unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard) |
| | | 335 | { |
| | | 336 | struct sockaddr_un sun; |
| | | 337 | |
| | | 338 | if (strlen(addr) > sizeof(sun.sun_path)) |
| | | 339 | return ENAMETOOLONG; |
| | | 340 | |
| | | 341 | /* |
| | | 342 | * The pathname can be all kinds of spaghetti elementals, |
| | | 343 | * so meek and obidient we accept everything. |
| | | 344 | */ |
| | | 345 | memset(&sun, 0, sizeof(sun)); |
| | | 346 | sun.sun_family = AF_LOCAL; |
| | | 347 | strlcpy(sun.sun_path, addr, sizeof(sun.sun_path)); |
| | | 348 | sun.sun_len = SUN_LEN(&sun); |
| | | 349 | |
| | | 350 | *sa = malloc(sun.sun_len); |
| | | 351 | if (*sa == NULL) |
| | | 352 | return errno; |
| | | 353 | memcpy(*sa, &sun, sun.sun_len); |
| | | 354 | |
| | | 355 | return 0; |
| | | 356 | } |
| | | 357 | |
332 | /*ARGSUSED*/ | | 358 | /*ARGSUSED*/ |
333 | static int | | 359 | static int |
334 | notsupp(void) | | 360 | notsupp(void) |
335 | { | | 361 | { |
336 | | | 362 | |
337 | fprintf(stderr, "rump_sp: support not yet implemented\n"); | | 363 | fprintf(stderr, "rump_sp: support not yet implemented\n"); |
338 | return EOPNOTSUPP; | | 364 | return EOPNOTSUPP; |
339 | } | | 365 | } |
340 | | | 366 | |
341 | static int | | 367 | static int |
342 | success(void) | | 368 | success(void) |
343 | { | | 369 | { |
344 | | | 370 | |
345 | return 0; | | 371 | return 0; |
346 | } | | 372 | } |
347 | | | 373 | |
348 | struct { | | 374 | struct { |
349 | const char *id; | | 375 | const char *id; |
350 | int domain; | | 376 | int domain; |
351 | addrparse_fn ap; | | 377 | addrparse_fn ap; |
352 | connecthook_fn connhook; | | 378 | connecthook_fn connhook; |
353 | } parsetab[] = { | | 379 | } parsetab[] = { |
354 | { "tcp", PF_INET, tcp_parse, tcp_connecthook }, | | 380 | { "tcp", PF_INET, tcp_parse, tcp_connecthook }, |
355 | { "unix", PF_LOCAL, (addrparse_fn)notsupp, (connecthook_fn)success }, | | 381 | { "unix", PF_LOCAL, unix_parse, (connecthook_fn)success }, |
356 | { "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success }, | | 382 | { "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success }, |
357 | }; | | 383 | }; |
358 | #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0])) | | 384 | #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0])) |
359 | | | 385 | |
360 | static int | | 386 | static int |
361 | parseurl(const char *url, struct sockaddr **sap, unsigned *idxp, | | 387 | parseurl(const char *url, struct sockaddr **sap, unsigned *idxp, |
362 | int allow_wildcard) | | 388 | int allow_wildcard) |
363 | { | | 389 | { |
364 | char id[16]; | | 390 | char id[16]; |
365 | const char *p, *p2; | | 391 | const char *p, *p2; |
366 | size_t l; | | 392 | size_t l; |
367 | unsigned i; | | 393 | unsigned i; |
368 | int error; | | 394 | int error; |