]> git.meshlink.io Git - catta/blobdiff - avahi-core/server.c
add support for service subtypes: avahi_server_add_service_subtype()
[catta] / avahi-core / server.c
index 18bead92b274c6fd40f66044f9b502879df3b6cb..fdc53d5737b78821186945bb2b2ae8c0c79f240b 100644 (file)
@@ -1285,7 +1285,7 @@ int avahi_server_set_host_name(AvahiServer *s, const char *host_name) {
     withdraw_host_rrs(s);
 
     avahi_free(s->host_name);
-    s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_host_name();
+    s->host_name = host_name ? avahi_normalize_name_strdup(host_name) : avahi_get_host_name_strdup();
     s->host_name[strcspn(s->host_name, ".")] = 0;
     update_fqdn(s);
 
@@ -1303,7 +1303,7 @@ int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name) {
     withdraw_host_rrs(s);
 
     avahi_free(s->domain_name);
-    s->domain_name = domain_name ? avahi_normalize_name(domain_name) : avahi_strdup("local");
+    s->domain_name = domain_name ? avahi_normalize_name_strdup(domain_name) : avahi_strdup("local");
     update_fqdn(s);
 
     register_stuff(s);
@@ -1431,9 +1431,9 @@ AvahiServer *avahi_server_new(const AvahiPoll *poll_api, const AvahiServerConfig
     } while (s->local_service_cookie == AVAHI_SERVICE_COOKIE_INVALID);
     
     /* Get host name */
-    s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
+    s->host_name = s->config.host_name ? avahi_normalize_name_strdup(s->config.host_name) : avahi_get_host_name_strdup();
     s->host_name[strcspn(s->host_name, ".")] = 0;
-    s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : avahi_strdup("local");
+    s->domain_name = s->config.domain_name ? avahi_normalize_name_strdup(s->config.domain_name) : avahi_strdup("local");
     s->host_name_fqdn = NULL;
     update_fqdn(s);
 
@@ -1702,7 +1702,7 @@ int avahi_server_add_ptr(
     if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl)))
         return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
         
-    r->data.ptr.name = avahi_normalize_name(dest);
+    r->data.ptr.name = avahi_normalize_name_strdup(dest);
     ret = avahi_server_add(s, g, interface, protocol, flags, r);
     avahi_record_unref(r);
     return ret;
@@ -1738,7 +1738,7 @@ int avahi_server_add_address(
     if (!name)
         name = s->host_name_fqdn;
     else {
-        if (!(n = avahi_normalize_name(name)))
+        if (!(n = avahi_normalize_name_strdup(name)))
             return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
 
         name = n;
@@ -1834,8 +1834,10 @@ static int server_add_txt_strlst_nocopy(
     
     assert(s);
 
-    if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl)))
+    if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
+        avahi_string_list_free(strlst);
         return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
+    }
     
     r->data.txt.string_list = strlst;
     ret = avahi_server_add(s, g, interface, protocol, flags, r);
@@ -1925,8 +1927,7 @@ static int server_add_service_strlst_nocopy(
     uint16_t port,
     AvahiStringList *strlst) {
 
-    char ptr_name[256], svc_name[256], enum_ptr[256];
-    char *t = NULL, *d = NULL, *h = NULL;
+    char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
     AvahiRecord *r = NULL;
     int ret = AVAHI_OK;
     
@@ -1934,40 +1935,13 @@ static int server_add_service_strlst_nocopy(
     assert(type);
     assert(name);
 
-    if (!AVAHI_IF_VALID(interface)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE);
-        goto fail;
-    }
-    
-    if (!AVAHI_PROTO_VALID(protocol)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL);
-        goto fail;
-    }
-    
-    if (!AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_IS_PROXY)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS);
-        goto fail;
-    }
-
-    if (!avahi_is_valid_service_name(name)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME);
-        goto fail;
-    }
-
-    if (!avahi_is_valid_service_type(type)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_TYPE);
-        goto fail;
-    }
-
-    if (domain && !avahi_is_valid_domain_name(domain)) {
-        ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
-        goto fail;
-    }
-
-    if (host && !avahi_is_valid_domain_name(host)) {
-        return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
-        goto fail;
-    }
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(interface), AVAHI_ERR_INVALID_PROTOCOL);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_IS_PROXY), AVAHI_ERR_INVALID_FLAGS);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
 
     if (!domain)
         domain = s->domain_name;
