]> git.meshlink.io Git - catta/blobdiff - avahi-common/domain.c
document that AVAHI_PROTO_xxx is no longer defined to be identical with AF_xxx
[catta] / avahi-common / domain.c
index f2e4395a7e3649813cd30840cb25394dc757069c..7d954deea3df212632adcea00ecca8f72fc4d7b7 100644 (file)
@@ -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);
@@ -491,7 +502,7 @@ int avahi_domain_ends_with(const char *domain, const char *suffix) {
 }
 
 int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
-    char escaped_name[AVAHI_LABEL_MAX];
+    char escaped_name[AVAHI_LABEL_MAX*4];
     char normalized_type[AVAHI_DOMAIN_NAME_MAX];
     char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
     
@@ -530,19 +541,22 @@ int avahi_service_name_join(char *p, size_t size, const char *name, const char *
     return AVAHI_OK;
 }
 
+#ifndef HAVE_STRLCPY
 
-char *avahi_strlcpy(char *dest, const char *src, size_t n) {
+static size_t strlcpy(char *dest, const char *src, size_t n) {
     assert(dest);
     assert(src);
-
-    if (n == 0)
-        return dest;
-        
-    strncpy(dest, src, n-1);
-    dest[n-1] = 0;
-    return dest;
+    
+    if (n > 0) {
+        strncpy(dest, src, n-1);
+        dest[n-1] = 0;
+    }
+    
+    return strlen(src);
 }
 
+#endif
+
 int avahi_service_name_split(const char *p, char *name, size_t name_size, char *type, size_t type_size, char *domain, size_t domain_size) {
     enum {
         NAME,
@@ -574,7 +588,7 @@ int avahi_service_name_split(const char *p, char *name, size_t name_size, char *
 
         switch (state) {
             case NAME:
-                avahi_strlcpy(name, buf, name_size);
+                strlcpy(name, buf, name_size);
                 state = TYPE;
                 break;