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
00022
00023 void
00024 xprintf_rdf(ldns_rdf *rd)
00025 {
00026
00027 fprintf(stderr, "size\t:%u\n", (unsigned int)ldns_rdf_size(rd));
00028 fprintf(stderr, "type\t:%u\n", (unsigned int)ldns_rdf_get_type(rd));
00029 fprintf(stderr, "data\t:[%.*s]\n", (int)ldns_rdf_size(rd),
00030 (char*)ldns_rdf_data(rd));
00031 }
00032
00033 void
00034 xprintf_rr(ldns_rr *rr)
00035 {
00036
00037 uint16_t count, i;
00038
00039 count = ldns_rr_rd_count(rr);
00040
00041 for(i = 0; i < count; i++) {
00042 fprintf(stderr, "print rd %u\n", (unsigned int) i);
00043 xprintf_rdf(rr->_rdata_fields[i]);
00044 }
00045 }
00046
00047 ldns_lookup_table *
00048 ldns_lookup_by_name(ldns_lookup_table *table, const char *name)
00049 {
00050 while (table->name != NULL) {
00051 if (strcasecmp(name, table->name) == 0)
00052 return table;
00053 table++;
00054 }
00055 return NULL;
00056 }
00057
00058 ldns_lookup_table *
00059 ldns_lookup_by_id(ldns_lookup_table *table, int id)
00060 {
00061 while (table->name != NULL) {
00062 if (table->id == id)
00063 return table;
00064 table++;
00065 }
00066 return NULL;
00067 }
00068
00069 int
00070 ldns_get_bit(uint8_t bits[], size_t index)
00071 {
00072
00073
00074
00075
00076 return (int) (bits[index / 8] & (1 << (7 - index % 8)));
00077 }
00078
00079 int
00080 ldns_get_bit_r(uint8_t bits[], size_t index)
00081 {
00082
00083
00084
00085
00086 return (int) bits[index / 8] & (1 << (index % 8));
00087 }
00088
00089 void
00090 ldns_set_bit(uint8_t *byte, int bit_nr, bool value)
00091 {
00092 if (bit_nr >= 0 && bit_nr < 8) {
00093 if (value) {
00094 *byte = *byte | (0x01 << bit_nr);
00095 } else {
00096 *byte = *byte & !(0x01 << bit_nr);
00097 }
00098 }
00099 }
00100
00101 int
00102 ldns_hexdigit_to_int(char ch)
00103 {
00104 switch (ch) {
00105 case '0': return 0;
00106 case '1': return 1;
00107 case '2': return 2;
00108 case '3': return 3;
00109 case '4': return 4;
00110 case '5': return 5;
00111 case '6': return 6;
00112 case '7': return 7;
00113 case '8': return 8;
00114 case '9': return 9;
00115 case 'a': case 'A': return 10;
00116 case 'b': case 'B': return 11;
00117 case 'c': case 'C': return 12;
00118 case 'd': case 'D': return 13;
00119 case 'e': case 'E': return 14;
00120 case 'f': case 'F': return 15;
00121 default:
00122 return -1;
00123 }
00124 }
00125
00126 char
00127 ldns_int_to_hexdigit(int i)
00128 {
00129 switch (i) {
00130 case 0: return '0';
00131 case 1: return '1';
00132 case 2: return '2';
00133 case 3: return '3';
00134 case 4: return '4';
00135 case 5: return '5';
00136 case 6: return '6';
00137 case 7: return '7';
00138 case 8: return '8';
00139 case 9: return '9';
00140 case 10: return 'a';
00141 case 11: return 'b';
00142 case 12: return 'c';
00143 case 13: return 'd';
00144 case 14: return 'e';
00145 case 15: return 'f';
00146 default:
00147 abort();
00148 }
00149 }
00150
00151 const char *
00152 ldns_version(void)
00153 {
00154 return (char*)LDNS_VERSION;
00155 }
00156
00157
00158 static const int mdays[] = {
00159 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
00160 };
00161
00162 static int
00163 is_leap_year(int year)
00164 {
00165 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
00166 }
00167
00168 static int
00169 leap_days(int y1, int y2)
00170 {
00171 --y1;
00172 --y2;
00173 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
00174 }
00175
00176
00177
00178
00179 time_t
00180 mktime_from_utc(const struct tm *tm)
00181 {
00182 int year = 1900 + tm->tm_year;
00183 time_t days = 365 * ((time_t) year - 1970) + leap_days(1970, year);
00184 time_t hours;
00185 time_t minutes;
00186 time_t seconds;
00187 int i;
00188
00189 for (i = 0; i < tm->tm_mon; ++i) {
00190 days += mdays[i];
00191 }
00192 if (tm->tm_mon > 1 && is_leap_year(year)) {
00193 ++days;
00194 }
00195 days += tm->tm_mday - 1;
00196
00197 hours = days * 24 + tm->tm_hour;
00198 minutes = hours * 60 + tm->tm_min;
00199 seconds = minutes * 60 + tm->tm_sec;
00200
00201 return seconds;
00202 }