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.
36 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
58 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
67 #include <mLib/alloc.h>
68 #include <mLib/arena.h>
69 #include <mLib/base64.h>
70 #include <mLib/bres.h>
71 #include <mLib/dstr.h>
73 #include <mLib/fdflags.h>
74 #include <mLib/fwatch.h>
75 #include <mLib/mdwopt.h>
76 #include <mLib/quis.h>
77 #include <mLib/report.h>
79 #include <mLib/selbuf.h>
83 #include <mLib/trace.h>
86 #include <catacomb/buf.h>
88 #include <catacomb/gcipher.h>
89 #include <catacomb/gmac.h>
90 #include <catacomb/grand.h>
91 #include <catacomb/key.h>
92 #include <catacomb/paranoia.h>
94 #include <catacomb/noise.h>
95 #include <catacomb/rand.h>
97 #include <catacomb/mp.h>
98 #include <catacomb/mprand.h>
99 #include <catacomb/dh.h>
100 #include <catacomb/ec.h>
101 #include <catacomb/ec-keys.h>
102 #include <catacomb/group.h>
104 #include "tripe-protocol.h"
109 /*----- Magic numbers -----------------------------------------------------*/
111 /* --- Trace flags --- */
119 #define T_KEYEXCH 64u
120 #define T_KEYMGMT 128u
127 #define SEC(n) (n##u)
128 #define MIN(n) (n##u * 60u)
129 #define MEG(n) (n##ul * 1024ul * 1024ul)
131 /* --- Other things --- */
133 #define PKBUFSZ 65536
135 /*----- Cipher selections -------------------------------------------------*/
137 typedef struct algswitch {
138 const gccipher *c; /* Symmetric encryption scheme */
139 const gccipher *mgf; /* Mask-generation function */
140 const gchash *h; /* Hash function */
141 const gcmac *m; /* Message authentication code */
142 size_t hashsz; /* Hash output size */
143 size_t tagsz; /* Length to truncate MAC tags */
144 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
147 extern algswitch algs;
149 #define MAXHASHSZ 64 /* Largest possible hash size */
151 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
153 /*----- Data structures ---------------------------------------------------*/
155 /* --- Socket addresses --- *
157 * A magic union of supported socket addresses.
162 struct sockaddr_in sin;
165 /* --- Sequence number checking --- */
167 typedef struct seqwin {
168 uint32 seq; /* First acceptable input sequence */
169 uint32 win; /* Window of acceptable numbers */
172 #define SEQ_WINSZ 32 /* Bits in sequence number window */
174 #define SEQ_RESET(iseq) {
176 #define SEQ_OK 0 /* Sequence number valid */
177 #define SEQ_OLD -1 /* Sequence number too old */
178 #define SEQ_REPLAY -2 /* Definitely replayed */
180 /* --- A symmetric keyset --- *
182 * A keyset contains a set of symmetric keys for encrypting and decrypting
183 * packets. Keysets are stored in a list, sorted in reverse order of
184 * creation, so that the most recent keyset (the one most likely to be used)
187 * Each keyset has a time limit and a data limit. The keyset is destroyed
188 * when either it has existed for too long, or it has been used to encrypt
189 * too much data. New key exchanges are triggered when keys are close to
193 typedef struct keyset {
194 struct keyset *next; /* Next active keyset in the list */
195 unsigned ref; /* Reference count for keyset */
196 struct peer *p; /* Pointer to peer structure */
197 time_t t_exp; /* Expiry time for this keyset */
198 unsigned long sz_exp; /* Data limit for the keyset */
199 T( unsigned seq; ) /* Sequence number for tracing */
200 unsigned f; /* Various useful flags */
201 gcipher *cin, *cout; /* Keyset ciphers for encryption */
202 size_t tagsz; /* Length to truncate MAC tags */
203 gmac *min, *mout; /* Keyset MACs for integrity */
204 uint32 oseq; /* Outbound sequence number */
205 seqwin iseq; /* Inbound sequence number */
208 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
209 #define KSF_LINK 2u /* Key is in a linked list */
211 /* --- Key exchange --- *
213 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
214 * Protocol has a number of desirable features (e.g., perfect forward
215 * secrecy, and zero-knowledge authentication) which make it attractive for
216 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
223 typedef struct kxchal {
224 struct keyexch *kx; /* Pointer back to key exchange */
225 ge *c; /* Responder's challenge */
226 ge *r; /* My reply to the challenge */
227 keyset *ks; /* Pointer to temporary keyset */
228 unsigned f; /* Various useful flags */
229 sel_timer t; /* Response timer for challenge */
230 octet hc[MAXHASHSZ]; /* Hash of his challenge */
231 mp *ck; /* The check value */
232 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
233 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
234 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
235 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
238 typedef struct keyexch {
239 struct peer *p; /* Pointer back to the peer */
240 keyset **ks; /* Peer's list of keysets */
241 unsigned f; /* Various useful flags */
242 unsigned s; /* Current state in exchange */
243 sel_timer t; /* Timer for next exchange */
244 ge *kpub; /* Peer's public key */
245 time_t texp_kpub; /* Expiry time for public key */
246 mp *alpha; /* My temporary secret */
247 ge *c; /* My challenge */
248 ge *rx; /* The expected response */
249 unsigned nr; /* Number of extant responses */
250 time_t t_valid; /* When this exchange goes bad */
251 octet hc[MAXHASHSZ]; /* Hash of my challenge */
252 kxchal *r[KX_NCHAL]; /* Array of challenges */
255 #define KXF_TIMER 1u /* Waiting for a timer to go off */
256 #define KXF_DEAD 2u /* The key-exchanger isn't up */
257 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
260 KXS_DEAD, /* Uninitialized state (magical) */
261 KXS_CHAL, /* Main answer-challenges state */
262 KXS_COMMIT, /* Committed: send switch request */
263 KXS_SWITCH /* Switched: send confirmation */
266 /* --- Tunnel structure --- *
268 * Used to maintain system-specific information about the tunnel interface.
271 typedef struct tunnel tunnel;
274 typedef struct tunnel_ops {
275 const char *name; /* Name of this tunnel driver */
276 void (*init)(void); /* Initializes the system */
277 tunnel *(*create)(struct peer */*p*/); /* Initializes a new tunnel */
278 const char *(*ifname)(tunnel */*t*/); /* Returns tunnel's interface name */
279 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
280 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
283 #ifndef TUN_INTERNALS
284 struct tunnel { const tunnel_ops *ops; };
287 /* --- Peer statistics --- *
289 * Contains various interesting and not-so-interesting statistics about a
290 * peer. This is updated by various parts of the code. The format of the
291 * structure isn't considered private, and @p_stats@ returns a pointer to the
292 * statistics block for a given peer.
295 typedef struct stats {
296 unsigned long sz_in, sz_out; /* Size of all data in and out */
297 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
298 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
299 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
300 unsigned long n_reject; /* Number of rejected packets */
301 unsigned long n_in, n_out; /* Number of packets in and out */
302 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
303 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
306 /* --- Peer structure --- *
308 * The main structure which glues everything else together.
311 typedef struct peerspec {
312 char *name; /* Peer's name */
313 const tunnel_ops *tops; /* Tunnel operations */
314 unsigned long t_ka; /* Keep alive interval */
315 addr sa; /* Socket address to speak to */
316 size_t sasz; /* Socket address size */
319 typedef struct peer {
320 struct peer *next, *prev; /* Links to next and previous */
321 struct ping *pings; /* Pings we're waiting for */
322 peerspec spec; /* Specifications for this peer */
323 tunnel *t; /* Tunnel for local packets */
324 keyset *ks; /* List head for keysets */
325 buf b; /* Buffer for sending packets */
326 stats st; /* Statistics */
327 keyexch kx; /* Key exchange protocol block */
328 sel_timer tka; /* Timer for keepalives */
331 typedef struct ping {
332 struct ping *next, *prev; /* Links to next and previous */
333 peer *p; /* Peer so we can free it */
334 unsigned msg; /* Kind of response expected */
335 uint32 id; /* Id so we can recognize response */
336 octet magic[32]; /* Some random data */
337 sel_timer t; /* Timeout for ping */
338 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
339 void *arg; /* Argument for callback */
350 /* --- Admin structure --- */
352 #define OBUFSZ 16384u
354 typedef struct obuf {
355 struct obuf *next; /* Next buffer in list */
356 char *p_in, *p_out; /* Pointers into the buffer */
357 char buf[OBUFSZ]; /* The actual buffer */
360 typedef struct oqueue {
361 obuf *hd, *tl; /* Head and tail pointers */
366 typedef struct admin_bgop {
367 struct admin_bgop *next, *prev; /* Links to next and previous */
368 struct admin *a; /* Owner job */
369 char *tag; /* Tag string for messages */
370 void (*cancel)(struct admin_bgop *); /* Destructor function */
373 typedef struct admin_resop {
374 admin_bgop bg; /* Background operation header */
375 char *addr; /* Hostname to be resolved */
376 bres_client r; /* Background resolver task */
377 sel_timer t; /* Timer for resolver */
378 addr sa; /* Socket address */
379 size_t sasz; /* Socket address size */
380 void (*func)(struct admin_resop *, int); /* Handler */
383 enum { ARES_OK, ARES_FAIL };
385 typedef struct admin_addop {
386 admin_resop r; /* Name resolution header */
387 peerspec peer; /* Peer pending creation */
390 typedef struct admin_greetop {
391 admin_resop r; /* Name resolution header */
392 void *c; /* Challenge block */
393 size_t sz; /* Length of challenge */
396 typedef struct admin_pingop {
397 admin_bgop bg; /* Background operation header */
398 ping ping; /* Ping pending response */
399 struct timeval pingtime; /* Time last ping was sent */
402 typedef struct admin {
403 struct admin *next, *prev; /* Links to next and previous */
404 unsigned f; /* Various useful flags */
405 unsigned ref; /* Reference counter */
407 unsigned seq; /* Sequence number for tracing */
409 oqueue out; /* Output buffer list */
410 oqueue delay; /* Delayed output buffer list */
411 admin_bgop *bg; /* Backgrounded operations */
412 selbuf b; /* Line buffer for commands */
413 sel_file w; /* Selector for write buffering */
416 #define AF_DEAD 1u /* Destroy this admin block */
417 #define AF_CLOSE 2u /* Client closed connection */
418 #define AF_NOTE 4u /* Catch notifications */
419 #define AF_WARN 8u /* Catch warning messages */
421 #define AF_TRACE 16u /* Catch tracing */
425 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
427 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
430 /*----- Global variables --------------------------------------------------*/
432 extern sel_state sel; /* Global I/O event state */
433 extern group *gg; /* The group we work in */
434 extern mp *kpriv; /* Our private key */
435 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
436 extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
437 extern const tunnel_ops *tun_default; /* Default tunnel to use */
440 extern const trace_opt tr_opts[]; /* Trace options array */
441 extern unsigned tr_flags; /* Trace options flags */
444 /*----- Other macros ------------------------------------------------------*/
446 #define TIMER noise_timer(RAND_GLOBAL)
448 /*----- Key management ----------------------------------------------------*/
450 /* --- @km_reload@ --- *
454 * Returns: Zero if OK, nonzero to force reloading of keys.
456 * Use: Checks the keyrings to see if they need reloading.
459 extern int km_reload(void);
461 /* --- @km_init@ --- *
463 * Arguments: @const char *kr_priv@ = private keyring file
464 * @const char *kr_pub@ = public keyring file
465 * @const char *tag@ = tag to load
469 * Use: Initializes, and loads the private key.
472 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
473 const char */*tag*/);
475 /* --- @km_getpubkey@ --- *
477 * Arguments: @const char *tag@ = public key tag to load
478 * @ge *kpub@ = where to put the public key
479 * @time_t *t_exp@ = where to put the expiry time
481 * Returns: Zero if OK, nonzero if it failed.
483 * Use: Fetches a public key from the keyring.
486 extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
489 /*----- Key exchange ------------------------------------------------------*/
491 /* --- @kx_start@ --- *
493 * Arguments: @keyexch *kx@ = pointer to key exchange context
494 * @int forcep@ = nonzero to ignore the quiet timer
498 * Use: Stimulates a key exchange. If a key exchage is in progress,
499 * a new challenge is sent (unless the quiet timer forbids
500 * this); if no exchange is in progress, one is commenced.
503 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
505 /* --- @kx_message@ --- *
507 * Arguments: @keyexch *kx@ = pointer to key exchange context
508 * @unsigned msg@ = the message code
509 * @buf *b@ = pointer to buffer containing the packet
513 * Use: Reads a packet containing key exchange messages and handles
517 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
519 /* --- @kx_free@ --- *
521 * Arguments: @keyexch *kx@ = pointer to key exchange context
525 * Use: Frees everything in a key exchange context.
528 extern void kx_free(keyexch */*kx*/);
530 /* --- @kx_newkeys@ --- *
532 * Arguments: @keyexch *kx@ = pointer to key exchange context
536 * Use: Informs the key exchange module that its keys may have
537 * changed. If fetching the new keys fails, the peer will be
538 * destroyed, we log messages and struggle along with the old
542 extern void kx_newkeys(keyexch */*kx*/);
544 /* --- @kx_init@ --- *
546 * Arguments: @keyexch *kx@ = pointer to key exchange context
547 * @peer *p@ = pointer to peer context
548 * @keyset **ks@ = pointer to keyset list
550 * Returns: Zero if OK, nonzero if it failed.
552 * Use: Initializes a key exchange module. The module currently
553 * contains no keys, and will attempt to initiate a key
557 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
559 /*----- Keysets and symmetric cryptography --------------------------------*/
561 /* --- @ks_drop@ --- *
563 * Arguments: @keyset *ks@ = pointer to a keyset
567 * Use: Decrements a keyset's reference counter. If the counter hits
568 * zero, the keyset is freed.
571 extern void ks_drop(keyset */*ks*/);
573 /* --- @ks_gen@ --- *
575 * Arguments: @const void *k@ = pointer to key material
576 * @size_t x, y, z@ = offsets into key material (see below)
577 * @peer *p@ = pointer to peer information
579 * Returns: A pointer to the new keyset.
581 * Use: Derives a new keyset from the given key material. The
582 * offsets @x@, @y@ and @z@ separate the key material into three
583 * parts. Between the @k@ and @k + x@ is `my' contribution to
584 * the key material; between @k + x@ and @k + y@ is `your'
585 * contribution; and between @k + y@ and @k + z@ is a shared
586 * value we made together. These are used to construct two
587 * pairs of symmetric keys. Each pair consists of an encryption
588 * key and a message authentication key. One pair is used for
589 * outgoing messages, the other for incoming messages.
591 * The new key is marked so that it won't be selected for output
592 * by @ksl_encrypt@. You can still encrypt data with it by
593 * calling @ks_encrypt@ directly.
596 extern keyset *ks_gen(const void */*k*/,
597 size_t /*x*/, size_t /*y*/, size_t /*z*/,
600 /* --- @ks_tregen@ --- *
602 * Arguments: @keyset *ks@ = pointer to a keyset
604 * Returns: The time at which moves ought to be made to replace this key.
607 extern time_t ks_tregen(keyset */*ks*/);
609 /* --- @ks_activate@ --- *
611 * Arguments: @keyset *ks@ = pointer to a keyset
615 * Use: Activates a keyset, so that it can be used for encrypting
619 extern void ks_activate(keyset */*ks*/);
621 /* --- @ks_encrypt@ --- *
623 * Arguments: @keyset *ks@ = pointer to a keyset
624 * @unsigned ty@ = message type
625 * @buf *b@ = pointer to input buffer
626 * @buf *bb@ = pointer to output buffer
628 * Returns: Zero if OK, nonzero if the key needs replacing. If the
629 * encryption failed, the output buffer is broken and zero is
632 * Use: Encrypts a block of data using the key. Note that the `key
633 * ought to be replaced' notification is only ever given once
634 * for each key. Also note that this call forces a keyset to be
635 * used even if it's marked as not for data output.
638 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
639 buf */*b*/, buf */*bb*/);
641 /* --- @ks_decrypt@ --- *
643 * Arguments: @keyset *ks@ = pointer to a keyset
644 * @unsigned ty@ = expected type code
645 * @buf *b@ = pointer to an input buffer
646 * @buf *bb@ = pointer to an output buffer
648 * Returns: Zero on success, or nonzero if there was some problem.
650 * Use: Attempts to decrypt a message using a given key. Note that
651 * requesting decryption with a key directly won't clear a
652 * marking that it's not for encryption.
655 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
656 buf */*b*/, buf */*bb*/);
658 /* --- @ksl_free@ --- *
660 * Arguments: @keyset **ksroot@ = pointer to keyset list head
664 * Use: Frees (releases references to) all of the keys in a keyset.
667 extern void ksl_free(keyset **/*ksroot*/);
669 /* --- @ksl_link@ --- *
671 * Arguments: @keyset **ksroot@ = pointer to keyset list head
672 * @keyset *ks@ = pointer to a keyset
676 * Use: Links a keyset into a list. A keyset can only be on one list
677 * at a time. Bad things happen otherwise.
680 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
682 /* --- @ksl_prune@ --- *
684 * Arguments: @keyset **ksroot@ = pointer to keyset list head
688 * Use: Prunes the keyset list by removing keys which mustn't be used
692 extern void ksl_prune(keyset **/*ksroot*/);
694 /* --- @ksl_encrypt@ --- *
696 * Arguments: @keyset **ksroot@ = pointer to keyset list head
697 * @unsigned ty@ = message type
698 * @buf *b@ = pointer to input buffer
699 * @buf *bb@ = pointer to output buffer
701 * Returns: Nonzero if a new key is needed.
703 * Use: Encrypts a packet.
706 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
707 buf */*b*/, buf */*bb*/);
709 /* --- @ksl_decrypt@ --- *
711 * Arguments: @keyset **ksroot@ = pointer to keyset list head
712 * @unsigned ty@ = expected type code
713 * @buf *b@ = pointer to input buffer
714 * @buf *bb@ = pointer to output buffer
716 * Returns: Nonzero if the packet couldn't be decrypted.
718 * Use: Decrypts a packet.
721 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
722 buf */*b*/, buf */*bb*/);
724 /*----- Challenges --------------------------------------------------------*/
728 * Arguments: @buf *b@ = where to put the challenge
730 * Returns: Zero if OK, nonzero on error.
732 * Use: Issues a new challenge.
735 extern int c_new(buf */*b*/);
737 /* --- @c_check@ --- *
739 * Arguments: @buf *b@ = where to find the challenge
741 * Returns: Zero if OK, nonzero if it didn't work.
743 * Use: Checks a challenge. On failure, the buffer is broken.
746 extern int c_check(buf */*b*/);
748 /*----- Administration interface ------------------------------------------*/
750 /* --- @a_warn@ --- *
752 * Arguments: @const char *fmt@ = pointer to format string
753 * @...@ = other arguments
757 * Use: Informs all admin connections of a warning.
760 extern void a_warn(const char */*fmt*/, ...);
762 /* --- @a_notify@ --- *
764 * Arguments: @const char *fmt@ = pointer to format string
765 * @...@ = other arguments
769 * Use: Sends a notification to interested admin connections.
772 extern void a_notify(const char */*fmt*/, ...);
774 /* --- @a_create@ --- *
776 * Arguments: @int fd_in, fd_out@ = file descriptors to use
777 * @unsigned f@ = initial flags to set
781 * Use: Creates a new admin connection.
784 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
786 /* --- @a_quit@ --- *
792 * Use: Shuts things down nicely.
795 extern void a_quit(void);
797 /* --- @a_daemon@ --- *
803 * Use: Informs the admin module that it's a daemon.
806 extern void a_daemon(void);
808 /* --- @a_init@ --- *
810 * Arguments: @const char *sock@ = socket name to create
814 * Use: Creates the admin listening socket.
817 extern void a_init(const char */*sock*/);
819 /*----- Peer management ---------------------------------------------------*/
821 /* --- @p_txstart@ --- *
823 * Arguments: @peer *p@ = pointer to peer block
824 * @unsigned msg@ = message type code
826 * Returns: A pointer to a buffer to write to.
828 * Use: Starts sending to a peer. Only one send can happen at a
832 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
834 /* --- @p_txend@ --- *
836 * Arguments: @peer *p@ = pointer to peer block
840 * Use: Sends a packet to the peer.
843 extern void p_txend(peer */*p*/);
845 /* --- @p_pingsend@ --- *
847 * Arguments: @peer *p@ = destination peer
848 * @ping *pg@ = structure to fill in
849 * @unsigned type@ = message type
850 * @unsigned long timeout@ = how long to wait before giving up
851 * @void (*func)(int, void *)@ = callback function
852 * @void *arg@ = argument for callback
854 * Returns: Zero if successful, nonzero if it failed.
856 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
857 * if we get an answer within the timeout, or zero if no answer.
860 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
861 unsigned long /*timeout*/,
862 void (*/*func*/)(int, void *), void */*arg*/);
864 /* --- @p_pingdone@ --- *
866 * Arguments: @ping *p@ = ping structure
867 * @int rc@ = return code to pass on
871 * Use: Disposes of a ping structure, maybe sending a notification.
874 extern void p_pingdone(ping */*p*/, int /*rc*/);
876 /* --- @p_greet@ --- *
878 * Arguments: @peer *p@ = peer to send to
879 * @const void *c@ = pointer to challenge
880 * @size_t sz@ = size of challenge
884 * Use: Sends a greeting packet.
887 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
891 * Arguments: @peer *p@ = pointer to peer block
892 * @buf *b@ = buffer containing incoming packet
896 * Use: Handles a packet which needs to be sent to a peer.
899 extern void p_tun(peer */*p*/, buf */*b*/);
901 /* --- @p_keyreload@ --- *
907 * Use: Forces a check of the daemon's keyring files.
910 extern void p_keyreload(void);
912 /* --- @p_interval@ --- *
918 * Use: Called periodically to do tidying.
921 extern void p_interval(void);
923 /* --- @p_stats@ --- *
925 * Arguments: @peer *p@ = pointer to a peer block
927 * Returns: A pointer to the peer's statistics.
930 extern stats *p_stats(peer */*p*/);
932 /* --- @p_ifname@ --- *
934 * Arguments: @peer *p@ = pointer to a peer block
936 * Returns: A pointer to the peer's interface name.
939 extern const char *p_ifname(peer */*p*/);
941 /* --- @p_addr@ --- *
943 * Arguments: @peer *p@ = pointer to a peer block
945 * Returns: A pointer to the peer's address.
948 extern const addr *p_addr(peer */*p*/);
950 /* --- @p_init@ --- *
952 * Arguments: @struct in_addr addr@ = address to bind to
953 * @unsigned port@ = port number to listen to
957 * Use: Initializes the peer system; creates the socket.
960 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
962 /* --- @p_port@ --- *
966 * Returns: Port number used for socket.
969 unsigned p_port(void);
971 /* --- @p_create@ --- *
973 * Arguments: @peerspec *spec@ = information about this peer
975 * Returns: Pointer to the peer block, or null if it failed.
977 * Use: Creates a new named peer block. No peer is actually attached
981 extern peer *p_create(peerspec */*spec*/);
983 /* --- @p_name@ --- *
985 * Arguments: @peer *p@ = pointer to a peer block
987 * Returns: A pointer to the peer's name.
989 * Use: Equivalent to @p_spec(p)->name@.
992 extern const char *p_name(peer */*p*/);
994 /* --- @p_spec@ --- *
996 * Arguments: @peer *p@ = pointer to a peer block
998 * Returns: Pointer to the peer's specification
1001 extern const peerspec *p_spec(peer */*p*/);
1003 /* --- @p_find@ --- *
1005 * Arguments: @const char *name@ = name to look up
1007 * Returns: Pointer to the peer block, or null if not found.
1009 * Use: Finds a peer by name.
1012 extern peer *p_find(const char */*name*/);
1014 /* --- @p_destroy@ --- *
1016 * Arguments: @peer *p@ = pointer to a peer
1020 * Use: Destroys a peer.
1023 extern void p_destroy(peer */*p*/);
1025 /* --- @p_first@, @p_next@ --- *
1027 * Arguments: @peer *p@ = a peer block
1029 * Returns: @peer_first@ returns the first peer in some ordering;
1030 * @peer_next@ returns the peer following a given one in the
1031 * same ordering. Null is returned for the end of the list.
1034 extern peer *p_first(void);
1035 extern peer *p_next(peer */*p*/);
1037 /*----- Tunnel drivers ----------------------------------------------------*/
1040 extern const tunnel_ops tun_linux;
1044 extern const tunnel_ops tun_unet;
1048 extern const tunnel_ops tun_bsd;
1051 extern const tunnel_ops tun_slip;
1053 /*----- Other handy utilities ---------------------------------------------*/
1055 /* --- @mpstr@ --- *
1057 * Arguments: @mp *m@ = a multiprecision integer
1059 * Returns: A pointer to the integer's textual representation.
1061 * Use: Converts a multiprecision integer to a string. Corrupts
1065 extern const char *mpstr(mp */*m*/);
1067 /* --- @gestr@ --- *
1069 * Arguments: @group *g@ = a group
1070 * @ge *x@ = a group element
1072 * Returns: A pointer to the element's textual representation.
1074 * Use: Converts a group element to a string. Corrupts
1078 extern const char *gestr(group */*g*/, ge */*x*/);
1080 /* --- @timestr@ --- *
1082 * Arguments: @time_t t@ = a time to convert
1084 * Returns: A pointer to a textual representation of the time.
1086 * Use: Converts a time to a textual representation. Corrupts
1090 extern const char *timestr(time_t /*t*/);
1092 /* --- @mystrieq@ --- *
1094 * Arguments: @const char *x, *y@ = two strings
1096 * Returns: True if @x@ and @y are equal, up to case.
1099 extern int mystrieq(const char */*x*/, const char */*y*/);
1101 /* --- @b64_encode@ --- *
1103 * Arguments: @const void *p@ = pointer to some gorp
1104 * @size_t sz@ = size of the gorp
1106 * Returns: Pointer to base64-encoded version in @buf_t@.
1109 extern const char *b64_encode(const void */*p*/, size_t /*sz*/);
1111 /* --- @seq_reset@ --- *
1113 * Arguments: @seqwin *s@ = sequence-checking window
1117 * Use: Resets a sequence number window.
1120 extern void seq_reset(seqwin */*s*/);
1122 /* --- @seq_check@ --- *
1124 * Arguments: @seqwin *s@ = sequence-checking window
1125 * @uint32 q@ = sequence number to check
1127 * Returns: A @SEQ_@ code.
1129 * Use: Checks a sequence number against the window, updating things
1133 extern int seq_check(seqwin */*s*/, uint32 /*q*/);
1135 /*----- That's all, folks -------------------------------------------------*/