4 * helper function header file
6 * a Net::DNS like library for C
10 * See the file LICENSE for the license
17 #include <sys/types.h>
19 #include <ldns/common.h>
27 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
28 /* #define dprintf(X, Y) */
30 #define LDNS_VERSION "1.6.16"
31 #define LDNS_REVISION ((1<<16)|(6<<8)|(16))
34 * splint static inline workaround
40 # define INLINE static
42 # define INLINE static inline
47 * Memory management macros
49 #define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
51 #define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
53 #define LDNS_CALLOC(type, count) ((type *) calloc((count), sizeof(type)))
55 #define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
57 #define LDNS_XREALLOC(ptr, type, count) \
58 ((type *) realloc((ptr), (count) * sizeof(type)))
60 #define LDNS_FREE(ptr) \
61 do { free((ptr)); (ptr) = NULL; } while (0)
63 #define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
66 * Copy data allowing for unaligned accesses in network byte order
70 ldns_read_uint16(const void *src
)
72 #ifdef ALLOW_UNALIGNED_ACCESSES
73 return ntohs(*(uint16_t *) src
);
75 uint8_t *p
= (uint8_t *) src
;
76 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
81 ldns_read_uint32(const void *src
)
83 #ifdef ALLOW_UNALIGNED_ACCESSES
84 return ntohl(*(uint32_t *) src
);
86 uint8_t *p
= (uint8_t *) src
;
87 return ( ((uint32_t) p
[0] << 24)
88 | ((uint32_t) p
[1] << 16)
89 | ((uint32_t) p
[2] << 8)
95 * Copy data allowing for unaligned accesses in network byte order
99 ldns_write_uint16(void *dst
, uint16_t data
)
101 #ifdef ALLOW_UNALIGNED_ACCESSES
102 * (uint16_t *) dst
= htons(data
);
104 uint8_t *p
= (uint8_t *) dst
;
105 p
[0] = (uint8_t) ((data
>> 8) & 0xff);
106 p
[1] = (uint8_t) (data
& 0xff);
111 ldns_write_uint32(void *dst
, uint32_t data
)
113 #ifdef ALLOW_UNALIGNED_ACCESSES
114 * (uint32_t *) dst
= htonl(data
);
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);
126 ldns_write_uint64_as_uint48(void *dst
, uint64_t data
)
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);
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
144 struct ldns_schwartzian_compare_struct
{
145 void *original_object
;
146 void *transformed_object
;
149 /** A general purpose lookup table
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
156 struct ldns_struct_lookup_table
{
160 typedef struct ldns_struct_lookup_table ldns_lookup_table
;
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
168 ldns_lookup_table
*ldns_lookup_by_name(ldns_lookup_table table
[],
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
177 ldns_lookup_table
*ldns_lookup_by_id(ldns_lookup_table table
[], int id
);
180 * Returns the value of the specified bit
181 * The bits are counted from left to right, so bit #0 is the
183 * \param[in] bits array holding the bits
184 * \param[in] index to the wanted bit
187 int ldns_get_bit(uint8_t bits
[], size_t index
);
191 * Returns the value of the specified bit
192 * The bits are counted from right to left, so bit #0 is the
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
198 int ldns_get_bit_r(uint8_t bits
[], size_t index
);
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
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
210 void ldns_set_bit(uint8_t *byte
, int bit_nr
, bool value
);
213 * Returns the value of a to the power of b
218 ldns_power(long a
, long b
) {
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
238 int ldns_hexdigit_to_int(char ch
);
241 * Returns the char (hex) representation of the given int
242 * \param[in] ch the int to convert
243 * \return the converted hex char
245 char ldns_int_to_hexdigit(int ch
);
248 * Converts a hex string to binary data
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
257 ldns_hexstring_to_data(uint8_t *data
, const char *str
);
260 * Show the internal library version
261 * \return a string with the version in it
263 const char * ldns_version(void);
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
271 time_t ldns_mktime_from_utc(const struct tm
*tm
);
273 time_t mktime_from_utc(const struct tm
*tm
);
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.
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
289 struct tm
* ldns_serial_arithmitics_gmtime_r(int32_t time
, time_t now
, struct tm
*result
);
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.
296 * applications should call this if they need entropy data within ldns
297 * If openSSL is available, it is automatically seeded from /dev/urandom
300 * If you need more entropy, or have no openssl available, this function
301 * MUST be called at the start of the program
303 * If openssl *is* available, this function just adds more entropy
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
310 int ldns_init_random(FILE *fd
, unsigned int size
);
314 * \return random number.
317 uint16_t ldns_get_random(void);
320 * Encode data as BubbleBabble
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
326 char *ldns_bubblebabble(uint8_t *data
, size_t len
);
329 int ldns_b32_ntop(uint8_t const *src
, size_t srclength
,
330 char *target
, size_t targsize
);
331 int b32_ntop(uint8_t const *src
, size_t srclength
,
332 char *target
, size_t targsize
);
333 int ldns_b32_ntop_extended_hex(uint8_t const *src
, size_t srclength
,
334 char *target
, size_t targsize
);
335 int b32_ntop_extended_hex(uint8_t const *src
, size_t srclength
,
336 char *target
, size_t targsize
);
338 * calculates the size needed to store the result of b32_ntop
341 INLINE
size_t ldns_b32_ntop_calculate_size(size_t srcsize
)
343 size_t result
= ((((srcsize
/ 5) * 8) - 2) + 2);
346 #endif /* !B32_NTOP */
348 int ldns_b32_pton(char const *src
, size_t hashed_owner_str_len
, uint8_t *target
, size_t targsize
);
349 int b32_pton(char const *src
, size_t hashed_owner_str_len
, uint8_t *target
, size_t targsize
);
350 int ldns_b32_pton_extended_hex(char const *src
, size_t hashed_owner_str_len
, uint8_t *target
, size_t targsize
);
351 int b32_pton_extended_hex(char const *src
, size_t hashed_owner_str_len
, uint8_t *target
, size_t targsize
);
353 * calculates the size needed to store the result of b32_pton
356 INLINE
size_t ldns_b32_pton_calculate_size(size_t srcsize
)
358 size_t result
= ((((srcsize
) / 8) * 5));
361 #endif /* !B32_PTON */
363 INLINE
time_t ldns_time(time_t *t
) { return time(t
); }
369 #endif /* !_UTIL_H */