Allow different peer associations to use different private keys.
[tripe] / server / tripe.h
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 3 * Main header file for TrIPE
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27#ifndef TRIPE_H
28#define TRIPE_H
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34/*----- Header files ------------------------------------------------------*/
35
73189848 36#include "config.h"
37
410c8acf 38#include <assert.h>
39#include <ctype.h>
40#include <errno.h>
b9066fbb 41#include <limits.h>
410c8acf 42#include <signal.h>
43#include <stdarg.h>
44#include <stddef.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <time.h>
49
50#include <sys/types.h>
51#include <sys/time.h>
52#include <unistd.h>
53#include <fcntl.h>
54#include <sys/stat.h>
388e0319 55#include <sys/wait.h>
410c8acf 56
57#include <sys/socket.h>
58#include <sys/un.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
61#include <netdb.h>
62
63#include <pwd.h>
64#include <grp.h>
65
66#include <mLib/alloc.h>
67#include <mLib/arena.h>
37941236 68#include <mLib/base64.h>
410c8acf 69#include <mLib/bres.h>
19dd2531 70#include <mLib/daemonize.h>
410c8acf 71#include <mLib/dstr.h>
72#include <mLib/env.h>
73#include <mLib/fdflags.h>
388e0319 74#include <mLib/fdpass.h>
410c8acf 75#include <mLib/fwatch.h>
c8e02c8a 76#include <mLib/hash.h>
165efde7 77#include <mLib/macros.h>
b9537f3b 78#include <mLib/mdup.h>
410c8acf 79#include <mLib/mdwopt.h>
80#include <mLib/quis.h>
81#include <mLib/report.h>
82#include <mLib/sel.h>
83#include <mLib/selbuf.h>
84#include <mLib/sig.h>
85#include <mLib/str.h>
86#include <mLib/sub.h>
87#include <mLib/trace.h>
0ba8de86 88#include <mLib/tv.h>
19dd2531 89#include <mLib/versioncmp.h>
410c8acf 90
165db1a8 91#include <catacomb/buf.h>
92
410c8acf 93#include <catacomb/gcipher.h>
94#include <catacomb/gmac.h>
95#include <catacomb/grand.h>
96#include <catacomb/key.h>
97#include <catacomb/paranoia.h>
98
410c8acf 99#include <catacomb/noise.h>
100#include <catacomb/rand.h>
410c8acf 101
102#include <catacomb/mp.h>
410c8acf 103#include <catacomb/mprand.h>
104#include <catacomb/dh.h>
52c03a2a 105#include <catacomb/ec.h>
106#include <catacomb/ec-keys.h>
107#include <catacomb/group.h>
410c8acf 108
388e0319 109#include "priv.h"
78698994 110#include "protocol.h"
10f681b1 111#include "slip.h"
410c8acf 112#include "util.h"
113
114#undef sun
115
116/*----- Magic numbers -----------------------------------------------------*/
117
410c8acf 118/* --- Trace flags --- */
119
120#define T_TUNNEL 1u
121#define T_PEER 2u
122#define T_PACKET 4u
123#define T_ADMIN 8u
124#define T_CRYPTO 16u
125#define T_KEYSET 32u
126#define T_KEYEXCH 64u
127#define T_KEYMGMT 128u
37941236 128#define T_CHAL 256u
388e0319 129/* T_PRIVSEP in priv.h */
410c8acf 130
388e0319 131#define T_ALL 1023u
410c8acf 132
133/* --- Units --- */
134
135#define SEC(n) (n##u)
136#define MIN(n) (n##u * 60u)
137#define MEG(n) (n##ul * 1024ul * 1024ul)
138
139/* --- Other things --- */
140
141#define PKBUFSZ 65536
142
832a2ab6 143/*----- Cipher selections -------------------------------------------------*/
144
b5c45da1 145typedef struct algswitch {
146 const gccipher *c; /* Symmetric encryption scheme */
147 const gccipher *mgf; /* Mask-generation function */
148 const gchash *h; /* Hash function */
149 const gcmac *m; /* Message authentication code */
150 size_t hashsz; /* Hash output size */
151 size_t tagsz; /* Length to truncate MAC tags */
383a9d71 152 size_t expsz; /* Size of data to process */
b5c45da1 153 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
154} algswitch;
832a2ab6 155
799e58b9
MW
156typedef struct kdata {
157 unsigned ref; /* Reference counter */
158 struct knode *kn; /* Pointer to cache entry */
159 char *tag; /* Full tag name of the key */
160 group *g; /* The group we work in */
161 size_t indexsz; /* Size of exponent for the group */
162 mp *kpriv; /* The private key (or null) */
163 ge *kpub; /* The public key */
164 time_t t_exp; /* Expiry time of the key */
165 algswitch algs; /* Collection of algorithms */
166} kdata;
167
168typedef struct knode {
169 sym_base _b; /* Symbol table intrusion */
170 unsigned f; /* Various flags */
171#define KNF_BROKEN 1u /* Don't use this key any more */
172 struct keyhalf *kh; /* Pointer to the home keyhalf */
173 kdata *kd; /* Pointer to the key data */
174} knode;
175
b5c45da1 176#define MAXHASHSZ 64 /* Largest possible hash size */
832a2ab6 177
b5c45da1 178#define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
410c8acf 179
aeeb5611 180/*----- Data structures ---------------------------------------------------*/
410c8acf 181
182/* --- Socket addresses --- *
183 *
184 * A magic union of supported socket addresses.
185 */
186
187typedef union addr {
188 struct sockaddr sa;
189 struct sockaddr_in sin;
190} addr;
191
c8e02c8a
MW
192/* --- Mapping keyed on addresses --- */
193
194typedef struct addrmap {
195 hash_table t;
196 size_t load;
197} addrmap;
198
199typedef struct addrmap_base {
200 hash_base b;
201 addr a;
202} addrmap_base;
203
37941236 204/* --- Sequence number checking --- */
205
206typedef struct seqwin {
207 uint32 seq; /* First acceptable input sequence */
208 uint32 win; /* Window of acceptable numbers */
209} seqwin;
210
211#define SEQ_WINSZ 32 /* Bits in sequence number window */
212
410c8acf 213/* --- A symmetric keyset --- *
214 *
215 * A keyset contains a set of symmetric keys for encrypting and decrypting
216 * packets. Keysets are stored in a list, sorted in reverse order of
217 * creation, so that the most recent keyset (the one most likely to be used)
218 * is first.
219 *
220 * Each keyset has a time limit and a data limit. The keyset is destroyed
221 * when either it has existed for too long, or it has been used to encrypt
222 * too much data. New key exchanges are triggered when keys are close to
223 * expiry.
224 */
225
226typedef struct keyset {
227 struct keyset *next; /* Next active keyset in the list */
832a2ab6 228 unsigned ref; /* Reference count for keyset */
9466fafa 229 struct peer *p; /* Pointer to peer structure */
410c8acf 230 time_t t_exp; /* Expiry time for this keyset */
383a9d71 231 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
832a2ab6 232 T( unsigned seq; ) /* Sequence number for tracing */
233 unsigned f; /* Various useful flags */
e04c2d50 234 gcipher *cin, *cout; /* Keyset ciphers for encryption */
b5c45da1 235 size_t tagsz; /* Length to truncate MAC tags */
e04c2d50 236 gmac *min, *mout; /* Keyset MACs for integrity */
1484d822 237 uint32 oseq; /* Outbound sequence number */
37941236 238 seqwin iseq; /* Inbound sequence number */
410c8acf 239} keyset;
240
832a2ab6 241#define KSF_LISTEN 1u /* Don't encrypt packets yet */
242#define KSF_LINK 2u /* Key is in a linked list */
243
a50f9a0e
MW
244#define KSERR_REGEN -1 /* Regenerate keys */
245#define KSERR_NOKEYS -2 /* No keys left */
246#define KSERR_DECRYPT -3 /* Unable to decrypt message */
12a26b8b
MW
247#define KSERR_SEQ -4 /* Incorrect sequence number */
248#define KSERR_MALFORMED -5 /* Input ciphertext is broken */
a50f9a0e 249
410c8acf 250/* --- Key exchange --- *
251 *
252 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
253 * Protocol has a number of desirable features (e.g., perfect forward
254 * secrecy, and zero-knowledge authentication) which make it attractive for
255 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
256 * Clive Jones.
257 */
258
832a2ab6 259#define KX_NCHAL 16u
832a2ab6 260
261typedef struct kxchal {
262 struct keyexch *kx; /* Pointer back to key exchange */
52c03a2a 263 ge *c; /* Responder's challenge */
264 ge *r; /* My reply to the challenge */
832a2ab6 265 keyset *ks; /* Pointer to temporary keyset */
266 unsigned f; /* Various useful flags */
267 sel_timer t; /* Response timer for challenge */
b5c45da1 268 octet hc[MAXHASHSZ]; /* Hash of his challenge */
de7bd20b 269 octet ck[MAXHASHSZ]; /* His magical check value */
b5c45da1 270 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
271 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
272 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
273 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
832a2ab6 274} kxchal;
275
410c8acf 276typedef struct keyexch {
410c8acf 277 struct peer *p; /* Pointer back to the peer */
35c8b547
MW
278 kdata *kpriv; /* Private key and related info */
279 kdata *kpub; /* Peer's public key */
832a2ab6 280 keyset **ks; /* Peer's list of keysets */
410c8acf 281 unsigned f; /* Various useful flags */
832a2ab6 282 unsigned s; /* Current state in exchange */
410c8acf 283 sel_timer t; /* Timer for next exchange */
832a2ab6 284 mp *alpha; /* My temporary secret */
52c03a2a 285 ge *c; /* My challenge */
286 ge *rx; /* The expected response */
832a2ab6 287 unsigned nr; /* Number of extant responses */
410c8acf 288 time_t t_valid; /* When this exchange goes bad */
b5c45da1 289 octet hc[MAXHASHSZ]; /* Hash of my challenge */
832a2ab6 290 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 291} keyexch;
292
293#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 294#define KXF_DEAD 2u /* The key-exchanger isn't up */
295#define KXF_PUBKEY 4u /* Key exchanger has a public key */
010e6f63 296#define KXF_CORK 8u /* Don't send anything yet */
832a2ab6 297
298enum {
299 KXS_DEAD, /* Uninitialized state (magical) */
300 KXS_CHAL, /* Main answer-challenges state */
301 KXS_COMMIT, /* Committed: send switch request */
302 KXS_SWITCH /* Switched: send confirmation */
303};
410c8acf 304
305/* --- Tunnel structure --- *
306 *
307 * Used to maintain system-specific information about the tunnel interface.
308 */
309
42da2a58 310typedef struct tunnel tunnel;
311struct peer;
110d564e 312
42da2a58 313typedef struct tunnel_ops {
314 const char *name; /* Name of this tunnel driver */
388e0319
MW
315 unsigned flags; /* Various interesting flags */
316#define TUNF_PRIVOPEN 1u /* Need helper to open file */
42da2a58 317 void (*init)(void); /* Initializes the system */
eb5f3fea 318 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
72917fe7
MW
319 /* Initializes a new tunnel */
320 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
321 /* Notifies ifname change */
42da2a58 322 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
323 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
324} tunnel_ops;
b9066fbb 325
42da2a58 326#ifndef TUN_INTERNALS
327struct tunnel { const tunnel_ops *ops; };
410c8acf 328#endif
410c8acf 329
832a2ab6 330/* --- Peer statistics --- *
331 *
332 * Contains various interesting and not-so-interesting statistics about a
333 * peer. This is updated by various parts of the code. The format of the
334 * structure isn't considered private, and @p_stats@ returns a pointer to the
335 * statistics block for a given peer.
336 */
337
338typedef struct stats {
339 unsigned long sz_in, sz_out; /* Size of all data in and out */
340 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
341 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
3cdc3f3a 342 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
832a2ab6 343 unsigned long n_reject; /* Number of rejected packets */
344 unsigned long n_in, n_out; /* Number of packets in and out */
345 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
346 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
347} stats;
348
410c8acf 349/* --- Peer structure --- *
350 *
351 * The main structure which glues everything else together.
352 */
353
0ba8de86 354typedef struct peerspec {
355 char *name; /* Peer's name */
fe2a5dcf 356 char *privtag; /* Private key tag */
48b84569 357 char *tag; /* Public key tag */
0ba8de86 358 const tunnel_ops *tops; /* Tunnel operations */
359 unsigned long t_ka; /* Keep alive interval */
360 addr sa; /* Socket address to speak to */
361 size_t sasz; /* Socket address size */
8743c776 362 unsigned f; /* Flags for the peer */
6411163d
MW
363#define PSF_KXMASK 255u /* Key-exchange flags to set */
364#define PSF_MOBILE 256u /* Address may change rapidly */
0ba8de86 365} peerspec;
366
c8e02c8a
MW
367typedef struct peer_byname {
368 sym_base _b;
369 struct peer *p;
370} peer_byname;
371
372typedef struct peer_byaddr {
373 addrmap_base _b;
374 struct peer *p;
375} peer_byaddr;
376
410c8acf 377typedef struct peer {
c8e02c8a
MW
378 peer_byname *byname; /* Lookup-by-name block */
379 peer_byaddr *byaddr; /* Lookup-by-address block */
0ba8de86 380 struct ping *pings; /* Pings we're waiting for */
381 peerspec spec; /* Specifications for this peer */
42da2a58 382 tunnel *t; /* Tunnel for local packets */
64cf2223 383 char *ifname; /* Interface name for tunnel */
410c8acf 384 keyset *ks; /* List head for keysets */
410c8acf 385 buf b; /* Buffer for sending packets */
832a2ab6 386 stats st; /* Statistics */
387 keyexch kx; /* Key exchange protocol block */
0ba8de86 388 sel_timer tka; /* Timer for keepalives */
410c8acf 389} peer;
390
c8e02c8a
MW
391typedef struct peer_iter { sym_iter i; } peer_iter;
392
0ba8de86 393typedef struct ping {
394 struct ping *next, *prev; /* Links to next and previous */
395 peer *p; /* Peer so we can free it */
396 unsigned msg; /* Kind of response expected */
397 uint32 id; /* Id so we can recognize response */
398 octet magic[32]; /* Some random data */
399 sel_timer t; /* Timeout for ping */
400 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
401 void *arg; /* Argument for callback */
402} ping;
403
404enum {
405 PING_NONOTIFY = -1,
406 PING_OK = 0,
407 PING_TIMEOUT,
408 PING_PEERDIED,
409 PING_MAX
410};
411
410c8acf 412/* --- Admin structure --- */
413
fd3cf232 414#define OBUFSZ 16384u
415
416typedef struct obuf {
417 struct obuf *next; /* Next buffer in list */
418 char *p_in, *p_out; /* Pointers into the buffer */
419 char buf[OBUFSZ]; /* The actual buffer */
420} obuf;
421
de014da6 422typedef struct oqueue {
423 obuf *hd, *tl; /* Head and tail pointers */
424} oqueue;
425
426struct admin;
427
428typedef struct admin_bgop {
429 struct admin_bgop *next, *prev; /* Links to next and previous */
430 struct admin *a; /* Owner job */
431 char *tag; /* Tag string for messages */
432 void (*cancel)(struct admin_bgop *); /* Destructor function */
433} admin_bgop;
434
37941236 435typedef struct admin_resop {
de014da6 436 admin_bgop bg; /* Background operation header */
37941236 437 char *addr; /* Hostname to be resolved */
de014da6 438 bres_client r; /* Background resolver task */
439 sel_timer t; /* Timer for resolver */
37941236 440 addr sa; /* Socket address */
441 size_t sasz; /* Socket address size */
442 void (*func)(struct admin_resop *, int); /* Handler */
443} admin_resop;
444
445enum { ARES_OK, ARES_FAIL };
446
447typedef struct admin_addop {
448 admin_resop r; /* Name resolution header */
449 peerspec peer; /* Peer pending creation */
de014da6 450} admin_addop;
451
452typedef struct admin_pingop {
453 admin_bgop bg; /* Background operation header */
454 ping ping; /* Ping pending response */
455 struct timeval pingtime; /* Time last ping was sent */
be6a1b7a
MW
456} admin_pingop;
457
458typedef struct admin_service {
459 sym_base _b; /* Hash table base structure */
460 char *version; /* The provided version */
461 struct admin *prov; /* Which client provides me */
462 struct admin_service *next, *prev; /* Client's list of services */
463} admin_service;
de014da6 464
5d46c0f8
MW
465typedef struct admin_svcop {
466 admin_bgop bg; /* Background operation header */
467 struct admin *prov; /* Client servicing this job */
cc921fba 468 unsigned index; /* This job's index */
5d46c0f8
MW
469 struct admin_svcop *next, *prev; /* Links for provider's jobs */
470} admin_svcop;
471
472typedef struct admin_jobentry {
473 unsigned short seq; /* Zero if unused */
474 union {
475 admin_svcop *op; /* Operation, if slot in use, ... */
476 uint32 next; /* ... or index of next free slot */
477 } u;
478} admin_jobentry;
479
480typedef struct admin_jobtable {
481 uint32 n, sz; /* Used slots and table size */
482 admin_svcop *active; /* List of active jobs */
483 uint32 free; /* Index of first free slot */
484 admin_jobentry *v; /* And the big array of entries */
485} admin_jobtable;
486
410c8acf 487typedef struct admin {
488 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 489 unsigned f; /* Various useful flags */
060ca767 490 unsigned ref; /* Reference counter */
410c8acf 491#ifndef NTRACE
492 unsigned seq; /* Sequence number for tracing */
493#endif
de014da6 494 oqueue out; /* Output buffer list */
495 oqueue delay; /* Delayed output buffer list */
496 admin_bgop *bg; /* Backgrounded operations */
be6a1b7a 497 admin_service *svcs; /* Which services I provide */
5d46c0f8 498 admin_jobtable j; /* Table of outstanding jobs */
fd3cf232 499 selbuf b; /* Line buffer for commands */
500 sel_file w; /* Selector for write buffering */
410c8acf 501} admin;
502
fd3cf232 503#define AF_DEAD 1u /* Destroy this admin block */
060ca767 504#define AF_CLOSE 2u /* Client closed connection */
3cdc3f3a 505#define AF_NOTE 4u /* Catch notifications */
de014da6 506#define AF_WARN 8u /* Catch warning messages */
3cdc3f3a 507#ifndef NTRACE
de014da6 508 #define AF_TRACE 16u /* Catch tracing */
3cdc3f3a 509#endif
46dde080 510#define AF_FOREGROUND 32u /* Quit server when client closes */
3cdc3f3a 511
512#ifndef NTRACE
513# define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
514#else
515# define AF_ALLMSGS (AF_NOTE | AF_WARN)
516#endif
fd3cf232 517
410c8acf 518/*----- Global variables --------------------------------------------------*/
519
520extern sel_state sel; /* Global I/O event state */
a4b808b0 521extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
42da2a58 522extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
523extern const tunnel_ops *tun_default; /* Default tunnel to use */
799e58b9
MW
524extern kdata *master; /* Default private key */
525extern const char *tag_priv; /* Default private key tag */
410c8acf 526
527#ifndef NTRACE
528extern const trace_opt tr_opts[]; /* Trace options array */
529extern unsigned tr_flags; /* Trace options flags */
530#endif
531
8d0c7a83 532/*----- Other macros ------------------------------------------------------*/
533
534#define TIMER noise_timer(RAND_GLOBAL)
535
410c8acf 536/*----- Key management ----------------------------------------------------*/
537
799e58b9
MW
538/* --- @km_init@ --- *
539 *
540 * Arguments: @const char *privkr@ = private keyring file
541 * @const char *pubkr@ = public keyring file
542 * @const char *ptag@ = default private-key tag
543 *
544 * Returns: ---
545 *
546 * Use: Initializes the key-management machinery, loading the
547 * keyrings and so on.
548 */
549
550extern void km_init(const char */*privkr*/, const char */*pubkr*/,
551 const char */*ptag*/);
552
de014da6 553/* --- @km_reload@ --- *
410c8acf 554 *
555 * Arguments: ---
556 *
557 * Returns: Zero if OK, nonzero to force reloading of keys.
558 *
de014da6 559 * Use: Checks the keyrings to see if they need reloading.
410c8acf 560 */
561
de014da6 562extern int km_reload(void);
410c8acf 563
799e58b9
MW
564/* --- @km_findpub@, @km_findpriv@ --- *
565 *
566 * Arguments: @const char *tag@ = key tag to load
567 *
568 * Returns: Pointer to the kdata object if successful, or null on error.
569 *
570 * Use: Fetches a public or private key from the keyring.
571 */
572
573extern kdata *km_findpub(const char */*tag*/);
574extern kdata *km_findpriv(const char */*tag*/);
575
576/* --- @km_samealgsp@ --- *
577 *
578 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
410c8acf 579 *
799e58b9
MW
580 * Returns: Nonzero if their two algorithm selections are the same.
581 *
582 * Use: Checks sameness of algorithm selections: used to ensure that
583 * peers are using sensible algorithms.
584 */
585
586extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
587
588/* --- @km_ref@ --- *
589 *
590 * Arguments: @kdata *kd@ = pointer to the kdata object
410c8acf 591 *
592 * Returns: ---
593 *
799e58b9
MW
594 * Use: Claim a new reference to a kdata object.
595 */
596
597extern void km_ref(kdata */*kd*/);
598
599/* --- @km_unref@ --- *
600 *
601 * Arguments: @kdata *kd@ = pointer to the kdata object
602 *
603 * Returns: ---
604 *
605 * Use: Releases a reference to a kdata object.
606 */
607
608extern void km_unref(kdata */*kd*/);
609
610/* --- @km_tag@ --- *
611 *
612 * Arguments: @kdata *kd@ - pointer to the kdata object
613 *
614 * Returns: A pointer to the short tag by which the kdata was loaded.
410c8acf 615 */
616
799e58b9 617extern const char *km_tag(kdata */*kd*/);
410c8acf 618
410c8acf 619/*----- Key exchange ------------------------------------------------------*/
620
621/* --- @kx_start@ --- *
622 *
623 * Arguments: @keyexch *kx@ = pointer to key exchange context
de014da6 624 * @int forcep@ = nonzero to ignore the quiet timer
410c8acf 625 *
626 * Returns: ---
627 *
628 * Use: Stimulates a key exchange. If a key exchage is in progress,
629 * a new challenge is sent (unless the quiet timer forbids
630 * this); if no exchange is in progress, one is commenced.
631 */
632
de014da6 633extern void kx_start(keyexch */*kx*/, int /*forcep*/);
410c8acf 634
832a2ab6 635/* --- @kx_message@ --- *
410c8acf 636 *
637 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 638 * @unsigned msg@ = the message code
639 * @buf *b@ = pointer to buffer containing the packet
410c8acf 640 *
641 * Returns: ---
642 *
832a2ab6 643 * Use: Reads a packet containing key exchange messages and handles
644 * it.
410c8acf 645 */
646
832a2ab6 647extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 648
649/* --- @kx_free@ --- *
650 *
651 * Arguments: @keyexch *kx@ = pointer to key exchange context
652 *
653 * Returns: ---
654 *
655 * Use: Frees everything in a key exchange context.
656 */
657
658extern void kx_free(keyexch */*kx*/);
659
660/* --- @kx_newkeys@ --- *
661 *
662 * Arguments: @keyexch *kx@ = pointer to key exchange context
663 *
664 * Returns: ---
665 *
666 * Use: Informs the key exchange module that its keys may have
667 * changed. If fetching the new keys fails, the peer will be
668 * destroyed, we log messages and struggle along with the old
669 * keys.
670 */
671
672extern void kx_newkeys(keyexch */*kx*/);
673
674/* --- @kx_init@ --- *
675 *
676 * Arguments: @keyexch *kx@ = pointer to key exchange context
677 * @peer *p@ = pointer to peer context
678 * @keyset **ks@ = pointer to keyset list
010e6f63 679 * @unsigned f@ = various useful flags
410c8acf 680 *
681 * Returns: Zero if OK, nonzero if it failed.
682 *
683 * Use: Initializes a key exchange module. The module currently
684 * contains no keys, and will attempt to initiate a key
685 * exchange.
686 */
687
010e6f63
MW
688extern int kx_init(keyexch */*kx*/, peer */*p*/,
689 keyset **/*ks*/, unsigned /*f*/);
410c8acf 690
691/*----- Keysets and symmetric cryptography --------------------------------*/
692
832a2ab6 693/* --- @ks_drop@ --- *
694 *
695 * Arguments: @keyset *ks@ = pointer to a keyset
696 *
697 * Returns: ---
698 *
699 * Use: Decrements a keyset's reference counter. If the counter hits
700 * zero, the keyset is freed.
701 */
702
703extern void ks_drop(keyset */*ks*/);
704
705/* --- @ks_gen@ --- *
706 *
707 * Arguments: @const void *k@ = pointer to key material
708 * @size_t x, y, z@ = offsets into key material (see below)
9466fafa 709 * @peer *p@ = pointer to peer information
832a2ab6 710 *
711 * Returns: A pointer to the new keyset.
712 *
713 * Use: Derives a new keyset from the given key material. The
714 * offsets @x@, @y@ and @z@ separate the key material into three
715 * parts. Between the @k@ and @k + x@ is `my' contribution to
716 * the key material; between @k + x@ and @k + y@ is `your'
717 * contribution; and between @k + y@ and @k + z@ is a shared
718 * value we made together. These are used to construct two
719 * pairs of symmetric keys. Each pair consists of an encryption
720 * key and a message authentication key. One pair is used for
721 * outgoing messages, the other for incoming messages.
722 *
723 * The new key is marked so that it won't be selected for output
724 * by @ksl_encrypt@. You can still encrypt data with it by
725 * calling @ks_encrypt@ directly.
726 */
727
728extern keyset *ks_gen(const void */*k*/,
9466fafa 729 size_t /*x*/, size_t /*y*/, size_t /*z*/,
730 peer */*p*/);
832a2ab6 731
732/* --- @ks_tregen@ --- *
733 *
734 * Arguments: @keyset *ks@ = pointer to a keyset
735 *
736 * Returns: The time at which moves ought to be made to replace this key.
737 */
738
739extern time_t ks_tregen(keyset */*ks*/);
740
741/* --- @ks_activate@ --- *
742 *
743 * Arguments: @keyset *ks@ = pointer to a keyset
744 *
745 * Returns: ---
746 *
747 * Use: Activates a keyset, so that it can be used for encrypting
748 * outgoing messages.
749 */
750
751extern void ks_activate(keyset */*ks*/);
752
753/* --- @ks_encrypt@ --- *
754 *
755 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 756 * @unsigned ty@ = message type
832a2ab6 757 * @buf *b@ = pointer to input buffer
758 * @buf *bb@ = pointer to output buffer
759 *
a50f9a0e
MW
760 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
761 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
762 * returns zero if there was insufficient buffer (but the output
763 * buffer is broken in this case).
832a2ab6 764 *
765 * Use: Encrypts a block of data using the key. Note that the `key
766 * ought to be replaced' notification is only ever given once
767 * for each key. Also note that this call forces a keyset to be
768 * used even if it's marked as not for data output.
769 */
770
7ed14135 771extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
772 buf */*b*/, buf */*bb*/);
832a2ab6 773
774/* --- @ks_decrypt@ --- *
775 *
776 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 777 * @unsigned ty@ = expected type code
832a2ab6 778 * @buf *b@ = pointer to an input buffer
779 * @buf *bb@ = pointer to an output buffer
780 *
a50f9a0e
MW
781 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
782 * zero if there was insufficient buffer (but the output buffer
783 * is broken in this case).
832a2ab6 784 *
785 * Use: Attempts to decrypt a message using a given key. Note that
786 * requesting decryption with a key directly won't clear a
787 * marking that it's not for encryption.
788 */
789
7ed14135 790extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
791 buf */*b*/, buf */*bb*/);
832a2ab6 792
793/* --- @ksl_free@ --- *
410c8acf 794 *
795 * Arguments: @keyset **ksroot@ = pointer to keyset list head
796 *
797 * Returns: ---
798 *
832a2ab6 799 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 800 */
801
832a2ab6 802extern void ksl_free(keyset **/*ksroot*/);
410c8acf 803
832a2ab6 804/* --- @ksl_link@ --- *
410c8acf 805 *
806 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 807 * @keyset *ks@ = pointer to a keyset
410c8acf 808 *
809 * Returns: ---
810 *
832a2ab6 811 * Use: Links a keyset into a list. A keyset can only be on one list
812 * at a time. Bad things happen otherwise.
410c8acf 813 */
814
832a2ab6 815extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 816
832a2ab6 817/* --- @ksl_prune@ --- *
410c8acf 818 *
819 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 820 *
832a2ab6 821 * Returns: ---
410c8acf 822 *
832a2ab6 823 * Use: Prunes the keyset list by removing keys which mustn't be used
824 * any more.
410c8acf 825 */
826
832a2ab6 827extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 828
832a2ab6 829/* --- @ksl_encrypt@ --- *
410c8acf 830 *
831 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 832 * @unsigned ty@ = message type
410c8acf 833 * @buf *b@ = pointer to input buffer
834 * @buf *bb@ = pointer to output buffer
835 *
a50f9a0e
MW
836 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
837 * new key; @KSERR_NOKEYS@ if there are no suitable keys
838 * available. Also returns zero if there was insufficient
839 * buffer space (but the output buffer is broken in this case).
410c8acf 840 *
841 * Use: Encrypts a packet.
842 */
843
7ed14135 844extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
845 buf */*b*/, buf */*bb*/);
410c8acf 846
832a2ab6 847/* --- @ksl_decrypt@ --- *
410c8acf 848 *
849 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 850 * @unsigned ty@ = expected type code
410c8acf 851 * @buf *b@ = pointer to input buffer
852 * @buf *bb@ = pointer to output buffer
853 *
a50f9a0e
MW
854 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
855 * zero if there was insufficient buffer (but the output buffer
856 * is broken in this case).
410c8acf 857 *
858 * Use: Decrypts a packet.
859 */
860
7ed14135 861extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
862 buf */*b*/, buf */*bb*/);
410c8acf 863
37941236 864/*----- Challenges --------------------------------------------------------*/
865
866/* --- @c_new@ --- *
867 *
868 * Arguments: @buf *b@ = where to put the challenge
869 *
870 * Returns: Zero if OK, nonzero on error.
871 *
872 * Use: Issues a new challenge.
873 */
874
875extern int c_new(buf */*b*/);
876
877/* --- @c_check@ --- *
878 *
879 * Arguments: @buf *b@ = where to find the challenge
880 *
881 * Returns: Zero if OK, nonzero if it didn't work.
882 *
883 * Use: Checks a challenge. On failure, the buffer is broken.
884 */
885
886extern int c_check(buf */*b*/);
887
410c8acf 888/*----- Administration interface ------------------------------------------*/
889
f43df819
MW
890#define A_END ((char *)0)
891
7f73baaf
MW
892/* --- @a_vformat@ --- *
893 *
894 * Arguments: @dstr *d@ = where to leave the formatted message
895 * @const char *fmt@ = pointer to format string
896 * @va_list ap@ = arguments in list
897 *
898 * Returns: ---
899 *
900 * Use: Main message token formatting driver. The arguments are
901 * interleaved formatting tokens and their parameters, finally
902 * terminated by an entry @A_END@.
903 *
904 * Tokens recognized:
905 *
906 * * "*..." ... -- pretokenized @dstr_putf@-like string
907 *
908 * * "?ADDR" SOCKADDR -- a socket address, to be converted
909 *
910 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
911 *
912 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
913 *
914 * * "?PEER" PEER -- peer's name
915 *
916 * * "?ERRNO" ERRNO -- system error code
917 *
918 * * "[!]..." ... -- @dstr_putf@-like string as single token
919 */
920
921extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list /*ap*/);
922
f241e36c
MW
923/* --- @a_format@ --- *
924 *
925 * Arguments: @dstr *d@ = where to leave the formatted message
926 * @const char *fmt@ = pointer to format string
927 *
928 * Returns: ---
929 *
930 * Use: Writes a tokenized message into a string, for later
931 * presentation.
932 */
933
934extern void a_format(dstr */*d*/, const char */*fmt*/, ...);
935
410c8acf 936/* --- @a_warn@ --- *
937 *
938 * Arguments: @const char *fmt@ = pointer to format string
939 * @...@ = other arguments
940 *
941 * Returns: ---
942 *
943 * Use: Informs all admin connections of a warning.
944 */
945
946extern void a_warn(const char */*fmt*/, ...);
947
3cdc3f3a 948/* --- @a_notify@ --- *
949 *
950 * Arguments: @const char *fmt@ = pointer to format string
951 * @...@ = other arguments
952 *
953 * Returns: ---
954 *
955 * Use: Sends a notification to interested admin connections.
956 */
957
958extern void a_notify(const char */*fmt*/, ...);
959
410c8acf 960/* --- @a_create@ --- *
961 *
962 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 963 * @unsigned f@ = initial flags to set
410c8acf 964 *
965 * Returns: ---
966 *
967 * Use: Creates a new admin connection.
968 */
969
3cdc3f3a 970extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
410c8acf 971
972/* --- @a_quit@ --- *
973 *
974 * Arguments: ---
975 *
976 * Returns: ---
977 *
978 * Use: Shuts things down nicely.
979 */
980
981extern void a_quit(void);
982
c511e1f9
MW
983/* --- @a_preselect@ --- *
984 *
985 * Arguments: ---
986 *
987 * Returns: ---
988 *
989 * Use: Informs the admin module that we're about to select again,
990 * and that it should do cleanup things it has delayed until a
991 * `safe' time.
992 */
993
994extern void a_preselect(void);
995
410c8acf 996/* --- @a_daemon@ --- *
997 *
998 * Arguments: ---
999 *
1000 * Returns: ---
1001 *
1002 * Use: Informs the admin module that it's a daemon.
1003 */
1004
1005extern void a_daemon(void);
1006
1007/* --- @a_init@ --- *
1008 *
1009 * Arguments: @const char *sock@ = socket name to create
aa2405e8
MW
1010 * @uid_t u@ = user to own the socket
1011 * @gid_t g@ = group to own the socket
a9279e37 1012 * @mode_t m@ = permissions to set on the socket
410c8acf 1013 *
1014 * Returns: ---
1015 *
1016 * Use: Creates the admin listening socket.
1017 */
1018
a9279e37
MW
1019extern void a_init(const char */*sock*/,
1020 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
410c8acf 1021
c8e02c8a
MW
1022/*----- Mapping with addresses as keys ------------------------------------*/
1023
1024/* --- @am_create@ --- *
1025 *
1026 * Arguments: @addrmap *m@ = pointer to map
1027 *
1028 * Returns: ---
1029 *
1030 * Use: Create an address map, properly set up.
1031 */
1032
1033extern void am_create(addrmap */*m*/);
1034
1035/* --- @am_destroy@ --- *
1036 *
1037 * Arguments: @addrmap *m@ = pointer to map
1038 *
1039 * Returns: ---
1040 *
1041 * Use: Destroy an address map, throwing away all the entries.
1042 */
1043
1044extern void am_destroy(addrmap */*m*/);
1045
1046/* --- @am_find@ --- *
1047 *
1048 * Arguments: @addrmap *m@ = pointer to map
1049 * @const addr *a@ = address to look up
1050 * @size_t sz@ = size of block to allocate
1051 * @unsigned *f@ = where to store flags
1052 *
1053 * Returns: Pointer to found item, or null.
1054 *
1055 * Use: Finds a record with the given IP address, set @*f@ nonzero
1056 * and returns it. If @sz@ is zero, and no match was found,
1057 * return null; otherwise allocate a new block of @sz@ bytes,
1058 * clear @*f@ to zero and return the block pointer.
1059 */
1060
1061extern void *am_find(addrmap */*m*/, const addr */*a*/,
1062 size_t /*sz*/, unsigned */*f*/);
1063
1064/* --- @am_remove@ --- *
1065 *
1066 * Arguments: @addrmap *m@ = pointer to map
1067 * @void *i@ = pointer to the item
1068 *
1069 * Returns: ---
1070 *
1071 * Use: Removes an item from the map.
1072 */
1073
1074extern void am_remove(addrmap */*m*/, void */*i*/);
1075
388e0319
MW
1076/*----- Privilege separation ----------------------------------------------*/
1077
1078/* --- @ps_trace@ --- *
1079 *
1080 * Arguments: @unsigned mask@ = trace mask to check
1081 * @const char *fmt@ = message format
1082 * @...@ = values for placeholders
1083 *
1084 * Returns: ---
1085 *
1086 * Use: Writes a trace message.
1087 */
1088
1089T( extern void ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1090
1091/* --- @ps_warn@ --- *
1092 *
1093 * Arguments: @const char *fmt@ = message format
1094 * @...@ = values for placeholders
1095 *
1096 * Returns: ---
1097 *
1098 * Use: Writes a warning message.
1099 */
1100
1101extern void ps_warn(const char */*fmt*/, ...);
1102
1103/* --- @ps_tunfd@ --- *
1104 *
1105 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1106 * @char **ifn@ = where to put the interface name
1107 *
1108 * Returns: The file descriptor, or @-1@ on error.
1109 *
1110 * Use: Fetches a file descriptor for a tunnel driver.
1111 */
1112
1113extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1114
1115/* --- @ps_split@ --- *
1116 *
1117 * Arguments: @int detachp@ = whether to detach the child from its terminal
1118 *
1119 * Returns: ---
1120 *
1121 * Use: Separates off the privileged tunnel-opening service from the
1122 * rest of the server.
1123 */
1124
1125extern void ps_split(int /*detachp*/);
1126
1127/* --- @ps_quit@ --- *
1128 *
1129 * Arguments: ---
1130 *
1131 * Returns: ---
1132 *
1133 * Use: Detaches from the helper process.
1134 */
1135
1136extern void ps_quit(void);
1137
410c8acf 1138/*----- Peer management ---------------------------------------------------*/
1139
1140/* --- @p_txstart@ --- *
1141 *
1142 * Arguments: @peer *p@ = pointer to peer block
1143 * @unsigned msg@ = message type code
1144 *
1145 * Returns: A pointer to a buffer to write to.
1146 *
1147 * Use: Starts sending to a peer. Only one send can happen at a
1148 * time.
1149 */
1150
1151extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1152
1153/* --- @p_txend@ --- *
1154 *
1155 * Arguments: @peer *p@ = pointer to peer block
1156 *
1157 * Returns: ---
1158 *
1159 * Use: Sends a packet to the peer.
1160 */
1161
1162extern void p_txend(peer */*p*/);
1163
0ba8de86 1164/* --- @p_pingsend@ --- *
1165 *
1166 * Arguments: @peer *p@ = destination peer
1167 * @ping *pg@ = structure to fill in
1168 * @unsigned type@ = message type
1169 * @unsigned long timeout@ = how long to wait before giving up
1170 * @void (*func)(int, void *)@ = callback function
1171 * @void *arg@ = argument for callback
1172 *
1173 * Returns: Zero if successful, nonzero if it failed.
1174 *
1175 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1176 * if we get an answer within the timeout, or zero if no answer.
1177 */
1178
1179extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1180 unsigned long /*timeout*/,
1181 void (*/*func*/)(int, void *), void */*arg*/);
1182
1183/* --- @p_pingdone@ --- *
1184 *
1185 * Arguments: @ping *p@ = ping structure
1186 * @int rc@ = return code to pass on
1187 *
1188 * Returns: ---
1189 *
1190 * Use: Disposes of a ping structure, maybe sending a notification.
1191 */
1192
1193extern void p_pingdone(ping */*p*/, int /*rc*/);
1194
37941236 1195/* --- @p_greet@ --- *
1196 *
1197 * Arguments: @peer *p@ = peer to send to
1198 * @const void *c@ = pointer to challenge
1199 * @size_t sz@ = size of challenge
1200 *
1201 * Returns: ---
1202 *
1203 * Use: Sends a greeting packet.
1204 */
1205
1206extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1207
410c8acf 1208/* --- @p_tun@ --- *
1209 *
1210 * Arguments: @peer *p@ = pointer to peer block
1211 * @buf *b@ = buffer containing incoming packet
1212 *
1213 * Returns: ---
1214 *
1215 * Use: Handles a packet which needs to be sent to a peer.
1216 */
1217
1218extern void p_tun(peer */*p*/, buf */*b*/);
1219
de014da6 1220/* --- @p_keyreload@ --- *
1221 *
1222 * Arguments: ---
1223 *
1224 * Returns: ---
1225 *
1226 * Use: Forces a check of the daemon's keyring files.
1227 */
1228
1229extern void p_keyreload(void);
1230
410c8acf 1231/* --- @p_interval@ --- *
1232 *
1233 * Arguments: ---
1234 *
1235 * Returns: ---
1236 *
1237 * Use: Called periodically to do tidying.
1238 */
1239
1240extern void p_interval(void);
1241
832a2ab6 1242/* --- @p_stats@ --- *
1243 *
1244 * Arguments: @peer *p@ = pointer to a peer block
1245 *
1246 * Returns: A pointer to the peer's statistics.
1247 */
1248
1249extern stats *p_stats(peer */*p*/);
1250
410c8acf 1251/* --- @p_ifname@ --- *
1252 *
1253 * Arguments: @peer *p@ = pointer to a peer block
1254 *
1255 * Returns: A pointer to the peer's interface name.
1256 */
1257
1258extern const char *p_ifname(peer */*p*/);
1259
64cf2223
MW
1260/* --- @p_setifname@ --- *
1261 *
1262 * Arguments: @peer *p@ = pointer to a peer block
1263 * @const char *name@ = pointer to the new name
1264 *
1265 * Returns: ---
1266 *
1267 * Use: Changes the name held for a peer's interface.
1268 */
1269
1270extern void p_setifname(peer */*p*/, const char */*name*/);
1271
410c8acf 1272/* --- @p_addr@ --- *
1273 *
1274 * Arguments: @peer *p@ = pointer to a peer block
1275 *
1276 * Returns: A pointer to the peer's address.
1277 */
1278
1279extern const addr *p_addr(peer */*p*/);
1280
1281/* --- @p_init@ --- *
1282 *
767b36e2 1283 * Arguments: @struct in_addr addr@ = address to bind to
1284 * @unsigned port@ = port number to listen to
410c8acf 1285 *
1286 * Returns: ---
1287 *
1288 * Use: Initializes the peer system; creates the socket.
1289 */
1290
767b36e2 1291extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1292
1293/* --- @p_port@ --- *
1294 *
1295 * Arguments: ---
1296 *
1297 * Returns: Port number used for socket.
1298 */
1299
1300unsigned p_port(void);
1301
1302/* --- @p_create@ --- *
1303 *
0ba8de86 1304 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1305 *
1306 * Returns: Pointer to the peer block, or null if it failed.
1307 *
1308 * Use: Creates a new named peer block. No peer is actually attached
1309 * by this point.
1310 */
1311
0ba8de86 1312extern peer *p_create(peerspec */*spec*/);
410c8acf 1313
1314/* --- @p_name@ --- *
1315 *
1316 * Arguments: @peer *p@ = pointer to a peer block
1317 *
1318 * Returns: A pointer to the peer's name.
060ca767 1319 *
1320 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1321 */
1322
1323extern const char *p_name(peer */*p*/);
1324
48b84569
MW
1325/* --- @p_tag@ --- *
1326 *
1327 * Arguments: @peer *p@ = pointer to a peer block
1328 *
1329 * Returns: A pointer to the peer's public key tag.
1330 */
1331
1332extern const char *p_tag(peer */*p*/);
1333
fe2a5dcf
MW
1334/* --- @p_privtag@ --- *
1335 *
1336 * Arguments: @peer *p@ = pointer to a peer block
1337 *
1338 * Returns: A pointer to the peer's private key tag.
1339 */
1340
1341extern const char *p_privtag(peer */*p*/);
1342
060ca767 1343/* --- @p_spec@ --- *
1344 *
1345 * Arguments: @peer *p@ = pointer to a peer block
1346 *
1347 * Returns: Pointer to the peer's specification
1348 */
1349
1350extern const peerspec *p_spec(peer */*p*/);
1351
c8e02c8a
MW
1352/* --- @p_findbyaddr@ --- *
1353 *
1354 * Arguments: @const addr *a@ = address to look up
1355 *
1356 * Returns: Pointer to the peer block, or null if not found.
1357 *
1358 * Use: Finds a peer by address.
1359 */
1360
1361extern peer *p_findbyaddr(const addr */*a*/);
1362
410c8acf 1363/* --- @p_find@ --- *
1364 *
1365 * Arguments: @const char *name@ = name to look up
1366 *
1367 * Returns: Pointer to the peer block, or null if not found.
1368 *
1369 * Use: Finds a peer by name.
1370 */
1371
1372extern peer *p_find(const char */*name*/);
1373
1374/* --- @p_destroy@ --- *
1375 *
1376 * Arguments: @peer *p@ = pointer to a peer
1377 *
1378 * Returns: ---
1379 *
1380 * Use: Destroys a peer.
1381 */
1382
1383extern void p_destroy(peer */*p*/);
1384
c8e02c8a
MW
1385/* --- @FOREACH_PEER@ --- *
1386 *
1387 * Arguments: @p@ = name to bind to each peer
1388 * @stuff@ = thing to do for each item
1389 *
1390 * Use: Does something for each current peer.
1391 */
1392
1393#define FOREACH_PEER(p, stuff) do { \
1394 peer_iter i_; \
1395 peer *p; \
46401b81 1396 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
c8e02c8a
MW
1397} while (0)
1398
1399/* --- @p_mkiter@ --- *
1400 *
1401 * Arguments: @peer_iter *i@ = pointer to an iterator
1402 *
1403 * Returns: ---
1404 *
1405 * Use: Initializes the iterator.
1406 */
1407
1408extern void p_mkiter(peer_iter */*i*/);
1409
1410/* --- @p_next@ --- *
1411 *
1412 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1413 *
c8e02c8a 1414 * Returns: Next peer, or null if at the end.
410c8acf 1415 *
c8e02c8a 1416 * Use: Returns the next peer.
410c8acf 1417 */
1418
c8e02c8a 1419extern peer *p_next(peer_iter */*i*/);
410c8acf 1420
42da2a58 1421/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1422
42da2a58 1423#ifdef TUN_LINUX
1424 extern const tunnel_ops tun_linux;
1425#endif
410c8acf 1426
42da2a58 1427#ifdef TUN_UNET
1428 extern const tunnel_ops tun_unet;
1429#endif
410c8acf 1430
42da2a58 1431#ifdef TUN_BSD
1432 extern const tunnel_ops tun_bsd;
1433#endif
410c8acf 1434
42da2a58 1435extern const tunnel_ops tun_slip;
410c8acf 1436
410c8acf 1437/*----- Other handy utilities ---------------------------------------------*/
1438
1439/* --- @mpstr@ --- *
1440 *
1441 * Arguments: @mp *m@ = a multiprecision integer
1442 *
1443 * Returns: A pointer to the integer's textual representation.
1444 *
1445 * Use: Converts a multiprecision integer to a string. Corrupts
a4b808b0 1446 * @buf_u@.
410c8acf 1447 */
1448
1449extern const char *mpstr(mp */*m*/);
1450
52c03a2a 1451/* --- @gestr@ --- *
1452 *
1453 * Arguments: @group *g@ = a group
1454 * @ge *x@ = a group element
1455 *
1456 * Returns: A pointer to the element's textual representation.
1457 *
1458 * Use: Converts a group element to a string. Corrupts
a4b808b0 1459 * @buf_u@.
52c03a2a 1460 */
1461
1462extern const char *gestr(group */*g*/, ge */*x*/);
1463
832a2ab6 1464/* --- @timestr@ --- *
1465 *
1466 * Arguments: @time_t t@ = a time to convert
1467 *
1468 * Returns: A pointer to a textual representation of the time.
1469 *
1470 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1471 * @buf_u@.
832a2ab6 1472 */
1473
1474extern const char *timestr(time_t /*t*/);
1475
42da2a58 1476/* --- @mystrieq@ --- *
1477 *
1478 * Arguments: @const char *x, *y@ = two strings
1479 *
1480 * Returns: True if @x@ and @y are equal, up to case.
1481 */
1482
1483extern int mystrieq(const char */*x*/, const char */*y*/);
1484
37941236 1485/* --- @seq_reset@ --- *
1486 *
1487 * Arguments: @seqwin *s@ = sequence-checking window
1488 *
1489 * Returns: ---
1490 *
1491 * Use: Resets a sequence number window.
1492 */
1493
1494extern void seq_reset(seqwin */*s*/);
1495
1496/* --- @seq_check@ --- *
1497 *
1498 * Arguments: @seqwin *s@ = sequence-checking window
1499 * @uint32 q@ = sequence number to check
f43df819 1500 * @const char *service@ = service to report message from
37941236 1501 *
1502 * Returns: A @SEQ_@ code.
1503 *
1504 * Use: Checks a sequence number against the window, updating things
1505 * as necessary.
1506 */
1507
f43df819 1508extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1509
410c8acf 1510/*----- That's all, folks -------------------------------------------------*/
1511
1512#ifdef __cplusplus
1513 }
1514#endif
1515
1516#endif