3 * $Id: tripe.h,v 1.15 2003/05/16 12:09:03 mdw Exp $
5 * Main header file for TrIPE
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.15 2003/05/16 12:09:03 mdw
33 * Allow binding to a chosen address.
35 * Revision 1.14 2003/04/06 10:36:33 mdw
36 * Rearrange so as not to include Linux headers unless we need to.
38 * Revision 1.13 2003/04/06 10:26:35 mdw
39 * Report peer name on decrypt errors.
41 * Revision 1.12 2003/04/06 10:25:17 mdw
42 * Support Linux TUN/TAP device. Fix some bugs.
44 * Revision 1.11 2002/01/13 14:57:42 mdw
47 * Revision 1.10 2002/01/13 14:54:58 mdw
50 * Revision 1.9 2001/06/22 19:40:36 mdw
51 * Support expiry of other peers' public keys.
53 * Revision 1.8 2001/06/19 22:10:57 mdw
54 * Some more constants for the algorithms. Document the packet format
55 * change for non-malleability. Moved @buf@ definitions to separate header
58 * Revision 1.7 2001/03/03 12:07:08 mdw
59 * Rename word get and put functions now that there's 16-bit support.
61 * Revision 1.6 2001/02/19 19:11:09 mdw
62 * Output buffering on admin connections.
64 * Revision 1.5 2001/02/16 21:41:43 mdw
65 * Major changes. See source files for details.
67 * Revision 1.4 2001/02/05 19:56:37 mdw
68 * Sequence number protection, and BSD tunnels.
70 * Revision 1.3 2001/02/04 01:17:55 mdw
71 * Create a configuration header file to tidy up command lines.
73 * Revision 1.2 2001/02/03 22:40:29 mdw
74 * Put timer information into the entropy pool when packets are received
75 * and on similar events. Reseed the generator on the interval timer.
77 * Revision 1.1 2001/02/03 20:26:37 mdw
89 /*----- Header files ------------------------------------------------------*/
104 #include <sys/types.h>
105 #include <sys/time.h>
108 #include <sys/stat.h>
110 #include <sys/socket.h>
112 #include <netinet/in.h>
113 #include <arpa/inet.h>
119 #include <mLib/alloc.h>
120 #include <mLib/arena.h>
121 #include <mLib/bres.h>
122 #include <mLib/dstr.h>
123 #include <mLib/env.h>
124 #include <mLib/fdflags.h>
125 #include <mLib/fwatch.h>
126 #include <mLib/mdwopt.h>
127 #include <mLib/quis.h>
128 #include <mLib/report.h>
129 #include <mLib/sel.h>
130 #include <mLib/selbuf.h>
131 #include <mLib/sig.h>
132 #include <mLib/str.h>
133 #include <mLib/sub.h>
134 #include <mLib/trace.h>
136 #include <catacomb/gcipher.h>
137 #include <catacomb/gmac.h>
138 #include <catacomb/grand.h>
139 #include <catacomb/key.h>
140 #include <catacomb/paranoia.h>
142 #include <catacomb/noise.h>
143 #include <catacomb/rand.h>
145 #include <catacomb/mp.h>
146 #include <catacomb/mpmont.h>
147 #include <catacomb/mprand.h>
148 #include <catacomb/dh.h>
155 /*----- Magic numbers -----------------------------------------------------*/
157 /* --- Tunnel types --- */
164 /* --- Trace flags --- */
172 #define T_KEYEXCH 64u
173 #define T_KEYMGMT 128u
179 #define SEC(n) (n##u)
180 #define MIN(n) (n##u * 60u)
181 #define MEG(n) (n##ul * 1024ul * 1024ul)
183 /* --- Other things --- */
185 #define PKBUFSZ 65536
187 /*----- TrIPE protocol ----------------------------------------------------*/
189 /* --- TrIPE message format --- *
191 * A packet begins with a single-byte message type. The top four bits are a
192 * category code used to send the message to the right general place in the
193 * code; the bottom bits identify the actual message type.
196 #define MSG_CATMASK 0xf0
197 #define MSG_TYPEMASK 0x0f
199 /* --- Encrypted message packets --- *
201 * Messages of category @MSG_PACKET@ contain encrypted network packets. The
202 * message content is a symmetric-encrypted block (see below). Reception of
203 * a packet encrypted under a new key implicitly permits that key to be used
204 * to send further packets.
206 * The only packet type accepted is zero.
208 * Packets may be encrypted under any live keyset, but should use the most
212 #define MSG_PACKET 0x00
214 /* --- Key exchange packets --- */
216 #define MSG_KEYEXCH 0x10
218 #define KX_PRECHAL 0u
223 #define KX_SWITCHOK 5u
226 /* --- Symmetric encryption and keysets --- *
228 * Packets consist of an 80-bit MAC, a 32-bit sequence number, and the
231 * The plaintext is encrypted using Blowfish in CBC mode with ciphertext
232 * stealing (as described in [Schneier]. The initialization vector is
233 * selected randomly, and prepended to the actual ciphertext.
235 * The MAC is computed using the HMAC construction with RIPEMD160 over the
236 * sequence number and the ciphertext (with IV); the first 80 bits of the
237 * output are used. (This is the minimum allowed by the draft FIPS for HMAC,
238 * and the recommended truncation.)
240 * A keyset consists of
242 * * an integrity (MAC) key;
243 * * a confidentiality (encryption) key; and
244 * * a sequence numbering space
246 * in each direction. The packets sent by a host encrypted under a
247 * particular keyset are assigned consecutive sequence numbers starting from
248 * zero. The receiving host must ensure that it only accepts each packet at
249 * most once. It should maintain a window of sequence numbers: packets with
250 * numbers beyond the end of the window are accepted and cause the window to
251 * be advanced; packets with numbers before the start of the window are
252 * rejected; packets with numbers which appear within the window are accepted
253 * only if the number has not been seen before.
255 * When a host sends a @KX_SWITCH@ or @KX_SWITCHOK@ message, it installs the
256 * newly-negotiated keyset in a `listen-only' state: it may not send a packet
257 * encrypted under the keyset until either it has received a @KX_SWITCH@ or
258 * @KX_SWITCHOK@ message, or a @MSG_PACKET@ encrypted under the keyset, from
262 /*----- Cipher selections -------------------------------------------------*/
264 #include <catacomb/blowfish.h>
265 #include <catacomb/blowfish-cbc.h>
266 #include <catacomb/blowfish-counter.h>
267 #include <catacomb/rmd160.h>
268 #include <catacomb/rmd160-hmac.h>
270 #define CIPHER (&blowfish_cbc)
271 #define MAC (&rmd160_hmac)
273 #define HASH_CTX rmd160_ctx
274 #define HASH_INIT rmd160_init
275 #define HASH rmd160_hash
276 #define HASH_STRING(c, s) HASH((c), s, sizeof(s))
277 #define HASH_DONE rmd160_done
278 #define HASHSZ RMD160_HASHSZ
280 #define MGF_CTX blowfish_counterctx
281 #define MGF_INIT blowfish_counterinit
282 #define MGF_CRYPT blowfish_counterencrypt
285 #define IVSZ BLOWFISH_BLKSZ
288 /*----- Data structures ---------------------------------------------------*/
290 /* --- Socket addresses --- *
292 * A magic union of supported socket addresses.
297 struct sockaddr_in sin;
300 /* --- A symmetric keyset --- *
302 * A keyset contains a set of symmetric keys for encrypting and decrypting
303 * packets. Keysets are stored in a list, sorted in reverse order of
304 * creation, so that the most recent keyset (the one most likely to be used)
307 * Each keyset has a time limit and a data limit. The keyset is destroyed
308 * when either it has existed for too long, or it has been used to encrypt
309 * too much data. New key exchanges are triggered when keys are close to
313 typedef struct keyset {
314 struct keyset *next; /* Next active keyset in the list */
315 unsigned ref; /* Reference count for keyset */
316 struct peer *p; /* Pointer to peer structure */
317 time_t t_exp; /* Expiry time for this keyset */
318 unsigned long sz_exp; /* Data limit for the keyset */
319 T( unsigned seq; ) /* Sequence number for tracing */
320 unsigned f; /* Various useful flags */
321 gcipher *cin, *cout; /* Keyset ciphers for encryption */
322 gmac *min, *mout; /* Keyset MACs for integrity */
323 uint32 oseq; /* Outbound sequence number */
324 uint32 iseq, iwin; /* Inbound sequence number */
327 #define KS_SEQWINSZ 32 /* Bits in sequence number window */
329 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
330 #define KSF_LINK 2u /* Key is in a linked list */
332 /* --- Key exchange --- *
334 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
335 * Protocol has a number of desirable features (e.g., perfect forward
336 * secrecy, and zero-knowledge authentication) which make it attractive for
337 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
344 typedef struct kxchal {
345 struct keyexch *kx; /* Pointer back to key exchange */
346 mp *c; /* Responder's challenge */
347 mp *r; /* My reply to the challenge */
348 keyset *ks; /* Pointer to temporary keyset */
349 unsigned f; /* Various useful flags */
350 sel_timer t; /* Response timer for challenge */
351 octet hc[HASHSZ]; /* Hash of his challenge */
352 mp *ck; /* The check value */
353 octet hswrq_in[HASHSZ]; /* Inbound switch request message */
354 octet hswok_in[HASHSZ]; /* Inbound switch confirmation */
355 octet hswrq_out[HASHSZ]; /* Outbound switch request message */
356 octet hswok_out[HASHSZ]; /* Outbound switch confirmation */
359 typedef struct keyexch {
360 struct peer *p; /* Pointer back to the peer */
361 keyset **ks; /* Peer's list of keysets */
362 unsigned f; /* Various useful flags */
363 unsigned s; /* Current state in exchange */
364 sel_timer t; /* Timer for next exchange */
365 dh_pub kpub; /* Peer's public key */
366 time_t texp_kpub; /* Expiry time for public key */
367 mp *alpha; /* My temporary secret */
368 mp *c; /* My challenge */
369 mp *rx; /* The expected response */
370 unsigned nr; /* Number of extant responses */
371 time_t t_valid; /* When this exchange goes bad */
372 octet hc[HASHSZ]; /* Hash of my challenge */
373 kxchal *r[KX_NCHAL]; /* Array of challenges */
376 #define KXF_TIMER 1u /* Waiting for a timer to go off */
377 #define KXF_DEAD 2u /* The key-exchanger isn't up */
378 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
381 KXS_DEAD, /* Uninitialized state (magical) */
382 KXS_CHAL, /* Main answer-challenges state */
383 KXS_COMMIT, /* Committed: send switch request */
384 KXS_SWITCH /* Switched: send confirmation */
387 /* --- Tunnel structure --- *
389 * Used to maintain system-specific information about the tunnel interface.
392 #if TUN_TYPE == TUN_LINUX
393 # include <linux/if.h>
394 # include <linux/if_tun.h>
397 typedef struct tunnel {
398 #if TUN_TYPE == TUN_UNET
399 sel_file f; /* Selector for Usernet device */
400 struct peer *p; /* Pointer to my peer */
401 #elif TUN_TYPE == TUN_LINUX
402 sel_file f; /* Selector for TUN/TAP device */
403 struct peer *p; /* Pointer to my peer */
404 char ifn[IFNAMSIZ]; /* Interface name buffer */
405 #elif TUN_TYPE == TUN_BSD
406 sel_file f; /* Selector for tunnel device */
407 struct peer *p; /* Pointer to my peer */
408 unsigned n; /* Number of my tunnel device */
410 # error "No support for this tunnel type"
414 /* --- Peer statistics --- *
416 * Contains various interesting and not-so-interesting statistics about a
417 * peer. This is updated by various parts of the code. The format of the
418 * structure isn't considered private, and @p_stats@ returns a pointer to the
419 * statistics block for a given peer.
422 typedef struct stats {
423 unsigned long sz_in, sz_out; /* Size of all data in and out */
424 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
425 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
426 time_t t_start, t_last; /* Time peer created, last recv */
427 unsigned long n_reject; /* Number of rejected packets */
428 unsigned long n_in, n_out; /* Number of packets in and out */
429 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
430 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
433 /* --- Peer structure --- *
435 * The main structure which glues everything else together.
438 typedef struct peer {
439 struct peer *next, *prev; /* Links to next and previous */
440 char *name; /* Name of this peer */
441 tunnel t; /* Tunnel for local packets */
442 keyset *ks; /* List head for keysets */
443 buf b; /* Buffer for sending packets */
444 addr peer; /* Peer socket address */
445 size_t sasz; /* Socket address size */
446 stats st; /* Statistics */
447 keyexch kx; /* Key exchange protocol block */
450 /* --- Admin structure --- */
452 #define OBUFSZ 16384u
454 typedef struct obuf {
455 struct obuf *next; /* Next buffer in list */
456 char *p_in, *p_out; /* Pointers into the buffer */
457 char buf[OBUFSZ]; /* The actual buffer */
460 typedef struct admin {
461 struct admin *next, *prev; /* Links to next and previous */
462 unsigned f; /* Various useful flags */
464 unsigned seq; /* Sequence number for tracing */
466 char *pname; /* Peer name to create */
467 char *paddr; /* Address string to resolve */
468 obuf *o_head, *o_tail; /* Output buffer list */
469 selbuf b; /* Line buffer for commands */
470 sel_file w; /* Selector for write buffering */
471 bres_client r; /* Background resolver task */
472 sel_timer t; /* Timer for resolver */
473 addr peer; /* Address to set */
474 size_t sasz; /* Size of the address */
477 #define AF_DEAD 1u /* Destroy this admin block */
478 #define AF_LOCK 2u /* Don't destroy it yet */
480 /*----- Global variables --------------------------------------------------*/
482 extern sel_state sel; /* Global I/O event state */
483 extern dh_priv kpriv; /* Our private key */
484 extern mpmont mg; /* Montgomery context for DH group */
485 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
488 extern const trace_opt tr_opts[]; /* Trace options array */
489 extern unsigned tr_flags; /* Trace options flags */
492 /*----- Other macros ------------------------------------------------------*/
494 #define TIMER noise_timer(RAND_GLOBAL)
496 /*----- Key management ----------------------------------------------------*/
498 /* --- @km_interval@ --- *
502 * Returns: Zero if OK, nonzero to force reloading of keys.
504 * Use: Called on the interval timer to perform various useful jobs.
507 extern int km_interval(void);
509 /* --- @km_init@ --- *
511 * Arguments: @const char *kr_priv@ = private keyring file
512 * @const char *kr_pub@ = public keyring file
513 * @const char *tag@ = tag to load
517 * Use: Initializes, and loads the private key.
520 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
521 const char */*tag*/);
523 /* --- @km_getpubkey@ --- *
525 * Arguments: @const char *tag@ = public key tag to load
526 * @dh_pub *kpub@ = where to put the public key
527 * @time_t *t_exp@ = where to put the expiry time
529 * Returns: Zero if OK, nonzero if it failed.
531 * Use: Fetches a public key from the keyring.
534 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/,
537 /*----- Key exchange ------------------------------------------------------*/
539 /* --- @kx_start@ --- *
541 * Arguments: @keyexch *kx@ = pointer to key exchange context
545 * Use: Stimulates a key exchange. If a key exchage is in progress,
546 * a new challenge is sent (unless the quiet timer forbids
547 * this); if no exchange is in progress, one is commenced.
550 extern void kx_start(keyexch */*kx*/);
552 /* --- @kx_message@ --- *
554 * Arguments: @keyexch *kx@ = pointer to key exchange context
555 * @unsigned msg@ = the message code
556 * @buf *b@ = pointer to buffer containing the packet
560 * Use: Reads a packet containing key exchange messages and handles
564 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
566 /* --- @kx_free@ --- *
568 * Arguments: @keyexch *kx@ = pointer to key exchange context
572 * Use: Frees everything in a key exchange context.
575 extern void kx_free(keyexch */*kx*/);
577 /* --- @kx_newkeys@ --- *
579 * Arguments: @keyexch *kx@ = pointer to key exchange context
583 * Use: Informs the key exchange module that its keys may have
584 * changed. If fetching the new keys fails, the peer will be
585 * destroyed, we log messages and struggle along with the old
589 extern void kx_newkeys(keyexch */*kx*/);
591 /* --- @kx_init@ --- *
593 * Arguments: @keyexch *kx@ = pointer to key exchange context
594 * @peer *p@ = pointer to peer context
595 * @keyset **ks@ = pointer to keyset list
597 * Returns: Zero if OK, nonzero if it failed.
599 * Use: Initializes a key exchange module. The module currently
600 * contains no keys, and will attempt to initiate a key
604 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
606 /*----- Keysets and symmetric cryptography --------------------------------*/
608 /* --- @ks_drop@ --- *
610 * Arguments: @keyset *ks@ = pointer to a keyset
614 * Use: Decrements a keyset's reference counter. If the counter hits
615 * zero, the keyset is freed.
618 extern void ks_drop(keyset */*ks*/);
620 /* --- @ks_gen@ --- *
622 * Arguments: @const void *k@ = pointer to key material
623 * @size_t x, y, z@ = offsets into key material (see below)
624 * @peer *p@ = pointer to peer information
626 * Returns: A pointer to the new keyset.
628 * Use: Derives a new keyset from the given key material. The
629 * offsets @x@, @y@ and @z@ separate the key material into three
630 * parts. Between the @k@ and @k + x@ is `my' contribution to
631 * the key material; between @k + x@ and @k + y@ is `your'
632 * contribution; and between @k + y@ and @k + z@ is a shared
633 * value we made together. These are used to construct two
634 * pairs of symmetric keys. Each pair consists of an encryption
635 * key and a message authentication key. One pair is used for
636 * outgoing messages, the other for incoming messages.
638 * The new key is marked so that it won't be selected for output
639 * by @ksl_encrypt@. You can still encrypt data with it by
640 * calling @ks_encrypt@ directly.
643 extern keyset *ks_gen(const void */*k*/,
644 size_t /*x*/, size_t /*y*/, size_t /*z*/,
647 /* --- @ks_tregen@ --- *
649 * Arguments: @keyset *ks@ = pointer to a keyset
651 * Returns: The time at which moves ought to be made to replace this key.
654 extern time_t ks_tregen(keyset */*ks*/);
656 /* --- @ks_activate@ --- *
658 * Arguments: @keyset *ks@ = pointer to a keyset
662 * Use: Activates a keyset, so that it can be used for encrypting
666 extern void ks_activate(keyset */*ks*/);
668 /* --- @ks_encrypt@ --- *
670 * Arguments: @keyset *ks@ = pointer to a keyset
671 * @buf *b@ = pointer to input buffer
672 * @buf *bb@ = pointer to output buffer
674 * Returns: Zero if OK, nonzero if the key needs replacing. If the
675 * encryption failed, the output buffer is broken and zero is
678 * Use: Encrypts a block of data using the key. Note that the `key
679 * ought to be replaced' notification is only ever given once
680 * for each key. Also note that this call forces a keyset to be
681 * used even if it's marked as not for data output.
684 extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
686 /* --- @ks_decrypt@ --- *
688 * Arguments: @keyset *ks@ = pointer to a keyset
689 * @buf *b@ = pointer to an input buffer
690 * @buf *bb@ = pointer to an output buffer
692 * Returns: Zero on success, or nonzero if there was some problem.
694 * Use: Attempts to decrypt a message using a given key. Note that
695 * requesting decryption with a key directly won't clear a
696 * marking that it's not for encryption.
699 extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
701 /* --- @ksl_free@ --- *
703 * Arguments: @keyset **ksroot@ = pointer to keyset list head
707 * Use: Frees (releases references to) all of the keys in a keyset.
710 extern void ksl_free(keyset **/*ksroot*/);
712 /* --- @ksl_link@ --- *
714 * Arguments: @keyset **ksroot@ = pointer to keyset list head
715 * @keyset *ks@ = pointer to a keyset
719 * Use: Links a keyset into a list. A keyset can only be on one list
720 * at a time. Bad things happen otherwise.
723 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
725 /* --- @ksl_prune@ --- *
727 * Arguments: @keyset **ksroot@ = pointer to keyset list head
731 * Use: Prunes the keyset list by removing keys which mustn't be used
735 extern void ksl_prune(keyset **/*ksroot*/);
737 /* --- @ksl_encrypt@ --- *
739 * Arguments: @keyset **ksroot@ = pointer to keyset list head
740 * @buf *b@ = pointer to input buffer
741 * @buf *bb@ = pointer to output buffer
743 * Returns: Nonzero if a new key is needed.
745 * Use: Encrypts a packet.
748 extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
750 /* --- @ksl_decrypt@ --- *
752 * Arguments: @keyset **ksroot@ = pointer to keyset list head
753 * @buf *b@ = pointer to input buffer
754 * @buf *bb@ = pointer to output buffer
756 * Returns: Nonzero if the packet couldn't be decrypted.
758 * Use: Decrypts a packet.
761 extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
763 /*----- Administration interface ------------------------------------------*/
765 /* --- @a_warn@ --- *
767 * Arguments: @const char *fmt@ = pointer to format string
768 * @...@ = other arguments
772 * Use: Informs all admin connections of a warning.
775 extern void a_warn(const char */*fmt*/, ...);
777 /* --- @a_create@ --- *
779 * Arguments: @int fd_in, fd_out@ = file descriptors to use
783 * Use: Creates a new admin connection.
786 extern void a_create(int /*fd_in*/, int /*fd_out*/);
788 /* --- @a_quit@ --- *
794 * Use: Shuts things down nicely.
797 extern void a_quit(void);
799 /* --- @a_daemon@ --- *
805 * Use: Informs the admin module that it's a daemon.
808 extern void a_daemon(void);
810 /* --- @a_init@ --- *
812 * Arguments: @const char *sock@ = socket name to create
816 * Use: Creates the admin listening socket.
819 extern void a_init(const char */*sock*/);
821 /*----- Peer management ---------------------------------------------------*/
823 /* --- @p_txstart@ --- *
825 * Arguments: @peer *p@ = pointer to peer block
826 * @unsigned msg@ = message type code
828 * Returns: A pointer to a buffer to write to.
830 * Use: Starts sending to a peer. Only one send can happen at a
834 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
836 /* --- @p_txend@ --- *
838 * Arguments: @peer *p@ = pointer to peer block
842 * Use: Sends a packet to the peer.
845 extern void p_txend(peer */*p*/);
849 * Arguments: @peer *p@ = pointer to peer block
850 * @buf *b@ = buffer containing incoming packet
854 * Use: Handles a packet which needs to be sent to a peer.
857 extern void p_tun(peer */*p*/, buf */*b*/);
859 /* --- @p_interval@ --- *
865 * Use: Called periodically to do tidying.
868 extern void p_interval(void);
870 /* --- @p_stats@ --- *
872 * Arguments: @peer *p@ = pointer to a peer block
874 * Returns: A pointer to the peer's statistics.
877 extern stats *p_stats(peer */*p*/);
879 /* --- @p_ifname@ --- *
881 * Arguments: @peer *p@ = pointer to a peer block
883 * Returns: A pointer to the peer's interface name.
886 extern const char *p_ifname(peer */*p*/);
888 /* --- @p_addr@ --- *
890 * Arguments: @peer *p@ = pointer to a peer block
892 * Returns: A pointer to the peer's address.
895 extern const addr *p_addr(peer */*p*/);
897 /* --- @p_init@ --- *
899 * Arguments: @struct in_addr addr@ = address to bind to
900 * @unsigned port@ = port number to listen to
904 * Use: Initializes the peer system; creates the socket.
907 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
909 /* --- @p_port@ --- *
913 * Returns: Port number used for socket.
916 unsigned p_port(void);
918 /* --- @p_create@ --- *
920 * Arguments: @const char *name@ = name for this peer
921 * @struct sockaddr *sa@ = socket address of peer
922 * @size_t sz@ = size of socket address
924 * Returns: Pointer to the peer block, or null if it failed.
926 * Use: Creates a new named peer block. No peer is actually attached
930 extern peer *p_create(const char */*name*/,
931 struct sockaddr */*sa*/, size_t /*sz*/);
933 /* --- @p_name@ --- *
935 * Arguments: @peer *p@ = pointer to a peer block
937 * Returns: A pointer to the peer's name.
940 extern const char *p_name(peer */*p*/);
942 /* --- @p_find@ --- *
944 * Arguments: @const char *name@ = name to look up
946 * Returns: Pointer to the peer block, or null if not found.
948 * Use: Finds a peer by name.
951 extern peer *p_find(const char */*name*/);
953 /* --- @p_destroy@ --- *
955 * Arguments: @peer *p@ = pointer to a peer
959 * Use: Destroys a peer.
962 extern void p_destroy(peer */*p*/);
964 /* --- @p_first@, @p_next@ --- *
966 * Arguments: @peer *p@ = a peer block
968 * Returns: @peer_first@ returns the first peer in some ordering;
969 * @peer_next@ returns the peer following a given one in the
970 * same ordering. Null is returned for the end of the list.
973 extern peer *p_first(void);
974 extern peer *p_next(peer */*p*/);
976 /*----- Tunnel interface --------------------------------------------------*/
978 /* --- @tun_init@ --- *
984 * Use: Initializes the tunneling system. Maybe this will require
985 * opening file descriptors or something.
988 extern void tun_init(void);
990 /* --- @tun_create@ --- *
992 * Arguments: @tunnel *t@ = pointer to tunnel block
993 * @peer *p@ = pointer to peer block
995 * Returns: Zero if it worked, nonzero on failure.
997 * Use: Initializes a new tunnel.
1000 extern int tun_create(tunnel */*t*/, peer */*p*/);
1002 /* --- @tun_ifname@ --- *
1004 * Arguments: @tunnel *t@ = pointer to tunnel block
1006 * Returns: A pointer to the tunnel's interface name.
1009 extern const char *tun_ifname(tunnel */*t*/);
1011 /* --- @tun_inject@ --- *
1013 * Arguments: @tunnel *t@ = pointer to tunnel block
1014 * @buf *b@ = buffer to send
1018 * Use: Injects a packet into the local network stack.
1021 extern void tun_inject(tunnel */*t*/, buf */*b*/);
1023 /* --- @tun_destroy@ --- *
1025 * Arguments: @tunnel *t@ = pointer to tunnel block
1029 * Use: Destroys a tunnel.
1032 extern void tun_destroy(tunnel */*t*/);
1034 /*----- Other handy utilities ---------------------------------------------*/
1036 /* --- @mpstr@ --- *
1038 * Arguments: @mp *m@ = a multiprecision integer
1040 * Returns: A pointer to the integer's textual representation.
1042 * Use: Converts a multiprecision integer to a string. Corrupts
1046 extern const char *mpstr(mp */*m*/);
1048 /* --- @timestr@ --- *
1050 * Arguments: @time_t t@ = a time to convert
1052 * Returns: A pointer to a textual representation of the time.
1054 * Use: Converts a time to a textual representation. Corrupts
1058 extern const char *timestr(time_t /*t*/);
1060 /*----- That's all, folks -------------------------------------------------*/