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