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
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) ++);
112 /* Escape "\" and ".", append \0 */
113 char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
120 assert(*ret_size > 0);
124 while (src_length > 0) {
125 if (*src == '.' || *src == '\\') {
127 /* Dot or backslash */
132 *((*ret_name) ++) = '\\';
133 *((*ret_name) ++) = *src;
139 (*src >= '0' && *src <= '9') ||
140 (*src >= 'a' && *src <= 'z') ||
141 (*src >= 'A' && *src <= 'Z')) {
143 /* Proper character */
148 *((*ret_name)++) = *src;
153 /* Everything else */
158 *((*ret_name) ++) = '\\';
159 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src / 100);
160 *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
161 *((*ret_name) ++) = '0' + (char) ((uint8_t) *src % 10);
175 char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
187 char label[AVAHI_LABEL_MAX];
189 if (!(avahi_unescape_label(&s, label, sizeof(label))))
194 if (*s == 0 && empty)
210 avahi_escape_label(label, strlen(label), &r, &size);
216 char *avahi_normalize_name_strdup(const char *s) {
217 char t[AVAHI_DOMAIN_NAME_MAX];
220 if (!(avahi_normalize_name(s, t, sizeof(t))))
223 return avahi_strdup(t);
226 int avahi_domain_equal(const char *a, const char *b) {
234 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
236 r = avahi_unescape_label(&a, ca, sizeof(ca));
238 r = avahi_unescape_label(&b, cb, sizeof(cb));
241 if (strcasecmp(ca, cb))
251 int avahi_is_valid_service_type_generic(const char *t) {
254 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
258 char label[AVAHI_LABEL_MAX];
260 if (!(avahi_unescape_label(&t, label, sizeof(label))))
263 if (strlen(label) <= 2 || label[0] != '_')
271 int avahi_is_valid_service_type_strict(const char *t) {
272 char label[AVAHI_LABEL_MAX];
275 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
278 /* Application name */
280 if (!(avahi_unescape_label(&t, label, sizeof(label))))
283 if (strlen(label) <= 2 || label[0] != '_')
289 /* _tcp or _udp boilerplate */
291 if (!(avahi_unescape_label(&t, label, sizeof(label))))
294 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
303 const char *avahi_get_type_from_subtype(const char *t) {
304 char label[AVAHI_LABEL_MAX];
308 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
313 if (!(avahi_unescape_label(&t, label, sizeof(label))))
316 if (strlen(label) <= 2 || label[0] != '_')
324 if (!(avahi_unescape_label(&t, label, sizeof(label))))
327 if (strcasecmp(label, "_sub"))
335 /* Application name */
337 if (!(avahi_unescape_label(&t, label, sizeof(label))))
340 if (strlen(label) <= 2 || label[0] != '_')
346 /* _tcp or _udp boilerplate */
348 if (!(avahi_unescape_label(&t, label, sizeof(label))))
351 if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
360 int avahi_is_valid_service_subtype(const char *t) {
363 return !!avahi_get_type_from_subtype(t);
366 int avahi_is_valid_domain_name(const char *t) {
370 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
374 char label[AVAHI_LABEL_MAX];
376 if (!(avahi_unescape_label(&t, label, sizeof(label))))
379 /* Explicitly allow the root domain name */
380 if (is_first && label[0] == 0 && *t == 0)
393 int avahi_is_valid_service_name(const char *t) {
396 if (strlen(t) >= AVAHI_LABEL_MAX || !*t)
402 int avahi_is_valid_host_name(const char *t) {
403 char label[AVAHI_LABEL_MAX];
406 if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
409 if (!(avahi_unescape_label(&t, label, sizeof(label))))
412 if (strlen(label) < 1)
421 unsigned avahi_domain_hash(const char *s) {
425 char c[AVAHI_LABEL_MAX], *p, *r;
427 r = avahi_unescape_label(&s, c, sizeof(c));
431 hash = 31 * hash + tolower(*p);
437 int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
438 char escaped_name[AVAHI_LABEL_MAX*4];
439 char normalized_type[AVAHI_DOMAIN_NAME_MAX];
440 char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
444 /* Validity checks */
446 if ((name && !avahi_is_valid_service_name(name)))
447 return AVAHI_ERR_INVALID_SERVICE_NAME;
449 if (!avahi_is_valid_service_type_generic(type))
450 return AVAHI_ERR_INVALID_SERVICE_TYPE;
452 if (!avahi_is_valid_domain_name(domain))
453 return AVAHI_ERR_INVALID_DOMAIN_NAME;
458 size_t l = sizeof(escaped_name);
459 char *e = escaped_name, *r;
460 r = avahi_escape_label(name, strlen(name), &e, &l);
464 if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
465 return AVAHI_ERR_INVALID_SERVICE_TYPE;
467 if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
468 return AVAHI_ERR_INVALID_DOMAIN_NAME;
472 snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
479 static size_t strlcpy(char *dest, const char *src, size_t n) {
484 strncpy(dest, src, n-1);
493 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) {
499 int type_empty = 1, domain_empty = 1;
503 assert(type_size > 0);
505 assert(domain_size > 0);
508 assert(name_size > 0);
519 if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
524 strlcpy(name, buf, name_size);
534 return AVAHI_ERR_NO_MEMORY;
542 if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
543 return AVAHI_ERR_NO_MEMORY;
555 return AVAHI_ERR_NO_MEMORY;
562 if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
563 return AVAHI_ERR_NO_MEMORY;