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