9b8b52dd91d7d7be1d0d1ece376e78f3b4d7c02d
[strongswan.git] / src / libstrongswan / definitions.h
1 /**
2 * @file definitions.h
3 *
4 * @brief General purpose definitions and macros.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 * Copyright (C) 1998, 1999 D. Hugh Redelmeier. (Endian stuff)
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef DEFINITIONS_H_
26 #define DEFINITIONS_H_
27
28 #include <stddef.h>
29
30 /* stolen from FreeS/WAN */
31 #if linux
32 # if defined(i386) && !defined(__i386__)
33 # define __i386__ 1
34 # define MYHACKFORTHIS 1
35 # endif
36 # include <endian.h>
37 # ifdef MYHACKFORTHIS
38 # undef __i386__
39 # undef MYHACKFORTHIS
40 # endif
41 #elif !(defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN) && defined(BYTE_ORDER))
42 /* we don't know how to do this, so we require the macros to be defined
43 * with compiler flags:
44 * -DBIG_ENDIAN=4321 -DLITTLE_ENDIAN=1234 -DBYTE_ORDER=BIG_ENDIAN
45 * or -DBIG_ENDIAN=4321 -DLITTLE_ENDIAN=1234 -DBYTE_ORDER=LITTLE_ENDIAN
46 * Thse match the GNU definitions
47 */
48 # include <sys/endian.h>
49 #endif
50
51 #ifndef BIG_ENDIAN
52 #error "BIG_ENDIAN must be defined"
53 #endif
54
55 #ifndef LITTLE_ENDIAN
56 #error "LITTLE_ENDIAN must be defined"
57 #endif
58
59 #ifndef BYTE_ORDER
60 #error "BYTE_ORDER must be defined"
61 #endif
62
63 #define BITS_PER_BYTE 8
64 #define RSA_MIN_OCTETS (1024 / BITS_PER_BYTE)
65 #define RSA_MIN_OCTETS_UGH "RSA modulus too small for security: less than 1024 bits"
66 #define RSA_MAX_OCTETS (8192 / BITS_PER_BYTE)
67 #define RSA_MAX_OCTETS_UGH "RSA modulus too large: more than 8192 bits"
68
69 /**
70 * Default length for various auxiliary text buffers
71 */
72 #define BUF_LEN 512
73
74 /**
75 * Macro compares two strings for equality
76 */
77 #define streq(x,y) (strcmp(x, y) == 0)
78
79 /**
80 * Macro compares two binary blobs for equality
81 */
82 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
83
84 /**
85 * Macro gives back larger of two values.
86 */
87 #define max(x,y) ((x) > (y) ? (x):(y))
88
89 /**
90 * Macro gives back smaller of two values.
91 */
92 #define min(x,y) ((x) < (y) ? (x):(y))
93
94 /**
95 * Call destructor of a object if object != NULL
96 */
97 #define DESTROY_IF(obj) if (obj) obj->destroy(obj)
98
99 /**
100 * Debug macro to follow control flow
101 */
102 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
103
104 /**
105 * Macro to allocate a sized type.
106 *
107 * @param thing object on which a sizeof is performed
108 * @return poiner to allocated memory
109 */
110 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
111
112
113 /**
114 * Mapping entry which defines the end of a mapping_t array.
115 */
116 #define MAPPING_END (-1)
117
118 typedef struct mapping_t mapping_t;
119
120 /**
121 * @brief Mapping entry, where enum-to-string mappings are stored.
122 */
123 struct mapping_t
124 {
125 /**
126 * Enumeration value.
127 */
128 int value;
129
130 /**
131 * Mapped string.
132 */
133 char *string;
134 };
135
136
137 /**
138 * @brief Find a mapping_string in the mapping[].
139 *
140 * @param mappings mappings array
141 * @param value enum-value to get the string from
142 *
143 */
144 char *mapping_find(mapping_t *mappings, int value);
145
146 /**
147 * @brief Describes an enumeration
148 * enum_name() returns the name of an enum value, or NULL if invalid.
149 */
150 typedef const struct enum_names enum_names;
151
152 struct enum_names {
153 unsigned long en_first; /* first value in range */
154 unsigned long en_last; /* last value in range (inclusive) */
155 const char *const *en_names;
156 enum_names *en_next_range; /* descriptor of next range */
157 };
158
159 /**
160 * @brief Returns the name of an enum value, or NULL if invalid
161 */
162 const char *enum_name(enum_names *ed, unsigned long val);
163
164 #endif /*DEFINITIONS_H_*/