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);
98 int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) {
102 if (a->tv_sec < b->tv_sec)
105 if (a->tv_sec > b->tv_sec)
108 if (a->tv_usec < b->tv_usec)
111 if (a->tv_usec > b->tv_usec)
117 AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) {
121 if (avahi_timeval_compare(a, b) < 0)
122 return - avahi_timeval_diff(b, a);
124 return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
127 struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
131 u = usec + a->tv_usec;
134 a->tv_usec = (long) (1000000 + (u % 1000000));
135 a->tv_sec += (long) (-1 + (u / 1000000));
137 a->tv_usec = (long) (u % 1000000);
138 a->tv_sec += (long) (u / 1000000);
144 AvahiUsec avahi_age(const struct timeval *a) {
149 gettimeofday(&now, NULL);
151 return avahi_timeval_diff(&now, a);
155 struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
158 gettimeofday(tv, NULL);
161 avahi_timeval_add(tv, (AvahiUsec) msec*1000);
164 avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0)));
169 int avahi_set_cloexec(int fd) {
174 if ((n = fcntl(fd, F_GETFD)) < 0)
180 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
183 int avahi_set_nonblock(int fd) {
188 if ((n = fcntl(fd, F_GETFL)) < 0)
194 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
197 int avahi_wait_for_write(int fd) {
204 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0)
212 /* Read the first label from string *name, unescape "\" and write it to dest */
213 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
238 if (**name == '\\') {
245 *(d++) = *((*name) ++);
256 /* Escape "\" and ".", append \0 */
257 char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size) {
264 assert(*ret_size > 0);
268 while (src_length > 0) {
269 if (*src == '.' || *src == '\\') {
273 *((*ret_name) ++) = '\\';
280 *((*ret_name)++) = *src;
292 int avahi_domain_equal(const char *a, const char *b) {
300 char ca[65], cb[65], *pa, *pb;
302 pa = avahi_unescape_label(&a, ca, sizeof(ca));
303 pb = avahi_unescape_label(&b, cb, sizeof(cb));
307 else if ((pa && !pb) || (!pa && pb))
310 if (strcasecmp(pa, pb))
317 int avahi_binary_domain_cmp(const char *a, const char *b) {
325 char ca[65], cb[65], *pa, *pb;
328 pa = avahi_unescape_label(&a, ca, sizeof(ca));
329 pb = avahi_unescape_label(&b, cb, sizeof(cb));
338 if ((r = strcmp(pa, pb)))
343 void avahi_hexdump(const void* p, size_t size) {
344 const uint8_t *c = p;
347 printf("Dumping %u bytes from %p:\n", size, p);
352 for (i = 0; i < 16; i++) {
354 printf("%02x ", c[i]);
359 for (i = 0; i < 16; i++) {
361 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
377 unsigned avahi_strhash(const char *p) {
381 hash = 31 * hash + *p;
386 unsigned avahi_domain_hash(const char *s) {
392 if (!avahi_unescape_label(&s, c, sizeof(c)))
398 hash += avahi_strhash(avahi_strdown(c));
402 char *avahi_format_mac_address(const uint8_t* mac, size_t size) {
405 static const char hex[] = "0123456789abcdef";
407 t = r = avahi_new(char, size > 0 ? size*3 : 1);
414 for (i = 0; i < size; i++) {
415 *(t++) = hex[*mac >> 4];
416 *(t++) = hex[*mac & 0xF];
426 int avahi_valid_service_type(const char *t) {
436 if (!(p = strchr(t, '.')))
439 if (p - t > 63 || p - t < 2)
448 if (strlen(p) > 63 || strlen(p) < 2)
454 int avahi_valid_domain_name(const char *t) {
463 /* Domains may not start with a dot */
469 for (p = t; *p; p++) {
472 if (dot) /* Two subsequent dots */
488 /* A trailing dot IS allowed */
493 int avahi_valid_service_name(const char *t) {
505 int avahi_valid_host_name(const char *t) {
520 char *avahi_strdown(char *s) {
526 *c = (char) tolower(*c);
531 char *avahi_strup(char *s) {
536 *c = (char) toupper(*c);