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 glong 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 ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
126 gint avahi_set_cloexec(gint fd) {
131 if ((n = fcntl(fd, F_GETFD)) < 0)
137 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
140 gint avahi_set_nonblock(gint fd) {
145 if ((n = fcntl(fd, F_GETFL)) < 0)
151 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
154 gint avahi_wait_for_write(gint fd) {
161 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
162 g_message("select() failed: %s", strerror(errno));
172 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
175 g_get_current_time(tv);
178 g_time_val_add(tv, msec*1000);
181 g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
186 glong avahi_age(const GTimeVal *a) {
191 g_get_current_time(&now);
193 return avahi_timeval_diff(&now, a);
196 /* Read the first label from string *name, unescape "\" and write it to dest */
197 gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
222 if (**name == '\\') {
229 *(d++) = *((*name) ++);
240 /* Escape "\" and ".", append \0 */
241 gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
248 g_assert(*ret_size > 0);
252 while (src_length > 0) {
253 if (*src == '.' || *src == '\\') {
257 *((*ret_name) ++) = '\\';
264 *((*ret_name)++) = *src;
276 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
284 gchar ca[65], cb[65], *pa, *pb;
286 pa = avahi_unescape_label(&a, ca, sizeof(ca));
287 pb = avahi_unescape_label(&b, cb, sizeof(cb));
291 else if ((pa && !pb) || (!pa && pb))
294 if (g_ascii_strcasecmp(pa, pb))
301 gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
309 gchar ca[65], cb[65], *pa, *pb;
312 pa = avahi_unescape_label(&a, ca, sizeof(ca));
313 pb = avahi_unescape_label(&b, cb, sizeof(cb));
322 if ((r = strcmp(pa, pb)))
327 void avahi_hexdump(gconstpointer p, guint size) {
331 printf("Dumping %u bytes from %p:\n", size, p);
336 for (i = 0; i < 16; i++) {
338 printf("%02x ", c[i]);
343 for (i = 0; i < 16; i++) {
345 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
361 guint avahi_domain_hash(const gchar *s) {
367 if (!avahi_unescape_label(&s, c, sizeof(c)))
373 m = g_ascii_strdown(c, -1);
374 hash += g_str_hash(m);
379 gchar *avahi_format_mac_address(const guint8* mac, guint size) {
382 static const gchar hex[] = "0123456789abcdef";
384 t = r = g_new(gchar, size > 0 ? size*3 : 1);
391 for (i = 0; i < size; i++) {
392 *(t++) = hex[*mac >> 4];
393 *(t++) = hex[*mac & 0xF];