util.c revision 238104
1/* 2 * util.c 3 * 4 * some general memory functions 5 * 6 * a Net::DNS like library for C 7 * 8 * (c) NLnet Labs, 2004-2006 9 * 10 * See the file LICENSE for the license 11 */ 12 13#include <ldns/config.h> 14 15#include <ldns/rdata.h> 16#include <ldns/rr.h> 17#include <ldns/util.h> 18#include <strings.h> 19#include <stdlib.h> 20#include <stdio.h> 21#include <sys/time.h> 22#include <time.h> 23 24#ifdef HAVE_SSL 25#include <openssl/rand.h> 26#endif 27 28/* put this here tmp. for debugging */ 29void 30xprintf_rdf(ldns_rdf *rd) 31{ 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)); 37} 38 39void 40xprintf_rr(ldns_rr *rr) 41{ 42 /* assume printable string */ 43 uint16_t count, i; 44 45 count = ldns_rr_rd_count(rr); 46 47 for(i = 0; i < count; i++) { 48 fprintf(stderr, "print rd %u\n", (unsigned int) i); 49 xprintf_rdf(rr->_rdata_fields[i]); 50 } 51} 52 53void xprintf_hex(uint8_t *data, size_t len) 54{ 55 size_t i; 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); 59 } 60 printf("%02x ", (unsigned int) data[i]); 61 } 62 printf("\n"); 63} 64 65ldns_lookup_table * 66ldns_lookup_by_name(ldns_lookup_table *table, const char *name) 67{ 68 while (table->name != NULL) { 69 if (strcasecmp(name, table->name) == 0) 70 return table; 71 table++; 72 } 73 return NULL; 74} 75 76ldns_lookup_table * 77ldns_lookup_by_id(ldns_lookup_table *table, int id) 78{ 79 while (table->name != NULL) { 80 if (table->id == id) 81 return table; 82 table++; 83 } 84 return NULL; 85} 86 87int 88ldns_get_bit(uint8_t bits[], size_t index) 89{ 90 /* 91 * The bits are counted from left to right, so bit #0 is the 92 * left most bit. 93 */ 94 return (int) (bits[index / 8] & (1 << (7 - index % 8))); 95} 96 97int 98ldns_get_bit_r(uint8_t bits[], size_t index) 99{ 100 /* 101 * The bits are counted from right to left, so bit #0 is the 102 * right most bit. 103 */ 104 return (int) bits[index / 8] & (1 << (index % 8)); 105} 106 107void 108ldns_set_bit(uint8_t *byte, int bit_nr, bool value) 109{ 110 /* 111 * The bits are counted from right to left, so bit #0 is the 112 * right most bit. 113 */ 114 if (bit_nr >= 0 && bit_nr < 8) { 115 if (value) { 116 *byte = *byte | (0x01 << bit_nr); 117 } else { 118 *byte = *byte & ~(0x01 << bit_nr); 119 } 120 } 121} 122 123int 124ldns_hexdigit_to_int(char ch) 125{ 126 switch (ch) { 127 case '0': return 0; 128 case '1': return 1; 129 case '2': return 2; 130 case '3': return 3; 131 case '4': return 4; 132 case '5': return 5; 133 case '6': return 6; 134 case '7': return 7; 135 case '8': return 8; 136 case '9': return 9; 137 case 'a': case 'A': return 10; 138 case 'b': case 'B': return 11; 139 case 'c': case 'C': return 12; 140 case 'd': case 'D': return 13; 141 case 'e': case 'E': return 14; 142 case 'f': case 'F': return 15; 143 default: 144 return -1; 145 } 146} 147 148char 149ldns_int_to_hexdigit(int i) 150{ 151 switch (i) { 152 case 0: return '0'; 153 case 1: return '1'; 154 case 2: return '2'; 155 case 3: return '3'; 156 case 4: return '4'; 157 case 5: return '5'; 158 case 6: return '6'; 159 case 7: return '7'; 160 case 8: return '8'; 161 case 9: return '9'; 162 case 10: return 'a'; 163 case 11: return 'b'; 164 case 12: return 'c'; 165 case 13: return 'd'; 166 case 14: return 'e'; 167 case 15: return 'f'; 168 default: 169 abort(); 170 } 171} 172 173int 174ldns_hexstring_to_data(uint8_t *data, const char *str) 175{ 176 size_t i; 177 178 if (!str || !data) { 179 return -1; 180 } 181 182 if (strlen(str) % 2 != 0) { 183 return -2; 184 } 185 186 for (i = 0; i < strlen(str) / 2; i++) { 187 data[i] = 188 16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) + 189 (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]); 190 } 191 192 return (int) i; 193} 194 195const char * 196ldns_version(void) 197{ 198 return (char*)LDNS_VERSION; 199} 200 201/* Number of days per month (except for February in leap years). */ 202static const int mdays[] = { 203 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 204}; 205 206#define LDNS_MOD(x,y) (((x) % (y) < 0) ? ((x) % (y) + (y)) : ((x) % (y))) 207#define LDNS_DIV(x,y) (((x) % (y) < 0) ? ((x) / (y) - 1 ) : ((x) / (y))) 208 209static int 210is_leap_year(int year) 211{ 212 return LDNS_MOD(year, 4) == 0 && (LDNS_MOD(year, 100) != 0 213 || LDNS_MOD(year, 400) == 0); 214} 215 216static int 217leap_days(int y1, int y2) 218{ 219 --y1; 220 --y2; 221 return (LDNS_DIV(y2, 4) - LDNS_DIV(y1, 4)) - 222 (LDNS_DIV(y2, 100) - LDNS_DIV(y1, 100)) + 223 (LDNS_DIV(y2, 400) - LDNS_DIV(y1, 400)); 224} 225 226/* 227 * Code adapted from Python 2.4.1 sources (Lib/calendar.py). 228 */ 229time_t 230mktime_from_utc(const struct tm *tm) 231{ 232 int year = 1900 + tm->tm_year; 233 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year); 234 time_t hours; 235 time_t minutes; 236 time_t seconds; 237 int i; 238 239 for (i = 0; i < tm->tm_mon; ++i) { 240 days += mdays[i]; 241 } 242 if (tm->tm_mon > 1 && is_leap_year(year)) { 243 ++days; 244 } 245 days += tm->tm_mday - 1; 246 247 hours = days * 24 + tm->tm_hour; 248 minutes = hours * 60 + tm->tm_min; 249 seconds = minutes * 60 + tm->tm_sec; 250 251 return seconds; 252} 253 254#if SIZEOF_TIME_T <= 4 255 256static void 257ldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result) 258{ 259 int year = 1970; 260 int new_year; 261 262 while (days < 0 || days >= (int64_t) (is_leap_year(year) ? 366 : 365)) { 263 new_year = year + (int) LDNS_DIV(days, 365); 264 days -= (new_year - year) * 365; 265 days -= leap_days(year, new_year); 266 year = new_year; 267 } 268 result->tm_year = year; 269 result->tm_yday = (int) days; 270} 271 272/* Number of days per month in a leap year. */ 273static const int leap_year_mdays[] = { 274 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 275}; 276 277static void 278ldns_mon_and_mday_from_year_and_yday(struct tm *result) 279{ 280 int idays = result->tm_yday; 281 const int *mon_lengths = is_leap_year(result->tm_year) ? 282 leap_year_mdays : mdays; 283 284 result->tm_mon = 0; 285 while (idays >= mon_lengths[result->tm_mon]) { 286 idays -= mon_lengths[result->tm_mon++]; 287 } 288 result->tm_mday = idays + 1; 289} 290 291static void 292ldns_wday_from_year_and_yday(struct tm *result) 293{ 294 result->tm_wday = 4 /* 1-1-1970 was a thursday */ 295 + LDNS_MOD((result->tm_year - 1970), 7) * LDNS_MOD(365, 7) 296 + leap_days(1970, result->tm_year) 297 + result->tm_yday; 298 result->tm_wday = LDNS_MOD(result->tm_wday, 7); 299 if (result->tm_wday < 0) { 300 result->tm_wday += 7; 301 } 302} 303 304static struct tm * 305ldns_gmtime64_r(int64_t clock, struct tm *result) 306{ 307 result->tm_isdst = 0; 308 result->tm_sec = (int) LDNS_MOD(clock, 60); 309 clock = LDNS_DIV(clock, 60); 310 result->tm_min = (int) LDNS_MOD(clock, 60); 311 clock = LDNS_DIV(clock, 60); 312 result->tm_hour = (int) LDNS_MOD(clock, 24); 313 clock = LDNS_DIV(clock, 24); 314 315 ldns_year_and_yday_from_days_since_epoch(clock, result); 316 ldns_mon_and_mday_from_year_and_yday(result); 317 ldns_wday_from_year_and_yday(result); 318 result->tm_year -= 1900; 319 320 return result; 321} 322 323#endif /* SIZEOF_TIME_T <= 4 */ 324 325static int64_t 326ldns_serial_arithmitics_time(int32_t time, time_t now) 327{ 328 int32_t offset = time - (int32_t) now; 329 return (int64_t) now + offset; 330} 331 332 333struct tm * 334ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result) 335{ 336#if SIZEOF_TIME_T <= 4 337 int64_t secs_since_epoch = ldns_serial_arithmitics_time(time, now); 338 return ldns_gmtime64_r(secs_since_epoch, result); 339#else 340 time_t secs_since_epoch = ldns_serial_arithmitics_time(time, now); 341 return gmtime_r(&secs_since_epoch, result); 342#endif 343} 344 345/** 346 * Init the random source 347 * applications should call this if they need entropy data within ldns 348 * If openSSL is available, it is automatically seeded from /dev/urandom 349 * or /dev/random 350 * 351 * If you need more entropy, or have no openssl available, this function 352 * MUST be called at the start of the program 353 * 354 * If openssl *is* available, this function just adds more entropy 355 **/ 356int 357ldns_init_random(FILE *fd, unsigned int size) 358{ 359 /* if fp is given, seed srandom with data from file 360 otherwise use /dev/urandom */ 361 FILE *rand_f; 362 uint8_t *seed; 363 size_t read = 0; 364 unsigned int seed_i; 365 struct timeval tv; 366 367 /* we'll need at least sizeof(unsigned int) bytes for the 368 standard prng seed */ 369 if (size < (unsigned int) sizeof(seed_i)){ 370 size = (unsigned int) sizeof(seed_i); 371 } 372 373 seed = LDNS_XMALLOC(uint8_t, size); 374 if(!seed) { 375 return 1; 376 } 377 378 if (!fd) { 379 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) { 380 /* no readable /dev/urandom, try /dev/random */ 381 if ((rand_f = fopen("/dev/random", "r")) == NULL) { 382 /* no readable /dev/random either, and no entropy 383 source given. we'll have to improvise */ 384 for (read = 0; read < size; read++) { 385 gettimeofday(&tv, NULL); 386 seed[read] = (uint8_t) (tv.tv_usec % 256); 387 } 388 } else { 389 read = fread(seed, 1, size, rand_f); 390 } 391 } else { 392 read = fread(seed, 1, size, rand_f); 393 } 394 } else { 395 rand_f = fd; 396 read = fread(seed, 1, size, rand_f); 397 } 398 399 if (read < size) { 400 LDNS_FREE(seed); 401 return 1; 402 } else { 403#ifdef HAVE_SSL 404 /* Seed the OpenSSL prng (most systems have it seeded 405 automatically, in that case this call just adds entropy */ 406 RAND_seed(seed, (int) size); 407#else 408 /* Seed the standard prng, only uses the first 409 * unsigned sizeof(unsiged int) bytes found in the entropy pool 410 */ 411 memcpy(&seed_i, seed, sizeof(seed_i)); 412 srandom(seed_i); 413#endif 414 LDNS_FREE(seed); 415 } 416 417 if (!fd) { 418 if (rand_f) fclose(rand_f); 419 } 420 421 return 0; 422} 423 424/** 425 * Get random number. 426 * 427 */ 428uint16_t 429ldns_get_random(void) 430{ 431 uint16_t rid = 0; 432#ifdef HAVE_SSL 433 if (RAND_bytes((unsigned char*)&rid, 2) != 1) { 434 rid = (uint16_t) random(); 435 } 436#else 437 rid = (uint16_t) random(); 438#endif 439 return rid; 440} 441 442/* 443 * BubbleBabble code taken from OpenSSH 444 * Copyright (c) 2001 Carsten Raskgaard. All rights reserved. 445 */ 446char * 447ldns_bubblebabble(uint8_t *data, size_t len) 448{ 449 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; 450 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 451 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; 452 size_t i, j = 0, rounds, seed = 1; 453 char *retval; 454 455 rounds = (len / 2) + 1; 456 retval = LDNS_XMALLOC(char, rounds * 6); 457 if(!retval) return NULL; 458 retval[j++] = 'x'; 459 for (i = 0; i < rounds; i++) { 460 size_t idx0, idx1, idx2, idx3, idx4; 461 if ((i + 1 < rounds) || (len % 2 != 0)) { 462 idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) + 463 seed) % 6; 464 idx1 = (((size_t)(data[2 * i])) >> 2) & 15; 465 idx2 = ((((size_t)(data[2 * i])) & 3) + 466 (seed / 6)) % 6; 467 retval[j++] = vowels[idx0]; 468 retval[j++] = consonants[idx1]; 469 retval[j++] = vowels[idx2]; 470 if ((i + 1) < rounds) { 471 idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15; 472 idx4 = (((size_t)(data[(2 * i) + 1]))) & 15; 473 retval[j++] = consonants[idx3]; 474 retval[j++] = '-'; 475 retval[j++] = consonants[idx4]; 476 seed = ((seed * 5) + 477 ((((size_t)(data[2 * i])) * 7) + 478 ((size_t)(data[(2 * i) + 1])))) % 36; 479 } 480 } else { 481 idx0 = seed % 6; 482 idx1 = 16; 483 idx2 = seed / 6; 484 retval[j++] = vowels[idx0]; 485 retval[j++] = consonants[idx1]; 486 retval[j++] = vowels[idx2]; 487 } 488 } 489 retval[j++] = 'x'; 490 retval[j++] = '\0'; 491 return retval; 492} 493