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