From: Miek Gieben Date: Mon, 21 Feb 2005 12:31:56 +0000 (+0000) Subject: another conversion routine X-Git-Tag: release-0.50~398 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ee7d27f9267d09c0010a30684089da9a5a3c2f3;p=thirdparty%2Fldns.git another conversion routine --- diff --git a/TODO b/TODO index 1b82af5e..f9772469 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ Current TODO list Errors: o names starting with a dot .www or not handled correctly -o absolute/relative dnames??? Implementation: @@ -20,8 +19,6 @@ o network code sane default values o DNSSEC o Make a shared library and test compile some programs -o Higherlevel functions, like search for this RR with this name in - these RRlists. Give me all RR's of this zone with this type. etc. Documentation: -------------- @@ -35,7 +32,8 @@ o Mailing list(s) To ponder and discuss: ---------------------- -o Drop ldns_resolver_set_configured() ? -o char * <=> uint8_t * -o Second layer (higher level convenience functions)? +o char * <=> uint8_t * (partly done? Erik?) +o Second layer (higher level convenience functions)? (Later) + o Higherlevel functions, like search for this RR with this name in + these RRlists. Give me all RR's of this zone with this type. etc. o Dependency on openSSL? diff --git a/host2str.c b/host2str.c index 760288a7..37398682 100644 --- a/host2str.c +++ b/host2str.c @@ -1008,7 +1008,7 @@ ldns_pkt2str(ldns_pkt *pkt) { char *result = NULL; /* XXX MAXTEXTPACKET?? */ - ldns_buffer *tmp_buffer = ldns_buffer_new(MAX_PACKET_SIZE); + ldns_buffer *tmp_buffer = ldns_buffer_new(MAX_PACKETLEN); if (ldns_pkt2buffer_str(tmp_buffer, pkt) == LDNS_STATUS_OK) { /* export and return string, destroy rest */ diff --git a/host2wire.c b/host2wire.c index e6a3778e..9be10a86 100644 --- a/host2wire.c +++ b/host2wire.c @@ -174,7 +174,7 @@ ldns_pkt2buffer_wire(ldns_buffer *buffer, ldns_pkt *packet) uint8_t * ldns_rdf2wire(ldns_rdf *rdf, size_t *result_size) { - ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKETLEN); uint8_t *result = NULL; *result_size = 0; if (ldns_rdf2buffer_wire(buffer, rdf) == LDNS_STATUS_OK) { @@ -199,7 +199,7 @@ ldns_rdf2wire(ldns_rdf *rdf, size_t *result_size) uint8_t * ldns_rr2wire(ldns_rr *rr, int section, size_t *result_size) { - ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKETLEN); uint8_t *result = NULL; *result_size = 0; if (ldns_rr2buffer_wire(buffer, rr, section) == LDNS_STATUS_OK) { @@ -221,7 +221,7 @@ ldns_rr2wire(ldns_rr *rr, int section, size_t *result_size) uint8_t * ldns_pkt2wire(ldns_pkt *packet, size_t *result_size) { - ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKETLEN); uint8_t *result = NULL; *result_size = 0; if (ldns_pkt2buffer_wire(buffer, packet) == LDNS_STATUS_OK) { diff --git a/ldns/error.h b/ldns/error.h index 02bf0fef..20d45abc 100644 --- a/ldns/error.h +++ b/ldns/error.h @@ -30,7 +30,8 @@ enum ldns_enum_status LDNS_STATUS_INVALID_IP4, LDNS_STATUS_INVALID_IP6, LDNS_STATUS_INVALID_STR, - LDNS_STATUS_INVALID_B64 + LDNS_STATUS_INVALID_B64, + LDNS_STATUS_INVALID_HEX }; typedef enum ldns_enum_status ldns_status; diff --git a/ldns/packet.h b/ldns/packet.h index 074517ee..74441b34 100644 --- a/ldns/packet.h +++ b/ldns/packet.h @@ -171,7 +171,7 @@ void ldns_pkt_free(ldns_pkt *packet); ldns_pkt * ldns_pkt_query_new_frm_str(const char *, ldns_rr_type, ldns_rr_class, uint16_t); ldns_pkt * ldns_pkt_query_new(ldns_rdf *, ldns_rr_type, ldns_rr_class, uint16_t); -#define MAX_PACKET_SIZE 65535 +#define MAX_PACKETLEN 65535 /* allow flags to be given to mk_query */ #define LDNS_QR 1 diff --git a/ldns/rdata.h b/ldns/rdata.h index c90862e1..477319b5 100644 --- a/ldns/rdata.h +++ b/ldns/rdata.h @@ -17,6 +17,7 @@ #include #include +#define MAX_RDFLEN 65535 /** * The different types of RDATA fields. diff --git a/ldns/rr.h b/ldns/rr.h index 38122892..37d0341e 100644 --- a/ldns/rr.h +++ b/ldns/rr.h @@ -168,11 +168,11 @@ enum ldns_enum_rr_type typedef enum ldns_enum_rr_type ldns_rr_type; /** Maximum length of a dname label */ -#define MAXLABELLEN 63 +#define MAX_LABELLEN 63 /** Maximum length of a complete dname */ -#define MAXDOMAINLEN 255 +#define MAX_DOMAINLEN 255 /** Maximum number of pointers in 1 dname */ -#define MAXPOINTERS 65535 +#define MAX_POINTERS 65535 /** * \brief Resource Record type diff --git a/ldns/str2host.h b/ldns/str2host.h index 3691550f..3a5fd65b 100644 --- a/ldns/str2host.h +++ b/ldns/str2host.h @@ -31,7 +31,7 @@ ldns_status ldns_str2rdf_service(ldns_rdf **, const char *); ldns_status ldns_str2rdf_loc(ldns_rdf **, const char *); ldns_status ldns_str2rdf_wks(ldns_rdf **, const char *); ldns_status ldns_str2rdf_nsap(ldns_rdf **, const char *); - +ldns_status ldns_str2rdf_nsap(ldns_rdf **, const char *); ldns_status ldns_str2rdf_dname(ldns_rdf **, const char *); -#endif +#endif /* _LDNS_2HOST_H */ diff --git a/libdns.vim b/libdns.vim index 5023855b..3b0e77c1 100644 --- a/libdns.vim +++ b/libdns.vim @@ -45,6 +45,7 @@ syn keyword ldnsConstant LDNS_RDF_TYPE_SERVICE syn keyword ldnsConstant LDNS_RDF_TYPE_LOC syn keyword ldnsConstant LDNS_RDF_TYPE_WKS syn keyword ldnsConstant LDNS_RDF_TYPE_NSAP +syn keyword ldnsConstant MAX_RDFLEN " ldns/dns.h syn keyword ldnsConstant LDNS_PORT @@ -67,6 +68,8 @@ syn keyword ldnsConstant LDNS_SECTION_QUESTION syn keyword ldnsConstant LDNS_SECTION_ANSWER syn keyword ldnsConstant LDNS_SECTION_AUTHORITY syn keyword ldnsConstant LDNS_SECTION_ADDITIONAL +syn keyword ldnsConstant MAX_PACKETLEN + " dns/error.h syn keyword ldnsMacro LDNS_STATUS_OK @@ -142,8 +145,8 @@ syn keyword ldnsConstant LDNS_RR_TYPE_AXFR syn keyword ldnsConstant LDNS_RR_TYPE_MAILB syn keyword ldnsConstant LDNS_RR_TYPE_MAILA syn keyword ldnsConstant LDNS_RR_TYPE_ANY -syn keyword ldnsConstant MAXLABELLEN -syn keyword ldnsConstant MAXDOMAINLEN +syn keyword ldnsConstant MAX_LABELLEN +syn keyword ldnsConstant MAX_DOMAINLEN syn keyword ldnsConstant LDNS_RR_COMPRESS syn keyword ldnsConstant LDNS_RR_NO_COMPRESS diff --git a/net.c b/net.c index 13108bb9..57531a16 100644 --- a/net.c +++ b/net.c @@ -69,7 +69,7 @@ ldns_send(ldns_resolver *r, ldns_pkt *query_pkt) ns_array = ldns_resolver_nameservers(r); reply = NULL; ns_len = 0; - qb = ldns_buffer_new(MAX_PACKET_SIZE); + qb = ldns_buffer_new(MAX_PACKETLEN); if (ldns_pkt2buffer_wire(qb, query_pkt) != LDNS_STATUS_OK) { printf("could not convert to wire fmt\n"); @@ -166,13 +166,13 @@ ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to } /* wait for an response*/ - answer = XMALLOC(uint8_t, MAX_PACKET_SIZE); + answer = XMALLOC(uint8_t, MAX_PACKETLEN); if (!answer) { printf("respons alloc error\n"); return NULL; } - bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0); + bytes = recv(sockfd, answer, MAX_PACKETLEN, 0); close(sockfd); @@ -262,7 +262,7 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to } /* wait for an response*/ - answer = XMALLOC(uint8_t, MAX_PACKET_SIZE); + answer = XMALLOC(uint8_t, MAX_PACKETLEN); if (!answer) { printf("respons alloc error\n"); return NULL; @@ -272,7 +272,7 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to we must be sure that we receive those */ total_bytes = 0; while (total_bytes < 2) { - bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0); + bytes = recv(sockfd, answer, MAX_PACKETLEN, 0); if (bytes == -1) { if (errno == EAGAIN) { fprintf(stderr, "socket timeout\n"); @@ -290,7 +290,7 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to /* if we did not receive the whole packet in one tcp packet, we must recv() on */ while (total_bytes < (ssize_t) (answer_size + 2)) { - bytes = recv(sockfd, answer+total_bytes, (size_t) (MAX_PACKET_SIZE-total_bytes), 0); + bytes = recv(sockfd, answer + total_bytes, (size_t) (MAX_PACKETLEN - total_bytes), 0); if (bytes == -1) { if (errno == EAGAIN) { fprintf(stderr, "socket timeout\n"); diff --git a/run-test0.c b/run-test0.c index 11f55cf8..b8fb9bdf 100644 --- a/run-test0.c +++ b/run-test0.c @@ -57,7 +57,7 @@ main(void) rr = ldns_rr_new(); - rdf_data = (uint8_t *) XMALLOC(char, MAXDOMAINLEN); + rdf_data = (uint8_t *) XMALLOC(char, MAX_DOMAINLEN); rdf_data[0] = 3; memcpy(rdf_data+1, "www", 3); rdf_data[4] = 4; diff --git a/str2host.c b/str2host.c index 8d867cdf..0b62e0c4 100644 --- a/str2host.c +++ b/str2host.c @@ -150,10 +150,10 @@ ldns_str2rdf_dname(ldns_rdf **d, const char *str) size_t len; uint8_t *s,*p,*q, *pq, val, label_len; - uint8_t buf[MAXDOMAINLEN + 1]; + uint8_t buf[MAX_DOMAINLEN + 1]; len = strlen((char*)str); - if (len > MAXDOMAINLEN) { + if (len > MAX_DOMAINLEN) { return LDNS_STATUS_DOMAINNAME_OVERFLOW; } if (0 == len) { @@ -173,7 +173,7 @@ ldns_str2rdf_dname(ldns_rdf **d, const char *str) switch (*s) { case '.': /* todo: check length (overflow und <1 */ - if (label_len > MAXLABELLEN) { + if (label_len > MAX_LABELLEN) { return LDNS_STATUS_LABEL_OVERFLOW; } if (label_len == 0) { @@ -309,15 +309,43 @@ ldns_str2rdf_b64(ldns_rdf **rd, const char *str) } /** - * convert .... into wireformat + * convert a hex value into wireformat * \param[in] rd the rdf where to put the data * \param[in] str the string to be converted * \return ldns_status */ ldns_status -ldns_str2rdf_hex(ldns_rdf **ATTR_UNUSED(rd), const char *ATTR_UNUSED(str)) +ldns_str2rdf_hex(ldns_rdf **rd, const char *str) { - abort(); + uint8_t *t; + int i; + size_t len; + + len = strlen(str); + + if (len % 2 != 0) { + return LDNS_STATUS_INVALID_HEX; + } else if (len > MAX_RDFLEN * 2) { + return LDNS_STATUS_LABEL_OVERFLOW; + } else { + t = XMALLOC(uint8_t, (len / 2)); + + /* Now process octet by octet... */ + while (*str) { + *t = 0; + for (i = 16; i >= 1; i -= 15) { + if (isxdigit(*str)) { + *t += hexdigit_to_int(*str) * i; + } else { + return LDNS_STATUS_ERR; + } + ++str; + } + ++t; + } + *rd = ldns_rdf_new_frm_data(len / 2, LDNS_RDF_TYPE_HEX, t); + } + return LDNS_STATUS_OK; } /** diff --git a/wire2host.c b/wire2host.c index 8744edb3..6fb2c5cf 100644 --- a/wire2host.c +++ b/wire2host.c @@ -145,7 +145,7 @@ ldns_wire2dname(ldns_rdf **dname, const uint8_t *wire, size_t max, size_t *pos) size_t dname_pos = 0; size_t uncompressed_length = 0; size_t compression_pos = 0; - uint8_t tmp_dname[MAXDOMAINLEN]; + uint8_t tmp_dname[MAX_DOMAINLEN]; uint8_t *dname_ar; unsigned int pointer_count = 0; @@ -172,13 +172,13 @@ ldns_wire2dname(ldns_rdf **dname, const uint8_t *wire, size_t max, size_t *pos) return LDNS_STATUS_INVALID_POINTER; } else if (pointer_target > max) { return LDNS_STATUS_INVALID_POINTER; - } else if (pointer_count > MAXPOINTERS) { + } else if (pointer_count > MAX_POINTERS) { return LDNS_STATUS_INVALID_POINTER; } *pos = pointer_target; label_size = wire[*pos]; } - if (label_size > MAXLABELLEN) { + if (label_size > MAX_LABELLEN) { return LDNS_STATUS_LABEL_OVERFLOW; } if (*pos + label_size > max) {