/*
* Initialize the logging system. All messages are logged to stderr
* until log_open and log_set_log_function are called.
*/
void log_init(const char *ident);
/*
* Open the system log. If FILENAME is not NULL, a log file is opened
* as well.
*/
void log_open(int option, int facility, const char *filename);
/*
* Finalize the logging system.
*/
void log_finalize(void);
/*
* Type of function to use for the actual logging.
*/
typedef void log_function_type(int priority, const char *message);
/*
* The function used to log to the log file.
*/
log_function_type log_file;
/*
* The function used to log to syslog. The messages are also logged
* using log_file.
*/
log_function_type log_syslog;
/*
* The function used to log to syslog only.
*/
log_function_type log_only_syslog;
/*
* Set the logging function to use (log_file or log_syslog).
*/
void log_set_log_function(log_function_type *log_function);
/*
* Log a message using the current log function.
*/
void log_msg(int priority, const char *format, ...)
ATTR_FORMAT(printf, 2, 3);
/*
* Log a message using the current log function.
*/
void log_vmsg(int priority, const char *format, va_list args);
/*
* Verbose output switch
*/
extern int verbosity;
#define VERBOSITY(level, args) \
do { \
if ((level) <= verbosity) { \
log_msg args ; \
} \
} while (0)
/*
* Set the INDEXth bit of BITS to 1.
*/
void set_bit(uint8_t bits[], size_t index);
/*
* Set the INDEXth bit of BITS to 0.
*/
void clear_bit(uint8_t bits[], size_t index);
/*
* Return the value of the INDEXth bit of BITS.
*/
int get_bit(uint8_t bits[], size_t index);
/* A general purpose lookup table */
typedef struct lookup_table lookup_table_type;
struct lookup_table {
int id;
const char *name;
};
/*
* Looks up the table entry by name, returns NULL if not found.
*/
lookup_table_type *lookup_by_name(lookup_table_type table[], const char *name);
/*
* Looks up the table entry by id, returns NULL if not found.
*/
lookup_table_type *lookup_by_id(lookup_table_type table[], int id);
/*
* (Re-)allocate SIZE bytes of memory. Report an error if the memory
* could not be allocated and exit the program. These functions never
* return NULL.
*/
void *xalloc(size_t size);
void *xmallocarray(size_t num, size_t size);
void *xalloc_zero(size_t size);
void *xalloc_array_zero(size_t num, size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *src);
/*
* Write SIZE bytes of DATA to FILE. Report an error on failure.
*
* Returns 0 on failure, 1 on success.
*/
int write_data(FILE *file, const void *data, size_t size);
/*
* like write_data, but keeps track of crc
*/
int write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc);
/*
* Write the complete buffer to the socket, irrespective of short
* writes or interrupts. This function blocks to write the data.
* Returns 0 on error, 1 on success.
*/
int write_socket(int s, const void *data, size_t size);
/* get the time */
void get_time(struct timespec* t);
/*
* Converts a string representation of a period of time into
* a long integer of seconds or serial value.
*
* Set the endptr to the first illegal character.
*
* Interface is similar as strtol(3)
*
* Returns:
* LONG_MIN if underflow occurs
* LONG_MAX if overflow occurs.
* otherwise number of seconds
*
* XXX These functions do not check the range.
*
*/
uint32_t strtoserial(const char *nptr, const char **endptr);
uint32_t strtottl(const char *nptr, const char **endptr);
/*
* Convert binary data to a string of hexadecimal characters.
*/
ssize_t hex_ntop(uint8_t const *src, size_t srclength, char *target,
size_t targsize);
ssize_t hex_pton(const char* src, uint8_t* target, size_t targsize);
/*
* convert base32 data from and to string. Returns length.
* -1 on error. Use (byte count*8)%5==0.
*/
int b32_pton(char const *src, uint8_t *target, size_t targsize);
int b32_ntop(uint8_t const *src, size_t srclength, char *target,
size_t targsize);
/*
* Strip trailing and leading whitespace from str.
*/
void strip_string(char *str);
/*
* Convert a single (hexadecimal) digit to its integer value.
*/
int hexdigit_to_int(char ch);
/*
* Convert TM to seconds since epoch (midnight, January 1st, 1970).
* Like timegm(3), which is not always available.
*/
time_t mktime_from_utc(const struct tm *tm);
/*
* Add bytes to given crc. Returns new CRC sum.
* Start crc val with 0xffffffff on first call. XOR crc with
* 0xffffffff at the end again to get final POSIX 1003.2 checksum.
*/
uint32_t compute_crc(uint32_t crc, uint8_t* data, size_t len);
/*
* Compares two 32-bit serial numbers as defined in RFC1982. Returns
* <0 if a < b, 0 if a == b, and >0 if a > b. The result is undefined
* if a != b but neither is greater or smaller (see RFC1982 section
* 3.2.).
*/
int compare_serial(uint32_t a, uint32_t b);
/*
* Generate a random query ID.
*/
uint16_t qid_generate(void);
/* value between 0 .. (max-1) inclusive */
int random_generate(int max);
/*
* call region_destroy on (region*)data, useful for region_add_cleanup().
*/
void cleanup_region(void *data);
/*
* Region used to store owner and origin of previous RR (used
* for pretty printing of zone data).
* Keep the same between calls to print_rr.
*/
struct state_pretty_rr {
struct region *previous_owner_region;
const struct dname *previous_owner;
const struct dname *previous_owner_origin;
};
struct state_pretty_rr* create_pretty_rr(struct region* region);
/* print rr to file, returns 0 on failure(nothing is written) */
int print_rr(FILE *out, struct state_pretty_rr* state, struct rr *record,
struct region* tmp_region, struct buffer* tmp_buffer);
/*
* Convert a numeric rcode value to a human readable string
*/
const char* rcode2str(int rc);
/** copy dirname string and append slash. Previous dirname is leaked,
* but it is to be used once, at startup, for chroot */
void append_trailing_slash(const char** dirname, struct region* region);
/** true if filename starts with chroot or is not absolute */
int file_inside_chroot(const char* fname, const char* chr);
/** Something went wrong, give error messages and exit. */
void error(const char *format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
#if HAVE_CPUSET_T
int number_of_cpus(void);
int set_cpu_affinity(cpuset_t *set);
#endif
/* Add a cookie secret. If there are no secrets yet, the secret will become
* the active secret. Otherwise it will become the staging secret.
* Active secrets are used to both verify and create new DNS Cookies.
* Staging secrets are only used to verify DNS Cookies. */
void add_cookie_secret(struct nsd* nsd, uint8_t* secret);
/* Makes the staging cookie secret active and the active secret staging. */
void activate_cookie_secret(struct nsd* nsd);
/* Drop a cookie secret. Drops the staging secret. An active secret will not
* be dropped. */
void drop_cookie_secret(struct nsd* nsd);
#endif /* UTIL_H */