1 /* misc. universal things
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * RCSID $Id: defs.h,v 1.31 2002/03/15 22:30:14 dhr Exp $
20 # define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
21 # define NEVER_RETURNS __attribute__ ((noreturn))
22 # define UNUSED __attribute__ ((unused))
23 # define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
25 # define PRINTF_LIKE(n) /* ignore */
26 # define NEVER_RETURNS /* ignore */
27 # define UNUSED /* ignore */
28 # define BLANK_FORMAT ""
32 # define USED_BY_KLIPS /* ignore */
34 # define USED_BY_KLIPS UNUSED
38 # define USED_BY_DEBUG /* ignore */
40 # define USED_BY_DEBUG UNUSED
43 /* type of serial number of a state object
44 * Needed in connections.h and state.h; here to simplify dependencies.
46 typedef unsigned long so_serial_t;
47 #define SOS_NOBODY 0 /* null serial number */
48 #define SOS_FIRST 1 /* first normal serial number */
50 /* memory allocation */
52 extern void *alloc_bytes(size_t size, const char *name);
53 #define alloc_thing(thing, name) (alloc_bytes(sizeof(thing), (name)))
55 extern void *clone_bytes(const void *orig, size_t size, const char *name);
56 #define clone_thing(orig, name) clone_bytes((const void *)&(orig), sizeof(orig), (name))
57 #define clone_str(str, name) \
58 ((str) == NULL? NULL : clone_bytes((str), strlen((str))+1, (name)))
61 extern void pfree(void *ptr);
62 extern void report_leaks(void);
64 # define pfree(ptr) free(ptr) /* ordinary stdc free */
66 #define pfreeany(p) { if ((p) != NULL) pfree(p); }
67 #define replace(p, q) { pfreeany(p); (p) = (q); }
70 /* chunk is a simple pointer-and-size abstraction */
76 typedef struct chunk chunk_t;
78 #define setchunk(ch, addr, size) { (ch).ptr = (addr); (ch).len = (size); }
79 /* NOTE: freeanychunk, unlike pfreeany, NULLs .ptr */
80 #define freeanychunk(ch) { pfreeany((ch).ptr); (ch).ptr = NULL; }
81 #define clonetochunk(ch, addr, size, name) \
82 { (ch).ptr = clone_bytes((addr), (ch).len = (size), name); }
83 #define clonereplacechunk(ch, addr, size, name) \
84 { pfreeany((ch).ptr); clonetochunk(ch, addr, size, name); }
85 #define chunkcpy(dst, chunk) \
86 { memcpy(dst, chunk.ptr, chunk.len); dst += chunk.len;}
88 extern const chunk_t empty_chunk;
90 /* display a date either in local or UTC time */
91 extern char* timetoa(const time_t *time, bool utc);
93 /* warns a predefined interval before expiry */
94 extern const char* check_expiry(time_t expiration_date,
95 int warning_interval, bool strict);
97 /* no time defined in time_t */
98 #define UNDEFINED_TIME 0
100 /* size of timetoa string buffer */
101 #define TIMETOA_BUF 30
103 #define NO_IP 0 /* our s_addr value signifying no IPv4 address */
105 #define is_NO_IP(a) ((a).u.v4.sin_addr.s_addr == NO_IP)
107 /* cleanly exit Pluto */
109 extern void exit_pluto(int /*status*/) NEVER_RETURNS;
113 #define zero(x) memset((x), '\0', sizeof(*(x)))
115 /* are all bytes 0? */
116 extern bool all_zero(const unsigned char *m, size_t len);
119 /* some MP utilities */
123 extern void n_to_mpz(MP_INT *mp, const u_char *nbytes, size_t nlen);
124 extern chunk_t mpz_to_n(const MP_INT *mp, size_t bytes);
126 /* var := mod(base ** exp, mod), ensuring var is mpz_inited */
127 #define mpz_init_powm(flag, var, base, exp, mod) { \
131 mpz_powm(&(var), &(base), &(exp), (mod)); \
135 /* pad_up(n, m) is the amount to add to n to make it a multiple of m */
136 #define pad_up(n, m) (((m) - 1) - (((n) + (m) - 1) % (m)))