From d0a9e76f8f858b7dd98c9950a376dc347b230af9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 3 Oct 2005 22:56:14 +0000 Subject: Add avahi_service_name_snprint() git-svn-id: file:///home/lennart/svn/public/avahi/trunk@683 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/domain-test.c | 14 ++++++++++ avahi-common/domain.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ avahi-common/domain.h | 3 +++ 3 files changed, 84 insertions(+) (limited to 'avahi-common') diff --git a/avahi-common/domain-test.c b/avahi-common/domain-test.c index 76f806a..35ce6d6 100644 --- a/avahi-common/domain-test.c +++ b/avahi-common/domain-test.c @@ -30,12 +30,17 @@ int main(int argc, char *argv[]) { char *s; + char t[256]; printf("host name: %s\n", s = avahi_get_host_name()); avahi_free(s); printf("%s\n", s = avahi_normalize_name("foo.foo.")); avahi_free(s); + + printf("%s\n", s = avahi_normalize_name("foo\.foo.")); + avahi_free(s); + printf("%s\n", s = avahi_normalize_name("\\f\\o\\\\o\\..\\f\\ \\o\\o.")); avahi_free(s); @@ -46,6 +51,15 @@ int main(int argc, char *argv[]) { printf("%i\n", avahi_domain_equal("a", "aaa")); printf("%u = %u\n", avahi_domain_hash("\\Aaaab\\\\."), avahi_domain_hash("aaaa\\b\\\\")); + + + avahi_service_name_snprint(t, sizeof(t), "foo.foo.foo \.", "_http._tcp", "test.local"); + printf("<%s>\n", t); + + + avahi_service_name_snprint(t, sizeof(t), NULL, "_http._tcp", "one.two\. .local"); + printf("<%s>\n", t); + return 0; } diff --git a/avahi-common/domain.c b/avahi-common/domain.c index 1a4f8a6..bffe563 100644 --- a/avahi-common/domain.c +++ b/avahi-common/domain.c @@ -35,6 +35,7 @@ #include "domain.h" #include "malloc.h" +#include "error.h" char *avahi_get_host_name(void) { #ifdef HOST_NAME_MAX @@ -355,3 +356,69 @@ int avahi_domain_ends_with(const char *domain, const char *suffix) { return 0; } } + +static void escape_service_name(char *d, size_t size, const char *s) { + assert(d); + assert(size); + assert(s); + + while (*s && size >= 2) { + if (*s == '.' || *s == '\\') { + if (size < 3) + break; + + *(d++) = '\\'; + size--; + } + + *(d++) = *(s++); + size--; + } + + assert(size > 0); + *(d++) = 0; +} + + +int avahi_service_name_snprint(char *p, size_t size, const char *name, const char *type, const char *domain) { + char *t = NULL, *d = NULL; + char ename[64]; + int ret; + + assert(p); + + if ((name && !avahi_is_valid_service_name(name))) { + ret = AVAHI_ERR_INVALID_SERVICE_NAME; + goto fail; + } + + if (!avahi_is_valid_service_type(type)) { + ret = AVAHI_ERR_INVALID_SERVICE_TYPE; + goto fail; + } + + if (!avahi_is_valid_domain_name(domain)) { + ret = AVAHI_ERR_INVALID_DOMAIN_NAME; + goto fail; + } + + if (name) + escape_service_name(ename, sizeof(ename), name); + + if (!(d = avahi_normalize_name(domain)) || + !(t = avahi_normalize_name(type))) { + ret = AVAHI_ERR_NO_MEMORY; + goto fail; + } + + snprintf(p, size, "%s%s%s.%s", name ? ename : "", name ? "." : "", t, d); + + ret = AVAHI_OK; + +fail: + + avahi_free(t); + avahi_free(d); + + return ret; +} diff --git a/avahi-common/domain.h b/avahi-common/domain.h index 0f16e54..34f0681 100644 --- a/avahi-common/domain.h +++ b/avahi-common/domain.h @@ -72,6 +72,9 @@ unsigned avahi_domain_hash(const char *name); /** Returns 1 if the the end labels of domain are eqal to suffix */ int avahi_domain_ends_with(const char *domain, const char *suffix); +/** Construct a valid complete service name from a name, a type and a domain */ +int avahi_service_name_snprint(char *p, size_t size, const char *name, const char *type, const char *domain); + #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END #endif -- cgit