00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00029 void
00030 xprintf_rdf(ldns_rdf *rd)
00031 {
00032
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
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
00092
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
00102
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 if (bit_nr >= 0 && bit_nr < 8) {
00111 if (value) {
00112 *byte = *byte | (0x01 << bit_nr);
00113 } else {
00114 *byte = *byte & !(0x01 << bit_nr);
00115 }
00116 }
00117 }
00118
00119 int
00120 ldns_hexdigit_to_int(char ch)
00121 {
00122 switch (ch) {
00123 case '0': return 0;
00124 case '1': return 1;
00125 case '2': return 2;
00126 case '3': return 3;
00127 case '4': return 4;
00128 case '5': return 5;
00129 case '6': return 6;
00130 case '7': return 7;
00131 case '8': return 8;
00132 case '9': return 9;
00133 case 'a': case 'A': return 10;
00134 case 'b': case 'B': return 11;
00135 case 'c': case 'C': return 12;
00136 case 'd': case 'D': return 13;
00137 case 'e': case 'E': return 14;
00138 case 'f': case 'F': return 15;
00139 default:
00140 return -1;
00141 }
00142 }
00143
00144 char
00145 ldns_int_to_hexdigit(int i)
00146 {
00147 switch (i) {
00148 case 0: return '0';
00149 case 1: return '1';
00150 case 2: return '2';
00151 case 3: return '3';
00152 case 4: return '4';
00153 case 5: return '5';
00154 case 6: return '6';
00155 case 7: return '7';
00156 case 8: return '8';
00157 case 9: return '9';
00158 case 10: return 'a';
00159 case 11: return 'b';
00160 case 12: return 'c';
00161 case 13: return 'd';
00162 case 14: return 'e';
00163 case 15: return 'f';
00164 default:
00165 abort();
00166 }
00167 }
00168
00169 int
00170 ldns_hexstring_to_data(uint8_t *data, const char *str)
00171 {
00172 unsigned int i;
00173
00174 if (!str || !data) {
00175 return -1;
00176 }
00177
00178 if (strlen(str) % 2 != 0) {
00179 return -2;
00180 }
00181
00182 for (i = 0; i < strlen(str) / 2; i++) {
00183 data[i] =
00184 16 * ldns_hexdigit_to_int(str[i*2]) +
00185 ldns_hexdigit_to_int(str[i*2 + 1]);
00186 }
00187
00188 return (int) i;
00189 }
00190
00191 const char *
00192 ldns_version(void)
00193 {
00194 return (char*)LDNS_VERSION;
00195 }
00196
00197
00198 static const int mdays[] = {
00199 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
00200 };
00201
00202 static int
00203 is_leap_year(int year)
00204 {
00205 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
00206 }
00207
00208 static int
00209 leap_days(int y1, int y2)
00210 {
00211 --y1;
00212 --y2;
00213 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
00214 }
00215
00216
00217
00218
00219 time_t
00220 mktime_from_utc(const struct tm *tm)
00221 {
00222 int year = 1900 + tm->tm_year;
00223 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
00224 time_t hours;
00225 time_t minutes;
00226 time_t seconds;
00227 int i;
00228
00229 for (i = 0; i < tm->tm_mon; ++i) {
00230 days += mdays[i];
00231 }
00232 if (tm->tm_mon > 1 && is_leap_year(year)) {
00233 ++days;
00234 }
00235 days += tm->tm_mday - 1;
00236
00237 hours = days * 24 + tm->tm_hour;
00238 minutes = hours * 60 + tm->tm_min;
00239 seconds = minutes * 60 + tm->tm_sec;
00240
00241 return seconds;
00242 }
00243
00255 int
00256 ldns_init_random(FILE *fd, unsigned int size)
00257 {
00258
00259
00260 FILE *rand_f;
00261 unsigned int *seed;
00262 size_t read = 0;
00263 unsigned int seed_i;
00264 struct timeval tv;
00265 struct timezone tz;
00266
00267
00268
00269 if (size < sizeof(seed_i)){
00270 size = sizeof(seed_i);
00271 }
00272
00273 seed = LDNS_XMALLOC(unsigned int, size);
00274
00275 if (!fd) {
00276 if ((rand_f = fopen("/dev/urandom", "r")) == NULL) {
00277
00278 if ((rand_f = fopen("/dev/random", "r")) == NULL) {
00279
00280
00281 for (read = 0; read < size; read++) {
00282 gettimeofday(&tv, &tz);
00283 seed[read] = (uint8_t) (tv.tv_usec % 256);
00284 }
00285 }
00286 }
00287 } else {
00288 rand_f = fd;
00289 read = fread(seed, 1, size, rand_f);
00290 }
00291
00292 if (read < size) {
00293 LDNS_FREE(seed);
00294 return 1;
00295 } else {
00296 #ifdef HAVE_SSL
00297
00298
00299 RAND_seed(seed, size);
00300 #else
00301
00302
00303
00304 memcpy(&seed_i, seed, sizeof(seed_i));
00305 srandom(seed_i);
00306 #endif
00307 LDNS_FREE(seed);
00308 }
00309
00310 if (!fd) {
00311 fclose(rand_f);
00312 }
00313
00314 return 0;
00315 }
00316