3 * Main header file for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
33 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
66 # define ADNS_FEATURE_MANYAF
70 #include <mLib/alloc.h>
71 #include <mLib/arena.h>
72 #include <mLib/base64.h>
74 # include <mLib/bres.h>
76 #include <mLib/codec.h>
77 #include <mLib/daemonize.h>
78 #include <mLib/dstr.h>
80 #include <mLib/fdflags.h>
81 #include <mLib/fdpass.h>
82 #include <mLib/fwatch.h>
83 #include <mLib/hash.h>
84 #include <mLib/macros.h>
85 #include <mLib/mdup.h>
86 #include <mLib/mdwopt.h>
87 #include <mLib/quis.h>
88 #include <mLib/report.h>
90 #include <mLib/selbuf.h>
94 #include <mLib/trace.h>
96 #include <mLib/versioncmp.h>
98 #include <catacomb/buf.h>
99 #include <catacomb/ct.h>
101 #include <catacomb/chacha.h>
102 #include <catacomb/gcipher.h>
103 #include <catacomb/gmac.h>
104 #include <catacomb/grand.h>
105 #include <catacomb/key.h>
106 #include <catacomb/paranoia.h>
107 #include <catacomb/poly1305.h>
108 #include <catacomb/salsa20.h>
110 #include <catacomb/noise.h>
111 #include <catacomb/rand.h>
113 #include <catacomb/mp.h>
114 #include <catacomb/mpmont.h>
115 #include <catacomb/mprand.h>
116 #include <catacomb/dh.h>
117 #include <catacomb/ec.h>
118 #include <catacomb/ec-raw.h>
119 #include <catacomb/ec-keys.h>
120 #include <catacomb/x25519.h>
121 #include <catacomb/x448.h>
124 #include "protocol.h"
130 /*----- Magic numbers -----------------------------------------------------*/
132 /* --- Trace flags --- */
140 #define T_KEYEXCH 64u
141 #define T_KEYMGMT 128u
143 /* T_PRIVSEP in priv.h */
149 #define SEC(n) (n##u)
150 #define MIN(n) (n##u * 60u)
151 #define F_2P32 (65536.0*65536.0)
152 #define MEG(n) (n##ul * 1024ul * 1024ul)
154 /* --- Timing parameters --- */
156 #define T_EXP MIN(60) /* Expiry time for a key */
157 #define T_REGEN MIN(40) /* Regeneration time for a key */
159 #define T_VALID SEC(20) /* Challenge validity period */
160 #define T_RETRYMIN SEC(2) /* Minimum retry interval */
161 #define T_RETRYMAX MIN(5) /* Maximum retry interval */
162 #define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */
164 #define T_WOBBLE (1.0/3.0) /* Relative timer randomness */
166 /* --- Other things --- */
168 #define PKBUFSZ 65536
170 /*----- Cipher selections -------------------------------------------------*/
172 typedef struct keyset keyset;
173 typedef struct algswitch algswitch;
174 typedef struct kdata kdata;
175 typedef struct admin admin;
177 typedef struct dhgrp {
178 const struct dhops *ops;
182 typedef struct dhsc dhsc;
183 typedef struct dhge dhge;
186 DHFMT_STD, /* Fixed-width format, suitable for encryption */
187 DHFMT_HASH, /* Deterministic format, suitable for hashing */
188 DHFMT_VAR /* Variable-width-format, mostly a bad idea */
191 typedef struct bulkalgs {
192 const struct bulkops *ops;
195 typedef struct bulkctx {
196 const struct bulkops *ops;
199 typedef struct bulkchal {
200 const struct bulkops *ops;
206 typedef struct dhops {
209 int (*ldpriv)(key_file */*kf*/, key */*k*/, key_data */*d*/,
210 kdata */*kd*/, dstr */*t*/, dstr */*e*/);
211 /* Load a private key from @d@, storing the data in @kd@. The key's
212 * file and key object are in @kf@ and @k@, mostly in case its
213 * attributes are interesting; the key tag is in @t@; errors are
214 * reported by writing tokens to @e@ and returning nonzero.
217 int (*ldpub)(key_file */*kf*/, key */*k*/, key_data */*d*/,
218 kdata */*kd*/, dstr */*t*/, dstr */*e*/);
219 /* Load a public key from @d@, storing the data in @kd@. The key's
220 * file and key object are in @kf@ and @k@, mostly in case its
221 * attributes are interesting; the key tag is in @t@; errors are
222 * reported by writing tokens to @e@ and returning nonzero.
225 const char *(*checkgrp)(const dhgrp */*g*/);
226 /* Check that the group is valid; return null on success, or an error
230 void (*grpinfo)(const dhgrp */*g*/, admin */*a*/);
231 /* Report on the group to an admin client. */
233 T( void (*tracegrp)(const dhgrp */*g*/); )
234 /* Trace a description of the group. */
236 int (*samegrpp)(const dhgrp */*g*/, const dhgrp */*gg*/);
237 /* Return nonzero if the two group objects represent the same
241 void (*freegrp)(dhgrp */*g*/);
242 /* Free a group and the resources it holds. */
244 dhsc *(*ldsc)(const dhgrp */*g*/, const void */*p*/, size_t /*sz*/);
245 /* Load a scalar from @p@, @sz@ and return it. Return null on
249 int (*stsc)(const dhgrp */*g*/,
250 void */*p*/, size_t /*sz*/, const dhsc */*x*/);
251 /* Store a scalar at @p@, @sz@. Return nonzero on error. */
253 dhsc *(*randsc)(const dhgrp */*g*/);
254 /* Return a random scalar. */
256 T( const char *(*scstr)(const dhgrp */*g*/, const dhsc */*x*/); )
257 /* Return a human-readable representation of @x@; @buf_t@ may be used
261 void (*freesc)(const dhgrp */*g*/, dhsc */*x*/);
262 /* Free a scalar and the resources it holds. */
264 dhge *(*ldge)(const dhgrp */*g*/, buf */*b*/, int /*fmt*/);
265 /* Load a group element from @b@, encoded using format @fmt@. Return
269 int (*stge)(const dhgrp */*g*/, buf */*b*/,
270 const dhge */*Y*/, int /*fmt*/);
271 /* Store a group element in @b@, encoded using format @fmt@. Return
275 int (*checkge)(const dhgrp */*h*/, const dhge */*Y*/);
276 /* Check a group element for validity. Return zero if everything
277 * checks out; nonzero on failure.
280 int (*eq)(const dhgrp */*g*/, const dhge */*Y*/, const dhge */*Z*/);
281 /* Return nonzero if @Y@ and @Z@ are equal. */
283 dhge *(*mul)(const dhgrp */*g*/, const dhsc */*x*/, const dhge */*Y*/);
284 /* Multiply a group element by a scalar, resulting in a shared-secret
285 * group element. If @y@ is null, then multiply the well-known
289 T( const char *(*gestr)(const dhgrp */*g*/, const dhge */*Y*/); )
290 /* Return a human-readable representation of @Y@; @buf_t@ may be used
294 void (*freege)(const dhgrp */*g*/, dhge */*Y*/);
295 /* Free a group element and the resources it holds. */
299 typedef struct bulkops {
302 bulkalgs *(*getalgs)(const algswitch */*asw*/, dstr */*e*/,
303 key_file */*kf*/, key */*k*/);
304 /* Determine algorithms to use and return a @bulkalgs@ object
305 * representing the decision. On error, write tokens to @e@ and
309 T( void (*tracealgs)(const bulkalgs */*a*/); )
310 /* Write trace information about the algorithm selection. */
312 int (*checkalgs)(bulkalgs */*a*/, const algswitch */*asw*/, dstr */*e*/);
313 /* Check that the algorithms in @a@ and @asw@ are acceptable. On
314 * error, write tokens to @e@ and return @-1@; otherwise return zero.
317 int (*samealgsp)(const bulkalgs */*a*/, const bulkalgs */*aa*/);
318 /* If @a@ and @aa@ represent the same algorithm selection, return
319 * nonzero; if not, return zero.
322 void (*alginfo)(const bulkalgs */*a*/, admin */*adm*/);
323 /* Report on the algorithm selection to an admin client: call
324 * @a_info@ with appropriate key-value pairs.
327 size_t (*overhead)(const bulkalgs */*a*/);
328 /* Return the per-packet overhead of the bulk transform, in bytes. */
330 size_t (*expsz)(const bulkalgs */*a*/);
331 /* Return the total size limit for the bulk transform, in bytes,
332 * after which the keys must no longer be used.
335 bulkctx *(*genkeys)(const bulkalgs */*a*/, const struct rawkey */*rk*/);
336 /* Generate session keys and construct and return an appropriate
337 * context for using them, by calling @ks_derive@.
340 bulkchal *(*genchal)(const bulkalgs */*a*/);
341 /* Construct and return a challenge issuing and verification
342 * context with a fresh random key.
345 void (*freealgs)(bulkalgs */*a*/);
346 /* Release an algorithm selection object. (Associated bulk
347 * encryption contexts and challenge contexts may still exist and
348 * must remain valid.)
351 int (*encrypt)(bulkctx */*bc*/, unsigned /*ty*/,
352 buf */*b*/, buf */*bb*/, uint32 /*seq*/);
353 /* Encrypt the packet in @b@, with type @ty@ (which doesn't need
354 * encoding separately) and sequence number @seq@ (which must be
355 * recoverable by @decrypt@), and write the result to @bb@. On
356 * error, return a @KSERR_...@ code and/or break the output buffer.
359 int (*decrypt)(bulkctx */*bc*/, unsigned /*ty*/,
360 buf */*b*/, buf */*bb*/, uint32 */*seq*/);
361 /* Decrypt the packet in @b@, with type @ty@, writing the result to
362 * @bb@ and storing the incoming (claimed) sequence number in @seq@.
363 * On error, return a @KSERR_...@ code.
366 void (*freectx)(bulkctx */*a*/);
367 /* Release a bulk encryption context and the resources it holds. */
369 int (*chaltag)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/,
371 /* Calculate a tag for the challenge in @m@, @msz@, and write it to
372 * @t@. Return @-1@ on error, zero on success.
375 int (*chalvrf)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/,
377 /* Check the tag @t@ on @m@, @msz@: return zero if the tag is OK,
378 * nonzero if it's bad.
381 void (*freechal)(bulkchal */*bc*/);
382 /* Release a challenge context and the resources it holds. */
387 const gchash *h; size_t hashsz; /* Hash function */
388 const gccipher *mgf; /* Mask-generation function */
389 bulkalgs *bulk; /* Bulk crypto algorithms */
393 unsigned ref; /* Reference counter */
394 struct knode *kn; /* Pointer to cache entry */
395 char *tag; /* Full tag name of the key */
396 dhgrp *grp; /* The group we work in */
397 dhsc *k; /* The private key (or null) */
398 dhge *K; /* The public key */
399 time_t t_exp; /* Expiry time of the key */
400 algswitch algs; /* Collection of algorithms */
403 typedef struct knode {
404 sym_base _b; /* Symbol table intrusion */
405 unsigned f; /* Various flags */
406 #define KNF_BROKEN 1u /* Don't use this key any more */
407 struct keyhalf *kh; /* Pointer to the home keyhalf */
408 kdata *kd; /* Pointer to the key data */
411 #define MAXHASHSZ 64 /* Largest possible hash size */
413 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
415 extern const dhops dhtab[];
416 extern const bulkops bulktab[];
418 /*----- Data structures ---------------------------------------------------*/
420 /* --- The address-family table --- */
427 #define ENUM(af, qf) AFIX_##af,
433 extern const struct addrfam {
441 /* --- Socket addresses --- *
443 * A magic union of supported socket addresses.
448 struct sockaddr_in sin;
449 struct sockaddr_in6 sin6;
452 /* --- Mapping keyed on addresses --- */
454 typedef struct addrmap {
459 typedef struct addrmap_base {
464 /* --- Sequence number checking --- */
466 typedef struct seqwin {
467 uint32 seq; /* First acceptable input sequence */
468 uint32 win; /* Window of acceptable numbers */
471 #define SEQ_WINSZ 32 /* Bits in sequence number window */
473 /* --- A symmetric keyset --- *
475 * A keyset contains a set of symmetric keys for encrypting and decrypting
476 * packets. Keysets are stored in a list, sorted in reverse order of
477 * creation, so that the most recent keyset (the one most likely to be used)
480 * Each keyset has a time limit and a data limit. The keyset is destroyed
481 * when either it has existed for too long, or it has been used to encrypt
482 * too much data. New key exchanges are triggered when keys are close to
486 enum { DIR_IN, DIR_OUT, NDIR };
489 struct keyset *next; /* Next active keyset in the list */
490 unsigned ref; /* Reference count for keyset */
491 struct peer *p; /* Pointer to peer structure */
492 time_t t_exp; /* Expiry time for this keyset */
493 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
494 T( unsigned seq; ) /* Sequence number for tracing */
495 unsigned f; /* Various useful flags */
496 bulkctx *bulk; /* Bulk crypto transform */
497 uint32 oseq; /* Outbound sequence number */
498 seqwin iseq; /* Inbound sequence number */
501 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
502 #define KSF_LINK 2u /* Key is in a linked list */
504 #define KSERR_REGEN -1 /* Regenerate keys */
505 #define KSERR_NOKEYS -2 /* No keys left */
506 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
507 #define KSERR_SEQ -4 /* Incorrect sequence number */
508 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
510 /* --- Key exchange --- *
512 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
513 * Protocol has a number of desirable features (e.g., perfect forward
514 * secrecy, and zero-knowledge authentication) which make it attractive for
515 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
519 typedef struct retry {
520 double t; /* Current retry time */
525 typedef struct kxchal {
526 struct keyexch *kx; /* Pointer back to key exchange */
527 dhge *C; /* Responder's challenge */
528 dhge *R; /* My reply to the challenge */
529 keyset *ks; /* Pointer to temporary keyset */
530 unsigned f; /* Various useful flags */
531 sel_timer t; /* Response timer for challenge */
532 retry rs; /* Retry state */
533 octet hc[MAXHASHSZ]; /* Hash of his challenge */
534 octet ck[MAXHASHSZ]; /* His magical check value */
535 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
536 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
537 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
538 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
541 typedef struct keyexch {
542 struct peer *p; /* Pointer back to the peer */
543 kdata *kpriv; /* Private key and related info */
544 kdata *kpub; /* Peer's public key */
545 keyset **ks; /* Peer's list of keysets */
546 unsigned f; /* Various useful flags */
547 unsigned s; /* Current state in exchange */
548 sel_timer t; /* Timer for next exchange */
549 retry rs; /* Retry state */
550 dhsc *a; /* My temporary secret */
551 dhge *C; /* My challenge */
552 dhge *RX; /* The expected response */
553 unsigned nr; /* Number of extant responses */
554 time_t t_valid; /* When this exchange goes bad */
555 octet hc[MAXHASHSZ]; /* Hash of my challenge */
556 kxchal *r[KX_NCHAL]; /* Array of challenges */
559 #define KXF_TIMER 1u /* Waiting for a timer to go off */
560 #define KXF_DEAD 2u /* The key-exchanger isn't up */
561 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
562 #define KXF_CORK 8u /* Don't send anything yet */
565 KXS_DEAD, /* Uninitialized state (magical) */
566 KXS_CHAL, /* Main answer-challenges state */
567 KXS_COMMIT, /* Committed: send switch request */
568 KXS_SWITCH /* Switched: send confirmation */
571 /* --- Tunnel structure --- *
573 * Used to maintain system-specific information about the tunnel interface.
576 typedef struct tunnel tunnel;
579 typedef struct tunnel_ops {
580 const char *name; /* Name of this tunnel driver */
581 unsigned flags; /* Various interesting flags */
582 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
583 void (*init)(void); /* Initializes the system */
584 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
585 /* Initializes a new tunnel */
586 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
587 /* Notifies ifname change */
588 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
589 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
592 #ifndef TUN_INTERNALS
593 struct tunnel { const tunnel_ops *ops; };
596 /* --- Peer statistics --- *
598 * Contains various interesting and not-so-interesting statistics about a
599 * peer. This is updated by various parts of the code. The format of the
600 * structure isn't considered private, and @p_stats@ returns a pointer to the
601 * statistics block for a given peer.
604 typedef struct stats {
605 unsigned long sz_in, sz_out; /* Size of all data in and out */
606 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
607 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
608 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
609 unsigned long n_reject; /* Number of rejected packets */
610 unsigned long n_in, n_out; /* Number of packets in and out */
611 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
612 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
615 /* --- Peer structure --- *
617 * The main structure which glues everything else together.
620 typedef struct peerspec {
621 char *name; /* Peer's name */
622 char *privtag; /* Private key tag */
623 char *tag; /* Public key tag */
624 const tunnel_ops *tops; /* Tunnel operations */
625 unsigned long t_ka; /* Keep alive interval */
626 addr sa; /* Socket address to speak to */
627 unsigned f; /* Flags for the peer */
628 #define PSF_KXMASK 255u /* Key-exchange flags to set */
629 #define PSF_MOBILE 256u /* Address may change rapidly */
632 typedef struct peer_byname {
637 typedef struct peer_byaddr {
642 typedef struct peer {
643 peer_byname *byname; /* Lookup-by-name block */
644 peer_byaddr *byaddr; /* Lookup-by-address block */
645 struct ping *pings; /* Pings we're waiting for */
646 peerspec spec; /* Specifications for this peer */
647 int afix; /* Index of address family */
648 tunnel *t; /* Tunnel for local packets */
649 char *ifname; /* Interface name for tunnel */
650 keyset *ks; /* List head for keysets */
651 buf b; /* Buffer for sending packets */
652 stats st; /* Statistics */
653 keyexch kx; /* Key exchange protocol block */
654 sel_timer tka; /* Timer for keepalives */
657 typedef struct peer_iter { sym_iter i; } peer_iter;
659 typedef struct ping {
660 struct ping *next, *prev; /* Links to next and previous */
661 peer *p; /* Peer so we can free it */
662 unsigned msg; /* Kind of response expected */
663 uint32 id; /* Id so we can recognize response */
664 octet magic[32]; /* Some random data */
665 sel_timer t; /* Timeout for ping */
666 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
667 void *arg; /* Argument for callback */
678 /* --- Admin structure --- */
680 #define OBUFSZ 16384u
682 typedef struct obuf {
683 struct obuf *next; /* Next buffer in list */
684 char *p_in, *p_out; /* Pointers into the buffer */
685 char buf[OBUFSZ]; /* The actual buffer */
688 typedef struct oqueue {
689 obuf *hd, *tl; /* Head and tail pointers */
694 typedef struct admin_bgop {
695 struct admin_bgop *next, *prev; /* Links to next and previous */
696 struct admin *a; /* Owner job */
697 char *tag; /* Tag string for messages */
698 void (*cancel)(struct admin_bgop *); /* Destructor function */
701 typedef struct admin_resop {
702 admin_bgop bg; /* Background operation header */
703 char *addr; /* Hostname to be resolved */
707 bres_client r; /* Background resolver task */
709 sel_timer t; /* Timer for resolver */
710 addr sa; /* Socket address */
711 unsigned port; /* Port number chosen */
712 size_t sasz; /* Socket address size */
713 void (*func)(struct admin_resop *, int); /* Handler */
716 enum { ARES_OK, ARES_FAIL };
718 typedef struct admin_addop {
719 admin_resop r; /* Name resolution header */
720 peerspec peer; /* Peer pending creation */
723 typedef struct admin_pingop {
724 admin_bgop bg; /* Background operation header */
725 ping ping; /* Ping pending response */
726 struct timeval pingtime; /* Time last ping was sent */
729 typedef struct admin_service {
730 sym_base _b; /* Hash table base structure */
731 char *version; /* The provided version */
732 struct admin *prov; /* Which client provides me */
733 struct admin_service *next, *prev; /* Client's list of services */
736 typedef struct admin_svcop {
737 admin_bgop bg; /* Background operation header */
738 struct admin *prov; /* Client servicing this job */
739 unsigned index; /* This job's index */
740 struct admin_svcop *next, *prev; /* Links for provider's jobs */
743 typedef struct admin_jobentry {
744 unsigned short seq; /* Zero if unused */
746 admin_svcop *op; /* Operation, if slot in use, ... */
747 uint32 next; /* ... or index of next free slot */
751 typedef struct admin_jobtable {
752 uint32 n, sz; /* Used slots and table size */
753 admin_svcop *active; /* List of active jobs */
754 uint32 free; /* Index of first free slot */
755 admin_jobentry *v; /* And the big array of entries */
759 struct admin *next, *prev; /* Links to next and previous */
760 unsigned f; /* Various useful flags */
761 unsigned ref; /* Reference counter */
763 unsigned seq; /* Sequence number for tracing */
765 oqueue out; /* Output buffer list */
766 oqueue delay; /* Delayed output buffer list */
767 admin_bgop *bg; /* Backgrounded operations */
768 admin_service *svcs; /* Which services I provide */
769 admin_jobtable j; /* Table of outstanding jobs */
770 selbuf b; /* Line buffer for commands */
771 sel_file w; /* Selector for write buffering */
774 #define AF_DEAD 1u /* Destroy this admin block */
775 #define AF_CLOSE 2u /* Client closed connection */
776 #define AF_NOTE 4u /* Catch notifications */
777 #define AF_WARN 8u /* Catch warning messages */
779 # define AF_TRACE 16u /* Catch tracing */
781 #define AF_FOREGROUND 32u /* Quit server when client closes */
784 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
786 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
789 /*----- Global variables --------------------------------------------------*/
791 extern sel_state sel; /* Global I/O event state */
792 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
793 extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
794 extern const tunnel_ops *tun_default; /* Default tunnel to use */
795 extern sel_file udpsock[NADDRFAM]; /* The master UDP sockets */
796 extern kdata *master; /* Default private key */
797 extern const char *tag_priv; /* Default private key tag */
800 extern const trace_opt tr_opts[]; /* Trace options array */
801 extern unsigned tr_flags; /* Trace options flags */
804 /*----- Other macros ------------------------------------------------------*/
807 do { rand_quick(RAND_GLOBAL); noise_timer(RAND_GLOBAL); } while (0)
809 /*----- Key management ----------------------------------------------------*/
811 /* --- @km_init@ --- *
813 * Arguments: @const char *privkr@ = private keyring file
814 * @const char *pubkr@ = public keyring file
815 * @const char *ptag@ = default private-key tag
819 * Use: Initializes the key-management machinery, loading the
820 * keyrings and so on.
823 extern void km_init(const char */*privkr*/, const char */*pubkr*/,
824 const char */*ptag*/);
826 /* --- @km_reload@ --- *
830 * Returns: Zero if OK, nonzero to force reloading of keys.
832 * Use: Checks the keyrings to see if they need reloading.
835 extern int km_reload(void);
837 /* --- @km_findpub@, @km_findpriv@ --- *
839 * Arguments: @const char *tag@ = key tag to load
841 * Returns: Pointer to the kdata object if successful, or null on error.
843 * Use: Fetches a public or private key from the keyring.
846 extern kdata *km_findpub(const char */*tag*/);
847 extern kdata *km_findpriv(const char */*tag*/);
849 /* --- @km_samealgsp@ --- *
851 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
853 * Returns: Nonzero if their two algorithm selections are the same.
855 * Use: Checks sameness of algorithm selections: used to ensure that
856 * peers are using sensible algorithms.
859 extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
861 /* --- @km_ref@ --- *
863 * Arguments: @kdata *kd@ = pointer to the kdata object
867 * Use: Claim a new reference to a kdata object.
870 extern void km_ref(kdata */*kd*/);
872 /* --- @km_unref@ --- *
874 * Arguments: @kdata *kd@ = pointer to the kdata object
878 * Use: Releases a reference to a kdata object.
881 extern void km_unref(kdata */*kd*/);
883 /* --- @km_tag@ --- *
885 * Arguments: @kdata *kd@ - pointer to the kdata object
887 * Returns: A pointer to the short tag by which the kdata was loaded.
890 extern const char *km_tag(kdata */*kd*/);
892 /*----- Key exchange ------------------------------------------------------*/
894 /* --- @kx_start@ --- *
896 * Arguments: @keyexch *kx@ = pointer to key exchange context
897 * @int forcep@ = nonzero to ignore the quiet timer
901 * Use: Stimulates a key exchange. If a key exchage is in progress,
902 * a new challenge is sent (unless the quiet timer forbids
903 * this); if no exchange is in progress, one is commenced.
906 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
908 /* --- @kx_message@ --- *
910 * Arguments: @keyexch *kx@ = pointer to key exchange context
911 * @unsigned msg@ = the message code
912 * @buf *b@ = pointer to buffer containing the packet
916 * Use: Reads a packet containing key exchange messages and handles
920 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
922 /* --- @kx_free@ --- *
924 * Arguments: @keyexch *kx@ = pointer to key exchange context
928 * Use: Frees everything in a key exchange context.
931 extern void kx_free(keyexch */*kx*/);
933 /* --- @kx_newkeys@ --- *
935 * Arguments: @keyexch *kx@ = pointer to key exchange context
939 * Use: Informs the key exchange module that its keys may have
940 * changed. If fetching the new keys fails, the peer will be
941 * destroyed, we log messages and struggle along with the old
945 extern void kx_newkeys(keyexch */*kx*/);
947 /* --- @kx_init@ --- *
949 * Arguments: @keyexch *kx@ = pointer to key exchange context
950 * @peer *p@ = pointer to peer context
951 * @keyset **ks@ = pointer to keyset list
952 * @unsigned f@ = various useful flags
954 * Returns: Zero if OK, nonzero if it failed.
956 * Use: Initializes a key exchange module. The module currently
957 * contains no keys, and will attempt to initiate a key
961 extern int kx_init(keyexch */*kx*/, peer */*p*/,
962 keyset **/*ks*/, unsigned /*f*/);
964 /*----- Keysets and symmetric cryptography --------------------------------*/
966 /* --- @ks_drop@ --- *
968 * Arguments: @keyset *ks@ = pointer to a keyset
972 * Use: Decrements a keyset's reference counter. If the counter hits
973 * zero, the keyset is freed.
976 extern void ks_drop(keyset */*ks*/);
978 /* --- @ks_derivekey@ --- *
980 * Arguments: @octet *k@ = pointer to an output buffer of at least
982 * @size_t ksz@ = actual size wanted (for tracing)
983 * @const struct rawkey *rk@ = a raw key, as passed into
985 * @int dir@ = direction for the key (@DIR_IN@ or @DIR_OUT@)
986 * @const char *what@ = label for the key (input to derivation)
990 * Use: Derives a session key, for use on incoming or outgoing data.
991 * This function is part of a private protocol between @ks_gen@
992 * and the bulk crypto transform @genkeys@ operation.
995 extern void ks_derivekey(octet */*k*/, size_t /*ksz*/,
996 const struct rawkey */*rk*/,
997 int /*dir*/, const char */*what*/);
999 /* --- @ks_gen@ --- *
1001 * Arguments: @const void *k@ = pointer to key material
1002 * @size_t x, y, z@ = offsets into key material (see below)
1003 * @peer *p@ = pointer to peer information
1005 * Returns: A pointer to the new keyset.
1007 * Use: Derives a new keyset from the given key material. The
1008 * offsets @x@, @y@ and @z@ separate the key material into three
1009 * parts. Between the @k@ and @k + x@ is `my' contribution to
1010 * the key material; between @k + x@ and @k + y@ is `your'
1011 * contribution; and between @k + y@ and @k + z@ is a shared
1012 * value we made together. These are used to construct two
1013 * collections of symmetric keys: one for outgoing messages, the
1014 * other for incoming messages.
1016 * The new key is marked so that it won't be selected for output
1017 * by @ksl_encrypt@. You can still encrypt data with it by
1018 * calling @ks_encrypt@ directly.
1021 extern keyset *ks_gen(const void */*k*/,
1022 size_t /*x*/, size_t /*y*/, size_t /*z*/,
1025 /* --- @ks_activate@ --- *
1027 * Arguments: @keyset *ks@ = pointer to a keyset
1031 * Use: Activates a keyset, so that it can be used for encrypting
1032 * outgoing messages.
1035 extern void ks_activate(keyset */*ks*/);
1037 /* --- @ks_encrypt@ --- *
1039 * Arguments: @keyset *ks@ = pointer to a keyset
1040 * @unsigned ty@ = message type
1041 * @buf *b@ = pointer to input buffer
1042 * @buf *bb@ = pointer to output buffer
1044 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
1045 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
1046 * returns zero if there was insufficient buffer (but the output
1047 * buffer is broken in this case).
1049 * Use: Encrypts a block of data using the key. Note that the `key
1050 * ought to be replaced' notification is only ever given once
1051 * for each key. Also note that this call forces a keyset to be
1052 * used even if it's marked as not for data output.
1054 * The encryption transform is permitted to corrupt @buf_u@ for
1055 * its own purposes. Neither the source nor destination should
1056 * be within @buf_u@; and callers mustn't expect anything stored
1057 * in @buf_u@ to still
1060 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
1061 buf */*b*/, buf */*bb*/);
1063 /* --- @ks_decrypt@ --- *
1065 * Arguments: @keyset *ks@ = pointer to a keyset
1066 * @unsigned ty@ = expected type code
1067 * @buf *b@ = pointer to an input buffer
1068 * @buf *bb@ = pointer to an output buffer
1070 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1071 * zero if there was insufficient buffer (but the output buffer
1072 * is broken in this case).
1074 * Use: Attempts to decrypt a message using a given key. Note that
1075 * requesting decryption with a key directly won't clear a
1076 * marking that it's not for encryption.
1078 * The decryption transform is permitted to corrupt @buf_u@ for
1079 * its own purposes. Neither the source nor destination should
1080 * be within @buf_u@; and callers mustn't expect anything stored
1081 * in @buf_u@ to still
1084 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
1085 buf */*b*/, buf */*bb*/);
1087 /* --- @ksl_free@ --- *
1089 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1093 * Use: Frees (releases references to) all of the keys in a keyset.
1096 extern void ksl_free(keyset **/*ksroot*/);
1098 /* --- @ksl_link@ --- *
1100 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1101 * @keyset *ks@ = pointer to a keyset
1105 * Use: Links a keyset into a list. A keyset can only be on one list
1106 * at a time. Bad things happen otherwise.
1109 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
1111 /* --- @ksl_prune@ --- *
1113 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1117 * Use: Prunes the keyset list by removing keys which mustn't be used
1121 extern void ksl_prune(keyset **/*ksroot*/);
1123 /* --- @ksl_encrypt@ --- *
1125 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1126 * @unsigned ty@ = message type
1127 * @buf *b@ = pointer to input buffer
1128 * @buf *bb@ = pointer to output buffer
1130 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
1131 * new key; @KSERR_NOKEYS@ if there are no suitable keys
1132 * available. Also returns zero if there was insufficient
1133 * buffer space (but the output buffer is broken in this case).
1135 * Use: Encrypts a packet.
1138 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1139 buf */*b*/, buf */*bb*/);
1141 /* --- @ksl_decrypt@ --- *
1143 * Arguments: @keyset **ksroot@ = pointer to keyset list head
1144 * @unsigned ty@ = expected type code
1145 * @buf *b@ = pointer to input buffer
1146 * @buf *bb@ = pointer to output buffer
1148 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
1149 * zero if there was insufficient buffer (but the output buffer
1150 * is broken in this case).
1152 * Use: Decrypts a packet.
1155 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
1156 buf */*b*/, buf */*bb*/);
1158 /*----- Challenges --------------------------------------------------------*/
1160 /* --- @c_new@ --- *
1162 * Arguments: @buf *b@ = where to put the challenge
1164 * Returns: Zero if OK, nonzero on error.
1166 * Use: Issues a new challenge.
1169 extern int c_new(buf */*b*/);
1171 /* --- @c_check@ --- *
1173 * Arguments: @buf *b@ = where to find the challenge
1175 * Returns: Zero if OK, nonzero if it didn't work.
1177 * Use: Checks a challenge. On failure, the buffer is broken.
1180 extern int c_check(buf */*b*/);
1182 /*----- Administration interface ------------------------------------------*/
1184 #define A_END ((char *)0)
1186 /* --- @a_vformat@ --- *
1188 * Arguments: @dstr *d@ = where to leave the formatted message
1189 * @const char *fmt@ = pointer to format string
1190 * @va_list *ap@ = arguments in list
1194 * Use: Main message token formatting driver. The arguments are
1195 * interleaved formatting tokens and their parameters, finally
1196 * terminated by an entry @A_END@.
1198 * Tokens recognized:
1200 * * "*..." ... -- pretokenized @dstr_putf@-like string
1202 * * "?ADDR" SOCKADDR -- a socket address, to be converted
1204 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
1206 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
1208 * * "?PEER" PEER -- peer's name
1210 * * "?ERRNO" ERRNO -- system error code
1212 * * "[!]..." ... -- @dstr_putf@-like string as single token
1215 extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list */*ap*/);
1217 /* --- @a_format@ --- *
1219 * Arguments: @dstr *d@ = where to leave the formatted message
1220 * @const char *fmt@ = pointer to format string
1224 * Use: Writes a tokenized message into a string, for later
1228 extern void EXECL_LIKE(0) a_format(dstr */*d*/, const char */*fmt*/, ...);
1230 /* --- @a_info@ --- *
1232 * Arguments: @admin *a@ = connection
1233 * @const char *fmt@ = format string
1234 * @...@ = other arguments
1238 * Use: Report information to an admin client.
1241 extern void EXECL_LIKE(0) a_info(admin */*a*/, const char */*fmt*/, ...);
1243 /* --- @a_warn@ --- *
1245 * Arguments: @const char *fmt@ = pointer to format string
1246 * @...@ = other arguments
1250 * Use: Informs all admin connections of a warning.
1253 extern void EXECL_LIKE(0) a_warn(const char */*fmt*/, ...);
1255 /* --- @a_notify@ --- *
1257 * Arguments: @const char *fmt@ = pointer to format string
1258 * @...@ = other arguments
1262 * Use: Sends a notification to interested admin connections.
1265 extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...);
1267 /* --- @a_create@ --- *
1269 * Arguments: @int fd_in, fd_out@ = file descriptors to use
1270 * @unsigned f@ = initial flags to set
1274 * Use: Creates a new admin connection.
1277 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
1279 /* --- @a_quit@ --- *
1285 * Use: Shuts things down nicely.
1288 extern void a_quit(void);
1290 /* --- @a_preselect@ --- *
1296 * Use: Informs the admin module that we're about to select again,
1297 * and that it should do cleanup things it has delayed until a
1301 extern void a_preselect(void);
1303 /* --- @a_daemon@ --- *
1309 * Use: Informs the admin module that it's a daemon.
1312 extern void a_daemon(void);
1314 /* --- @a_init@ --- *
1316 * Arguments: @const char *sock@ = socket name to create
1317 * @uid_t u@ = user to own the socket
1318 * @gid_t g@ = group to own the socket
1319 * @mode_t m@ = permissions to set on the socket
1323 * Use: Creates the admin listening socket.
1326 extern void a_init(const char */*sock*/,
1327 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
1329 /*----- Mapping with addresses as keys ------------------------------------*/
1331 /* --- @am_create@ --- *
1333 * Arguments: @addrmap *m@ = pointer to map
1337 * Use: Create an address map, properly set up.
1340 extern void am_create(addrmap */*m*/);
1342 /* --- @am_destroy@ --- *
1344 * Arguments: @addrmap *m@ = pointer to map
1348 * Use: Destroy an address map, throwing away all the entries.
1351 extern void am_destroy(addrmap */*m*/);
1353 /* --- @am_find@ --- *
1355 * Arguments: @addrmap *m@ = pointer to map
1356 * @const addr *a@ = address to look up
1357 * @size_t sz@ = size of block to allocate
1358 * @unsigned *f@ = where to store flags
1360 * Returns: Pointer to found item, or null.
1362 * Use: Finds a record with the given IP address, set @*f@ nonzero
1363 * and returns it. If @sz@ is zero, and no match was found,
1364 * return null; otherwise allocate a new block of @sz@ bytes,
1365 * clear @*f@ to zero and return the block pointer.
1368 extern void *am_find(addrmap */*m*/, const addr */*a*/,
1369 size_t /*sz*/, unsigned */*f*/);
1371 /* --- @am_remove@ --- *
1373 * Arguments: @addrmap *m@ = pointer to map
1374 * @void *i@ = pointer to the item
1378 * Use: Removes an item from the map.
1381 extern void am_remove(addrmap */*m*/, void */*i*/);
1383 /*----- Privilege separation ----------------------------------------------*/
1385 /* --- @ps_trace@ --- *
1387 * Arguments: @unsigned mask@ = trace mask to check
1388 * @const char *fmt@ = message format
1389 * @...@ = values for placeholders
1393 * Use: Writes a trace message.
1396 T( extern void PRINTF_LIKE(2, 3)
1397 ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1399 /* --- @ps_warn@ --- *
1401 * Arguments: @const char *fmt@ = message format
1402 * @...@ = values for placeholders
1406 * Use: Writes a warning message.
1409 extern void PRINTF_LIKE(1, 2) ps_warn(const char */*fmt*/, ...);
1411 /* --- @ps_tunfd@ --- *
1413 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1414 * @char **ifn@ = where to put the interface name
1416 * Returns: The file descriptor, or @-1@ on error.
1418 * Use: Fetches a file descriptor for a tunnel driver.
1421 extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1423 /* --- @ps_split@ --- *
1425 * Arguments: @int detachp@ = whether to detach the child from its terminal
1429 * Use: Separates off the privileged tunnel-opening service from the
1430 * rest of the server.
1433 extern void ps_split(int /*detachp*/);
1435 /* --- @ps_quit@ --- *
1441 * Use: Detaches from the helper process.
1444 extern void ps_quit(void);
1446 /*----- Peer management ---------------------------------------------------*/
1448 /* --- @p_updateaddr@ --- *
1450 * Arguments: @peer *p@ = pointer to peer block
1451 * @const addr *a@ = address to associate with this peer
1453 * Returns: Zero if the address was changed; @+1@ if it was already
1456 * Use: Updates our idea of @p@'s address.
1459 extern int p_updateaddr(peer */*p*/, const addr */*a*/);
1461 /* --- @p_txstart@ --- *
1463 * Arguments: @peer *p@ = pointer to peer block
1464 * @unsigned msg@ = message type code
1466 * Returns: A pointer to a buffer to write to.
1468 * Use: Starts sending to a peer. Only one send can happen at a
1472 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1474 /* --- @p_txend@ --- *
1476 * Arguments: @peer *p@ = pointer to peer block
1480 * Use: Sends a packet to the peer.
1483 extern void p_txend(peer */*p*/);
1485 /* --- @p_pingsend@ --- *
1487 * Arguments: @peer *p@ = destination peer
1488 * @ping *pg@ = structure to fill in
1489 * @unsigned type@ = message type
1490 * @unsigned long timeout@ = how long to wait before giving up
1491 * @void (*func)(int, void *)@ = callback function
1492 * @void *arg@ = argument for callback
1494 * Returns: Zero if successful, nonzero if it failed.
1496 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1497 * if we get an answer within the timeout, or zero if no answer.
1500 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1501 unsigned long /*timeout*/,
1502 void (*/*func*/)(int, void *), void */*arg*/);
1504 /* --- @p_pingdone@ --- *
1506 * Arguments: @ping *p@ = ping structure
1507 * @int rc@ = return code to pass on
1511 * Use: Disposes of a ping structure, maybe sending a notification.
1514 extern void p_pingdone(ping */*p*/, int /*rc*/);
1516 /* --- @p_greet@ --- *
1518 * Arguments: @peer *p@ = peer to send to
1519 * @const void *c@ = pointer to challenge
1520 * @size_t sz@ = size of challenge
1524 * Use: Sends a greeting packet.
1527 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1529 /* --- @p_tun@ --- *
1531 * Arguments: @peer *p@ = pointer to peer block
1532 * @buf *b@ = buffer containing incoming packet
1536 * Use: Handles a packet which needs to be sent to a peer.
1539 extern void p_tun(peer */*p*/, buf */*b*/);
1541 /* --- @p_keyreload@ --- *
1547 * Use: Forces a check of the daemon's keyring files.
1550 extern void p_keyreload(void);
1552 /* --- @p_interval@ --- *
1558 * Use: Called periodically to do tidying.
1561 extern void p_interval(void);
1563 /* --- @p_stats@ --- *
1565 * Arguments: @peer *p@ = pointer to a peer block
1567 * Returns: A pointer to the peer's statistics.
1570 extern stats *p_stats(peer */*p*/);
1572 /* --- @p_ifname@ --- *
1574 * Arguments: @peer *p@ = pointer to a peer block
1576 * Returns: A pointer to the peer's interface name.
1579 extern const char *p_ifname(peer */*p*/);
1581 /* --- @p_setifname@ --- *
1583 * Arguments: @peer *p@ = pointer to a peer block
1584 * @const char *name@ = pointer to the new name
1588 * Use: Changes the name held for a peer's interface.
1591 extern void p_setifname(peer */*p*/, const char */*name*/);
1593 /* --- @p_addr@ --- *
1595 * Arguments: @peer *p@ = pointer to a peer block
1597 * Returns: A pointer to the peer's address.
1600 extern const addr *p_addr(peer */*p*/);
1602 /* --- @p_init@ --- *
1604 * Arguments: @struct addrinfo *ailist@ = addresses to bind to
1608 * Use: Initializes the peer system; creates the socket.
1611 extern void p_init(struct addrinfo */*ailist*/);
1613 /* --- @p_port@ --- *
1615 * Arguments: @int i@ = address family index to retrieve
1617 * Returns: Port number used for socket.
1620 extern unsigned p_port(int /*i*/);
1622 /* --- @p_create@ --- *
1624 * Arguments: @peerspec *spec@ = information about this peer
1626 * Returns: Pointer to the peer block, or null if it failed.
1628 * Use: Creates a new named peer block. No peer is actually attached
1632 extern peer *p_create(peerspec */*spec*/);
1634 /* --- @p_name@ --- *
1636 * Arguments: @peer *p@ = pointer to a peer block
1638 * Returns: A pointer to the peer's name.
1640 * Use: Equivalent to @p_spec(p)->name@.
1643 extern const char *p_name(peer */*p*/);
1645 /* --- @p_tag@ --- *
1647 * Arguments: @peer *p@ = pointer to a peer block
1649 * Returns: A pointer to the peer's public key tag.
1652 extern const char *p_tag(peer */*p*/);
1654 /* --- @p_privtag@ --- *
1656 * Arguments: @peer *p@ = pointer to a peer block
1658 * Returns: A pointer to the peer's private key tag.
1661 extern const char *p_privtag(peer */*p*/);
1663 /* --- @p_spec@ --- *
1665 * Arguments: @peer *p@ = pointer to a peer block
1667 * Returns: Pointer to the peer's specification
1670 extern const peerspec *p_spec(peer */*p*/);
1672 /* --- @p_findbyaddr@ --- *
1674 * Arguments: @const addr *a@ = address to look up
1676 * Returns: Pointer to the peer block, or null if not found.
1678 * Use: Finds a peer by address.
1681 extern peer *p_findbyaddr(const addr */*a*/);
1683 /* --- @p_find@ --- *
1685 * Arguments: @const char *name@ = name to look up
1687 * Returns: Pointer to the peer block, or null if not found.
1689 * Use: Finds a peer by name.
1692 extern peer *p_find(const char */*name*/);
1694 /* --- @p_destroy@ --- *
1696 * Arguments: @peer *p@ = pointer to a peer
1700 * Use: Destroys a peer.
1703 extern void p_destroy(peer */*p*/);
1705 /* --- @FOREACH_PEER@ --- *
1707 * Arguments: @p@ = name to bind to each peer
1708 * @stuff@ = thing to do for each item
1710 * Use: Does something for each current peer.
1713 #define FOREACH_PEER(p, stuff) do { \
1716 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1719 /* --- @p_mkiter@ --- *
1721 * Arguments: @peer_iter *i@ = pointer to an iterator
1725 * Use: Initializes the iterator.
1728 extern void p_mkiter(peer_iter */*i*/);
1730 /* --- @p_next@ --- *
1732 * Arguments: @peer_iter *i@ = pointer to an iterator
1734 * Returns: Next peer, or null if at the end.
1736 * Use: Returns the next peer.
1739 extern peer *p_next(peer_iter */*i*/);
1741 /*----- Tunnel drivers ----------------------------------------------------*/
1744 extern const tunnel_ops tun_linux;
1748 extern const tunnel_ops tun_unet;
1752 extern const tunnel_ops tun_bsd;
1755 extern const tunnel_ops tun_slip;
1757 /*----- Other handy utilities ---------------------------------------------*/
1759 /* --- @timestr@ --- *
1761 * Arguments: @time_t t@ = a time to convert
1763 * Returns: A pointer to a textual representation of the time.
1765 * Use: Converts a time to a textual representation. Corrupts
1769 extern const char *timestr(time_t /*t*/);
1771 /* --- @mystrieq@ --- *
1773 * Arguments: @const char *x, *y@ = two strings
1775 * Returns: True if @x@ and @y are equal, up to case.
1778 extern int mystrieq(const char */*x*/, const char */*y*/);
1782 * Arguments: @int af@ = an address family code
1784 * Returns: The index of the address family's record in @aftab@, or @-1@.
1787 extern int afix(int af);
1789 /* --- @addrsz@ --- *
1791 * Arguments: @const addr *a@ = a network address
1793 * Returns: The size of the address, for passing into the sockets API.
1796 extern socklen_t addrsz(const addr */*a*/);
1798 /* --- @getport@, @setport@ --- *
1800 * Arguments: @addr *a@ = a network address
1801 * @unsigned port@ = port number to set
1805 * Use: Retrieves or sets the port number in an address structure.
1808 extern unsigned getport(addr */*a*/);
1809 extern void setport(addr */*a*/, unsigned /*port*/);
1811 /* --- @seq_reset@ --- *
1813 * Arguments: @seqwin *s@ = sequence-checking window
1817 * Use: Resets a sequence number window.
1820 extern void seq_reset(seqwin */*s*/);
1822 /* --- @seq_check@ --- *
1824 * Arguments: @seqwin *s@ = sequence-checking window
1825 * @uint32 q@ = sequence number to check
1826 * @const char *service@ = service to report message from
1828 * Returns: A @SEQ_@ code.
1830 * Use: Checks a sequence number against the window, updating things
1834 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1836 /*----- That's all, folks -------------------------------------------------*/