4 * some general memory functions
6 * a Net::DNS like library for C
8 * (c) NLnet Labs, 2004-2006
10 * See the file LICENSE for the license
13 #include <ldns/config.h>
15 #include <ldns/rdata.h>
17 #include <ldns/util.h>
25 #include <openssl/rand.h>
29 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
31 while (table->name != NULL) {
32 if (strcasecmp(name, table->name) == 0)
40 ldns_lookup_by_id(ldns_lookup_table *table, int id)
42 while (table->name != NULL) {
51 ldns_get_bit(uint8_t bits[], size_t index)
54 * The bits are counted from left to right, so bit #0 is the
57 return (int) (bits[index / 8] & (1 << (7 - index % 8)));
61 ldns_get_bit_r(uint8_t bits[], size_t index)
64 * The bits are counted from right to left, so bit #0 is the
67 return (int) bits[index / 8] & (1 << (index % 8));
71 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
74 * The bits are counted from right to left, so bit #0 is the
77 if (bit_nr >= 0 && bit_nr < 8) {
79 *byte = *byte | (0x01 << bit_nr);
81 *byte = *byte & ~(0x01 << bit_nr);
87 ldns_hexdigit_to_int(char ch)
100 case 'a': case 'A': return 10;
101 case 'b': case 'B': return 11;
102 case 'c': case 'C': return 12;
103 case 'd': case 'D': return 13;
104 case 'e': case 'E': return 14;
105 case 'f': case 'F': return 15;
112 ldns_int_to_hexdigit(int i)
137 ldns_hexstring_to_data(uint8_t *data, const char *str)
145 if (strlen(str) % 2 != 0) {
149 for (i = 0; i < strlen(str) / 2; i++) {
151 16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) +
152 (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]);
161 return (char*)LDNS_VERSION;
164 /* Number of days per month (except for February in leap years). */
165 static const int mdays[] = {
166 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
169 #define LDNS_MOD(x,y) (((x) % (y) < 0) ? ((x) % (y) + (y)) : ((x) % (y)))
170 #define LDNS_DIV(x,y) (((x) % (y) < 0) ? ((x) / (y) - 1 ) : ((x) / (y)))
173 is_leap_year(int year)
175 return LDNS_MOD(year, 4) == 0 && (LDNS_MOD(year, 100) != 0
176 || LDNS_MOD(year, 400) == 0);
180 leap_days(int y1, int y2)
184 return (LDNS_DIV(y2, 4) - LDNS_DIV(y1, 4)) -
185 (LDNS_DIV(y2, 100) - LDNS_DIV(y1, 100)) +
186 (LDNS_DIV(y2, 400) - LDNS_DIV(y1, 400));
190 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
193 ldns_mktime_from_utc(const struct tm *tm)
195 int year = 1900 + tm->tm_year;
196 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
202 for (i = 0; i < tm->tm_mon; ++i) {
205 if (tm->tm_mon > 1 && is_leap_year(year)) {
208 days += tm->tm_mday - 1;
210 hours = days * 24 + tm->tm_hour;
211 minutes = hours * 60 + tm->tm_min;
212 seconds = minutes * 60 + tm->tm_sec;
218 mktime_from_utc(const struct tm *tm)
220 return ldns_mktime_from_utc(tm);
223 #if SIZEOF_TIME_T <= 4
226 ldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result)
231 while (days < 0 || days >= (int64_t) (is_leap_year(year) ? 366 : 365)) {
232 new_year = year + (int) LDNS_DIV(days, 365);
233 days -= (new_year - year) * 365;
234 days -= leap_days(year, new_year);
237 result->tm_year = year;
238 result->tm_yday = (int) days;
241 /* Number of days per month in a leap year. */
242 static const int leap_year_mdays[] = {
243 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
247 ldns_mon_and_mday_from_year_and_yday(struct tm *result)
249 int idays = result->tm_yday;
250 const int *mon_lengths = is_leap_year(result->tm_year) ?
251 leap_year_mdays : mdays;
254 while (idays >= mon_lengths[result->tm_mon]) {
255 idays -= mon_lengths[result->tm_mon++];
257 result->tm_mday = idays + 1;
261 ldns_wday_from_year_and_yday(struct tm *result)
263 result->tm_wday = 4 /* 1-1-1970 was a thursday */
264 + LDNS_MOD((result->tm_year - 1970), 7) * LDNS_MOD(365, 7)
265 + leap_days(1970, result->tm_year)
267 result->tm_wday = LDNS_MOD(result->tm_wday, 7);
268 if (result->tm_wday < 0) {
269 result->tm_wday += 7;
274 ldns_gmtime64_r(int64_t clock, struct tm *result)
276 result->tm_isdst = 0;
277 result->tm_sec = (int) LDNS_MOD(clock, 60);
278 clock = LDNS_DIV(clock, 60);
279 result->tm_min = (int) LDNS_MOD(clock, 60);
280 clock = LDNS_DIV(clock, 60);
281 result->tm_hour = (int) LDNS_MOD(clock, 24);
282 clock = LDNS_DIV(clock, 24);
284 ldns_year_and_yday_from_days_since_epoch(clock, result);
285 ldns_mon_and_mday_from_year_and_yday(result);
286 ldns_wday_from_year_and_yday(result);
287 result->tm_year -= 1900;
292 #endif /* SIZEOF_TIME_T <= 4 */
295 ldns_serial_arithmitics_time(int32_t time, time_t now)
297 int32_t offset = time - (int32_t) now;
298 return (int64_t) now + offset;
303 ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
305 #if SIZEOF_TIME_T <= 4
306 int64_t secs_since_epoch = ldns_serial_arithmitics_time(time, now);
307 return ldns_gmtime64_r(secs_since_epoch, result);
309 time_t secs_since_epoch = ldns_serial_arithmitics_time(time, now);
310 return gmtime_r(&secs_since_epoch, result);
315 * Init the random source
316 * applications should call this if they need entropy data within ldns
317 * If openSSL is available, it is automatically seeded from /dev/urandom
320 * If you need more entropy, or have no openssl available, this function
321 * MUST be called at the start of the program
323 * If openssl *is* available, this function just adds more entropy
326 ldns_init_random(FILE *fd, unsigned int size)
328 /* if fp is given, seed srandom with data from file
329 otherwise use /dev/urandom */
336 /* we'll need at least sizeof(unsigned int) bytes for the
337 standard prng seed */
338 if (size < (unsigned int) sizeof(seed_i)){
339 size = (unsigned int) sizeof(seed_i);
342 seed = LDNS_XMALLOC(uint8_t, size);
348 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
349 /* no readable /dev/urandom, try /dev/random */
350 if ((rand_f = fopen("/dev/random", "r")) == NULL) {
351 /* no readable /dev/random either, and no entropy
352 source given. we'll have to improvise */
353 for (read = 0; read < size; read++) {
354 gettimeofday(&tv, NULL);
355 seed[read] = (uint8_t) (tv.tv_usec % 256);
358 read = fread(seed, 1, size, rand_f);
361 read = fread(seed, 1, size, rand_f);
365 read = fread(seed, 1, size, rand_f);
370 if (!fd) fclose(rand_f);
374 /* Seed the OpenSSL prng (most systems have it seeded
375 automatically, in that case this call just adds entropy */
376 RAND_seed(seed, (int) size);
378 /* Seed the standard prng, only uses the first
379 * unsigned sizeof(unsiged int) bytes found in the entropy pool
381 memcpy(&seed_i, seed, sizeof(seed_i));
388 if (rand_f) fclose(rand_f);
399 ldns_get_random(void)
403 if (RAND_bytes((unsigned char*)&rid, 2) != 1) {
404 rid = (uint16_t) random();
407 rid = (uint16_t) random();
413 * BubbleBabble code taken from OpenSSH
414 * Copyright (c) 2001 Carsten Raskgaard. All rights reserved.
417 ldns_bubblebabble(uint8_t *data, size_t len)
419 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
420 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
421 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
422 size_t i, j = 0, rounds, seed = 1;
425 rounds = (len / 2) + 1;
426 retval = LDNS_XMALLOC(char, rounds * 6);
427 if(!retval) return NULL;
429 for (i = 0; i < rounds; i++) {
430 size_t idx0, idx1, idx2, idx3, idx4;
431 if ((i + 1 < rounds) || (len % 2 != 0)) {
432 idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) +
434 idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
435 idx2 = ((((size_t)(data[2 * i])) & 3) +
437 retval[j++] = vowels[idx0];
438 retval[j++] = consonants[idx1];
439 retval[j++] = vowels[idx2];
440 if ((i + 1) < rounds) {
441 idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15;
442 idx4 = (((size_t)(data[(2 * i) + 1]))) & 15;
443 retval[j++] = consonants[idx3];
445 retval[j++] = consonants[idx4];
447 ((((size_t)(data[2 * i])) * 7) +
448 ((size_t)(data[(2 * i) + 1])))) % 36;
454 retval[j++] = vowels[idx0];
455 retval[j++] = consonants[idx1];
456 retval[j++] = vowels[idx2];