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