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>
28 /* put this here tmp. for debugging */
30 xprintf_rdf(ldns_rdf *rd)
32 /* assume printable string */
33 fprintf(stderr, "size\t:%u\n", (unsigned int)ldns_rdf_size(rd));
34 fprintf(stderr, "type\t:%u\n", (unsigned int)ldns_rdf_get_type(rd));
35 fprintf(stderr, "data\t:[%.*s]\n", (int)ldns_rdf_size(rd),
36 (char*)ldns_rdf_data(rd));
40 xprintf_rr(ldns_rr *rr)
42 /* assume printable string */
45 count = ldns_rr_rd_count(rr);
47 for(i = 0; i < count; i++) {
48 fprintf(stderr, "print rd %u\n", (unsigned int) i);
49 xprintf_rdf(rr->_rdata_fields[i]);
53 void xprintf_hex(uint8_t *data, size_t len)
56 for (i = 0; i < len; i++) {
57 if (i > 0 && i % 20 == 0) {
58 printf("\t; %u - %u\n", (unsigned int) i - 19, (unsigned int) i);
60 printf("%02x ", (unsigned int) data[i]);
66 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
68 while (table->name != NULL) {
69 if (strcasecmp(name, table->name) == 0)
77 ldns_lookup_by_id(ldns_lookup_table *table, int id)
79 while (table->name != NULL) {
88 ldns_get_bit(uint8_t bits[], size_t index)
91 * The bits are counted from left to right, so bit #0 is the
94 return (int) (bits[index / 8] & (1 << (7 - index % 8)));
98 ldns_get_bit_r(uint8_t bits[], size_t index)
101 * The bits are counted from right to left, so bit #0 is the
104 return (int) bits[index / 8] & (1 << (index % 8));
108 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
110 if (bit_nr >= 0 && bit_nr < 8) {
112 *byte = *byte | (0x01 << bit_nr);
114 *byte = *byte & !(0x01 << bit_nr);
120 ldns_hexdigit_to_int(char ch)
133 case 'a': case 'A': return 10;
134 case 'b': case 'B': return 11;
135 case 'c': case 'C': return 12;
136 case 'd': case 'D': return 13;
137 case 'e': case 'E': return 14;
138 case 'f': case 'F': return 15;
145 ldns_int_to_hexdigit(int i)
170 ldns_hexstring_to_data(uint8_t *data, const char *str)
178 if (strlen(str) % 2 != 0) {
182 for (i = 0; i < strlen(str) / 2; i++) {
184 16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) +
185 (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]);
194 return (char*)LDNS_VERSION;
197 /* Number of days per month (except for February in leap years). */
198 static const int mdays[] = {
199 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
203 is_leap_year(int year)
205 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
209 leap_days(int y1, int y2)
213 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
217 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
220 mktime_from_utc(const struct tm *tm)
222 int year = 1900 + tm->tm_year;
223 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
229 for (i = 0; i < tm->tm_mon; ++i) {
232 if (tm->tm_mon > 1 && is_leap_year(year)) {
235 days += tm->tm_mday - 1;
237 hours = days * 24 + tm->tm_hour;
238 minutes = hours * 60 + tm->tm_min;
239 seconds = minutes * 60 + tm->tm_sec;
245 * Init the random source
246 * applications should call this if they need entropy data within ldns
247 * If openSSL is available, it is automatically seeded from /dev/urandom
250 * If you need more entropy, or have no openssl available, this function
251 * MUST be called at the start of the program
253 * If openssl *is* available, this function just adds more entropy
256 ldns_init_random(FILE *fd, unsigned int size)
258 /* if fp is given, seed srandom with data from file
259 otherwise use /dev/urandom */
266 /* we'll need at least sizeof(unsigned int) bytes for the
267 standard prng seed */
268 if (size < (unsigned int) sizeof(seed_i)){
269 size = (unsigned int) sizeof(seed_i);
272 seed = LDNS_XMALLOC(uint8_t, size);
275 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
276 /* no readable /dev/urandom, try /dev/random */
277 if ((rand_f = fopen("/dev/random", "r")) == NULL) {
278 /* no readable /dev/random either, and no entropy
279 source given. we'll have to improvise */
280 for (read = 0; read < size; read++) {
281 gettimeofday(&tv, NULL);
282 seed[read] = (uint8_t) (tv.tv_usec % 256);
285 read = fread(seed, 1, size, rand_f);
288 read = fread(seed, 1, size, rand_f);
292 read = fread(seed, 1, size, rand_f);
300 /* Seed the OpenSSL prng (most systems have it seeded
301 automatically, in that case this call just adds entropy */
302 RAND_seed(seed, (int) size);
304 /* Seed the standard prng, only uses the first
305 * unsigned sizeof(unsiged int) bytes found in the entropy pool
307 memcpy(&seed_i, seed, sizeof(seed_i));
314 if (rand_f) fclose(rand_f);
325 ldns_get_random(void)
329 if (RAND_bytes((unsigned char*)&rid, 2) != 1) {
330 rid = (uint16_t) random();
333 rid = (uint16_t) random();
339 * BubbleBabble code taken from OpenSSH
340 * Copyright (c) 2001 Carsten Raskgaard. All rights reserved.
343 ldns_bubblebabble(uint8_t *data, size_t len)
345 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
346 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
347 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
348 size_t i, j = 0, rounds, seed = 1;
351 rounds = (len / 2) + 1;
352 retval = LDNS_XMALLOC(char, rounds * 6);
353 if(!retval) return NULL;
355 for (i = 0; i < rounds; i++) {
356 size_t idx0, idx1, idx2, idx3, idx4;
357 if ((i + 1 < rounds) || (len % 2 != 0)) {
358 idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) +
360 idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
361 idx2 = ((((size_t)(data[2 * i])) & 3) +
363 retval[j++] = vowels[idx0];
364 retval[j++] = consonants[idx1];
365 retval[j++] = vowels[idx2];
366 if ((i + 1) < rounds) {
367 idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15;
368 idx4 = (((size_t)(data[(2 * i) + 1]))) & 15;
369 retval[j++] = consonants[idx3];
371 retval[j++] = consonants[idx4];
373 ((((size_t)(data[2 * i])) * 7) +
374 ((size_t)(data[(2 * i) + 1])))) % 36;
380 retval[j++] = vowels[idx0];
381 retval[j++] = consonants[idx1];
382 retval[j++] = vowels[idx2];