145634e6e409bf6c0c9f7b56717707adb9e0f423
1 /* misc. universal things
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
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>.
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
22 #include <sys/types.h>
25 # define USED_BY_KLIPS /* ignore */
27 # define USED_BY_KLIPS UNUSED
31 # define USED_BY_DEBUG /* ignore */
33 # define USED_BY_DEBUG UNUSED
36 /* type of serial number of a state object
37 * Needed in connections.h and state.h; here to simplify dependencies.
39 typedef unsigned long so_serial_t
;
40 #define SOS_NOBODY 0 /* null serial number */
41 #define SOS_FIRST 1 /* first normal serial number */
43 /* memory allocation */
45 extern void *clone_bytes(const void *orig
, size_t size
);
47 #define clone_thing(orig) clone_bytes((const void *)&(orig), sizeof(orig))
49 #define clone_str(str) \
50 ((str) == NULL? NULL : strdup(str))
52 #define replace(p, q) \
53 { free(p); (p) = (q); }
56 /* chunk is a simple pointer-and-size abstraction */
62 typedef struct chunk chunk_t
;
64 extern const chunk_t chunk_empty
;
66 static inline chunk_t
chunk_create(u_char
*ptr
, size_t len
)
68 chunk_t chunk
= {ptr
, len
};
72 static inline void chunk_free(chunk_t
*chunk
)
78 static inline void chunk_clear(chunk_t
*chunk
)
82 memset(chunk
->ptr
, 0, chunk
->len
);
87 static inline bool chunk_equals(chunk_t a
, chunk_t b
)
89 return a
.ptr
!= NULL
&& b
.ptr
!= NULL
&&
90 a
.len
== b
.len
&& memeq(a
.ptr
, b
.ptr
, a
.len
);
93 extern chunk_t
chunk_create_clone(u_char
*ptr
, chunk_t chunk
);
95 #define chunk_clone(chunk) \
96 chunk_create_clone((chunk).len ? malloc((chunk).len) : NULL, chunk)
98 #define chunk_from_buf(str) { str, sizeof(str) }
100 #define chunkcpy(dst, chunk) \
101 { memcpy(dst, chunk.ptr, chunk.len); dst += chunk.len;}
103 extern char* temporary_cyclic_buffer(void);
104 extern const char* concatenate_paths(const char *a
, const char *b
);
106 /* compare two chunks */
107 extern int chunk_compare(chunk_t a
, chunk_t b
);
109 /* move a chunk to a memory position and free it after insertion */
110 extern void mv_chunk(u_char
**pos
, chunk_t content
);
112 /* write the binary contents of a chunk_t to a file */
113 extern bool write_chunk(const char *filename
, const char *label
, chunk_t ch
114 ,mode_t mask
, bool force
);
116 /* display a date either in local or UTC time */
117 extern char* timetoa(const time_t *time
, bool utc
);
119 /* warns a predefined interval before expiry */
120 extern const char* check_expiry(time_t expiration_date
,
121 int warning_interval
, bool strict
);
123 #define MAX_PROMPT_PASS_TRIALS 5
124 #define PROMPT_PASS_LEN 64
126 /* struct used to prompt for a secret passphrase
127 * from a console with file descriptor fd
130 char secret
[PROMPT_PASS_LEN
+1];
135 /* size of timetoa string buffer */
136 #define TIMETOA_BUF 30
138 /* filter eliminating the directory entries '.' and '..' */
139 typedef struct dirent dirent_t
;
140 extern int file_select(const dirent_t
*entry
);
142 /* cleanly exit Pluto */
144 extern void exit_pluto(int /*status*/) NEVER_RETURNS
;
148 #define zero(x) memset((x), '\0', sizeof(*(x)))
150 /* are all bytes 0? */
151 extern bool all_zero(const unsigned char *m
, size_t len
);
153 /* pad_up(n, m) is the amount to add to n to make it a multiple of m */
154 #define pad_up(n, m) (((m) - 1) - (((n) + (m) - 1) % (m)))