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