1df278fc2SJan Lentfer /* 2df278fc2SJan Lentfer * util.h 3df278fc2SJan Lentfer * 4df278fc2SJan Lentfer * helper function header file 5df278fc2SJan Lentfer * 6df278fc2SJan Lentfer * a Net::DNS like library for C 7df278fc2SJan Lentfer * 8df278fc2SJan Lentfer * (c) NLnet Labs, 2004 9df278fc2SJan Lentfer * 10df278fc2SJan Lentfer * See the file LICENSE for the license 11df278fc2SJan Lentfer */ 12df278fc2SJan Lentfer 13df278fc2SJan Lentfer #ifndef _UTIL_H 14df278fc2SJan Lentfer #define _UTIL_H 15df278fc2SJan Lentfer 16df278fc2SJan Lentfer #include <inttypes.h> 17fcd4e566SJan Lentfer #include <sys/types.h> 18fcd4e566SJan Lentfer #include <unistd.h> 19df278fc2SJan Lentfer #include <ldns/common.h> 20df278fc2SJan Lentfer #include <time.h> 21df278fc2SJan Lentfer #include <stdio.h> 22df278fc2SJan Lentfer 23fcd4e566SJan Lentfer #ifdef __cplusplus 24fcd4e566SJan Lentfer extern "C" { 25fcd4e566SJan Lentfer #endif 26fcd4e566SJan Lentfer 27df278fc2SJan Lentfer #define dprintf(X,Y) fprintf(stderr, (X), (Y)) 28df278fc2SJan Lentfer /* #define dprintf(X, Y) */ 29df278fc2SJan Lentfer 30*de9b0015SJan Lentfer #define LDNS_VERSION "1.6.11" 31*de9b0015SJan Lentfer #define LDNS_REVISION ((1<<16)|(6<<8)|(11)) 32df278fc2SJan Lentfer 33df278fc2SJan Lentfer /** 34df278fc2SJan Lentfer * splint static inline workaround 35df278fc2SJan Lentfer */ 36df278fc2SJan Lentfer #ifdef S_SPLINT_S 37df278fc2SJan Lentfer # define INLINE 38df278fc2SJan Lentfer #else 39df278fc2SJan Lentfer # ifdef SWIG 40df278fc2SJan Lentfer # define INLINE static 41df278fc2SJan Lentfer # else 42df278fc2SJan Lentfer # define INLINE static inline 43df278fc2SJan Lentfer # endif 44df278fc2SJan Lentfer #endif 45df278fc2SJan Lentfer 46df278fc2SJan Lentfer /** 47df278fc2SJan Lentfer * Memory management macros 48df278fc2SJan Lentfer */ 49df278fc2SJan Lentfer #define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1) 50df278fc2SJan Lentfer 51df278fc2SJan Lentfer #define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type))) 52df278fc2SJan Lentfer 53*de9b0015SJan Lentfer #define LDNS_CALLOC(type, count) ((type *) calloc((count), sizeof(type))) 54*de9b0015SJan Lentfer 55df278fc2SJan Lentfer #define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1) 56df278fc2SJan Lentfer 57df278fc2SJan Lentfer #define LDNS_XREALLOC(ptr, type, count) \ 58df278fc2SJan Lentfer ((type *) realloc((ptr), (count) * sizeof(type))) 59df278fc2SJan Lentfer 60df278fc2SJan Lentfer #define LDNS_FREE(ptr) \ 61df278fc2SJan Lentfer do { free((ptr)); (ptr) = NULL; } while (0) 62df278fc2SJan Lentfer 63df278fc2SJan Lentfer #define LDNS_DEP printf("DEPRECATED FUNCTION!\n"); 64df278fc2SJan Lentfer 65df278fc2SJan Lentfer /* 66df278fc2SJan Lentfer * Copy data allowing for unaligned accesses in network byte order 67df278fc2SJan Lentfer * (big endian). 68df278fc2SJan Lentfer */ 69df278fc2SJan Lentfer INLINE uint16_t 70df278fc2SJan Lentfer ldns_read_uint16(const void *src) 71df278fc2SJan Lentfer { 72df278fc2SJan Lentfer #ifdef ALLOW_UNALIGNED_ACCESSES 73df278fc2SJan Lentfer return ntohs(*(uint16_t *) src); 74df278fc2SJan Lentfer #else 75df278fc2SJan Lentfer uint8_t *p = (uint8_t *) src; 76df278fc2SJan Lentfer return ((uint16_t) p[0] << 8) | (uint16_t) p[1]; 77df278fc2SJan Lentfer #endif 78df278fc2SJan Lentfer } 79df278fc2SJan Lentfer 80df278fc2SJan Lentfer INLINE uint32_t 81df278fc2SJan Lentfer ldns_read_uint32(const void *src) 82df278fc2SJan Lentfer { 83df278fc2SJan Lentfer #ifdef ALLOW_UNALIGNED_ACCESSES 84df278fc2SJan Lentfer return ntohl(*(uint32_t *) src); 85df278fc2SJan Lentfer #else 86df278fc2SJan Lentfer uint8_t *p = (uint8_t *) src; 87df278fc2SJan Lentfer return ( ((uint32_t) p[0] << 24) 88df278fc2SJan Lentfer | ((uint32_t) p[1] << 16) 89df278fc2SJan Lentfer | ((uint32_t) p[2] << 8) 90df278fc2SJan Lentfer | (uint32_t) p[3]); 91df278fc2SJan Lentfer #endif 92df278fc2SJan Lentfer } 93df278fc2SJan Lentfer 94df278fc2SJan Lentfer /* 95df278fc2SJan Lentfer * Copy data allowing for unaligned accesses in network byte order 96df278fc2SJan Lentfer * (big endian). 97df278fc2SJan Lentfer */ 98df278fc2SJan Lentfer INLINE void 99df278fc2SJan Lentfer ldns_write_uint16(void *dst, uint16_t data) 100df278fc2SJan Lentfer { 101df278fc2SJan Lentfer #ifdef ALLOW_UNALIGNED_ACCESSES 102df278fc2SJan Lentfer * (uint16_t *) dst = htons(data); 103df278fc2SJan Lentfer #else 104df278fc2SJan Lentfer uint8_t *p = (uint8_t *) dst; 105df278fc2SJan Lentfer p[0] = (uint8_t) ((data >> 8) & 0xff); 106df278fc2SJan Lentfer p[1] = (uint8_t) (data & 0xff); 107df278fc2SJan Lentfer #endif 108df278fc2SJan Lentfer } 109df278fc2SJan Lentfer 110df278fc2SJan Lentfer INLINE void 111df278fc2SJan Lentfer ldns_write_uint32(void *dst, uint32_t data) 112df278fc2SJan Lentfer { 113df278fc2SJan Lentfer #ifdef ALLOW_UNALIGNED_ACCESSES 114df278fc2SJan Lentfer * (uint32_t *) dst = htonl(data); 115df278fc2SJan Lentfer #else 116df278fc2SJan Lentfer uint8_t *p = (uint8_t *) dst; 117df278fc2SJan Lentfer p[0] = (uint8_t) ((data >> 24) & 0xff); 118df278fc2SJan Lentfer p[1] = (uint8_t) ((data >> 16) & 0xff); 119df278fc2SJan Lentfer p[2] = (uint8_t) ((data >> 8) & 0xff); 120df278fc2SJan Lentfer p[3] = (uint8_t) (data & 0xff); 121df278fc2SJan Lentfer #endif 122df278fc2SJan Lentfer } 123df278fc2SJan Lentfer 124df278fc2SJan Lentfer /* warning. */ 125df278fc2SJan Lentfer INLINE void 126df278fc2SJan Lentfer ldns_write_uint64_as_uint48(void *dst, uint64_t data) 127df278fc2SJan Lentfer { 128df278fc2SJan Lentfer uint8_t *p = (uint8_t *) dst; 129df278fc2SJan Lentfer p[0] = (uint8_t) ((data >> 40) & 0xff); 130df278fc2SJan Lentfer p[1] = (uint8_t) ((data >> 32) & 0xff); 131df278fc2SJan Lentfer p[2] = (uint8_t) ((data >> 24) & 0xff); 132df278fc2SJan Lentfer p[3] = (uint8_t) ((data >> 16) & 0xff); 133df278fc2SJan Lentfer p[4] = (uint8_t) ((data >> 8) & 0xff); 134df278fc2SJan Lentfer p[5] = (uint8_t) (data & 0xff); 135df278fc2SJan Lentfer } 136df278fc2SJan Lentfer 137df278fc2SJan Lentfer 138df278fc2SJan Lentfer /** 139df278fc2SJan Lentfer * Structure to do a Schwartzian-like transformation, for instance when 140df278fc2SJan Lentfer * sorting. If you need a transformation on the objects that are sorted, 141df278fc2SJan Lentfer * you can sue this to store the transformed values, so you do not 142df278fc2SJan Lentfer * need to do the transformation again for each comparison 143df278fc2SJan Lentfer */ 144df278fc2SJan Lentfer struct ldns_schwartzian_compare_struct { 145df278fc2SJan Lentfer void *original_object; 146df278fc2SJan Lentfer void *transformed_object; 147df278fc2SJan Lentfer }; 148df278fc2SJan Lentfer 149df278fc2SJan Lentfer /** A general purpose lookup table 150df278fc2SJan Lentfer * 151df278fc2SJan Lentfer * Lookup tables are arrays of (id, name) pairs, 152df278fc2SJan Lentfer * So you can for instance lookup the RCODE 3, which is "NXDOMAIN", 153df278fc2SJan Lentfer * and vice versa. The lookup tables themselves are defined wherever needed, 154df278fc2SJan Lentfer * for instance in \ref host2str.c 155df278fc2SJan Lentfer */ 156df278fc2SJan Lentfer struct ldns_struct_lookup_table { 157df278fc2SJan Lentfer int id; 158df278fc2SJan Lentfer const char *name; 159df278fc2SJan Lentfer }; 160df278fc2SJan Lentfer typedef struct ldns_struct_lookup_table ldns_lookup_table; 161df278fc2SJan Lentfer 162df278fc2SJan Lentfer /** 163df278fc2SJan Lentfer * Looks up the table entry by name, returns NULL if not found. 164df278fc2SJan Lentfer * \param[in] table the lookup table to search in 165df278fc2SJan Lentfer * \param[in] name what to search for 166df278fc2SJan Lentfer * \return the item found 167df278fc2SJan Lentfer */ 168df278fc2SJan Lentfer ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[], 169df278fc2SJan Lentfer const char *name); 170df278fc2SJan Lentfer 171df278fc2SJan Lentfer /** 172df278fc2SJan Lentfer * Looks up the table entry by id, returns NULL if not found. 173df278fc2SJan Lentfer * \param[in] table the lookup table to search in 174df278fc2SJan Lentfer * \param[in] id what to search for 175df278fc2SJan Lentfer * \return the item found 176df278fc2SJan Lentfer */ 177df278fc2SJan Lentfer ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id); 178df278fc2SJan Lentfer 179df278fc2SJan Lentfer /** 180df278fc2SJan Lentfer * Returns the value of the specified bit 181df278fc2SJan Lentfer * The bits are counted from left to right, so bit #0 is the 182df278fc2SJan Lentfer * left most bit. 183df278fc2SJan Lentfer * \param[in] bits array holding the bits 184df278fc2SJan Lentfer * \param[in] index to the wanted bit 185df278fc2SJan Lentfer * \return 186df278fc2SJan Lentfer */ 187df278fc2SJan Lentfer int ldns_get_bit(uint8_t bits[], size_t index); 188df278fc2SJan Lentfer 189df278fc2SJan Lentfer 190df278fc2SJan Lentfer /** 191df278fc2SJan Lentfer * Returns the value of the specified bit 192df278fc2SJan Lentfer * The bits are counted from right to left, so bit #0 is the 193df278fc2SJan Lentfer * right most bit. 194df278fc2SJan Lentfer * \param[in] bits array holding the bits 195df278fc2SJan Lentfer * \param[in] index to the wanted bit 196df278fc2SJan Lentfer * \return 1 or 0 depending no the bit state 197df278fc2SJan Lentfer */ 198df278fc2SJan Lentfer int ldns_get_bit_r(uint8_t bits[], size_t index); 199df278fc2SJan Lentfer 200df278fc2SJan Lentfer /** 201df278fc2SJan Lentfer * sets the specified bit in the specified byte to 202df278fc2SJan Lentfer * 1 if value is true, 0 if false 203df278fc2SJan Lentfer * The bits are counted from right to left, so bit #0 is the 204df278fc2SJan Lentfer * right most bit. 205df278fc2SJan Lentfer * \param[in] byte the bit to set the bit in 206df278fc2SJan Lentfer * \param[in] bit_nr the bit to set (0 <= n <= 7) 207df278fc2SJan Lentfer * \param[in] value whether to set the bit to 1 or 0 208df278fc2SJan Lentfer * \return 1 or 0 depending no the bit state 209df278fc2SJan Lentfer */ 210df278fc2SJan Lentfer void ldns_set_bit(uint8_t *byte, int bit_nr, bool value); 211df278fc2SJan Lentfer 212df278fc2SJan Lentfer /** 213df278fc2SJan Lentfer * Returns the value of a to the power of b 214df278fc2SJan Lentfer * (or 1 of b < 1) 215df278fc2SJan Lentfer */ 216df278fc2SJan Lentfer /*@unused@*/ 217df278fc2SJan Lentfer INLINE long 218df278fc2SJan Lentfer ldns_power(long a, long b) { 219df278fc2SJan Lentfer long result = 1; 220df278fc2SJan Lentfer while (b > 0) { 221df278fc2SJan Lentfer if (b & 1) { 222df278fc2SJan Lentfer result *= a; 223df278fc2SJan Lentfer if (b == 1) { 224df278fc2SJan Lentfer return result; 225df278fc2SJan Lentfer } 226df278fc2SJan Lentfer } 227df278fc2SJan Lentfer a *= a; 228df278fc2SJan Lentfer b /= 2; 229df278fc2SJan Lentfer } 230df278fc2SJan Lentfer return result; 231df278fc2SJan Lentfer } 232df278fc2SJan Lentfer 233df278fc2SJan Lentfer /** 234df278fc2SJan Lentfer * Returns the int value of the given (hex) digit 235df278fc2SJan Lentfer * \param[in] ch the hex char to convert 236df278fc2SJan Lentfer * \return the converted decimal value 237df278fc2SJan Lentfer */ 238df278fc2SJan Lentfer int ldns_hexdigit_to_int(char ch); 239df278fc2SJan Lentfer 240df278fc2SJan Lentfer /** 241df278fc2SJan Lentfer * Returns the char (hex) representation of the given int 242df278fc2SJan Lentfer * \param[in] ch the int to convert 243df278fc2SJan Lentfer * \return the converted hex char 244df278fc2SJan Lentfer */ 245df278fc2SJan Lentfer char ldns_int_to_hexdigit(int ch); 246df278fc2SJan Lentfer 247df278fc2SJan Lentfer /** 248df278fc2SJan Lentfer * Converts a hex string to binary data 249df278fc2SJan Lentfer * 250df278fc2SJan Lentfer * \param[out] data The binary result is placed here. 251df278fc2SJan Lentfer * At least strlen(str)/2 bytes should be allocated 252df278fc2SJan Lentfer * \param[in] str The hex string to convert. 253df278fc2SJan Lentfer * This string should not contain spaces 254df278fc2SJan Lentfer * \return The number of bytes of converted data, or -1 if one of the arguments * is NULL, or -2 if the string length is not an even number 255df278fc2SJan Lentfer */ 256df278fc2SJan Lentfer int 257df278fc2SJan Lentfer ldns_hexstring_to_data(uint8_t *data, const char *str); 258df278fc2SJan Lentfer 259df278fc2SJan Lentfer /** 260df278fc2SJan Lentfer * Show the internal library version 261df278fc2SJan Lentfer * \return a string with the version in it 262df278fc2SJan Lentfer */ 263df278fc2SJan Lentfer const char * ldns_version(void); 264df278fc2SJan Lentfer 265df278fc2SJan Lentfer /** 266df278fc2SJan Lentfer * Convert TM to seconds since epoch (midnight, January 1st, 1970). 267df278fc2SJan Lentfer * Like timegm(3), which is not always available. 268df278fc2SJan Lentfer * \param[in] tm a struct tm* with the date 269df278fc2SJan Lentfer * \return the seconds since epoch 270df278fc2SJan Lentfer */ 271df278fc2SJan Lentfer time_t mktime_from_utc(const struct tm *tm); 272df278fc2SJan Lentfer 273df278fc2SJan Lentfer /** 274*de9b0015SJan Lentfer * The function interprets time as the number of seconds since epoch 275*de9b0015SJan Lentfer * with respect to now using serial arithmitics (rfc1982). 276*de9b0015SJan Lentfer * That number of seconds is then converted to broken-out time information. 277*de9b0015SJan Lentfer * This is especially usefull when converting the inception and expiration 278*de9b0015SJan Lentfer * fields of RRSIG records. 279*de9b0015SJan Lentfer * 280*de9b0015SJan Lentfer * \param[in] time number of seconds since epoch (midnight, January 1st, 1970) 281*de9b0015SJan Lentfer * to be intepreted as a serial arithmitics number relative to now. 282*de9b0015SJan Lentfer * \param[in] now number of seconds since epoch (midnight, January 1st, 1970) 283*de9b0015SJan Lentfer * to which the time value is compared to determine the final value. 284*de9b0015SJan Lentfer * \param[out] result the struct with the broken-out time information 285*de9b0015SJan Lentfer * \return result on success or NULL on error 286*de9b0015SJan Lentfer */ 287*de9b0015SJan Lentfer struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result); 288*de9b0015SJan Lentfer 289*de9b0015SJan Lentfer /** 290df278fc2SJan Lentfer * Seed the random function. 291df278fc2SJan Lentfer * If the file descriptor is specified, the random generator is seeded with 292df278fc2SJan Lentfer * data from that file. If not, /dev/urandom is used. 293df278fc2SJan Lentfer * 294df278fc2SJan Lentfer * applications should call this if they need entropy data within ldns 295df278fc2SJan Lentfer * If openSSL is available, it is automatically seeded from /dev/urandom 296df278fc2SJan Lentfer * or /dev/random. 297df278fc2SJan Lentfer * 298df278fc2SJan Lentfer * If you need more entropy, or have no openssl available, this function 299df278fc2SJan Lentfer * MUST be called at the start of the program 300df278fc2SJan Lentfer * 301df278fc2SJan Lentfer * If openssl *is* available, this function just adds more entropy 302df278fc2SJan Lentfer * 303df278fc2SJan Lentfer * \param[in] fd a file providing entropy data for the seed 304df278fc2SJan Lentfer * \param[in] size the number of bytes to use as entropy data. If this is 0, 305df278fc2SJan Lentfer * only the minimal amount is taken (usually 4 bytes) 306df278fc2SJan Lentfer * \return 0 if seeding succeeds, 1 if it fails 307df278fc2SJan Lentfer */ 308df278fc2SJan Lentfer int ldns_init_random(FILE *fd, unsigned int size); 309df278fc2SJan Lentfer 310fcd4e566SJan Lentfer /** 311fcd4e566SJan Lentfer * Get random number. 312fcd4e566SJan Lentfer * \return random number. 313fcd4e566SJan Lentfer * 314fcd4e566SJan Lentfer */ 315fcd4e566SJan Lentfer uint16_t ldns_get_random(void); 316df278fc2SJan Lentfer 317df278fc2SJan Lentfer /** 318df278fc2SJan Lentfer * Encode data as BubbleBabble 319df278fc2SJan Lentfer * 320df278fc2SJan Lentfer * \param[in] data a pointer to data to be encoded 321df278fc2SJan Lentfer * \param[in] len size the number of bytes of data 322df278fc2SJan Lentfer * \return a string of BubbleBabble 323df278fc2SJan Lentfer */ 324df278fc2SJan Lentfer char *ldns_bubblebabble(uint8_t *data, size_t len); 325df278fc2SJan Lentfer 326df278fc2SJan Lentfer #ifndef B32_NTOP 327df278fc2SJan Lentfer int ldns_b32_ntop(uint8_t const *src, size_t srclength, 328df278fc2SJan Lentfer char *target, size_t targsize); 329df278fc2SJan Lentfer int b32_ntop(uint8_t const *src, size_t srclength, 330df278fc2SJan Lentfer char *target, size_t targsize); 331df278fc2SJan Lentfer int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, 332df278fc2SJan Lentfer char *target, size_t targsize); 333df278fc2SJan Lentfer int b32_ntop_extended_hex(uint8_t const *src, size_t srclength, 334df278fc2SJan Lentfer char *target, size_t targsize); 335df278fc2SJan Lentfer /** 336df278fc2SJan Lentfer * calculates the size needed to store the result of b32_ntop 337df278fc2SJan Lentfer */ 338df278fc2SJan Lentfer /*@unused@*/ 339df278fc2SJan Lentfer INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize) 340df278fc2SJan Lentfer { 341df278fc2SJan Lentfer size_t result = ((((srcsize / 5) * 8) - 2) + 2); 342df278fc2SJan Lentfer return result; 343df278fc2SJan Lentfer } 344df278fc2SJan Lentfer #endif /* !B32_NTOP */ 345df278fc2SJan Lentfer #ifndef B32_PTON 346df278fc2SJan Lentfer int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); 347df278fc2SJan Lentfer int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); 348df278fc2SJan Lentfer int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); 349df278fc2SJan Lentfer int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); 350df278fc2SJan Lentfer /** 351df278fc2SJan Lentfer * calculates the size needed to store the result of b32_pton 352df278fc2SJan Lentfer */ 353df278fc2SJan Lentfer /*@unused@*/ 354df278fc2SJan Lentfer INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize) 355df278fc2SJan Lentfer { 356df278fc2SJan Lentfer size_t result = ((((srcsize) / 8) * 5)); 357df278fc2SJan Lentfer return result; 358df278fc2SJan Lentfer } 359df278fc2SJan Lentfer #endif /* !B32_PTON */ 360df278fc2SJan Lentfer 361fcd4e566SJan Lentfer #ifdef __cplusplus 362fcd4e566SJan Lentfer } 363fcd4e566SJan Lentfer #endif 364fcd4e566SJan Lentfer 365df278fc2SJan Lentfer #endif /* !_UTIL_H */ 366