| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: tripe.h,v 1.4 2001/02/05 19:56:37 mdw Exp $ |
| 4 | * |
| 5 | * Main header file for TrIPE |
| 6 | * |
| 7 | * (c) 2001 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Trivial IP Encryption (TrIPE). |
| 13 | * |
| 14 | * TrIPE is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * TrIPE is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: tripe.h,v $ |
| 32 | * Revision 1.4 2001/02/05 19:56:37 mdw |
| 33 | * Sequence number protection, and BSD tunnels. |
| 34 | * |
| 35 | * Revision 1.3 2001/02/04 01:17:55 mdw |
| 36 | * Create a configuration header file to tidy up command lines. |
| 37 | * |
| 38 | * Revision 1.2 2001/02/03 22:40:29 mdw |
| 39 | * Put timer information into the entropy pool when packets are received |
| 40 | * and on similar events. Reseed the generator on the interval timer. |
| 41 | * |
| 42 | * Revision 1.1 2001/02/03 20:26:37 mdw |
| 43 | * Initial checkin. |
| 44 | * |
| 45 | */ |
| 46 | |
| 47 | #ifndef TRIPE_H |
| 48 | #define TRIPE_H |
| 49 | |
| 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
| 54 | /*----- Header files ------------------------------------------------------*/ |
| 55 | |
| 56 | #include "config.h" |
| 57 | |
| 58 | #include <assert.h> |
| 59 | #include <ctype.h> |
| 60 | #include <errno.h> |
| 61 | #include <signal.h> |
| 62 | #include <stdarg.h> |
| 63 | #include <stddef.h> |
| 64 | #include <stdio.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <string.h> |
| 67 | #include <time.h> |
| 68 | |
| 69 | #include <sys/types.h> |
| 70 | #include <sys/time.h> |
| 71 | #include <unistd.h> |
| 72 | #include <fcntl.h> |
| 73 | #include <sys/stat.h> |
| 74 | |
| 75 | #include <sys/socket.h> |
| 76 | #include <sys/un.h> |
| 77 | #include <netinet/in.h> |
| 78 | #include <arpa/inet.h> |
| 79 | #include <netdb.h> |
| 80 | |
| 81 | #include <pwd.h> |
| 82 | #include <grp.h> |
| 83 | |
| 84 | #include <mLib/alloc.h> |
| 85 | #include <mLib/arena.h> |
| 86 | #include <mLib/bres.h> |
| 87 | #include <mLib/dstr.h> |
| 88 | #include <mLib/env.h> |
| 89 | #include <mLib/fdflags.h> |
| 90 | #include <mLib/fwatch.h> |
| 91 | #include <mLib/mdwopt.h> |
| 92 | #include <mLib/quis.h> |
| 93 | #include <mLib/report.h> |
| 94 | #include <mLib/sel.h> |
| 95 | #include <mLib/selbuf.h> |
| 96 | #include <mLib/sig.h> |
| 97 | #include <mLib/str.h> |
| 98 | #include <mLib/sub.h> |
| 99 | #include <mLib/trace.h> |
| 100 | |
| 101 | #include <catacomb/gcipher.h> |
| 102 | #include <catacomb/gmac.h> |
| 103 | #include <catacomb/grand.h> |
| 104 | #include <catacomb/key.h> |
| 105 | #include <catacomb/paranoia.h> |
| 106 | |
| 107 | #include <catacomb/blowfish.h> |
| 108 | #include <catacomb/blowfish-cbc.h> |
| 109 | #include <catacomb/noise.h> |
| 110 | #include <catacomb/rand.h> |
| 111 | #include <catacomb/rmd160.h> |
| 112 | #include <catacomb/rmd160-hmac.h> |
| 113 | |
| 114 | #include <catacomb/mp.h> |
| 115 | #include <catacomb/mpmont.h> |
| 116 | #include <catacomb/mprand.h> |
| 117 | #include <catacomb/dh.h> |
| 118 | |
| 119 | #include "util.h" |
| 120 | |
| 121 | #undef sun |
| 122 | |
| 123 | /*----- Magic numbers -----------------------------------------------------*/ |
| 124 | |
| 125 | /* --- Tunnel types --- */ |
| 126 | |
| 127 | #define TUN_NOTDEF 0 |
| 128 | #define TUN_UNET 1 |
| 129 | #define TUN_BSD 2 |
| 130 | |
| 131 | /* --- Trace flags --- */ |
| 132 | |
| 133 | #define T_TUNNEL 1u |
| 134 | #define T_PEER 2u |
| 135 | #define T_PACKET 4u |
| 136 | #define T_ADMIN 8u |
| 137 | #define T_CRYPTO 16u |
| 138 | #define T_KEYSET 32u |
| 139 | #define T_KEYEXCH 64u |
| 140 | #define T_KEYMGMT 128u |
| 141 | |
| 142 | #define T_ALL 255u |
| 143 | |
| 144 | /* --- Units --- */ |
| 145 | |
| 146 | #define SEC(n) (n##u) |
| 147 | #define MIN(n) (n##u * 60u) |
| 148 | #define MEG(n) (n##ul * 1024ul * 1024ul) |
| 149 | |
| 150 | /* --- Other things --- */ |
| 151 | |
| 152 | #define PKBUFSZ 65536 |
| 153 | |
| 154 | /*----- TrIPE protocol ----------------------------------------------------*/ |
| 155 | |
| 156 | /* --- TrIPE packet format --- * |
| 157 | * |
| 158 | * A packet begins with a single-byte packet type. The remaining data |
| 159 | * depends on the packet type. |
| 160 | */ |
| 161 | |
| 162 | #define MSG_PACKET 0u |
| 163 | /* Followed by a 64-bit MAC and an encrypted packet. The MAC is used as an |
| 164 | * IV for a 64-bit block cipher in CBC-stealing mode. |
| 165 | */ |
| 166 | |
| 167 | #define MSG_PRECHALLENGE 1u |
| 168 | /* Followed by the challenge only. Useful for bootstrapping the system. |
| 169 | */ |
| 170 | |
| 171 | #define MSG_CHALLENGE 2u |
| 172 | /* Followed by a response hash and a large-integer challenge. |
| 173 | */ |
| 174 | |
| 175 | #define MSG_RESPONSE 3u |
| 176 | /* Followed by a large-integer response. |
| 177 | */ |
| 178 | |
| 179 | /*----- Data structures ---------------------------------------------------*/ |
| 180 | |
| 181 | /* --- Buffers --- * |
| 182 | * |
| 183 | * Buffers provide a simple stream-like interface for building and parsing |
| 184 | * packets. |
| 185 | */ |
| 186 | |
| 187 | typedef struct buf { |
| 188 | octet *base, *p, *limit; /* Pointers to the buffer */ |
| 189 | unsigned f; /* Various flags */ |
| 190 | } buf; |
| 191 | |
| 192 | #define BF_BROKEN 1u /* Buffer is broken */ |
| 193 | |
| 194 | /* --- Socket addresses --- * |
| 195 | * |
| 196 | * A magic union of supported socket addresses. |
| 197 | */ |
| 198 | |
| 199 | typedef union addr { |
| 200 | struct sockaddr sa; |
| 201 | struct sockaddr_in sin; |
| 202 | } addr; |
| 203 | |
| 204 | /* --- A symmetric keyset --- * |
| 205 | * |
| 206 | * A keyset contains a set of symmetric keys for encrypting and decrypting |
| 207 | * packets. Keysets are stored in a list, sorted in reverse order of |
| 208 | * creation, so that the most recent keyset (the one most likely to be used) |
| 209 | * is first. |
| 210 | * |
| 211 | * Each keyset has a time limit and a data limit. The keyset is destroyed |
| 212 | * when either it has existed for too long, or it has been used to encrypt |
| 213 | * too much data. New key exchanges are triggered when keys are close to |
| 214 | * expiry. |
| 215 | */ |
| 216 | |
| 217 | typedef struct keyset { |
| 218 | struct keyset *next; /* Next active keyset in the list */ |
| 219 | time_t t_exp; /* Expiry time for this keyset */ |
| 220 | unsigned long sz_exp; /* Data limit for the keyset */ |
| 221 | #ifndef NTRACE |
| 222 | unsigned seq; /* Sequence number for tracing */ |
| 223 | #endif |
| 224 | gcipher *c; /* Keyset cipher for encryption */ |
| 225 | gmac *m; /* Keyset MAC for integrity */ |
| 226 | uint32 oseq; /* Outbound sequence number */ |
| 227 | uint32 iseq, iwin; /* Inbound sequence number */ |
| 228 | } keyset; |
| 229 | |
| 230 | #define KS_SEQWINSZ 32 /* Bits in sequence number window */ |
| 231 | |
| 232 | /* --- Key exchange --- * |
| 233 | * |
| 234 | * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers |
| 235 | * Protocol has a number of desirable features (e.g., perfect forward |
| 236 | * secrecy, and zero-knowledge authentication) which make it attractive for |
| 237 | * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and |
| 238 | * Clive Jones. |
| 239 | */ |
| 240 | |
| 241 | typedef struct keyexch { |
| 242 | keyset **ks; /* Peer's list of keysets */ |
| 243 | struct peer *p; /* Pointer back to the peer */ |
| 244 | unsigned f; /* Various useful flags */ |
| 245 | sel_timer t; /* Timer for next exchange */ |
| 246 | dh_pub kpub; /* Peer's public key */ |
| 247 | mp *my_x, *my_gx, *my_gxy; /* My half of the exchange */ |
| 248 | octet my_h[RMD160_HASHSZ]; /* My challenge hash */ |
| 249 | mp *your_gx, *your_gxy; /* Your half of the exchange */ |
| 250 | octet your_h[RMD160_HASHSZ]; /* Your challenge hash */ |
| 251 | time_t t_valid; /* When this exchange goes bad */ |
| 252 | time_t t_qchal, t_qresp; /* Quiet timers for packet types */ |
| 253 | time_t t_newchal; /* When to accept a new challenge */ |
| 254 | } keyexch; |
| 255 | |
| 256 | #define KXF_TIMER 1u /* Waiting for a timer to go off */ |
| 257 | #define KXF_INIT 2u /* Big numbers are initialized */ |
| 258 | #define KXF_MYH 4u /* My hash has been computed */ |
| 259 | #define KXF_YOURH 8u /* Your hash has been received */ |
| 260 | #define KXF_REPLY 16u /* Received your response OK */ |
| 261 | #define KXF_DONE 32u /* Key exchange completed */ |
| 262 | |
| 263 | /* --- Tunnel structure --- * |
| 264 | * |
| 265 | * Used to maintain system-specific information about the tunnel interface. |
| 266 | */ |
| 267 | |
| 268 | typedef struct tunnel { |
| 269 | #if TUN_TYPE == TUN_UNET |
| 270 | sel_file f; /* Selector for Usernet device */ |
| 271 | struct peer *p; /* Pointer to my peer */ |
| 272 | #elif TUN_TYPE == TUN_BSD |
| 273 | sel_file f; /* Selector for tunnel device */ |
| 274 | struct peer *p; /* Pointer to my peer */ |
| 275 | unsigned n; /* Number of my tunnel device */ |
| 276 | #else |
| 277 | # error "No support for this tunnel type" |
| 278 | #endif |
| 279 | } tunnel; |
| 280 | |
| 281 | /* --- Peer structure --- * |
| 282 | * |
| 283 | * The main structure which glues everything else together. |
| 284 | */ |
| 285 | |
| 286 | typedef struct peer { |
| 287 | struct peer *next, *prev; /* Links to next and previous */ |
| 288 | char *name; /* Name of this peer */ |
| 289 | tunnel t; /* Tunnel for local packets */ |
| 290 | keyset *ks; /* List head for keysets */ |
| 291 | keyexch kx; /* Key exchange protocol block */ |
| 292 | buf b; /* Buffer for sending packets */ |
| 293 | addr peer; /* Peer socket address */ |
| 294 | size_t sasz; /* Socket address size */ |
| 295 | } peer; |
| 296 | |
| 297 | /* --- Admin structure --- */ |
| 298 | |
| 299 | typedef struct admin { |
| 300 | struct admin *next, *prev; /* Links to next and previous */ |
| 301 | selbuf b; /* Line buffer for commands */ |
| 302 | int fd; /* File descriptor for output */ |
| 303 | #ifndef NTRACE |
| 304 | unsigned seq; /* Sequence number for tracing */ |
| 305 | #endif |
| 306 | char *pname; /* Peer name to create */ |
| 307 | char *paddr; /* Address string to resolve */ |
| 308 | bres_client r; /* Background resolver task */ |
| 309 | sel_timer t; /* Timer for resolver */ |
| 310 | addr peer; /* Address to set */ |
| 311 | size_t sasz; /* Size of the address */ |
| 312 | } admin; |
| 313 | |
| 314 | /*----- Global variables --------------------------------------------------*/ |
| 315 | |
| 316 | extern sel_state sel; /* Global I/O event state */ |
| 317 | extern dh_priv kpriv; /* Our private key */ |
| 318 | extern mpmont mg; /* Montgomery context for DH group */ |
| 319 | extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */ |
| 320 | |
| 321 | #ifndef NTRACE |
| 322 | extern const trace_opt tr_opts[]; /* Trace options array */ |
| 323 | extern unsigned tr_flags; /* Trace options flags */ |
| 324 | #endif |
| 325 | |
| 326 | /*----- Other macros ------------------------------------------------------*/ |
| 327 | |
| 328 | #define TIMER noise_timer(RAND_GLOBAL) |
| 329 | |
| 330 | /*----- Key management ----------------------------------------------------*/ |
| 331 | |
| 332 | /* --- @km_interval@ --- * |
| 333 | * |
| 334 | * Arguments: --- |
| 335 | * |
| 336 | * Returns: Zero if OK, nonzero to force reloading of keys. |
| 337 | * |
| 338 | * Use: Called on the interval timer to perform various useful jobs. |
| 339 | */ |
| 340 | |
| 341 | extern int km_interval(void); |
| 342 | |
| 343 | /* --- @km_init@ --- * |
| 344 | * |
| 345 | * Arguments: @const char *kr_priv@ = private keyring file |
| 346 | * @const char *kr_pub@ = public keyring file |
| 347 | * @const char *tag@ = tag to load |
| 348 | * |
| 349 | * Returns: --- |
| 350 | * |
| 351 | * Use: Initializes, and loads the private key. |
| 352 | */ |
| 353 | |
| 354 | extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/, |
| 355 | const char */*tag*/); |
| 356 | |
| 357 | /* --- @km_getpubkey@ --- * |
| 358 | * |
| 359 | * Arguments: @const char *tag@ = public key tag to load |
| 360 | * @dh_pub *kpub@ = where to put the public key |
| 361 | * |
| 362 | * Returns: Zero if OK, nonzero if it failed. |
| 363 | * |
| 364 | * Use: Fetches a public key from the keyring. |
| 365 | */ |
| 366 | |
| 367 | extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/); |
| 368 | |
| 369 | /*----- Key exchange ------------------------------------------------------*/ |
| 370 | |
| 371 | /* --- @kx_start@ --- * |
| 372 | * |
| 373 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 374 | * |
| 375 | * Returns: --- |
| 376 | * |
| 377 | * Use: Stimulates a key exchange. If a key exchage is in progress, |
| 378 | * a new challenge is sent (unless the quiet timer forbids |
| 379 | * this); if no exchange is in progress, one is commenced. |
| 380 | */ |
| 381 | |
| 382 | extern void kx_start(keyexch */*kx*/); |
| 383 | |
| 384 | /* --- @kx_prechallenge@ --- * |
| 385 | * |
| 386 | * Arguments: @keyexch *kx@ = pointer to key exhange context |
| 387 | * @buf *b@ = pointer to buffer containing the packet |
| 388 | * |
| 389 | * Returns: --- |
| 390 | * |
| 391 | * Use: Reads a prechallenge packet from the buffer and handles it. |
| 392 | */ |
| 393 | |
| 394 | extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/); |
| 395 | |
| 396 | /* --- @kx_challenge@ --- * |
| 397 | * |
| 398 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 399 | * @buf *b@ = a buffer containing the packet to read |
| 400 | * |
| 401 | * Returns: --- |
| 402 | * |
| 403 | * Use: Reads a challenge from the buffer and handles it. |
| 404 | */ |
| 405 | |
| 406 | extern void kx_challenge(keyexch */*kx*/, buf */*b*/); |
| 407 | |
| 408 | /* --- @kx_response@ --- * |
| 409 | * |
| 410 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 411 | * @buf *b@ = a buffer containing the packet to read |
| 412 | * |
| 413 | * Returns: --- |
| 414 | * |
| 415 | * Use: Reads a response from the buffer and handles it. |
| 416 | */ |
| 417 | |
| 418 | extern void kx_response(keyexch */*kx*/, buf */*b*/); |
| 419 | |
| 420 | /* --- @kx_free@ --- * |
| 421 | * |
| 422 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 423 | * |
| 424 | * Returns: --- |
| 425 | * |
| 426 | * Use: Frees everything in a key exchange context. |
| 427 | */ |
| 428 | |
| 429 | extern void kx_free(keyexch */*kx*/); |
| 430 | |
| 431 | /* --- @kx_newkeys@ --- * |
| 432 | * |
| 433 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 434 | * |
| 435 | * Returns: --- |
| 436 | * |
| 437 | * Use: Informs the key exchange module that its keys may have |
| 438 | * changed. If fetching the new keys fails, the peer will be |
| 439 | * destroyed, we log messages and struggle along with the old |
| 440 | * keys. |
| 441 | */ |
| 442 | |
| 443 | extern void kx_newkeys(keyexch */*kx*/); |
| 444 | |
| 445 | /* --- @kx_init@ --- * |
| 446 | * |
| 447 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 448 | * @peer *p@ = pointer to peer context |
| 449 | * @keyset **ks@ = pointer to keyset list |
| 450 | * |
| 451 | * Returns: Zero if OK, nonzero if it failed. |
| 452 | * |
| 453 | * Use: Initializes a key exchange module. The module currently |
| 454 | * contains no keys, and will attempt to initiate a key |
| 455 | * exchange. |
| 456 | */ |
| 457 | |
| 458 | extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/); |
| 459 | |
| 460 | /*----- Keysets and symmetric cryptography --------------------------------*/ |
| 461 | |
| 462 | /* --- @ks_free@ --- * |
| 463 | * |
| 464 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 465 | * |
| 466 | * Returns: --- |
| 467 | * |
| 468 | * Use: Frees all of the keys in a keyset. |
| 469 | */ |
| 470 | |
| 471 | extern void ks_free(keyset **/*ksroot*/); |
| 472 | |
| 473 | /* --- @ks_prune@ --- * |
| 474 | * |
| 475 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 476 | * |
| 477 | * Returns: --- |
| 478 | * |
| 479 | * Use: Prunes the keyset list by removing keys which mustn't be used |
| 480 | * any more. |
| 481 | */ |
| 482 | |
| 483 | extern void ks_prune(keyset **/*ksroot*/); |
| 484 | |
| 485 | /* --- @ks_gen@ --- * |
| 486 | * |
| 487 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 488 | * @const void *k@ = pointer to key material |
| 489 | * @size_t sz@ = size of the key material |
| 490 | * |
| 491 | * Returns: The regeneration time for the new key. |
| 492 | * |
| 493 | * Use: Derives a keyset from the given key material and adds it to |
| 494 | * the list. |
| 495 | */ |
| 496 | |
| 497 | extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/); |
| 498 | |
| 499 | /* --- @ks_encrypt@ --- * |
| 500 | * |
| 501 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 502 | * @buf *b@ = pointer to input buffer |
| 503 | * @buf *bb@ = pointer to output buffer |
| 504 | * |
| 505 | * Returns: Nonzero if a new key is needed. |
| 506 | * |
| 507 | * Use: Encrypts a packet. |
| 508 | */ |
| 509 | |
| 510 | extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/); |
| 511 | |
| 512 | /* --- @ks_decrypt@ --- * |
| 513 | * |
| 514 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 515 | * @buf *b@ = pointer to input buffer |
| 516 | * @buf *bb@ = pointer to output buffer |
| 517 | * |
| 518 | * Returns: Nonzero if the packet couldn't be decrypted. |
| 519 | * |
| 520 | * Use: Decrypts a packet. |
| 521 | */ |
| 522 | |
| 523 | extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/); |
| 524 | |
| 525 | /*----- Administration interface ------------------------------------------*/ |
| 526 | |
| 527 | /* --- @a_warn@ --- * |
| 528 | * |
| 529 | * Arguments: @const char *fmt@ = pointer to format string |
| 530 | * @...@ = other arguments |
| 531 | * |
| 532 | * Returns: --- |
| 533 | * |
| 534 | * Use: Informs all admin connections of a warning. |
| 535 | */ |
| 536 | |
| 537 | extern void a_warn(const char */*fmt*/, ...); |
| 538 | |
| 539 | /* --- @a_create@ --- * |
| 540 | * |
| 541 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
| 542 | * |
| 543 | * Returns: --- |
| 544 | * |
| 545 | * Use: Creates a new admin connection. |
| 546 | */ |
| 547 | |
| 548 | extern void a_create(int /*fd_in*/, int /*fd_out*/); |
| 549 | |
| 550 | /* --- @a_quit@ --- * |
| 551 | * |
| 552 | * Arguments: --- |
| 553 | * |
| 554 | * Returns: --- |
| 555 | * |
| 556 | * Use: Shuts things down nicely. |
| 557 | */ |
| 558 | |
| 559 | extern void a_quit(void); |
| 560 | |
| 561 | /* --- @a_daemon@ --- * |
| 562 | * |
| 563 | * Arguments: --- |
| 564 | * |
| 565 | * Returns: --- |
| 566 | * |
| 567 | * Use: Informs the admin module that it's a daemon. |
| 568 | */ |
| 569 | |
| 570 | extern void a_daemon(void); |
| 571 | |
| 572 | /* --- @a_init@ --- * |
| 573 | * |
| 574 | * Arguments: @const char *sock@ = socket name to create |
| 575 | * |
| 576 | * Returns: --- |
| 577 | * |
| 578 | * Use: Creates the admin listening socket. |
| 579 | */ |
| 580 | |
| 581 | extern void a_init(const char */*sock*/); |
| 582 | |
| 583 | /*----- Peer management ---------------------------------------------------*/ |
| 584 | |
| 585 | /* --- @p_txstart@ --- * |
| 586 | * |
| 587 | * Arguments: @peer *p@ = pointer to peer block |
| 588 | * @unsigned msg@ = message type code |
| 589 | * |
| 590 | * Returns: A pointer to a buffer to write to. |
| 591 | * |
| 592 | * Use: Starts sending to a peer. Only one send can happen at a |
| 593 | * time. |
| 594 | */ |
| 595 | |
| 596 | extern buf *p_txstart(peer */*p*/, unsigned /*msg*/); |
| 597 | |
| 598 | /* --- @p_txend@ --- * |
| 599 | * |
| 600 | * Arguments: @peer *p@ = pointer to peer block |
| 601 | * |
| 602 | * Returns: --- |
| 603 | * |
| 604 | * Use: Sends a packet to the peer. |
| 605 | */ |
| 606 | |
| 607 | extern void p_txend(peer */*p*/); |
| 608 | |
| 609 | /* --- @p_tun@ --- * |
| 610 | * |
| 611 | * Arguments: @peer *p@ = pointer to peer block |
| 612 | * @buf *b@ = buffer containing incoming packet |
| 613 | * |
| 614 | * Returns: --- |
| 615 | * |
| 616 | * Use: Handles a packet which needs to be sent to a peer. |
| 617 | */ |
| 618 | |
| 619 | extern void p_tun(peer */*p*/, buf */*b*/); |
| 620 | |
| 621 | /* --- @p_interval@ --- * |
| 622 | * |
| 623 | * Arguments: --- |
| 624 | * |
| 625 | * Returns: --- |
| 626 | * |
| 627 | * Use: Called periodically to do tidying. |
| 628 | */ |
| 629 | |
| 630 | extern void p_interval(void); |
| 631 | |
| 632 | /* --- @p_ifname@ --- * |
| 633 | * |
| 634 | * Arguments: @peer *p@ = pointer to a peer block |
| 635 | * |
| 636 | * Returns: A pointer to the peer's interface name. |
| 637 | */ |
| 638 | |
| 639 | extern const char *p_ifname(peer */*p*/); |
| 640 | |
| 641 | /* --- @p_addr@ --- * |
| 642 | * |
| 643 | * Arguments: @peer *p@ = pointer to a peer block |
| 644 | * |
| 645 | * Returns: A pointer to the peer's address. |
| 646 | */ |
| 647 | |
| 648 | extern const addr *p_addr(peer */*p*/); |
| 649 | |
| 650 | /* --- @p_init@ --- * |
| 651 | * |
| 652 | * Arguments: @unsigned port@ = port number to listen to |
| 653 | * |
| 654 | * Returns: --- |
| 655 | * |
| 656 | * Use: Initializes the peer system; creates the socket. |
| 657 | */ |
| 658 | |
| 659 | extern void p_init(unsigned /*port*/); |
| 660 | |
| 661 | /* --- @p_port@ --- * |
| 662 | * |
| 663 | * Arguments: --- |
| 664 | * |
| 665 | * Returns: Port number used for socket. |
| 666 | */ |
| 667 | |
| 668 | unsigned p_port(void); |
| 669 | |
| 670 | /* --- @p_create@ --- * |
| 671 | * |
| 672 | * Arguments: @const char *name@ = name for this peer |
| 673 | * @struct sockaddr *sa@ = socket address of peer |
| 674 | * @size_t sz@ = size of socket address |
| 675 | * |
| 676 | * Returns: Pointer to the peer block, or null if it failed. |
| 677 | * |
| 678 | * Use: Creates a new named peer block. No peer is actually attached |
| 679 | * by this point. |
| 680 | */ |
| 681 | |
| 682 | extern peer *p_create(const char */*name*/, |
| 683 | struct sockaddr */*sa*/, size_t /*sz*/); |
| 684 | |
| 685 | /* --- @p_name@ --- * |
| 686 | * |
| 687 | * Arguments: @peer *p@ = pointer to a peer block |
| 688 | * |
| 689 | * Returns: A pointer to the peer's name. |
| 690 | */ |
| 691 | |
| 692 | extern const char *p_name(peer */*p*/); |
| 693 | |
| 694 | /* --- @p_find@ --- * |
| 695 | * |
| 696 | * Arguments: @const char *name@ = name to look up |
| 697 | * |
| 698 | * Returns: Pointer to the peer block, or null if not found. |
| 699 | * |
| 700 | * Use: Finds a peer by name. |
| 701 | */ |
| 702 | |
| 703 | extern peer *p_find(const char */*name*/); |
| 704 | |
| 705 | /* --- @p_destroy@ --- * |
| 706 | * |
| 707 | * Arguments: @peer *p@ = pointer to a peer |
| 708 | * |
| 709 | * Returns: --- |
| 710 | * |
| 711 | * Use: Destroys a peer. |
| 712 | */ |
| 713 | |
| 714 | extern void p_destroy(peer */*p*/); |
| 715 | |
| 716 | /* --- @p_first@, @p_next@ --- * |
| 717 | * |
| 718 | * Arguments: @peer *p@ = a peer block |
| 719 | * |
| 720 | * Returns: @peer_first@ returns the first peer in some ordering; |
| 721 | * @peer_next@ returns the peer following a given one in the |
| 722 | * same ordering. Null is returned for the end of the list. |
| 723 | */ |
| 724 | |
| 725 | extern peer *p_first(void); |
| 726 | extern peer *p_next(peer */*p*/); |
| 727 | |
| 728 | /*----- Tunnel interface --------------------------------------------------*/ |
| 729 | |
| 730 | /* --- @tun_init@ --- * |
| 731 | * |
| 732 | * Arguments: --- |
| 733 | * |
| 734 | * Returns: --- |
| 735 | * |
| 736 | * Use: Initializes the tunneling system. Maybe this will require |
| 737 | * opening file descriptors or something. |
| 738 | */ |
| 739 | |
| 740 | extern void tun_init(void); |
| 741 | |
| 742 | /* --- @tun_create@ --- * |
| 743 | * |
| 744 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 745 | * @peer *p@ = pointer to peer block |
| 746 | * |
| 747 | * Returns: Zero if it worked, nonzero on failure. |
| 748 | * |
| 749 | * Use: Initializes a new tunnel. |
| 750 | */ |
| 751 | |
| 752 | extern int tun_create(tunnel */*t*/, peer */*p*/); |
| 753 | |
| 754 | /* --- @tun_ifname@ --- * |
| 755 | * |
| 756 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 757 | * |
| 758 | * Returns: A pointer to the tunnel's interface name. |
| 759 | */ |
| 760 | |
| 761 | extern const char *tun_ifname(tunnel */*t*/); |
| 762 | |
| 763 | /* --- @tun_inject@ --- * |
| 764 | * |
| 765 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 766 | * @buf *b@ = buffer to send |
| 767 | * |
| 768 | * Returns: --- |
| 769 | * |
| 770 | * Use: Injects a packet into the local network stack. |
| 771 | */ |
| 772 | |
| 773 | extern void tun_inject(tunnel */*t*/, buf */*b*/); |
| 774 | |
| 775 | /* --- @tun_destroy@ --- * |
| 776 | * |
| 777 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 778 | * |
| 779 | * Returns: --- |
| 780 | * |
| 781 | * Use: Destroys a tunnel. |
| 782 | */ |
| 783 | |
| 784 | extern void tun_destroy(tunnel */*t*/); |
| 785 | |
| 786 | /*----- Buffer handling ---------------------------------------------------*/ |
| 787 | |
| 788 | /* --- Useful macros --- */ |
| 789 | |
| 790 | #define BBASE(b) ((b)->base) |
| 791 | #define BLIM(b) ((b)->limit) |
| 792 | #define BCUR(b) ((b)->p) |
| 793 | #define BSZ(b) ((b)->limit - (b)->base) |
| 794 | #define BLEN(b) ((b)->p - (b)->base) |
| 795 | #define BLEFT(b) ((b)->limit - (b)->p) |
| 796 | #define BSTEP(b, sz) ((b)->p += (sz)) |
| 797 | #define BBAD(b) ((b)->f & BF_BROKEN) |
| 798 | #define BOK(b) (!BBAD(b)) |
| 799 | |
| 800 | #define BENSURE(b, sz) \ |
| 801 | (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0) |
| 802 | |
| 803 | /* --- @buf_init@ --- * |
| 804 | * |
| 805 | * Arguments: @buf *b@ = pointer to a buffer block |
| 806 | * @void *p@ = pointer to a buffer |
| 807 | * @size_t sz@ = size of the buffer |
| 808 | * |
| 809 | * Returns: --- |
| 810 | * |
| 811 | * Use: Initializes the buffer block appropriately. |
| 812 | */ |
| 813 | |
| 814 | extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/); |
| 815 | |
| 816 | /* --- @buf_break@ --- * |
| 817 | * |
| 818 | * Arguments: @buf *b@ = pointer to a buffer block |
| 819 | * |
| 820 | * Returns: Some negative value. |
| 821 | * |
| 822 | * Use: Marks a buffer as broken. |
| 823 | */ |
| 824 | |
| 825 | extern int buf_break(buf */*b*/); |
| 826 | |
| 827 | /* --- @buf_ensure@ --- * |
| 828 | * |
| 829 | * Arguments: @buf *b@ = pointer to a buffer block |
| 830 | * @size_t sz@ = size of data wanted |
| 831 | * |
| 832 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 833 | * |
| 834 | * Use: Ensures that there are @sz@ bytes still in the buffer. |
| 835 | */ |
| 836 | |
| 837 | extern int buf_ensure(buf */*b*/, size_t /*sz*/); |
| 838 | |
| 839 | /* --- @buf_get@ --- * |
| 840 | * |
| 841 | * Arguments: @buf *b@ = pointer to a buffer block |
| 842 | * @void *p@ = pointer to a buffer |
| 843 | * @size_t sz@ = size of the buffer |
| 844 | * |
| 845 | * Returns: Zero if it worked, nonzero if there wasn't enough data. |
| 846 | * |
| 847 | * Use: Fetches data from the buffer into some other place. |
| 848 | */ |
| 849 | |
| 850 | extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/); |
| 851 | |
| 852 | /* --- @buf_put@ --- * |
| 853 | * |
| 854 | * Arguments: @buf *b@ = pointer to a buffer block |
| 855 | * @const void *p@ = pointer to a buffer |
| 856 | * @size_t sz@ = size of the buffer |
| 857 | * |
| 858 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 859 | * |
| 860 | * Use: Fetches data from some place and puts it in the buffer |
| 861 | */ |
| 862 | |
| 863 | extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/); |
| 864 | |
| 865 | /* --- @buf_getbyte@ --- * |
| 866 | * |
| 867 | * Arguments: @buf *b@ = pointer to a buffer block |
| 868 | * |
| 869 | * Returns: A byte, or less than zero if there wasn't a byte there. |
| 870 | * |
| 871 | * Use: Gets a single byte from a buffer. |
| 872 | */ |
| 873 | |
| 874 | extern int buf_getbyte(buf */*b*/); |
| 875 | |
| 876 | /* --- @buf_putbyte@ --- * |
| 877 | * |
| 878 | * Arguments: @buf *b@ = pointer to a buffer block |
| 879 | * @int ch@ = byte to write |
| 880 | * |
| 881 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 882 | * |
| 883 | * Use: Puts a single byte in a buffer. |
| 884 | */ |
| 885 | |
| 886 | extern int buf_putbyte(buf */*b*/, int /*ch*/); |
| 887 | |
| 888 | /* --- @buf_getword@ --- * |
| 889 | * |
| 890 | * Arguments: @buf *b@ = pointer to a buffer block |
| 891 | * @uint32 *w@ = where to put the word |
| 892 | * |
| 893 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 894 | * |
| 895 | * Use: Gets a 32-bit word from a buffer. |
| 896 | */ |
| 897 | |
| 898 | extern int buf_getword(buf */*b*/, uint32 */*w*/); |
| 899 | |
| 900 | /* --- @buf_putword@ --- * |
| 901 | * |
| 902 | * Arguments: @buf *b@ = pointer to a buffer block |
| 903 | * @uint32 w@ = word to write |
| 904 | * |
| 905 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 906 | * |
| 907 | * Use: Puts a 32-but word in a buffer. |
| 908 | */ |
| 909 | |
| 910 | extern int buf_putword(buf */*b*/, uint32 /*w*/); |
| 911 | |
| 912 | /* --- @buf_getmp@ --- * |
| 913 | * |
| 914 | * Arguments: @buf *b@ = pointer to a buffer block |
| 915 | * |
| 916 | * Returns: A multiprecision integer, or null if there wasn't one there. |
| 917 | * |
| 918 | * Use: Gets a multiprecision integer from a buffer. |
| 919 | */ |
| 920 | |
| 921 | extern mp *buf_getmp(buf */*b*/, mp */*d*/); |
| 922 | |
| 923 | /* --- @buf_putmp@ --- * |
| 924 | * |
| 925 | * Arguments: @buf *b@ = pointer to a buffer block |
| 926 | * @mp *m@ = a multiprecision integer |
| 927 | * |
| 928 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 929 | * |
| 930 | * Use: Puts a multiprecision integer to a buffer. |
| 931 | */ |
| 932 | |
| 933 | extern int buf_putmp(buf */*b*/, mp */*m*/); |
| 934 | |
| 935 | /*----- Other handy utilities ---------------------------------------------*/ |
| 936 | |
| 937 | /* --- @mpstr@ --- * |
| 938 | * |
| 939 | * Arguments: @mp *m@ = a multiprecision integer |
| 940 | * |
| 941 | * Returns: A pointer to the integer's textual representation. |
| 942 | * |
| 943 | * Use: Converts a multiprecision integer to a string. Corrupts |
| 944 | * @buf_o@. |
| 945 | */ |
| 946 | |
| 947 | extern const char *mpstr(mp */*m*/); |
| 948 | |
| 949 | /*----- That's all, folks -------------------------------------------------*/ |
| 950 | |
| 951 | #ifdef __cplusplus |
| 952 | } |
| 953 | #endif |
| 954 | |
| 955 | #endif |