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));
89 while (l > 0 && tmp[l-1] == '.')
95 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
99 if (a->tv_sec < b->tv_sec)
102 if (a->tv_sec > b->tv_sec)
105 if (a->tv_usec < b->tv_usec)
108 if (a->tv_usec > b->tv_usec)
114 AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
118 if (avahi_timeval_compare(a, b) < 0)
119 return - avahi_timeval_diff(b, a);
121 return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
124 GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
128 u = usec + a->tv_usec;
131 a->tv_usec = (glong) (1000000 + (u % 1000000));
132 a->tv_sec += (glong) (-1 + (u / 1000000));
134 a->tv_usec = (glong) (u % 1000000);
135 a->tv_sec += (glong) (u / 1000000);
141 AvahiUsec avahi_age(const GTimeVal *a) {
146 g_get_current_time(&now);
148 return avahi_timeval_diff(&now, a);
151 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
154 g_get_current_time(tv);
157 avahi_timeval_add(tv, (AvahiUsec) msec*1000);
160 avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
165 gint avahi_set_cloexec(gint fd) {
170 if ((n = fcntl(fd, F_GETFD)) < 0)
176 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
179 gint avahi_set_nonblock(gint fd) {
184 if ((n = fcntl(fd, F_GETFL)) < 0)
190 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
193 gint avahi_wait_for_write(gint fd) {
200 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
201 g_message("select() failed: %s", strerror(errno));
211 /* Read the first label from string *name, unescape "\" and write it to dest */
212 gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
237 if (**name == '\\') {
244 *(d++) = *((*name) ++);
255 /* Escape "\" and ".", append \0 */
256 gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
263 g_assert(*ret_size > 0);
267 while (src_length > 0) {
268 if (*src == '.' || *src == '\\') {
272 *((*ret_name) ++) = '\\';
279 *((*ret_name)++) = *src;
291 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
299 gchar ca[65], cb[65], *pa, *pb;
301 pa = avahi_unescape_label(&a, ca, sizeof(ca));
302 pb = avahi_unescape_label(&b, cb, sizeof(cb));
306 else if ((pa && !pb) || (!pa && pb))
309 if (g_ascii_strcasecmp(pa, pb))
316 gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
324 gchar ca[65], cb[65], *pa, *pb;
327 pa = avahi_unescape_label(&a, ca, sizeof(ca));
328 pb = avahi_unescape_label(&b, cb, sizeof(cb));
337 if ((r = strcmp(pa, pb)))
342 void avahi_hexdump(gconstpointer p, guint size) {
346 printf("Dumping %u bytes from %p:\n", size, p);
351 for (i = 0; i < 16; i++) {
353 printf("%02x ", c[i]);
358 for (i = 0; i < 16; i++) {
360 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
376 guint avahi_domain_hash(const gchar *s) {
382 if (!avahi_unescape_label(&s, c, sizeof(c)))
388 m = g_ascii_strdown(c, -1);
389 hash += g_str_hash(m);
394 gchar *avahi_format_mac_address(const guint8* mac, guint size) {
397 static const gchar hex[] = "0123456789abcdef";
399 t = r = g_new(gchar, size > 0 ? size*3 : 1);
406 for (i = 0; i < size; i++) {
407 *(t++) = hex[*mac >> 4];
408 *(t++) = hex[*mac & 0xF];