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 "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 /* --- A symmetric keyset --- *
176 * A keyset contains a set of symmetric keys for encrypting and decrypting
177 * packets. Keysets are stored in a list, sorted in reverse order of
178 * creation, so that the most recent keyset (the one most likely to be used)
181 * Each keyset has a time limit and a data limit. The keyset is destroyed
182 * when either it has existed for too long, or it has been used to encrypt
183 * too much data. New key exchanges are triggered when keys are close to
187 typedef struct keyset
{
188 struct keyset
*next
; /* Next active keyset in the list */
189 unsigned ref
; /* Reference count for keyset */
190 struct peer
*p
; /* Pointer to peer structure */
191 time_t t_exp
; /* Expiry time for this keyset */
192 unsigned long sz_exp
; /* Data limit for the keyset */
193 T( unsigned seq
; ) /* Sequence number for tracing */
194 unsigned f
; /* Various useful flags */
195 gcipher
*cin
, *cout
; /* Keyset ciphers for encryption */
196 size_t tagsz
; /* Length to truncate MAC tags */
197 gmac
*min
, *mout
; /* Keyset MACs for integrity */
198 uint32 oseq
; /* Outbound sequence number */
199 seqwin iseq
; /* Inbound sequence number */
202 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
203 #define KSF_LINK 2u /* Key is in a linked list */
205 /* --- Key exchange --- *
207 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
208 * Protocol has a number of desirable features (e.g., perfect forward
209 * secrecy, and zero-knowledge authentication) which make it attractive for
210 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
216 typedef struct kxchal
{
217 struct keyexch
*kx
; /* Pointer back to key exchange */
218 ge
*c
; /* Responder's challenge */
219 ge
*r
; /* My reply to the challenge */
220 keyset
*ks
; /* Pointer to temporary keyset */
221 unsigned f
; /* Various useful flags */
222 sel_timer t
; /* Response timer for challenge */
223 octet hc
[MAXHASHSZ
]; /* Hash of his challenge */
224 octet ck
[MAXHASHSZ
]; /* His magical check value */
225 octet hswrq_in
[MAXHASHSZ
]; /* Inbound switch request message */
226 octet hswok_in
[MAXHASHSZ
]; /* Inbound switch confirmation */
227 octet hswrq_out
[MAXHASHSZ
]; /* Outbound switch request message */
228 octet hswok_out
[MAXHASHSZ
]; /* Outbound switch confirmation */
231 typedef struct keyexch
{
232 struct peer
*p
; /* Pointer back to the peer */
233 keyset
**ks
; /* Peer's list of keysets */
234 unsigned f
; /* Various useful flags */
235 unsigned s
; /* Current state in exchange */
236 sel_timer t
; /* Timer for next exchange */
237 ge
*kpub
; /* Peer's public key */
238 time_t texp_kpub
; /* Expiry time for public key */
239 mp
*alpha
; /* My temporary secret */
240 ge
*c
; /* My challenge */
241 ge
*rx
; /* The expected response */
242 unsigned nr
; /* Number of extant responses */
243 time_t t_valid
; /* When this exchange goes bad */
244 octet hc
[MAXHASHSZ
]; /* Hash of my challenge */
245 kxchal
*r
[KX_NCHAL
]; /* Array of challenges */
248 #define KXF_TIMER 1u /* Waiting for a timer to go off */
249 #define KXF_DEAD 2u /* The key-exchanger isn't up */
250 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
253 KXS_DEAD
, /* Uninitialized state (magical) */
254 KXS_CHAL
, /* Main answer-challenges state */
255 KXS_COMMIT
, /* Committed: send switch request */
256 KXS_SWITCH
/* Switched: send confirmation */
259 /* --- Tunnel structure --- *
261 * Used to maintain system-specific information about the tunnel interface.
264 typedef struct tunnel tunnel
;
267 typedef struct tunnel_ops
{
268 const char *name
; /* Name of this tunnel driver */
269 void (*init
)(void); /* Initializes the system */
270 tunnel
*(*create
)(struct peer */
*p*/
); /* Initializes a new tunnel */
271 const char *(*ifname
)(tunnel */
*t*/
); /* Returns tunnel's interface name */
272 void (*inject
)(tunnel */
*t*/
, buf */
*b*/
); /* Sends packet through if */
273 void (*destroy
)(tunnel */
*t*/
); /* Destroys a tunnel */
276 #ifndef TUN_INTERNALS
277 struct tunnel
{ const tunnel_ops
*ops
; };
280 /* --- Peer statistics --- *
282 * Contains various interesting and not-so-interesting statistics about a
283 * peer. This is updated by various parts of the code. The format of the
284 * structure isn't considered private, and @p_stats@ returns a pointer to the
285 * statistics block for a given peer.
288 typedef struct stats
{
289 unsigned long sz_in
, sz_out
; /* Size of all data in and out */
290 unsigned long sz_kxin
, sz_kxout
; /* Size of key exchange messages */
291 unsigned long sz_ipin
, sz_ipout
; /* Size of encapsulated IP packets */
292 time_t t_start
, t_last
, t_kx
; /* Time peer created, last pk, kx */
293 unsigned long n_reject
; /* Number of rejected packets */
294 unsigned long n_in
, n_out
; /* Number of packets in and out */
295 unsigned long n_kxin
, n_kxout
; /* Number of key exchange packets */
296 unsigned long n_ipin
, n_ipout
; /* Number of encrypted packets */
299 /* --- Peer structure --- *
301 * The main structure which glues everything else together.
304 typedef struct peerspec
{
305 char *name
; /* Peer's name */
306 const tunnel_ops
*tops
; /* Tunnel operations */
307 unsigned long t_ka
; /* Keep alive interval */
308 addr sa
; /* Socket address to speak to */
309 size_t sasz
; /* Socket address size */
312 typedef struct peer
{
313 struct peer
*next
, *prev
; /* Links to next and previous */
314 struct ping
*pings
; /* Pings we're waiting for */
315 peerspec spec
; /* Specifications for this peer */
316 tunnel
*t
; /* Tunnel for local packets */
317 keyset
*ks
; /* List head for keysets */
318 buf b
; /* Buffer for sending packets */
319 stats st
; /* Statistics */
320 keyexch kx
; /* Key exchange protocol block */
321 sel_timer tka
; /* Timer for keepalives */
324 typedef struct ping
{
325 struct ping
*next
, *prev
; /* Links to next and previous */
326 peer
*p
; /* Peer so we can free it */
327 unsigned msg
; /* Kind of response expected */
328 uint32 id
; /* Id so we can recognize response */
329 octet magic
[32]; /* Some random data */
330 sel_timer t
; /* Timeout for ping */
331 void (*func
)(int /*rc*/, void */
*arg*/
); /* Function to call when done */
332 void *arg
; /* Argument for callback */
343 /* --- Admin structure --- */
345 #define OBUFSZ 16384u
347 typedef struct obuf
{
348 struct obuf
*next
; /* Next buffer in list */
349 char *p_in
, *p_out
; /* Pointers into the buffer */
350 char buf
[OBUFSZ
]; /* The actual buffer */
353 typedef struct oqueue
{
354 obuf
*hd
, *tl
; /* Head and tail pointers */
359 typedef struct admin_bgop
{
360 struct admin_bgop
*next
, *prev
; /* Links to next and previous */
361 struct admin
*a
; /* Owner job */
362 char *tag
; /* Tag string for messages */
363 void (*cancel
)(struct admin_bgop
*); /* Destructor function */
366 typedef struct admin_resop
{
367 admin_bgop bg
; /* Background operation header */
368 char *addr
; /* Hostname to be resolved */
369 bres_client r
; /* Background resolver task */
370 sel_timer t
; /* Timer for resolver */
371 addr sa
; /* Socket address */
372 size_t sasz
; /* Socket address size */
373 void (*func
)(struct admin_resop
*, int); /* Handler */
376 enum { ARES_OK
, ARES_FAIL
};
378 typedef struct admin_addop
{
379 admin_resop r
; /* Name resolution header */
380 peerspec peer
; /* Peer pending creation */
383 typedef struct admin_greetop
{
384 admin_resop r
; /* Name resolution header */
385 void *c
; /* Challenge block */
386 size_t sz
; /* Length of challenge */
389 typedef struct admin_pingop
{
390 admin_bgop bg
; /* Background operation header */
391 ping ping
; /* Ping pending response */
392 struct timeval pingtime
; /* Time last ping was sent */
395 typedef struct admin
{
396 struct admin
*next
, *prev
; /* Links to next and previous */
397 unsigned f
; /* Various useful flags */
398 unsigned ref
; /* Reference counter */
400 unsigned seq
; /* Sequence number for tracing */
402 oqueue out
; /* Output buffer list */
403 oqueue delay
; /* Delayed output buffer list */
404 admin_bgop
*bg
; /* Backgrounded operations */
405 selbuf b
; /* Line buffer for commands */
406 sel_file w
; /* Selector for write buffering */
409 #define AF_DEAD 1u /* Destroy this admin block */
410 #define AF_CLOSE 2u /* Client closed connection */
411 #define AF_NOTE 4u /* Catch notifications */
412 #define AF_WARN 8u /* Catch warning messages */
414 #define AF_TRACE 16u /* Catch tracing */
418 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
420 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
423 /*----- Global variables --------------------------------------------------*/
425 extern sel_state sel
; /* Global I/O event state */
426 extern group
*gg
; /* The group we work in */
427 extern size_t indexsz
; /* Size of exponent for the group */
428 extern mp
*kpriv
; /* Our private key */
429 extern ge
*kpub
; /* Our public key */
430 extern octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
];
431 extern const tunnel_ops
*tunnels
[]; /* Table of tunnels (0-term) */
432 extern const tunnel_ops
*tun_default
; /* Default tunnel to use */
435 extern const trace_opt tr_opts
[]; /* Trace options array */
436 extern unsigned tr_flags
; /* Trace options flags */
439 /*----- Other macros ------------------------------------------------------*/
441 #define TIMER noise_timer(RAND_GLOBAL)
443 /*----- Key management ----------------------------------------------------*/
445 /* --- @km_reload@ --- *
449 * Returns: Zero if OK, nonzero to force reloading of keys.
451 * Use: Checks the keyrings to see if they need reloading.
454 extern int km_reload(void);
456 /* --- @km_init@ --- *
458 * Arguments: @const char *kr_priv@ = private keyring file
459 * @const char *kr_pub@ = public keyring file
460 * @const char *tag@ = tag to load
464 * Use: Initializes, and loads the private key.
467 extern void km_init(const char */
*kr_priv*/
, const char */
*kr_pub*/
,
468 const char */
*tag*/
);
470 /* --- @km_getpubkey@ --- *
472 * Arguments: @const char *tag@ = public key tag to load
473 * @ge *kpub@ = where to put the public key
474 * @time_t *t_exp@ = where to put the expiry time
476 * Returns: Zero if OK, nonzero if it failed.
478 * Use: Fetches a public key from the keyring.
481 extern int km_getpubkey(const char */
*tag*/
, ge */
*kpub*/
,
484 /*----- Key exchange ------------------------------------------------------*/
486 /* --- @kx_start@ --- *
488 * Arguments: @keyexch *kx@ = pointer to key exchange context
489 * @int forcep@ = nonzero to ignore the quiet timer
493 * Use: Stimulates a key exchange. If a key exchage is in progress,
494 * a new challenge is sent (unless the quiet timer forbids
495 * this); if no exchange is in progress, one is commenced.
498 extern void kx_start(keyexch */
*kx*/
, int /*forcep*/);
500 /* --- @kx_message@ --- *
502 * Arguments: @keyexch *kx@ = pointer to key exchange context
503 * @unsigned msg@ = the message code
504 * @buf *b@ = pointer to buffer containing the packet
508 * Use: Reads a packet containing key exchange messages and handles
512 extern void kx_message(keyexch */
*kx*/
, unsigned /*msg*/, buf */
*b*/
);
514 /* --- @kx_free@ --- *
516 * Arguments: @keyexch *kx@ = pointer to key exchange context
520 * Use: Frees everything in a key exchange context.
523 extern void kx_free(keyexch */
*kx*/
);
525 /* --- @kx_newkeys@ --- *
527 * Arguments: @keyexch *kx@ = pointer to key exchange context
531 * Use: Informs the key exchange module that its keys may have
532 * changed. If fetching the new keys fails, the peer will be
533 * destroyed, we log messages and struggle along with the old
537 extern void kx_newkeys(keyexch */
*kx*/
);
539 /* --- @kx_init@ --- *
541 * Arguments: @keyexch *kx@ = pointer to key exchange context
542 * @peer *p@ = pointer to peer context
543 * @keyset **ks@ = pointer to keyset list
545 * Returns: Zero if OK, nonzero if it failed.
547 * Use: Initializes a key exchange module. The module currently
548 * contains no keys, and will attempt to initiate a key
552 extern int kx_init(keyexch */
*kx*/
, peer */
*p*/
, keyset
**/
*ks*/
);
554 /*----- Keysets and symmetric cryptography --------------------------------*/
556 /* --- @ks_drop@ --- *
558 * Arguments: @keyset *ks@ = pointer to a keyset
562 * Use: Decrements a keyset's reference counter. If the counter hits
563 * zero, the keyset is freed.
566 extern void ks_drop(keyset */
*ks*/
);
568 /* --- @ks_gen@ --- *
570 * Arguments: @const void *k@ = pointer to key material
571 * @size_t x, y, z@ = offsets into key material (see below)
572 * @peer *p@ = pointer to peer information
574 * Returns: A pointer to the new keyset.
576 * Use: Derives a new keyset from the given key material. The
577 * offsets @x@, @y@ and @z@ separate the key material into three
578 * parts. Between the @k@ and @k + x@ is `my' contribution to
579 * the key material; between @k + x@ and @k + y@ is `your'
580 * contribution; and between @k + y@ and @k + z@ is a shared
581 * value we made together. These are used to construct two
582 * pairs of symmetric keys. Each pair consists of an encryption
583 * key and a message authentication key. One pair is used for
584 * outgoing messages, the other for incoming messages.
586 * The new key is marked so that it won't be selected for output
587 * by @ksl_encrypt@. You can still encrypt data with it by
588 * calling @ks_encrypt@ directly.
591 extern keyset
*ks_gen(const void */
*k*/
,
592 size_t /*x*/, size_t /*y*/, size_t /*z*/,
595 /* --- @ks_tregen@ --- *
597 * Arguments: @keyset *ks@ = pointer to a keyset
599 * Returns: The time at which moves ought to be made to replace this key.
602 extern time_t ks_tregen(keyset */
*ks*/
);
604 /* --- @ks_activate@ --- *
606 * Arguments: @keyset *ks@ = pointer to a keyset
610 * Use: Activates a keyset, so that it can be used for encrypting
614 extern void ks_activate(keyset */
*ks*/
);
616 /* --- @ks_encrypt@ --- *
618 * Arguments: @keyset *ks@ = pointer to a keyset
619 * @unsigned ty@ = message type
620 * @buf *b@ = pointer to input buffer
621 * @buf *bb@ = pointer to output buffer
623 * Returns: Zero if OK, nonzero if the key needs replacing. If the
624 * encryption failed, the output buffer is broken and zero is
627 * Use: Encrypts a block of data using the key. Note that the `key
628 * ought to be replaced' notification is only ever given once
629 * for each key. Also note that this call forces a keyset to be
630 * used even if it's marked as not for data output.
633 extern int ks_encrypt(keyset */
*ks*/
, unsigned /*ty*/,
634 buf */
*b*/
, buf */
*bb*/
);
636 /* --- @ks_decrypt@ --- *
638 * Arguments: @keyset *ks@ = pointer to a keyset
639 * @unsigned ty@ = expected type code
640 * @buf *b@ = pointer to an input buffer
641 * @buf *bb@ = pointer to an output buffer
643 * Returns: Zero on success, or nonzero if there was some problem.
645 * Use: Attempts to decrypt a message using a given key. Note that
646 * requesting decryption with a key directly won't clear a
647 * marking that it's not for encryption.
650 extern int ks_decrypt(keyset */
*ks*/
, unsigned /*ty*/,
651 buf */
*b*/
, buf */
*bb*/
);
653 /* --- @ksl_free@ --- *
655 * Arguments: @keyset **ksroot@ = pointer to keyset list head
659 * Use: Frees (releases references to) all of the keys in a keyset.
662 extern void ksl_free(keyset
**/
*ksroot*/
);
664 /* --- @ksl_link@ --- *
666 * Arguments: @keyset **ksroot@ = pointer to keyset list head
667 * @keyset *ks@ = pointer to a keyset
671 * Use: Links a keyset into a list. A keyset can only be on one list
672 * at a time. Bad things happen otherwise.
675 extern void ksl_link(keyset
**/
*ksroot*/
, keyset */
*ks*/
);
677 /* --- @ksl_prune@ --- *
679 * Arguments: @keyset **ksroot@ = pointer to keyset list head
683 * Use: Prunes the keyset list by removing keys which mustn't be used
687 extern void ksl_prune(keyset
**/
*ksroot*/
);
689 /* --- @ksl_encrypt@ --- *
691 * Arguments: @keyset **ksroot@ = pointer to keyset list head
692 * @unsigned ty@ = message type
693 * @buf *b@ = pointer to input buffer
694 * @buf *bb@ = pointer to output buffer
696 * Returns: Nonzero if a new key is needed.
698 * Use: Encrypts a packet.
701 extern int ksl_encrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
702 buf */
*b*/
, buf */
*bb*/
);
704 /* --- @ksl_decrypt@ --- *
706 * Arguments: @keyset **ksroot@ = pointer to keyset list head
707 * @unsigned ty@ = expected type code
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*/
, unsigned /*ty*/,
717 buf */
*b*/
, buf */
*bb*/
);
719 /*----- Challenges --------------------------------------------------------*/
723 * Arguments: @buf *b@ = where to put the challenge
725 * Returns: Zero if OK, nonzero on error.
727 * Use: Issues a new challenge.
730 extern int c_new(buf */
*b*/
);
732 /* --- @c_check@ --- *
734 * Arguments: @buf *b@ = where to find the challenge
736 * Returns: Zero if OK, nonzero if it didn't work.
738 * Use: Checks a challenge. On failure, the buffer is broken.
741 extern int c_check(buf */
*b*/
);
743 /*----- Administration interface ------------------------------------------*/
745 #define A_END ((char *)0)
747 /* --- @a_warn@ --- *
749 * Arguments: @const char *fmt@ = pointer to format string
750 * @...@ = other arguments
754 * Use: Informs all admin connections of a warning.
757 extern void a_warn(const char */
*fmt*/
, ...);
759 /* --- @a_notify@ --- *
761 * Arguments: @const char *fmt@ = pointer to format string
762 * @...@ = other arguments
766 * Use: Sends a notification to interested admin connections.
769 extern void a_notify(const char */
*fmt*/
, ...);
771 /* --- @a_create@ --- *
773 * Arguments: @int fd_in, fd_out@ = file descriptors to use
774 * @unsigned f@ = initial flags to set
778 * Use: Creates a new admin connection.
781 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
783 /* --- @a_quit@ --- *
789 * Use: Shuts things down nicely.
792 extern void a_quit(void);
794 /* --- @a_daemon@ --- *
800 * Use: Informs the admin module that it's a daemon.
803 extern void a_daemon(void);
805 /* --- @a_init@ --- *
807 * Arguments: @const char *sock@ = socket name to create
811 * Use: Creates the admin listening socket.
814 extern void a_init(const char */
*sock*/
);
816 /*----- Peer management ---------------------------------------------------*/
818 /* --- @p_txstart@ --- *
820 * Arguments: @peer *p@ = pointer to peer block
821 * @unsigned msg@ = message type code
823 * Returns: A pointer to a buffer to write to.
825 * Use: Starts sending to a peer. Only one send can happen at a
829 extern buf
*p_txstart(peer */
*p*/
, unsigned /*msg*/);
831 /* --- @p_txend@ --- *
833 * Arguments: @peer *p@ = pointer to peer block
837 * Use: Sends a packet to the peer.
840 extern void p_txend(peer */
*p*/
);
842 /* --- @p_pingsend@ --- *
844 * Arguments: @peer *p@ = destination peer
845 * @ping *pg@ = structure to fill in
846 * @unsigned type@ = message type
847 * @unsigned long timeout@ = how long to wait before giving up
848 * @void (*func)(int, void *)@ = callback function
849 * @void *arg@ = argument for callback
851 * Returns: Zero if successful, nonzero if it failed.
853 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
854 * if we get an answer within the timeout, or zero if no answer.
857 extern int p_pingsend(peer */
*p*/
, ping */
*pg*/
, unsigned /*type*/,
858 unsigned long /*timeout*/,
859 void (*/
*func*/
)(int, void *), void */
*arg*/
);
861 /* --- @p_pingdone@ --- *
863 * Arguments: @ping *p@ = ping structure
864 * @int rc@ = return code to pass on
868 * Use: Disposes of a ping structure, maybe sending a notification.
871 extern void p_pingdone(ping */
*p*/
, int /*rc*/);
873 /* --- @p_greet@ --- *
875 * Arguments: @peer *p@ = peer to send to
876 * @const void *c@ = pointer to challenge
877 * @size_t sz@ = size of challenge
881 * Use: Sends a greeting packet.
884 extern void p_greet(peer */
*p*/
, const void */
*c*/
, size_t /*sz*/);
888 * Arguments: @peer *p@ = pointer to peer block
889 * @buf *b@ = buffer containing incoming packet
893 * Use: Handles a packet which needs to be sent to a peer.
896 extern void p_tun(peer */
*p*/
, buf */
*b*/
);
898 /* --- @p_keyreload@ --- *
904 * Use: Forces a check of the daemon's keyring files.
907 extern void p_keyreload(void);
909 /* --- @p_interval@ --- *
915 * Use: Called periodically to do tidying.
918 extern void p_interval(void);
920 /* --- @p_stats@ --- *
922 * Arguments: @peer *p@ = pointer to a peer block
924 * Returns: A pointer to the peer's statistics.
927 extern stats
*p_stats(peer */
*p*/
);
929 /* --- @p_ifname@ --- *
931 * Arguments: @peer *p@ = pointer to a peer block
933 * Returns: A pointer to the peer's interface name.
936 extern const char *p_ifname(peer */
*p*/
);
938 /* --- @p_addr@ --- *
940 * Arguments: @peer *p@ = pointer to a peer block
942 * Returns: A pointer to the peer's address.
945 extern const addr
*p_addr(peer */
*p*/
);
947 /* --- @p_init@ --- *
949 * Arguments: @struct in_addr addr@ = address to bind to
950 * @unsigned port@ = port number to listen to
954 * Use: Initializes the peer system; creates the socket.
957 extern void p_init(struct in_addr
/*addr*/, unsigned /*port*/);
959 /* --- @p_port@ --- *
963 * Returns: Port number used for socket.
966 unsigned p_port(void);
968 /* --- @p_create@ --- *
970 * Arguments: @peerspec *spec@ = information about this peer
972 * Returns: Pointer to the peer block, or null if it failed.
974 * Use: Creates a new named peer block. No peer is actually attached
978 extern peer
*p_create(peerspec */
*spec*/
);
980 /* --- @p_name@ --- *
982 * Arguments: @peer *p@ = pointer to a peer block
984 * Returns: A pointer to the peer's name.
986 * Use: Equivalent to @p_spec(p)->name@.
989 extern const char *p_name(peer */
*p*/
);
991 /* --- @p_spec@ --- *
993 * Arguments: @peer *p@ = pointer to a peer block
995 * Returns: Pointer to the peer's specification
998 extern const peerspec
*p_spec(peer */
*p*/
);
1000 /* --- @p_find@ --- *
1002 * Arguments: @const char *name@ = name to look up
1004 * Returns: Pointer to the peer block, or null if not found.
1006 * Use: Finds a peer by name.
1009 extern peer
*p_find(const char */
*name*/
);
1011 /* --- @p_destroy@ --- *
1013 * Arguments: @peer *p@ = pointer to a peer
1017 * Use: Destroys a peer.
1020 extern void p_destroy(peer */
*p*/
);
1022 /* --- @p_first@, @p_next@ --- *
1024 * Arguments: @peer *p@ = a peer block
1026 * Returns: @peer_first@ returns the first peer in some ordering;
1027 * @peer_next@ returns the peer following a given one in the
1028 * same ordering. Null is returned for the end of the list.
1031 extern peer
*p_first(void);
1032 extern peer
*p_next(peer */
*p*/
);
1034 /*----- Tunnel drivers ----------------------------------------------------*/
1037 extern const tunnel_ops tun_linux
;
1041 extern const tunnel_ops tun_unet
;
1045 extern const tunnel_ops tun_bsd
;
1048 extern const tunnel_ops tun_slip
;
1050 /*----- Other handy utilities ---------------------------------------------*/
1052 /* --- @mpstr@ --- *
1054 * Arguments: @mp *m@ = a multiprecision integer
1056 * Returns: A pointer to the integer's textual representation.
1058 * Use: Converts a multiprecision integer to a string. Corrupts
1062 extern const char *mpstr(mp */
*m*/
);
1064 /* --- @gestr@ --- *
1066 * Arguments: @group *g@ = a group
1067 * @ge *x@ = a group element
1069 * Returns: A pointer to the element's textual representation.
1071 * Use: Converts a group element to a string. Corrupts
1075 extern const char *gestr(group */
*g*/
, ge */
*x*/
);
1077 /* --- @timestr@ --- *
1079 * Arguments: @time_t t@ = a time to convert
1081 * Returns: A pointer to a textual representation of the time.
1083 * Use: Converts a time to a textual representation. Corrupts
1087 extern const char *timestr(time_t /*t*/);
1089 /* --- @mystrieq@ --- *
1091 * Arguments: @const char *x, *y@ = two strings
1093 * Returns: True if @x@ and @y are equal, up to case.
1096 extern int mystrieq(const char */
*x*/
, const char */
*y*/
);
1098 /* --- @seq_reset@ --- *
1100 * Arguments: @seqwin *s@ = sequence-checking window
1104 * Use: Resets a sequence number window.
1107 extern void seq_reset(seqwin */
*s*/
);
1109 /* --- @seq_check@ --- *
1111 * Arguments: @seqwin *s@ = sequence-checking window
1112 * @uint32 q@ = sequence number to check
1113 * @const char *service@ = service to report message from
1115 * Returns: A @SEQ_@ code.
1117 * Use: Checks a sequence number against the window, updating things
1121 extern int seq_check(seqwin */
*s*/
, uint32
/*q*/, const char */
*service*/
);
1123 /*----- That's all, folks -------------------------------------------------*/