5a7dbfa8d101866f2e864ab687b719dd9767af9a
[fwd] / socket.c
1 /* -*-c-*-
2 *
3 * Socket source and target definitions
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the `fw' port forwarder.
11 *
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "config.h"
30
31 #include <ctype.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45
46 #include <mLib/alloc.h>
47 #include <mLib/conn.h>
48 #include <mLib/dstr.h>
49 #include <mLib/fdflags.h>
50 #include <mLib/sel.h>
51 #include <mLib/sub.h>
52
53 #include "addr.h"
54 #include "conf.h"
55 #include "endpt.h"
56 #include "fw.h"
57 #include "scan.h"
58 #include "socket.h"
59 #include "target.h"
60
61 #include "inet.h"
62 #include "un.h"
63
64 /*----- Data structures ---------------------------------------------------*/
65
66 /* --- Socket source options --- */
67
68 typedef struct ssource_opts {
69 unsigned opt;
70 unsigned conn;
71 unsigned listen;
72 } ssource_opts;
73
74 static ssource_opts ssgo = { 256, 0, 5 };
75
76 #define SOCKOPT_LIMIT 0u
77 #define SOCKOPT_NOLIMIT 1u
78 #define SOCKOPT_ONESHOT 2u
79
80 /* --- Socket source --- */
81
82 typedef struct ssource {
83 source s;
84 addr *a;
85 target *t;
86 addr_opts *ao;
87 ssource_opts o;
88 sel_file r;
89 } ssource;
90
91 /* --- Socket target --- */
92
93 typedef struct starget {
94 target t;
95 addr *a;
96 addr_opts *ao;
97 } starget;
98
99 /* --- Socket target endpoint --- */
100
101 typedef struct stept {
102 endpt e;
103 conn c;
104 char *desc;
105 } stept;
106
107 /* --- Socket source endpoint --- */
108
109 typedef struct ssept {
110 endpt e;
111 ssource *s;
112 } ssept;
113
114 /*----- Protocol table ----------------------------------------------------*/
115
116 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
117
118 /*----- Other persistent variables ----------------------------------------*/
119
120 static addr_opts gsao = { 0 }, gtao = { 0 };
121
122 /*----- Parsing address types ---------------------------------------------*/
123
124 /* --- @getaddrtype@ --- *
125 *
126 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
127 * @const char *p@ = pointer to protocol name
128 * @int abbrev@ = nonzero to allow abbreviations
129 *
130 * Returns: Pointer to address operations table or null.
131 *
132 * Use: Looks up a protocol name. Handy when parsing addresses and
133 * other bits of configuration. Returns null if no matching
134 * address was found.
135 */
136
137 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
138 {
139 addr_ops **ops;
140 addr_ops *chosen = 0;
141 size_t sz = strlen(p);
142
143 for (ops = addrs; *ops; ops++) {
144 if (strncmp((*ops)->name, p, sz) == 0) {
145 if ((*ops)->name[sz] == 0)
146 return (*ops);
147 else if (chosen && abbrev)
148 error(sc, "ambiguous socket address type `%s'", p);
149 chosen = *ops;
150 }
151 }
152 if (!abbrev)
153 return (0);
154 return (chosen);
155 }
156
157 /* --- @getaddr@ --- *
158 *
159 * Arguments: @scanner *sc@ = pointer to scanner to read from
160 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
161 *
162 * Returns: Pointer to an address successfully read.
163 *
164 * Use: Reads an optionally qualified address.
165 */
166
167 static addr *getaddr(scanner *sc, unsigned type)
168 {
169 addr_ops *ops = 0;
170 int abbrev = 0;
171
172 if (sc->t == ':') {
173 token(sc);
174 abbrev = 1;
175 }
176 if (sc->t == CTOK_WORD)
177 ops = getaddrtype(sc, sc->d.buf, abbrev);
178 if (ops)
179 token(sc);
180 else if (abbrev)
181 error(sc, "unknown socket address type `%s'", sc->d.buf);
182 else
183 ops = &inet_ops;
184 if (sc->t == ':')
185 token(sc);
186
187 return (ops->read(sc, type));
188 }
189
190 /*----- Socket endpoints --------------------------------------------------*/
191
192 /* --- @wclose@ --- */
193
194 static void sept_wclose(endpt *e)
195 {
196 shutdown(e->out->fd, 1);
197 }
198
199 /* --- @close@ (source) --- */
200
201 static void ss_listen(ssource */*ss*/);
202
203 static void ssept_close(endpt *e)
204 {
205 ssept *ee = (ssept *)e;
206
207 if (ee->s->o.opt == SOCKOPT_LIMIT) {
208 ee->s->o.conn++;
209 if (ee->s->o.conn == 1)
210 ss_listen(ee->s);
211 }
212 REFFD_DEC(ee->e.in);
213 REFFD_DEC(ee->e.out);
214 fw_dec();
215 DESTROY(ee);
216 }
217
218 /* --- @close@ (target) --- */
219
220 static void stept_close(endpt *e)
221 {
222 stept *ee = (stept *)e;
223
224 if (ee->e.f & EPF_PENDING)
225 conn_kill(&ee->c);
226 else {
227 REFFD_DEC(ee->e.in);
228 REFFD_DEC(ee->e.out);
229 }
230
231 xfree(ee->desc);
232 fw_dec();
233 DESTROY(ee);
234 }
235
236 /* --- @starget_connected@ --- *
237 *
238 * Arguments: @int fd@ = file descriptor now ready for use
239 * @void *p@ = pointer to an endpoint structure
240 *
241 * Returns: ---
242 *
243 * Use: Handles successful connection of the target endpoint.
244 */
245
246 void starget_connected(int fd, void *p)
247 {
248 stept *e = p;
249
250 if (fd == -1) {
251 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
252 endpt_kill(&e->e);
253 } else {
254 reffd *r = reffd_init(fd);
255 int opt = 1;
256 REFFD_INC(r);
257 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
258 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
259 e->e.in = e->e.out = r;
260 e->e.f &= ~EPF_PENDING;
261 if (e->e.other)
262 endpt_join(&e->e, e->e.other);
263 }
264 }
265
266 /* --- Socket endpoint definition --- */
267
268 static endpt_ops ssept_ops = {
269 0, 0, sept_wclose, ssept_close
270 };
271
272 static endpt_ops stept_ops = {
273 0, 0, sept_wclose, stept_close
274 };
275
276 /*----- Source definition -------------------------------------------------*/
277
278 /* --- @option@ --- */
279
280 static int ssource_option(source *s, scanner *sc)
281 {
282 ssource *ss = (ssource *)s;
283 ssource_opts *sso = ss ? &ss->o : &ssgo;
284
285 CONF_BEGIN(sc, "socket", "socket source")
286
287 /* --- Make sure the next token is a word --- */
288
289 if (sc->t != CTOK_WORD)
290 error(sc, "parse error, option keyword expected");
291
292 /* --- Handle options at this level --- */
293
294 if (strcmp(sc->d.buf, "conn") == 0) {
295 token(sc);
296 if (sc->t == '=')
297 token(sc);
298 if (sc->t != CTOK_WORD)
299 error(sc, "parse error, expected `unlimited', `one-shot' or number");
300 if (isdigit((unsigned char)sc->d.buf[0])) {
301 sso->conn = atoi(sc->d.buf);
302 if (sso->conn == 0)
303 error(sc, "argument of `conn' must be positive");
304 sso->opt = SOCKOPT_LIMIT;
305 token(sc);
306 } else {
307 sso->conn = 0;
308 sso->opt = 1 + (1 & conf_enum(sc,
309 "unlimited,one-shot,infinite",
310 ENUM_ABBREV, "`conn' option"));
311 }
312 CONF_ACCEPT;
313 }
314
315 if (strcmp(sc->d.buf, "listen") == 0) {
316 token(sc);
317 if (sc->t == '=')
318 token(sc);
319 if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0]))
320 error(sc, "parse error, expected number");
321 sso->listen = atoi(sc->d.buf);
322 if (sso->listen == 0)
323 error(sc, "argument of `listen' must be positive");
324 token(sc);
325 CONF_ACCEPT;
326 }
327
328 if (strcmp(sc->d.buf, "logging") == 0 ||
329 strcmp(sc->d.buf, "log") == 0) {
330 addr_opts *ao = ss ? ss->ao : &gsao;
331 token(sc);
332 if (sc->t == '=')
333 token(sc);
334 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
335 ao->f &= ~ADDRF_NOLOG;
336 else
337 ao->f |= ADDRF_NOLOG;
338 CONF_ACCEPT;
339 }
340
341 /* --- Pass the option around the various address types --- */
342
343 if (ss) {
344 if (ss->a->ops->option && ss->a->ops->option(sc, ss->ao, ADDR_SRC))
345 CONF_ACCEPT;
346 } else {
347 addr_ops **a;
348 for (a = addrs; *a; a++) {
349 if ((*a)->option && (*a)->option(sc, 0, ADDR_GLOBAL))
350 CONF_ACCEPT;
351 }
352 }
353
354 /* --- Nobody understood the option --- */
355
356 CONF_END;
357 }
358
359 /* --- @read@ --- */
360
361 static source *ssource_read(scanner *sc)
362 {
363 ssource *ss;
364
365 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
366 ss = CREATE(ssource);
367 ss->s.ops = &ssource_ops;
368 ss->s.desc = 0;
369 ss->t = 0;
370 ss->a = getaddr(sc, ADDR_SRC);
371 if (ss->a->ops->initsrcopts)
372 ss->ao = ss->a->ops->initsrcopts();
373 else {
374 ss->ao = CREATE(addr_opts);
375 *ss->ao = gsao;
376 }
377 ss->o = ssgo;
378 return (&ss->s);
379 }
380
381 /* --- @ss_accept@ --- *
382 *
383 * Arguments: @int fd@ = file descriptor to accept from
384 * @unsigned mode@ = what's ready with the descriptor
385 * @void *p@ = pointer to the source definition
386 *
387 * Returns: ---
388 *
389 * Use: Accepts an incoming connection and attaches it to a target
390 * endpoint.
391 */
392
393 static void ssource_destroy(source */*s*/);
394
395 static void ss_accept(int fd, unsigned mode, void *p)
396 {
397 ssource *ss = p;
398 ssept *e;
399 endpt *ee;
400 reffd *r;
401
402 /* --- Make the file descriptor --- */
403
404 {
405 int opt = 1;
406 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
407 return;
408 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
409 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410 }
411
412 /* --- Make an endpoint --- */
413
414 e = CREATE(ssept);
415 e->e.ops = &ssept_ops;
416 e->e.other = 0;
417 e->e.f = EPF_FILE;
418 e->e.t = 0;
419 e->e.in = e->e.out = r;
420 e->s = ss;
421 REFFD_INC(r);
422
423 /* --- Obtain the target endpoint and let rip --- */
424
425 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
426 REFFD_DEC(r);
427 REFFD_DEC(r);
428 DESTROY(e);
429 return;
430 }
431 fw_inc();
432
433 /* --- Remove the listening socket if necessary --- */
434
435 switch (ss->o.opt) {
436 case SOCKOPT_LIMIT:
437 ss->o.conn--;
438 if (!ss->o.conn) {
439 if (!(ss->ao->f & ADDRF_NOLOG))
440 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
441 sel_rmfile(&ss->r);
442 close(ss->r.fd);
443 if (ss->a->ops->unbind)
444 ss->a->ops->unbind(ss->a);
445 }
446 break;
447 case SOCKOPT_NOLIMIT:
448 break;
449 case SOCKOPT_ONESHOT:
450 sel_rmfile(&ss->r);
451 close(ss->r.fd);
452 if (ss->a->ops->unbind)
453 ss->a->ops->unbind(ss->a);
454 ssource_destroy(&ss->s);
455 break;
456 }
457
458 /* --- Let everything else happen --- */
459
460 endpt_join(&e->e, ee);
461 }
462
463 /* --- @ss_listen@ --- *
464 *
465 * Arguments: @ssource *ss@ = source to listen on
466 *
467 * Returns: ---
468 *
469 * Use: Sets the socket to listen again, if it stopped for some
470 * reason. This is a copy of the code in the @read@ function,
471 * because it has different (wildly different) error handling
472 * behaviour.
473 */
474
475 static void ss_listen(ssource *ss)
476 {
477 int fd;
478 int opt = 1;
479
480 if (!(ss->ao->f & ADDRF_NOLOG))
481 fw_log(-1, "[%s] reattaching listener", ss->s.desc);
482
483 /* --- Make the socket --- */
484
485 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0) {
486 fw_log(-1, "[%s] couldn't create socket: %s",
487 ss->s.desc, strerror(errno));
488 goto fail_0;
489 }
490
491 /* --- Set it to listen for connections --- */
492
493 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
494 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
495 if (listen(fd, ss->o.listen)) {
496 fw_log(-1, "[%s] couldn't listen on socket: %s",
497 ss->s.desc, strerror(errno));
498 goto fail_1;
499 }
500
501 /* --- Set the listener up again --- */
502
503 ss->r.fd = fd;
504 sel_addfile(&ss->r);
505 return;
506
507 /* --- Tidy up if it failed --- *
508 *
509 * I'll just remove the entire source.
510 */
511
512 fail_1:
513 close(fd);
514 fail_0:
515 ss->o.conn = 0;
516 ssource_destroy(&ss->s);
517 }
518
519 /* --- @attach@ --- */
520
521 static void ssource_attach(source *s, scanner *sc, target *t)
522 {
523 ssource *ss = (ssource *)s;
524 int fd;
525 int opt = 1;
526
527 ss->t = t;
528
529 /* --- Initialize the description string --- */
530
531 {
532 dstr d = DSTR_INIT;
533 dstr_puts(&d, "socket.");
534 ss->a->ops->print(ss->a, ADDR_SRC, &d);
535 dstr_puts(&d, " -> ");
536 dstr_puts(&d, ss->t->desc);
537 ss->s.desc = xstrdup(d.buf);
538 dstr_destroy(&d);
539 }
540
541 /* --- Confirm the address --- */
542
543 if (ss->a->ops->confirm)
544 ss->a->ops->confirm(ss->a, ADDR_SRC, ss->ao);
545
546 /* --- Initialize the socket for listening --- */
547
548 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0)
549 error(sc, "couldn't bind socket `%s': %s", ss->s.desc, strerror(errno));
550
551 /* --- Set it to listen for connections --- */
552
553 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
554 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
555 if (listen(fd, ss->o.listen)) {
556 error(sc, "couldn't listen on socket `%s': %s",
557 ss->s.desc, strerror(errno));
558 }
559
560 /* --- We're ready to go now --- */
561
562 sel_initfile(sel, &ss->r, fd, SEL_READ, ss_accept, ss);
563 sel_addfile(&ss->r);
564 source_add(&ss->s);
565 fw_inc();
566 }
567
568 /* --- @destroy@ --- */
569
570 static void ssource_destroy(source *s)
571 {
572 ssource *ss = (ssource *)s;
573
574 if (ss->o.conn || ss->o.opt != SOCKOPT_LIMIT) {
575 sel_rmfile(&ss->r);
576 close(ss->r.fd);
577 if (ss->a->ops->unbind)
578 ss->a->ops->unbind(ss->a);
579 }
580 if (ss->a->ops->freesrcopts)
581 ss->a->ops->freesrcopts(ss->ao);
582 else
583 DESTROY(ss->ao);
584 xfree(ss->s.desc);
585 ss->a->ops->destroy(ss->a);
586 ss->t->ops->destroy(ss->t);
587 source_remove(&ss->s);
588 DESTROY(ss);
589 fw_dec();
590 }
591
592 /* --- Source definition block --- */
593
594 source_ops ssource_ops = {
595 "socket",
596 ssource_option, ssource_read, ssource_attach, ssource_destroy
597 };
598
599 /*----- Target definition -------------------------------------------------*/
600
601 /* --- @options@ --- */
602
603 static int starget_option(target *t, scanner *sc)
604 {
605 starget *st = (starget *)t;
606
607 CONF_BEGIN(sc, "starget", "socket target")
608
609 /* --- Pass the option around the various address types --- */
610
611 if (st) {
612 if (st->a->ops->option && st->a->ops->option(sc, st->ao, ADDR_DEST))
613 CONF_ACCEPT;
614 }
615 /* We'd have done it already if it was global */
616
617 /* --- Done --- */
618
619 CONF_END;
620 }
621
622 /* --- @read@ --- */
623
624 static target *starget_read(scanner *sc)
625 {
626 starget *st;
627 dstr d = DSTR_INIT;
628
629 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
630 st = CREATE(starget);
631 st->t.ops = &starget_ops;
632 st->a = getaddr(sc, ADDR_DEST);
633 if (st->a->ops->inittargopts)
634 st->ao = st->a->ops->inittargopts();
635 else {
636 st->ao = CREATE(addr_opts);
637 *st->ao = gtao;
638 }
639 dstr_puts(&d, "socket.");
640 st->a->ops->print(st->a, ADDR_DEST, &d);
641 st->t.desc = xstrdup(d.buf);
642 dstr_destroy(&d);
643 return (&st->t);
644 }
645
646 /* --- @confirm@ --- */
647
648 static void starget_confirm(target *t)
649 {
650 starget *st = (starget *)t;
651
652 if (st->a->ops->confirm)
653 st->a->ops->confirm(st->a, ADDR_DEST, st->ao);
654 }
655
656 /* --- @create@ --- */
657
658 static endpt *starget_create(target *t, const char *desc)
659 {
660 starget *st = (starget *)t;
661 stept *e = CREATE(stept);
662
663 e->e.ops = &stept_ops;
664 e->e.other = 0;
665 e->e.f = EPF_FILE | EPF_PENDING;
666 e->e.t = 0;
667 e->desc = xstrdup(desc);
668 if (st->a->ops->connect(st->a, st->ao, &e->c, &e->e)) {
669 fw_log(-1, "[%s] couldn't connect: %s", e->desc, strerror(errno));
670 DESTROY(e);
671 return (0);
672 }
673 fw_inc();
674 return (&e->e);
675 }
676
677 /* --- @destroy@ --- */
678
679 static void starget_destroy(target *t)
680 {
681 starget *st = (starget *)t;
682 if (st->a->ops->freetargopts)
683 st->a->ops->freetargopts(st->ao);
684 else
685 DESTROY(st->ao);
686 st->a->ops->destroy(st->a);
687 xfree(st->t.desc);
688 DESTROY(st);
689 }
690
691 /* --- Socket target definition block --- */
692
693 target_ops starget_ops = {
694 "socket",
695 starget_option, starget_read, starget_confirm,
696 starget_create, starget_destroy
697 };
698
699 /*----- That's all, folks -------------------------------------------------*/