OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / pluto / defs.h
1 /* misc. universal things
2  * Copyright (C) 1997 Angelos D. Keromytis.
3  * Copyright (C) 1998-2001  D. Hugh Redelmeier.
4  *
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>.
9  *
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
13  * for more details.
14  *
15  * RCSID $Id: defs.h,v 1.31 2002/03/15 22:30:14 dhr Exp $
16  */
17
18 /* GCC magic! */
19 #ifdef GCC_LINT
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 */
24 #else
25 # define PRINTF_LIKE(n) /* ignore */
26 # define NEVER_RETURNS /* ignore */
27 # define UNUSED /* ignore */
28 # define BLANK_FORMAT ""
29 #endif
30
31 #ifdef KLIPS
32 # define USED_BY_KLIPS  /* ignore */
33 #else
34 # define USED_BY_KLIPS  UNUSED
35 #endif
36
37 #ifdef DEBUG
38 # define USED_BY_DEBUG  /* ignore */
39 #else
40 # define USED_BY_DEBUG  UNUSED
41 #endif
42
43 /* type of serial number of a state object
44  * Needed in connections.h and state.h; here to simplify dependencies.
45  */
46 typedef unsigned long so_serial_t;
47 #define SOS_NOBODY      0       /* null serial number */
48 #define SOS_FIRST       1       /* first normal serial number */
49
50 /* memory allocation */
51
52 extern void *alloc_bytes(size_t size, const char *name);
53 #define alloc_thing(thing, name) (alloc_bytes(sizeof(thing), (name)))
54
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)))
59
60 #ifdef LEAK_DETECTIVE
61   extern void pfree(void *ptr);
62   extern void report_leaks(void);
63 #else
64 # define pfree(ptr) free(ptr)   /* ordinary stdc free */
65 #endif
66 #define pfreeany(p) { if ((p) != NULL) pfree(p); }
67 #define replace(p, q) { pfreeany(p); (p) = (q); }
68
69
70 /* chunk is a simple pointer-and-size abstraction */
71
72 struct chunk {
73     u_char *ptr;
74     size_t len;
75     };
76 typedef struct chunk chunk_t;
77
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;}
87
88 extern const chunk_t empty_chunk;
89
90 /* display a date either in local or UTC time */
91 extern char* timetoa(const time_t *time, bool utc);
92
93 /* warns a predefined interval before expiry */
94 extern const char* check_expiry(time_t expiration_date,
95     int warning_interval, bool strict);
96
97 /* no time defined in time_t */
98 #define UNDEFINED_TIME  0
99
100 /* size of timetoa string buffer */
101 #define TIMETOA_BUF     30
102
103 #define NO_IP   0       /* our s_addr value signifying no IPv4 address */
104
105 #define is_NO_IP(a) ((a).u.v4.sin_addr.s_addr == NO_IP)
106
107 /* cleanly exit Pluto */
108
109 extern void exit_pluto(int /*status*/) NEVER_RETURNS;
110
111
112 /* zero all bytes */
113 #define zero(x) memset((x), '\0', sizeof(*(x)))
114
115 /* are all bytes 0? */
116 extern bool all_zero(const unsigned char *m, size_t len);
117
118
119 /* some MP utilities */
120
121 #include <gmp.h>
122
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);
125
126 /* var := mod(base ** exp, mod), ensuring var is mpz_inited */
127 #define mpz_init_powm(flag, var, base, exp, mod) { \
128     if (!(flag)) \
129         mpz_init(&(var)); \
130     (flag) = TRUE; \
131     mpz_powm(&(var), &(base), &(exp), (mod)); \
132     }
133
134
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)))