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