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.9"
31 #define LDNS_REVISION ((1<<16)|(6<<8)|(9))
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_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
55 #define LDNS_XREALLOC(ptr, type, count) \
56 ((type *) realloc((ptr), (count) * sizeof(type)))
58 #define LDNS_FREE(ptr) \
59 do { free((ptr)); (ptr) = NULL; } while (0)
61 #define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
64 * Copy data allowing for unaligned accesses in network byte order
68 ldns_read_uint16(const void *src)
70 #ifdef ALLOW_UNALIGNED_ACCESSES
71 return ntohs(*(uint16_t *) src);
73 uint8_t *p = (uint8_t *) src;
74 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
79 ldns_read_uint32(const void *src)
81 #ifdef ALLOW_UNALIGNED_ACCESSES
82 return ntohl(*(uint32_t *) src);
84 uint8_t *p = (uint8_t *) src;
85 return ( ((uint32_t) p[0] << 24)
86 | ((uint32_t) p[1] << 16)
87 | ((uint32_t) p[2] << 8)
93 * Copy data allowing for unaligned accesses in network byte order
97 ldns_write_uint16(void *dst, uint16_t data)
99 #ifdef ALLOW_UNALIGNED_ACCESSES
100 * (uint16_t *) dst = htons(data);
102 uint8_t *p = (uint8_t *) dst;
103 p[0] = (uint8_t) ((data >> 8) & 0xff);
104 p[1] = (uint8_t) (data & 0xff);
109 ldns_write_uint32(void *dst, uint32_t data)
111 #ifdef ALLOW_UNALIGNED_ACCESSES
112 * (uint32_t *) dst = htonl(data);
114 uint8_t *p = (uint8_t *) dst;
115 p[0] = (uint8_t) ((data >> 24) & 0xff);
116 p[1] = (uint8_t) ((data >> 16) & 0xff);
117 p[2] = (uint8_t) ((data >> 8) & 0xff);
118 p[3] = (uint8_t) (data & 0xff);
124 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
126 uint8_t *p = (uint8_t *) dst;
127 p[0] = (uint8_t) ((data >> 40) & 0xff);
128 p[1] = (uint8_t) ((data >> 32) & 0xff);
129 p[2] = (uint8_t) ((data >> 24) & 0xff);
130 p[3] = (uint8_t) ((data >> 16) & 0xff);
131 p[4] = (uint8_t) ((data >> 8) & 0xff);
132 p[5] = (uint8_t) (data & 0xff);
137 * Structure to do a Schwartzian-like transformation, for instance when
138 * sorting. If you need a transformation on the objects that are sorted,
139 * you can sue this to store the transformed values, so you do not
140 * need to do the transformation again for each comparison
142 struct ldns_schwartzian_compare_struct {
143 void *original_object;
144 void *transformed_object;
147 /** A general purpose lookup table
149 * Lookup tables are arrays of (id, name) pairs,
150 * So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
151 * and vice versa. The lookup tables themselves are defined wherever needed,
152 * for instance in \ref host2str.c
154 struct ldns_struct_lookup_table {
158 typedef struct ldns_struct_lookup_table ldns_lookup_table;
161 * Looks up the table entry by name, returns NULL if not found.
162 * \param[in] table the lookup table to search in
163 * \param[in] name what to search for
164 * \return the item found
166 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
170 * Looks up the table entry by id, returns NULL if not found.
171 * \param[in] table the lookup table to search in
172 * \param[in] id what to search for
173 * \return the item found
175 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
178 * Returns the value of the specified bit
179 * The bits are counted from left to right, so bit #0 is the
181 * \param[in] bits array holding the bits
182 * \param[in] index to the wanted bit
185 int ldns_get_bit(uint8_t bits[], size_t index);
189 * Returns the value of the specified bit
190 * The bits are counted from right to left, so bit #0 is the
192 * \param[in] bits array holding the bits
193 * \param[in] index to the wanted bit
194 * \return 1 or 0 depending no the bit state
196 int ldns_get_bit_r(uint8_t bits[], size_t index);
199 * sets the specified bit in the specified byte to
200 * 1 if value is true, 0 if false
201 * The bits are counted from right to left, so bit #0 is the
203 * \param[in] byte the bit to set the bit in
204 * \param[in] bit_nr the bit to set (0 <= n <= 7)
205 * \param[in] value whether to set the bit to 1 or 0
206 * \return 1 or 0 depending no the bit state
208 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
211 * Returns the value of a to the power of b
216 ldns_power(long a, long b) {
232 * Returns the int value of the given (hex) digit
233 * \param[in] ch the hex char to convert
234 * \return the converted decimal value
236 int ldns_hexdigit_to_int(char ch);
239 * Returns the char (hex) representation of the given int
240 * \param[in] ch the int to convert
241 * \return the converted hex char
243 char ldns_int_to_hexdigit(int ch);
246 * Converts a hex string to binary data
248 * \param[out] data The binary result is placed here.
249 * At least strlen(str)/2 bytes should be allocated
250 * \param[in] str The hex string to convert.
251 * This string should not contain spaces
252 * \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 ldns_hexstring_to_data(uint8_t *data, const char *str);
258 * Show the internal library version
259 * \return a string with the version in it
261 const char * ldns_version(void);
264 * Convert TM to seconds since epoch (midnight, January 1st, 1970).
265 * Like timegm(3), which is not always available.
266 * \param[in] tm a struct tm* with the date
267 * \return the seconds since epoch
269 time_t mktime_from_utc(const struct tm *tm);
272 * Seed the random function.
273 * If the file descriptor is specified, the random generator is seeded with
274 * data from that file. If not, /dev/urandom is used.
276 * applications should call this if they need entropy data within ldns
277 * If openSSL is available, it is automatically seeded from /dev/urandom
280 * If you need more entropy, or have no openssl available, this function
281 * MUST be called at the start of the program
283 * If openssl *is* available, this function just adds more entropy
285 * \param[in] fd a file providing entropy data for the seed
286 * \param[in] size the number of bytes to use as entropy data. If this is 0,
287 * only the minimal amount is taken (usually 4 bytes)
288 * \return 0 if seeding succeeds, 1 if it fails
290 int ldns_init_random(FILE *fd, unsigned int size);
294 * \return random number.
297 uint16_t ldns_get_random(void);
300 * Encode data as BubbleBabble
302 * \param[in] data a pointer to data to be encoded
303 * \param[in] len size the number of bytes of data
304 * \return a string of BubbleBabble
306 char *ldns_bubblebabble(uint8_t *data, size_t len);
309 int ldns_b32_ntop(uint8_t const *src, size_t srclength,
310 char *target, size_t targsize);
311 int b32_ntop(uint8_t const *src, size_t srclength,
312 char *target, size_t targsize);
313 int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
314 char *target, size_t targsize);
315 int b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
316 char *target, size_t targsize);
318 * calculates the size needed to store the result of b32_ntop
321 INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize)
323 size_t result = ((((srcsize / 5) * 8) - 2) + 2);
326 #endif /* !B32_NTOP */
328 int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
329 int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
330 int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
331 int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
333 * calculates the size needed to store the result of b32_pton
336 INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize)
338 size_t result = ((((srcsize) / 8) * 5));
341 #endif /* !B32_PTON */
347 #endif /* !_UTIL_H */