summaryrefslogtreecommitdiffstats
blob: eb7658e5e3c7c5d4687486e6c56e22c78244e583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * util.h
 *  
 * helper function header file
 * 
 * a Net::DNS like library for C
 * 
 * (c) NLnet Labs, 2004
 * 
 * See the file LICENSE for the license
 */

#ifndef _UTIL_H
#define _UTIL_H

#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <ldns/common.h>
#include <time.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define dprintf(X,Y) fprintf(stderr, (X), (Y))
/* #define	dprintf(X, Y)  */

#define LDNS_VERSION "1.6.12"
#define LDNS_REVISION ((1<<16)|(6<<8)|(12))

/**
 * splint static inline workaround
 */
#ifdef S_SPLINT_S
#  define INLINE 
#else
#  ifdef SWIG
#    define INLINE static
#  else
#    define INLINE static inline
#  endif
#endif

/**
 * Memory management macros
 */
#define LDNS_MALLOC(type)		LDNS_XMALLOC(type, 1)

#define LDNS_XMALLOC(type, count)	((type *) malloc((count) * sizeof(type)))

#define LDNS_CALLOC(type, count)	((type *) calloc((count), sizeof(type)))

#define LDNS_REALLOC(ptr, type)		LDNS_XREALLOC((ptr), type, 1)

#define LDNS_XREALLOC(ptr, type, count)				\
	((type *) realloc((ptr), (count) * sizeof(type)))

#define LDNS_FREE(ptr) \
	do { free((ptr)); (ptr) = NULL; } while (0)

#define LDNS_DEP     printf("DEPRECATED FUNCTION!\n");

/*
 * Copy data allowing for unaligned accesses in network byte order
 * (big endian).
 */
INLINE uint16_t
ldns_read_uint16(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
	return ntohs(*(uint16_t *) src);
#else
	uint8_t *p = (uint8_t *) src;
	return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
#endif
}

INLINE uint32_t
ldns_read_uint32(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
	return ntohl(*(uint32_t *) src);
#else
	uint8_t *p = (uint8_t *) src;
	return (  ((uint32_t) p[0] << 24)
		| ((uint32_t) p[1] << 16)
		| ((uint32_t) p[2] << 8)
		|  (uint32_t) p[3]);
#endif
}

/*
 * Copy data allowing for unaligned accesses in network byte order
 * (big endian).
 */
INLINE void
ldns_write_uint16(void *dst, uint16_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
	* (uint16_t *) dst = htons(data);
#else
	uint8_t *p = (uint8_t *) dst;
	p[0] = (uint8_t) ((data >> 8) & 0xff);
	p[1] = (uint8_t) (data & 0xff);
#endif
}

INLINE void
ldns_write_uint32(void *dst, uint32_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
	* (uint32_t *) dst = htonl(data);
#else
	uint8_t *p = (uint8_t *) dst;
	p[0] = (uint8_t) ((data >> 24) & 0xff);
	p[1] = (uint8_t) ((data >> 16) & 0xff);
	p[2] = (uint8_t) ((data >> 8) & 0xff);
	p[3] = (uint8_t) (data & 0xff);
#endif
}

/* warning. */
INLINE void
ldns_write_uint64_as_uint48(void *dst, uint64_t data)
{
	uint8_t *p = (uint8_t *) dst;
	p[0] = (uint8_t) ((data >> 40) & 0xff);
	p[1] = (uint8_t) ((data >> 32) & 0xff);
	p[2] = (uint8_t) ((data >> 24) & 0xff);
	p[3] = (uint8_t) ((data >> 16) & 0xff);
	p[4] = (uint8_t) ((data >> 8) & 0xff);
	p[5] = (uint8_t) (data & 0xff);
}


/**
 * Structure to do a Schwartzian-like transformation, for instance when
 * sorting. If you need a transformation on the objects that are sorted,
 * you can sue this to store the transformed values, so you do not
 * need to do the transformation again for each comparison
 */
struct ldns_schwartzian_compare_struct {
	void *original_object;
	void *transformed_object;
};

/** A general purpose lookup table
 *  
 *  Lookup tables are arrays of (id, name) pairs,
 *  So you can for instance lookup the RCODE 3, which is "NXDOMAIN",
 *  and vice versa. The lookup tables themselves are defined wherever needed,
 *  for instance in \ref host2str.c
 */
struct ldns_struct_lookup_table {
        int id;
        const char *name;
};
typedef struct ldns_struct_lookup_table ldns_lookup_table;
  
/**
 * Looks up the table entry by name, returns NULL if not found.
 * \param[in] table the lookup table to search in
 * \param[in] name what to search for
 * \return the item found
 */
ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
                                       const char *name);

/**
 * Looks up the table entry by id, returns NULL if not found.
 * \param[in] table the lookup table to search in
 * \param[in] id what to search for
 * \return the item found
 */
ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);

/**
 * Returns the value of the specified bit
 * The bits are counted from left to right, so bit #0 is the
 * left most bit.
 * \param[in] bits array holding the bits
 * \param[in] index to the wanted bit
 * \return 
 */
