157 lines
3.9 KiB
C
157 lines
3.9 KiB
C
/* misc. universal things
|
|
* Copyright (C) 1997 Angelos D. Keromytis.
|
|
* Copyright (C) 1998-2001 D. Hugh Redelmeier.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
* for more details.
|
|
*
|
|
* RCSID $Id$
|
|
*/
|
|
|
|
#ifndef _DEFS_H
|
|
#define _DEFS_H
|
|
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
|
|
#ifdef KLIPS
|
|
# define USED_BY_KLIPS /* ignore */
|
|
#else
|
|
# define USED_BY_KLIPS UNUSED
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
# define USED_BY_DEBUG /* ignore */
|
|
#else
|
|
# define USED_BY_DEBUG UNUSED
|
|
#endif
|
|
|
|
/* type of serial number of a state object
|
|
* Needed in connections.h and state.h; here to simplify dependencies.
|
|
*/
|
|
typedef unsigned long so_serial_t;
|
|
#define SOS_NOBODY 0 /* null serial number */
|
|
#define SOS_FIRST 1 /* first normal serial number */
|
|
|
|
/* memory allocation */
|
|
|
|
extern void *clone_bytes(const void *orig, size_t size);
|
|
|
|
#define clone_thing(orig) clone_bytes((const void *)&(orig), sizeof(orig))
|
|
|
|
#define clone_str(str) \
|
|
((str) == NULL? NULL : strdup(str))
|
|
|
|
#define replace(p, q) \
|
|
{ free(p); (p) = (q); }
|
|
|
|
|
|
/* chunk is a simple pointer-and-size abstraction */
|
|
|
|
struct chunk {
|
|
u_char *ptr;
|
|
size_t len;
|
|
};
|
|
typedef struct chunk chunk_t;
|
|
|
|
extern const chunk_t chunk_empty;
|
|
|
|
static inline chunk_t chunk_create(u_char *ptr, size_t len)
|
|
{
|
|
chunk_t chunk = {ptr, len};
|
|
return chunk;
|
|
}
|
|
|
|
static inline void chunk_free(chunk_t *chunk)
|
|
{
|
|
free(chunk->ptr);
|
|
*chunk = chunk_empty;
|
|
}
|
|
|
|
static inline void chunk_clear(chunk_t *chunk)
|
|
{
|
|
if (chunk->ptr)
|
|
{
|
|
memset(chunk->ptr, 0, chunk->len);
|
|
chunk_free(chunk);
|
|
}
|
|
}
|
|
|
|
static inline bool chunk_equals(chunk_t a, chunk_t b)
|
|
{
|
|
return a.ptr != NULL && b.ptr != NULL &&
|
|
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
|
|
}
|
|
|
|
extern chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk);
|
|
|
|
#define chunk_clone(chunk) \
|
|
chunk_create_clone((chunk).len ? malloc((chunk).len) : NULL, chunk)
|
|
|
|
#define chunk_from_buf(str) { str, sizeof(str) }
|
|
|
|
#define chunkcpy(dst, chunk) \
|
|
{ memcpy(dst, chunk.ptr, chunk.len); dst += chunk.len;}
|
|
|
|
extern char* temporary_cyclic_buffer(void);
|
|
extern const char* concatenate_paths(const char *a, const char *b);
|
|
|
|
/* compare two chunks */
|
|
extern int chunk_compare(chunk_t a, chunk_t b);
|
|
|
|
/* move a chunk to a memory position and free it after insertion */
|
|
extern void mv_chunk(u_char **pos, chunk_t content);
|
|
|
|
/* write the binary contents of a chunk_t to a file */
|
|
extern bool write_chunk(const char *filename, const char *label, chunk_t ch
|
|
,mode_t mask, bool force);
|
|
|
|
/* display a date either in local or UTC time */
|
|
extern char* timetoa(const time_t *time, bool utc);
|
|
|
|
/* warns a predefined interval before expiry */
|
|
extern const char* check_expiry(time_t expiration_date,
|
|
int warning_interval, bool strict);
|
|
|
|
#define MAX_PROMPT_PASS_TRIALS 5
|
|
#define PROMPT_PASS_LEN 64
|
|
|
|
/* struct used to prompt for a secret passphrase
|
|
* from a console with file descriptor fd
|
|
*/
|
|
typedef struct {
|
|
char secret[PROMPT_PASS_LEN+1];
|
|
bool prompt;
|
|
int fd;
|
|
} prompt_pass_t;
|
|
|
|
/* size of timetoa string buffer */
|
|
#define TIMETOA_BUF 30
|
|
|
|
/* filter eliminating the directory entries '.' and '..' */
|
|
typedef struct dirent dirent_t;
|
|
extern int file_select(const dirent_t *entry);
|
|
|
|
/* cleanly exit Pluto */
|
|
|
|
extern void exit_pluto(int /*status*/) NEVER_RETURNS;
|
|
|
|
|
|
/* zero all bytes */
|
|
#define zero(x) memset((x), '\0', sizeof(*(x)))
|
|
|
|
/* are all bytes 0? */
|
|
extern bool all_zero(const unsigned char *m, size_t len);
|
|
|
|
/* pad_up(n, m) is the amount to add to n to make it a multiple of m */
|
|
#define pad_up(n, m) (((m) - 1) - (((n) + (m) - 1) % (m)))
|
|
|
|
#endif /* _DEFS_H */
|