+
+ return hash;
+}
+
+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*4];
+ char normalized_type[AVAHI_DOMAIN_NAME_MAX];
+ char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
+
+ assert(p);
+
+ /* Validity checks */
+
+ if ((name && !avahi_is_valid_service_name(name)))
+ return AVAHI_ERR_INVALID_SERVICE_NAME;
+
+ if (!avahi_is_valid_service_type_generic(type))
+ return AVAHI_ERR_INVALID_SERVICE_TYPE;
+
+ if (!avahi_is_valid_domain_name(domain))
+ return AVAHI_ERR_INVALID_DOMAIN_NAME;
+
+ /* Preparation */
+
+ if (name) {
+ size_t l = sizeof(escaped_name);
+ char *e = escaped_name, *r;
+ r = avahi_escape_label(name, strlen(name), &e, &l);
+ assert(r);
+ }
+
+ if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
+ return AVAHI_ERR_INVALID_SERVICE_TYPE;
+
+ if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
+ return AVAHI_ERR_INVALID_DOMAIN_NAME;
+
+ /* Concatenation */
+
+ snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
+
+ return AVAHI_OK;
+}
+
+#ifndef HAVE_STRLCPY
+
+static size_t strlcpy(char *dest, const char *src, size_t n) {
+ assert(dest);
+ assert(src);
+
+ 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,
+ TYPE,
+ DOMAIN
+ } state;
+ int type_empty = 1, domain_empty = 1;
+
+ assert(p);
+ assert(type);
+ assert(type_size > 0);
+ assert(domain);
+ assert(domain_size > 0);
+
+ if (name) {
+ assert(name_size > 0);
+ *name = 0;
+ state = NAME;
+ } else
+ state = TYPE;
+
+ *type = *domain = 0;
+
+ while (*p) {
+ char buf[64];
+
+ if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
+ return -1;
+
+ switch (state) {
+ case NAME:
+ strlcpy(name, buf, name_size);
+ state = TYPE;
+ break;
+
+ case TYPE:
+
+ if (buf[0] == '_') {
+
+ if (!type_empty) {
+ if (!type_size)
+ return AVAHI_ERR_NO_MEMORY;
+
+ *(type++) = '.';
+ type_size --;
+
+ } else
+ type_empty = 0;
+
+ if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
+ return AVAHI_ERR_NO_MEMORY;
+
+ break;
+ }
+
+ state = DOMAIN;
+ /* fall through */
+
+ case DOMAIN:
+
+ if (!domain_empty) {
+ if (!domain_size)
+ return AVAHI_ERR_NO_MEMORY;
+
+ *(domain++) = '.';
+ domain_size --;
+ } else
+ domain_empty = 0;
+
+ if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
+ return AVAHI_ERR_NO_MEMORY;
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+int avahi_is_valid_fqdn(const char *t) {
+ char label[AVAHI_LABEL_MAX];
+ char normalized[AVAHI_DOMAIN_NAME_MAX];
+ const char *k = t;
+ AvahiAddress a;
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
+ return 0;
+
+ if (!avahi_is_valid_domain_name(t))
+ return 0;
+
+ /* Check if there are at least two labels*/
+ if (!(avahi_unescape_label(&k, label, sizeof(label))))
+ return 0;
+
+ if (label[0] == 0 || !k)
+ return 0;
+
+ if (!(avahi_unescape_label(&k, label, sizeof(label))))
+ return 0;
+
+ if (label[0] == 0 || !k)
+ return 0;
+
+ /* Make sure that the name is not an IP address */
+ if (!(avahi_normalize_name(t, normalized, sizeof(normalized))))
+ return 0;
+
+ if (avahi_address_parse(normalized, AVAHI_PROTO_UNSPEC, &a))
+ return 0;
+
+ return 1;