diff options
author | Lennart Poettering <lennart@poettering.net> | 2005-10-23 20:21:39 +0000 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2005-10-23 20:21:39 +0000 |
commit | e865e3dba3c6ee5de5581dbfbafc025aa5c9947e (patch) | |
tree | 7630d4d1b2f9982d1c5df4dad767ea7511ad33ee /avahi-common | |
parent | 0d0c9486e3fe26fdd67624ee57961ba2d5846ebb (diff) |
* correctly handle empty domain names (i.e. the root zone)
* fix memory initialization error in wide-area.c
* make server_add_xx() functions atomic, i.e. clean up half-created entries on failure
* add some more validity checking macros and change everything to make use of them
* clean up avahi_server_add_address()
* change some functions from domain.[ch] to work on a stack buffer instead of malloced memory
* update avahi-test.c a little
* replace avahi_reverse_lookup_name_{ipv4,ipv6}() with a single function avahi_reverse_lookup_name()
* remove avahi_server_add_dns_server_name() from public API
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@845 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-common')
-rw-r--r-- | avahi-common/address.c | 73 | ||||
-rw-r--r-- | avahi-common/address.h | 7 | ||||
-rw-r--r-- | avahi-common/domain-test.c | 7 | ||||
-rw-r--r-- | avahi-common/domain.c | 43 |
4 files changed, 70 insertions, 60 deletions
diff --git a/avahi-common/address.c b/avahi-common/address.c index f3f3a67..d02d997 100644 --- a/avahi-common/address.c +++ b/avahi-common/address.c @@ -29,6 +29,7 @@ #include <arpa/inet.h> #include <string.h> #include <assert.h> +#include <stdio.h> #include "address.h" #include "malloc.h" @@ -65,48 +66,42 @@ char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) { return s; } -char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) { - uint32_t n = ntohl(a->address); +char* avahi_reverse_lookup_name(char *ret_s, size_t length, const AvahiAddress *a) { + assert(ret_s); + assert(length > 0); assert(a); - return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24); -} + if (a->proto == AVAHI_PROTO_INET) { + uint32_t n = ntohl(a->data.ipv4.address); + snprintf( + ret_s, length, + "%u.%u.%u.%u.in-addr.arpa", + n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24); + } else { + assert(a->proto == AVAHI_PROTO_INET6); + + snprintf( + ret_s, length, + "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", + a->data.ipv6.address[15] & 0xF, a->data.ipv6.address[15] >> 4, + a->data.ipv6.address[14] & 0xF, a->data.ipv6.address[14] >> 4, + a->data.ipv6.address[13] & 0xF, a->data.ipv6.address[13] >> 4, + a->data.ipv6.address[12] & 0xF, a->data.ipv6.address[12] >> 4, + a->data.ipv6.address[11] & 0xF, a->data.ipv6.address[11] >> 4, + a->data.ipv6.address[10] & 0xF, a->data.ipv6.address[10] >> 4, + a->data.ipv6.address[ 9] & 0xF, a->data.ipv6.address[ 9] >> 4, + a->data.ipv6.address[ 8] & 0xF, a->data.ipv6.address[ 8] >> 4, + a->data.ipv6.address[ 7] & 0xF, a->data.ipv6.address[ 7] >> 4, + a->data.ipv6.address[ 6] & 0xF, a->data.ipv6.address[ 6] >> 4, + a->data.ipv6.address[ 5] & 0xF, a->data.ipv6.address[ 5] >> 4, + a->data.ipv6.address[ 4] & 0xF, a->data.ipv6.address[ 4] >> 4, + a->data.ipv6.address[ 3] & 0xF, a->data.ipv6.address[ 3] >> 4, + a->data.ipv6.address[ 2] & 0xF, a->data.ipv6.address[ 2] >> 4, + a->data.ipv6.address[ 1] & 0xF, a->data.ipv6.address[ 1] >> 4, + a->data.ipv6.address[ 0] & 0xF, a->data.ipv6.address[ 0] >> 4); + } -char *avahi_reverse_lookup_name_ipv6(const AvahiIPv6Address *a) { - - return avahi_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", - a->address[15] & 0xF, - a->address[15] >> 4, - a->address[14] & 0xF, - a->address[14] >> 4, - a->address[13] & 0xF, - a->address[13] >> 4, - a->address[12] & 0xF, - a->address[12] >> 4, - a->address[11] & 0xF, - a->address[11] >> 4, - a->address[10] & 0xF, - a->address[10] >> 4, - a->address[9] & 0xF, - a->address[9] >> 4, - a->address[8] & 0xF, - a->address[8] >> 4, - a->address[7] & 0xF, - a->address[7] >> 4, - a->address[6] & 0xF, - a->address[6] >> 4, - a->address[5] & 0xF, - a->address[5] >> 4, - a->address[4] & 0xF, - a->address[4] >> 4, - a->address[3] & 0xF, - a->address[3] >> 4, - a->address[2] & 0xF, - a->address[2] >> 4, - a->address[1] & 0xF, - a->address[1] >> 4, - a->address[0] & 0xF, - a->address[0] >> 4); + return ret_s; } AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) { diff --git a/avahi-common/address.h b/avahi-common/address.h index f1ac428..c383250 100644 --- a/avahi-common/address.h +++ b/avahi-common/address.h @@ -102,11 +102,8 @@ AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddres /** Return the port number of a sockaddr structure (either IPv4 or IPv6) */ uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa); -/** Generate the DNS reverse lookup name for an IPv4 address. avahi_free() the result! */ -char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a); - -/** Generate the modern DNS reverse lookup name for an IPv6 address, ending in ipv6.arpa. avahi_free() the result! */ -char* avahi_reverse_lookup_name_ipv6(const AvahiIPv6Address *a); +/** Generate the DNS reverse lookup name for an IPv4 or IPv6 address. */ +char* avahi_reverse_lookup_name(char *ret_s, size_t length, const AvahiAddress *a); /** Check whether the specified IPv6 address is in fact an * encapsulated IPv4 address, returns 1 if yes, 0 otherwise */ diff --git a/avahi-common/domain-test.c b/avahi-common/domain-test.c index f1313cb..279be72 100644 --- a/avahi-common/domain-test.c +++ b/avahi-common/domain-test.c @@ -110,5 +110,12 @@ int main(int argc, char *argv[]) { assert(avahi_is_valid_host_name("sfooo.")); assert(avahi_is_valid_host_name("sfooo")); + assert(avahi_is_valid_domain_name(".")); + assert(avahi_is_valid_domain_name("")); + + assert(avahi_normalize_name(".", t, sizeof(t))); + assert(avahi_normalize_name("", t, sizeof(t))); + + return 0; } diff --git a/avahi-common/domain.c b/avahi-common/domain.c index 988ba7e..7d954de 100644 --- a/avahi-common/domain.c +++ b/avahi-common/domain.c @@ -206,31 +206,35 @@ char *avahi_normalize_name(const char *s, char *ret_s, size_t size) { assert(size > 0); r = ret_s; + *ret_s = 0; + while (*s) { char label[AVAHI_LABEL_MAX]; if (!(avahi_unescape_label(&s, label, sizeof(label)))) return NULL; - if (strlen(label) > 0) { - - if (!empty) { - if (size < 1) - return NULL; + if (label[0] == 0) { - *(r++) = '.'; - size--; + if (*s == 0 && empty) + return ret_s; - } else - empty = 0; - - avahi_escape_label(label, strlen(label), &r, &size); + return NULL; } + + if (!empty) { + if (size < 1) + return NULL; + + *(r++) = '.'; + size--; + + } else + empty = 0; + + avahi_escape_label(label, strlen(label), &r, &size); } - if (empty) - return NULL; - return ret_s; } @@ -409,9 +413,10 @@ int avahi_is_valid_service_subtype(const char *t) { } int avahi_is_valid_domain_name(const char *t) { + int is_first = 1; assert(t); - if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t) + if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX) return 0; do { @@ -420,7 +425,13 @@ int avahi_is_valid_domain_name(const char *t) { if (!(avahi_unescape_label(&t, label, sizeof(label)))) return 0; - if (strlen(label) < 1) + /* Explicitly allow the root domain name */ + if (is_first && label[0] == 0 && *t == 0) + return 1; + + is_first = 0; + + if (label[0] == 0) return 0; } while (*t); |