chiark / gitweb /
Issue ADD and KXSTART notifications in the right order!
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id$
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, t_kx;         /* Time peer created, last pk, kx */
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 #define AF_NOTE 4u                      /* Catch notifications */
349 #ifndef NTRACE
350   #define AF_TRACE 8u                   /* Catch tracing */
351 #endif
352 #define AF_WARN 16u                     /* Catch warning messages */
353
354 #ifndef NTRACE
355 #  define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
356 #else
357 #  define AF_ALLMSGS (AF_NOTE | AF_WARN)
358 #endif
359
360 /*----- Global variables --------------------------------------------------*/
361
362 extern sel_state sel;                   /* Global I/O event state */
363 extern group *gg;                       /* The group we work in */
364 extern mp *kpriv;                       /* Our private key */
365 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
366
367 #ifndef NTRACE
368 extern const trace_opt tr_opts[];       /* Trace options array */
369 extern unsigned tr_flags;               /* Trace options flags */
370 #endif
371
372 /*----- Other macros ------------------------------------------------------*/
373
374 #define TIMER noise_timer(RAND_GLOBAL)
375
376 /*----- Key management ----------------------------------------------------*/
377
378 /* --- @km_interval@ --- *
379  *
380  * Arguments:   ---
381  *
382  * Returns:     Zero if OK, nonzero to force reloading of keys.
383  *
384  * Use:         Called on the interval timer to perform various useful jobs.
385  */
386
387 extern int km_interval(void);
388
389 /* --- @km_init@ --- *
390  *
391  * Arguments:   @const char *kr_priv@ = private keyring file
392  *              @const char *kr_pub@ = public keyring file
393  *              @const char *tag@ = tag to load
394  *
395  * Returns:     ---
396  *
397  * Use:         Initializes, and loads the private key.
398  */
399
400 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
401                     const char */*tag*/);
402
403 /* --- @km_getpubkey@ --- *
404  *
405  * Arguments:   @const char *tag@ = public key tag to load
406  *              @ge *kpub@ = where to put the public key
407  *              @time_t *t_exp@ = where to put the expiry time
408  *
409  * Returns:     Zero if OK, nonzero if it failed.
410  *
411  * Use:         Fetches a public key from the keyring.
412  */
413
414 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
415                         time_t */*t_exp*/);
416
417 /*----- Key exchange ------------------------------------------------------*/
418
419 /* --- @kx_start@ --- *
420  *
421  * Arguments:   @keyexch *kx@ = pointer to key exchange context
422  *
423  * Returns:     ---
424  *
425  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
426  *              a new challenge is sent (unless the quiet timer forbids
427  *              this); if no exchange is in progress, one is commenced.
428  */
429
430 extern void kx_start(keyexch */*kx*/);
431
432 /* --- @kx_message@ --- *
433  *
434  * Arguments:   @keyexch *kx@ = pointer to key exchange context
435  *              @unsigned msg@ = the message code
436  *              @buf *b@ = pointer to buffer containing the packet
437  *
438  * Returns:     ---
439  *
440  * Use:         Reads a packet containing key exchange messages and handles
441  *              it.
442  */
443
444 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
445
446 /* --- @kx_free@ --- *
447  *
448  * Arguments:   @keyexch *kx@ = pointer to key exchange context
449  *
450  * Returns:     ---
451  *
452  * Use:         Frees everything in a key exchange context.
453  */
454
455 extern void kx_free(keyexch */*kx*/);
456
457 /* --- @kx_newkeys@ --- *
458  *
459  * Arguments:   @keyexch *kx@ = pointer to key exchange context
460  *
461  * Returns:     ---
462  *
463  * Use:         Informs the key exchange module that its keys may have
464  *              changed.  If fetching the new keys fails, the peer will be
465  *              destroyed, we log messages and struggle along with the old
466  *              keys.
467  */
468
469 extern void kx_newkeys(keyexch */*kx*/);
470
471 /* --- @kx_init@ --- *
472  *
473  * Arguments:   @keyexch *kx@ = pointer to key exchange context
474  *              @peer *p@ = pointer to peer context
475  *              @keyset **ks@ = pointer to keyset list
476  *
477  * Returns:     Zero if OK, nonzero if it failed.
478  *
479  * Use:         Initializes a key exchange module.  The module currently
480  *              contains no keys, and will attempt to initiate a key
481  *              exchange.
482  */
483
484 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
485
486 /*----- Keysets and symmetric cryptography --------------------------------*/
487
488 /* --- @ks_drop@ --- *
489  *
490  * Arguments:   @keyset *ks@ = pointer to a keyset
491  *
492  * Returns:     ---
493  *
494  * Use:         Decrements a keyset's reference counter.  If the counter hits
495  *              zero, the keyset is freed.
496  */
497
498 extern void ks_drop(keyset */*ks*/);
499
500 /* --- @ks_gen@ --- *
501  *
502  * Arguments:   @const void *k@ = pointer to key material
503  *              @size_t x, y, z@ = offsets into key material (see below)
504  *              @peer *p@ = pointer to peer information
505  *
506  * Returns:     A pointer to the new keyset.
507  *
508  * Use:         Derives a new keyset from the given key material.  The
509  *              offsets @x@, @y@ and @z@ separate the key material into three
510  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
511  *              the key material; between @k + x@ and @k + y@ is `your'
512  *              contribution; and between @k + y@ and @k + z@ is a shared
513  *              value we made together.  These are used to construct two
514  *              pairs of symmetric keys.  Each pair consists of an encryption
515  *              key and a message authentication key.  One pair is used for
516  *              outgoing messages, the other for incoming messages.
517  *
518  *              The new key is marked so that it won't be selected for output
519  *              by @ksl_encrypt@.  You can still encrypt data with it by
520  *              calling @ks_encrypt@ directly.
521  */
522
523 extern keyset *ks_gen(const void */*k*/,
524                       size_t /*x*/, size_t /*y*/, size_t /*z*/,
525                       peer */*p*/);
526
527 /* --- @ks_tregen@ --- *
528  *
529  * Arguments:   @keyset *ks@ = pointer to a keyset
530  *
531  * Returns:     The time at which moves ought to be made to replace this key.
532  */
533
534 extern time_t ks_tregen(keyset */*ks*/);
535
536 /* --- @ks_activate@ --- *
537  *
538  * Arguments:   @keyset *ks@ = pointer to a keyset
539  *
540  * Returns:     ---
541  *
542  * Use:         Activates a keyset, so that it can be used for encrypting
543  *              outgoing messages.
544  */
545
546 extern void ks_activate(keyset */*ks*/);
547
548 /* --- @ks_encrypt@ --- *
549  *
550  * Arguments:   @keyset *ks@ = pointer to a keyset
551  *              @unsigned ty@ = message type
552  *              @buf *b@ = pointer to input buffer
553  *              @buf *bb@ = pointer to output buffer
554  *
555  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
556  *              encryption failed, the output buffer is broken and zero is
557  *              returned.
558  *
559  * Use:         Encrypts a block of data using the key.  Note that the `key
560  *              ought to be replaced' notification is only ever given once
561  *              for each key.  Also note that this call forces a keyset to be
562  *              used even if it's marked as not for data output.
563  */
564
565 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
566                       buf */*b*/, buf */*bb*/);
567
568 /* --- @ks_decrypt@ --- *
569  *
570  * Arguments:   @keyset *ks@ = pointer to a keyset
571  *              @unsigned ty@ = expected type code
572  *              @buf *b@ = pointer to an input buffer
573  *              @buf *bb@ = pointer to an output buffer
574  *
575  * Returns:     Zero on success, or nonzero if there was some problem.
576  *
577  * Use:         Attempts to decrypt a message using a given key.  Note that
578  *              requesting decryption with a key directly won't clear a
579  *              marking that it's not for encryption.
580  */
581
582 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
583                       buf */*b*/, buf */*bb*/);
584
585 /* --- @ksl_free@ --- *
586  *
587  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
588  *
589  * Returns:     ---
590  *
591  * Use:         Frees (releases references to) all of the keys in a keyset.
592  */
593
594 extern void ksl_free(keyset **/*ksroot*/);
595
596 /* --- @ksl_link@ --- *
597  *
598  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
599  *              @keyset *ks@ = pointer to a keyset
600  *
601  * Returns:     ---
602  *
603  * Use:         Links a keyset into a list.  A keyset can only be on one list
604  *              at a time.  Bad things happen otherwise.
605  */
606
607 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
608
609 /* --- @ksl_prune@ --- *
610  *
611  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
612  *
613  * Returns:     ---
614  *
615  * Use:         Prunes the keyset list by removing keys which mustn't be used
616  *              any more.
617  */
618
619 extern void ksl_prune(keyset **/*ksroot*/);
620
621 /* --- @ksl_encrypt@ --- *
622  *
623  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
624  *              @unsigned ty@ = message type
625  *              @buf *b@ = pointer to input buffer
626  *              @buf *bb@ = pointer to output buffer
627  *
628  * Returns:     Nonzero if a new key is needed.
629  *
630  * Use:         Encrypts a packet.
631  */
632
633 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
634                        buf */*b*/, buf */*bb*/);
635
636 /* --- @ksl_decrypt@ --- *
637  *
638  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
639  *              @unsigned ty@ = expected type code
640  *              @buf *b@ = pointer to input buffer
641  *              @buf *bb@ = pointer to output buffer
642  *
643  * Returns:     Nonzero if the packet couldn't be decrypted.
644  *
645  * Use:         Decrypts a packet.
646  */
647
648 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
649                        buf */*b*/, buf */*bb*/);
650
651 /*----- Administration interface ------------------------------------------*/
652
653 /* --- @a_warn@ --- *
654  *
655  * Arguments:   @const char *fmt@ = pointer to format string
656  *              @...@ = other arguments
657  *
658  * Returns:     ---
659  *
660  * Use:         Informs all admin connections of a warning.
661  */
662
663 extern void a_warn(const char */*fmt*/, ...);
664
665 /* --- @a_notify@ --- *
666  *
667  * Arguments:   @const char *fmt@ = pointer to format string
668  *              @...@ = other arguments
669  *
670  * Returns:     ---
671  *
672  * Use:         Sends a notification to interested admin connections.
673  */
674
675 extern void a_notify(const char */*fmt*/, ...);
676
677 /* --- @a_create@ --- *
678  *
679  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
680  *              @unsigned f@ = initial flags to set
681  *
682  * Returns:     ---
683  *
684  * Use:         Creates a new admin connection.
685  */
686
687 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
688
689 /* --- @a_quit@ --- *
690  *
691  * Arguments:   ---
692  *
693  * Returns:     ---
694  *
695  * Use:         Shuts things down nicely.
696  */
697
698 extern void a_quit(void);
699
700 /* --- @a_daemon@ --- *
701  *
702  * Arguments:   ---
703  *
704  * Returns:     ---
705  *
706  * Use:         Informs the admin module that it's a daemon.
707  */
708
709 extern void a_daemon(void);
710
711 /* --- @a_init@ --- *
712  *
713  * Arguments:   @const char *sock@ = socket name to create
714  *
715  * Returns:     ---
716  *
717  * Use:         Creates the admin listening socket.
718  */
719
720 extern void a_init(const char */*sock*/);
721
722 /*----- Peer management ---------------------------------------------------*/
723
724 /* --- @p_txstart@ --- *
725  *
726  * Arguments:   @peer *p@ = pointer to peer block
727  *              @unsigned msg@ = message type code
728  *
729  * Returns:     A pointer to a buffer to write to.
730  *
731  * Use:         Starts sending to a peer.  Only one send can happen at a
732  *              time.
733  */
734
735 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
736
737 /* --- @p_txend@ --- *
738  *
739  * Arguments:   @peer *p@ = pointer to peer block
740  *
741  * Returns:     ---
742  *
743  * Use:         Sends a packet to the peer.
744  */
745
746 extern void p_txend(peer */*p*/);
747
748 /* --- @p_tun@ --- *
749  *
750  * Arguments:   @peer *p@ = pointer to peer block
751  *              @buf *b@ = buffer containing incoming packet
752  *
753  * Returns:     ---
754  *
755  * Use:         Handles a packet which needs to be sent to a peer.
756  */
757
758 extern void p_tun(peer */*p*/, buf */*b*/);
759
760 /* --- @p_interval@ --- *
761  *
762  * Arguments:   ---
763  *
764  * Returns:     ---
765  *
766  * Use:         Called periodically to do tidying.
767  */
768
769 extern void p_interval(void);
770
771 /* --- @p_stats@ --- *
772  *
773  * Arguments:   @peer *p@ = pointer to a peer block
774  *
775  * Returns:     A pointer to the peer's statistics.
776  */
777
778 extern stats *p_stats(peer */*p*/);
779
780 /* --- @p_ifname@ --- *
781  *
782  * Arguments:   @peer *p@ = pointer to a peer block
783  *
784  * Returns:     A pointer to the peer's interface name.
785  */
786
787 extern const char *p_ifname(peer */*p*/);
788
789 /* --- @p_addr@ --- *
790  *
791  * Arguments:   @peer *p@ = pointer to a peer block
792  *
793  * Returns:     A pointer to the peer's address.
794  */
795
796 extern const addr *p_addr(peer */*p*/);
797
798 /* --- @p_init@ --- *
799  *
800  * Arguments:   @struct in_addr addr@ = address to bind to
801  *              @unsigned port@ = port number to listen to
802  *
803  * Returns:     ---
804  *
805  * Use:         Initializes the peer system; creates the socket.
806  */
807
808 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
809
810 /* --- @p_port@ --- *
811  *
812  * Arguments:   ---
813  *
814  * Returns:     Port number used for socket.
815  */
816
817 unsigned p_port(void);
818
819 /* --- @p_create@ --- *
820  *
821  * Arguments:   @const char *name@ = name for this peer
822  *              @struct sockaddr *sa@ = socket address of peer
823  *              @size_t sz@ = size of socket address
824  *
825  * Returns:     Pointer to the peer block, or null if it failed.
826  *
827  * Use:         Creates a new named peer block.  No peer is actually attached
828  *              by this point.
829  */
830
831 extern peer *p_create(const char */*name*/,
832                       struct sockaddr */*sa*/, size_t /*sz*/);
833
834 /* --- @p_name@ --- *
835  *
836  * Arguments:   @peer *p@ = pointer to a peer block
837  *
838  * Returns:     A pointer to the peer's name.
839  */
840
841 extern const char *p_name(peer */*p*/);
842
843 /* --- @p_find@ --- *
844  *
845  * Arguments:   @const char *name@ = name to look up
846  *
847  * Returns:     Pointer to the peer block, or null if not found.
848  *
849  * Use:         Finds a peer by name.
850  */
851
852 extern peer *p_find(const char */*name*/);
853
854 /* --- @p_destroy@ --- *
855  *
856  * Arguments:   @peer *p@ = pointer to a peer
857  *
858  * Returns:     ---
859  *
860  * Use:         Destroys a peer.
861  */
862
863 extern void p_destroy(peer */*p*/);
864
865 /* --- @p_first@, @p_next@ --- *
866  *
867  * Arguments:   @peer *p@ = a peer block
868  *
869  * Returns:     @peer_first@ returns the first peer in some ordering;
870  *              @peer_next@ returns the peer following a given one in the
871  *              same ordering.  Null is returned for the end of the list.
872  */
873
874 extern peer *p_first(void);
875 extern peer *p_next(peer */*p*/);
876
877 /*----- Tunnel interface --------------------------------------------------*/
878
879 /* --- @tun_init@ --- *
880  *
881  * Arguments:   ---
882  *
883  * Returns:     ---
884  *
885  * Use:         Initializes the tunneling system.  Maybe this will require
886  *              opening file descriptors or something.
887  */
888
889 extern void tun_init(void);
890
891 /* --- @tun_create@ --- *
892  *
893  * Arguments:   @tunnel *t@ = pointer to tunnel block
894  *              @peer *p@ = pointer to peer block
895  *
896  * Returns:     Zero if it worked, nonzero on failure.
897  *
898  * Use:         Initializes a new tunnel.
899  */
900
901 extern int tun_create(tunnel */*t*/, peer */*p*/);
902
903 /* --- @tun_ifname@ --- *
904  *
905  * Arguments:   @tunnel *t@ = pointer to tunnel block
906  *
907  * Returns:     A pointer to the tunnel's interface name.
908  */
909
910 extern const char *tun_ifname(tunnel */*t*/);
911
912 /* --- @tun_inject@ --- *
913  *
914  * Arguments:   @tunnel *t@ = pointer to tunnel block
915  *              @buf *b@ = buffer to send
916  *
917  * Returns:     ---
918  *
919  * Use:         Injects a packet into the local network stack.
920  */
921
922 extern void tun_inject(tunnel */*t*/, buf */*b*/);
923
924 /* --- @tun_destroy@ --- *
925  *
926  * Arguments:   @tunnel *t@ = pointer to tunnel block
927  *
928  * Returns:     ---
929  *
930  * Use:         Destroys a tunnel.
931  */
932
933 extern void tun_destroy(tunnel */*t*/);
934
935 /*----- Other handy utilities ---------------------------------------------*/
936
937 /* --- @mpstr@ --- *
938  *
939  * Arguments:   @mp *m@ = a multiprecision integer
940  *
941  * Returns:     A pointer to the integer's textual representation.
942  *
943  * Use:         Converts a multiprecision integer to a string.  Corrupts
944  *              @buf_t@.
945  */
946
947 extern const char *mpstr(mp */*m*/);
948
949 /* --- @gestr@ --- *
950  *
951  * Arguments:   @group *g@ = a group
952  *              @ge *x@ = a group element
953  *
954  * Returns:     A pointer to the element's textual representation.
955  *
956  * Use:         Converts a group element to a string.  Corrupts
957  *              @buf_t@.
958  */
959
960 extern const char *gestr(group */*g*/, ge */*x*/);
961
962 /* --- @timestr@ --- *
963  *
964  * Arguments:   @time_t t@ = a time to convert
965  *
966  * Returns:     A pointer to a textual representation of the time.
967  *
968  * Use:         Converts a time to a textual representation.  Corrupts
969  *              @buf_t@.
970  */
971
972 extern const char *timestr(time_t /*t*/);
973
974 /*----- That's all, folks -------------------------------------------------*/
975
976 #ifdef __cplusplus
977   }
978 #endif
979
980 #endif