3 * $Id: tripe.h,v 1.6 2001/02/19 19:11:09 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.6 2001/02/19 19:11:09 mdw
33 * Output buffering on admin connections.
35 * Revision 1.5 2001/02/16 21:41:43 mdw
36 * Major changes. See source files for details.
38 * Revision 1.4 2001/02/05 19:56:37 mdw
39 * Sequence number protection, and BSD tunnels.
41 * Revision 1.3 2001/02/04 01:17:55 mdw
42 * Create a configuration header file to tidy up command lines.
44 * Revision 1.2 2001/02/03 22:40:29 mdw
45 * Put timer information into the entropy pool when packets are received
46 * and on similar events. Reseed the generator on the interval timer.
48 * Revision 1.1 2001/02/03 20:26:37 mdw
60 /*----- Header files ------------------------------------------------------*/
75 #include <sys/types.h>
81 #include <sys/socket.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
90 #include <mLib/alloc.h>
91 #include <mLib/arena.h>
92 #include <mLib/bres.h>
93 #include <mLib/dstr.h>
95 #include <mLib/fdflags.h>
96 #include <mLib/fwatch.h>
97 #include <mLib/mdwopt.h>
98 #include <mLib/quis.h>
99 #include <mLib/report.h>
100 #include <mLib/sel.h>
101 #include <mLib/selbuf.h>
102 #include <mLib/sig.h>
103 #include <mLib/str.h>
104 #include <mLib/sub.h>
105 #include <mLib/trace.h>
107 #include <catacomb/gcipher.h>
108 #include <catacomb/gmac.h>
109 #include <catacomb/grand.h>
110 #include <catacomb/key.h>
111 #include <catacomb/paranoia.h>
113 #include <catacomb/noise.h>
114 #include <catacomb/rand.h>
116 #include <catacomb/mp.h>
117 #include <catacomb/mpmont.h>
118 #include <catacomb/mprand.h>
119 #include <catacomb/dh.h>
125 /*----- Magic numbers -----------------------------------------------------*/
127 /* --- Tunnel types --- */
133 /* --- Trace flags --- */
141 #define T_KEYEXCH 64u
142 #define T_KEYMGMT 128u
148 #define SEC(n) (n##u)
149 #define MIN(n) (n##u * 60u)
150 #define MEG(n) (n##ul * 1024ul * 1024ul)
152 /* --- Other things --- */
154 #define PKBUFSZ 65536
156 /*----- TrIPE protocol ----------------------------------------------------*/
158 /* --- TrIPE message format --- *
160 * A packet begins with a single-byte message type. The top four bits are a
161 * category code used to send the message to the right general place in the
162 * code; the bottom bits identify the actual message type.
165 #define MSG_CATMASK 0xf0
166 #define MSG_TYPEMASK 0x0f
168 /* --- Encrypted message packets --- *
170 * Messages of category @MSG_PACKET@ contain encrypted network packets. The
171 * message content is a symmetric-encrypted block (see below). Reception of
172 * a packet encrypted under a new key implicitly permits that key to be used
173 * to send further packets.
175 * The only packet type accepted is zero.
177 * Packets may be encrypted under any live keyset, but should use the most
181 #define MSG_PACKET 0x00
183 /* --- Key exchange packets --- */
185 #define MSG_KEYEXCH 0x10
187 #define KX_PRECHAL 0u
192 #define KX_SWITCHOK 5u
195 /* --- Symmetric encryption and keysets --- *
197 * Packets consist of a 64-bit MAC, a 32-bit sequence number, and the
200 * The MAC is computed using the HMAC construction with RIPEMD160 over the
201 * sequence number and the original packet plaintext; the first 64 bits of
202 * the output are used.
204 * The plaintext is encrypted using Blowfish in CBC mode with ciphertext
205 * stealing (as described in [Schneier]. The initialization vector is
206 * precisely the 64-bit MAC computed previously.
208 * A keyset consists of
210 * * an integrity (MAC) key;
211 * * a confidentiality (encryption) key; and
212 * * a sequence numbering space
214 * in each direction. The packets sent by a host encrypted under a
215 * particular keyset are assigned consecutive sequence numbers starting from
216 * zero. The receiving host must ensure that it only accepts each packet at
217 * most once. It should maintain a window of sequence numbers: packets with
218 * numbers beyond the end of the window are accepted and cause the window to
219 * be advanced; packets with numbers before the start of the window are
220 * rejected; packets with numbers which appear within the window are accepted
221 * only if the number has not been seen before.
223 * When a host sends a @KX_SWITCH@ or @KX_SWITCHOK@ message, it installs the
224 * newly-negotiated keyset in a `listen-only' state: it may not send a packet
225 * encrypted under the keyset until either it has received a @KX_SWITCH@ or
226 * @KX_SWITCHOK@ message, or a @MSG_PACKET@ encrypted under the keyset, from
230 /*----- Cipher selections -------------------------------------------------*/
232 #include <catacomb/blowfish.h>
233 #include <catacomb/blowfish-cbc.h>
234 #include <catacomb/rmd160.h>
235 #include <catacomb/rmd160-hmac.h>
237 #define CIPHER (&blowfish_cbc)
238 #define MAC (&rmd160_hmac)
240 #define HASH_CTX rmd160_ctx
241 #define HASH_INIT rmd160_init
242 #define HASH rmd160_hash
243 #define HASH_STRING(c, s) HASH((c), s, sizeof(s))
244 #define HASH_DONE rmd160_done
245 #define HASHSZ RMD160_HASHSZ
247 /*----- Data structures ---------------------------------------------------*/
251 * Buffers provide a simple stream-like interface for building and parsing
256 octet *base, *p, *limit; /* Pointers to the buffer */
257 unsigned f; /* Various flags */
260 #define BF_BROKEN 1u /* Buffer is broken */
262 /* --- Socket addresses --- *
264 * A magic union of supported socket addresses.
269 struct sockaddr_in sin;
272 /* --- A symmetric keyset --- *
274 * A keyset contains a set of symmetric keys for encrypting and decrypting
275 * packets. Keysets are stored in a list, sorted in reverse order of
276 * creation, so that the most recent keyset (the one most likely to be used)
279 * Each keyset has a time limit and a data limit. The keyset is destroyed
280 * when either it has existed for too long, or it has been used to encrypt
281 * too much data. New key exchanges are triggered when keys are close to
285 typedef struct keyset {
286 struct keyset *next; /* Next active keyset in the list */
287 unsigned ref; /* Reference count for keyset */
288 time_t t_exp; /* Expiry time for this keyset */
289 unsigned long sz_exp; /* Data limit for the keyset */
290 T( unsigned seq; ) /* Sequence number for tracing */
291 unsigned f; /* Various useful flags */
292 gcipher *cin, *cout; /* Keyset ciphers for encryption */
293 gmac *min, *mout; /* Keyset MACs for integrity */
294 uint32 oseq; /* Outbound sequence number */
295 uint32 iseq, iwin; /* Inbound sequence number */
298 #define KS_SEQWINSZ 32 /* Bits in sequence number window */
300 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
301 #define KSF_LINK 2u /* Key is in a linked list */
303 /* --- Key exchange --- *
305 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
306 * Protocol has a number of desirable features (e.g., perfect forward
307 * secrecy, and zero-knowledge authentication) which make it attractive for
308 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
315 typedef struct kxchal {
316 struct keyexch *kx; /* Pointer back to key exchange */
317 mp *c; /* Responder's challenge */
318 mp *r; /* My reply to the challenge */
319 keyset *ks; /* Pointer to temporary keyset */
320 unsigned f; /* Various useful flags */
321 sel_timer t; /* Response timer for challenge */
322 octet hc[HASHSZ]; /* Hash of his challenge */
323 octet hrx[HASHSZ]; /* My expected reply hash */
324 octet hswrq_in[HASHSZ]; /* Inbound switch request message */
325 octet hswok_in[HASHSZ]; /* Inbound switch confirmation */
326 octet hswrq_out[HASHSZ]; /* Outbound switch request message */
327 octet hswok_out[HASHSZ]; /* Outbound switch confirmation */
330 typedef struct keyexch {
331 struct peer *p; /* Pointer back to the peer */
332 keyset **ks; /* Peer's list of keysets */
333 unsigned f; /* Various useful flags */
334 unsigned s; /* Current state in exchange */
335 sel_timer t; /* Timer for next exchange */
336 dh_pub kpub; /* Peer's public key */
337 mp *alpha; /* My temporary secret */
338 mp *c; /* My challenge */
339 mp *rx; /* The expected response */
340 unsigned nr; /* Number of extant responses */
341 time_t t_valid; /* When this exchange goes bad */
342 octet hc[HASHSZ]; /* Hash of my challenge */
343 kxchal *r[KX_NCHAL]; /* Array of challenges */
346 #define KXF_TIMER 1u /* Waiting for a timer to go off */
349 KXS_DEAD, /* Uninitialized state (magical) */
350 KXS_CHAL, /* Main answer-challenges state */
351 KXS_COMMIT, /* Committed: send switch request */
352 KXS_SWITCH /* Switched: send confirmation */
355 /* --- Tunnel structure --- *
357 * Used to maintain system-specific information about the tunnel interface.
360 typedef struct tunnel {
361 #if TUN_TYPE == TUN_UNET
362 sel_file f; /* Selector for Usernet device */
363 struct peer *p; /* Pointer to my peer */
364 #elif TUN_TYPE == TUN_BSD
365 sel_file f; /* Selector for tunnel device */
366 struct peer *p; /* Pointer to my peer */
367 unsigned n; /* Number of my tunnel device */
369 # error "No support for this tunnel type"
373 /* --- Peer statistics --- *
375 * Contains various interesting and not-so-interesting statistics about a
376 * peer. This is updated by various parts of the code. The format of the
377 * structure isn't considered private, and @p_stats@ returns a pointer to the
378 * statistics block for a given peer.
381 typedef struct stats {
382 unsigned long sz_in, sz_out; /* Size of all data in and out */
383 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
384 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
385 time_t t_start, t_last; /* Time peer created, last recv */
386 unsigned long n_reject; /* Number of rejected packets */
387 unsigned long n_in, n_out; /* Number of packets in and out */
388 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
389 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
392 /* --- Peer structure --- *
394 * The main structure which glues everything else together.
397 typedef struct peer {
398 struct peer *next, *prev; /* Links to next and previous */
399 char *name; /* Name of this peer */
400 tunnel t; /* Tunnel for local packets */
401 keyset *ks; /* List head for keysets */
402 buf b; /* Buffer for sending packets */
403 addr peer; /* Peer socket address */
404 size_t sasz; /* Socket address size */
405 stats st; /* Statistics */
406 keyexch kx; /* Key exchange protocol block */
409 /* --- Admin structure --- */
411 #define OBUFSZ 16384u
413 typedef struct obuf {
414 struct obuf *next; /* Next buffer in list */
415 char *p_in, *p_out; /* Pointers into the buffer */
416 char buf[OBUFSZ]; /* The actual buffer */
419 typedef struct admin {
420 struct admin *next, *prev; /* Links to next and previous */
421 unsigned f; /* Various useful flags */
423 unsigned seq; /* Sequence number for tracing */
425 char *pname; /* Peer name to create */
426 char *paddr; /* Address string to resolve */
427 obuf *o_head, *o_tail; /* Output buffer list */
428 selbuf b; /* Line buffer for commands */
429 sel_file w; /* Selector for write buffering */
430 bres_client r; /* Background resolver task */
431 sel_timer t; /* Timer for resolver */
432 addr peer; /* Address to set */
433 size_t sasz; /* Size of the address */
436 #define AF_DEAD 1u /* Destroy this admin block */
437 #define AF_LOCK 2u /* Don't destroy it yet */
439 /*----- Global variables --------------------------------------------------*/
441 extern sel_state sel; /* Global I/O event state */
442 extern dh_priv kpriv; /* Our private key */
443 extern mpmont mg; /* Montgomery context for DH group */
444 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
447 extern const trace_opt tr_opts[]; /* Trace options array */
448 extern unsigned tr_flags; /* Trace options flags */
451 /*----- Other macros ------------------------------------------------------*/
453 #define TIMER noise_timer(RAND_GLOBAL)
455 /*----- Key management ----------------------------------------------------*/
457 /* --- @km_interval@ --- *
461 * Returns: Zero if OK, nonzero to force reloading of keys.
463 * Use: Called on the interval timer to perform various useful jobs.
466 extern int km_interval(void);
468 /* --- @km_init@ --- *
470 * Arguments: @const char *kr_priv@ = private keyring file
471 * @const char *kr_pub@ = public keyring file
472 * @const char *tag@ = tag to load
476 * Use: Initializes, and loads the private key.
479 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
480 const char */*tag*/);
482 /* --- @km_getpubkey@ --- *
484 * Arguments: @const char *tag@ = public key tag to load
485 * @dh_pub *kpub@ = where to put the public key
487 * Returns: Zero if OK, nonzero if it failed.
489 * Use: Fetches a public key from the keyring.
492 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
494 /*----- Key exchange ------------------------------------------------------*/
496 /* --- @kx_start@ --- *
498 * Arguments: @keyexch *kx@ = pointer to key exchange context
502 * Use: Stimulates a key exchange. If a key exchage is in progress,
503 * a new challenge is sent (unless the quiet timer forbids
504 * this); if no exchange is in progress, one is commenced.
507 extern void kx_start(keyexch */*kx*/);
509 /* --- @kx_message@ --- *
511 * Arguments: @keyexch *kx@ = pointer to key exchange context
512 * @unsigned msg@ = the message code
513 * @buf *b@ = pointer to buffer containing the packet
517 * Use: Reads a packet containing key exchange messages and handles
521 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
523 /* --- @kx_free@ --- *
525 * Arguments: @keyexch *kx@ = pointer to key exchange context
529 * Use: Frees everything in a key exchange context.
532 extern void kx_free(keyexch */*kx*/);
534 /* --- @kx_newkeys@ --- *
536 * Arguments: @keyexch *kx@ = pointer to key exchange context
540 * Use: Informs the key exchange module that its keys may have
541 * changed. If fetching the new keys fails, the peer will be
542 * destroyed, we log messages and struggle along with the old
546 extern void kx_newkeys(keyexch */*kx*/);
548 /* --- @kx_init@ --- *
550 * Arguments: @keyexch *kx@ = pointer to key exchange context
551 * @peer *p@ = pointer to peer context
552 * @keyset **ks@ = pointer to keyset list
554 * Returns: Zero if OK, nonzero if it failed.
556 * Use: Initializes a key exchange module. The module currently
557 * contains no keys, and will attempt to initiate a key
561 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
563 /*----- Keysets and symmetric cryptography --------------------------------*/
565 /* --- @ks_drop@ --- *
567 * Arguments: @keyset *ks@ = pointer to a keyset
571 * Use: Decrements a keyset's reference counter. If the counter hits
572 * zero, the keyset is freed.
575 extern void ks_drop(keyset */*ks*/);
577 /* --- @ks_gen@ --- *
579 * Arguments: @const void *k@ = pointer to key material
580 * @size_t x, y, z@ = offsets into key material (see below)
582 * Returns: A pointer to the new keyset.
584 * Use: Derives a new keyset from the given key material. The
585 * offsets @x@, @y@ and @z@ separate the key material into three
586 * parts. Between the @k@ and @k + x@ is `my' contribution to
587 * the key material; between @k + x@ and @k + y@ is `your'
588 * contribution; and between @k + y@ and @k + z@ is a shared
589 * value we made together. These are used to construct two
590 * pairs of symmetric keys. Each pair consists of an encryption
591 * key and a message authentication key. One pair is used for
592 * outgoing messages, the other for incoming messages.
594 * The new key is marked so that it won't be selected for output
595 * by @ksl_encrypt@. You can still encrypt data with it by
596 * calling @ks_encrypt@ directly.
599 extern keyset *ks_gen(const void */*k*/,
600 size_t /*x*/, size_t /*y*/, size_t /*z*/);
602 /* --- @ks_tregen@ --- *
604 * Arguments: @keyset *ks@ = pointer to a keyset
606 * Returns: The time at which moves ought to be made to replace this key.
609 extern time_t ks_tregen(keyset */*ks*/);
611 /* --- @ks_activate@ --- *
613 * Arguments: @keyset *ks@ = pointer to a keyset
617 * Use: Activates a keyset, so that it can be used for encrypting
621 extern void ks_activate(keyset */*ks*/);
623 /* --- @ks_encrypt@ --- *
625 * Arguments: @keyset *ks@ = pointer to a keyset
626 * @buf *b@ = pointer to input buffer
627 * @buf *bb@ = pointer to output buffer
629 * Returns: Zero if OK, nonzero if the key needs replacing. If the
630 * encryption failed, the output buffer is broken and zero is
633 * Use: Encrypts a block of data using the key. Note that the `key
634 * ought to be replaced' notification is only ever given once
635 * for each key. Also note that this call forces a keyset to be
636 * used even if it's marked as not for data output.
639 extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
641 /* --- @ks_decrypt@ --- *
643 * Arguments: @keyset *ks@ = pointer to a keyset
644 * @buf *b@ = pointer to an input buffer
645 * @buf *bb@ = pointer to an output buffer
647 * Returns: Zero on success, or nonzero if there was some problem.
649 * Use: Attempts to decrypt a message using a given key. Note that
650 * requesting decryption with a key directly won't clear a
651 * marking that it's not for encryption.
654 extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
656 /* --- @ksl_free@ --- *
658 * Arguments: @keyset **ksroot@ = pointer to keyset list head
662 * Use: Frees (releases references to) all of the keys in a keyset.
665 extern void ksl_free(keyset **/*ksroot*/);
667 /* --- @ksl_link@ --- *
669 * Arguments: @keyset **ksroot@ = pointer to keyset list head
670 * @keyset *ks@ = pointer to a keyset
674 * Use: Links a keyset into a list. A keyset can only be on one list
675 * at a time. Bad things happen otherwise.
678 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
680 /* --- @ksl_prune@ --- *
682 * Arguments: @keyset **ksroot@ = pointer to keyset list head
686 * Use: Prunes the keyset list by removing keys which mustn't be used
690 extern void ksl_prune(keyset **/*ksroot*/);
692 /* --- @ksl_encrypt@ --- *
694 * Arguments: @keyset **ksroot@ = pointer to keyset list head
695 * @buf *b@ = pointer to input buffer
696 * @buf *bb@ = pointer to output buffer
698 * Returns: Nonzero if a new key is needed.
700 * Use: Encrypts a packet.
703 extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
705 /* --- @ksl_decrypt@ --- *
707 * Arguments: @keyset **ksroot@ = pointer to keyset list head
708 * @buf *b@ = pointer to input buffer
709 * @buf *bb@ = pointer to output buffer
711 * Returns: Nonzero if the packet couldn't be decrypted.
713 * Use: Decrypts a packet.
716 extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
718 /*----- Administration interface ------------------------------------------*/
720 /* --- @a_warn@ --- *
722 * Arguments: @const char *fmt@ = pointer to format string
723 * @...@ = other arguments
727 * Use: Informs all admin connections of a warning.
730 extern void a_warn(const char */*fmt*/, ...);
732 /* --- @a_create@ --- *
734 * Arguments: @int fd_in, fd_out@ = file descriptors to use
738 * Use: Creates a new admin connection.
741 extern void a_create(int /*fd_in*/, int /*fd_out*/);
743 /* --- @a_quit@ --- *
749 * Use: Shuts things down nicely.
752 extern void a_quit(void);
754 /* --- @a_daemon@ --- *
760 * Use: Informs the admin module that it's a daemon.
763 extern void a_daemon(void);
765 /* --- @a_init@ --- *
767 * Arguments: @const char *sock@ = socket name to create
771 * Use: Creates the admin listening socket.
774 extern void a_init(const char */*sock*/);
776 /*----- Peer management ---------------------------------------------------*/
778 /* --- @p_txstart@ --- *
780 * Arguments: @peer *p@ = pointer to peer block
781 * @unsigned msg@ = message type code
783 * Returns: A pointer to a buffer to write to.
785 * Use: Starts sending to a peer. Only one send can happen at a
789 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
791 /* --- @p_txend@ --- *
793 * Arguments: @peer *p@ = pointer to peer block
797 * Use: Sends a packet to the peer.
800 extern void p_txend(peer */*p*/);
804 * Arguments: @peer *p@ = pointer to peer block
805 * @buf *b@ = buffer containing incoming packet
809 * Use: Handles a packet which needs to be sent to a peer.
812 extern void p_tun(peer */*p*/, buf */*b*/);
814 /* --- @p_interval@ --- *
820 * Use: Called periodically to do tidying.
823 extern void p_interval(void);
825 /* --- @p_stats@ --- *
827 * Arguments: @peer *p@ = pointer to a peer block
829 * Returns: A pointer to the peer's statistics.
832 extern stats *p_stats(peer */*p*/);
834 /* --- @p_ifname@ --- *
836 * Arguments: @peer *p@ = pointer to a peer block
838 * Returns: A pointer to the peer's interface name.
841 extern const char *p_ifname(peer */*p*/);
843 /* --- @p_addr@ --- *
845 * Arguments: @peer *p@ = pointer to a peer block
847 * Returns: A pointer to the peer's address.
850 extern const addr *p_addr(peer */*p*/);
852 /* --- @p_init@ --- *
854 * Arguments: @unsigned port@ = port number to listen to
858 * Use: Initializes the peer system; creates the socket.
861 extern void p_init(unsigned /*port*/);
863 /* --- @p_port@ --- *
867 * Returns: Port number used for socket.
870 unsigned p_port(void);
872 /* --- @p_create@ --- *
874 * Arguments: @const char *name@ = name for this peer
875 * @struct sockaddr *sa@ = socket address of peer
876 * @size_t sz@ = size of socket address
878 * Returns: Pointer to the peer block, or null if it failed.
880 * Use: Creates a new named peer block. No peer is actually attached
884 extern peer *p_create(const char */*name*/,
885 struct sockaddr */*sa*/, size_t /*sz*/);
887 /* --- @p_name@ --- *
889 * Arguments: @peer *p@ = pointer to a peer block
891 * Returns: A pointer to the peer's name.
894 extern const char *p_name(peer */*p*/);
896 /* --- @p_find@ --- *
898 * Arguments: @const char *name@ = name to look up
900 * Returns: Pointer to the peer block, or null if not found.
902 * Use: Finds a peer by name.
905 extern peer *p_find(const char */*name*/);
907 /* --- @p_destroy@ --- *
909 * Arguments: @peer *p@ = pointer to a peer
913 * Use: Destroys a peer.
916 extern void p_destroy(peer */*p*/);
918 /* --- @p_first@, @p_next@ --- *
920 * Arguments: @peer *p@ = a peer block
922 * Returns: @peer_first@ returns the first peer in some ordering;
923 * @peer_next@ returns the peer following a given one in the
924 * same ordering. Null is returned for the end of the list.
927 extern peer *p_first(void);
928 extern peer *p_next(peer */*p*/);
930 /*----- Tunnel interface --------------------------------------------------*/
932 /* --- @tun_init@ --- *
938 * Use: Initializes the tunneling system. Maybe this will require
939 * opening file descriptors or something.
942 extern void tun_init(void);
944 /* --- @tun_create@ --- *
946 * Arguments: @tunnel *t@ = pointer to tunnel block
947 * @peer *p@ = pointer to peer block
949 * Returns: Zero if it worked, nonzero on failure.
951 * Use: Initializes a new tunnel.
954 extern int tun_create(tunnel */*t*/, peer */*p*/);
956 /* --- @tun_ifname@ --- *
958 * Arguments: @tunnel *t@ = pointer to tunnel block
960 * Returns: A pointer to the tunnel's interface name.
963 extern const char *tun_ifname(tunnel */*t*/);
965 /* --- @tun_inject@ --- *
967 * Arguments: @tunnel *t@ = pointer to tunnel block
968 * @buf *b@ = buffer to send
972 * Use: Injects a packet into the local network stack.
975 extern void tun_inject(tunnel */*t*/, buf */*b*/);
977 /* --- @tun_destroy@ --- *
979 * Arguments: @tunnel *t@ = pointer to tunnel block
983 * Use: Destroys a tunnel.
986 extern void tun_destroy(tunnel */*t*/);
988 /*----- Buffer handling ---------------------------------------------------*/
990 /* --- Useful macros --- */
992 #define BBASE(b) ((b)->base)
993 #define BLIM(b) ((b)->limit)
994 #define BCUR(b) ((b)->p)
995 #define BSZ(b) ((b)->limit - (b)->base)
996 #define BLEN(b) ((b)->p - (b)->base)
997 #define BLEFT(b) ((b)->limit - (b)->p)
998 #define BSTEP(b, sz) ((b)->p += (sz))
999 #define BBAD(b) ((b)->f & BF_BROKEN)
1000 #define BOK(b) (!BBAD(b))
1002 #define BENSURE(b, sz) \
1003 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
1005 /* --- @buf_init@ --- *
1007 * Arguments: @buf *b@ = pointer to a buffer block
1008 * @void *p@ = pointer to a buffer
1009 * @size_t sz@ = size of the buffer
1013 * Use: Initializes the buffer block appropriately.
1016 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
1018 /* --- @buf_break@ --- *
1020 * Arguments: @buf *b@ = pointer to a buffer block
1022 * Returns: Some negative value.
1024 * Use: Marks a buffer as broken.
1027 extern int buf_break(buf */*b*/);
1029 /* --- @buf_flip@ --- *
1031 * Arguments: @buf *b@ = pointer to a buffer block
1035 * Use: Flips a buffer so that if you've just been writing to it,
1036 * you can now read from the bit you've written.
1039 extern void buf_flip(buf */*b*/);
1041 /* --- @buf_ensure@ --- *
1043 * Arguments: @buf *b@ = pointer to a buffer block
1044 * @size_t sz@ = size of data wanted
1046 * Returns: Zero if it worked, nonzero if there wasn't enough space.
1048 * Use: Ensures that there are @sz@ bytes still in the buffer.
1051 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
1053 /* --- @buf_get@ --- *
1055 * Arguments: @buf *b@ = pointer to a buffer block
1056 * @size_t sz@ = size of the buffer
1058 * Returns: Pointer to the place in the buffer.
1060 * Use: Reserves a space in the buffer of the requested size, and
1061 * returns its start address.
1064 extern void *buf_get(buf */*b*/, size_t /*sz*/);
1066 /* --- @buf_put@ --- *
1068 * Arguments: @buf *b@ = pointer to a buffer block
1069 * @const void *p@ = pointer to a buffer
1070 * @size_t sz@ = size of the buffer
1072 * Returns: Zero if it worked, nonzero if there wasn't enough space.
1074 * Use: Fetches data from some place and puts it in the buffer
1077 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
1079 /* --- @buf_getbyte@ --- *
1081 * Arguments: @buf *b@ = pointer to a buffer block
1083 * Returns: A byte, or less than zero if there wasn't a byte there.
1085 * Use: Gets a single byte from a buffer.
1088 extern int buf_getbyte(buf */*b*/);
1090 /* --- @buf_putbyte@ --- *
1092 * Arguments: @buf *b@ = pointer to a buffer block
1093 * @int ch@ = byte to write
1095 * Returns: Zero if OK, nonzero if there wasn't enough space.
1097 * Use: Puts a single byte in a buffer.
1100 extern int buf_putbyte(buf */*b*/, int /*ch*/);
1102 /* --- @buf_getword@ --- *
1104 * Arguments: @buf *b@ = pointer to a buffer block
1105 * @uint32 *w@ = where to put the word
1107 * Returns: Zero if OK, or nonzero if there wasn't a word there.
1109 * Use: Gets a 32-bit word from a buffer.
1112 extern int buf_getword(buf */*b*/, uint32 */*w*/);
1114 /* --- @buf_putword@ --- *
1116 * Arguments: @buf *b@ = pointer to a buffer block
1117 * @uint32 w@ = word to write
1119 * Returns: Zero if OK, nonzero if there wasn't enough space.
1121 * Use: Puts a 32-but word in a buffer.
1124 extern int buf_putword(buf */*b*/, uint32 /*w*/);
1126 /* --- @buf_getmp@ --- *
1128 * Arguments: @buf *b@ = pointer to a buffer block
1130 * Returns: A multiprecision integer, or null if there wasn't one there.
1132 * Use: Gets a multiprecision integer from a buffer.
1135 extern mp *buf_getmp(buf */*b*/);
1137 /* --- @buf_putmp@ --- *
1139 * Arguments: @buf *b@ = pointer to a buffer block
1140 * @mp *m@ = a multiprecision integer
1142 * Returns: Zero if it worked, nonzero if there wasn't enough space.
1144 * Use: Puts a multiprecision integer to a buffer.
1147 extern int buf_putmp(buf */*b*/, mp */*m*/);
1149 /*----- Other handy utilities ---------------------------------------------*/
1151 /* --- @mpstr@ --- *
1153 * Arguments: @mp *m@ = a multiprecision integer
1155 * Returns: A pointer to the integer's textual representation.
1157 * Use: Converts a multiprecision integer to a string. Corrupts
1161 extern const char *mpstr(mp */*m*/);
1163 /* --- @timestr@ --- *
1165 * Arguments: @time_t t@ = a time to convert
1167 * Returns: A pointer to a textual representation of the time.
1169 * Use: Converts a time to a textual representation. Corrupts
1173 extern const char *timestr(time_t /*t*/);
1175 /*----- That's all, folks -------------------------------------------------*/