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