int ldns_get_bit(uint8_t bits[], size_t index);


/**
 * Returns the value of the specified bit
 * The bits are counted from right to left, so bit #0 is the
 * right most bit.
 * \param[in] bits array holding the bits
 * \param[in] index to the wanted bit
 * \return 1 or 0 depending no the bit state
 */
int ldns_get_bit_r(uint8_t bits[], size_t index);

/**
 * sets the specified bit in the specified byte to
 * 1 if value is true, 0 if false
 * The bits are counted from right to left, so bit #0 is the
 * right most bit.
 * \param[in] byte the bit to set the bit in
 * \param[in] bit_nr the bit to set (0 <= n <= 7)
 * \param[in] value whether to set the bit to 1 or 0
 * \return 1 or 0 depending no the bit state
 */
void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);

/**
 * Returns the value of a to the power of b
 * (or 1 of b < 1)
 */
/*@unused@*/
INLINE long
ldns_power(long a, long b) {
	long result = 1;
	while (b > 0) {
		if (b & 1) {
			result *= a;
			if (b == 1) {
				return result;
			}
		}
		a *= a;
		b /= 2;
	}
	return result;
}

/**
 * Returns the int value of the given (hex) digit
 * \param[in] ch the hex char to convert
 * \return the converted decimal value
 */
int ldns_hexdigit_to_int(char ch);

/**
 * Returns the char (hex) representation of the given int
 * \param[in] ch the int to convert
 * \return the converted hex char
 */
char ldns_int_to_hexdigit(int ch);

/**
 * Converts a hex string to binary data
 *
 * \param[out] data The binary result is placed here.
 * At least strlen(str)/2 bytes should be allocated
 * \param[in] str The hex string to convert.
 * This string should not contain spaces
 * \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
 */
int
ldns_hexstring_to_data(uint8_t *data, const char *str);

/**
 * Show the internal library version
 * \return a string with the version in it
 */
const char * ldns_version(void);

/**
 * Convert TM to seconds since epoch (midnight, January 1st, 1970).
 * Like timegm(3), which is not always available.
 * \param[in] tm a struct tm* with the date
 * \return the seconds since epoch
 */
time_t mktime_from_utc(const struct tm *tm);

/**
 * The function interprets time as the number of seconds since epoch
 * with respect to now using serial arithmitics (rfc1982).
 * That number of seconds is then converted to broken-out time information.
 * This is especially usefull when converting the inception and expiration
 * fields of RRSIG records.
 *
 * \param[in] time number of seconds since epoch (midnight, January 1st, 1970)
 *            to be intepreted as a serial arithmitics number relative to now.
 * \param[in] now number of seconds since epoch (midnight, January 1st, 1970)
 *            to which the time value is compared to determine the final value.
 * \param[out] result the struct with the broken-out time information
 * \return result on success or NULL on error
 */
struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
 
/**
 * Seed the random function.
 * If the file descriptor is specified, the random generator is seeded with
 * data from that file. If not, /dev/urandom is used.
 *
 * applications should call this if they need entropy data within ldns
 * If openSSL is available, it is automatically seeded from /dev/urandom
 * or /dev/random.
 *
 * If you need more entropy, or have no openssl available, this function
 * MUST be called at the start of the program
 *
 * If openssl *is* available, this function just adds more entropy
 *
 * \param[in] fd a file providing entropy data for the seed
 * \param[in] size the number of bytes to use as entropy data. If this is 0,
 *            only the minimal amount is taken (usually 4 bytes)
 * \return 0 if seeding succeeds, 1 if it fails
 */
int ldns_init_random(FILE *fd, unsigned int size);

/**
 * Get random number.
 * \return random number.
 *
 */
uint16_t ldns_get_random(void);

/**
 * Encode data as BubbleBabble
 *
 * \param[in] data a pointer to data to be encoded
 * \param[in] len size the number of bytes of data
 * \return a string of BubbleBabble
 */
char *ldns_bubblebabble(uint8_t *data, size_t len);

#ifndef B32_NTOP
int ldns_b32_ntop(uint8_t const *src, size_t srclength,
	     char *target, size_t targsize);
int b32_ntop(uint8_t const *src, size_t srclength,
	     char *target, size_t targsize);
int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
	     char *target, size_t targsize);
int b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
	     char *target, size_t targsize);
/**
 * calculates the size needed to store the result of b32_ntop
 */
/*@unused@*/
INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize)
{
	size_t result = ((((srcsize / 5) * 8) - 2) + 2);
	return result;
}
#endif /* !B32_NTOP */
#ifndef B32_PTON
int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
/**
 * calculates the size needed to store the result of b32_pton
 */
/*@unused@*/
INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize)
{
	size_t result = ((((srcsize) / 8) * 5));
	return result;
}
#endif /* !B32_PTON */

INLINE time_t ldns_time(time_t *t) { return time(t); }

#ifdef __cplusplus
}
#endif

#endif /* !_UTIL_H */