xref: /openbsd-src/usr.sbin/nsd/util.h (revision bf87c3c07c3ad89262e2b8cae09f17e70aa9e1ee)
162ac0c33Sjakob /*
262ac0c33Sjakob  * util.h -- set of various support routines.
362ac0c33Sjakob  *
4dd5b221eSsthen  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
562ac0c33Sjakob  *
662ac0c33Sjakob  * See LICENSE for the license.
762ac0c33Sjakob  *
862ac0c33Sjakob  */
962ac0c33Sjakob 
103efee2e1Sflorian #ifndef UTIL_H
113efee2e1Sflorian #define UTIL_H
1262ac0c33Sjakob 
1362ac0c33Sjakob #include <sys/time.h>
1462ac0c33Sjakob #include <stdarg.h>
1562ac0c33Sjakob #include <stdio.h>
1662ac0c33Sjakob #include <time.h>
1762ac0c33Sjakob struct rr;
18533110e2Sbrad struct buffer;
19533110e2Sbrad struct region;
20063644e9Sflorian struct nsd;
2162ac0c33Sjakob 
2262ac0c33Sjakob #ifdef HAVE_SYSLOG_H
2362ac0c33Sjakob #  include <syslog.h>
2462ac0c33Sjakob #else
2562ac0c33Sjakob #  define LOG_ERR 3
2662ac0c33Sjakob #  define LOG_WARNING 4
2762ac0c33Sjakob #  define LOG_NOTICE 5
2862ac0c33Sjakob #  define LOG_INFO 6
29ee5153b7Sflorian 
30ee5153b7Sflorian /* Unused, but passed to log_open. */
31ee5153b7Sflorian #  define LOG_PID 0x01
32ee5153b7Sflorian #  define LOG_DAEMON (3<<3)
3362ac0c33Sjakob #endif
3462ac0c33Sjakob 
3562ac0c33Sjakob #define ALIGN_UP(n, alignment)  \
3662ac0c33Sjakob 	(((n) + (alignment) - 1) & (~((alignment) - 1)))
3762ac0c33Sjakob #define PADDING(n, alignment)   \
3862ac0c33Sjakob 	(ALIGN_UP((n), (alignment)) - (n))
3962ac0c33Sjakob 
4062ac0c33Sjakob /*
4162ac0c33Sjakob  * Initialize the logging system.  All messages are logged to stderr
4262ac0c33Sjakob  * until log_open and log_set_log_function are called.
4362ac0c33Sjakob  */
4462ac0c33Sjakob void log_init(const char *ident);
4562ac0c33Sjakob 
46*bf87c3c0Sflorian #ifdef USE_LOG_PROCESS_ROLE
47*bf87c3c0Sflorian /*
48*bf87c3c0Sflorian  * Set the name of the role for the process (for debugging purposes)
49*bf87c3c0Sflorian  */
50*bf87c3c0Sflorian void log_set_process_role(const char* role);
51*bf87c3c0Sflorian #else
52*bf87c3c0Sflorian #define log_set_process_role(role) /* empty */
53*bf87c3c0Sflorian #endif
54*bf87c3c0Sflorian 
5562ac0c33Sjakob /*
5662ac0c33Sjakob  * Open the system log.  If FILENAME is not NULL, a log file is opened
5762ac0c33Sjakob  * as well.
5862ac0c33Sjakob  */
5962ac0c33Sjakob void log_open(int option, int facility, const char *filename);
6062ac0c33Sjakob 
6162ac0c33Sjakob /*
6262ac0c33Sjakob  * Reopen the logfile.
6362ac0c33Sjakob  */
6462ac0c33Sjakob void log_reopen(const char *filename, uint8_t verbose);
6562ac0c33Sjakob 
6662ac0c33Sjakob /*
6762ac0c33Sjakob  * Finalize the logging system.
6862ac0c33Sjakob  */
6962ac0c33Sjakob void log_finalize(void);
7062ac0c33Sjakob 
7162ac0c33Sjakob /*
7262ac0c33Sjakob  * Type of function to use for the actual logging.
7362ac0c33Sjakob  */
7462ac0c33Sjakob typedef void log_function_type(int priority, const char *message);
7562ac0c33Sjakob 
7662ac0c33Sjakob /*
7762ac0c33Sjakob  * The function used to log to the log file.
7862ac0c33Sjakob  */
7962ac0c33Sjakob log_function_type log_file;
8062ac0c33Sjakob 
8162ac0c33Sjakob /*
8262ac0c33Sjakob  * The function used to log to syslog.  The messages are also logged
8362ac0c33Sjakob  * using log_file.
8462ac0c33Sjakob  */
8562ac0c33Sjakob log_function_type log_syslog;
8662ac0c33Sjakob 
8762ac0c33Sjakob /*
88ac5517e4Sflorian  * The function used to log to syslog only.
89ac5517e4Sflorian  */
90ac5517e4Sflorian log_function_type log_only_syslog;
91ac5517e4Sflorian 
92ac5517e4Sflorian /*
9362ac0c33Sjakob  * Set the logging function to use (log_file or log_syslog).
9462ac0c33Sjakob  */
9562ac0c33Sjakob void log_set_log_function(log_function_type *log_function);
9662ac0c33Sjakob 
9762ac0c33Sjakob /*
9862ac0c33Sjakob  * Log a message using the current log function.
9962ac0c33Sjakob  */
10062ac0c33Sjakob void log_msg(int priority, const char *format, ...)
10162ac0c33Sjakob 	ATTR_FORMAT(printf, 2, 3);
10262ac0c33Sjakob 
10362ac0c33Sjakob /*
10462ac0c33Sjakob  * Log a message using the current log function.
10562ac0c33Sjakob  */
10662ac0c33Sjakob void log_vmsg(int priority, const char *format, va_list args);
10762ac0c33Sjakob 
10862ac0c33Sjakob /*
10962ac0c33Sjakob  * Verbose output switch
11062ac0c33Sjakob  */
11162ac0c33Sjakob extern int verbosity;
11262ac0c33Sjakob #define VERBOSITY(level, args)					\
11362ac0c33Sjakob 	do {							\
11462ac0c33Sjakob 		if ((level) <= verbosity) {			\
11562ac0c33Sjakob 			log_msg args ;				\
11662ac0c33Sjakob 		}						\
11762ac0c33Sjakob 	} while (0)
11862ac0c33Sjakob 
11962ac0c33Sjakob /*
12062ac0c33Sjakob  * Set the INDEXth bit of BITS to 1.
12162ac0c33Sjakob  */
12262ac0c33Sjakob void set_bit(uint8_t bits[], size_t index);
12362ac0c33Sjakob 
12462ac0c33Sjakob /*
12562ac0c33Sjakob  * Set the INDEXth bit of BITS to 0.
12662ac0c33Sjakob  */
12762ac0c33Sjakob void clear_bit(uint8_t bits[], size_t index);
12862ac0c33Sjakob 
12962ac0c33Sjakob /*
13062ac0c33Sjakob  * Return the value of the INDEXth bit of BITS.
13162ac0c33Sjakob  */
13262ac0c33Sjakob int get_bit(uint8_t bits[], size_t index);
13362ac0c33Sjakob 
13462ac0c33Sjakob /* A general purpose lookup table */
13562ac0c33Sjakob typedef struct lookup_table lookup_table_type;
13662ac0c33Sjakob struct lookup_table {
13762ac0c33Sjakob 	int id;
13862ac0c33Sjakob 	const char *name;
13962ac0c33Sjakob };
14062ac0c33Sjakob 
14162ac0c33Sjakob /*
14262ac0c33Sjakob  * Looks up the table entry by name, returns NULL if not found.
14362ac0c33Sjakob  */
14462ac0c33Sjakob lookup_table_type *lookup_by_name(lookup_table_type table[], const char *name);
14562ac0c33Sjakob 
14662ac0c33Sjakob /*
14762ac0c33Sjakob  * Looks up the table entry by id, returns NULL if not found.
14862ac0c33Sjakob  */
14962ac0c33Sjakob lookup_table_type *lookup_by_id(lookup_table_type table[], int id);
15062ac0c33Sjakob 
15162ac0c33Sjakob /*
15262ac0c33Sjakob  * (Re-)allocate SIZE bytes of memory.  Report an error if the memory
15362ac0c33Sjakob  * could not be allocated and exit the program.  These functions never
15462ac0c33Sjakob  * return NULL.
15562ac0c33Sjakob  */
15662ac0c33Sjakob void *xalloc(size_t size);
1578d8f1862Ssthen void *xmallocarray(size_t num, size_t size);
15862ac0c33Sjakob void *xalloc_zero(size_t size);
1598d8f1862Ssthen void *xalloc_array_zero(size_t num, size_t size);
16062ac0c33Sjakob void *xrealloc(void *ptr, size_t size);
1613b24e79eSsthen char *xstrdup(const char *src);
16262ac0c33Sjakob 
16362ac0c33Sjakob /*
1644ab91c82Sjakob  * Mmap allocator routines.
1654ab91c82Sjakob  *
1664ab91c82Sjakob  */
1674ab91c82Sjakob #ifdef USE_MMAP_ALLOC
1684ab91c82Sjakob void *mmap_alloc(size_t size);
1694ab91c82Sjakob void mmap_free(void *ptr);
1704ab91c82Sjakob #endif /* USE_MMAP_ALLOC */
1714ab91c82Sjakob 
1724ab91c82Sjakob /*
17362ac0c33Sjakob  * Write SIZE bytes of DATA to FILE.  Report an error on failure.
17462ac0c33Sjakob  *
17562ac0c33Sjakob  * Returns 0 on failure, 1 on success.
17662ac0c33Sjakob  */
17762ac0c33Sjakob int write_data(FILE *file, const void *data, size_t size);
17862ac0c33Sjakob 
17962ac0c33Sjakob /*
18062ac0c33Sjakob  * like write_data, but keeps track of crc
18162ac0c33Sjakob  */
18262ac0c33Sjakob int write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc);
18362ac0c33Sjakob 
18462ac0c33Sjakob /*
18562ac0c33Sjakob  * Write the complete buffer to the socket, irrespective of short
18662ac0c33Sjakob  * writes or interrupts. This function blocks to write the data.
18762ac0c33Sjakob  * Returns 0 on error, 1 on success.
18862ac0c33Sjakob  */
18962ac0c33Sjakob int write_socket(int s, const void *data, size_t size);
19062ac0c33Sjakob 
19162ac0c33Sjakob /*
19262ac0c33Sjakob  * Copy data allowing for unaligned accesses in network byte order
19362ac0c33Sjakob  * (big endian).
19462ac0c33Sjakob  */
19562ac0c33Sjakob static inline void
write_uint16(void * dst,uint16_t data)19662ac0c33Sjakob write_uint16(void *dst, uint16_t data)
19762ac0c33Sjakob {
19862ac0c33Sjakob #ifdef ALLOW_UNALIGNED_ACCESSES
19962ac0c33Sjakob 	* (uint16_t *) dst = htons(data);
20062ac0c33Sjakob #else
20162ac0c33Sjakob 	uint8_t *p = (uint8_t *) dst;
20262ac0c33Sjakob 	p[0] = (uint8_t) ((data >> 8) & 0xff);
20362ac0c33Sjakob 	p[1] = (uint8_t) (data & 0xff);
20462ac0c33Sjakob #endif
20562ac0c33Sjakob }
20662ac0c33Sjakob 
20762ac0c33Sjakob static inline void
write_uint32(void * dst,uint32_t data)20862ac0c33Sjakob write_uint32(void *dst, uint32_t data)
20962ac0c33Sjakob {
21062ac0c33Sjakob #ifdef ALLOW_UNALIGNED_ACCESSES
21162ac0c33Sjakob 	* (uint32_t *) dst = htonl(data);
21262ac0c33Sjakob #else
21362ac0c33Sjakob 	uint8_t *p = (uint8_t *) dst;
21462ac0c33Sjakob 	p[0] = (uint8_t) ((data >> 24) & 0xff);
21562ac0c33Sjakob 	p[1] = (uint8_t) ((data >> 16) & 0xff);
21662ac0c33Sjakob 	p[2] = (uint8_t) ((data >> 8) & 0xff);
21762ac0c33Sjakob 	p[3] = (uint8_t) (data & 0xff);
21862ac0c33Sjakob #endif
21962ac0c33Sjakob }
22062ac0c33Sjakob 
2216e9bf1eeSflorian static inline void
write_uint64(void * dst,uint64_t data)2226e9bf1eeSflorian write_uint64(void *dst, uint64_t data)
2236e9bf1eeSflorian {
2246e9bf1eeSflorian 	uint8_t *p = (uint8_t *) dst;
2256e9bf1eeSflorian 	p[0] = (uint8_t) ((data >> 56) & 0xff);
2266e9bf1eeSflorian 	p[1] = (uint8_t) ((data >> 48) & 0xff);
2276e9bf1eeSflorian 	p[2] = (uint8_t) ((data >> 40) & 0xff);
2286e9bf1eeSflorian 	p[3] = (uint8_t) ((data >> 32) & 0xff);
2296e9bf1eeSflorian 	p[4] = (uint8_t) ((data >> 24) & 0xff);
2306e9bf1eeSflorian 	p[5] = (uint8_t) ((data >> 16) & 0xff);
2316e9bf1eeSflorian 	p[6] = (uint8_t) ((data >> 8) & 0xff);
2326e9bf1eeSflorian 	p[7] = (uint8_t) (data & 0xff);
2336e9bf1eeSflorian }
2346e9bf1eeSflorian 
23562ac0c33Sjakob /*
23662ac0c33Sjakob  * Copy data allowing for unaligned accesses in network byte order
23762ac0c33Sjakob  * (big endian).
23862ac0c33Sjakob  */
23962ac0c33Sjakob static inline uint16_t
read_uint16(const void * src)24062ac0c33Sjakob read_uint16(const void *src)
24162ac0c33Sjakob {
24262ac0c33Sjakob #ifdef ALLOW_UNALIGNED_ACCESSES
243977db6e5Sflorian 	return ntohs(* (const uint16_t *) src);
24462ac0c33Sjakob #else
245977db6e5Sflorian 	const uint8_t *p = (const uint8_t *) src;
24662ac0c33Sjakob 	return (p[0] << 8) | p[1];
24762ac0c33Sjakob #endif
24862ac0c33Sjakob }
24962ac0c33Sjakob 
25062ac0c33Sjakob static inline uint32_t
read_uint32(const void * src)25162ac0c33Sjakob read_uint32(const void *src)
25262ac0c33Sjakob {
25362ac0c33Sjakob #ifdef ALLOW_UNALIGNED_ACCESSES
254977db6e5Sflorian 	return ntohl(* (const uint32_t *) src);
25562ac0c33Sjakob #else
256977db6e5Sflorian 	const uint8_t *p = (const uint8_t *) src;
25762ac0c33Sjakob 	return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
25862ac0c33Sjakob #endif
25962ac0c33Sjakob }
26062ac0c33Sjakob 
2616e9bf1eeSflorian static inline uint64_t
read_uint64(const void * src)2626e9bf1eeSflorian read_uint64(const void *src)
2636e9bf1eeSflorian {
264977db6e5Sflorian 	const uint8_t *p = (const uint8_t *) src;
2656e9bf1eeSflorian 	return
2666e9bf1eeSflorian 	    ((uint64_t)p[0] << 56) |
2676e9bf1eeSflorian 	    ((uint64_t)p[1] << 48) |
2686e9bf1eeSflorian 	    ((uint64_t)p[2] << 40) |
2696e9bf1eeSflorian 	    ((uint64_t)p[3] << 32) |
2706e9bf1eeSflorian 	    ((uint64_t)p[4] << 24) |
2716e9bf1eeSflorian 	    ((uint64_t)p[5] << 16) |
2726e9bf1eeSflorian 	    ((uint64_t)p[6] <<  8) |
2736e9bf1eeSflorian 	    (uint64_t)p[7];
2746e9bf1eeSflorian }
2756e9bf1eeSflorian 
27662ac0c33Sjakob /*
27762ac0c33Sjakob  * Print debugging information using log_msg,
27862ac0c33Sjakob  * set the logfile as /dev/stdout or /dev/stderr if you like.
27962ac0c33Sjakob  * nsd -F 0xFFFF enables all debug facilities.
28062ac0c33Sjakob  */
28162ac0c33Sjakob #define DEBUG_PARSER           0x0001U
28262ac0c33Sjakob #define DEBUG_ZONEC            0x0002U
28362ac0c33Sjakob #define DEBUG_QUERY            0x0004U
28462ac0c33Sjakob #define DEBUG_DBACCESS         0x0008U
28562ac0c33Sjakob #define DEBUG_NAME_COMPRESSION 0x0010U
28662ac0c33Sjakob #define DEBUG_XFRD             0x0020U
28762ac0c33Sjakob #define DEBUG_IPC              0x0040U
28862ac0c33Sjakob 
28962ac0c33Sjakob extern unsigned nsd_debug_facilities;
29062ac0c33Sjakob extern int nsd_debug_level;
29162ac0c33Sjakob #ifdef NDEBUG
29262ac0c33Sjakob #define DEBUG(facility, level, args)  /* empty */
29362ac0c33Sjakob #else
29462ac0c33Sjakob #define DEBUG(facility, level, args)				\
29562ac0c33Sjakob 	do {							\
29662ac0c33Sjakob 		if ((facility) & nsd_debug_facilities &&	\
29762ac0c33Sjakob 		    (level) <= nsd_debug_level) {		\
29862ac0c33Sjakob 			log_msg args ;				\
29962ac0c33Sjakob 		}						\
30062ac0c33Sjakob 	} while (0)
30162ac0c33Sjakob #endif
30262ac0c33Sjakob 
303533110e2Sbrad /* set to true to log time prettyprinted, or false to print epoch */
304533110e2Sbrad extern int log_time_asc;
30562ac0c33Sjakob 
30662ac0c33Sjakob /*
30762ac0c33Sjakob  * Timespec functions.
30862ac0c33Sjakob  */
30962ac0c33Sjakob int timespec_compare(const struct timespec *left, const struct timespec *right);
31062ac0c33Sjakob void timespec_add(struct timespec *left, const struct timespec *right);
31162ac0c33Sjakob void timespec_subtract(struct timespec *left, const struct timespec *right);
31262ac0c33Sjakob 
31362ac0c33Sjakob static inline void
timeval_to_timespec(struct timespec * left,const struct timeval * right)31462ac0c33Sjakob timeval_to_timespec(struct timespec *left,
31562ac0c33Sjakob 		    const struct timeval *right)
31662ac0c33Sjakob {
31762ac0c33Sjakob 	left->tv_sec = right->tv_sec;
31862ac0c33Sjakob 	left->tv_nsec = 1000 * right->tv_usec;
31962ac0c33Sjakob }
32062ac0c33Sjakob 
321275a8d89Sflorian /* get the time */
322275a8d89Sflorian void get_time(struct timespec* t);
32362ac0c33Sjakob 
32462ac0c33Sjakob /*
32562ac0c33Sjakob  * Converts a string representation of a period of time into
32662ac0c33Sjakob  * a long integer of seconds or serial value.
32762ac0c33Sjakob  *
32862ac0c33Sjakob  * Set the endptr to the first illegal character.
32962ac0c33Sjakob  *
33062ac0c33Sjakob  * Interface is similar as strtol(3)
33162ac0c33Sjakob  *
33262ac0c33Sjakob  * Returns:
33362ac0c33Sjakob  *	LONG_MIN if underflow occurs
33462ac0c33Sjakob  *	LONG_MAX if overflow occurs.
33562ac0c33Sjakob  *	otherwise number of seconds
33662ac0c33Sjakob  *
33762ac0c33Sjakob  * XXX These functions do not check the range.
33862ac0c33Sjakob  *
33962ac0c33Sjakob  */
34062ac0c33Sjakob uint32_t strtoserial(const char *nptr, const char **endptr);
34162ac0c33Sjakob uint32_t strtottl(const char *nptr, const char **endptr);
34262ac0c33Sjakob 
34362ac0c33Sjakob /*
34462ac0c33Sjakob  * Convert binary data to a string of hexadecimal characters.
34562ac0c33Sjakob  */
34662ac0c33Sjakob ssize_t hex_ntop(uint8_t const *src, size_t srclength, char *target,
34762ac0c33Sjakob 		 size_t targsize);
34862ac0c33Sjakob ssize_t hex_pton(const char* src, uint8_t* target, size_t targsize);
34962ac0c33Sjakob 
35062ac0c33Sjakob /*
35162ac0c33Sjakob  * convert base32 data from and to string. Returns length.
35262ac0c33Sjakob  * -1 on error. Use (byte count*8)%5==0.
35362ac0c33Sjakob  */
35462ac0c33Sjakob int b32_pton(char const *src, uint8_t *target, size_t targsize);
35562ac0c33Sjakob int b32_ntop(uint8_t const *src, size_t srclength, char *target,
35662ac0c33Sjakob 	size_t targsize);
35762ac0c33Sjakob 
35862ac0c33Sjakob /*
35962ac0c33Sjakob  * Strip trailing and leading whitespace from str.
36062ac0c33Sjakob  */
36162ac0c33Sjakob void strip_string(char *str);
36262ac0c33Sjakob 
36362ac0c33Sjakob /*
3642fd875a4Ssthen  * Convert a single (hexadecimal) digit to its integer value.
36562ac0c33Sjakob  */
36662ac0c33Sjakob int hexdigit_to_int(char ch);
36762ac0c33Sjakob 
36862ac0c33Sjakob /*
36962ac0c33Sjakob  * Convert TM to seconds since epoch (midnight, January 1st, 1970).
37062ac0c33Sjakob  * Like timegm(3), which is not always available.
37162ac0c33Sjakob  */
37262ac0c33Sjakob time_t mktime_from_utc(const struct tm *tm);
37362ac0c33Sjakob 
37462ac0c33Sjakob /*
37562ac0c33Sjakob  * Add bytes to given crc. Returns new CRC sum.
37662ac0c33Sjakob  * Start crc val with 0xffffffff on first call. XOR crc with
37762ac0c33Sjakob  * 0xffffffff at the end again to get final POSIX 1003.2 checksum.
37862ac0c33Sjakob  */
37962ac0c33Sjakob uint32_t compute_crc(uint32_t crc, uint8_t* data, size_t len);
38062ac0c33Sjakob 
38162ac0c33Sjakob /*
38262ac0c33Sjakob  * Compares two 32-bit serial numbers as defined in RFC1982.  Returns
38362ac0c33Sjakob  * <0 if a < b, 0 if a == b, and >0 if a > b.  The result is undefined
38462ac0c33Sjakob  * if a != b but neither is greater or smaller (see RFC1982 section
38562ac0c33Sjakob  * 3.2.).
38662ac0c33Sjakob  */
38762ac0c33Sjakob int compare_serial(uint32_t a, uint32_t b);
38862ac0c33Sjakob 
38962ac0c33Sjakob /*
3904ab91c82Sjakob  * Generate a random query ID.
3914ab91c82Sjakob  */
3924ab91c82Sjakob uint16_t qid_generate(void);
393dd5b221eSsthen /* value between 0 .. (max-1) inclusive */
394dd5b221eSsthen int random_generate(int max);
3954ab91c82Sjakob 
3964ab91c82Sjakob /*
39762ac0c33Sjakob  * call region_destroy on (region*)data, useful for region_add_cleanup().
39862ac0c33Sjakob  */
39962ac0c33Sjakob void cleanup_region(void *data);
40062ac0c33Sjakob 
40162ac0c33Sjakob /*
40262ac0c33Sjakob  * Region used to store owner and origin of previous RR (used
40362ac0c33Sjakob  * for pretty printing of zone data).
40462ac0c33Sjakob  * Keep the same between calls to print_rr.
40562ac0c33Sjakob  */
40662ac0c33Sjakob struct state_pretty_rr {
40762ac0c33Sjakob 	struct region *previous_owner_region;
40862ac0c33Sjakob 	const struct dname *previous_owner;
40962ac0c33Sjakob 	const struct dname *previous_owner_origin;
41062ac0c33Sjakob };
41162ac0c33Sjakob struct state_pretty_rr* create_pretty_rr(struct region* region);
41262ac0c33Sjakob /* print rr to file, returns 0 on failure(nothing is written) */
413533110e2Sbrad int print_rr(FILE *out, struct state_pretty_rr* state, struct rr *record,
414533110e2Sbrad 	struct region* tmp_region, struct buffer* tmp_buffer);
41562ac0c33Sjakob 
41662ac0c33Sjakob /*
41762ac0c33Sjakob  * Convert a numeric rcode value to a human readable string
41862ac0c33Sjakob  */
41962ac0c33Sjakob const char* rcode2str(int rc);
42062ac0c33Sjakob 
421dd5b221eSsthen void addr2str(
42262ac0c33Sjakob #ifdef INET6
423dd5b221eSsthen 	struct sockaddr_storage *addr
42462ac0c33Sjakob #else
425dd5b221eSsthen 	struct sockaddr_in *addr
42662ac0c33Sjakob #endif
427dd5b221eSsthen 	, char* str, size_t len);
42862ac0c33Sjakob 
429eab1363eSsthen /* print addr@port */
430eab1363eSsthen void addrport2str(
431eab1363eSsthen #ifdef INET6
432eab1363eSsthen 	struct sockaddr_storage *addr
433eab1363eSsthen #else
434eab1363eSsthen 	struct sockaddr_in *addr
435eab1363eSsthen #endif
436eab1363eSsthen 	, char* str, size_t len);
437eab1363eSsthen 
4384b6a9f59Sflorian /** copy dirname string and append slash.  Previous dirname is leaked,
4394b6a9f59Sflorian  * but it is to be used once, at startup, for chroot */
4404b6a9f59Sflorian void append_trailing_slash(const char** dirname, struct region* region);
4414b6a9f59Sflorian 
4424b6a9f59Sflorian /** true if filename starts with chroot or is not absolute */
4434b6a9f59Sflorian int file_inside_chroot(const char* fname, const char* chr);
4444b6a9f59Sflorian 
4454b6a9f59Sflorian /** Something went wrong, give error messages and exit. */
4464b6a9f59Sflorian void error(const char *format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
4474b6a9f59Sflorian 
448308d2509Sflorian #if HAVE_CPUSET_T
449308d2509Sflorian int number_of_cpus(void);
450308d2509Sflorian int set_cpu_affinity(cpuset_t *set);
451308d2509Sflorian #endif
452308d2509Sflorian 
453063644e9Sflorian /* Add a cookie secret. If there are no secrets yet, the secret will become
454063644e9Sflorian  * the active secret. Otherwise it will become the staging secret.
455063644e9Sflorian  * Active secrets are used to both verify and create new DNS Cookies.
456063644e9Sflorian  * Staging secrets are only used to verify DNS Cookies. */
457063644e9Sflorian void add_cookie_secret(struct nsd* nsd, uint8_t* secret);
458063644e9Sflorian /* Makes the staging cookie secret active and the active secret staging. */
459063644e9Sflorian void activate_cookie_secret(struct nsd* nsd);
460063644e9Sflorian /* Drop a cookie secret. Drops the staging secret. An active secret will not
461063644e9Sflorian  * be dropped. */
462063644e9Sflorian void drop_cookie_secret(struct nsd* nsd);
4633efee2e1Sflorian #endif /* UTIL_H */
464