ac18228c7d7bd1f4552d94e8d45811b737637840
[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$
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 /* type of serial number of a state object
36 * Needed in connections.h and state.h; here to simplify dependencies.
37 */
38 typedef unsigned long so_serial_t;
39 #define SOS_NOBODY 0 /* null serial number */
40 #define SOS_FIRST 1 /* first normal serial number */
41
42 /* memory allocation */
43
44 extern void *clone_bytes(const void *orig, size_t size);
45
46 #define clone_thing(orig) clone_bytes((const void *)&(orig), sizeof(orig))
47
48 #define clone_str(str) \
49 ((str) == NULL? NULL : strdup(str))
50
51 #define replace(p, q) { free(p); (p) = (q); }
52
53
54 /* chunk is a simple pointer-and-size abstraction */
55
56 struct chunk {
57 u_char *ptr;
58 size_t len;
59 };
60 typedef struct chunk chunk_t;
61
62 #define setchunk(ch, addr, size) { (ch).ptr = (addr); (ch).len = (size); }
63
64 #define strchunk(str) { str, sizeof(str) }
65
66 /* NOTE: freeanychunk NULLs .ptr */
67 #define freeanychunk(ch) { free((ch).ptr); (ch).ptr = NULL; }
68
69 #define clonetochunk(ch, addr, size) \
70 { (ch).ptr = clone_bytes((addr), (ch).len = (size)); }
71
72 #define clonereplacechunk(ch, addr, size) \
73 { free((ch).ptr); clonetochunk(ch, addr, size); }
74
75 #define chunkcpy(dst, chunk) \
76 { memcpy(dst, chunk.ptr, chunk.len); dst += chunk.len;}
77
78 #define same_chunk(a, b) \
79 ( (a).len == (b).len && memcmp((a).ptr, (b).ptr, (b).len) == 0 )
80
81 extern char* temporary_cyclic_buffer(void);
82 extern const char* concatenate_paths(const char *a, const char *b);
83
84 extern const chunk_t chunk_empty;
85
86 /* compare two chunks */
87 extern int cmp_chunk(chunk_t a, chunk_t b);
88
89 /* move a chunk to a memory position and free it after insertion */
90 extern void mv_chunk(u_char **pos, chunk_t content);
91
92 /* write the binary contents of a chunk_t to a file */
93 extern bool write_chunk(const char *filename, const char *label, chunk_t ch
94 ,mode_t mask, bool force);
95
96 /* display a date either in local or UTC time */
97 extern char* timetoa(const time_t *time, bool utc);
98
99 /* warns a predefined interval before expiry */
100 extern const char* check_expiry(time_t expiration_date,
101 int warning_interval, bool strict);
102
103 #define MAX_PROMPT_PASS_TRIALS 5
104 #define PROMPT_PASS_LEN 64
105
106 /* struct used to prompt for a secret passphrase
107 * from a console with file descriptor fd
108 */
109 typedef struct {
110 char secret[PROMPT_PASS_LEN+1];
111 bool prompt;
112 int fd;
113 } prompt_pass_t;
114
115 /* size of timetoa string buffer */
116 #define TIMETOA_BUF 30
117
118 /* filter eliminating the directory entries '.' and '..' */
119 typedef struct dirent dirent_t;
120 extern int file_select(const dirent_t *entry);
121
122 /* cleanly exit Pluto */
123
124 extern void exit_pluto(int /*status*/) NEVER_RETURNS;
125
126
127 /* zero all bytes */
128 #define zero(x) memset((x), '\0', sizeof(*(x)))
129
130 /* are all bytes 0? */
131 extern bool all_zero(const unsigned char *m, size_t len);
132
133 /* pad_up(n, m) is the amount to add to n to make it a multiple of m */
134 #define pad_up(n, m) (((m) - 1) - (((n) + (m) - 1) % (m)))
135
136 #endif /* _DEFS_H */