24
24
#include <openswan.h>
26
26
#include "constants.h"
27
#include "openswan/ipsec_policy.h"
29
30
#include "whack.h" /* for RC_LOG_SERIOUS */
31
const chunk_t empty_chunk = { NULL, 0 };
34
all_zero(const unsigned char *m, size_t len)
38
for (i = 0; i != len; i++)
44
32
/* Convert MP_INT to network form (binary octets, big-endian).
45
33
* We do the malloc; caller must eventually do free.
95
* LEAK_DETECTIVE puts a wrapper around each allocation and maintains
96
* a list of live ones. If a dead one is freed, an assertion MIGHT fail.
97
* If the live list is currupted, that will often be detected.
98
* In the end, report_leaks() is called, and the names of remaining
99
* live allocations are printed. At the moment, it is hoped, not that
100
* the list is empty, but that there will be no surprises.
103
* - "struct iface" and "device name" (for "discovered" net interfaces)
104
* - "struct event in event_schedule()" (events not associated with states)
105
* - "Pluto lock name" (one only, needed until end -- why bother?)
108
#ifdef LEAK_DETECTIVE
110
/* this magic number is 3671129837 decimal (623837458 complemented) */
111
#define LEAK_MAGIC 0xDAD0FEEDul
116
union mhdr *older, *newer;
119
unsigned long junk; /* force maximal alignment */
122
static union mhdr *allocs = NULL;
124
void *alloc_bytes(size_t size, const char *name)
126
union mhdr *p = malloc(sizeof(union mhdr) + size);
129
exit_log("unable to malloc %lu bytes for %s"
130
, (unsigned long) size, name);
137
p->i.magic = LEAK_MAGIC;
139
memset(p+1, '\0', size);
144
clone_bytes(const void *orig, size_t size, const char *name)
146
void *p = alloc_bytes(size, name);
148
memcpy(p, orig, size);
157
passert(ptr != NULL);
158
p = ((union mhdr *)ptr) - 1;
159
passert(p->i.magic == LEAK_MAGIC);
160
if (p->i.older != NULL)
162
passert(p->i.older->i.newer == p);
163
p->i.older->i.newer = p->i.newer;
165
if (p->i.newer == NULL)
167
passert(p == allocs);
172
passert(p->i.newer->i.older == p);
173
p->i.newer->i.older = p->i.older;
175
p->i.magic = ~LEAK_MAGIC;
189
passert(p->i.magic == LEAK_MAGIC);
190
passert(pprev == p->i.newer);
194
if (p == NULL || pprev->i.name != p->i.name)
197
plog("leak: %lu * %s", n, pprev->i.name);
199
plog("leak: %s", pprev->i.name);
205
#else /* !LEAK_DETECTIVE */
207
void *alloc_bytes(size_t size, const char *name)
209
void *p = malloc(size);
212
exit_log("unable to malloc %lu bytes for %s"
213
, (unsigned long) size, name);
214
memset(p, '\0', size);
218
void *clone_bytes(const void *orig, size_t size, const char *name)
220
void *p = malloc(size);
223
exit_log("unable to malloc %lu bytes for %s"
224
, (unsigned long) size, name);
225
memcpy(p, orig, size);
228
#endif /* !LEAK_DETECTIVE */
231
80
/* Names of the months */
233
82
static const char* months[] = {
240
89
* Display a date either in local or UTC time
243
timetoa(const time_t *time, bool utc)
92
timetoa(const time_t *time, bool utc, char *b, size_t blen)
245
static char buf[TIMETOA_BUF];
247
94
if (*time == UNDEFINED_TIME)
248
sprintf(buf, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
95
snprintf(b, blen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
251
98
struct tm *t = (utc)? gmtime(time) : localtime(time);
253
sprintf(buf, "%s %02d %02d:%02d:%02d%s%04d",
100
snprintf(b, blen, "%s %02d %02d:%02d:%02d%s%04d",
254
101
months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
255
102
(utc)?" UTC ":" ", t->tm_year + 1900
261
108
/* checks if the expiration date has been reached and
177
/* compare two chunks, returns zero if a equals b
178
* negative/positive if a is earlier/later in the alphabet than b
181
cmp_chunk(chunk_t a, chunk_t b)
183
int cmp_len, len, cmp_value;
185
cmp_len = a.len - b.len;
186
len = (cmp_len < 0)? a.len : b.len;
187
cmp_value = memcmp(a.ptr, b.ptr, len);
189
return (cmp_value == 0)? cmp_len : cmp_value;