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