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