chiark / gitweb /
Expunge CVS cruft.
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.20 2004/04/18 18:08:11 mdw Exp $
4  *
5  * Main header file for TrIPE
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #ifndef TRIPE_H
30 #define TRIPE_H
31
32 #ifdef __cplusplus
33   extern "C" {
34 #endif
35
36 /*----- Header files ------------------------------------------------------*/
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50
51 #include <sys/types.h>
52 #include <sys/time.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <sys/stat.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/bres.h>
69 #include <mLib/dstr.h>
70 #include <mLib/env.h>
71 #include <mLib/fdflags.h>
72 #include <mLib/fwatch.h>
73 #include <mLib/mdwopt.h>
74 #include <mLib/quis.h>
75 #include <mLib/report.h>
76 #include <mLib/sel.h>
77 #include <mLib/selbuf.h>
78 #include <mLib/sig.h>
79 #include <mLib/str.h>
80 #include <mLib/sub.h>
81 #include <mLib/trace.h>
82
83 #include <catacomb/buf.h>
84
85 #include <catacomb/gcipher.h>
86 #include <catacomb/gmac.h>
87 #include <catacomb/grand.h>
88 #include <catacomb/key.h>
89 #include <catacomb/paranoia.h>
90
91 #include <catacomb/noise.h>
92 #include <catacomb/rand.h>
93
94 #include <catacomb/mp.h>
95 #include <catacomb/mprand.h>
96 #include <catacomb/dh.h>
97 #include <catacomb/ec.h>
98 #include <catacomb/ec-keys.h>
99 #include <catacomb/group.h>
100
101 #include "tripe-protocol.h"
102 #include "util.h"
103
104 #undef sun
105
106 /*----- Magic numbers -----------------------------------------------------*/
107
108 /* --- Tunnel types --- */
109
110 #define TUN_NOTDEF 0
111 #define TUN_UNET 1
112 #define TUN_BSD 2
113 #define TUN_LINUX 3
114
115 /* --- Trace flags --- */
116
117 #define T_TUNNEL 1u
118 #define T_PEER 2u
119 #define T_PACKET 4u
120 #define T_ADMIN 8u
121 #define T_CRYPTO 16u
122 #define T_KEYSET 32u
123 #define T_KEYEXCH 64u
124 #define T_KEYMGMT 128u
125
126 #define T_ALL 255u
127
128 /* --- Units --- */
129
130 #define SEC(n) (n##u)
131 #define MIN(n) (n##u * 60u)
132 #define MEG(n) (n##ul * 1024ul * 1024ul)
133
134 /* --- Other things --- */
135
136 #define PKBUFSZ 65536
137
138 /*----- Cipher selections -------------------------------------------------*/
139
140 typedef struct algswitch {
141   const gccipher *c;                    /* Symmetric encryption scheme */
142   const gccipher *mgf;                  /* Mask-generation function */
143   const gchash *h;                      /* Hash function */
144   const gcmac *m;                       /* Message authentication code */
145   size_t hashsz;                        /* Hash output size */
146   size_t tagsz;                         /* Length to truncate MAC tags */
147   size_t cksz, mksz;                    /* Key lengths for @c@ and @m@ */
148 } algswitch;
149
150 extern algswitch algs;
151
152 #define MAXHASHSZ 64                    /* Largest possible hash size */
153
154 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
155
156 /*----- Data structures ---------------------------------------------------*/
157
158 /* --- Socket addresses --- *
159  *
160  * A magic union of supported socket addresses.
161  */
162
163 typedef union addr {
164   struct sockaddr sa;
165   struct sockaddr_in sin;
166 } addr;
167
168 /* --- A symmetric keyset --- *
169  *
170  * A keyset contains a set of symmetric keys for encrypting and decrypting
171  * packets.  Keysets are stored in a list, sorted in reverse order of
172  * creation, so that the most recent keyset (the one most likely to be used)
173  * is first.
174  *
175  * Each keyset has a time limit and a data limit.  The keyset is destroyed
176  * when either it has existed for too long, or it has been used to encrypt
177  * too much data.  New key exchanges are triggered when keys are close to
178  * expiry.
179  */
180
181 typedef struct keyset {
182   struct keyset *next;                  /* Next active keyset in the list */
183   unsigned ref;                         /* Reference count for keyset */
184   struct peer *p;                       /* Pointer to peer structure */
185   time_t t_exp;                         /* Expiry time for this keyset */
186   unsigned long sz_exp;                 /* Data limit for the keyset */
187   T( unsigned seq; )                    /* Sequence number for tracing */
188   unsigned f;                           /* Various useful flags */
189   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
190   size_t tagsz;                         /* Length to truncate MAC tags */
191   gmac *min, *mout;                     /* Keyset MACs for integrity */
192   uint32 oseq;                          /* Outbound sequence number */
193   uint32 iseq, iwin;                    /* Inbound sequence number */
194 } keyset;
195
196 #define KS_SEQWINSZ 32                  /* Bits in sequence number window */
197
198 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
199 #define KSF_LINK 2u                     /* Key is in a linked list */
200
201 /* --- Key exchange --- *
202  *
203  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
204  * Protocol has a number of desirable features (e.g., perfect forward
205  * secrecy, and zero-knowledge authentication) which make it attractive for
206  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
207  * Clive Jones.
208  */
209
210 #define KX_NCHAL 16u
211 #define KX_THRESH 4u
212
213 typedef struct kxchal {
214   struct keyexch *kx;                   /* Pointer back to key exchange */
215   ge *c;                                /* Responder's challenge */
216   ge *r;                                /* My reply to the challenge */
217   keyset *ks;                           /* Pointer to temporary keyset */
218   unsigned f;                           /* Various useful flags */
219   sel_timer t;                          /* Response timer for challenge */
220   octet hc[MAXHASHSZ];                  /* Hash of his challenge */
221   mp *ck;                               /* The check value */
222   octet hswrq_in[MAXHASHSZ];            /* Inbound switch request message */
223   octet hswok_in[MAXHASHSZ];            /* Inbound switch confirmation */
224   octet hswrq_out[MAXHASHSZ];           /* Outbound switch request message */
225   octet hswok_out[MAXHASHSZ];           /* Outbound switch confirmation */
226 } kxchal;
227
228 typedef struct keyexch {
229   struct peer *p;                       /* Pointer back to the peer */
230   keyset **ks;                          /* Peer's list of keysets */
231   unsigned f;                           /* Various useful flags */
232   unsigned s;                           /* Current state in exchange */
233   sel_timer t;                          /* Timer for next exchange */
234   ge *kpub;                             /* Peer's public key */
235   time_t texp_kpub;                     /* Expiry time for public key */
236   mp *alpha;                            /* My temporary secret */
237   ge *c;                                /* My challenge */
238   ge *rx;                               /* The expected response */
239   unsigned nr;                          /* Number of extant responses */
240   time_t t_valid;                       /* When this exchange goes bad */
241   octet hc[MAXHASHSZ];                  /* Hash of my challenge */
242   kxchal *r[KX_NCHAL];                  /* Array of challenges */
243 } keyexch;
244
245 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
246 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
247 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
248
249 enum {
250   KXS_DEAD,                             /* Uninitialized state (magical) */
251   KXS_CHAL,                             /* Main answer-challenges state */
252   KXS_COMMIT,                           /* Committed: send switch request */
253   KXS_SWITCH                            /* Switched: send confirmation */
254 };
255
256 /* --- Tunnel structure --- *
257  *
258  * Used to maintain system-specific information about the tunnel interface.
259  */
260
261 #if TUN_TYPE == TUN_LINUX
262 #  include <linux/if.h>
263 #  include <linux/if_tun.h>
264 #endif
265
266 typedef struct tunnel {
267 #if TUN_TYPE == TUN_UNET 
268   sel_file f;                           /* Selector for Usernet device */
269   struct peer *p;                       /* Pointer to my peer */
270 #elif TUN_TYPE == TUN_LINUX
271   sel_file f;                           /* Selector for TUN/TAP device */
272   struct peer *p;                       /* Pointer to my peer */
273   char ifn[IFNAMSIZ];                   /* Interface name buffer */
274 #elif TUN_TYPE == TUN_BSD
275   sel_file f;                           /* Selector for tunnel device */
276   struct peer *p;                       /* Pointer to my peer */
277   unsigned n;                           /* Number of my tunnel device */
278 #else
279 #  error "No support for this tunnel type"
280 #endif
281 } tunnel;
282
283 /* --- Peer statistics --- *
284  *
285  * Contains various interesting and not-so-interesting statistics about a
286  * peer.  This is updated by various parts of the code.  The format of the
287  * structure isn't considered private, and @p_stats@ returns a pointer to the
288  * statistics block for a given peer.
289  */
290
291 typedef struct stats {
292   unsigned long sz_in, sz_out;          /* Size of all data in and out */
293   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
294   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
295   time_t t_start, t_last;               /* Time peer created, last recv */
296   unsigned long n_reject;               /* Number of rejected packets */
297   unsigned long n_in, n_out;            /* Number of packets in and out */
298   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
299   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
300 } stats;
301
302 /* --- Peer structure --- *
303  *
304  * The main structure which glues everything else together.
305  */
306
307 typedef struct peer {
308   struct peer *next, *prev;             /* Links to next and previous */
309   char *name;                           /* Name of this peer */
310   tunnel t;                             /* Tunnel for local packets */
311   keyset *ks;                           /* List head for keysets */
312   buf b;                                /* Buffer for sending packets */
313   addr peer;                            /* Peer socket address */
314   size_t sasz;                          /* Socket address size */
315   stats st;                             /* Statistics */
316   keyexch kx;                           /* Key exchange protocol block */
317 } peer;
318
319 /* --- Admin structure --- */
320
321 #define OBUFSZ 16384u
322
323 typedef struct obuf {
324   struct obuf *next;                    /* Next buffer in list */
325   char *p_in, *p_out;                   /* Pointers into the buffer */
326   char buf[OBUFSZ];                     /* The actual buffer */
327 } obuf;
328
329 typedef struct admin {
330   struct admin *next, *prev;            /* Links to next and previous */
331   unsigned f;                           /* Various useful flags */
332 #ifndef NTRACE
333   unsigned seq;                         /* Sequence number for tracing */
334 #endif
335   char *pname;                          /* Peer name to create */
336   char *paddr;                          /* Address string to resolve */
337   obuf *o_head, *o_tail;                /* Output buffer list */
338   selbuf b;                             /* Line buffer for commands */
339   sel_file w;                           /* Selector for write buffering */
340   bres_client r;                        /* Background resolver task */
341   sel_timer t;                          /* Timer for resolver */
342   addr peer;                            /* Address to set */
343   size_t sasz;                          /* Size of the address */
344 } admin;
345
346 #define AF_DEAD 1u                      /* Destroy this admin block */
347 #define AF_LOCK 2u                      /* Don't destroy it yet */
348
349 /*----- Global variables --------------------------------------------------*/
350
351 extern sel_state sel;                   /* Global I/O event state */
352 extern group *gg;                       /* The group we work in */
353 extern mp *kpriv;                       /* Our private key */
354 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
355
356 #ifndef NTRACE
357 extern const trace_opt tr_opts[];       /* Trace options array */
358 extern unsigned tr_flags;               /* Trace options flags */
359 #endif
360
361 /*----- Other macros ------------------------------------------------------*/
362
363 #define TIMER noise_timer(RAND_GLOBAL)
364
365 /*----- Key management ----------------------------------------------------*/
366
367 /* --- @km_interval@ --- *
368  *
369  * Arguments:   ---
370  *
371  * Returns:     Zero if OK, nonzero to force reloading of keys.
372  *
373  * Use:         Called on the interval timer to perform various useful jobs.
374  */
375
376 extern int km_interval(void);
377
378 /* --- @km_init@ --- *
379  *
380  * Arguments:   @const char *kr_priv@ = private keyring file
381  *              @const char *kr_pub@ = public keyring file
382  *              @const char *tag@ = tag to load
383  *
384  * Returns:     ---
385  *
386  * Use:         Initializes, and loads the private key.
387  */
388
389 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
390                     const char */*tag*/);
391
392 /* --- @km_getpubkey@ --- *
393  *
394  * Arguments:   @const char *tag@ = public key tag to load
395  *              @ge *kpub@ = where to put the public key
396  *              @time_t *t_exp@ = where to put the expiry time
397  *
398  * Returns:     Zero if OK, nonzero if it failed.
399  *
400  * Use:         Fetches a public key from the keyring.
401  */
402
403 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
404                         time_t */*t_exp*/);
405
406 /*----- Key exchange ------------------------------------------------------*/
407
408 /* --- @kx_start@ --- *
409  *
410  * Arguments:   @keyexch *kx@ = pointer to key exchange context
411  *
412  * Returns:     ---
413  *
414  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
415  *              a new challenge is sent (unless the quiet timer forbids
416  *              this); if no exchange is in progress, one is commenced.
417  */
418
419 extern void kx_start(keyexch */*kx*/);
420
421 /* --- @kx_message@ --- *
422  *
423  * Arguments:   @keyexch *kx@ = pointer to key exchange context
424  *              @unsigned msg@ = the message code
425  *              @buf *b@ = pointer to buffer containing the packet
426  *
427  * Returns:     ---
428  *
429  * Use:         Reads a packet containing key exchange messages and handles
430  *              it.
431  */
432
433 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
434
435 /* --- @kx_free@ --- *
436  *
437  * Arguments:   @keyexch *kx@ = pointer to key exchange context
438  *
439  * Returns:     ---
440  *
441  * Use:         Frees everything in a key exchange context.
442  */
443
444 extern void kx_free(keyexch */*kx*/);
445
446 /* --- @kx_newkeys@ --- *
447  *
448  * Arguments:   @keyexch *kx@ = pointer to key exchange context
449  *
450  * Returns:     ---
451  *
452  * Use:         Informs the key exchange module that its keys may have
453  *              changed.  If fetching the new keys fails, the peer will be
454  *              destroyed, we log messages and struggle along with the old
455  *              keys.
456  */
457
458 extern void kx_newkeys(keyexch */*kx*/);
459
460 /* --- @kx_init@ --- *
461  *
462  * Arguments:   @keyexch *kx@ = pointer to key exchange context
463  *              @peer *p@ = pointer to peer context
464  *              @keyset **ks@ = pointer to keyset list
465  *
466  * Returns:     Zero if OK, nonzero if it failed.
467  *
468  * Use:         Initializes a key exchange module.  The module currently
469  *              contains no keys, and will attempt to initiate a key
470  *              exchange.
471  */
472
473 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
474
475 /*----- Keysets and symmetric cryptography --------------------------------*/
476
477 /* --- @ks_drop@ --- *
478  *
479  * Arguments:   @keyset *ks@ = pointer to a keyset
480  *
481  * Returns:     ---
482  *
483  * Use:         Decrements a keyset's reference counter.  If the counter hits
484  *              zero, the keyset is freed.
485  */
486
487 extern void ks_drop(keyset */*ks*/);
488
489 /* --- @ks_gen@ --- *
490  *
491  * Arguments:   @const void *k@ = pointer to key material
492  *              @size_t x, y, z@ = offsets into key material (see below)
493  *              @peer *p@ = pointer to peer information
494  *
495  * Returns:     A pointer to the new keyset.
496  *
497  * Use:         Derives a new keyset from the given key material.  The
498  *              offsets @x@, @y@ and @z@ separate the key material into three
499  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
500  *              the key material; between @k + x@ and @k + y@ is `your'
501  *              contribution; and between @k + y@ and @k + z@ is a shared
502  *              value we made together.  These are used to construct two
503  *              pairs of symmetric keys.  Each pair consists of an encryption
504  *              key and a message authentication key.  One pair is used for
505  *              outgoing messages, the other for incoming messages.
506  *
507  *              The new key is marked so that it won't be selected for output
508  *              by @ksl_encrypt@.  You can still encrypt data with it by
509  *              calling @ks_encrypt@ directly.
510  */
511
512 extern keyset *ks_gen(const void */*k*/,
513                       size_t /*x*/, size_t /*y*/, size_t /*z*/,
514                       peer */*p*/);
515
516 /* --- @ks_tregen@ --- *
517  *
518  * Arguments:   @keyset *ks@ = pointer to a keyset
519  *
520  * Returns:     The time at which moves ought to be made to replace this key.
521  */
522
523 extern time_t ks_tregen(keyset */*ks*/);
524
525 /* --- @ks_activate@ --- *
526  *
527  * Arguments:   @keyset *ks@ = pointer to a keyset
528  *
529  * Returns:     ---
530  *
531  * Use:         Activates a keyset, so that it can be used for encrypting
532  *              outgoing messages.
533  */
534
535 extern void ks_activate(keyset */*ks*/);
536
537 /* --- @ks_encrypt@ --- *
538  *
539  * Arguments:   @keyset *ks@ = pointer to a keyset
540  *              @unsigned ty@ = message type
541  *              @buf *b@ = pointer to input buffer
542  *              @buf *bb@ = pointer to output buffer
543  *
544  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
545  *              encryption failed, the output buffer is broken and zero is
546  *              returned.
547  *
548  * Use:         Encrypts a block of data using the key.  Note that the `key
549  *              ought to be replaced' notification is only ever given once
550  *              for each key.  Also note that this call forces a keyset to be
551  *              used even if it's marked as not for data output.
552  */
553
554 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
555                       buf */*b*/, buf */*bb*/);
556
557 /* --- @ks_decrypt@ --- *
558  *
559  * Arguments:   @keyset *ks@ = pointer to a keyset
560  *              @unsigned ty@ = expected type code
561  *              @buf *b@ = pointer to an input buffer
562  *              @buf *bb@ = pointer to an output buffer
563  *
564  * Returns:     Zero on success, or nonzero if there was some problem.
565  *
566  * Use:         Attempts to decrypt a message using a given key.  Note that
567  *              requesting decryption with a key directly won't clear a
568  *              marking that it's not for encryption.
569  */
570
571 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
572                       buf */*b*/, buf */*bb*/);
573
574 /* --- @ksl_free@ --- *
575  *
576  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
577  *
578  * Returns:     ---
579  *
580  * Use:         Frees (releases references to) all of the keys in a keyset.
581  */
582
583 extern void ksl_free(keyset **/*ksroot*/);
584
585 /* --- @ksl_link@ --- *
586  *
587  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
588  *              @keyset *ks@ = pointer to a keyset
589  *
590  * Returns:     ---
591  *
592  * Use:         Links a keyset into a list.  A keyset can only be on one list
593  *              at a time.  Bad things happen otherwise.
594  */
595
596 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
597
598 /* --- @ksl_prune@ --- *
599  *
600  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
601  *
602  * Returns:     ---
603  *
604  * Use:         Prunes the keyset list by removing keys which mustn't be used
605  *              any more.
606  */
607
608 extern void ksl_prune(keyset **/*ksroot*/);
609
610 /* --- @ksl_encrypt@ --- *
611  *
612  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
613  *              @unsigned ty@ = message type
614  *              @buf *b@ = pointer to input buffer
615  *              @buf *bb@ = pointer to output buffer
616  *
617  * Returns:     Nonzero if a new key is needed.
618  *
619  * Use:         Encrypts a packet.
620  */
621
622 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
623                        buf */*b*/, buf */*bb*/);
624
625 /* --- @ksl_decrypt@ --- *
626  *
627  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
628  *              @unsigned ty@ = expected type code
629  *              @buf *b@ = pointer to input buffer
630  *              @buf *bb@ = pointer to output buffer
631  *
632  * Returns:     Nonzero if the packet couldn't be decrypted.
633  *
634  * Use:         Decrypts a packet.
635  */
636
637 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
638                        buf */*b*/, buf */*bb*/);
639
640 /*----- Administration interface ------------------------------------------*/
641
642 /* --- @a_warn@ --- *
643  *
644  * Arguments:   @const char *fmt@ = pointer to format string
645  *              @...@ = other arguments
646  *
647  * Returns:     ---
648  *
649  * Use:         Informs all admin connections of a warning.
650  */
651
652 extern void a_warn(const char */*fmt*/, ...);
653
654 /* --- @a_create@ --- *
655  *
656  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
657  *
658  * Returns:     ---
659  *
660  * Use:         Creates a new admin connection.
661  */
662
663 extern void a_create(int /*fd_in*/, int /*fd_out*/);
664
665 /* --- @a_quit@ --- *
666  *
667  * Arguments:   ---
668  *
669  * Returns:     ---
670  *
671  * Use:         Shuts things down nicely.
672  */
673
674 extern void a_quit(void);
675
676 /* --- @a_daemon@ --- *
677  *
678  * Arguments:   ---
679  *
680  * Returns:     ---
681  *
682  * Use:         Informs the admin module that it's a daemon.
683  */
684
685 extern void a_daemon(void);
686
687 /* --- @a_init@ --- *
688  *
689  * Arguments:   @const char *sock@ = socket name to create
690  *
691  * Returns:     ---
692  *
693  * Use:         Creates the admin listening socket.
694  */
695
696 extern void a_init(const char */*sock*/);
697
698 /*----- Peer management ---------------------------------------------------*/
699
700 /* --- @p_txstart@ --- *
701  *
702  * Arguments:   @peer *p@ = pointer to peer block
703  *              @unsigned msg@ = message type code
704  *
705  * Returns:     A pointer to a buffer to write to.
706  *
707  * Use:         Starts sending to a peer.  Only one send can happen at a
708  *              time.
709  */
710
711 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
712
713 /* --- @p_txend@ --- *
714  *
715  * Arguments:   @peer *p@ = pointer to peer block
716  *
717  * Returns:     ---
718  *
719  * Use:         Sends a packet to the peer.
720  */
721
722 extern void p_txend(peer */*p*/);
723
724 /* --- @p_tun@ --- *
725  *
726  * Arguments:   @peer *p@ = pointer to peer block
727  *              @buf *b@ = buffer containing incoming packet
728  *
729  * Returns:     ---
730  *
731  * Use:         Handles a packet which needs to be sent to a peer.
732  */
733
734 extern void p_tun(peer */*p*/, buf */*b*/);
735
736 /* --- @p_interval@ --- *
737  *
738  * Arguments:   ---
739  *
740  * Returns:     ---
741  *
742  * Use:         Called periodically to do tidying.
743  */
744
745 extern void p_interval(void);
746
747 /* --- @p_stats@ --- *
748  *
749  * Arguments:   @peer *p@ = pointer to a peer block
750  *
751  * Returns:     A pointer to the peer's statistics.
752  */
753
754 extern stats *p_stats(peer */*p*/);
755
756 /* --- @p_ifname@ --- *
757  *
758  * Arguments:   @peer *p@ = pointer to a peer block
759  *
760  * Returns:     A pointer to the peer's interface name.
761  */
762
763 extern const char *p_ifname(peer */*p*/);
764
765 /* --- @p_addr@ --- *
766  *
767  * Arguments:   @peer *p@ = pointer to a peer block
768  *
769  * Returns:     A pointer to the peer's address.
770  */
771
772 extern const addr *p_addr(peer */*p*/);
773
774 /* --- @p_init@ --- *
775  *
776  * Arguments:   @struct in_addr addr@ = address to bind to
777  *              @unsigned port@ = port number to listen to
778  *
779  * Returns:     ---
780  *
781  * Use:         Initializes the peer system; creates the socket.
782  */
783
784 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
785
786 /* --- @p_port@ --- *
787  *
788  * Arguments:   ---
789  *
790  * Returns:     Port number used for socket.
791  */
792
793 unsigned p_port(void);
794
795 /* --- @p_create@ --- *
796  *
797  * Arguments:   @const char *name@ = name for this peer
798  *              @struct sockaddr *sa@ = socket address of peer
799  *              @size_t sz@ = size of socket address
800  *
801  * Returns:     Pointer to the peer block, or null if it failed.
802  *
803  * Use:         Creates a new named peer block.  No peer is actually attached
804  *              by this point.
805  */
806
807 extern peer *p_create(const char */*name*/,
808                       struct sockaddr */*sa*/, size_t /*sz*/);
809
810 /* --- @p_name@ --- *
811  *
812  * Arguments:   @peer *p@ = pointer to a peer block
813  *
814  * Returns:     A pointer to the peer's name.
815  */
816
817 extern const char *p_name(peer */*p*/);
818
819 /* --- @p_find@ --- *
820  *
821  * Arguments:   @const char *name@ = name to look up
822  *
823  * Returns:     Pointer to the peer block, or null if not found.
824  *
825  * Use:         Finds a peer by name.
826  */
827
828 extern peer *p_find(const char */*name*/);
829
830 /* --- @p_destroy@ --- *
831  *
832  * Arguments:   @peer *p@ = pointer to a peer
833  *
834  * Returns:     ---
835  *
836  * Use:         Destroys a peer.
837  */
838
839 extern void p_destroy(peer */*p*/);
840
841 /* --- @p_first@, @p_next@ --- *
842  *
843  * Arguments:   @peer *p@ = a peer block
844  *
845  * Returns:     @peer_first@ returns the first peer in some ordering;
846  *              @peer_next@ returns the peer following a given one in the
847  *              same ordering.  Null is returned for the end of the list.
848  */
849
850 extern peer *p_first(void);
851 extern peer *p_next(peer */*p*/);
852
853 /*----- Tunnel interface --------------------------------------------------*/
854
855 /* --- @tun_init@ --- *
856  *
857  * Arguments:   ---
858  *
859  * Returns:     ---
860  *
861  * Use:         Initializes the tunneling system.  Maybe this will require
862  *              opening file descriptors or something.
863  */
864
865 extern void tun_init(void);
866
867 /* --- @tun_create@ --- *
868  *
869  * Arguments:   @tunnel *t@ = pointer to tunnel block
870  *              @peer *p@ = pointer to peer block
871  *
872  * Returns:     Zero if it worked, nonzero on failure.
873  *
874  * Use:         Initializes a new tunnel.
875  */
876
877 extern int tun_create(tunnel */*t*/, peer */*p*/);
878
879 /* --- @tun_ifname@ --- *
880  *
881  * Arguments:   @tunnel *t@ = pointer to tunnel block
882  *
883  * Returns:     A pointer to the tunnel's interface name.
884  */
885
886 extern const char *tun_ifname(tunnel */*t*/);
887
888 /* --- @tun_inject@ --- *
889  *
890  * Arguments:   @tunnel *t@ = pointer to tunnel block
891  *              @buf *b@ = buffer to send
892  *
893  * Returns:     ---
894  *
895  * Use:         Injects a packet into the local network stack.
896  */
897
898 extern void tun_inject(tunnel */*t*/, buf */*b*/);
899
900 /* --- @tun_destroy@ --- *
901  *
902  * Arguments:   @tunnel *t@ = pointer to tunnel block
903  *
904  * Returns:     ---
905  *
906  * Use:         Destroys a tunnel.
907  */
908
909 extern void tun_destroy(tunnel */*t*/);
910
911 /*----- Other handy utilities ---------------------------------------------*/
912
913 /* --- @mpstr@ --- *
914  *
915  * Arguments:   @mp *m@ = a multiprecision integer
916  *
917  * Returns:     A pointer to the integer's textual representation.
918  *
919  * Use:         Converts a multiprecision integer to a string.  Corrupts
920  *              @buf_t@.
921  */
922
923 extern const char *mpstr(mp */*m*/);
924
925 /* --- @gestr@ --- *
926  *
927  * Arguments:   @group *g@ = a group
928  *              @ge *x@ = a group element
929  *
930  * Returns:     A pointer to the element's textual representation.
931  *
932  * Use:         Converts a group element to a string.  Corrupts
933  *              @buf_t@.
934  */
935
936 extern const char *gestr(group */*g*/, ge */*x*/);
937
938 /* --- @timestr@ --- *
939  *
940  * Arguments:   @time_t t@ = a time to convert
941  *
942  * Returns:     A pointer to a textual representation of the time.
943  *
944  * Use:         Converts a time to a textual representation.  Corrupts
945  *              @buf_t@.
946  */
947
948 extern const char *timestr(time_t /*t*/);
949
950 /*----- That's all, folks -------------------------------------------------*/
951
952 #ifdef __cplusplus
953   }
954 #endif
955
956 #endif