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
37 gchar *avahi_get_host_name(void) {
39 char t[HOST_NAME_MAX];
43 gethostname(t, sizeof(t));
45 return avahi_normalize_name(t);
48 static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
49 gboolean escaped = FALSE;
57 if (!escaped && *src == '\\')
59 else if (escaped && (*src == '.' || *src == '\\')) {
61 if ((size -= 2) <= 1) break;
67 if (--size <= 1) break;
80 gchar *avahi_normalize_name(const gchar *s) {
85 unescape_uneeded(s, tmp, sizeof(tmp));
87 if ((l = strlen(tmp)) == 0)
93 return g_strdup_printf("%s.", tmp);
96 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
100 if (a->tv_sec < b->tv_sec)
103 if (a->tv_sec > b->tv_sec)
106 if (a->tv_usec < b->tv_usec)
109 if (a->tv_usec > b->tv_usec)
115 AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
119 if (avahi_timeval_compare(a, b) < 0)
120 return - avahi_timeval_diff(b, a);
122 return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
125 GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
129 u = usec + a->tv_usec;
132 a->tv_usec = (glong) (1000000 + (u % 1000000));
133 a->tv_sec += (glong) (-1 + (u / 1000000));
135 a->tv_usec = (glong) (u % 1000000);
136 a->tv_sec += (glong) (u / 1000000);
142 AvahiUsec avahi_age(const GTimeVal *a) {
147 g_get_current_time(&now);
149 return avahi_timeval_diff(&now, a);
152 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
155 g_get_current_time(tv);
158 avahi_timeval_add(tv, (AvahiUsec) msec*1000);
161 avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
166 gint avahi_set_cloexec(gint fd) {
171 if ((n = fcntl(fd, F_GETFD)) < 0)
177 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
180 gint avahi_set_nonblock(gint fd) {
185 if ((n = fcntl(fd, F_GETFL)) < 0)
191 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
194 gint avahi_wait_for_write(gint fd) {
201 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
202 g_message("select() failed: %s", strerror(errno));
212 /* Read the first label from string *name, unescape "\" and write it to dest */
213 gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
238 if (**name == '\\') {
245 *(d++) = *((*name) ++);
256 /* Escape "\" and ".", append \0 */
257 gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
264 g_assert(*ret_size > 0);
268 while (src_length > 0) {
269 if (*src == '.' || *src == '\\') {
273 *((*ret_name) ++) = '\\';
280 *((*ret_name)++) = *src;
292 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
300 gchar 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 (g_ascii_strcasecmp(pa, pb))
317 gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
325 gchar 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(gconstpointer p, guint size) {
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 guint avahi_domain_hash(const gchar *s) {
383 if (!avahi_unescape_label(&s, c, sizeof(c)))
389 m = g_ascii_strdown(c, -1);
390 hash += g_str_hash(m);
395 gchar *avahi_format_mac_address(const guint8* mac, guint size) {
398 static const gchar hex[] = "0123456789abcdef";
400 t = r = g_new(gchar, size > 0 ? size*3 : 1);
407 for (i = 0; i < size; i++) {
408 *(t++) = hex[*mac >> 4];
409 *(t++) = hex[*mac & 0xF];