2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 /* Read the first label from string *name, unescape "\" and write it to dest */
41 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
64 /* Escaped character */
72 else if (**name == '\\' || **name == '.') {
73 /* Escaped backslash or dot */
74 *(d++) = *((*name) ++);
76 } else if (isdigit(**name)) {
79 /* Escaped literal ASCII character */
81 if (!isdigit(*(*name+1)) || !isdigit(*(*name+2)))
84 n = ((uint8_t) (**name - '0') * 100) + ((uint8_t) (*(*name+1) - '0') * 10) + ((uint8_t) (*(*name +2) - '0'));
86 if (n > 255 || n == 0)
98 /* Normal character */
100 *(d++) = *((*name) ++);
109 if (!avahi_utf8_valid(dest))
115 /* Escape "\" and ".", append \0 */
116 char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
123 assert(*ret_size > 0);
127 while (src_length > 0) {
128 if (*src == '.' || *src == '\\') {
130 /* Dot or backslash */
135 *((*ret_name) ++) = '\\';
136 *((*ret_name) ++) = *src;
142 (*src >= '0' && *src <= '9') ||
143 (*src >= 'a' && *src <= 'z') ||
144 (*src >= 'A' && *src <= 'Z')) {
146 /* Proper character */
151 *((*ret_name)++) = *src;
156 /* Everything else */
161 *((*ret_name) ++) = '\\';
162 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src / 100);
163 *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
164 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src % 10);
178 char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
190 char label[AVAHI_LABEL_MAX];
192 if (!(avahi_unescape_label(&s, label, sizeof(label))))
197 if (*s == 0 && empty)
213 avahi_escape_label(label, strlen(label), &r, &size);
219 char *avahi_normalize_name_strdup(const char *s) {
220 char t[AVAHI_DOMAIN_NAME_MAX];
223 if (!(avahi_normalize_name(s, t, sizeof(t))))
226 return avahi_strdup(t);
229 int avahi_domain_equal(const char *a, const char *b) {
237 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
239 r = avahi_unescape_label(&a, ca, sizeof(ca));
241 r = avahi_unescape_label(&b, cb, sizeof(cb));
244 if (strcasecmp(ca, cb))
254 int avahi_is_valid_service_type_generic(const char *t) {
257 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
261 char label[AVAHI_LABEL_MAX];
263 if (!(avahi_unescape_label(&t, label, sizeof(label))))
266 if (strlen(label) <= 2 || label[0] != '_')
274 int avahi_is_valid_service_type_strict(const char *t) {
275 char label[AVAHI_LABEL_MAX];
278 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
281 /* Application name */
283 if (!(avahi_unescape_label(&t, label, sizeof(label))))
286 if (strlen(label) <= 2 || label[0] != '_')
292 /* _tcp or _udp boilerplate */
294 if (!(avahi_unescape_label(&t, label, sizeof(label))))
297 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
306 const char *avahi_get_type_from_subtype(const char *t) {
307 char label[AVAHI_LABEL_MAX];
311 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
316 if (!(avahi_unescape_label(&t, label, sizeof(label))))
319 if (strlen(label) <= 2 || label[0] != '_')
327 if (!(avahi_unescape_label(&t, label, sizeof(label))))
330 if (strcasecmp(label, "_sub"))
338 /* Application name */
340 if (!(avahi_unescape_label(&t, label, sizeof(label))))
343 if (strlen(label) <= 2 || label[0] != '_')
349 /* _tcp or _udp boilerplate */
351 if (!(avahi_unescape_label(&t, label, sizeof(label))))
354 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
363 int avahi_is_valid_service_subtype(const char *t) {
366 return !!avahi_get_type_from_subtype(t);
369 int avahi_is_valid_domain_name(const char *t) {
373 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
377 char label[AVAHI_LABEL_MAX];
379 if (!(avahi_unescape_label(&t, label, sizeof(label))))
382 /* Explicitly allow the root domain name */
383 if (is_first && label[0] == 0 && *t == 0)
396 int avahi_is_valid_service_name(const char *t) {
399 if (strlen(t) >= AVAHI_LABEL_MAX || !*t)
405 int avahi_is_valid_host_name(const char *t) {
406 char label[AVAHI_LABEL_MAX];
409 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
412 if (!(avahi_unescape_label(&t, label, sizeof(label))))
415 if (strlen(label) < 1)
424 unsigned avahi_domain_hash(const char *s) {
428 char c[AVAHI_LABEL_MAX], *p, *r;
430 r = avahi_unescape_label(&s, c, sizeof(c));
434 hash = 31 * hash + tolower(*p);
440 int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
441 char escaped_name[AVAHI_LABEL_MAX*4];
442 char normalized_type[AVAHI_DOMAIN_NAME_MAX];
443 char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
447 /* Validity checks */
449 if ((name && !avahi_is_valid_service_name(name)))
450 return AVAHI_ERR_INVALID_SERVICE_NAME;
452 if (!avahi_is_valid_service_type_generic(type))
453 return AVAHI_ERR_INVALID_SERVICE_TYPE;
455 if (!avahi_is_valid_domain_name(domain))
456 return AVAHI_ERR_INVALID_DOMAIN_NAME;
461 size_t l = sizeof(escaped_name);
462 char *e = escaped_name, *r;
463 r = avahi_escape_label(name, strlen(name), &e, &l);
467 if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
468 return AVAHI_ERR_INVALID_SERVICE_TYPE;
470 if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
471 return AVAHI_ERR_INVALID_DOMAIN_NAME;
475 snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
482 static size_t strlcpy(char *dest, const char *src, size_t n) {
487 strncpy(dest, src, n-1);
496 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) {
502 int type_empty = 1, domain_empty = 1;
506 assert(type_size > 0);
508 assert(domain_size > 0);
511 assert(name_size > 0);
522 if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
527 strlcpy(name, buf, name_size);
537 return AVAHI_ERR_NO_MEMORY;
545 if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
546 return AVAHI_ERR_NO_MEMORY;
558 return AVAHI_ERR_NO_MEMORY;
565 if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
566 return AVAHI_ERR_NO_MEMORY;
575 int avahi_is_valid_fqdn(const char *t) {
576 char label[AVAHI_LABEL_MAX];
577 char normalized[AVAHI_DOMAIN_NAME_MAX];
582 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
585 if (!avahi_is_valid_domain_name(t))
588 /* Check if there are at least two labels*/
589 if (!(avahi_unescape_label(&k, label, sizeof(label))))
592 if (label[0] == 0 || !k)
595 if (!(avahi_unescape_label(&k, label, sizeof(label))))
598 if (label[0] == 0 || !k)
601 /* Make sure that the name is not an IP address */
602 if (!(avahi_normalize_name(t, normalized, sizeof(normalized))))
605 if (avahi_address_parse(normalized, AVAHI_PROTO_UNSPEC, &a))