cosmetics
[strongswan.git] / src / 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.10 2006/01/04 21:00:43 as Exp $
16 */
17
18 #ifndef _DEFS_H
19 #define _DEFS_H
20
21 #include <sys/types.h>
22
23 #ifdef KLIPS
24 # define USED_BY_KLIPS /* ignore */
25 #else
26 # define USED_BY_KLIPS UNUSED
27 #endif
28
29 #ifdef DEBUG
30 # define USED_BY_DEBUG /* ignore */
31 #else
32 # define USED_BY_DEBUG UNUSED
33 #endif
34
35 /* Length of temporary buffers */
36
37 #define BUF_LEN 512
38
39 /* type of serial number of a state object
40 * Needed in connections.h and state.h; here to simplify dependencies.
41 */
42 typedef unsigned long so_serial_t;
43 #define SOS_NOBODY 0 /* null serial number */
44 #define SOS_FIRST 1 /* first normal serial number */
45
46 /* memory allocation */
47
48 extern void *alloc_bytes(size_t size, const char *name);
49 #define alloc_thing(thing, name) (alloc_bytes(sizeof(thing), (name)))
50
51 extern void *clone_bytes(const void *orig, size_t size, const char *name);
52 #define clone_thing(orig, name) clone_bytes((const void *)&(orig), sizeof(orig), (name))
53 #define clone_str(str, name) \
54 ((str) == NULL? NULL : clone_bytes((str), strlen((str))+1, (name)))
55
56 #ifdef LEAK_DETECTIVE
57 extern void pfree(void *ptr);
58 extern void report_leaks(void);
59 #else
60 # define pfree(ptr) free(ptr) /* ordinary stdc free */
61 #endif
62 #define pfreeany(p) { if ((p) != NULL) pfree(p); }
63 #define replace(p, q) { pfreeany(p); (p) = (q); }
64
65
66 /* chunk is a simple pointer-and-size abstraction */
67
68 struct chunk {
69 u_char *ptr;
70 size_t len;
71 };
72 typedef struct chunk chunk_t;
73
74 #define setchunk(ch, addr, size) { (ch).ptr = (addr); (ch).len = (size); }
75 #define strchunk(str) { str, sizeof(str) }
76 /* NOTE: freeanychunk, unlike pfreeany, NULLs .ptr */
77 #define freeanychunk(ch) { pfreeany((ch).ptr); (ch).ptr = NULL; }
78 #define clonetochunk(ch, addr, size, name) \
79 { (ch).ptr = clone_bytes((addr), (ch).len = (size), name); }
80 #define clonereplacechunk(ch, addr, size, name) \
81 { pfreeany((ch).ptr); clonetochunk(ch, addr, size, name); }
82 #define chunkcpy(dst, chunk) \
83 { memcpy(dst, chunk.ptr, chunk.len); dst += chunk.len;}
84 #define same_chunk(a, b) \
85 ( (a).len == (b).len && memcmp((a).ptr, (b).ptr, (b).len) == 0 )
86
87 extern char* temporary_cyclic_buffer(void);
88 extern const char* concatenate_paths(const char *a, const char *b);
89
90 extern const chunk_t empty_chunk;
91
92 /* compare two chunks */
93 extern bool cmp_chunk(chunk_t a, chunk_t b);
94
95 /* move a chunk to a memory position and free it after insertion */
96 extern void mv_chunk(u_char **pos, chunk_t content);
97
98 /* write the binary contents of a chunk_t to a file */
99 extern bool write_chunk(const char *filename, const char *label, chunk_t ch
100 ,mode_t mask, bool force);
101
102 /* display a date either in local or UTC time */
103 extern char* timetoa(const time_t *time, bool utc);
104
105 /* warns a predefined interval before expiry */
106 extern const char* check_expiry(time_t expiration_date,
107 int warning_interval, bool strict);
108
109 #define MAX_PROMPT_PASS_TRIALS 5
110 #define PROMPT_PASS_LEN 64
111
112 /* struct used to prompt for a secret passphrase
113 * from a console with file descriptor fd
114 */
115 typedef struct {
116 char secret[PROMPT_PASS_LEN+1];
117 bool prompt;
118 int fd;
119 } prompt_pass_t;
120
121 /* no time defined in time_t */
122 #define UNDEFINED_TIME 0
123
124 /* size of timetoa string buffer */
125 #define TIMETOA_BUF 30
126
127 /* filter eliminating the directory entries '.' and '..' */
128 typedef struct dirent dirent_t;
129 extern int file_select(const dirent_t *entry);
130
131 /* cleanly exit Pluto */
132
133 extern void exit_pluto(int /*status*/) NEVER_RETURNS;
134
135
136 /* zero all bytes */
137 #define zero(x) memset((x), '\0', sizeof(*(x)))
138
139 /* are all bytes 0? */
140 extern bool all_zero(const unsigned char *m, size_t len);
141
142 /* pad_up(n, m) is the amount to add to n to make it a multiple of m */
143 #define pad_up(n, m) (((m) - 1) - (((n) + (m) - 1) % (m)))
144
145 #endif /* _DEFS_H */