util.c

Go to the documentation of this file.
00001 /*
00002  * util.c
00003  *
00004  * some general memory functions
00005  *
00006  * a Net::DNS like library for C
00007  *
00008  * (c) NLnet Labs, 2004-2006
00009  *
00010  * See the file LICENSE for the license
00011  */
00012 
00013 #include <ldns/config.h>
00014 
00015 #include <ldns/rdata.h>
00016 #include <ldns/rr.h>
00017 #include <ldns/util.h>
00018 #include <strings.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <sys/time.h>
00022 #include <time.h>
00023 
00024 #ifdef HAVE_SSL
00025 #include <openssl/rand.h>
00026 #endif
00027 
00028 /* put this here tmp. for debugging */
00029 void
00030 xprintf_rdf(ldns_rdf *rd)
00031 {
00032         /* assume printable string */
00033         fprintf(stderr, "size\t:%u\n", (unsigned int)ldns_rdf_size(rd));
00034         fprintf(stderr, "type\t:%u\n", (unsigned int)ldns_rdf_get_type(rd));
00035         fprintf(stderr, "data\t:[%.*s]\n", (int)ldns_rdf_size(rd),
00036                         (char*)ldns_rdf_data(rd));
00037 }
00038 
00039 void
00040 xprintf_rr(ldns_rr *rr)
00041 {
00042         /* assume printable string */
00043         uint16_t count, i;
00044 
00045         count = ldns_rr_rd_count(rr);
00046 
00047         for(i = 0; i < count; i++) {
00048                 fprintf(stderr, "print rd %u\n", (unsigned int) i);
00049                 xprintf_rdf(rr->_rdata_fields[i]);
00050         }
00051 }
00052 
00053 void xprintf_hex(uint8_t *data, size_t len)
00054 {
00055         size_t i;
00056         for (i = 0; i < len; i++) {
00057                 if (i > 0 && i % 20 == 0) {
00058                         printf("\t; %u - %u\n", (unsigned int) i - 19, (unsigned int) i);
00059                 }
00060                 printf("%02x ", (unsigned int) data[i]);
00061         }
00062         printf("\n");
00063 }
00064 
00065 ldns_lookup_table *
00066 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
00067 {
00068         while (table->name != NULL) {
00069                 if (strcasecmp(name, table->name) == 0)
00070                         return table;
00071                 table++;
00072         }
00073         return NULL;
00074 }
00075 
00076 ldns_lookup_table *
00077 ldns_lookup_by_id(ldns_lookup_table *table, int id)
00078 {
00079         while (table->name != NULL) {
00080                 if (table->id == id)
00081                         return table;
00082                 table++;
00083         }
00084         return NULL;
00085 }
00086 
00087 int
00088 ldns_get_bit(uint8_t bits[], size_t index)
00089 {
00090         /*
00091          * The bits are counted from left to right, so bit #0 is the
00092          * left most bit.
00093          */
00094         return (int) (bits[index / 8] & (1 << (7 - index % 8)));
00095 }
00096 
00097 int
00098 ldns_get_bit_r(uint8_t bits[], size_t index)
00099 {
00100         /*
00101          * The bits are counted from right to left, so bit #0 is the
00102          * right most bit.
00103          */
00104         return (int) bits[index / 8] & (1 << (index % 8));
00105 }
00106 
00107 void
00108 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
00109 {
00110         /*
00111          * The bits are counted from right to left, so bit #0 is the
00112          * right most bit.
00113          */
00114         if (bit_nr >= 0 && bit_nr < 8) {
00115                 if (value) {
00116                         *byte = *byte | (0x01 << bit_nr);
00117                 } else {
00118                         *byte = *byte & ~(0x01 << bit_nr);
00119                 }
00120         }
00121 }
00122 
00123 int
00124 ldns_hexdigit_to_int(char ch)
00125 {
00126         switch (ch) {
00127         case '0': return 0;
00128         case '1': return 1;
00129         case '2': return 2;
00130         case '3': return 3;
00131         case '4': return 4;
00132         case '5': return 5;
00133         case '6': return 6;
00134         case '7': return 7;
00135         case '8': return 8;
00136         case '9': return 9;
00137         case 'a': case 'A': return 10;
00138         case 'b': case 'B': return 11;
00139         case 'c': case 'C': return 12;
00140         case 'd': case 'D': return 13;
00141         case 'e': case 'E': return 14;
00142         case 'f': case 'F': return 15;
00143         default:
00144                 return -1;
00145         }
00146 }
00147 
00148 char
00149 ldns_int_to_hexdigit(int i)
00150 {
00151         switch (i) {
00152         case 0: return '0';
00153         case 1: return '1';
00154         case 2: return '2';
00155         case 3: return '3';
00156         case 4: return '4';
00157         case 5: return '5';
00158         case 6: return '6';
00159         case 7: return '7';
00160         case 8: return '8';
00161         case 9: return '9';
00162         case 10: return 'a';
00163         case 11: return 'b';
00164         case 12: return 'c';
00165         case 13: return 'd';
00166         case 14: return 'e';
00167         case 15: return 'f';
00168         default:
00169                 abort();
00170         }
00171 }
00172 
00173 int
00174 ldns_hexstring_to_data(uint8_t *data, const char *str)
00175 {
00176         size_t i;
00177 
00178         if (!str || !data) {
00179                 return -1;
00180         }
00181 
00182         if (strlen(str) % 2 != 0) {
00183                 return -2;
00184         }
00185 
00186         for (i = 0; i < strlen(str) / 2; i++) {
00187                 data[i] =
00188                         16 * (uint8_t) ldns_hexdigit_to_int(str[i*2]) +
00189                         (uint8_t) ldns_hexdigit_to_int(str[i*2 + 1]);
00190         }
00191 
00192         return (int) i;
00193 }
00194 
00195 const char *
00196 ldns_version(void)
00197 {
00198         return (char*)LDNS_VERSION;
00199 }
00200 
00201 /* Number of days per month (except for February in leap years). */
00202 static const int mdays[] = {
00203         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
00204 };
00205 
00206 #define LDNS_MOD(x,y) (((x) % (y) < 0) ? ((x) % (y) + (y)) : ((x) % (y)))
00207 #define LDNS_DIV(x,y) (((x) % (y) < 0) ? ((x) / (y) -  1 ) : ((x) / (y)))
00208 
00209 static int
00210 is_leap_year(int year)
00211 {
00212         return LDNS_MOD(year,   4) == 0 && (LDNS_MOD(year, 100) != 0 
00213             || LDNS_MOD(year, 400) == 0);
00214 }
00215 
00216 static int
00217 leap_days(int y1, int y2)
00218 {
00219         --y1;
00220         --y2;
00221         return (LDNS_DIV(y2,   4) - LDNS_DIV(y1,   4)) - 
00222                (LDNS_DIV(y2, 100) - LDNS_DIV(y1, 100)) +
00223                (LDNS_DIV(y2, 400) - LDNS_DIV(y1, 400));
00224 }
00225 
00226 /*
00227  * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
00228  */
00229 time_t
00230 mktime_from_utc(const struct tm *tm)
00231 {
00232         int year = 1900 + tm->tm_year;
00233         time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
00234         time_t hours;
00235         time_t minutes;
00236         time_t seconds;
00237         int i;
00238 
00239         for (i = 0; i < tm->tm_mon; ++i) {
00240                 days += mdays[i];
00241         }
00242         if (tm->tm_mon > 1 && is_leap_year(year)) {
00243                 ++days;
00244         }
00245         days += tm->tm_mday - 1;
00246 
00247         hours = days * 24 + tm->tm_hour;
00248         minutes = hours * 60 + tm->tm_min;
00249         seconds = minutes * 60 + tm->tm_sec;
00250 
00251         return seconds;
00252 }
00253 
00254 #if SIZEOF_TIME_T <= 4
00255 
00256 static void
00257 ldns_year_and_yday_from_days_since_epoch(int64_t days, struct tm *result)
00258 {
00259         int year = 1970;
00260         int new_year;
00261 
00262         while (days < 0 || days >= (int64_t) (is_leap_year(year) ? 366 : 365)) {
00263                 new_year = year + (int) LDNS_DIV(days, 365);
00264                 days -= (new_year - year) * 365;
00265                 days -= leap_days(year, new_year);
00266                 year  = new_year;
00267         }
00268         result->tm_year = year;
00269         result->tm_yday = (int) days;
00270 }
00271 
00272 /* Number of days per month in a leap year. */
00273 static const int leap_year_mdays[] = {
00274         31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
00275 };
00276 
00277 static void
00278 ldns_mon_and_mday_from_year_and_yday(struct tm *result)
00279 {
00280         int idays = result->tm_yday;
00281         const int *mon_lengths = is_leap_year(result->tm_year) ? 
00282                                         leap_year_mdays : mdays;
00283 
00284         result->tm_mon = 0;
00285         while  (idays >= mon_lengths[result->tm_mon]) {
00286                 idays -= mon_lengths[result->tm_mon++];
00287         }
00288         result->tm_mday = idays + 1;
00289 }
00290 
00291 static void
00292 ldns_wday_from_year_and_yday(struct tm *result)
00293 {
00294         result->tm_wday = 4 /* 1-1-1970 was a thursday */
00295                         + LDNS_MOD((result->tm_year - 1970), 7) * LDNS_MOD(365, 7)
00296                         + leap_days(1970, result->tm_year)
00297                         + result->tm_yday;
00298         result->tm_wday = LDNS_MOD(result->tm_wday, 7);
00299         if (result->tm_wday < 0) {
00300                 result->tm_wday += 7;
00301         }
00302 }
00303 
00304 static struct tm *
00305 ldns_gmtime64_r(int64_t clock, struct tm *result)
00306 {
00307         result->tm_isdst = 0;
00308         result->tm_sec   = (int) LDNS_MOD(clock, 60);
00309         clock            =       LDNS_DIV(clock, 60);
00310         result->tm_min   = (int) LDNS_MOD(clock, 60);
00311         clock            =       LDNS_DIV(clock, 60);
00312         result->tm_hour  = (int) LDNS_MOD(clock, 24);
00313         clock            =       LDNS_DIV(clock, 24);
00314 
00315         ldns_year_and_yday_from_days_since_epoch(clock, result);
00316         ldns_mon_and_mday_from_year_and_yday(result);
00317         ldns_wday_from_year_and_yday(result);
00318         result->tm_year -= 1900;
00319 
00320         return result;
00321 }
00322 
00323 #endif /* SIZEOF_TIME_T <= 4 */
00324 
00325 static int64_t
00326 ldns_serial_arithmitics_time(int32_t time, time_t now)
00327 {
00328         int32_t offset = time - (int32_t) now;
00329         return (int64_t) now + offset;
00330 }
00331 
00332 
00333 struct tm *
00334 ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result)
00335 {
00336 #if SIZEOF_TIME_T <= 4
00337         int64_t secs_since_epoch = ldns_serial_arithmitics_time(time, now);
00338         return  ldns_gmtime64_r(secs_since_epoch, result);
00339 #else
00340         time_t  secs_since_epoch = ldns_serial_arithmitics_time(time, now);
00341         return  gmtime_r(&secs_since_epoch, result);
00342 #endif
00343 }
00344 
00356 int
00357 ldns_init_random(FILE *fd, unsigned int size)
00358 {
00359         /* if fp is given, seed srandom with data from file
00360            otherwise use /dev/urandom */
00361         FILE *rand_f;
00362         uint8_t *seed;
00363         size_t read = 0;
00364         unsigned int seed_i;
00365         struct timeval tv;
00366 
00367         /* we'll need at least sizeof(unsigned int) bytes for the
00368            standard prng seed */
00369         if (size < (unsigned int) sizeof(seed_i)){
00370                 size = (unsigned int) sizeof(seed_i);
00371         }
00372 
00373         seed = LDNS_XMALLOC(uint8_t, size);
00374         if(!seed) {
00375                 return 1;
00376         }
00377 
00378         if (!fd) {
00379                 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
00380                         /* no readable /dev/urandom, try /dev/random */
00381                         if ((rand_f = fopen("/dev/random", "r")) == NULL) {
00382                                 /* no readable /dev/random either, and no entropy
00383                                    source given. we'll have to improvise */
00384                                 for (read = 0; read < size; read++) {
00385                                         gettimeofday(&tv, NULL);
00386                                         seed[read] = (uint8_t) (tv.tv_usec % 256);
00387                                 }
00388                         } else {
00389                                 read = fread(seed, 1, size, rand_f);
00390                         }
00391                 } else {
00392                         read = fread(seed, 1, size, rand_f);
00393                 }
00394         } else {
00395                 rand_f = fd;
00396                 read = fread(seed, 1, size, rand_f);
00397         }
00398 
00399         if (read < size) {
00400                 LDNS_FREE(seed);
00401                 return 1;
00402         } else {
00403 #ifdef HAVE_SSL
00404                 /* Seed the OpenSSL prng (most systems have it seeded
00405                    automatically, in that case this call just adds entropy */
00406                 RAND_seed(seed, (int) size);
00407 #else
00408                 /* Seed the standard prng, only uses the first
00409                  * unsigned sizeof(unsiged int) bytes found in the entropy pool
00410                  */
00411                 memcpy(&seed_i, seed, sizeof(seed_i));
00412                 srandom(seed_i);
00413 #endif
00414                 LDNS_FREE(seed);
00415         }
00416 
00417         if (!fd) {
00418                 if (rand_f) fclose(rand_f);
00419         }
00420 
00421         return 0;
00422 }
00423 
00428 uint16_t
00429 ldns_get_random(void)
00430 {
00431         uint16_t rid = 0;
00432 #ifdef HAVE_SSL
00433         if (RAND_bytes((unsigned char*)&rid, 2) != 1) {
00434                 rid = (uint16_t) random();
00435         }
00436 #else
00437         rid = (uint16_t) random();
00438 #endif
00439         return rid;
00440 }
00441 
00442 /*
00443  * BubbleBabble code taken from OpenSSH
00444  * Copyright (c) 2001 Carsten Raskgaard.  All rights reserved.
00445  */
00446 char *
00447 ldns_bubblebabble(uint8_t *data, size_t len)
00448 {
00449         char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
00450         char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
00451             'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
00452         size_t i, j = 0, rounds, seed = 1;
00453         char *retval;
00454 
00455         rounds = (len / 2) + 1;
00456         retval = LDNS_XMALLOC(char, rounds * 6);
00457         if(!retval) return NULL;
00458         retval[j++] = 'x';
00459         for (i = 0; i < rounds; i++) {
00460                 size_t idx0, idx1, idx2, idx3, idx4;
00461                 if ((i + 1 < rounds) || (len % 2 != 0)) {
00462                         idx0 = (((((size_t)(data[2 * i])) >> 6) & 3) +
00463                             seed) % 6;
00464                         idx1 = (((size_t)(data[2 * i])) >> 2) & 15;
00465                         idx2 = ((((size_t)(data[2 * i])) & 3) +
00466                             (seed / 6)) % 6;
00467                         retval[j++] = vowels[idx0];
00468                         retval[j++] = consonants[idx1];
00469                         retval[j++] = vowels[idx2];
00470                         if ((i + 1) < rounds) {
00471                                 idx3 = (((size_t)(data[(2 * i) + 1])) >> 4) & 15;
00472                                 idx4 = (((size_t)(data[(2 * i) + 1]))) & 15;
00473                                 retval[j++] = consonants[idx3];
00474                                 retval[j++] = '-';
00475                                 retval[j++] = consonants[idx4];
00476                                 seed = ((seed * 5) +
00477                                     ((((size_t)(data[2 * i])) * 7) +
00478                                     ((size_t)(data[(2 * i) + 1])))) % 36;
00479                         }
00480                 } else {
00481                         idx0 = seed % 6;
00482                         idx1 = 16;
00483                         idx2 = seed / 6;
00484                         retval[j++] = vowels[idx0];
00485                         retval[j++] = consonants[idx1];
00486                         retval[j++] = vowels[idx2];
00487                 }
00488         }
00489         retval[j++] = 'x';
00490         retval[j++] = '\0';
00491         return retval;
00492 }

Generated on Fri Jun 8 17:07:47 2012 for ldns by  doxygen 1.4.7