@@ -1975,22 +1949,25 @@ static int server_add_service_strlst_nocopy(
     if (!host)
         host = s->host_name_fqdn;
 
-    if (!(d = avahi_normalize_name(domain)) ||
-        !(t = avahi_normalize_name(type)) ||
-        !(h = avahi_normalize_name(host))) {
+    if (!(h = avahi_normalize_name_strdup(host))) {
         ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
         goto fail;
     }
 
-    if ((ret = avahi_service_name_snprint(svc_name, sizeof(svc_name), name, t, d)) < 0 ||
-        (ret = avahi_service_name_snprint(ptr_name, sizeof(ptr_name), NULL, t, d)) < 0) {
+    if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
+        (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
+        (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
         avahi_server_set_errno(s, ret);
         goto fail;
     }
 
+    /* Add service enumeration PTR record */
+    
     if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
         goto fail;
 
+    /* Add SRV record */
+    
     if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
         ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
         goto fail;
@@ -2007,6 +1984,8 @@ static int server_add_service_strlst_nocopy(
     if (ret < 0)
         goto fail;
 
+    /* Add TXT record */
+
     if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
         strlst = add_magic_cookie(s, strlst);
     
@@ -2016,20 +1995,20 @@ static int server_add_service_strlst_nocopy(
     if (ret < 0)
         goto fail;
 
-    snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d);
+    /* Add service type enumeration record */
+    
     ret = avahi_server_add_ptr(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY), AVAHI_DEFAULT_TTL, enum_ptr, ptr_name);
 
 fail:
     
-    avahi_free(d);
-    avahi_free(t);
-    avahi_free(h);
-
     avahi_string_list_free(strlst);
+    avahi_free(h);
     
     return ret;
 }
 
+
+
 int avahi_server_add_service_strlst(
     AvahiServer *s,
     AvahiSEntryGroup *g,
@@ -2061,7 +2040,7 @@ int avahi_server_add_service_va(
     const char *domain,
     const char *host,
     uint16_t port,
-    va_list va){
+    va_list va) {
 
     assert(s);
     assert(type);
@@ -2093,6 +2072,50 @@ int avahi_server_add_service(
     va_start(va, port);
     ret = avahi_server_add_service_va(s, g, interface, protocol, flags, name, type, domain, host, port, va);
     va_end(va);
+    
+    return ret;
+}
+
+int avahi_server_add_service_subtype(
+    AvahiServer *s,
+    AvahiSEntryGroup *g,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    AvahiPublishFlags flags,
+    const char *name,        
+    const char *type,        
+    const char *domain,      
+    const char *subtype) {
+
+    int ret = AVAHI_OK;
+    char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
+    
+    assert(name);
+    assert(type);
+    assert(subtype);
+
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(interface), AVAHI_ERR_INVALID_PROTOCOL);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_IS_PROXY), AVAHI_ERR_INVALID_FLAGS);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
+    AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
+
+    if (!domain)
+        domain = s->domain_name;
+
+    if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
+        (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
+        avahi_server_set_errno(s, ret);
+        goto fail;
+    }
+
+    if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
+        goto fail;
+
+fail:
+    
     return ret;
 }
 
@@ -2217,8 +2240,8 @@ int avahi_server_add_dns_server_name(
     if (!domain)
         domain = s->domain_name;
 
-    if (!(n = avahi_normalize_name(name)) ||
-        !(d = avahi_normalize_name(domain))) {
+    if (!(n = avahi_normalize_name_strdup(name)) ||
+        !(d = avahi_normalize_name_strdup(domain))) {
         avahi_free(n);
         avahi_free(d);
         return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
@@ -2593,7 +2616,7 @@ int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiP
     if (domain && !avahi_is_valid_domain_name(domain))
         return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
 
-    if ((ret = avahi_service_name_snprint(n, sizeof(n), name, type, domain) < 0))
+    if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain) < 0))
         return avahi_server_set_errno(s, ret);
         
     if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV)))