cda7f1d786e114e53d48de00c794fa7e2e918951
[secnet] / secnet.h
1 /* Core interface of secnet, to be used by all modules */
2 /*
3 * This file is part of secnet.
4 * See README for full list of copyright holders.
5 *
6 * secnet is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * secnet is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 3 along with secnet; if not, see
18 * https://www.gnu.org/licenses/gpl.html.
19 */
20
21 #ifndef secnet_h
22 #define secnet_h
23
24 #define ADNS_FEATURE_MANYAF
25
26 #include "config.h"
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <fnmatch.h>
37 #include <sys/poll.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <sys/time.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43
44 #include <bsd/sys/queue.h>
45
46 #define MAX_PEER_ADDRS 5
47 /* send at most this many copies; honour at most that many addresses */
48
49 struct comm_if;
50 struct comm_addr;
51
52 typedef char *string_t;
53 typedef const char *cstring_t;
54
55 #define False (_Bool)0
56 #define True (_Bool)1
57 typedef _Bool bool_t;
58
59 union iaddr {
60 struct sockaddr sa;
61 struct sockaddr_in sin;
62 #ifdef CONFIG_IPV6
63 struct sockaddr_in6 sin6;
64 #endif
65 };
66
67 #define ASSERT(x) do { if (!(x)) { fatal("assertion failed line %d file " \
68 __FILE__,__LINE__); } } while(0)
69
70 /* from version.c */
71
72 extern char version[];
73
74 /* from logmsg.c */
75 extern uint32_t message_level;
76 extern bool_t secnet_is_daemon;
77 extern struct log_if *system_log;
78
79 /* from process.c */
80 extern void start_signal_handling(void);
81
82 void afterfork(void);
83 /* Must be called before exec in every child made after
84 start_signal_handling. Safe to call in earlier children too. */
85
86 void childpersist_closefd_hook(void *fd_p, uint32_t newphase);
87 /* Convenience hook function for use with add_hook PHASE_CHILDPERSIST.
88 With `int fd' in your state struct, pass fd_p=&fd. The hook checks
89 whether fd>=0, so you can use it for an fd which is only sometimes
90 open. This function will set fd to -1, so it is idempotent. */
91
92 /***** CONFIGURATION support *****/
93
94 extern bool_t just_check_config; /* If True then we're going to exit after
95 reading the configuration file */
96 extern bool_t background; /* If True then we'll eventually run as a daemon */
97
98 typedef struct dict dict_t; /* Configuration dictionary */
99 typedef struct closure closure_t;
100 typedef struct item item_t;
101 typedef struct list list_t; /* A list of items */
102
103 /* Configuration file location, for error-reporting */
104 struct cloc {
105 cstring_t file;
106 int line;
107 };
108
109 /* Modules export closures, which can be invoked from the configuration file.
110 "Invoking" a closure usually returns another closure (of a different
111 type), but can actually return any configuration object. */
112 typedef list_t *(apply_fn)(closure_t *self, struct cloc loc,
113 dict_t *context, list_t *data);
114 struct closure {
115 cstring_t description; /* For debugging */
116 uint32_t type; /* Central registry... */
117 apply_fn *apply;
118 void *interface; /* Interface for use inside secnet; depends on type */
119 };
120
121 enum types { t_null, t_bool, t_string, t_number, t_dict, t_closure };
122 struct item {
123 enum types type;
124 union {
125 bool_t bool;
126 string_t string;
127 uint32_t number;
128 dict_t *dict;
129 closure_t *closure;
130 } data;
131 struct cloc loc;
132 };
133
134 /* Note that it is unwise to use this structure directly; use the list
135 manipulation functions instead. */
136 struct list {
137 item_t *item;
138 struct list *next;
139 };
140
141 /* In the following two lookup functions, NULL means 'not found' */
142 /* Lookup a value in the specified dictionary, or its parents */
143 extern list_t *dict_lookup(dict_t *dict, cstring_t key);
144 /* Lookup a value in just the specified dictionary */
145 extern list_t *dict_lookup_primitive(dict_t *dict, cstring_t key);
146 /* Add a value to the specified dictionary */
147 extern void dict_add(dict_t *dict, cstring_t key, list_t *val);
148 /* Obtain an array of keys in the dictionary. malloced; caller frees */
149 extern cstring_t *dict_keys(dict_t *dict);
150
151 /* List-manipulation functions */
152 extern list_t *list_new(void);
153 extern int32_t list_length(const list_t *a);
154 extern list_t *list_append(list_t *a, item_t *i);
155 extern list_t *list_append_list(list_t *a, list_t *b);
156 /* Returns an item from the list (index starts at 0), or NULL */
157 extern item_t *list_elem(list_t *l, int32_t index);
158
159 /* Convenience functions */
160 extern list_t *new_closure(closure_t *cl);
161 extern void add_closure(dict_t *dict, cstring_t name, apply_fn apply);
162 extern void *find_cl_if(dict_t *dict, cstring_t name, uint32_t type,
163 bool_t fail_if_invalid, cstring_t desc,
164 struct cloc loc);
165 extern item_t *dict_find_item(dict_t *dict, cstring_t key, bool_t required,
166 cstring_t desc, struct cloc loc);
167 extern string_t dict_read_string(dict_t *dict, cstring_t key, bool_t required,
168 cstring_t desc, struct cloc loc);
169 extern uint32_t dict_read_number(dict_t *dict, cstring_t key, bool_t required,
170 cstring_t desc, struct cloc loc,
171 uint32_t def);
172 /* return value can safely be assigned to int32_t */
173 extern bool_t dict_read_bool(dict_t *dict, cstring_t key, bool_t required,
174 cstring_t desc, struct cloc loc, bool_t def);
175 extern dict_t *dict_read_dict(dict_t *dict, cstring_t key, bool_t required,
176 cstring_t desc, struct cloc loc);
177 const char **dict_read_string_array(dict_t *dict, cstring_t key,
178 bool_t required, cstring_t desc,
179 struct cloc loc, const char *const *def);
180 /* Return value is a NULL-terminated array obtained from malloc;
181 * Individual string values are still owned by config file machinery
182 * and must not be modified or freed. Returns NULL if key not
183 * found. */
184
185 struct flagstr {
186 cstring_t name;
187 uint32_t value;
188 };
189 extern uint32_t string_to_word(cstring_t s, struct cloc loc,
190 struct flagstr *f, cstring_t desc);
191 extern uint32_t string_list_to_word(list_t *l, struct flagstr *f,
192 cstring_t desc);
193
194 /***** END of configuration support *****/
195
196 /***** UTILITY functions *****/
197
198 extern char *safe_strdup(const char *string, const char *message);
199 extern void *safe_malloc(size_t size, const char *message);
200 extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
201 extern void *safe_realloc_ary(void *p, size_t size, size_t count,
202 const char *message);
203
204 #define NEW(p) \
205 ((p)=safe_malloc(sizeof(*(p)), \
206 __FILE__ ":" #p))
207 #define NEW_ARY(p,count) \
208 ((p)=safe_malloc_ary(sizeof(*(p)),(count), \
209 __FILE__ ":" #p "[" #count "]"))
210 #define REALLOC_ARY(p,count) \
211 ((p)=safe_realloc_ary((p),sizeof(*(p)),(count), \
212 __FILE__ ":" #p "[" #count "]"))
213
214 void setcloexec(int fd); /* cannot fail */
215 void setnonblock(int fd); /* cannot fail */
216 void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */
217
218 extern int sys_cmd(const char *file, const char *argc, ...);
219
220 extern uint64_t now_global;
221 extern struct timeval tv_now_global;
222
223 static const uint64_t *const now = &now_global;
224 static const struct timeval *const tv_now = &tv_now_global;
225
226 /* "now" is current program time, in milliseconds. It is derived
227 from tv_now. Both are provided by the event loop. */
228
229 /***** END of utility functions *****/
230
231 /***** START of max_start_pad handling *****/
232
233 extern int32_t site_max_start_pad, transform_max_start_pad,
234 comm_max_start_pad;
235
236 void update_max_start_pad(int32_t *our_module_global, int32_t our_instance);
237 int32_t calculate_max_start_pad(void);
238
239 /***** END of max_start_pad handling *****/
240
241 /***** SCHEDULING support */
242
243 /* If nfds_io is insufficient for your needs, set it to the required
244 number and return ERANGE. timeout is in milliseconds; if it is too
245 high then lower it. It starts at -1 (==infinite). */
246 /* Note that beforepoll_fn may NOT do anything which might change the
247 fds or timeouts wanted by other registered poll loop loopers.
248 Callers should make sure of this by not making any calls into other
249 modules from the beforepoll_fn; the easiest way to ensure this is
250 for beforepoll_fn to only retreive information and not take any
251 action.
252 */
253 typedef int beforepoll_fn(void *st, struct pollfd *fds, int *nfds_io,
254 int *timeout_io);
255 typedef void afterpoll_fn(void *st, struct pollfd *fds, int nfds);
256 /* If beforepoll_fn returned ERANGE, afterpoll_fn gets nfds==0.
257 afterpoll_fn never gets !!(fds[].revents & POLLNVAL) - such
258 a report is detected as a fatal error by the event loop. */
259
260 /* void BEFOREPOLL_WANT_FDS(int want);
261 * Expects: int *nfds_io;
262 * Can perform non-local exit.
263 * Checks whether there is space for want fds. If so, sets *nfds_io.
264 * If not, sets *nfds_io and returns. */
265 #define BEFOREPOLL_WANT_FDS(want) do{ \
266 if (*nfds_io<(want)) { *nfds_io=(want); return ERANGE; } \
267 *nfds_io=(want); \
268 }while(0)
269
270 /* Register interest in the main loop of the program. Before a call
271 to poll() your supplied beforepoll function will be called. After
272 the call to poll() the supplied afterpoll function will be called. */
273 struct poll_interest *register_for_poll(void *st, beforepoll_fn *before,
274 afterpoll_fn *after, cstring_t desc);
275 void deregister_for_poll(struct poll_interest *i);
276
277 /***** END of scheduling support */
278
279 /***** PROGRAM LIFETIME support */
280
281 /* The secnet program goes through a number of phases in its lifetime.
282 Module code may arrange to be called just as various phases are
283 entered.
284
285 Remember to update the table in util.c if changing the set of
286 phases. */
287
288 enum phase {
289 PHASE_INIT,
290 PHASE_GETOPTS, /* Process command-line arguments */
291 PHASE_READCONFIG, /* Parse and process configuration file */
292 PHASE_SETUP, /* Process information in configuration */
293 PHASE_DAEMONIZE, /* Become a daemon (if necessary) */
294 PHASE_GETRESOURCES, /* Obtain all external resources */
295 PHASE_DROPPRIV, /* Last chance for privileged operations */
296 PHASE_RUN,
297 PHASE_SHUTDOWN, /* About to die; delete key material, etc. */
298 PHASE_CHILDPERSIST, /* Forked long-term child: close fds, etc. */
299 /* Keep this last: */
300 NR_PHASES,
301 };
302
303 /* Each module should, in its CHILDPERSIST hooks, close all fds which
304 constitute ownership of important operating system resources, or
305 which are used for IPC with other processes who want to get the
306 usual disconnection effects if the main secnet process dies.
307 CHILDPERSIST hooks are not run if the child is going to exec;
308 so fds such as described above should be CLOEXEC too. */
309
310 typedef void hook_fn(void *self, uint32_t newphase);
311 bool_t add_hook(uint32_t phase, hook_fn *f, void *state);
312 bool_t remove_hook(uint32_t phase, hook_fn *f, void *state);
313
314 extern uint32_t current_phase;
315 extern void enter_phase(uint32_t new_phase);
316
317 void phase_hooks_init(void); /* for main() only */
318 void clear_phase_hooks(uint32_t phase); /* for afterfork() */
319
320 /* Some features (like netlink 'soft' routes) require that secnet
321 retain root privileges. They should indicate that here when
322 appropriate. */
323 extern bool_t require_root_privileges;
324 extern cstring_t require_root_privileges_explanation;
325
326 /* Some modules may want to know whether secnet is going to drop
327 privilege, so that they know whether to do privsep. Call only
328 in phases SETUP and later. */
329 bool_t will_droppriv(void);
330
331 /***** END of program lifetime support *****/
332
333 /***** MODULE support *****/
334
335 /* Module initialisation function type - modules export one function of
336 this type which is called to initialise them. For dynamically loaded
337 modules it's called "secnet_module". */
338 typedef void init_module(dict_t *dict);
339
340 extern void init_builtin_modules(dict_t *dict);
341
342 extern init_module resolver_module;
343 extern init_module random_module;
344 extern init_module udp_module;
345 extern init_module polypath_module;
346 extern init_module util_module;
347 extern init_module site_module;
348 extern init_module transform_eax_module;
349 extern init_module transform_cbcmac_module;
350 extern init_module netlink_module;
351 extern init_module rsa_module;
352 extern init_module dh_module;
353 extern init_module md5_module;
354 extern init_module slip_module;
355 extern init_module tun_module;
356 extern init_module sha1_module;
357 extern init_module sha512_module;
358 extern init_module log_module;
359
360 /***** END of module support *****/
361
362 /***** CLOSURE TYPES and interface definitions *****/
363
364 #define CL_PURE 0
365 #define CL_RESOLVER 1
366 #define CL_RANDOMSRC 2
367 #define CL_RSAPUBKEY 3
368 #define CL_RSAPRIVKEY 4
369 #define CL_COMM 5
370 #define CL_IPIF 6
371 #define CL_LOG 7
372 #define CL_SITE 8
373 #define CL_TRANSFORM 9
374 #define CL_DH 11
375 #define CL_HASH 12
376 #define CL_BUFFER 13
377 #define CL_NETLINK 14
378
379 struct buffer_if;
380
381 /* PURE closure requires no interface */
382
383 /* RESOLVER interface */
384
385 /* Answers to queries are delivered to a function of this
386 type. 'address' will be NULL if there was a problem with the query. It
387 will be freed once resolve_answer_fn returns. naddrs is the actual
388 size of the array at addrs; was_naddrs is the number of addresses
389 actually found in the DNS, which may be bigger if addrs is equal
390 to MAX_PEER_ADDRS (ie there were too many). */
391 typedef void resolve_answer_fn(void *st, const struct comm_addr *addrs,
392 int naddrs, int was_naddrs,
393 const char *name, const char *failwhy);
394 /* name is the same ptr as passed to request, so its lifetime must
395 * be suitable*/
396 typedef bool_t resolve_request_fn(void *st, cstring_t name,
397 int remoteport, struct comm_if *comm,
398 resolve_answer_fn *cb, void *cst);
399 struct resolver_if {
400 void *st;
401 resolve_request_fn *request;
402 };
403
404 /* RANDOMSRC interface */
405
406 /* Return some random data. Cannot fail. */
407 typedef void random_fn(void *st, int32_t bytes, uint8_t *buff);
408
409 struct random_if {
410 void *st;
411 bool_t blocking;
412 random_fn *generate;
413 };
414
415 /* RSAPUBKEY interface */
416
417 typedef bool_t rsa_checksig_fn(void *st, uint8_t *data, int32_t datalen,
418 cstring_t signature);
419 struct rsapubkey_if {
420 void *st;
421 rsa_checksig_fn *check;
422 };
423
424 /* RSAPRIVKEY interface */
425
426 typedef string_t rsa_makesig_fn(void *st, uint8_t *data, int32_t datalen);
427 struct rsaprivkey_if {
428 void *st;
429 rsa_makesig_fn *sign;
430 };
431
432 /* COMM interface */
433
434 struct comm_addr {
435 /* This struct is pure data; in particular comm's clients may
436 freely copy it. */
437 struct comm_if *comm;
438 union iaddr ia;
439 int ix; /* see comment `Re comm_addr.ix' in udp.c */
440 };
441
442 struct comm_clientinfo; /* private for comm */
443
444 typedef struct comm_clientinfo *comm_clientinfo_fn(void *state, dict_t*,
445 struct cloc cloc);
446 /* A comm client may call this during configuration, and then pass
447 * the resulting comm_clientinfo* to some or all sendmsg calls.
448 * The semantics depend on the dict and defined by the comm, and
449 * should be documented in README. */
450
451 /* Return True if the packet was processed, and shouldn't be passed to
452 any other potential receivers. (buf is freed iff True returned.) */
453 typedef bool_t comm_notify_fn(void *state, struct buffer_if *buf,
454 const struct comm_addr *source);
455 typedef void comm_request_notify_fn(void *commst, void *nst,
456 comm_notify_fn *fn);
457 typedef void comm_release_notify_fn(void *commst, void *nst,
458 comm_notify_fn *fn);
459 typedef bool_t comm_sendmsg_fn(void *commst, struct buffer_if *buf,
460 const struct comm_addr *dest,
461 struct comm_clientinfo* /* 0 OK */);
462 /* Only returns false if (we know that) the local network
463 * environment is such that this address cannot work; transient
464 * or unknown/unexpected failures return true. */
465 typedef const char *comm_addr_to_string_fn(void *commst,
466 const struct comm_addr *ca);
467 /* Returned string is in a static buffer. */
468 struct comm_if {
469 void *st;
470 comm_clientinfo_fn *clientinfo;
471 comm_request_notify_fn *request_notify;
472 comm_release_notify_fn *release_notify;
473 comm_sendmsg_fn *sendmsg;
474 comm_addr_to_string_fn *addr_to_string;
475 };
476
477 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib,
478 bool_t ignoreport);
479
480 static inline const char *comm_addr_to_string(const struct comm_addr *ca)
481 {
482 return ca->comm->addr_to_string(ca->comm->st, ca);
483 }
484
485 static inline bool_t comm_addr_equal(const struct comm_addr *a,
486 const struct comm_addr *b)
487 {
488 return a->comm==b->comm && iaddr_equal(&a->ia,&b->ia,False);
489 }
490
491 /* LOG interface */
492
493 #define LOG_MESSAGE_BUFLEN 1023
494
495 typedef void log_msg_fn(void *st, int class, const char *message, ...);
496 typedef void log_vmsg_fn(void *st, int class, const char *message,
497 va_list args);
498 struct log_if {
499 void *st;
500 log_vmsg_fn *vlogfn; /* printf format checking. Use [v]slilog instead */
501 char buff[LOG_MESSAGE_BUFLEN+1];
502 };
503 /* (convenience functions, defined in util.c) */
504 extern void slilog(struct log_if *lf, int class, const char *message, ...)
505 FORMAT(printf,3,4);
506 extern void vslilog(struct log_if *lf, int class, const char *message, va_list)
507 FORMAT(printf,3,0);
508
509 /* Versions which take (parts of) (multiple) messages, using \n to
510 * distinguish one message from another. */
511 extern void slilog_part(struct log_if *lf, int class, const char *message, ...)
512 FORMAT(printf,3,4);
513 extern void vslilog_part(struct log_if *lf, int class, const char *message,
514 va_list) FORMAT(printf,3,0);
515
516 /* SITE interface */
517
518 /* Pretty much a placeholder; allows starting and stopping of processing,
519 key expiry, etc. */
520 typedef void site_control_fn(void *st, bool_t run);
521 typedef uint32_t site_status_fn(void *st);
522 struct site_if {
523 void *st;
524 site_control_fn *control;
525 site_status_fn *status;
526 };
527
528 /* TRANSFORM interface */
529
530 /* A reversable transformation. Transforms buffer in-place; may add
531 data to start or end. (Reverse transformations decrease
532 length, of course.) Transformations may be key-dependent, in which
533 case key material is passed in at initialisation time. They may
534 also depend on internal factors (eg. time) and keep internal
535 state. A struct transform_if only represents a particular type of
536 transformation; instances of the transformation (eg. with
537 particular key material) have a different C type. The same
538 secret key will be used in opposite directions between a pair of
539 secnets; one of these pairs will get direction==False, the other True. */
540
541 typedef struct transform_inst_if *transform_createinstance_fn(void *st);
542 typedef bool_t transform_setkey_fn(void *st, uint8_t *key, int32_t keylen,
543 bool_t direction);
544 typedef bool_t transform_valid_fn(void *st); /* 0: no key; 1: ok */
545 typedef void transform_delkey_fn(void *st);
546 typedef void transform_destroyinstance_fn(void *st);
547
548 typedef enum {
549 transform_apply_ok = 0, /* all is well (everyone may assume==0) */
550 transform_apply_err = 1, /* any other problem */
551 transform_apply_seqrange = 2,
552 /* message decrypted but sequence number was out of recent range */
553 transform_apply_seqdupe = 3,
554 /* message decrypted but was dupe of recent packet */
555 } transform_apply_return;
556
557 static inline bool_t
558 transform_apply_return_badseq(transform_apply_return problem) {
559 return problem == transform_apply_seqrange ||
560 problem == transform_apply_seqdupe;
561 }
562
563 typedef transform_apply_return transform_apply_fn(void *st,
564 struct buffer_if *buf, const char **errmsg);
565
566 struct transform_inst_if {
567 void *st;
568 transform_setkey_fn *setkey;
569 transform_valid_fn *valid;
570 transform_delkey_fn *delkey;
571 transform_apply_fn *forwards;
572 transform_apply_fn *reverse;
573 transform_destroyinstance_fn *destroy;
574 };
575
576 struct transform_if {
577 void *st;
578 int capab_bit;
579 transform_createinstance_fn *create;
580 };
581
582 /* NETLINK interface */
583
584 /* Used by netlink to deliver to site, and by site to deliver to
585 netlink. cid is the client identifier returned by
586 netlink_regnets_fn. If buf has size 0 then the function is just
587 being called for its site-effects (eg. making the site code attempt
588 to bring up a network link) */
589 typedef void netlink_deliver_fn(void *st, struct buffer_if *buf);
590 /* site code can tell netlink when outgoing packets will be dropped,
591 so netlink can generate appropriate ICMP and make routing decisions */
592 #define LINK_QUALITY_UNUSED 0 /* This link is unused, do not make this netlink */
593 #define LINK_QUALITY_DOWN 1 /* No chance of a packet being delivered right away*/
594 #define LINK_QUALITY_DOWN_STALE_ADDRESS 2 /* Link down, old address information */
595 #define LINK_QUALITY_DOWN_CURRENT_ADDRESS 3 /* Link down, current address information */
596 #define LINK_QUALITY_UP 4 /* Link active */
597 #define MAXIMUM_LINK_QUALITY 3
598 typedef void netlink_link_quality_fn(void *st, uint32_t quality);
599 typedef void netlink_register_fn(void *st, netlink_deliver_fn *deliver,
600 void *dst, uint32_t *localmtu_r /* NULL ok */);
601 typedef void netlink_output_config_fn(void *st, struct buffer_if *buf);
602 typedef bool_t netlink_check_config_fn(void *st, struct buffer_if *buf);
603 typedef void netlink_set_mtu_fn(void *st, int32_t new_mtu);
604 struct netlink_if {
605 void *st;
606 netlink_register_fn *reg;
607 netlink_deliver_fn *deliver;
608 netlink_link_quality_fn *set_quality;
609 netlink_set_mtu_fn *set_mtu;
610 };
611
612 /* DH interface */
613
614 /* Write public value corresponding to the secret to the public buffer, and
615 * return its actual length. The size of the buffer is given in publiclen,
616 * which will be at least the public_len reported by the closure. The
617 * secretlen will be the secret_len reported by the closure. This operation
618 * is not expected to fail.
619 */
620 typedef int32_t dh_makepublic_fn(void *st, void *public, int32_t publiclen,
621 uint8_t *secret, int32_t secretlen);
622
623 /* Fills buffer (up to buflen) with shared secret. The publiclen comes from
624 * the remote site, and may not be acceptable, though it has been checked for
625 * memory-safety. The secretlen and buflen are the secret_len and shared_len
626 * reported by the closure, respectively. Return false on faliure (e.g., if
627 * the publiclen is unacceptable).
628 */
629 typedef bool_t dh_makeshared_fn(void *st,
630 uint8_t *secret, int32_t secretlen,
631 const void *public, int32_t publiclen,
632 uint8_t *sharedsecret, int32_t buflen);
633
634 struct dh_if {
635 void *st;
636 int32_t secret_len; /* Size of random secret to generate */
637 int32_t public_len; /* Maximum number of bytes needed for public value */
638 int32_t shared_len; /* Size of generated shared secret */
639 int capab_bit;
640 dh_makepublic_fn *makepublic;
641 dh_makeshared_fn *makeshared;
642 };
643
644 /* HASH interface */
645
646 typedef void *hash_init_fn(void);
647 typedef void hash_update_fn(void *st, const void *buf, int32_t len);
648 typedef void hash_final_fn(void *st, uint8_t *digest);
649 struct hash_if {
650 int32_t len; /* Hash output length in bytes */
651 hash_init_fn *init;
652 hash_update_fn *update;
653 hash_final_fn *final;
654 };
655
656 /* BUFFER interface */
657
658 struct buffer_if {
659 bool_t free;
660 cstring_t owner; /* Set to constant string */
661 uint32_t flags; /* How paranoid should we be? */
662 struct cloc loc; /* Where we were defined */
663 uint8_t *base;
664 uint8_t *start;
665 int32_t size; /* Size of buffer contents */
666 int32_t alloclen; /* Total length allocated at base */
667 };
668
669 /***** LOG functions *****/
670
671 #define M_DEBUG_CONFIG 0x001
672 #define M_DEBUG_PHASE 0x002
673 #define M_DEBUG 0x004
674 #define M_INFO 0x008
675 #define M_NOTICE 0x010
676 #define M_WARNING 0x020
677 #define M_ERR 0x040
678 #define M_SECURITY 0x080
679 #define M_FATAL 0x100
680
681 /* The fatal() family of functions require messages that do not end in '\n' */
682 extern NORETURN(fatal(const char *message, ...)) FORMAT(printf,1,2);
683 extern NORETURN(fatal_perror(const char *message, ...)) FORMAT(printf,1,2);
684 extern NORETURN(fatal_status(int status, const char *message, ...))
685 FORMAT(printf,2,3);
686 extern NORETURN(fatal_perror_status(int status, const char *message, ...))
687 FORMAT(printf,2,3);
688
689 /* Convenient nonfatal logging. Requires message that does not end in '\n'.
690 * If class contains M_FATAL, exits (after entering PHASE_SHUTDOWN).
691 * lg, errnoval and loc may sensibly be 0. desc must NOT be 0.
692 * lg_[v]perror save and restore errno. */
693 void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
694 int class, int errnoval, const char *fmt, va_list al)
695 FORMAT(printf,6,0);
696 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
697 int class, int errnoval, const char *fmt, ...)
698 FORMAT(printf,6,7);
699 void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
700 int class, int status, const char *progname);
701
702 /* The cfgfatal() family of functions require messages that end in '\n' */
703 extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,
704 const char *message, ...)) FORMAT(printf,3,4);
705 extern void cfgfile_postreadcheck(struct cloc loc, FILE *f);
706 extern NORETURN(vcfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
707 cstring_t facility, const char *message,
708 va_list))
709 FORMAT(printf,4,0);
710 extern NORETURN(cfgfatal_maybefile(FILE *maybe_f, struct cloc loc,
711 cstring_t facility,
712 const char *message, ...))
713 FORMAT(printf,4,5);
714
715 extern void Message(uint32_t class, const char *message, ...)
716 FORMAT(printf,2,3);
717 extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log);
718
719 /***** END of log functions *****/
720
721 #define STRING2(x) #x
722 #define STRING(x) STRING2(x)
723
724 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
725 #define ARRAY_SIZE(ary) (sizeof((ary))/sizeof((ary)[0]))
726
727 /*
728 * void COPY_OBJ( OBJECT& dst, const OBJECT& src);
729 * void COPY_ARRAY(OBJECT *dst, const OBJECT *src, INTEGER count);
730 * // Typesafe: we check that the type OBJECT is the same in both cases.
731 * // It is OK to use COPY_OBJ on an array object, provided it's
732 * // _actually_ the whole array object and not decayed into a
733 * // pointer (e.g. a formal parameter).
734 */
735 #define COPY_OBJ(dst,src) \
736 (&(dst)==&(src), memcpy(&(dst),&(src),sizeof((dst))))
737 #define COPY_ARRAY(dst,src,count) \
738 (&(dst)[0]==&(src)[0], memcpy((dst),(src),sizeof((dst)[0])*(count)))
739
740 #endif /* secnet_h */