4 * conversion routines from the presentation format
7 * a Net::DNS like library for C
9 * (c) NLnet Labs, 2004-2006
11 * See the file LICENSE for the license
13 #include <ldns/config.h>
15 #include <ldns/ldns.h>
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>
36 ldns_str2rdf_int16(ldns_rdf **rd, const char *shortstr)
40 r = LDNS_MALLOC(uint16_t);
42 *r = htons((uint16_t)strtol((char *)shortstr, &end, 10));
46 return LDNS_STATUS_INVALID_INT;
48 *rd = ldns_rdf_new_frm_data(
49 LDNS_RDF_TYPE_INT16, sizeof(uint16_t), r);
51 return LDNS_STATUS_OK;
56 ldns_str2rdf_time(ldns_rdf **rd, const char *time)
58 /* convert a time YYYYDDMMHHMMSS to wireformat */
64 /* Try to scan the time... */
65 r = (uint16_t*)LDNS_MALLOC(uint32_t);
67 memset(&tm, 0, sizeof(tm));
69 if (strlen(time) == 14 &&
70 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
75 if (tm.tm_year < 70) {
78 if (tm.tm_mon < 0 || tm.tm_mon > 11) {
81 if (tm.tm_mday < 1 || tm.tm_mday > 31) {
85 if (tm.tm_hour < 0 || tm.tm_hour > 23) {
89 if (tm.tm_min < 0 || tm.tm_min > 59) {
93 if (tm.tm_sec < 0 || tm.tm_sec > 59) {
97 l = htonl(mktime_from_utc(&tm));
98 memcpy(r, &l, sizeof(uint32_t));
99 *rd = ldns_rdf_new_frm_data(
100 LDNS_RDF_TYPE_TIME, sizeof(uint32_t), r);
102 return LDNS_STATUS_OK;
104 /* handle it as 32 bits timestamp */
105 l = htonl((uint32_t)strtol((char*)time, &end, 10));
108 return LDNS_STATUS_ERR;
110 memcpy(r, &l, sizeof(uint32_t));
111 *rd = ldns_rdf_new_frm_data(
112 LDNS_RDF_TYPE_INT32, sizeof(uint32_t), r);
114 return LDNS_STATUS_OK;
120 return LDNS_STATUS_INVALID_TIME;
124 ldns_str2rdf_nsec3_salt(ldns_rdf **rd, const char *salt_str)
133 salt_length_str = strlen(salt_str);
134 if (salt_length_str == 1 && salt_str[0] == '-') {
136 } else if (salt_length_str % 2 != 0) {
137 return LDNS_STATUS_INVALID_HEX;
139 if (salt_length_str > 512) {
140 return LDNS_STATUS_INVALID_HEX;
143 salt = LDNS_XMALLOC(uint8_t, salt_length_str / 2);
144 for (c = 0; c < salt_length_str; c += 2) {
145 if (isxdigit((int) salt_str[c]) && isxdigit((int) salt_str[c+1])) {
146 salt[c/2] = (uint8_t) ldns_hexdigit_to_int(salt_str[c]) * 16 +
147 ldns_hexdigit_to_int(salt_str[c+1]);
150 return LDNS_STATUS_INVALID_HEX;
153 salt_length = (uint8_t) (salt_length_str / 2);
155 data = LDNS_XMALLOC(uint8_t, 1 + salt_length);
156 data[0] = salt_length;
157 memcpy(&data[1], salt, salt_length);
158 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_NSEC3_SALT, 1 + salt_length, data);
162 return LDNS_STATUS_OK;
166 ldns_str2rdf_period(ldns_rdf **rd,const char *period)
171 /* Allocate required space... */
172 p = ldns_str2period(period, &end);
175 return LDNS_STATUS_ERR;
177 p = (uint32_t) htonl(p);
178 *rd = ldns_rdf_new_frm_data(
179 LDNS_RDF_TYPE_PERIOD, sizeof(uint32_t), &p);
181 return LDNS_STATUS_OK;
185 ldns_str2rdf_int32(ldns_rdf **rd, const char *longstr)
191 r = (uint16_t*)LDNS_MALLOC(uint32_t);
192 errno = 0; /* must set to zero before call,
193 note race condition on errno */
195 l = htonl((uint32_t)strtol((char*)longstr, &end, 10));
196 else l = htonl((uint32_t)strtoul((char*)longstr, &end, 10));
200 return LDNS_STATUS_ERR;
202 if (errno == ERANGE) {
204 return LDNS_STATUS_SYNTAX_INTEGER_OVERFLOW;
206 memcpy(r, &l, sizeof(uint32_t));
207 *rd = ldns_rdf_new_frm_data(
208 LDNS_RDF_TYPE_INT32, sizeof(uint32_t), r);
210 return LDNS_STATUS_OK;
215 ldns_str2rdf_int8(ldns_rdf **rd, const char *bytestr)
220 r = LDNS_MALLOC(uint8_t);
222 *r = (uint8_t)strtol((char*)bytestr, &end, 10);
226 return LDNS_STATUS_ERR;
228 *rd = ldns_rdf_new_frm_data(
229 LDNS_RDF_TYPE_INT8, sizeof(uint8_t), r);
231 return LDNS_STATUS_OK;
237 * Checks whether the escaped value at **s is an octal value or
238 * a 'normally' escaped character (and not eos)
240 * The string pointer at *s is increased by either 0 (on error), 1 (on
241 * normal escapes), or 3 (on octals)
243 * Returns the number of bytes read from the escaped string, or
247 parse_escape(uint8_t *s, uint8_t *q) {
249 if (strlen((char *)s) > 3 &&
250 isdigit((int) s[1]) &&
251 isdigit((int) s[2]) &&
252 isdigit((int) s[3])) {
253 /* cast this so it fits */
254 val = (uint8_t) ldns_hexdigit_to_int((char) s[1]) * 100 +
255 ldns_hexdigit_to_int((char) s[2]) * 10 +
256 ldns_hexdigit_to_int((char) s[3]);
261 if (*s == '\0' || isdigit((int) *s)) {
262 /* apparently the string terminator
263 * or a digit has been escaped...
273 * No special care is taken, all dots are translated into
275 * Could be made more efficient....we do 3 memcpy's in total...
278 ldns_str2rdf_dname(ldns_rdf **d, const char *str)
283 uint8_t *s, *q, *pq, label_len;
284 uint8_t buf[LDNS_MAX_DOMAINLEN + 1];
287 len = strlen((char*)str);
288 /* octet representation can make strings a lot longer than actual length */
289 if (len > LDNS_MAX_DOMAINLEN * 4) {
290 return LDNS_STATUS_DOMAINNAME_OVERFLOW;
293 return LDNS_STATUS_DOMAINNAME_UNDERFLOW;
297 if (1 == len && *str == '.') {
298 *d = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME, 1, "\0");
299 return LDNS_STATUS_OK;
302 /* get on with the rest */
304 /* s is on the current character in the string
305 * pq points to where the labellength is going to go
306 * label_len keeps track of the current label's length
307 * q builds the dname inside the buf array
313 for (s = (uint8_t *)str; *s; s++, q++) {
314 if (q > buf + LDNS_MAX_DOMAINLEN) {
315 return LDNS_STATUS_DOMAINNAME_OVERFLOW;
320 if (label_len > LDNS_MAX_LABELLEN) {
321 return LDNS_STATUS_LABEL_OVERFLOW;
323 if (label_len == 0) {
324 return LDNS_STATUS_EMPTY_LABEL;
326 len += label_len + 1;
332 /* octet value or literal char */
333 esc = parse_escape(s, q);
338 return LDNS_STATUS_SYNTAX_BAD_ESCAPE;
347 /* add root label if last char was not '.' */
348 if (!ldns_dname_str_absolute(str)) {
349 if (q > buf + LDNS_MAX_DOMAINLEN) {
350 return LDNS_STATUS_DOMAINNAME_OVERFLOW;
352 if (label_len > LDNS_MAX_LABELLEN) {
353 return LDNS_STATUS_LABEL_OVERFLOW;
355 if (label_len == 0) { /* label_len 0 but not . at end? */
356 return LDNS_STATUS_EMPTY_LABEL;
358 len += label_len + 1;
364 *d = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_DNAME, len, buf);
365 return LDNS_STATUS_OK;
369 ldns_str2rdf_a(ldns_rdf **rd, const char *str)
372 if (inet_pton(AF_INET, (char*)str, &address) != 1) {
373 return LDNS_STATUS_INVALID_IP4;
375 *rd = ldns_rdf_new_frm_data(
376 LDNS_RDF_TYPE_A, sizeof(address), &address);
378 return LDNS_STATUS_OK;
382 ldns_str2rdf_aaaa(ldns_rdf **rd, const char *str)
384 uint8_t address[LDNS_IP6ADDRLEN + 1];
386 if (inet_pton(AF_INET6, (char*)str, address) != 1) {
387 return LDNS_STATUS_INVALID_IP6;
389 *rd = ldns_rdf_new_frm_data(
390 LDNS_RDF_TYPE_AAAA, sizeof(address) - 1, &address);
392 return LDNS_STATUS_OK;
396 ldns_str2rdf_str(ldns_rdf **rd, const char *str)
399 size_t i, str_i, esc_i;
401 if (strlen(str) > 255) {
402 return LDNS_STATUS_INVALID_STR;
405 data = LDNS_XMALLOC(uint8_t, strlen(str) + 1);
407 for (str_i = 0; str_i < strlen(str); str_i++) {
408 if (str[str_i] == '\\') {
409 /* octet value or literal char */
410 esc_i = (size_t) parse_escape((uint8_t*) &str[str_i], (uint8_t*) &data[i]);
413 return LDNS_STATUS_SYNTAX_BAD_ESCAPE;
417 data[i] = (uint8_t) str[str_i];
422 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_STR, i, data);
424 return LDNS_STATUS_OK;
428 ldns_str2rdf_apl(ldns_rdf **rd, const char *str)
430 const char *my_str = str;
437 uint8_t afdlength = 0;
445 /* [!]afi:address/prefix */
446 if (strlen(my_str) < 2) {
447 return LDNS_STATUS_INVALID_STR;
450 if (my_str[0] == '!') {
457 family = (uint16_t) atoi(my_str);
459 my_str = strchr(my_str, ':') + 1;
461 /* need ip addr and only ip addr for inet_pton */
462 ip_str_len = (size_t) (strchr(my_str, '/') - my_str);
463 my_ip_str = LDNS_XMALLOC(char, ip_str_len + 1);
464 strncpy(my_ip_str, my_str, ip_str_len + 1);
465 my_ip_str[ip_str_len] = '\0';
469 afdpart = LDNS_XMALLOC(uint8_t, 4);
470 if (inet_pton(AF_INET, my_ip_str, afdpart) == 0) {
471 return LDNS_STATUS_INVALID_STR;
473 for (i = 0; i < 4; i++) {
474 if (afdpart[i] != 0) {
478 } else if (family == 2) {
480 afdpart = LDNS_XMALLOC(uint8_t, 16);
481 if (inet_pton(AF_INET6, my_ip_str, afdpart) == 0) {
482 return LDNS_STATUS_INVALID_STR;
484 for (i = 0; i < 16; i++) {
485 if (afdpart[i] != 0) {
491 LDNS_FREE(my_ip_str);
492 return LDNS_STATUS_INVALID_STR;
495 my_str = strchr(my_str, '/') + 1;
496 prefix = (uint8_t) atoi(my_str);
498 data = LDNS_XMALLOC(uint8_t, 4 + afdlength);
499 ldns_write_uint16(data, family);
503 /* set bit 1 of byte 3 */
504 data[3] = data[3] | 0x80;
507 memcpy(data + 4, afdpart, afdlength);
509 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_APL, afdlength + 4, data);
512 LDNS_FREE(my_ip_str);
514 return LDNS_STATUS_OK;
518 ldns_str2rdf_b64(ldns_rdf **rd, const char *str)
523 buffer = LDNS_XMALLOC(uint8_t, ldns_b64_ntop_calculate_size(strlen(str)));
525 i = (uint16_t)ldns_b64_pton((const char*)str, buffer,
526 ldns_b64_ntop_calculate_size(strlen(str)));
529 return LDNS_STATUS_INVALID_B64;
531 *rd = ldns_rdf_new_frm_data(
532 LDNS_RDF_TYPE_B64, (uint16_t) i, buffer);
536 return LDNS_STATUS_OK;
540 ldns_str2rdf_b32_ext(ldns_rdf **rd, const char *str)
544 /* first byte contains length of actual b32 data */
545 uint8_t len = ldns_b32_pton_calculate_size(strlen(str));
546 buffer = LDNS_XMALLOC(uint8_t, len + 1);
549 i = ldns_b32_pton_extended_hex((const char*)str, strlen(str), buffer + 1,
550 ldns_b32_ntop_calculate_size(strlen(str)));
552 return LDNS_STATUS_INVALID_B32_EXT;
554 *rd = ldns_rdf_new_frm_data(
555 LDNS_RDF_TYPE_B32_EXT, (uint16_t) i + 1, buffer);
559 return LDNS_STATUS_OK;
563 ldns_str2rdf_hex(ldns_rdf **rd, const char *str)
571 if (len > LDNS_MAX_RDFLEN * 2) {
572 return LDNS_STATUS_LABEL_OVERFLOW;
574 t = LDNS_XMALLOC(uint8_t, (len / 2) + 1);
576 /* Now process octet by octet... */
579 if (isspace((int) *str)) {
582 for (i = 16; i >= 1; i -= 15) {
583 while (*str && isspace((int) *str)) { str++; }
585 if (isxdigit((int) *str)) {
586 *t += ldns_hexdigit_to_int(*str) * i;
588 return LDNS_STATUS_ERR;
596 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX,
597 (size_t) (t - t_orig),
601 return LDNS_STATUS_OK;
605 ldns_str2rdf_nsec(ldns_rdf **rd, const char *str)
607 const char *delimiters = "\n\t ";
608 char *token = LDNS_XMALLOC(char, LDNS_MAX_RDFLEN);
609 ldns_buffer *str_buf;
612 size_t type_count = 0;
613 ldns_rr_type type_list[1024];
614 if(!token) return LDNS_STATUS_MEM_ERR;
617 return LDNS_STATUS_NULL;
620 str_buf = LDNS_MALLOC(ldns_buffer);
623 return LDNS_STATUS_MEM_ERR;
625 ldns_buffer_new_frm_data(str_buf, (char *)str, strlen(str));
626 if(ldns_buffer_status(str_buf) != LDNS_STATUS_OK) {
629 return LDNS_STATUS_MEM_ERR;
632 while ((c = ldns_bget_token(str_buf, token, delimiters, LDNS_MAX_RDFLEN)) != -1 && c != 0) {
633 cur_type = ldns_get_rr_type_by_name(token);
634 type_list[type_count] = cur_type;
638 *rd = ldns_dnssec_create_nsec_bitmap(type_list,
643 ldns_buffer_free(str_buf);
644 return LDNS_STATUS_OK;
648 ldns_str2rdf_type(ldns_rdf **rd, const char *str)
651 type = htons(ldns_get_rr_type_by_name(str));
652 /* ldns_rr_type is a 16 bit value */
653 *rd = ldns_rdf_new_frm_data(
654 LDNS_RDF_TYPE_TYPE, sizeof(uint16_t), &type);
655 return LDNS_STATUS_OK;
659 ldns_str2rdf_class(ldns_rdf **rd, const char *str)
662 klass = htons(ldns_get_rr_class_by_name(str));
663 /* class is 16 bit */
664 *rd = ldns_rdf_new_frm_data(
665 LDNS_RDF_TYPE_CLASS, sizeof(uint16_t), &klass);
666 return LDNS_STATUS_OK;
669 /* An certificate alg field can either be specified as a 8 bits number
670 * or by its symbolic name. Handle both
673 ldns_str2rdf_cert_alg(ldns_rdf **rd, const char *str)
675 ldns_lookup_table *lt;
678 lt = ldns_lookup_by_name(ldns_cert_algorithms, str);
682 ldns_write_uint16(idd, (uint16_t) lt->id);
683 *rd = ldns_rdf_new_frm_data(
684 LDNS_RDF_TYPE_INT16, sizeof(uint16_t), idd);
686 st = LDNS_STATUS_ERR;
689 /* try as-is (a number) */
690 st = ldns_str2rdf_int16(rd, str);
691 if (st == LDNS_STATUS_OK &&
692 ldns_rdf2native_int16(*rd) == 0) {
693 st = LDNS_STATUS_CERT_BAD_ALGORITHM;
700 /* An alg field can either be specified as a 8 bits number
701 * or by its symbolic name. Handle both
704 ldns_str2rdf_alg(ldns_rdf **rd, const char *str)
706 ldns_lookup_table *lt;
709 lt = ldns_lookup_by_name(ldns_algorithms, str);
713 /* it was given as a integer */
714 *rd = ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8, (uint8_t) lt->id);
716 st = LDNS_STATUS_ERR;
719 /* try as-is (a number) */
720 st = ldns_str2rdf_int8(rd, str);
726 ldns_str2rdf_unknown(ldns_rdf **rd, const char *str)
728 /* this should be caught in an earlier time (general str2host for
732 return LDNS_STATUS_NOT_IMPL;
736 ldns_str2rdf_tsig(ldns_rdf **rd, const char *str)
738 /* there is no strign representation for TSIG rrs */
741 return LDNS_STATUS_NOT_IMPL;
745 ldns_str2rdf_service(ldns_rdf **rd, const char *str)
747 /* is this used? is this actually WKS? or SRV? */
750 return LDNS_STATUS_NOT_IMPL;
754 loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e)
756 /* read <digits>[.<digits>][mM] */
757 /* into mantissa exponent format for LOC type */
758 uint32_t meters = 0, cm = 0, val;
759 while (isblank(*my_str)) {
762 meters = (uint32_t)strtol(my_str, &my_str, 10);
763 if (*my_str == '.') {
765 cm = (uint32_t)strtol(my_str, &my_str, 10);
782 if (*my_str == 'm' || *my_str == 'M') {
790 ldns_str2rdf_loc(ldns_rdf **rd, const char *str)
792 uint32_t latitude = 0;
793 uint32_t longitude = 0;
794 uint32_t altitude = 0;
797 uint32_t equator = (uint32_t) ldns_power(2, 31);
801 uint8_t size_b = 1, size_e = 2;
802 uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
803 uint8_t vert_pre_b = 1, vert_pre_e = 3;
809 char *my_str = (char *) str;
811 /* only support version 0 */
812 if (isdigit((int) *my_str)) {
813 h = (uint32_t) strtol(my_str, &my_str, 10);
815 return LDNS_STATUS_INVALID_STR;
818 while (isblank((int) *my_str)) {
822 if (isdigit((int) *my_str)) {
823 m = (uint32_t) strtol(my_str, &my_str, 10);
824 } else if (*my_str == 'N' || *my_str == 'S') {
827 return LDNS_STATUS_INVALID_STR;
830 while (isblank((int) *my_str)) {
834 if (isdigit((int) *my_str)) {
835 s = strtod(my_str, &my_str);
838 while (isblank((int) *my_str)) {
842 if (*my_str == 'N') {
844 } else if (*my_str == 'S') {
847 return LDNS_STATUS_INVALID_STR;
854 /* add a little to make floor in conversion a round */
856 latitude = (uint32_t) s;
857 latitude += 1000 * 60 * m;
858 latitude += 1000 * 60 * 60 * h;
860 latitude = equator + latitude;
862 latitude = equator - latitude;
864 while (isblank(*my_str)) {
868 if (isdigit((int) *my_str)) {
869 h = (uint32_t) strtol(my_str, &my_str, 10);
871 return LDNS_STATUS_INVALID_STR;
874 while (isblank((int) *my_str)) {
878 if (isdigit((int) *my_str)) {
879 m = (uint32_t) strtol(my_str, &my_str, 10);
880 } else if (*my_str == 'E' || *my_str == 'W') {
883 return LDNS_STATUS_INVALID_STR;
886 while (isblank(*my_str)) {
890 if (isdigit((int) *my_str)) {
891 s = strtod(my_str, &my_str);
895 while (isblank(*my_str)) {
899 if (*my_str == 'E') {
901 } else if (*my_str == 'W') {
904 return LDNS_STATUS_INVALID_STR;
911 /* add a little to make floor in conversion a round */
913 longitude = (uint32_t) s;
914 longitude += 1000 * 60 * m;
915 longitude += 1000 * 60 * 60 * h;
918 longitude += equator;
920 longitude = equator - longitude;
923 altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
925 if (*my_str == 'm' || *my_str == 'M') {
929 if (strlen(my_str) > 0) {
930 if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
931 return LDNS_STATUS_INVALID_STR;
934 if (strlen(my_str) > 0) {
935 if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
936 return LDNS_STATUS_INVALID_STR;
939 if (strlen(my_str) > 0) {
940 if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
941 return LDNS_STATUS_INVALID_STR;
944 data = LDNS_XMALLOC(uint8_t, 16);
947 data[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
948 data[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
949 data[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
950 ldns_write_uint32(data + 4, latitude);
951 ldns_write_uint32(data + 8, longitude);
952 ldns_write_uint32(data + 12, altitude);
954 *rd = ldns_rdf_new_frm_data(
955 LDNS_RDF_TYPE_LOC, 16, data);
958 return LDNS_STATUS_OK;
962 ldns_str2rdf_wks(ldns_rdf **rd, const char *str)
964 uint8_t *bitmap = NULL;
968 struct protoent *proto = NULL;
969 struct servent *serv = NULL;
972 ldns_buffer *str_buf;
974 char *proto_str = NULL;
977 token = LDNS_XMALLOC(char, 50);
978 else token = LDNS_XMALLOC(char, strlen(str)+2);
979 if(!token) return LDNS_STATUS_MEM_ERR;
981 str_buf = LDNS_MALLOC(ldns_buffer);
982 if(!str_buf) {LDNS_FREE(token); return LDNS_STATUS_MEM_ERR;}
983 ldns_buffer_new_frm_data(str_buf, (char *)str, strlen(str));
984 if(ldns_buffer_status(str_buf) != LDNS_STATUS_OK) {
987 return LDNS_STATUS_MEM_ERR;
990 while(ldns_bget_token(str_buf, token, "\t\n ", strlen(str)) > 0) {
992 proto_str = strdup(token);
996 ldns_buffer_free(str_buf);
997 return LDNS_STATUS_INVALID_STR;
1000 serv = getservbyname(token, proto_str);
1002 serv_port = (int) ntohs((uint16_t) serv->s_port);
1004 serv_port = atoi(token);
1006 if (serv_port / 8 >= bm_len) {
1007 uint8_t *b2 = LDNS_XREALLOC(bitmap, uint8_t, (serv_port / 8) + 1);
1011 ldns_buffer_free(str_buf);
1013 return LDNS_STATUS_INVALID_STR;
1016 /* set to zero to be sure */
1017 for (; bm_len <= serv_port / 8; bm_len++) {
1021 ldns_set_bit(bitmap + (serv_port / 8), 7 - (serv_port % 8), true);
1025 if (!proto_str || !bitmap) {
1028 ldns_buffer_free(str_buf);
1030 return LDNS_STATUS_INVALID_STR;
1033 data = LDNS_XMALLOC(uint8_t, bm_len + 1);
1036 ldns_buffer_free(str_buf);
1039 return LDNS_STATUS_INVALID_STR;
1042 proto = getprotobyname(proto_str);
1044 data[0] = (uint8_t) proto->p_proto;
1045 } else if (proto_str) {
1046 data[0] = (uint8_t) atoi(proto_str);
1050 memcpy(data + 1, bitmap, (size_t) bm_len);
1052 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_WKS, (uint16_t) (bm_len + 1), data);
1056 ldns_buffer_free(str_buf);
1059 #ifdef HAVE_ENDSERVENT
1062 #ifdef HAVE_ENDPROTOENT
1066 if(!*rd) return LDNS_STATUS_MEM_ERR;
1068 return LDNS_STATUS_OK;
1072 ldns_str2rdf_nsap(ldns_rdf **rd, const char *str)
1075 char* nsap_str = (char*) str;
1077 /* just a hex string with optional dots? */
1078 if (str[0] != '0' || str[1] != 'x') {
1079 return LDNS_STATUS_INVALID_STR;
1082 for (i=0; i < len; i++) {
1083 if (nsap_str[i] == '.')
1086 return ldns_str2rdf_hex(rd, str+2);
1091 ldns_str2rdf_atma(ldns_rdf **rd, const char *str)
1094 char* atma_str = (char*) str;
1097 /* just a hex string with optional dots? */
1099 for (i=0; i < len; i++) {
1100 if (atma_str[i] == '.')
1103 status = ldns_str2rdf_hex(rd, str);
1104 if (status != LDNS_STATUS_OK) {
1105 ; /* probably in e.164 format than */
1111 ldns_str2rdf_ipseckey(ldns_rdf **rd, const char *str)
1113 uint8_t precedence = 0;
1114 uint8_t gateway_type = 0;
1115 uint8_t algorithm = 0;
1116 char* gateway = NULL;
1117 char* publickey = NULL;
1119 ldns_buffer *str_buf;
1121 int token_count = 0;
1122 int ipseckey_len = 0;
1123 ldns_rdf* gateway_rdf = NULL;
1124 ldns_rdf* publickey_rdf = NULL;
1125 ldns_status status = LDNS_STATUS_OK;
1127 if(strlen(str) == 0)
1128 token = LDNS_XMALLOC(char, 256);
1129 else token = LDNS_XMALLOC(char, strlen(str)+2);
1130 if(!token) return LDNS_STATUS_MEM_ERR;
1132 str_buf = LDNS_MALLOC(ldns_buffer);
1133 if(!str_buf) {LDNS_FREE(token); return LDNS_STATUS_MEM_ERR;}
1134 ldns_buffer_new_frm_data(str_buf, (char *)str, strlen(str));
1135 if(ldns_buffer_status(str_buf) != LDNS_STATUS_OK) {
1138 return LDNS_STATUS_MEM_ERR;
1140 while(ldns_bget_token(str_buf, token, "\t\n ", strlen(str)) > 0) {
1141 switch (token_count) {
1143 precedence = atoi(token);
1146 gateway_type = atoi(token);
1149 algorithm = atoi(token);
1152 gateway = strdup(token);
1153 if (!gateway || (gateway_type == 0 &&
1154 (token[0] != '.' || token[1] != '\0'))) {
1157 ldns_buffer_free(str_buf);
1158 return LDNS_STATUS_INVALID_STR;
1162 publickey = strdup(token);
1166 ldns_buffer_free(str_buf);
1167 return LDNS_STATUS_INVALID_STR;
1173 if (!gateway || !publickey) {
1177 LDNS_FREE(publickey);
1179 ldns_buffer_free(str_buf);
1180 return LDNS_STATUS_INVALID_STR;
1183 if (gateway_type == 1) {
1184 status = ldns_str2rdf_a(&gateway_rdf, gateway);
1185 } else if (gateway_type == 2) {
1186 status = ldns_str2rdf_aaaa(&gateway_rdf, gateway);
1187 } else if (gateway_type == 3) {
1188 status = ldns_str2rdf_dname(&gateway_rdf, gateway);
1191 if (status != LDNS_STATUS_OK) {
1195 LDNS_FREE(publickey);
1197 ldns_buffer_free(str_buf);
1198 return LDNS_STATUS_INVALID_STR;
1201 status = ldns_str2rdf_b64(&publickey_rdf, publickey);
1203 if (status != LDNS_STATUS_OK) {
1207 LDNS_FREE(publickey);
1209 ldns_buffer_free(str_buf);
1210 if (gateway_rdf) ldns_rdf_free(gateway_rdf);
1211 return LDNS_STATUS_INVALID_STR;
1214 /* now copy all into one ipseckey rdf */
1216 ipseckey_len = 3 + ldns_rdf_size(gateway_rdf) + ldns_rdf_size(publickey_rdf);
1218 ipseckey_len = 3 + ldns_rdf_size(publickey_rdf);
1220 data = LDNS_XMALLOC(uint8_t, ipseckey_len);
1225 LDNS_FREE(publickey);
1227 ldns_buffer_free(str_buf);
1228 if (gateway_rdf) ldns_rdf_free(gateway_rdf);
1229 if (publickey_rdf) ldns_rdf_free(publickey_rdf);
1230 return LDNS_STATUS_MEM_ERR;
1233 data[0] = precedence;
1234 data[1] = gateway_type;
1235 data[2] = algorithm;
1239 ldns_rdf_data(gateway_rdf), ldns_rdf_size(gateway_rdf));
1240 memcpy(data + 3 + ldns_rdf_size(gateway_rdf),
1241 ldns_rdf_data(publickey_rdf), ldns_rdf_size(publickey_rdf));
1244 ldns_rdf_data(publickey_rdf), ldns_rdf_size(publickey_rdf));
1247 *rd = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_IPSECKEY, (uint16_t) ipseckey_len, data);
1252 LDNS_FREE(publickey);
1254 ldns_buffer_free(str_buf);
1255 ldns_rdf_free(gateway_rdf);
1256 ldns_rdf_free(publickey_rdf);
1258 if(!*rd) return LDNS_STATUS_MEM_ERR;
1259 return LDNS_STATUS_OK;