From: Jelte Jansen Date: Tue, 26 Jul 2005 08:46:08 +0000 (+0000) Subject: added some convenience functions X-Git-Tag: release-0.70~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=27fdfb4c0689d6e82fcdc6805ca5efc3c37d48be;p=thirdparty%2Fldns.git added some convenience functions --- diff --git a/Makefile.in b/Makefile.in index 06134467..fd2ba8e1 100644 --- a/Makefile.in +++ b/Makefile.in @@ -146,8 +146,6 @@ clean: rm -f tests/*.o rm -f $(PROG_TARGETS) rm -rf autom4te.cache/ - rm -f config.status - rm -f config.log rm -f tags realclean: clean docclean libclean diff --git a/ldns/str2host.h b/ldns/str2host.h index 004a083d..107863da 100644 --- a/ldns/str2host.h +++ b/ldns/str2host.h @@ -19,6 +19,16 @@ #include #include +/** + * Returns true if the given string represent an IPv4 address + */ +bool ldns_is_ipv4_addr(const char *str); + +/** + * Returns true if the given string represent an IPv6 address + */ +bool ldns_is_ipv6_addr(const char *str); + /** * convert a byte into wireformat * \param[in] rd the rdf where to put the data diff --git a/resolver.c b/resolver.c index 1cc2d545..105f9dc6 100644 --- a/resolver.c +++ b/resolver.c @@ -590,14 +590,14 @@ ldns_resolver_deep_free(ldns_resolver *res) for (i = 0; i < res->_searchlist_count; i++) { ldns_rdf_deep_free(res->_searchlist[i]); } + LDNS_FREE(res->_searchlist); } if (res->_nameservers) { for (i = 0; i < res->_nameserver_count; i++) { ldns_rdf_deep_free(res->_nameservers[i]); } + LDNS_FREE(res->_nameservers); } - LDNS_FREE(res->_searchlist); - LDNS_FREE(res->_nameservers); if (ldns_resolver_domain(res)) { ldns_rdf_deep_free(ldns_resolver_domain(res)); } diff --git a/str2host.c b/str2host.c index 847ca649..79c8b39f 100644 --- a/str2host.c +++ b/str2host.c @@ -24,6 +24,60 @@ #include #endif +bool +ldns_is_ipv4_addr(const char *str) +{ + int a, i; + const char *dot = str; + + if (strlen(str) < 7 || strlen(str) > 16) { + return false; + } + + for (i = 0; i < 3; i++) { + a = atoi(dot); + if (a < 0 || a > 255) { + return false; + } + dot = strchr(dot, '.'); + if (!dot) { + return false; + } + } + a = atoi(dot); + if (a < 0 || a > 255) { + return false; + } + + return true; +} + +/* todo: check for more errors (like ffffff:f etc) */ +bool +ldns_is_ipv6_addr(const char *str) +{ + int a; + size_t i; + const char *dot = str; + + if (strlen(str) < 3 || strlen(str) > 40) { + return false; + } + + for (i = 0; i < strlen(str); i++) { + if (str[i] != ':' && + ldns_hexdigit_to_int(str[i]) < 0) { + return false; + } + } + a = atoi(dot); + if (a < 0 || a > 255) { + return false; + } + + return true; +} + ldns_status ldns_str2rdf_int16(ldns_rdf **rd, const char *shortstr) { diff --git a/util.c b/util.c index b3d9ca6f..7e873caa 100644 --- a/util.c +++ b/util.c @@ -64,7 +64,6 @@ ldns_lookup_by_id(ldns_lookup_table *table, int id) } return NULL; } - int ldns_get_bit(uint8_t bits[], size_t index) { @@ -107,7 +106,7 @@ ldns_hexdigit_to_int(char ch) case 'e': case 'E': return 14; case 'f': case 'F': return 15; default: - abort(); + return -1; } }