Commit | Line | Data |
---|---|---|
e82f7154 | 1 | /* -*-c-*- |
e82f7154 | 2 | * |
3 | * Main header file for port forwarder | |
4 | * | |
61e3dbdf | 5 | * (c) 1999 Straylight/Edgeware |
e82f7154 | 6 | */ |
7 | ||
206212ca | 8 | /*----- Licensing notice --------------------------------------------------* |
e82f7154 | 9 | * |
9155ea97 | 10 | * This file is part of the `fwd' port forwarder. |
e82f7154 | 11 | * |
9155ea97 | 12 | * `fwd' is free software; you can redistribute it and/or modify |
e82f7154 | 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, |
e82f7154 | 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 | * |
e82f7154 | 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, |
e82f7154 | 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ | |
26 | ||
e82f7154 | 27 | #ifndef FW_H |
28 | #define FW_H | |
29 | ||
30 | #ifdef __cplusplus | |
31 | extern "C" { | |
32 | #endif | |
33 | ||
34 | /*----- Header files ------------------------------------------------------*/ | |
35 | ||
47a7631f MW |
36 | /* --- Configuration --- */ |
37 | ||
38 | #include "config.h" | |
39 | #define _GNU_SOURCE | |
40 | ||
41 | /* --- ANSI C --- */ | |
42 | ||
43 | #include <assert.h> | |
44 | #include <ctype.h> | |
45 | #include <errno.h> | |
46 | #include <float.h> | |
47 | #include <limits.h> | |
48 | #include <math.h> | |
49 | #include <signal.h> | |
50 | #include <stdarg.h> | |
51 | #include <stddef.h> | |
52 | #include <stdio.h> | |
53 | #include <stdlib.h> | |
54 | #include <string.h> | |
61e3dbdf | 55 | #include <time.h> |
47a7631f MW |
56 | |
57 | /* --- Unix --- */ | |
58 | ||
61e3dbdf | 59 | #include <fcntl.h> |
47a7631f MW |
60 | #include <unistd.h> |
61 | ||
62 | #include <sys/types.h> | |
63 | #include <sys/stat.h> | |
64 | #include <sys/time.h> | |
65 | #include <sys/uio.h> | |
66 | #include <sys/wait.h> | |
67 | ||
68 | #include <sys/socket.h> | |
69 | #include <sys/un.h> | |
70 | #include <netinet/in.h> | |
71 | #include <arpa/inet.h> | |
72 | #include <netdb.h> | |
73 | ||
74 | #include <pwd.h> | |
75 | #include <grp.h> | |
76 | ||
77 | #include <syslog.h> | |
78 | ||
79 | /* --- mLib --- */ | |
80 | ||
81 | #include <mLib/alloc.h> | |
82 | #include <mLib/bres.h> | |
83 | #include <mLib/conn.h> | |
84 | #include <mLib/darray.h> | |
85 | #include <mLib/dstr.h> | |
86 | #include <mLib/env.h> | |
87 | #include <mLib/fdflags.h> | |
88 | #include <mLib/fdpass.h> | |
89 | #include <mLib/ident.h> | |
aae68c41 | 90 | #include <mLib/macros.h> |
76654703 | 91 | #include <mLib/mdup.h> |
47a7631f MW |
92 | #include <mLib/mdwopt.h> |
93 | #include <mLib/quis.h> | |
94 | #include <mLib/report.h> | |
e82f7154 | 95 | #include <mLib/sel.h> |
47a7631f MW |
96 | #include <mLib/selbuf.h> |
97 | #include <mLib/sig.h> | |
98 | #include <mLib/str.h> | |
99 | #include <mLib/sub.h> | |
100 | #include <mLib/sym.h> | |
101 | #include <mLib/tv.h> | |
102 | ||
103 | /*----- Other subtleties --------------------------------------------------*/ | |
104 | ||
105 | #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON | |
106 | extern char **environ; | |
107 | #endif | |
108 | ||
1cd4a3bf MW |
109 | /*----- Resource limit names ----------------------------------------------*/ |
110 | ||
111 | #if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE) | |
112 | # define RLIMIT_NOFILE RLIMIT_OFILE | |
113 | #endif | |
114 | ||
115 | /* | |
116 | ;;; The resource-limit name table is very boring to type and less fun to | |
117 | ;;; maintain. To make life less awful, put the names in this list and | |
118 | ;;; evaluate the code to get Emacs to regenerate it. | |
119 | ||
120 | (let ((limits '(as core cpu data fsize locks memlock msgqueue | |
121 | nice nofile nproc rss rtprio sigpending stack | |
122 | vmem))) | |
123 | (save-excursion | |
124 | (goto-char | |
125 | (point-min)) | |
126 | (search-forward (concat "***" "BEGIN rlimitlist" "***")) | |
127 | (beginning-of-line 2) | |
128 | (delete-region (point) | |
129 | (progn | |
130 | (search-forward "***END***") | |
131 | (beginning-of-line) | |
132 | (point))) | |
133 | (let ((avail (make-marker)) | |
134 | (list (make-marker))) | |
135 | (set-marker avail (point)) | |
136 | (insert "#define RLIMITS(_)") | |
137 | (set-marker list (point)) | |
138 | (dolist (limit (sort (copy-list limits) #'string<)) | |
139 | (let* ((name (symbol-name limit)) | |
140 | (constant (concat "RLIMIT_" (upcase name))) | |
141 | (have (concat "HAVE_" constant "_P"))) | |
142 | (goto-char avail) | |
143 | (insert-before-markers (format (concat "#ifdef %s\n" | |
144 | "# define %s t\n" | |
145 | "#else\n" | |
146 | "# define %s nil\n" | |
147 | "#endif\n") | |
148 | constant have have)) | |
149 | (goto-char list) | |
150 | (insert-before-markers | |
151 | (format " \\\n MAYBE_ITEM(_, %s, (%s, %s))" | |
152 | have name constant)))) | |
153 | (goto-char list) | |
154 | (insert "\n")))) | |
155 | */ | |
156 | ||
157 | /***BEGIN rlimitlist***/ | |
158 | #ifdef RLIMIT_AS | |
159 | # define HAVE_RLIMIT_AS_P t | |
160 | #else | |
161 | # define HAVE_RLIMIT_AS_P nil | |
162 | #endif | |
163 | #ifdef RLIMIT_CORE | |
164 | # define HAVE_RLIMIT_CORE_P t | |
165 | #else | |
166 | # define HAVE_RLIMIT_CORE_P nil | |
167 | #endif | |
168 | #ifdef RLIMIT_CPU | |
169 | # define HAVE_RLIMIT_CPU_P t | |
170 | #else | |
171 | # define HAVE_RLIMIT_CPU_P nil | |
172 | #endif | |
173 | #ifdef RLIMIT_DATA | |
174 | # define HAVE_RLIMIT_DATA_P t | |
175 | #else | |
176 | # define HAVE_RLIMIT_DATA_P nil | |
177 | #endif | |
178 | #ifdef RLIMIT_FSIZE | |
179 | # define HAVE_RLIMIT_FSIZE_P t | |
180 | #else | |
181 | # define HAVE_RLIMIT_FSIZE_P nil | |
182 | #endif | |
183 | #ifdef RLIMIT_LOCKS | |
184 | # define HAVE_RLIMIT_LOCKS_P t | |
185 | #else | |
186 | # define HAVE_RLIMIT_LOCKS_P nil | |
187 | #endif | |
188 | #ifdef RLIMIT_MEMLOCK | |
189 | # define HAVE_RLIMIT_MEMLOCK_P t | |
190 | #else | |
191 | # define HAVE_RLIMIT_MEMLOCK_P nil | |
192 | #endif | |
193 | #ifdef RLIMIT_MSGQUEUE | |
194 | # define HAVE_RLIMIT_MSGQUEUE_P t | |
195 | #else | |
196 | # define HAVE_RLIMIT_MSGQUEUE_P nil | |
197 | #endif | |
198 | #ifdef RLIMIT_NICE | |
199 | # define HAVE_RLIMIT_NICE_P t | |
200 | #else | |
201 | # define HAVE_RLIMIT_NICE_P nil | |
202 | #endif | |
203 | #ifdef RLIMIT_NOFILE | |
204 | # define HAVE_RLIMIT_NOFILE_P t | |
205 | #else | |
206 | # define HAVE_RLIMIT_NOFILE_P nil | |
207 | #endif | |
208 | #ifdef RLIMIT_NPROC | |
209 | # define HAVE_RLIMIT_NPROC_P t | |
210 | #else | |
211 | # define HAVE_RLIMIT_NPROC_P nil | |
212 | #endif | |
213 | #ifdef RLIMIT_RSS | |
214 | # define HAVE_RLIMIT_RSS_P t | |
215 | #else | |
216 | # define HAVE_RLIMIT_RSS_P nil | |
217 | #endif | |
218 | #ifdef RLIMIT_RTPRIO | |
219 | # define HAVE_RLIMIT_RTPRIO_P t | |
220 | #else | |
221 | # define HAVE_RLIMIT_RTPRIO_P nil | |
222 | #endif | |
223 | #ifdef RLIMIT_SIGPENDING | |
224 | # define HAVE_RLIMIT_SIGPENDING_P t | |
225 | #else | |
226 | # define HAVE_RLIMIT_SIGPENDING_P nil | |
227 | #endif | |
228 | #ifdef RLIMIT_STACK | |
229 | # define HAVE_RLIMIT_STACK_P t | |
230 | #else | |
231 | # define HAVE_RLIMIT_STACK_P nil | |
232 | #endif | |
233 | #ifdef RLIMIT_VMEM | |
234 | # define HAVE_RLIMIT_VMEM_P t | |
235 | #else | |
236 | # define HAVE_RLIMIT_VMEM_P nil | |
237 | #endif | |
238 | #define RLIMITS(_) \ | |
239 | MAYBE_ITEM(_, HAVE_RLIMIT_AS_P, (as, RLIMIT_AS)) \ | |
240 | MAYBE_ITEM(_, HAVE_RLIMIT_CORE_P, (core, RLIMIT_CORE)) \ | |
241 | MAYBE_ITEM(_, HAVE_RLIMIT_CPU_P, (cpu, RLIMIT_CPU)) \ | |
242 | MAYBE_ITEM(_, HAVE_RLIMIT_DATA_P, (data, RLIMIT_DATA)) \ | |
243 | MAYBE_ITEM(_, HAVE_RLIMIT_FSIZE_P, (fsize, RLIMIT_FSIZE)) \ | |
244 | MAYBE_ITEM(_, HAVE_RLIMIT_LOCKS_P, (locks, RLIMIT_LOCKS)) \ | |
245 | MAYBE_ITEM(_, HAVE_RLIMIT_MEMLOCK_P, (memlock, RLIMIT_MEMLOCK)) \ | |
246 | MAYBE_ITEM(_, HAVE_RLIMIT_MSGQUEUE_P, (msgqueue, RLIMIT_MSGQUEUE)) \ | |
247 | MAYBE_ITEM(_, HAVE_RLIMIT_NICE_P, (nice, RLIMIT_NICE)) \ | |
248 | MAYBE_ITEM(_, HAVE_RLIMIT_NOFILE_P, (nofile, RLIMIT_NOFILE)) \ | |
249 | MAYBE_ITEM(_, HAVE_RLIMIT_NPROC_P, (nproc, RLIMIT_NPROC)) \ | |
250 | MAYBE_ITEM(_, HAVE_RLIMIT_RSS_P, (rss, RLIMIT_RSS)) \ | |
251 | MAYBE_ITEM(_, HAVE_RLIMIT_RTPRIO_P, (rtprio, RLIMIT_RTPRIO)) \ | |
252 | MAYBE_ITEM(_, HAVE_RLIMIT_SIGPENDING_P, (sigpending, RLIMIT_SIGPENDING)) \ | |
253 | MAYBE_ITEM(_, HAVE_RLIMIT_STACK_P, (stack, RLIMIT_STACK)) \ | |
254 | MAYBE_ITEM(_, HAVE_RLIMIT_VMEM_P, (vmem, RLIMIT_VMEM)) | |
255 | /***END***/ | |
256 | ||
257 | /* --- The unpleasant conditional-output machinery --- */ | |
258 | ||
259 | #define MAYBE_ITEM(_, emitp, args) GLUE(MAYBE_ITEM_, emitp)(_, args) | |
260 | #define MAYBE_ITEM_t(_, args) _ args | |
261 | #define MAYBE_ITEM_nil(_, args) | |
262 | ||
47a7631f | 263 | /*----- Main program ------------------------------------------------------*/ |
e82f7154 | 264 | |
47a7631f | 265 | /* --- The global select state --- */ |
e82f7154 | 266 | |
267 | extern sel_state *sel; | |
4fe31371 | 268 | |
47a7631f MW |
269 | /* --- Help text --- */ |
270 | ||
271 | extern const char grammar_text[]; | |
272 | extern const char option_text[]; | |
61e3dbdf | 273 | |
d935f68b MW |
274 | /* --- Generally useful magic constants --- */ |
275 | ||
276 | #define NOW ((time_t)-1) | |
277 | ||
61e3dbdf | 278 | /* --- @fw_log@ --- * |
279 | * | |
d935f68b | 280 | * Arguments: @time_t t@ = when the connection occurred or (@NOW@) |
61e3dbdf | 281 | * @const char *fmt@ = format string to fill in |
282 | * @...@ = other arguments | |
283 | * | |
284 | * Returns: --- | |
285 | * | |
286 | * Use: Logs a connection. | |
287 | */ | |
288 | ||
aae68c41 MW |
289 | extern void PRINTF_LIKE(2, 3) |
290 | fw_log(time_t /*t*/, const char */*fmt*/, ...); | |
61e3dbdf | 291 | |
292 | /* --- @fw_inc@, @fw_dec@ --- * | |
293 | * | |
294 | * Arguments: --- | |
295 | * | |
296 | * Returns: --- | |
297 | * | |
9155ea97 | 298 | * Use: Increments or decrements the active thing count. `fwd' won't |
61e3dbdf | 299 | * quit while there are active things. |
300 | */ | |
301 | ||
302 | extern void fw_inc(void); | |
303 | extern void fw_dec(void); | |
e82f7154 | 304 | |
47a7631f MW |
305 | /*----- Channel management ------------------------------------------------*/ |
306 | ||
307 | /* --- Magic numbers --- */ | |
308 | ||
309 | #define CHAN_BUFSZ 4096 | |
310 | ||
311 | /* --- Channel structure --- */ | |
312 | ||
313 | typedef struct chan { | |
314 | unsigned base, len; /* Base and length of data */ | |
315 | unsigned f; /* Various interesting flags */ | |
316 | void (*func)(void */*p*/); /* Function to call on closure */ | |
86c9bc4b | 317 | int err; /* What's wrong with the channel */ |
47a7631f MW |
318 | void *p; /* Argument to pass function */ |
319 | sel_file r, w; /* Reader and writer selectors */ | |
320 | char buf[CHAN_BUFSZ]; /* The actual data buffer */ | |
321 | } chan; | |
322 | ||
323 | #define CHANF_CLOSE 1u /* Close channel when buffer empty */ | |
324 | #define CHANF_READY 2u /* The channel destination exists */ | |
325 | ||
326 | /* --- @chan_close@ --- * | |
327 | * | |
328 | * Arguments: @chan *c@ = pointer to channel | |
329 | * | |
330 | * Returns: --- | |
331 | * | |
332 | * Use: Closes down a channel prematurely. | |
333 | */ | |
334 | ||
335 | extern void chan_close(chan */*c*/); | |
336 | ||
337 | /* --- @chan_dest@ --- * | |
338 | * | |
339 | * Arguments: @chan *c@ = pointer to channel | |
340 | * @int fd@ = destination file descriptor for channel | |
341 | * | |
342 | * Returns: --- | |
343 | * | |
344 | * Use: Sets the channel's destination so it knows where to put | |
345 | * data. | |
346 | */ | |
347 | ||
348 | extern void chan_dest(chan */*c*/, int /*fd*/); | |
349 | ||
350 | /* --- @chan_open@ --- * | |
351 | * | |
352 | * Arguments: @chan *c@ = pointer to channel to open | |
353 | * @int from, to@ = source and destination file descriptors | |
354 | * @void (*func)(void *p)@ = function to call on closure | |
355 | * @void *p@ = argument to pass to function | |
356 | * | |
357 | * Returns: --- | |
358 | * | |
359 | * Use: Opens a channel. Data is copied from the source to the | |
360 | * destination. The @to@ argument may be @-1@ if the file | |
361 | * descriptor isn't known yet. | |
362 | */ | |
363 | ||
364 | extern void chan_open(chan */*c*/, int /*from*/, int /*to*/, | |
365 | void (*/*func*/)(void */*p*/), void */*p*/); | |
366 | ||
367 | /*----- Character scanners ------------------------------------------------*/ | |
368 | ||
369 | /* --- A low-level scanner source --- */ | |
370 | ||
371 | typedef struct scansrc { | |
372 | struct scansrc *next; /* Next one in the list */ | |
373 | struct scansrc_ops *ops; /* Pointer to operations table */ | |
374 | char *src; /* Name of this source */ | |
375 | int line; /* Current line number */ | |
376 | dstr pushback; /* Pushback characters */ | |
377 | char *tok; /* Token pushback */ | |
378 | unsigned t; /* Token type pushback */ | |
379 | } scansrc; | |
380 | ||
381 | /* --- Scanner source operations --- */ | |
382 | ||
383 | typedef struct scansrc_ops { | |
384 | int (*scan)(scansrc */*ss*/); /* Read another character */ | |
385 | void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */ | |
386 | } scansrc_ops; | |
387 | ||
388 | /* --- A character scanner --- */ | |
389 | ||
390 | typedef struct scanner { | |
391 | scansrc *head, **tail; /* Scanner list head and tail */ | |
392 | int t; /* Token type */ | |
393 | dstr d; /* Current token value */ | |
394 | const char *wbegin, *wcont; /* Parsing exception strings */ | |
395 | } scanner; | |
396 | ||
397 | /* --- @scan_file@ --- * | |
398 | * | |
399 | * Arguments: @FILE *fp@ = pointer to file descriptor | |
400 | * @const char *name@ = pointer to source file name | |
401 | * @unsigned f@ = flags | |
402 | * | |
403 | * Returns: A scanner source. | |
404 | * | |
405 | * Use: Creates a new scanner source for reading from a file. | |
406 | */ | |
407 | ||
408 | #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */ | |
409 | ||
410 | extern scansrc *scan_file(FILE */*fp*/, const char */*name*/, | |
411 | unsigned /*f*/); | |
412 | ||
413 | /* --- @scan_argv@ --- * | |
414 | * | |
415 | * Arguments: @char **av@ = pointer to argument array (null terminated) | |
416 | * | |
417 | * Returns: A scanner source. | |
418 | * | |
419 | * Use: Creates a new scanner source for reading from an @argv@ | |
420 | * array. | |
421 | */ | |
422 | ||
423 | extern scansrc *scan_argv(char **/*av*/); | |
424 | ||
425 | /* --- @scan@ --- * | |
426 | * | |
427 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
428 | * | |
429 | * Returns: Character read, or end-of-file. | |
430 | * | |
431 | * Use: Scans a character from a source of characters. | |
432 | */ | |
433 | ||
434 | extern int scan(scanner */*sc*/); | |
435 | ||
436 | /* --- @unscan@ --- * | |
437 | * | |
438 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
439 | * @int ch@ = character to unscan | |
440 | * | |
441 | * Returns: --- | |
442 | * | |
443 | * Use: Scans a character from a source of characters. | |
444 | */ | |
445 | ||
446 | extern void unscan(scanner */*sc*/, int /*ch*/); | |
447 | ||
448 | /* --- @scan_push@ --- * | |
449 | * | |
450 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
451 | * @scansrc *ss@ = souorce to push | |
452 | * | |
453 | * Returns: --- | |
454 | * | |
455 | * Use: Pushes a scanner source onto the front of the queue. | |
456 | */ | |
457 | ||
458 | extern void scan_push(scanner */*sc*/, scansrc */*ss*/); | |
459 | ||
460 | /* --- @scan_add@ --- * | |
461 | * | |
462 | * Arguments: @scanner *sc@ = pointer to main scanner context | |
463 | * @scansrc *ss@ = souorce to push | |
464 | * | |
465 | * Returns: --- | |
466 | * | |
467 | * Use: Adds a scanner source onto the end of the queue. | |
468 | */ | |
469 | ||
470 | extern void scan_add(scanner */*sc*/, scansrc */*ss*/); | |
471 | ||
472 | /* --- @scan_create@ --- * | |
473 | * | |
474 | * Arguments: @scanner *sc@ = scanner context to initialize | |
475 | * | |
476 | * Returns: --- | |
477 | * | |
478 | * Use: Initializes a scanner block ready for use. | |
479 | */ | |
480 | ||
481 | extern void scan_create(scanner */*sc*/); | |
482 | ||
483 | /* --- @scan_destroy@ --- * | |
484 | * | |
485 | * Arguments: @scanner *sc@ = pointer to scanner context | |
486 | * | |
487 | * Returns: --- | |
488 | * | |
489 | * Use: Destroys a scanner and all the sources attached to it. | |
490 | */ | |
491 | ||
492 | extern void scan_destroy(scanner */*sc*/); | |
493 | ||
494 | /*----- Configuration parsing ---------------------------------------------*/ | |
495 | ||
496 | /* --- Magical constants --- */ | |
497 | ||
498 | #define CTOK_EOF (-1) | |
499 | #define CTOK_WORD 256 | |
500 | ||
501 | /* --- @conf_undelim@ --- * | |
502 | * | |
503 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
504 | * @const char *d, *dd@ = pointer to characters to escape | |
505 | * | |
506 | * Returns: --- | |
507 | * | |
508 | * Use: Modifies the tokenizer. Characters in the first list will | |
509 | * always be considered to begin a word. Characters in the | |
510 | * second list will always be allowed to continue a word. | |
511 | */ | |
512 | ||
513 | extern void conf_undelim(scanner */*sc*/, | |
514 | const char */*d*/, const char */*dd*/); | |
515 | ||
516 | /* --- @token@ --- * | |
517 | * | |
518 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
519 | * | |
520 | * Returns: Type of token scanned. | |
521 | * | |
522 | * Use: Reads the next token from the character scanner. | |
523 | */ | |
524 | ||
525 | extern int token(scanner */*sc*/); | |
526 | ||
527 | /* --- @error@ --- * | |
528 | * | |
529 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
530 | * @const char *msg@ = message skeleton string | |
531 | * @...@ = extra arguments for the skeleton | |
532 | * | |
533 | * Returns: Doesn't | |
534 | * | |
535 | * Use: Reports an error at the current scanner location. | |
536 | */ | |
537 | ||
aae68c41 MW |
538 | extern void PRINTF_LIKE(2, 3) NORETURN |
539 | error(scanner */*sc*/, const char */*msg*/, ...); | |
47a7631f MW |
540 | |
541 | /* --- @pushback@ --- * | |
542 | * | |
543 | * Arguments: @scanner *sc@ = pointer to scanner definition | |
544 | * | |
545 | * Returns: --- | |
546 | * | |
547 | * Use: Pushes the current token back. This is normally a precursor | |
548 | * to pushing a new scanner source. | |
549 | */ | |
550 | ||
551 | extern void pushback(scanner */*sc*/); | |
552 | ||
553 | /* --- @conf_enum@ --- * | |
554 | * | |
555 | * Arguments: @scanner *sc@ = pointer to a scanner object | |
556 | * @const char *list@ = comma-separated things to allow | |
557 | * @unsigned @f = flags for the search | |
558 | * @const char *err@ = error message if not found | |
559 | * | |
560 | * Returns: Index into list, zero-based, or @-1@. | |
561 | * | |
562 | * Use: Checks whether the current token is a string which matches | |
563 | * one of the comma-separated items given. The return value is | |
564 | * the index (zero-based) of the matched string in the list. | |
565 | * | |
566 | * The flags control the behaviour if no exact match is found. | |
567 | * If @ENUM_ABBREV@ is set, and the current token is a left | |
568 | * substring of exactly one of the possibilities, then that one | |
569 | * is chosen. If @ENUM_NONE@ is set, the value @-1@ is | |
570 | * returned; otherwise an error is reported and the program is | |
571 | * terminated. | |
572 | */ | |
573 | ||
574 | #define ENUM_ABBREV 1u | |
575 | #define ENUM_NONE 2u | |
576 | ||
577 | extern int conf_enum(scanner */*sc*/, const char */*list*/, | |
578 | unsigned /*flags*/, const char */*err*/); | |
579 | ||
580 | /* --- @conf_prefix@ --- * | |
581 | * | |
582 | * Arguments: @scanner *sc@ = pointer to a scanner object | |
583 | * @const char *p@ = pointer to prefix string to check | |
584 | * | |
585 | * Returns: Nonzero if the prefix matches. | |
586 | * | |
587 | * Use: If the current token is a word matching the given prefix | |
588 | * string, then it and an optional `.' character are removed and | |
589 | * a nonzero result is returned. Otherwise the current token is | |
590 | * left as it is, and zero is returned. | |
591 | * | |
592 | * Typical options parsing code would remove an expected prefix, | |
593 | * scan an option anyway (since qualifying prefixes are | |
594 | * optional) and if a match is found, claim the option. If no | |
595 | * match is found, and a prefix was stripped, then an error | |
596 | * should be reported. | |
597 | */ | |
598 | ||
599 | extern int conf_prefix(scanner */*sc*/, const char */*p*/); | |
600 | ||
601 | /* --- @CONF_BEGIN@, @CONF_END@ --- * | |
602 | * | |
603 | * Arguments: @sc@ = scanner to read from | |
604 | * @prefix@ = prefix to scan for | |
605 | * @desc@ = description of what we're parsing | |
606 | * | |
607 | * Use: Bracket an options parsing routine. The current token is | |
608 | * checked to see whether it matches the prefix. If so, it is | |
609 | * removed and the following token examined. If that's a `.' | |
610 | * then it's removed. If it's a `{' then the enclosed | |
611 | * option-parsing code is executed in a loop until a matching | |
612 | * '}' is found. If the options parser doesn't accept an | |
613 | * option, the behaviour is dependent on whether a prefix was | |
614 | * seen: if so, an error is reported; otherwse a zero return is | |
615 | * made. | |
616 | */ | |
617 | ||
618 | #define CS_PLAIN 0 | |
619 | #define CS_PREFIX 1 | |
620 | #define CS_BRACE 2 | |
621 | #define CS_UNKNOWN 3 | |
622 | ||
623 | #define CONF_BEGIN(sc, prefix, desc) do { \ | |
624 | scanner *_conf_sc = (sc); \ | |
625 | const char *_conf_desc = (desc); \ | |
626 | int _conf_state = CS_PLAIN; \ | |
627 | \ | |
628 | /* --- Read the initial prefix --- */ \ | |
629 | \ | |
630 | if (_conf_sc->t == CTOK_WORD && \ | |
631 | strcmp(_conf_sc->d.buf, (prefix)) == 0) { \ | |
632 | token(_conf_sc); \ | |
633 | _conf_state = CS_PREFIX; \ | |
634 | if (_conf_sc->t == '.') \ | |
635 | token(_conf_sc); \ | |
636 | else if (_conf_sc->t == '{') { \ | |
637 | token(_conf_sc); \ | |
638 | _conf_state = CS_BRACE; \ | |
639 | } \ | |
640 | } \ | |
641 | \ | |
642 | /* --- Ensure the next token is a word --- */ \ | |
643 | \ | |
644 | if (_conf_sc->t != CTOK_WORD) \ | |
645 | error(_conf_sc, "parse error, expected option keyword"); \ | |
646 | do { | |
647 | ||
648 | #define CONF_END \ | |
649 | \ | |
650 | /* --- Reject an option --- * \ | |
651 | * \ | |
652 | * We could get here as a result of an explicit @CONF_REJECT@ or \ | |
653 | * because the option wasn't accepted. \ | |
654 | */ \ | |
655 | \ | |
656 | goto _conf_reject; \ | |
657 | _conf_reject: \ | |
658 | if (_conf_state == CS_PLAIN) \ | |
659 | _conf_state = CS_UNKNOWN; \ | |
660 | else { \ | |
661 | error(_conf_sc, "unknown %s option `%s'", \ | |
662 | _conf_desc, _conf_sc->d.buf); \ | |
663 | } \ | |
664 | \ | |
665 | /* --- Accept an option --- * \ | |
666 | * \ | |
667 | * It's safe to drop through from above. Either an error will have \ | |
668 | * been reported, or the state is not @CS_BRACE@. \ | |
669 | */ \ | |
670 | \ | |
671 | _conf_accept: \ | |
672 | if (_conf_state == CS_BRACE && _conf_sc->t == ';') \ | |
673 | token(_conf_sc); \ | |
674 | } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \ | |
675 | \ | |
676 | /* --- Check for a closing brace --- */ \ | |
677 | \ | |
678 | if (_conf_state == CS_BRACE) { \ | |
679 | if (_conf_sc->t == '}') \ | |
680 | token(_conf_sc); \ | |
681 | else \ | |
682 | error(_conf_sc, "parse error, expected `}'"); \ | |
683 | } \ | |
684 | \ | |
685 | /* --- Return an appropriate value --- */ \ | |
686 | \ | |
687 | return (_conf_state != CS_UNKNOWN); \ | |
688 | } while (0) | |
689 | ||
690 | /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- * | |
691 | * | |
692 | * Arguments: --- | |
693 | * | |
694 | * Use: Within an options parser (between @CONF_BEGIN@ and | |
695 | * @CONF_END@), accept or reject an option. | |
696 | */ | |
697 | ||
698 | #define CONF_ACCEPT goto _conf_accept | |
699 | #define CONF_REJECT goto _conf_reject | |
700 | ||
701 | /* --- @CONF_QUAL@ --- * | |
702 | * | |
703 | * Arguments: --- | |
704 | * | |
705 | * Use: Evaluates to a nonzero value if the current option is | |
706 | * qualified. This can be used to decide whether abbreviations | |
707 | * for options should be accepted. | |
708 | */ | |
709 | ||
710 | #define CONF_QUAL (_conf_state != CS_PLAIN) | |
711 | ||
712 | /* --- @conf_name@ --- * | |
713 | * | |
714 | * Arguments: @scanner *sc@ = pointer to scanner | |
715 | * @char delim@ = delimiter character to look for | |
716 | * @dstr *d@ = pointer to dynamic string for output | |
717 | * | |
718 | * Returns: --- | |
719 | * | |
720 | * Use: Reads in a compound name consisting of words separated by | |
721 | * delimiters. Leading and trailing delimiters are permitted, | |
722 | * although they'll probably cause confusion if used. The name | |
723 | * may be enclosed in square brackets if that helps at all. | |
724 | * | |
725 | * Examples of compound names are filenames (delimited by `/') | |
726 | * and IP addresses (delimited by `.'). | |
727 | */ | |
728 | ||
729 | extern void conf_name(scanner */*sc*/, char /*delim*/, dstr */*d*/); | |
730 | ||
35a142ca MW |
731 | /* --- @conf_fname@ --- * |
732 | * | |
733 | * Arguments: @scanner *sc@ = pointer to scanner | |
734 | * @dstr *d@ = pointer to dynamic string for output | |
735 | * | |
736 | * Returns: --- | |
737 | * | |
738 | * Use: Reads a file name from the input and stores it in @d@. | |
739 | */ | |
740 | ||
741 | extern void conf_fname(scanner */*sc*/, dstr */*d*/); | |
742 | ||
47a7631f MW |
743 | /*----- Reference-counted file descriptors --------------------------------*/ |
744 | ||
745 | typedef struct reffd { | |
746 | int fd; | |
747 | unsigned ref; | |
748 | void (*proc)(void */*p*/); | |
749 | void *p; | |
750 | } reffd; | |
751 | ||
752 | /* --- @reffd_init@ --- * | |
753 | * | |
754 | * Arguments: @int fd@ = file descriptor | |
755 | * | |
756 | * Returns: Reference-counted file descriptor object. | |
757 | * | |
758 | * Use: Creates a refcounted file descriptor. | |
759 | */ | |
760 | ||
761 | extern reffd *reffd_init(int /*fd*/); | |
762 | ||
763 | /* --- @reffd_handler@ --- * | |
764 | * | |
765 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
766 | * @void (*proc)(void *p)@ = procedure to call | |
767 | * @void *p@ | |
768 | * | |
769 | * Returns: --- | |
770 | * | |
771 | * Use: Sets the reference counted file descriptor to call @proc@ | |
772 | * when it is no longer required. | |
773 | */ | |
774 | ||
775 | extern void reffd_handler(reffd */*r*/, void (*/*proc*/)(void */*p*/), | |
776 | void */*p*/); | |
777 | ||
778 | /* --- @reffd_inc@ --- * | |
779 | * | |
780 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
781 | * | |
782 | * Returns: --- | |
783 | * | |
784 | * Use: Increments the reference count for a file descriptor. | |
785 | */ | |
786 | ||
787 | #define REFFD_INC(r) do { (r)->ref++; } while (0) | |
788 | ||
789 | extern void reffd_inc(reffd */*r*/); | |
790 | ||
791 | /* --- @reffd_dec@ --- * | |
792 | * | |
793 | * Arguments: @reffd *r@ = pointer to reference counted filehandle | |
794 | * | |
795 | * Returns: --- | |
796 | * | |
797 | * Use: Decrements the reference count for a file descriptor. | |
798 | */ | |
799 | ||
800 | #define REFFD_DEC(r) do { \ | |
801 | reffd *_r = (r); \ | |
802 | _r->ref--; \ | |
803 | if (_r->ref == 0) { \ | |
804 | close(_r->fd); \ | |
805 | if (_r->proc) \ | |
806 | _r->proc(_r->p); \ | |
807 | DESTROY(_r); \ | |
808 | } \ | |
809 | } while (0) | |
810 | ||
811 | extern void reffd_dec(reffd */*r*/); | |
812 | ||
813 | /*----- Sources, targets and endpoints ------------------------------------*/ | |
814 | ||
815 | /* --- Basic endpoint structure --- */ | |
816 | ||
817 | typedef struct endpt { | |
818 | struct endpt_ops *ops; /* Pointer to operations table */ | |
819 | struct endpt *other; /* Pointer to sibling endpoint */ | |
820 | unsigned f; /* Various flags */ | |
821 | struct tango *t; /* Private data structure */ | |
822 | reffd *in, *out; /* File descriptors */ | |
823 | } endpt; | |
824 | ||
825 | /* --- Endpoint flags --- */ | |
826 | ||
827 | #define EPF_PENDING 1u /* Endpoint creation in progress */ | |
828 | #define EPF_FILE 2u /* Endpoint smells like a file */ | |
829 | ||
830 | /* --- Endpoint operations table --- */ | |
831 | ||
832 | typedef struct endpt_ops { | |
833 | ||
834 | /* --- @attach@ --- * | |
835 | * | |
836 | * Arguments: @endpt *e@ = pointer to endpoint to be attached | |
837 | * @reffd *in, *out@ = input and output file descriptors | |
838 | * | |
839 | * Returns: --- | |
840 | * | |
841 | * Use: Instructs a non-file endpoint to attach itself to a pair of | |
842 | * files. | |
843 | */ | |
844 | ||
845 | void (*attach)(endpt */*e*/, reffd */*in*/, reffd */*out*/); | |
846 | ||
847 | /* --- @file@ --- * | |
848 | * | |
849 | * Arguments: @endpt *e@ = pointer to endpoint in question | |
850 | * @endpt *f@ = pointer to a file endpoint | |
851 | * | |
852 | * Returns: --- | |
853 | * | |
854 | * Use: Informs a non-file endpoint of a file endpoint which will | |
855 | * want to be closed when it's finished with. At that time, the | |
856 | * endpoint should arrange to have both itself and its partner | |
857 | * closed. If no file is registered, the endpoint manager will | |
858 | * close both endpoints itself. | |
859 | */ | |
860 | ||
861 | void (*file)(endpt */*e*/, endpt */*f*/); | |
862 | ||
863 | /* --- @wclose@ --- * | |
864 | * | |
865 | * Arguments: @endpt *e@ = endpoint to be partially closed | |
866 | * | |
867 | * Returns: --- | |
868 | * | |
869 | * Use: Announces that the endpoint will not be written to any more. | |
870 | */ | |
871 | ||
872 | void (*wclose)(endpt */*e*/); | |
873 | ||
874 | /* --- @close@ --- * | |
875 | * | |
876 | * Arguments: @endpt *e@ = endpoint to be closed | |
877 | * | |
878 | * Returns: --- | |
879 | * | |
880 | * Use: Completely closes an endpoint. The endpoint's data may be | |
881 | * freed, although some endpoints may wish to delay freeing for | |
882 | * some reason. | |
883 | */ | |
884 | ||
885 | void (*close)(endpt */*e*/); | |
886 | ||
887 | } endpt_ops; | |
888 | ||
889 | /* --- A basic target object --- */ | |
890 | ||
891 | typedef struct target { | |
892 | struct target_ops *ops; | |
893 | char *desc; | |
894 | } target; | |
895 | ||
896 | /* --- Forwarding target operations --- */ | |
897 | ||
898 | typedef struct target_ops { | |
899 | const char *name; /* Name of this target */ | |
900 | ||
901 | /* --- @option@ --- * | |
902 | * | |
903 | * Arguments: @target *t@ = pointer to target object, or zero if global | |
904 | * @scanner *sc@ = scanner to read from | |
905 | * | |
906 | * Returns: Nonzero to claim the option. | |
907 | * | |
908 | * Use: Handles an option string from the configuration file. | |
909 | */ | |
910 | ||
911 | int (*option)(target */*t*/, scanner */*sc*/); | |
912 | ||
913 | /* --- @read@ --- * | |
914 | * | |
915 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
916 | * | |
917 | * Returns: Pointer to a target object to claim, null to reject. | |
918 | * | |
919 | * Use: Parses a target description from the configuration file. | |
920 | * Only the socket target is allowed to omit the prefix on a | |
921 | * target specification. | |
922 | */ | |
923 | ||
924 | target *(*read)(scanner */*sc*/); | |
925 | ||
926 | /* --- @confirm@ --- * | |
927 | * | |
928 | * Arguments: @target *t@ = pointer to target | |
929 | * | |
930 | * Returns: --- | |
931 | * | |
932 | * Use: Confirms configuration of a target. | |
933 | */ | |
934 | ||
935 | void (*confirm)(target */*t*/); | |
936 | ||
937 | /* --- @create@ --- * | |
938 | * | |
939 | * Arguments: @target *t@ = pointer to target | |
940 | * @const char *desc@ = description of connection | |
941 | * | |
942 | * Returns: Pointer to a created endpoint. | |
943 | * | |
944 | * Use: Generates a target endpoint for communication. | |
945 | */ | |
946 | ||
947 | endpt *(*create)(target */*t*/, const char */*desc*/); | |
948 | ||
949 | /* --- @destroy@ --- * | |
950 | * | |
951 | * Arguments: @target *t@ = pointer to target | |
952 | * | |
953 | * Returns: --- | |
954 | * | |
955 | * Use: Destroys a target. | |
956 | */ | |
957 | ||
958 | void (*destroy)(target */*t*/); | |
959 | ||
960 | } target_ops; | |
961 | ||
962 | /* --- A basic source object --- */ | |
963 | ||
964 | typedef struct source { | |
965 | struct source *next, *prev; | |
966 | struct source_ops *ops; | |
967 | char *desc; | |
968 | } source; | |
969 | ||
970 | /* --- Forwarding source operations --- */ | |
971 | ||
972 | typedef struct source_ops { | |
973 | const char *name; /* Name of this source */ | |
974 | ||
975 | /* --- @option@ --- * | |
976 | * | |
977 | * Arguments: @scanner *sc@ = scanner to read from | |
978 | * @source *s@ = pointer to source object, or zero if global | |
979 | * | |
980 | * Returns: Nonzero to claim the option. | |
981 | * | |
982 | * Use: Handles an option string from the configuration file. | |
983 | */ | |
984 | ||
985 | int (*option)(source */*s*/, scanner */*sc*/); | |
986 | ||
987 | /* --- @read@ --- * | |
988 | * | |
989 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
990 | * | |
991 | * Returns: Pointer to a source object to claim, null to reject. | |
992 | * | |
993 | * Use: Parses a source description from the configuration file. | |
994 | * Only the socket source is allowed to omit the prefix on a | |
995 | * source specification. | |
996 | */ | |
997 | ||
998 | source *(*read)(scanner */*sc*/); | |
999 | ||
1000 | /* --- @attach@ --- * | |
1001 | * | |
1002 | * Arguments: @source *s@ = pointer to source | |
1003 | * @scanner *sc@ = scanner (for error reporting) | |
1004 | * @target *t@ = pointer to target to attach | |
1005 | * | |
1006 | * Returns: --- | |
1007 | * | |
1008 | * Use: Attaches a target to a source. | |
1009 | */ | |
1010 | ||
1011 | void (*attach)(source */*s*/, scanner */*sc*/, target */*t*/); | |
1012 | ||
1013 | /* --- @destroy@ --- * | |
1014 | * | |
1015 | * Arguments: @source *s@ = pointer to source | |
1016 | * | |
1017 | * Returns: --- | |
1018 | * | |
1019 | * Use: Destroys a source. Used when closing the system down, for | |
1020 | * example as a result of a signal. | |
1021 | */ | |
1022 | ||
1023 | void (*destroy)(source */*s*/); | |
1024 | ||
1025 | } source_ops; | |
1026 | ||
1027 | /* --- @endpt_kill@ --- * | |
1028 | * | |
1029 | * Arguments: @endpt *a@ = an endpoint | |
1030 | * | |
1031 | * Returns: --- | |
1032 | * | |
1033 | * Use: Kills an endpoint. If the endpoint is joined to another, the | |
1034 | * other endpoint is also killed, as is the connection between | |
1035 | * them (and that's the tricky bit). | |
1036 | */ | |
1037 | ||
1038 | extern void endpt_kill(endpt */*a*/); | |
1039 | ||
1040 | /* --- @endpt_killall@ --- * | |
1041 | * | |
1042 | * Arguments: --- | |
1043 | * | |
1044 | * Returns: --- | |
1045 | * | |
1046 | * Use: Destroys all current endpoint connections. Used when | |
1047 | * shutting down. | |
1048 | */ | |
1049 | ||
1050 | extern void endpt_killall(void); | |
1051 | ||
1052 | /* --- @endpt_join@ --- * | |
1053 | * | |
1054 | * Arguments: @endpt *a@ = pointer to first endpoint | |
1055 | * @endpt *b@ = pointer to second endpoint | |
86c9bc4b | 1056 | * @const char *desc@ = description of connection |
47a7631f MW |
1057 | * |
1058 | * Returns: --- | |
1059 | * | |
86c9bc4b MW |
1060 | * Use: Joins two endpoints together. It's OK to join endpoints |
1061 | * which are already joined; in fact, the the right thing to do | |
1062 | * when your endpoint decides that it's not pending any more is | |
1063 | * to join it to its partner again. | |
1064 | * | |
1065 | * If the endpoints are already connected then the description | |
1066 | * string is ignored. The endpoint manager takes a copy of | |
1067 | * the string, so you don't need to keep it around. | |
47a7631f MW |
1068 | */ |
1069 | ||
86c9bc4b | 1070 | extern void endpt_join(endpt */*a*/, endpt */*b*/, const char */*desc*/); |
47a7631f MW |
1071 | |
1072 | /* --- @source_add@ --- * | |
1073 | * | |
1074 | * Arguments: @source *s@ = pointer to a source | |
1075 | * | |
1076 | * Returns: --- | |
1077 | * | |
1078 | * Use: Adds a source to the master list. Only do this for passive | |
1079 | * sources (e.g., listening sockets), not active sources (e.g., | |
1080 | * executable programs). | |
1081 | */ | |
1082 | ||
1083 | extern void source_add(source */*s*/); | |
1084 | ||
1085 | /* --- @source_remove@ --- * | |
1086 | * | |
1087 | * Arguments: @source *s@ = pointer to a source | |
1088 | * | |
1089 | * Returns: --- | |
1090 | * | |
1091 | * Use: Removes a source from the master list. | |
1092 | */ | |
1093 | ||
1094 | extern void source_remove(source */*s*/); | |
1095 | ||
1096 | /* --- @source_killall@ --- * | |
1097 | * | |
1098 | * Arguments: --- | |
1099 | * | |
1100 | * Returns: --- | |
1101 | * | |
1102 | * Use: Frees all sources. | |
1103 | */ | |
1104 | ||
1105 | extern void source_killall(void); | |
1106 | ||
1107 | /*----- The exec source and target ----------------------------------------*/ | |
1108 | ||
1109 | extern source_ops xsource_ops; | |
1110 | extern target_ops xtarget_ops; | |
1111 | ||
1112 | /* --- @exec_init@ --- * | |
1113 | * | |
1114 | * Arguments: --- | |
1115 | * | |
1116 | * Returns: --- | |
1117 | * | |
1118 | * Use: Initializes the executable problem source and target. | |
1119 | */ | |
1120 | ||
1121 | extern void exec_init(void); | |
1122 | ||
1123 | /*----- The file source and target ----------------------------------------*/ | |
1124 | ||
1125 | extern source_ops fsource_ops; | |
1126 | extern target_ops ftarget_ops; | |
1127 | ||
1128 | /*----- The socket source and target --------------------------------------*/ | |
1129 | ||
1130 | extern source_ops ssource_ops; | |
1131 | extern target_ops starget_ops; | |
1132 | ||
1133 | /* --- @starget_connected@ --- * | |
1134 | * | |
1135 | * Arguments: @int fd@ = file descriptor now ready for use | |
1136 | * @void *p@ = pointer to an endpoint structure | |
1137 | * | |
1138 | * Returns: --- | |
1139 | * | |
1140 | * Use: Handles successful connection of the target endpoint. | |
1141 | */ | |
1142 | ||
1143 | extern void starget_connected(int /*fd*/, void */*p*/); | |
1144 | ||
1145 | /*----- Handling of file attributes ---------------------------------------*/ | |
1146 | ||
1147 | /* --- File attribute options structure --- */ | |
1148 | ||
1149 | typedef struct fattr { | |
1150 | unsigned mode; | |
1151 | uid_t uid; | |
1152 | gid_t gid; | |
1153 | } fattr; | |
1154 | ||
1155 | /* --- Shared global options --- */ | |
1156 | ||
1157 | extern fattr fattr_global; | |
1158 | ||
1159 | /* --- @fattr_init@ --- * | |
1160 | * | |
1161 | * Arguments: @fattr *f@ = pointer to file attributes | |
1162 | * | |
1163 | * Returns: --- | |
1164 | * | |
1165 | * Use: Initializes a set of file attributes to default values. | |
1166 | */ | |
1167 | ||
1168 | extern void fattr_init(fattr */*f*/); | |
1169 | ||
1170 | /* --- @fattr_option@ --- * | |
1171 | * | |
1172 | * Arguments: @scanner *sc@ = pointer to scanner to read | |
1173 | * @fattr *f@ = pointer to file attributes to set | |
1174 | * | |
1175 | * Returns: Whether the option was clamed. | |
1176 | * | |
1177 | * Use: Reads file attributes from a scanner. | |
1178 | */ | |
1179 | ||
1180 | extern int fattr_option(scanner */*sc*/, fattr */*f*/); | |
1181 | ||
1182 | /* --- @fattr_apply@ --- * | |
1183 | * | |
1184 | * Arguments: @const char *file@ = pointer to filename | |
1185 | * @fattr *f@ = pointer to attribute set | |
1186 | * | |
1187 | * Returns: @-1@ if it failed. | |
1188 | * | |
1189 | * Use: Applies file attributes to a file. For best results, try to | |
1190 | * create the file with the right permissions and so on. This | |
1191 | * call will fix everything up, but there are potential races | |
1192 | * which might catch you out if you're not careful. | |
1193 | */ | |
1194 | ||
1195 | extern int fattr_apply(const char */*file*/, fattr */*f*/); | |
1196 | ||
1197 | /*----- Making privileged connections -------------------------------------*/ | |
1198 | ||
1199 | /* --- @privconn_split@ --- * | |
1200 | * | |
1201 | * Arguments: @sel_state *s@ = select state | |
1202 | * | |
1203 | * Returns: --- | |
1204 | * | |
1205 | * Use: Splits off the privileged binding code into a separate | |
1206 | * process. | |
1207 | */ | |
1208 | ||
1209 | extern void privconn_split(sel_state */*s*/); | |
1210 | ||
1211 | /* --- @privconn_adddest@ --- * | |
1212 | * | |
1213 | * Arguments: @struct in_addr peer@ = address to connect to | |
1214 | * @unsigned port@ = port to connect to | |
1215 | * | |
1216 | * Returns: Index for this destination address, or @-1@ if not | |
1217 | * available. | |
1218 | * | |
1219 | * Use: Adds a valid destination for a privileged connection. | |
1220 | */ | |
1221 | ||
1222 | extern int privconn_adddest(struct in_addr /*peer*/, unsigned /*port*/); | |
1223 | ||
1224 | /* --- @privconn_connect@ --- * | |
1225 | * | |
1226 | * Arguments: @conn *c@ = connection structure to fill in | |
1227 | * @sel_state *s@ = pointer to select state to attach to | |
1228 | * @int i@ = address index to connect to | |
1229 | * @struct in_addr bind@ = address to bind to | |
1230 | * @void (*func)(int, void *)@ = function to call on connect | |
1231 | * @void *p@ = argument for the function | |
1232 | * | |
1233 | * Returns: Zero on success, @-1@ on failure. | |
1234 | * | |
1235 | * Use: Sets up a privileged connection job. | |
1236 | */ | |
1237 | ||
1238 | extern int privconn_connect(conn */*c*/, sel_state */*s*/, | |
1239 | int /*i*/, struct in_addr /*bind*/, | |
1240 | void (*/*func*/)(int, void *), void */*p*/); | |
1241 | ||
1242 | /*----- Identifying remote clients ----------------------------------------*/ | |
1243 | ||
1244 | typedef struct id_req { | |
1245 | struct sockaddr_in lsin; /* Local address of connection */ | |
1246 | struct sockaddr_in rsin; /* Remote address of connection */ | |
1247 | const char *desc; /* Description of connection */ | |
1248 | const char *act; /* Action taken by server */ | |
1249 | reffd *r; /* Pointer to file descriptor */ | |
1250 | } id_req; | |
1251 | ||
1252 | /* --- @identify@ --- * | |
1253 | * | |
1254 | * Arguments: @const id_req *q@ = pointer to request block | |
1255 | * | |
1256 | * Returns: --- | |
1257 | * | |
1258 | * Use: Starts a background ident lookup and reverse-resolve job | |
1259 | * which will, eventually, report a message to the system log. | |
1260 | */ | |
1261 | ||
1262 | extern void identify(const id_req */*q*/); | |
1263 | ||
1264 | /*----- Host-based access control -----------------------------------------*/ | |
1265 | ||
1266 | /* --- An access control entry --- */ | |
1267 | ||
1268 | typedef struct acl_entry { | |
1269 | struct acl_entry *next; /* Next entry in the list */ | |
1270 | const struct acl_ops *ops; /* Operations for the ACL entry */ | |
1271 | unsigned act; /* What to do with matching hosts */ | |
1272 | } acl_entry; | |
1273 | ||
1274 | #define ACL_DENY 0 /* Deny access to matching conns */ | |
1275 | #define ACL_ALLOW 1 /* Allow access to matching conns */ | |
1276 | #define ACL_PERM 1u /* Bit mask for permission bit */ | |
1277 | ||
1278 | /* --- Host-based access control --- */ | |
1279 | ||
1280 | typedef struct acl_host { | |
1281 | acl_entry a; /* Base structure */ | |
1282 | struct in_addr addr, mask; /* Address and netmask */ | |
1283 | } acl_host; | |
1284 | ||
1285 | /* --- ACL methods --- */ | |
1286 | ||
1287 | typedef struct acl_ops { | |
1288 | int (*check)(void */*a*/, struct in_addr /*addr*/, unsigned /*port*/); | |
1289 | void (*dump)(void */*a*/, FILE */*fp*/); | |
1290 | void (*free)(void */*a*/); | |
1291 | } acl_ops; | |
1292 | ||
1293 | /* --- @acl_check@ --- * | |
1294 | * | |
1295 | * Arguments: @acl_entry *a@ = pointer to ACL to check against | |
1296 | * @struct in_addr addr@ = address to check | |
1297 | * @unsigned port@ = port number to check | |
1298 | * @int *act@ = verdict (should initially be @ACT_ALLOW@) | |
1299 | * | |
1300 | * Returns: Zero if undecided, nonzero if a rule matched. | |
1301 | * | |
1302 | * Use: Checks an address against an ACL. | |
1303 | */ | |
1304 | ||
1305 | extern int acl_check(acl_entry */*a*/, | |
1306 | struct in_addr /*addr*/, unsigned /*port*/, | |
1307 | int */*act*/); | |
1308 | ||
1309 | /* --- @acl_dump@ --- * | |
1310 | * | |
1311 | * Arguments: @acl_entry *a@ = pointer to ACL to dump | |
1312 | * @FILE *fp@ = pointer to stream to dump on | |
1313 | * | |
1314 | * Returns: --- | |
1315 | * | |
1316 | * Use: Dumps an access control list to an output stream. | |
1317 | */ | |
1318 | ||
1319 | extern void acl_dump(acl_entry */*a*/, FILE */*fp*/); | |
1320 | ||
1321 | /* --- @acl_free@ --- * | |
1322 | * | |
1323 | * Arguments: @acl_entry *a@ = pointer to a list of ACLs | |
1324 | * | |
1325 | * Returns: --- | |
1326 | * | |
1327 | * Use: Frees all of the memory used by an ACL. | |
1328 | */ | |
1329 | ||
1330 | extern void acl_free(acl_entry */*a*/); | |
1331 | ||
1332 | /* --- @acl_addhost@ --- * | |
1333 | * | |
1334 | * Arguments: @acl_entry ***a@ = address of pointer to list tail | |
1335 | * @unsigned act@ = what to do with matching addresses | |
1336 | * @struct in_addr addr, mask@ = address and mask to match | |
1337 | * | |
1338 | * Returns: --- | |
1339 | * | |
1340 | * Use: Adds a host-authentication entry to the end of an access | |
1341 | * control list. | |
1342 | */ | |
1343 | ||
1344 | extern void acl_addhost(acl_entry ***/*a*/, unsigned /*act*/, | |
1345 | struct in_addr /*addr*/, struct in_addr /*mask*/); | |
1346 | ||
1347 | /* --- @acl_addpriv@ --- * | |
1348 | * | |
1349 | * Arguments: @acl_entry ***a@ = address of pointer to list tail | |
1350 | * @unsigned act@ = what to do with matching addresses | |
1351 | * | |
1352 | * Returns: --- | |
1353 | * | |
1354 | * Use: Adds a privileged-port check to the end of an access control | |
1355 | * list. | |
1356 | */ | |
1357 | ||
1358 | extern void acl_addpriv(acl_entry ***/*a*/, unsigned /*act*/); | |
1359 | ||
1360 | /*----- Network addresses -------------------------------------------------*/ | |
1361 | ||
1362 | /* --- A generic socket address --- * | |
1363 | * | |
1364 | * Not all systems understand @sa_len@ fields. (In particular, Linux | |
1365 | * doesn't.) Some fairly ugly hacking is then performed on particular | |
1366 | * address types. | |
1367 | */ | |
1368 | ||
1369 | typedef struct addr { | |
1370 | struct addr_ops *ops; | |
1371 | size_t sz; | |
1372 | } addr; | |
1373 | ||
1374 | #define ADDRSZ(sz) (sizeof(addr) + (sz)) | |
1375 | ||
1376 | /* --- Address configuration --- * | |
1377 | * | |
1378 | * An address family will want to extend this. | |
1379 | */ | |
1380 | ||
1381 | typedef struct addr_opts { | |
1382 | unsigned f; | |
1383 | } addr_opts; | |
1384 | ||
1385 | #define ADDRF_NOLOG 1u | |
1386 | ||
1387 | /* --- Address types --- * | |
1388 | * | |
1389 | * For things like Internet addresses, source and destinations look | |
1390 | * different. | |
1391 | */ | |
1392 | ||
1393 | enum { | |
1394 | ADDR_SRC, | |
1395 | ADDR_DEST, | |
1396 | ADDR_GLOBAL | |
1397 | }; | |
1398 | ||
1399 | /* --- Description of an address type handler --- */ | |
1400 | ||
1401 | typedef struct addr_ops { | |
1402 | const char *name; /* Protocol's internal name */ | |
1403 | ||
1404 | /* --- @read@ --- * | |
1405 | * | |
1406 | * Arguments: @scanner *sc@ = pointer to scanner to read from | |
1407 | * @unsigned type@ = type of address to be read | |
1408 | * | |
1409 | * Returns: A filled-in socket address. | |
1410 | * | |
1411 | * Use: Parses a textual representation of a socket address. | |
1412 | */ | |
1413 | ||
1414 | addr *(*read)(scanner */*sc*/, unsigned /*type*/); | |
1415 | ||
1416 | /* --- @destroy@ --- * | |
1417 | * | |
1418 | * Arguments: @addr *a@ = pointer to an address block | |
1419 | * | |
1420 | * Returns: --- | |
1421 | * | |
1422 | * Use: Disposes of an address block in some suitable fashion. | |
1423 | */ | |
1424 | ||
1425 | void (*destroy)(addr */*a*/); | |
1426 | ||
1427 | /* --- @print@ --- * | |
1428 | * | |
1429 | * Arguments: @addr *a@ = pointer to socket address to read | |
1430 | * @unsigned type@ = type of address to be written | |
1431 | * @dstr *d@ = string on which to write the description | |
1432 | * | |
1433 | * Returns: --- | |
1434 | * | |
1435 | * Use: Writes a textual representation of a socket address to | |
1436 | * a string. | |
1437 | */ | |
1438 | ||
1439 | void (*print)(addr */*a*/, unsigned /*type*/, dstr */*d*/); | |
1440 | ||
1441 | /* --- @initsrcopts@ --- * | |
1442 | * | |
1443 | * Arguments: --- | |
1444 | * | |
1445 | * Returns: A pointer to a protocol-specific data block for a listener | |
1446 | * | |
1447 | * Use: Creates a data block for a listener. This is attached to the | |
1448 | * listener data structure. Options can then be requested, and | |
1449 | * are added to the block when necessary. | |
1450 | */ | |
1451 | ||
1452 | addr_opts *(*initsrcopts)(void); | |
1453 | ||
1454 | /* --- @option@ --- * | |
1455 | * | |
1456 | * Arguments: @scanner *sc@ = pointer to a scanner to read from | |
1457 | * @unsigned type@ = kind of option this is | |
1458 | * @addr_opts *ao@ = data block to modify (from @init@), or null | |
1459 | * | |
1460 | * Returns: Nonzero to claim the option. | |
1461 | * | |
1462 | * Use: Parses a source option, either global or listener-specific. | |
1463 | */ | |
1464 | ||
1465 | int (*option)(scanner */*sc*/, addr_opts */*ao*/, unsigned /*type*/); | |
1466 | ||
1467 | /* --- @confirm@ --- * | |
1468 | * | |
1469 | * Arguments: @addr *a@ = pointer to an address structure | |
1470 | * @unsigned type@ = kind of address this is | |
1471 | * @addr_opts *ao@ = address options | |
1472 | * | |
1473 | * Returns: --- | |
1474 | * | |
1475 | * Use: Called during initialization when an address is fully | |
1476 | * configured. | |
1477 | */ | |
1478 | ||
1479 | void (*confirm)(addr */*a*/, unsigned /*type*/, addr_opts */*ao*/); | |
1480 | ||
1481 | /* --- @freesrcopts@ --- * | |
1482 | * | |
1483 | * Arguments: @addr_opts *ao@ = data block to remove | |
1484 | * | |
1485 | * Returns: --- | |
1486 | * | |
1487 | * Use: Throws away all the configuration data for an address type. | |
1488 | */ | |
1489 | ||
1490 | void (*freesrcopts)(addr_opts */*ao*/); | |
1491 | ||
1492 | /* --- @bind@ --- * | |
1493 | * | |
1494 | * Arguments: @addr *a@ = the address to bind to | |
1495 | * @addr_opts *ao@ = the address options | |
1496 | * | |
1497 | * Returns: File descriptor of bound socket if OK, or @-1@ on error. | |
1498 | * | |
1499 | * Use: Binds a listening socket. The tedious stuff with @listen@ | |
1500 | * isn't necessary. | |
1501 | */ | |
1502 | ||
1503 | int (*bind)(addr */*a*/, addr_opts */*ao*/); | |
1504 | ||
1505 | /* --- @unbind@ --- * | |
1506 | * | |
1507 | * Arguments: @addr *a@ = pointer to an address | |
1508 | * | |
1509 | * Returns: --- | |
1510 | * | |
1511 | * Use: Unbinds an address. This is used when tidying up. The main | |
1512 | * purpose is to let the Unix-domain handler remove its socket | |
1513 | * node from the filesystem. | |
1514 | */ | |
1515 | ||
1516 | void (*unbind)(addr */*a*/); | |
1517 | ||
1518 | /* --- @accept@ --- * | |
1519 | * | |
1520 | * Arguments: @int fd@ = listening file descriptor | |
1521 | * @addr_opts *ao@ = data block to get configuration from | |
1522 | * @const char *desc@ = description of the listener | |
1523 | * | |
1524 | * Returns: Pointer to a reference counted file descriptor. | |
1525 | * | |
1526 | * Use: Accepts, verifies and logs an incoming connection. | |
1527 | */ | |
1528 | ||
1529 | reffd *(*accept)(int /*fd*/, addr_opts */*ao*/, const char */*desc*/); | |
1530 | ||
1531 | /* --- @inittargopts@ --- * | |
1532 | * | |
1533 | * Arguments: --- | |
1534 | * | |
1535 | * Returns: A pointer to a protocol-specific data block for a connecter | |
1536 | * | |
1537 | * Use: Creates a data block for a target. This is attached to the | |
1538 | * target data structure. Options can then be requested, and | |
1539 | * are added to the block when necessary. | |
1540 | */ | |
1541 | ||
1542 | addr_opts *(*inittargopts)(void); | |
1543 | ||
1544 | /* --- @freetargopts@ --- * | |
1545 | * | |
1546 | * Arguments: @addr_opts *ao@ = data block to remove | |
1547 | * | |
1548 | * Returns: --- | |
1549 | * | |
1550 | * Use: Throws away all the configuration data for an address type. | |
1551 | */ | |
1552 | ||
1553 | void (*freetargopts)(addr_opts */*ao*/); | |
1554 | ||
1555 | /* --- @connect@ --- * | |
1556 | * | |
1557 | * Arguments: @addr *a@ = destination address | |
1558 | * @addr_opts *ao@ = target address options | |
1559 | * @conn *c@ = connection structure | |
1560 | * @endpt *e@ = endpoint structure | |
1561 | * | |
1562 | * Returns: Zero if OK, @-1@ on some error. | |
1563 | * | |
1564 | * Use: Requests that a connection be made, or at least set in | |
1565 | * motion. An address may do one of these things: | |
1566 | * | |
1567 | * * Return @-1@. | |
1568 | * | |
1569 | * * Call @starget_connected@ with @-1@ or a connected file | |
1570 | * descriptor and the pointer @e@. | |
1571 | * | |
1572 | * * Call @conn_init@ or @conn_fd@, giving @starget_connected@ | |
1573 | * and @e@ as the function to call. | |
1574 | */ | |
1575 | ||
1576 | int (*connect)(addr */*a*/, addr_opts */*ao*/, conn */*c*/, endpt */*e*/); | |
1577 | ||
1578 | } addr_ops; | |
1579 | ||
1580 | /* --- Address types --- */ | |
1581 | ||
1582 | extern addr_ops un_ops; | |
1583 | extern addr_ops inet_ops; | |
1584 | ||
e82f7154 | 1585 | /*----- That's all, folks -------------------------------------------------*/ |
1586 | ||
1587 | #ifdef __cplusplus | |
1588 | } | |
1589 | #endif | |
1590 | ||
1591 | #endif |