chiark / gitweb /
server/keymgmt.c: Fix warning message to match documentation.
[tripe] / server / tripe.h
1 /* -*-c-*-
2  *
3  * Main header file for TrIPE
4  *
5  * (c) 2001 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
12  * TrIPE 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  * TrIPE 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 TrIPE; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifndef TRIPE_H
28 #define TRIPE_H
29
30 #ifdef __cplusplus
31   extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stddef.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/stat.h>
55 #include <sys/wait.h>
56
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <netdb.h>
62
63 #include <pwd.h>
64 #include <grp.h>
65
66 #include <mLib/alloc.h>
67 #include <mLib/arena.h>
68 #include <mLib/base64.h>
69 #include <mLib/bres.h>
70 #include <mLib/daemonize.h>
71 #include <mLib/dstr.h>
72 #include <mLib/env.h>
73 #include <mLib/fdflags.h>
74 #include <mLib/fdpass.h>
75 #include <mLib/fwatch.h>
76 #include <mLib/hash.h>
77 #include <mLib/macros.h>
78 #include <mLib/mdup.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
82 #include <mLib/sel.h>
83 #include <mLib/selbuf.h>
84 #include <mLib/sig.h>
85 #include <mLib/str.h>
86 #include <mLib/sub.h>
87 #include <mLib/trace.h>
88 #include <mLib/tv.h>
89 #include <mLib/versioncmp.h>
90
91 #include <catacomb/buf.h>
92
93 #include <catacomb/gcipher.h>
94 #include <catacomb/gmac.h>
95 #include <catacomb/grand.h>
96 #include <catacomb/key.h>
97 #include <catacomb/paranoia.h>
98
99 #include <catacomb/noise.h>
100 #include <catacomb/rand.h>
101
102 #include <catacomb/mp.h>
103 #include <catacomb/mprand.h>
104 #include <catacomb/dh.h>
105 #include <catacomb/ec.h>
106 #include <catacomb/ec-keys.h>
107 #include <catacomb/group.h>
108
109 #include "priv.h"
110 #include "protocol.h"
111 #include "slip.h"
112 #include "util.h"
113
114 #undef sun
115
116 /*----- Magic numbers -----------------------------------------------------*/
117
118 /* --- Trace flags --- */
119
120 #define T_TUNNEL 1u
121 #define T_PEER 2u
122 #define T_PACKET 4u
123 #define T_ADMIN 8u
124 #define T_CRYPTO 16u
125 #define T_KEYSET 32u
126 #define T_KEYEXCH 64u
127 #define T_KEYMGMT 128u
128 #define T_CHAL 256u
129 /* T_PRIVSEP  in priv.h */
130
131 #define T_ALL 1023u
132
133 /* --- Units --- */
134
135 #define SEC(n) (n##u)
136 #define MIN(n) (n##u * 60u)
137 #define F_2P32 (65536.0*65536.0)
138 #define MEG(n) (n##ul * 1024ul * 1024ul)
139
140 /* --- Timing parameters --- */
141
142 #define T_EXP MIN(60)                   /* Expiry time for a key */
143 #define T_REGEN MIN(40)                 /* Regeneration time for a key */
144
145 #define T_VALID SEC(20)                 /* Challenge validity period */
146 #define T_RETRYMIN SEC(2)               /* Minimum retry interval */
147 #define T_RETRYMAX MIN(5)               /* Maximum retry interval */
148 #define T_RETRYGROW (5.0/4.0)           /* Retry interval growth factor */
149
150 #define T_WOBBLE (1.0/3.0)              /* Relative timer randomness */
151
152 /* --- Other things --- */
153
154 #define PKBUFSZ 65536
155
156 /*----- Cipher selections -------------------------------------------------*/
157
158 typedef struct algswitch {
159   const gccipher *c;                    /* Symmetric encryption scheme */
160   const gccipher *mgf;                  /* Mask-generation function */
161   const gchash *h;                      /* Hash function */
162   const gcmac *m;                       /* Message authentication code */
163   size_t hashsz;                        /* Hash output size */
164   size_t tagsz;                         /* Length to truncate MAC tags */
165   size_t expsz;                         /* Size of data to process */
166   size_t cksz, mksz;                    /* Key lengths for @c@ and @m@ */
167 } algswitch;
168
169 typedef struct kdata {
170   unsigned ref;                         /* Reference counter */
171   struct knode *kn;                     /* Pointer to cache entry */
172   char *tag;                            /* Full tag name of the key */
173   group *g;                             /* The group we work in */
174   size_t indexsz;                       /* Size of exponent for the group */
175   mp *kpriv;                            /* The private key (or null) */
176   ge *kpub;                             /* The public key */
177   time_t t_exp;                         /* Expiry time of the key */
178   algswitch algs;                       /* Collection of algorithms */
179 } kdata;
180
181 typedef struct knode {
182   sym_base _b;                          /* Symbol table intrusion */
183   unsigned f;                           /* Various flags */
184 #define KNF_BROKEN 1u                   /*   Don't use this key any more */
185   struct keyhalf *kh;                   /* Pointer to the home keyhalf */
186   kdata *kd;                            /* Pointer to the key data */
187 } knode;
188
189 #define MAXHASHSZ 64                    /* Largest possible hash size */
190
191 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
192
193 /*----- Data structures ---------------------------------------------------*/
194
195 /* --- Socket addresses --- *
196  *
197  * A magic union of supported socket addresses.
198  */
199
200 typedef union addr {
201   struct sockaddr sa;
202   struct sockaddr_in sin;
203 } addr;
204
205 /* --- Mapping keyed on addresses --- */
206
207 typedef struct addrmap {
208   hash_table t;
209   size_t load;
210 } addrmap;
211
212 typedef struct addrmap_base {
213   hash_base b;
214   addr a;
215 } addrmap_base;
216
217 /* --- Sequence number checking --- */
218
219 typedef struct seqwin {
220   uint32 seq;                           /* First acceptable input sequence */
221   uint32 win;                           /* Window of acceptable numbers */
222 } seqwin;
223
224 #define SEQ_WINSZ 32                    /* Bits in sequence number window */
225
226 /* --- A symmetric keyset --- *
227  *
228  * A keyset contains a set of symmetric keys for encrypting and decrypting
229  * packets.  Keysets are stored in a list, sorted in reverse order of
230  * creation, so that the most recent keyset (the one most likely to be used)
231  * is first.
232  *
233  * Each keyset has a time limit and a data limit.  The keyset is destroyed
234  * when either it has existed for too long, or it has been used to encrypt
235  * too much data.  New key exchanges are triggered when keys are close to
236  * expiry.
237  */
238
239 typedef struct keyset {
240   struct keyset *next;                  /* Next active keyset in the list */
241   unsigned ref;                         /* Reference count for keyset */
242   struct peer *p;                       /* Pointer to peer structure */
243   time_t t_exp;                         /* Expiry time for this keyset */
244   unsigned long sz_exp, sz_regen;       /* Data limits for the keyset */
245   T( unsigned seq; )                    /* Sequence number for tracing */
246   unsigned f;                           /* Various useful flags */
247   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
248   size_t tagsz;                         /* Length to truncate MAC tags */
249   gmac *min, *mout;                     /* Keyset MACs for integrity */
250   uint32 oseq;                          /* Outbound sequence number */
251   seqwin iseq;                          /* Inbound sequence number */
252 } keyset;
253
254 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
255 #define KSF_LINK 2u                     /* Key is in a linked list */
256
257 #define KSERR_REGEN -1                  /* Regenerate keys */
258 #define KSERR_NOKEYS -2                 /* No keys left */
259 #define KSERR_DECRYPT -3                /* Unable to decrypt message */
260 #define KSERR_SEQ -4                    /* Incorrect sequence number */
261 #define KSERR_MALFORMED -5              /* Input ciphertext is broken */
262
263 /* --- Key exchange --- *
264  *
265  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
266  * Protocol has a number of desirable features (e.g., perfect forward
267  * secrecy, and zero-knowledge authentication) which make it attractive for
268  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
269  * Clive Jones.
270  */
271
272 typedef struct retry {
273   double t;                             /* Current retry time */
274 } retry;
275
276 #define KX_NCHAL 16u
277
278 typedef struct kxchal {
279   struct keyexch *kx;                   /* Pointer back to key exchange */
280   ge *c;                                /* Responder's challenge */
281   ge *r;                                /* My reply to the challenge */
282   keyset *ks;                           /* Pointer to temporary keyset */
283   unsigned f;                           /* Various useful flags */
284   sel_timer t;                          /* Response timer for challenge */
285   retry rs;                             /* Retry state */
286   octet hc[MAXHASHSZ];                  /* Hash of his challenge */
287   octet ck[MAXHASHSZ];                  /* His magical check value */
288   octet hswrq_in[MAXHASHSZ];            /* Inbound switch request message */
289   octet hswok_in[MAXHASHSZ];            /* Inbound switch confirmation */
290   octet hswrq_out[MAXHASHSZ];           /* Outbound switch request message */
291   octet hswok_out[MAXHASHSZ];           /* Outbound switch confirmation */
292 } kxchal;
293
294 typedef struct keyexch {
295   struct peer *p;                       /* Pointer back to the peer */
296   kdata *kpriv;                         /* Private key and related info */
297   kdata *kpub;                          /* Peer's public key */
298   keyset **ks;                          /* Peer's list of keysets */
299   unsigned f;                           /* Various useful flags */
300   unsigned s;                           /* Current state in exchange */
301   sel_timer t;                          /* Timer for next exchange */
302   retry rs;                             /* Retry state */
303   mp *alpha;                            /* My temporary secret */
304   ge *c;                                /* My challenge */
305   ge *rx;                               /* The expected response */
306   unsigned nr;                          /* Number of extant responses */
307   time_t t_valid;                       /* When this exchange goes bad */
308   octet hc[MAXHASHSZ];                  /* Hash of my challenge */
309   kxchal *r[KX_NCHAL];                  /* Array of challenges */
310 } keyexch;
311
312 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
313 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
314 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
315 #define KXF_CORK 8u                     /* Don't send anything yet */
316
317 enum {
318   KXS_DEAD,                             /* Uninitialized state (magical) */
319   KXS_CHAL,                             /* Main answer-challenges state */
320   KXS_COMMIT,                           /* Committed: send switch request */
321   KXS_SWITCH                            /* Switched: send confirmation */
322 };
323
324 /* --- Tunnel structure --- *
325  *
326  * Used to maintain system-specific information about the tunnel interface.
327  */
328
329 typedef struct tunnel tunnel;
330 struct peer;
331
332 typedef struct tunnel_ops {
333   const char *name;                     /* Name of this tunnel driver */
334   unsigned flags;                       /* Various interesting flags */
335 #define TUNF_PRIVOPEN 1u                /*   Need helper to open file */
336   void (*init)(void);                   /* Initializes the system */
337   tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
338                                         /* Initializes a new tunnel */
339   void (*setifname)(tunnel */*t*/, const char */*ifn*/);
340                                         /*  Notifies ifname change */
341   void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
342   void (*destroy)(tunnel */*t*/);       /* Destroys a tunnel */
343 } tunnel_ops;
344
345 #ifndef TUN_INTERNALS
346 struct tunnel { const tunnel_ops *ops; };
347 #endif
348
349 /* --- Peer statistics --- *
350  *
351  * Contains various interesting and not-so-interesting statistics about a
352  * peer.  This is updated by various parts of the code.  The format of the
353  * structure isn't considered private, and @p_stats@ returns a pointer to the
354  * statistics block for a given peer.
355  */
356
357 typedef struct stats {
358   unsigned long sz_in, sz_out;          /* Size of all data in and out */
359   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
360   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
361   time_t t_start, t_last, t_kx;         /* Time peer created, last pk, kx */
362   unsigned long n_reject;               /* Number of rejected packets */
363   unsigned long n_in, n_out;            /* Number of packets in and out */
364   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
365   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
366 } stats;
367
368 /* --- Peer structure --- *
369  *
370  * The main structure which glues everything else together.
371  */
372
373 typedef struct peerspec {
374   char *name;                           /* Peer's name */
375   char *privtag;                        /* Private key tag */
376   char *tag;                            /* Public key tag */
377   const tunnel_ops *tops;               /* Tunnel operations */
378   unsigned long t_ka;                   /* Keep alive interval */
379   addr sa;                              /* Socket address to speak to */
380   size_t sasz;                          /* Socket address size */
381   unsigned f;                           /* Flags for the peer */
382 #define PSF_KXMASK 255u                 /*   Key-exchange flags to set */
383 #define PSF_MOBILE 256u                 /*   Address may change rapidly */
384 } peerspec;
385
386 typedef struct peer_byname {
387   sym_base _b;
388   struct peer *p;
389 } peer_byname;
390
391 typedef struct peer_byaddr {
392   addrmap_base _b;
393   struct peer *p;
394 } peer_byaddr;
395
396 typedef struct peer {
397   peer_byname *byname;                  /* Lookup-by-name block */
398   peer_byaddr *byaddr;                  /* Lookup-by-address block */
399   struct ping *pings;                   /* Pings we're waiting for */
400   peerspec spec;                        /* Specifications for this peer */
401   tunnel *t;                            /* Tunnel for local packets */
402   char *ifname;                         /* Interface name for tunnel */
403   keyset *ks;                           /* List head for keysets */
404   buf b;                                /* Buffer for sending packets */
405   stats st;                             /* Statistics */
406   keyexch kx;                           /* Key exchange protocol block */
407   sel_timer tka;                        /* Timer for keepalives */
408 } peer;
409
410 typedef struct peer_iter { sym_iter i; } peer_iter;
411
412 typedef struct ping {
413   struct ping *next, *prev;             /* Links to next and previous */
414   peer *p;                              /* Peer so we can free it */
415   unsigned msg;                         /* Kind of response expected */
416   uint32 id;                            /* Id so we can recognize response */
417   octet magic[32];                      /* Some random data */
418   sel_timer t;                          /* Timeout for ping */
419   void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
420   void *arg;                            /* Argument for callback */
421 } ping;
422
423 enum {
424   PING_NONOTIFY = -1,
425   PING_OK = 0,
426   PING_TIMEOUT,
427   PING_PEERDIED,
428   PING_MAX
429 };
430
431 /* --- Admin structure --- */
432
433 #define OBUFSZ 16384u
434
435 typedef struct obuf {
436   struct obuf *next;                    /* Next buffer in list */
437   char *p_in, *p_out;                   /* Pointers into the buffer */
438   char buf[OBUFSZ];                     /* The actual buffer */
439 } obuf;
440
441 typedef struct oqueue {
442   obuf *hd, *tl;                        /* Head and tail pointers */
443 } oqueue;
444
445 struct admin;
446
447 typedef struct admin_bgop {
448   struct admin_bgop *next, *prev;       /* Links to next and previous */
449   struct admin *a;                      /* Owner job */
450   char *tag;                            /* Tag string for messages */
451   void (*cancel)(struct admin_bgop *);  /* Destructor function */
452 } admin_bgop;
453
454 typedef struct admin_resop {
455   admin_bgop bg;                        /* Background operation header */
456   char *addr;                           /* Hostname to be resolved */
457   bres_client r;                        /* Background resolver task */
458   sel_timer t;                          /* Timer for resolver */
459   addr sa;                              /* Socket address */
460   size_t sasz;                          /* Socket address size */
461   void (*func)(struct admin_resop *, int); /* Handler */
462 } admin_resop;
463
464 enum { ARES_OK, ARES_FAIL };
465
466 typedef struct admin_addop {
467   admin_resop r;                        /* Name resolution header */
468   peerspec peer;                        /* Peer pending creation */
469 } admin_addop;
470
471 typedef struct admin_pingop {
472   admin_bgop bg;                        /* Background operation header */
473   ping ping;                            /* Ping pending response */
474   struct timeval pingtime;              /* Time last ping was sent */
475 } admin_pingop;
476
477 typedef struct admin_service {
478   sym_base _b;                          /* Hash table base structure */
479   char *version;                        /* The provided version */
480   struct admin *prov;                   /* Which client provides me */
481   struct admin_service *next, *prev;    /* Client's list of services */
482 } admin_service;
483
484 typedef struct admin_svcop {
485   admin_bgop bg;                        /* Background operation header */
486   struct admin *prov;                   /* Client servicing this job */
487   unsigned index;                       /* This job's index */
488   struct admin_svcop *next, *prev;      /* Links for provider's jobs */
489 } admin_svcop;
490
491 typedef struct admin_jobentry {
492   unsigned short seq;                   /* Zero if unused */
493   union {
494     admin_svcop *op;                    /* Operation, if slot in use, ... */
495     uint32 next;                        /* ... or index of next free slot */
496   } u;
497 } admin_jobentry;
498
499 typedef struct admin_jobtable {
500   uint32 n, sz;                         /* Used slots and table size */
501   admin_svcop *active;                  /* List of active jobs */
502   uint32 free;                          /* Index of first free slot */
503   admin_jobentry *v;                    /* And the big array of entries */
504 } admin_jobtable;
505
506 typedef struct admin {
507   struct admin *next, *prev;            /* Links to next and previous */
508   unsigned f;                           /* Various useful flags */
509   unsigned ref;                         /* Reference counter */
510 #ifndef NTRACE
511   unsigned seq;                         /* Sequence number for tracing */
512 #endif
513   oqueue out;                           /* Output buffer list */
514   oqueue delay;                         /* Delayed output buffer list */
515   admin_bgop *bg;                       /* Backgrounded operations */
516   admin_service *svcs;                  /* Which services I provide */
517   admin_jobtable j;                     /* Table of outstanding jobs */
518   selbuf b;                             /* Line buffer for commands */
519   sel_file w;                           /* Selector for write buffering */
520 } admin;
521
522 #define AF_DEAD 1u                      /* Destroy this admin block */
523 #define AF_CLOSE 2u                     /* Client closed connection */
524 #define AF_NOTE 4u                      /* Catch notifications */
525 #define AF_WARN 8u                      /* Catch warning messages */
526 #ifndef NTRACE
527   #define AF_TRACE 16u                  /* Catch tracing */
528 #endif
529 #define AF_FOREGROUND 32u               /* Quit server when client closes */
530
531 #ifndef NTRACE
532 #  define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
533 #else
534 #  define AF_ALLMSGS (AF_NOTE | AF_WARN)
535 #endif
536
537 /*----- Global variables --------------------------------------------------*/
538
539 extern sel_state sel;                   /* Global I/O event state */
540 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
541 extern const tunnel_ops *tunnels[];     /* Table of tunnels (0-term) */
542 extern const tunnel_ops *tun_default;   /* Default tunnel to use */
543 extern kdata *master;                   /* Default private key */
544 extern const char *tag_priv;            /* Default private key tag */
545
546 #ifndef NTRACE
547 extern const trace_opt tr_opts[];       /* Trace options array */
548 extern unsigned tr_flags;               /* Trace options flags */
549 #endif
550
551 /*----- Other macros ------------------------------------------------------*/
552
553 #define TIMER noise_timer(RAND_GLOBAL)
554
555 /*----- Key management ----------------------------------------------------*/
556
557 /* --- @km_init@ --- *
558  *
559  * Arguments:   @const char *privkr@ = private keyring file
560  *              @const char *pubkr@ = public keyring file
561  *              @const char *ptag@ = default private-key tag
562  *
563  * Returns:     ---
564  *
565  * Use:         Initializes the key-management machinery, loading the
566  *              keyrings and so on.
567  */
568
569 extern void km_init(const char */*privkr*/, const char */*pubkr*/,
570                     const char */*ptag*/);
571
572 /* --- @km_reload@ --- *
573  *
574  * Arguments:   ---
575  *
576  * Returns:     Zero if OK, nonzero to force reloading of keys.
577  *
578  * Use:         Checks the keyrings to see if they need reloading.
579  */
580
581 extern int km_reload(void);
582
583 /* --- @km_findpub@, @km_findpriv@ --- *
584  *
585  * Arguments:   @const char *tag@ = key tag to load
586  *
587  * Returns:     Pointer to the kdata object if successful, or null on error.
588  *
589  * Use:         Fetches a public or private key from the keyring.
590  */
591
592 extern kdata *km_findpub(const char */*tag*/);
593 extern kdata *km_findpriv(const char */*tag*/);
594
595 /* --- @km_samealgsp@ --- *
596  *
597  * Arguments:   @const kdata *kdx, *kdy@ = two key data objects
598  *
599  * Returns:     Nonzero if their two algorithm selections are the same.
600  *
601  * Use:         Checks sameness of algorithm selections: used to ensure that
602  *              peers are using sensible algorithms.
603  */
604
605 extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
606
607 /* --- @km_ref@ --- *
608  *
609  * Arguments:   @kdata *kd@ = pointer to the kdata object
610  *
611  * Returns:     ---
612  *
613  * Use:         Claim a new reference to a kdata object.
614  */
615
616 extern void km_ref(kdata */*kd*/);
617
618 /* --- @km_unref@ --- *
619  *
620  * Arguments:   @kdata *kd@ = pointer to the kdata object
621  *
622  * Returns:     ---
623  *
624  * Use:         Releases a reference to a kdata object.
625  */
626
627 extern void km_unref(kdata */*kd*/);
628
629 /* --- @km_tag@ --- *
630  *
631  * Arguments:   @kdata *kd@ - pointer to the kdata object
632  *
633  * Returns:     A pointer to the short tag by which the kdata was loaded.
634  */
635
636 extern const char *km_tag(kdata */*kd*/);
637
638 /*----- Key exchange ------------------------------------------------------*/
639
640 /* --- @kx_start@ --- *
641  *
642  * Arguments:   @keyexch *kx@ = pointer to key exchange context
643  *              @int forcep@ = nonzero to ignore the quiet timer
644  *
645  * Returns:     ---
646  *
647  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
648  *              a new challenge is sent (unless the quiet timer forbids
649  *              this); if no exchange is in progress, one is commenced.
650  */
651
652 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
653
654 /* --- @kx_message@ --- *
655  *
656  * Arguments:   @keyexch *kx@ = pointer to key exchange context
657  *              @unsigned msg@ = the message code
658  *              @buf *b@ = pointer to buffer containing the packet
659  *
660  * Returns:     ---
661  *
662  * Use:         Reads a packet containing key exchange messages and handles
663  *              it.
664  */
665
666 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
667
668 /* --- @kx_free@ --- *
669  *
670  * Arguments:   @keyexch *kx@ = pointer to key exchange context
671  *
672  * Returns:     ---
673  *
674  * Use:         Frees everything in a key exchange context.
675  */
676
677 extern void kx_free(keyexch */*kx*/);
678
679 /* --- @kx_newkeys@ --- *
680  *
681  * Arguments:   @keyexch *kx@ = pointer to key exchange context
682  *
683  * Returns:     ---
684  *
685  * Use:         Informs the key exchange module that its keys may have
686  *              changed.  If fetching the new keys fails, the peer will be
687  *              destroyed, we log messages and struggle along with the old
688  *              keys.
689  */
690
691 extern void kx_newkeys(keyexch */*kx*/);
692
693 /* --- @kx_init@ --- *
694  *
695  * Arguments:   @keyexch *kx@ = pointer to key exchange context
696  *              @peer *p@ = pointer to peer context
697  *              @keyset **ks@ = pointer to keyset list
698  *              @unsigned f@ = various useful flags
699  *
700  * Returns:     Zero if OK, nonzero if it failed.
701  *
702  * Use:         Initializes a key exchange module.  The module currently
703  *              contains no keys, and will attempt to initiate a key
704  *              exchange.
705  */
706
707 extern int kx_init(keyexch */*kx*/, peer */*p*/,
708                    keyset **/*ks*/, unsigned /*f*/);
709
710 /*----- Keysets and symmetric cryptography --------------------------------*/
711
712 /* --- @ks_drop@ --- *
713  *
714  * Arguments:   @keyset *ks@ = pointer to a keyset
715  *
716  * Returns:     ---
717  *
718  * Use:         Decrements a keyset's reference counter.  If the counter hits
719  *              zero, the keyset is freed.
720  */
721
722 extern void ks_drop(keyset */*ks*/);
723
724 /* --- @ks_gen@ --- *
725  *
726  * Arguments:   @const void *k@ = pointer to key material
727  *              @size_t x, y, z@ = offsets into key material (see below)
728  *              @peer *p@ = pointer to peer information
729  *
730  * Returns:     A pointer to the new keyset.
731  *
732  * Use:         Derives a new keyset from the given key material.  The
733  *              offsets @x@, @y@ and @z@ separate the key material into three
734  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
735  *              the key material; between @k + x@ and @k + y@ is `your'
736  *              contribution; and between @k + y@ and @k + z@ is a shared
737  *              value we made together.  These are used to construct two
738  *              pairs of symmetric keys.  Each pair consists of an encryption
739  *              key and a message authentication key.  One pair is used for
740  *              outgoing messages, the other for incoming messages.
741  *
742  *              The new key is marked so that it won't be selected for output
743  *              by @ksl_encrypt@.  You can still encrypt data with it by
744  *              calling @ks_encrypt@ directly.
745  */
746
747 extern keyset *ks_gen(const void */*k*/,
748                       size_t /*x*/, size_t /*y*/, size_t /*z*/,
749                       peer */*p*/);
750
751 /* --- @ks_activate@ --- *
752  *
753  * Arguments:   @keyset *ks@ = pointer to a keyset
754  *
755  * Returns:     ---
756  *
757  * Use:         Activates a keyset, so that it can be used for encrypting
758  *              outgoing messages.
759  */
760
761 extern void ks_activate(keyset */*ks*/);
762
763 /* --- @ks_encrypt@ --- *
764  *
765  * Arguments:   @keyset *ks@ = pointer to a keyset
766  *              @unsigned ty@ = message type
767  *              @buf *b@ = pointer to input buffer
768  *              @buf *bb@ = pointer to output buffer
769  *
770  * Returns:     Zero if successful; @KSERR_REGEN@ if we should negotiate a
771  *              new key; @KSERR_NOKEYS@ if the key is not usable.  Also
772  *              returns zero if there was insufficient buffer (but the output
773  *              buffer is broken in this case).
774  *
775  * Use:         Encrypts a block of data using the key.  Note that the `key
776  *              ought to be replaced' notification is only ever given once
777  *              for each key.  Also note that this call forces a keyset to be
778  *              used even if it's marked as not for data output.
779  */
780
781 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
782                       buf */*b*/, buf */*bb*/);
783
784 /* --- @ks_decrypt@ --- *
785  *
786  * Arguments:   @keyset *ks@ = pointer to a keyset
787  *              @unsigned ty@ = expected type code
788  *              @buf *b@ = pointer to an input buffer
789  *              @buf *bb@ = pointer to an output buffer
790  *
791  * Returns:     Zero on success; @KSERR_DECRYPT@ on failure.  Also returns
792  *              zero if there was insufficient buffer (but the output buffer
793  *              is broken in this case).
794  *
795  * Use:         Attempts to decrypt a message using a given key.  Note that
796  *              requesting decryption with a key directly won't clear a
797  *              marking that it's not for encryption.
798  */
799
800 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
801                       buf */*b*/, buf */*bb*/);
802
803 /* --- @ksl_free@ --- *
804  *
805  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
806  *
807  * Returns:     ---
808  *
809  * Use:         Frees (releases references to) all of the keys in a keyset.
810  */
811
812 extern void ksl_free(keyset **/*ksroot*/);
813
814 /* --- @ksl_link@ --- *
815  *
816  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
817  *              @keyset *ks@ = pointer to a keyset
818  *
819  * Returns:     ---
820  *
821  * Use:         Links a keyset into a list.  A keyset can only be on one list
822  *              at a time.  Bad things happen otherwise.
823  */
824
825 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
826
827 /* --- @ksl_prune@ --- *
828  *
829  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
830  *
831  * Returns:     ---
832  *
833  * Use:         Prunes the keyset list by removing keys which mustn't be used
834  *              any more.
835  */
836
837 extern void ksl_prune(keyset **/*ksroot*/);
838
839 /* --- @ksl_encrypt@ --- *
840  *
841  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
842  *              @unsigned ty@ = message type
843  *              @buf *b@ = pointer to input buffer
844  *              @buf *bb@ = pointer to output buffer
845  *
846  * Returns:     Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
847  *              new key; @KSERR_NOKEYS@ if there are no suitable keys
848  *              available.  Also returns zero if there was insufficient
849  *              buffer space (but the output buffer is broken in this case).
850  *
851  * Use:         Encrypts a packet.
852  */
853
854 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
855                        buf */*b*/, buf */*bb*/);
856
857 /* --- @ksl_decrypt@ --- *
858  *
859  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
860  *              @unsigned ty@ = expected type code
861  *              @buf *b@ = pointer to input buffer
862  *              @buf *bb@ = pointer to output buffer
863  *
864  * Returns:     Zero on success; @KSERR_DECRYPT@ on failure.  Also returns
865  *              zero if there was insufficient buffer (but the output buffer
866  *              is broken in this case).
867  *
868  * Use:         Decrypts a packet.
869  */
870
871 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
872                        buf */*b*/, buf */*bb*/);
873
874 /*----- Challenges --------------------------------------------------------*/
875
876 /* --- @c_new@ --- *
877  *
878  * Arguments:   @buf *b@ = where to put the challenge
879  *
880  * Returns:     Zero if OK, nonzero on error.
881  *
882  * Use:         Issues a new challenge.
883  */
884
885 extern int c_new(buf */*b*/);
886
887 /* --- @c_check@ --- *
888  *
889  * Arguments:   @buf *b@ = where to find the challenge
890  *
891  * Returns:     Zero if OK, nonzero if it didn't work.
892  *
893  * Use:         Checks a challenge.  On failure, the buffer is broken.
894  */
895
896 extern int c_check(buf */*b*/);
897
898 /*----- Administration interface ------------------------------------------*/
899
900 #define A_END ((char *)0)
901
902 /* --- @a_vformat@ --- *
903  *
904  * Arguments:   @dstr *d@ = where to leave the formatted message
905  *              @const char *fmt@ = pointer to format string
906  *              @va_list ap@ = arguments in list
907  *
908  * Returns:     ---
909  *
910  * Use:         Main message token formatting driver.  The arguments are
911  *              interleaved formatting tokens and their parameters, finally
912  *              terminated by an entry @A_END@.
913  *
914  *              Tokens recognized:
915  *
916  *                * "*..." ... -- pretokenized @dstr_putf@-like string
917  *
918  *                * "?ADDR" SOCKADDR -- a socket address, to be converted
919  *
920  *                * "?B64" BUFFER SIZE -- binary data to be base64-encoded
921  *
922  *                * "?TOKENS" VECTOR -- null-terminated vector of tokens
923  *
924  *                * "?PEER" PEER -- peer's name
925  *
926  *                * "?ERRNO" ERRNO -- system error code
927  *
928  *                * "[!]..." ... -- @dstr_putf@-like string as single token
929  */
930
931 extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list /*ap*/);
932
933 /* --- @a_format@ --- *
934  *
935  * Arguments:   @dstr *d@ = where to leave the formatted message
936  *              @const char *fmt@ = pointer to format string
937  *
938  * Returns:     ---
939  *
940  * Use:         Writes a tokenized message into a string, for later
941  *              presentation.
942  */
943
944 extern void a_format(dstr */*d*/, const char */*fmt*/, ...);
945
946 /* --- @a_warn@ --- *
947  *
948  * Arguments:   @const char *fmt@ = pointer to format string
949  *              @...@ = other arguments
950  *
951  * Returns:     ---
952  *
953  * Use:         Informs all admin connections of a warning.
954  */
955
956 extern void a_warn(const char */*fmt*/, ...);
957
958 /* --- @a_notify@ --- *
959  *
960  * Arguments:   @const char *fmt@ = pointer to format string
961  *              @...@ = other arguments
962  *
963  * Returns:     ---
964  *
965  * Use:         Sends a notification to interested admin connections.
966  */
967
968 extern void a_notify(const char */*fmt*/, ...);
969
970 /* --- @a_create@ --- *
971  *
972  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
973  *              @unsigned f@ = initial flags to set
974  *
975  * Returns:     ---
976  *
977  * Use:         Creates a new admin connection.
978  */
979
980 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
981
982 /* --- @a_quit@ --- *
983  *
984  * Arguments:   ---
985  *
986  * Returns:     ---
987  *
988  * Use:         Shuts things down nicely.
989  */
990
991 extern void a_quit(void);
992
993 /* --- @a_preselect@ --- *
994  *
995  * Arguments:   ---
996  *
997  * Returns:     ---
998  *
999  * Use:         Informs the admin module that we're about to select again,
1000  *              and that it should do cleanup things it has delayed until a
1001  *              `safe' time.
1002  */
1003
1004 extern void a_preselect(void);
1005
1006 /* --- @a_daemon@ --- *
1007  *
1008  * Arguments:   ---
1009  *
1010  * Returns:     ---
1011  *
1012  * Use:         Informs the admin module that it's a daemon.
1013  */
1014
1015 extern void a_daemon(void);
1016
1017 /* --- @a_init@ --- *
1018  *
1019  * Arguments:   @const char *sock@ = socket name to create
1020  *              @uid_t u@ = user to own the socket
1021  *              @gid_t g@ = group to own the socket
1022  *              @mode_t m@ = permissions to set on the socket
1023  *
1024  * Returns:     ---
1025  *
1026  * Use:         Creates the admin listening socket.
1027  */
1028
1029 extern void a_init(const char */*sock*/,
1030                    uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
1031
1032 /*----- Mapping with addresses as keys ------------------------------------*/
1033
1034 /* --- @am_create@ --- *
1035  *
1036  * Arguments:   @addrmap *m@ = pointer to map
1037  *
1038  * Returns:     ---
1039  *
1040  * Use:         Create an address map, properly set up.
1041  */
1042
1043 extern void am_create(addrmap */*m*/);
1044
1045 /* --- @am_destroy@ --- *
1046  *
1047  * Arguments:   @addrmap *m@ = pointer to map
1048  *
1049  * Returns:     ---
1050  *
1051  * Use:         Destroy an address map, throwing away all the entries.
1052  */
1053
1054 extern void am_destroy(addrmap */*m*/);
1055
1056 /* --- @am_find@ --- *
1057  *
1058  * Arguments:   @addrmap *m@ = pointer to map
1059  *              @const addr *a@ = address to look up
1060  *              @size_t sz@ = size of block to allocate
1061  *              @unsigned *f@ = where to store flags
1062  *
1063  * Returns:     Pointer to found item, or null.
1064  *
1065  * Use:         Finds a record with the given IP address, set @*f@ nonzero
1066  *              and returns it.  If @sz@ is zero, and no match was found,
1067  *              return null; otherwise allocate a new block of @sz@ bytes,
1068  *              clear @*f@ to zero and return the block pointer.
1069  */
1070
1071 extern void *am_find(addrmap */*m*/, const addr */*a*/,
1072                      size_t /*sz*/, unsigned */*f*/);
1073
1074 /* --- @am_remove@ --- *
1075  *
1076  * Arguments:   @addrmap *m@ = pointer to map
1077  *              @void *i@ = pointer to the item
1078  *
1079  * Returns:     ---
1080  *
1081  * Use:         Removes an item from the map.
1082  */
1083
1084 extern void am_remove(addrmap */*m*/, void */*i*/);
1085
1086 /*----- Privilege separation ----------------------------------------------*/
1087
1088 /* --- @ps_trace@ --- *
1089  *
1090  * Arguments:   @unsigned mask@ = trace mask to check
1091  *              @const char *fmt@ = message format
1092  *              @...@ = values for placeholders
1093  *
1094  * Returns:     ---
1095  *
1096  * Use:         Writes a trace message.
1097  */
1098
1099 T( extern void ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1100
1101 /* --- @ps_warn@ --- *
1102  *
1103  * Arguments:   @const char *fmt@ = message format
1104  *              @...@ = values for placeholders
1105  *
1106  * Returns:     ---
1107  *
1108  * Use:         Writes a warning message.
1109  */
1110
1111 extern void ps_warn(const char */*fmt*/, ...);
1112
1113 /* --- @ps_tunfd@ --- *
1114  *
1115  * Arguments:   @const tunnel_ops *tops@ = pointer to tunnel operations
1116  *              @char **ifn@ = where to put the interface name
1117  *
1118  * Returns:     The file descriptor, or @-1@ on error.
1119  *
1120  * Use:         Fetches a file descriptor for a tunnel driver.
1121  */
1122
1123 extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1124
1125 /* --- @ps_split@ --- *
1126  *
1127  * Arguments:   @int detachp@ = whether to detach the child from its terminal
1128  *
1129  * Returns:     ---
1130  *
1131  * Use:         Separates off the privileged tunnel-opening service from the
1132  *              rest of the server.
1133  */
1134
1135 extern void ps_split(int /*detachp*/);
1136
1137 /* --- @ps_quit@ --- *
1138  *
1139  * Arguments:   ---
1140  *
1141  * Returns:     ---
1142  *
1143  * Use:         Detaches from the helper process.
1144  */
1145
1146 extern void ps_quit(void);
1147
1148 /*----- Peer management ---------------------------------------------------*/
1149
1150 /* --- @p_txstart@ --- *
1151  *
1152  * Arguments:   @peer *p@ = pointer to peer block
1153  *              @unsigned msg@ = message type code
1154  *
1155  * Returns:     A pointer to a buffer to write to.
1156  *
1157  * Use:         Starts sending to a peer.  Only one send can happen at a
1158  *              time.
1159  */
1160
1161 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1162
1163 /* --- @p_txend@ --- *
1164  *
1165  * Arguments:   @peer *p@ = pointer to peer block
1166  *
1167  * Returns:     ---
1168  *
1169  * Use:         Sends a packet to the peer.
1170  */
1171
1172 extern void p_txend(peer */*p*/);
1173
1174 /* --- @p_pingsend@ --- *
1175  *
1176  * Arguments:   @peer *p@ = destination peer
1177  *              @ping *pg@ = structure to fill in
1178  *              @unsigned type@ = message type
1179  *              @unsigned long timeout@ = how long to wait before giving up
1180  *              @void (*func)(int, void *)@ = callback function
1181  *              @void *arg@ = argument for callback
1182  *
1183  * Returns:     Zero if successful, nonzero if it failed.
1184  *
1185  * Use:         Sends a ping to a peer.  Call @func@ with a nonzero argument
1186  *              if we get an answer within the timeout, or zero if no answer.
1187  */
1188
1189 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1190                       unsigned long /*timeout*/,
1191                       void (*/*func*/)(int, void *), void */*arg*/);
1192
1193 /* --- @p_pingdone@ --- *
1194  *
1195  * Arguments:   @ping *p@ = ping structure
1196  *              @int rc@ = return code to pass on
1197  *
1198  * Returns:     ---
1199  *
1200  * Use:         Disposes of a ping structure, maybe sending a notification.
1201  */
1202
1203 extern void p_pingdone(ping */*p*/, int /*rc*/);
1204
1205 /* --- @p_greet@ --- *
1206  *
1207  * Arguments:   @peer *p@ = peer to send to
1208  *              @const void *c@ = pointer to challenge
1209  *              @size_t sz@ = size of challenge
1210  *
1211  * Returns:     ---
1212  *
1213  * Use:         Sends a greeting packet.
1214  */
1215
1216 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1217
1218 /* --- @p_tun@ --- *
1219  *
1220  * Arguments:   @peer *p@ = pointer to peer block
1221  *              @buf *b@ = buffer containing incoming packet
1222  *
1223  * Returns:     ---
1224  *
1225  * Use:         Handles a packet which needs to be sent to a peer.
1226  */
1227
1228 extern void p_tun(peer */*p*/, buf */*b*/);
1229
1230 /* --- @p_keyreload@ --- *
1231  *
1232  * Arguments:   ---
1233  *
1234  * Returns:     ---
1235  *
1236  * Use:         Forces a check of the daemon's keyring files.
1237  */
1238
1239 extern void p_keyreload(void);
1240
1241 /* --- @p_interval@ --- *
1242  *
1243  * Arguments:   ---
1244  *
1245  * Returns:     ---
1246  *
1247  * Use:         Called periodically to do tidying.
1248  */
1249
1250 extern void p_interval(void);
1251
1252 /* --- @p_stats@ --- *
1253  *
1254  * Arguments:   @peer *p@ = pointer to a peer block
1255  *
1256  * Returns:     A pointer to the peer's statistics.
1257  */
1258
1259 extern stats *p_stats(peer */*p*/);
1260
1261 /* --- @p_ifname@ --- *
1262  *
1263  * Arguments:   @peer *p@ = pointer to a peer block
1264  *
1265  * Returns:     A pointer to the peer's interface name.
1266  */
1267
1268 extern const char *p_ifname(peer */*p*/);
1269
1270 /* --- @p_setifname@ --- *
1271  *
1272  * Arguments:   @peer *p@ = pointer to a peer block
1273  *              @const char *name@ = pointer to the new name
1274  *
1275  * Returns:     ---
1276  *
1277  * Use:         Changes the name held for a peer's interface.
1278  */
1279
1280 extern void p_setifname(peer */*p*/, const char */*name*/);
1281
1282 /* --- @p_addr@ --- *
1283  *
1284  * Arguments:   @peer *p@ = pointer to a peer block
1285  *
1286  * Returns:     A pointer to the peer's address.
1287  */
1288
1289 extern const addr *p_addr(peer */*p*/);
1290
1291 /* --- @p_init@ --- *
1292  *
1293  * Arguments:   @struct in_addr addr@ = address to bind to
1294  *              @unsigned port@ = port number to listen to
1295  *
1296  * Returns:     ---
1297  *
1298  * Use:         Initializes the peer system; creates the socket.
1299  */
1300
1301 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
1302
1303 /* --- @p_port@ --- *
1304  *
1305  * Arguments:   ---
1306  *
1307  * Returns:     Port number used for socket.
1308  */
1309
1310 unsigned p_port(void);
1311
1312 /* --- @p_create@ --- *
1313  *
1314  * Arguments:   @peerspec *spec@ = information about this peer
1315  *
1316  * Returns:     Pointer to the peer block, or null if it failed.
1317  *
1318  * Use:         Creates a new named peer block.  No peer is actually attached
1319  *              by this point.
1320  */
1321
1322 extern peer *p_create(peerspec */*spec*/);
1323
1324 /* --- @p_name@ --- *
1325  *
1326  * Arguments:   @peer *p@ = pointer to a peer block
1327  *
1328  * Returns:     A pointer to the peer's name.
1329  *
1330  * Use:         Equivalent to @p_spec(p)->name@.
1331  */
1332
1333 extern const char *p_name(peer */*p*/);
1334
1335 /* --- @p_tag@ --- *
1336  *
1337  * Arguments:   @peer *p@ = pointer to a peer block
1338  *
1339  * Returns:     A pointer to the peer's public key tag.
1340  */
1341
1342 extern const char *p_tag(peer */*p*/);
1343
1344 /* --- @p_privtag@ --- *
1345  *
1346  * Arguments:   @peer *p@ = pointer to a peer block
1347  *
1348  * Returns:     A pointer to the peer's private key tag.
1349  */
1350
1351 extern const char *p_privtag(peer */*p*/);
1352
1353 /* --- @p_spec@ --- *
1354  *
1355  * Arguments:   @peer *p@ = pointer to a peer block
1356  *
1357  * Returns:     Pointer to the peer's specification
1358  */
1359
1360 extern const peerspec *p_spec(peer */*p*/);
1361
1362 /* --- @p_findbyaddr@ --- *
1363  *
1364  * Arguments:   @const addr *a@ = address to look up
1365  *
1366  * Returns:     Pointer to the peer block, or null if not found.
1367  *
1368  * Use:         Finds a peer by address.
1369  */
1370
1371 extern peer *p_findbyaddr(const addr */*a*/);
1372
1373 /* --- @p_find@ --- *
1374  *
1375  * Arguments:   @const char *name@ = name to look up
1376  *
1377  * Returns:     Pointer to the peer block, or null if not found.
1378  *
1379  * Use:         Finds a peer by name.
1380  */
1381
1382 extern peer *p_find(const char */*name*/);
1383
1384 /* --- @p_destroy@ --- *
1385  *
1386  * Arguments:   @peer *p@ = pointer to a peer
1387  *
1388  * Returns:     ---
1389  *
1390  * Use:         Destroys a peer.
1391  */
1392
1393 extern void p_destroy(peer */*p*/);
1394
1395 /* --- @FOREACH_PEER@ --- *
1396  *
1397  * Arguments:   @p@ = name to bind to each peer
1398  *              @stuff@ = thing to do for each item
1399  *
1400  * Use:         Does something for each current peer.
1401  */
1402
1403 #define FOREACH_PEER(p, stuff) do {                                     \
1404   peer_iter i_;                                                         \
1405   peer *p;                                                              \
1406   for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff                   \
1407 } while (0)
1408
1409 /* --- @p_mkiter@ --- *
1410  *
1411  * Arguments:   @peer_iter *i@ = pointer to an iterator
1412  *
1413  * Returns:     ---
1414  *
1415  * Use:         Initializes the iterator.
1416  */
1417
1418 extern void p_mkiter(peer_iter */*i*/);
1419
1420 /* --- @p_next@ --- *
1421  *
1422  * Arguments:   @peer_iter *i@ = pointer to an iterator
1423  *
1424  * Returns:     Next peer, or null if at the end.
1425  *
1426  * Use:         Returns the next peer.
1427  */
1428
1429 extern peer *p_next(peer_iter */*i*/);
1430
1431 /*----- Tunnel drivers ----------------------------------------------------*/
1432
1433 #ifdef TUN_LINUX
1434   extern const tunnel_ops tun_linux;
1435 #endif
1436
1437 #ifdef TUN_UNET
1438   extern const tunnel_ops tun_unet;
1439 #endif
1440
1441 #ifdef TUN_BSD
1442   extern const tunnel_ops tun_bsd;
1443 #endif
1444
1445 extern const tunnel_ops tun_slip;
1446
1447 /*----- Other handy utilities ---------------------------------------------*/
1448
1449 /* --- @mpstr@ --- *
1450  *
1451  * Arguments:   @mp *m@ = a multiprecision integer
1452  *
1453  * Returns:     A pointer to the integer's textual representation.
1454  *
1455  * Use:         Converts a multiprecision integer to a string.  Corrupts
1456  *              @buf_u@.
1457  */
1458
1459 extern const char *mpstr(mp */*m*/);
1460
1461 /* --- @gestr@ --- *
1462  *
1463  * Arguments:   @group *g@ = a group
1464  *              @ge *x@ = a group element
1465  *
1466  * Returns:     A pointer to the element's textual representation.
1467  *
1468  * Use:         Converts a group element to a string.  Corrupts
1469  *              @buf_u@.
1470  */
1471
1472 extern const char *gestr(group */*g*/, ge */*x*/);
1473
1474 /* --- @timestr@ --- *
1475  *
1476  * Arguments:   @time_t t@ = a time to convert
1477  *
1478  * Returns:     A pointer to a textual representation of the time.
1479  *
1480  * Use:         Converts a time to a textual representation.  Corrupts
1481  *              @buf_u@.
1482  */
1483
1484 extern const char *timestr(time_t /*t*/);
1485
1486 /* --- @mystrieq@ --- *
1487  *
1488  * Arguments:   @const char *x, *y@ = two strings
1489  *
1490  * Returns:     True if @x@ and @y are equal, up to case.
1491  */
1492
1493 extern int mystrieq(const char */*x*/, const char */*y*/);
1494
1495 /* --- @seq_reset@ --- *
1496  *
1497  * Arguments:   @seqwin *s@ = sequence-checking window
1498  *
1499  * Returns:     ---
1500  *
1501  * Use:         Resets a sequence number window.
1502  */
1503
1504 extern void seq_reset(seqwin */*s*/);
1505
1506 /* --- @seq_check@ --- *
1507  *
1508  * Arguments:   @seqwin *s@ = sequence-checking window
1509  *              @uint32 q@ = sequence number to check
1510  *              @const char *service@ = service to report message from
1511  *
1512  * Returns:     A @SEQ_@ code.
1513  *
1514  * Use:         Checks a sequence number against the window, updating things
1515  *              as necessary.
1516  */
1517
1518 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1519
1520 /*----- That's all, folks -------------------------------------------------*/
1521
1522 #ifdef __cplusplus
1523   }
1524 #endif
1525
1526 #endif