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 char *avahi_get_host_name(void) {
42 char t[HOST_NAME_MAX];
46 gethostname(t, sizeof(t));
48 return avahi_normalize_name(t);
51 static char *unescape_uneeded(const char *src, char *ret_dest, size_t size) {
60 if (!escaped && *src == '\\')
62 else if (escaped && (*src == '.' || *src == '\\')) {
64 if ((size -= 2) <= 1) break;
70 if (--size <= 1) break;
83 char *avahi_normalize_name(const char *s) {
89 unescape_uneeded(s, tmp, sizeof(tmp));
93 while (l > 0 && tmp[l-1] == '.')
96 return avahi_strdup(tmp);
100 /* Read the first label from string *name, unescape "\" and write it to dest */
101 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
126 if (**name == '\\') {
133 *(d++) = *((*name) ++);
144 /* Escape "\" and ".", append \0 */
145 char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size) {
152 assert(*ret_size > 0);
156 while (src_length > 0) {
157 if (*src == '.' || *src == '\\') {
161 *((*ret_name) ++) = '\\';
168 *((*ret_name)++) = *src;
180 int avahi_domain_equal(const char *a, const char *b) {
188 char ca[65], cb[65], *pa, *pb;
190 pa = avahi_unescape_label(&a, ca, sizeof(ca));
191 pb = avahi_unescape_label(&b, cb, sizeof(cb));
195 else if ((pa && !pb) || (!pa && pb))
198 if (strcasecmp(pa, pb))
205 int avahi_binary_domain_cmp(const char *a, const char *b) {
213 char ca[65], cb[65], *pa, *pb;
216 pa = avahi_unescape_label(&a, ca, sizeof(ca));
217 pb = avahi_unescape_label(&b, cb, sizeof(cb));
226 if ((r = strcmp(pa, pb)))
231 int avahi_is_valid_service_type(const char *t) {
241 if (!(p = strchr(t, '.')))
244 if (p - t > 63 || p - t < 2)
253 if (strlen(p) > 63 || strlen(p) < 2)
259 int avahi_is_valid_domain_name(const char *t) {
268 /* Domains may not start with a dot */
274 for (p = t; *p; p++) {
277 if (dot) /* Two subsequent dots */
293 /* A trailing dot IS allowed */
298 int avahi_is_valid_service_name(const char *t) {
310 int avahi_is_valid_host_name(const char *t) {
325 unsigned avahi_domain_hash(const char *s) {
331 if (!avahi_unescape_label(&s, c, sizeof(c)))
338 hash = 31 * hash + tolower(*p);
342 int avahi_domain_ends_with(const char *domain, const char *suffix) {
346 assert(avahi_is_valid_domain_name(domain));
347 assert(avahi_is_valid_domain_name(suffix));
352 if (avahi_domain_equal(domain, suffix))
355 if (!(avahi_unescape_label(&domain, dummy, sizeof(dummy))))
360 static void escape_service_name(char *d, size_t size, const char *s) {
365 while (*s && size >= 2) {
366 if (*s == '.' || *s == '\\') {
383 int avahi_service_name_snprint(char *p, size_t size, const char *name, const char *type, const char *domain) {
384 char *t = NULL, *d = NULL;
390 if ((name && !avahi_is_valid_service_name(name))) {
391 ret = AVAHI_ERR_INVALID_SERVICE_NAME;
395 if (!avahi_is_valid_service_type(type)) {
396 ret = AVAHI_ERR_INVALID_SERVICE_TYPE;
400 if (!avahi_is_valid_domain_name(domain)) {
401 ret = AVAHI_ERR_INVALID_DOMAIN_NAME;
406 escape_service_name(ename, sizeof(ename), name);
408 if (!(d = avahi_normalize_name(domain)) ||
409 !(t = avahi_normalize_name(type))) {
410 ret = AVAHI_ERR_NO_MEMORY;
414 snprintf(p, size, "%s%s%s.%s", name ? ename : "", name ? "." : "", t, d);