17 #ifdef HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
20 #ifdef HAVE_ARPA_INET_H
21 #include <arpa/inet.h>
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
43 *r = htons((uint16_t)strtol((
char *)shortstr, &end, 10));
69 memset(&tm, 0,
sizeof(tm));
71 if (strlen(time) == 14 &&
72 sscanf(time,
"%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6
77 if (tm.tm_year < 70) {
80 if (tm.tm_mon < 0 || tm.tm_mon > 11) {
83 if (tm.tm_mday < 1 || tm.tm_mday > 31) {
87 if (tm.tm_hour < 0 || tm.tm_hour > 23) {
91 if (tm.tm_min < 0 || tm.tm_min > 59) {
95 if (tm.tm_sec < 0 || tm.tm_sec > 59) {
100 memcpy(r, &l,
sizeof(uint32_t));
107 l = htonl((uint32_t)strtol((
char*)time, &end, 10));
112 memcpy(r, &l,
sizeof(uint32_t));
138 salt_length_str = (int)strlen(salt_str);
139 if (salt_length_str == 1 && salt_str[0] ==
'-') {
141 }
else if (salt_length_str % 2 != 0) {
144 if (salt_length_str > 512) {
152 for (c = 0; c < salt_length_str; c += 2) {
153 if (isxdigit((
int) salt_str[c]) && isxdigit((
int) salt_str[c+1])) {
161 salt_length = (uint8_t) (salt_length_str / 2);
168 data[0] = salt_length;
169 memcpy(&data[1], salt, salt_length);
189 p = (uint32_t) htonl(p);
208 l = htonl((uint32_t)strtol((
char*)longstr, &end, 10));
209 else l = htonl((uint32_t)strtoul((
char*)longstr, &end, 10));
215 if (errno == ERANGE) {
219 memcpy(r, &l,
sizeof(uint32_t));
236 *r = (uint8_t)strtol((
char*)bytestr, &end, 10);
261 parse_escape(uint8_t *ch_p,
const char** str_p)
265 if ((*str_p)[0] && isdigit((
unsigned char)(*str_p)[0]) &&
266 (*str_p)[1] && isdigit((
unsigned char)(*str_p)[1]) &&
267 (*str_p)[2] && isdigit((
unsigned char)(*str_p)[2])) {
269 val = (uint16_t)(((*str_p)[0] -
'0') * 100 +
270 ((*str_p)[1] -
'0') * 10 +
271 ((*str_p)[2] -
'0'));
276 *ch_p = (uint8_t)val;
280 }
else if ((*str_p)[0] && !isdigit((
unsigned char)(*str_p)[0])) {
282 *ch_p = (uint8_t)*(*str_p)++;
291 parse_char(uint8_t *ch_p,
const char** str_p)
295 case '\0':
return false;
297 case '\\': *str_p += 1;
298 return parse_escape(ch_p, str_p);
300 default: *ch_p = (uint8_t)*(*str_p)++;
316 uint8_t *q, *pq, label_len;
320 len = strlen((
char*)str);
330 if (1 == len && *str ==
'.') {
346 for (s = str; *s; s++, q++) {
356 if (label_len == 0) {
359 len += label_len + 1;
367 if (! parse_escape(q, &s)) {
387 if (label_len == 0) {
390 len += label_len + 1;
404 if (inet_pton(AF_INET, (
char*)str, &address) != 1) {
418 if (inet_pton(AF_INET6, (
char*)str, address) != 1) {
430 uint8_t *data, *dp, ch = 0;
434 dp = data =
LDNS_XMALLOC(uint8_t, strlen(str) > 255 ? 256 : (strlen(str) + 1));
440 while (parse_char(&ch, &str)) {
441 if (dp - data >= 255) {
450 length = (size_t)(dp - data);
452 data[0] = (uint8_t)length;
473 const char *my_str = str;
480 uint8_t afdlength = 0;
489 if (strlen(my_str) < 2
490 || strchr(my_str,
':') == NULL
491 || strchr(my_str,
'/') == NULL
492 || strchr(my_str,
':') > strchr(my_str,
'/')) {
496 if (my_str[0] ==
'!') {
503 family = (uint16_t) atoi(my_str);
505 my_str = strchr(my_str,
':') + 1;
508 ip_str_len = (size_t) (strchr(my_str,
'/') - my_str);
511 strncpy(my_ip_str, my_str, ip_str_len + 1);
512 my_ip_str[ip_str_len] =
'\0';
521 if (inet_pton(AF_INET, my_ip_str, afdpart) == 0) {
526 for (i = 0; i < 4; i++) {
527 if (afdpart[i] != 0) {
531 }
else if (family == 2) {
538 if (inet_pton(AF_INET6, my_ip_str, afdpart) == 0) {
543 for (i = 0; i < 16; i++) {
544 if (afdpart[i] != 0) {
554 my_str = strchr(my_str,
'/') + 1;
555 prefix = (uint8_t) atoi(my_str);
563 ldns_write_uint16(data, family);
568 data[3] = data[3] | 0x80;
571 memcpy(data + 4, afdpart, afdlength);
587 buffer =
LDNS_XMALLOC(uint8_t, ldns_b64_ntop_calculate_size(strlen(str)));
593 ldns_b64_ntop_calculate_size(strlen(str)));
612 uint8_t len = ldns_b32_pton_calculate_size(strlen(str));
620 ldns_b32_ntop_calculate_size(strlen(str)));
653 if (isspace((
int) *str)) {
656 for (i = 16; i >= 1; i -= 15) {
657 while (*str && isspace((
int) *str)) { str++; }
659 if (isxdigit((
int) *str)) {
672 (
size_t) (t - t_orig),
682 const char *delimiters =
"\n\t ";
687 size_t type_count = 0;
708 if(type_count >=
sizeof(type_list)) {
714 type_list[type_count] = cur_type;
762 ldns_write_uint16(idd, (uint16_t) lt->
id);
831 return ldns_str2rdf_mnemonic4int8(
832 ldns_tlsa_certificate_usages, rd, str);
838 return ldns_str2rdf_mnemonic4int8(ldns_tlsa_selectors, rd, str);
844 return ldns_str2rdf_mnemonic4int8(ldns_tlsa_matching_types, rd, str);
867 loc_parse_cm(
char* my_str,
char** endstr, uint8_t* m, uint8_t* e)
871 uint32_t meters = 0, cm = 0, val;
872 while (isblank((
unsigned char)*my_str)) {
875 meters = (uint32_t)strtol(my_str, &my_str, 10);
876 if (*my_str ==
'.') {
878 cm = (uint32_t)strtol(my_str, &my_str, 10);
895 if (*my_str ==
'm' || *my_str ==
'M') {
905 uint32_t latitude = 0;
906 uint32_t longitude = 0;
907 uint32_t altitude = 0;
910 uint32_t equator = (uint32_t) ldns_power(2, 31);
914 uint8_t size_b = 1, size_e = 2;
915 uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
916 uint8_t vert_pre_b = 1, vert_pre_e = 3;
922 char *my_str = (
char *) str;
925 if (isdigit((
int) *my_str)) {
926 h = (uint32_t) strtol(my_str, &my_str, 10);
931 while (isblank((
int) *my_str)) {
935 if (isdigit((
int) *my_str)) {
936 m = (uint32_t) strtol(my_str, &my_str, 10);
937 }
else if (*my_str ==
'N' || *my_str ==
'S') {
943 while (isblank((
int) *my_str)) {
947 if (isdigit((
int) *my_str)) {
948 s = strtod(my_str, &my_str);
951 while (isblank((
int) *my_str)) {
955 if (*my_str ==
'N') {
957 }
else if (*my_str ==
'S') {
969 latitude = (uint32_t) s;
970 latitude += 1000 * 60 * m;
971 latitude += 1000 * 60 * 60 * h;
973 latitude = equator + latitude;
975 latitude = equator - latitude;
977 while (isblank((
unsigned char)*my_str)) {
981 if (isdigit((
int) *my_str)) {
982 h = (uint32_t) strtol(my_str, &my_str, 10);
987 while (isblank((
int) *my_str)) {
991 if (isdigit((
int) *my_str)) {
992 m = (uint32_t) strtol(my_str, &my_str, 10);
993 }
else if (*my_str ==
'E' || *my_str ==
'W') {
999 while (isblank((
unsigned char)*my_str)) {
1003 if (isdigit((
int) *my_str)) {
1004 s = strtod(my_str, &my_str);
1008 while (isblank((
unsigned char)*my_str)) {
1012 if (*my_str ==
'E') {
1014 }
else if (*my_str ==
'W') {
1026 longitude = (uint32_t) s;
1027 longitude += 1000 * 60 * m;
1028 longitude += 1000 * 60 * 60 * h;
1031 longitude += equator;
1033 longitude = equator - longitude;
1036 altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
1038 if (*my_str ==
'm' || *my_str ==
'M') {
1042 if (strlen(my_str) > 0) {
1043 if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
1047 if (strlen(my_str) > 0) {
1048 if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
1052 if (strlen(my_str) > 0) {
1053 if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
1063 data[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
1064 data[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
1065 data[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
1066 ldns_write_uint32(data + 4, latitude);
1067 ldns_write_uint32(data + 8, longitude);
1068 ldns_write_uint32(data + 12, altitude);
1080 uint8_t *bitmap = NULL;
1084 struct protoent *proto = NULL;
1085 struct servent *serv = NULL;
1090 char *proto_str = NULL;
1092 if(strlen(str) == 0)
1108 proto_str = strdup(token);
1116 serv = getservbyname(token, proto_str);
1118 serv_port = (int) ntohs((uint16_t) serv->s_port);
1120 serv_port = atoi(token);
1122 if (serv_port / 8 >= bm_len) {
1123 uint8_t *b2 =
LDNS_XREALLOC(bitmap, uint8_t, (serv_port / 8) + 1);
1133 for (; bm_len <= serv_port / 8; bm_len++) {
1137 ldns_set_bit(bitmap + (serv_port / 8), 7 - (serv_port % 8),
true);
1141 if (!proto_str || !bitmap) {
1158 proto = getprotobyname(proto_str);
1160 data[0] = (uint8_t) proto->p_proto;
1161 }
else if (proto_str) {
1162 data[0] = (uint8_t) atoi(proto_str);
1164 memcpy(data + 1, bitmap, (
size_t) bm_len);
1173 #ifdef HAVE_ENDSERVENT
1176 #ifdef HAVE_ENDPROTOENT
1189 char* nsap_str = (
char*) str;
1192 if (str[0] !=
'0' || str[1] !=
'x') {
1196 for (i=0; i < len; i++) {
1197 if (nsap_str[i] ==
'.')
1208 char* atma_str = (
char*) str;
1213 for (i=0; i < len; i++) {
1214 if (atma_str[i] ==
'.')
1227 uint8_t precedence = 0;
1228 uint8_t gateway_type = 0;
1229 uint8_t algorithm = 0;
1230 char* gateway = NULL;
1231 char* publickey = NULL;
1235 int token_count = 0;
1236 int ipseckey_len = 0;
1241 if(strlen(str) == 0)
1255 switch (token_count) {
1257 precedence = (uint8_t)atoi(token);
1260 gateway_type = (uint8_t)atoi(token);
1263 algorithm = (uint8_t)atoi(token);
1266 gateway = strdup(token);
1267 if (!gateway || (gateway_type == 0 &&
1268 (token[0] !=
'.' || token[1] !=
'\0'))) {
1276 publickey = strdup(token);
1287 if (!gateway || !publickey) {
1297 if (gateway_type == 1) {
1299 }
else if (gateway_type == 2) {
1301 }
else if (gateway_type == 3) {
1347 data[0] = precedence;
1348 data[1] = gateway_type;
1349 data[2] = algorithm;
1379 unsigned int a, b, c, d;
1383 if (sscanf(str,
"%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 ||
1384 l != (
int)strlen(str) ||
1389 shorts[0] = htons(a);
1390 shorts[1] = htons(b);
1391 shorts[2] = htons(c);
1392 shorts[3] = htons(d);
1402 unsigned int a, b, c, d, e, f;
1406 if (sscanf(str,
"%2x-%2x-%2x-%2x-%2x-%2x%n",
1407 &a, &b, &c, &d, &e, &f, &l) != 6 ||
1408 l != (
int)strlen(str)) {
1425 unsigned int a, b, c, d, e, f, g, h;
1429 if (sscanf(str,
"%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n",
1430 &a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 ||
1431 l != (
int)strlen(str)) {
1453 if (strlen(str) > 255) {
1456 for (ptr = str; *ptr; ptr++) {
1457 if (! isalnum((
unsigned char)*ptr)) {
1465 data[0] = strlen(str);
1466 memcpy(data + 1, str, strlen(str));
1479 uint8_t *data, *dp, ch = 0;
1489 while (parse_char(&ch, &str)) {
1499 if (!(length = (
size_t)(dp - data))) {
1522 const char *hit = strchr(str,
' ') + 1;
1523 const char *pk = hit == NULL ? NULL : strchr(hit,
' ') + 1;
1524 size_t hit_size = hit == NULL ? 0
1525 : pk == NULL ? strlen(hit) : (size_t) (pk - hit) - 1;
1526 size_t pk_size = pk == NULL ? 0 : strlen(pk);
1527 size_t hit_wire_size = (hit_size + 1) / 2;
1528 size_t pk_wire_size = ldns_b64_pton_calculate_size(pk_size);
1529 size_t rdf_size = 4 + hit_wire_size + pk_wire_size;
1532 int algorithm = strtol(str, &endptr, 10);
1535 int hi, lo, written;
1537 if (hit_size == 0 || pk_size == 0 || (hit_size + 1) / 2 > 255
1539 || algorithm < 0 || algorithm > 255
1540 || (errno != 0 && algorithm == 0)
1541 || endptr == str ) {
1545 if ((data =
LDNS_XMALLOC(uint8_t, rdf_size)) == NULL) {
1576 data[0] = (uint8_t) hit_wire_size;
1577 data[1] = (uint8_t) algorithm;
1579 for (dp = data + 4; *hit && *hit !=
' '; dp++) {
1587 *dp = (uint8_t) hi << 4 | lo;
1598 pk_wire_size = (uint16_t) written;
1599 ldns_write_uint16(data + 2, pk_wire_size);
1600 rdf_size = 4 + hit_wire_size + pk_wire_size;