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
39 char *avahi_get_host_name(void) {
41 char t[HOST_NAME_MAX];
45 gethostname(t, sizeof(t));
47 return avahi_normalize_name(t);
50 static char *unescape_uneeded(const char *src, char *ret_dest, size_t size) {
59 if (!escaped && *src == '\\')
61 else if (escaped && (*src == '.' || *src == '\\')) {
63 if ((size -= 2) <= 1) break;
69 if (--size <= 1) break;
82 char *avahi_normalize_name(const char *s) {
88 unescape_uneeded(s, tmp, sizeof(tmp));
92 while (l > 0 && tmp[l-1] == '.')
95 return avahi_strdup(tmp);
99 /* Read the first label from string *name, unescape "\" and write it to dest */
100 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
125 if (**name == '\\') {
132 *(d++) = *((*name) ++);
143 /* Escape "\" and ".", append \0 */
144 char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size) {
151 assert(*ret_size > 0);
155 while (src_length > 0) {
156 if (*src == '.' || *src == '\\') {
160 *((*ret_name) ++) = '\\';
167 *((*ret_name)++) = *src;
179 int avahi_domain_equal(const char *a, const char *b) {
187 char ca[65], cb[65], *pa, *pb;
189 pa = avahi_unescape_label(&a, ca, sizeof(ca));
190 pb = avahi_unescape_label(&b, cb, sizeof(cb));
194 else if ((pa && !pb) || (!pa && pb))
197 if (strcasecmp(pa, pb))
204 int avahi_binary_domain_cmp(const char *a, const char *b) {
212 char ca[65], cb[65], *pa, *pb;
215 pa = avahi_unescape_label(&a, ca, sizeof(ca));
216 pb = avahi_unescape_label(&b, cb, sizeof(cb));
225 if ((r = strcmp(pa, pb)))
230 unsigned avahi_strhash(const char *p) {
234 hash = 31 * hash + *p;
239 unsigned avahi_domain_hash(const char *s) {
245 if (!avahi_unescape_label(&s, c, sizeof(c)))
251 hash += avahi_strhash(avahi_strdown(c));
255 int avahi_valid_service_type(const char *t) {
265 if (!(p = strchr(t, '.')))
268 if (p - t > 63 || p - t < 2)
277 if (strlen(p) > 63 || strlen(p) < 2)
283 int avahi_valid_domain_name(const char *t) {
292 /* Domains may not start with a dot */
298 for (p = t; *p; p++) {
301 if (dot) /* Two subsequent dots */
317 /* A trailing dot IS allowed */
322 int avahi_valid_service_name(const char *t) {
334 int avahi_valid_host_name(const char *t) {
349 char *avahi_strdown(char *s) {
355 *c = (char) tolower(*c);
360 char *avahi_strup(char *s) {
365 *c = (char) toupper(*c);