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