4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
41 /* Read the first label from string *name, unescape "\" and write it to dest */
42 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
65 /* Escaped character */
73 else if (**name == '\\' || **name == '.') {
74 /* Escaped backslash or dot */
75 *(d++) = *((*name) ++);
77 } else if (isdigit(**name)) {
80 /* Escaped literal ASCII character */
82 if (!isdigit(*(*name+1)) || !isdigit(*(*name+2)))
85 n = ((uint8_t) (**name - '0') * 100) + ((uint8_t) (*(*name+1) - '0') * 10) + ((uint8_t) (*(*name +2) - '0'));
87 if (n > 255 || n == 0)
99 /* Normal character */
101 *(d++) = *((*name) ++);
113 /* Escape "\" and ".", append \0 */
114 char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
121 assert(*ret_size > 0);
125 while (src_length > 0) {
126 if (*src == '.' || *src == '\\') {
128 /* Dot or backslash */
133 *((*ret_name) ++) = '\\';
134 *((*ret_name) ++) = *src;
140 (*src >= '0' && *src <= '9') ||
141 (*src >= 'a' && *src <= 'z') ||
142 (*src >= 'A' && *src <= 'Z')) {
144 /* Proper character */
149 *((*ret_name)++) = *src;
154 /* Everything else */
159 *((*ret_name) ++) = '\\';
160 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src / 100);
161 *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
162 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src % 10);
176 char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
188 char label[AVAHI_LABEL_MAX];
190 if (!(avahi_unescape_label(&s, label, sizeof(label))))
195 if (*s == 0 && empty)
211 avahi_escape_label(label, strlen(label), &r, &size);
217 char *avahi_normalize_name_strdup(const char *s) {
218 char t[AVAHI_DOMAIN_NAME_MAX];
221 if (!(avahi_normalize_name(s, t, sizeof(t))))
224 return avahi_strdup(t);
227 int avahi_domain_equal(const char *a, const char *b) {
235 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
237 r = avahi_unescape_label(&a, ca, sizeof(ca));
239 r = avahi_unescape_label(&b, cb, sizeof(cb));
242 if (strcasecmp(ca, cb))
252 int avahi_is_valid_service_type_generic(const char *t) {
255 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
259 char label[AVAHI_LABEL_MAX];
261 if (!(avahi_unescape_label(&t, label, sizeof(label))))
264 if (strlen(label) <= 2 || label[0] != '_')
272 int avahi_is_valid_service_type_strict(const char *t) {
273 char label[AVAHI_LABEL_MAX];
276 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
279 /* Application name */
281 if (!(avahi_unescape_label(&t, label, sizeof(label))))
284 if (strlen(label) <= 2 || label[0] != '_')
290 /* _tcp or _udp boilerplate */
292 if (!(avahi_unescape_label(&t, label, sizeof(label))))
295 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
304 const char *avahi_get_type_from_subtype(const char *t) {
305 char label[AVAHI_LABEL_MAX];
309 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
314 if (!(avahi_unescape_label(&t, label, sizeof(label))))
317 if (strlen(label) <= 2 || label[0] != '_')
325 if (!(avahi_unescape_label(&t, label, sizeof(label))))
328 if (strcasecmp(label, "_sub"))
336 /* Application name */
338 if (!(avahi_unescape_label(&t, label, sizeof(label))))
341 if (strlen(label) <= 2 || label[0] != '_')
347 /* _tcp or _udp boilerplate */
349 if (!(avahi_unescape_label(&t, label, sizeof(label))))
352 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
361 int avahi_is_valid_service_subtype(const char *t) {
364 return !!avahi_get_type_from_subtype(t);
367 int avahi_is_valid_domain_name(const char *t) {
371 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
375 char label[AVAHI_LABEL_MAX];
377 if (!(avahi_unescape_label(&t, label, sizeof(label))))
380 /* Explicitly allow the root domain name */
381 if (is_first && label[0] == 0 && *t == 0)
394 int avahi_is_valid_service_name(const char *t) {
397 if (strlen(t) >= AVAHI_LABEL_MAX || !*t)
403 int avahi_is_valid_host_name(const char *t) {
404 char label[AVAHI_LABEL_MAX];
407 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
410 if (!(avahi_unescape_label(&t, label, sizeof(label))))
413 if (strlen(label) < 1)
422 unsigned avahi_domain_hash(const char *s) {
426 char c[AVAHI_LABEL_MAX], *p, *r;
428 r = avahi_unescape_label(&s, c, sizeof(c));
432 hash = 31 * hash + tolower(*p);
438 int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
439 char escaped_name[AVAHI_LABEL_MAX*4];
440 char normalized_type[AVAHI_DOMAIN_NAME_MAX];
441 char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
445 /* Validity checks */
447 if ((name && !avahi_is_valid_service_name(name)))
448 return AVAHI_ERR_INVALID_SERVICE_NAME;
450 if (!avahi_is_valid_service_type_generic(type))
451 return AVAHI_ERR_INVALID_SERVICE_TYPE;
453 if (!avahi_is_valid_domain_name(domain))
454 return AVAHI_ERR_INVALID_DOMAIN_NAME;
459 size_t l = sizeof(escaped_name);
460 char *e = escaped_name, *r;
461 r = avahi_escape_label(name, strlen(name), &e, &l);
465 if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
466 return AVAHI_ERR_INVALID_SERVICE_TYPE;
468 if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
469 return AVAHI_ERR_INVALID_DOMAIN_NAME;
473 snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
480 static size_t strlcpy(char *dest, const char *src, size_t n) {
485 strncpy(dest, src, n-1);
494 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) {
500 int type_empty = 1, domain_empty = 1;
504 assert(type_size > 0);
506 assert(domain_size > 0);
509 assert(name_size > 0);
520 if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
525 strlcpy(name, buf, name_size);
535 return AVAHI_ERR_NO_MEMORY;
543 if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
544 return AVAHI_ERR_NO_MEMORY;
556 return AVAHI_ERR_NO_MEMORY;
563 if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
564 return AVAHI_ERR_NO_MEMORY;
573 int avahi_is_valid_fqdn(const char *t) {
574 char label[AVAHI_LABEL_MAX];
575 char normalized[AVAHI_DOMAIN_NAME_MAX];
580 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
583 if (!avahi_is_valid_domain_name(t))
586 /* Check if there are at least two labels*/
587 if (!(avahi_unescape_label(&k, label, sizeof(label))))
590 if (label[0] == 0 || !k)
593 if (!(avahi_unescape_label(&k, label, sizeof(label))))
596 if (label[0] == 0 || !k)
599 /* Make sure that the name is not an IP address */
600 if (!(avahi_normalize_name(t, normalized, sizeof(normalized))))
603 if (avahi_address_parse(normalized, AVAHI_PROTO_UNSPEC, &a))