]> git.meshlink.io Git - catta/blobdiff - avahi-core/util.c
forgot to pull the publish_no_reverse change to the example.
[catta] / avahi-core / util.c
index fa97eecedbcc3eb6cb33482c09a0eb6becb98955..21ef94c4f405ae0639f44d08d58784be0e08380c 100644 (file)
@@ -1,18 +1,16 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
+
   avahi is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.
+
   avahi is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
   Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public
   License along with avahi; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 #endif
 
 #include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <limits.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <ctype.h>
 
+#include <avahi-common/malloc.h>
 #include "util.h"
 
-gchar *avahi_get_host_name(void) {
-#ifdef HOST_NAME_MAX
-    char t[HOST_NAME_MAX];
-#else
-    char t[256];
-#endif
-    gethostname(t, sizeof(t));
-    t[sizeof(t)-1] = 0;
-    return avahi_normalize_name(t);
-}
-
-gchar *avahi_normalize_name(const gchar *s) {
-    size_t l;
-    g_assert(s);
-
-    l = strlen(s);
-
-    if (!l)
-        return g_strdup(".");
-
-    if (s[l-1] == '.')
-        return g_strdup(s);
-    
-    return g_strdup_printf("%s.", s);
-}
-
-gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
-    g_assert(a);
-    g_assert(b);
-
-    if (a->tv_sec < b->tv_sec)
-        return -1;
-
-    if (a->tv_sec > b->tv_sec)
-        return 1;
-
-    if (a->tv_usec < b->tv_usec)
-        return -1;
-
-    if (a->tv_usec > b->tv_usec)
-        return 1;
-
-    return 0;
-}
-
-glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
-    g_assert(a);
-    g_assert(b);
+void avahi_hexdump(const void* p, size_t size) {
+    const uint8_t *c = p;
+    assert(p);
 
-    if (avahi_timeval_compare(a, b) < 0)
-        return avahi_timeval_diff(b, a);
+    printf("Dumping %lu bytes from %p:\n", (unsigned long) size, p);
 
-    return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
-}
-
-
-gint avahi_set_cloexec(gint fd) {
-    gint n;
-
-    g_assert(fd >= 0);
-    
-    if ((n = fcntl(fd, F_GETFD)) < 0)
-        return -1;
-
-    if (n & FD_CLOEXEC)
-        return 0;
-
-    return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
-}
+    while (size > 0) {
+        unsigned i;
 
-gint avahi_set_nonblock(gint fd) {
-    gint n;
+        for (i = 0; i < 16; i++) {
+            if (i < size)
+                printf("%02x ", c[i]);
+            else
+                printf("   ");
+        }
 
-    g_assert(fd >= 0);
+        for (i = 0; i < 16; i++) {
+            if (i < size)
+                printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
+            else
+                printf(" ");
+        }
 
-    if ((n = fcntl(fd, F_GETFL)) < 0)
-        return -1;
+        printf("\n");
 
-    if (n & O_NONBLOCK)
-        return 0;
+        c += 16;
 
-    return fcntl(fd, F_SETFL, n|O_NONBLOCK);
-}
+        if (size <= 16)
+            break;
 
-gint avahi_wait_for_write(gint fd) {
-    fd_set fds;
-    gint r;
-    
-    FD_ZERO(&fds);
-    FD_SET(fd, &fds);
-    
-    if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
-        g_message("select() failed: %s", strerror(errno));
-
-        return -1;
+        size -= 16;
     }
-    
-    g_assert(r > 0);
-
-    return 0;
 }
 
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
-    g_assert(tv);
+char *avahi_format_mac_address(char *r, size_t l, const uint8_t* mac, size_t size) {
+    char *t = r;
+    unsigned i;
+    static const char hex[] = "0123456789abcdef";
 
-    g_get_current_time(tv);
+    assert(r);
+    assert(l > 0);
+    assert(mac);
 
-    if (msec)
-        g_time_val_add(tv, msec*1000);
-
-    if (jitter)
-        g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
-        
-    return tv;
-}
+    if (size <= 0) {
+        *r = 0;
+        return r;
+    }
 
-gint avahi_age(const GTimeVal *a) {
-    GTimeVal now;
-    
-    g_assert(a);
+    for (i = 0; i < size; i++) {
+        if (l < 3)
+            break;
 
-    g_get_current_time(&now);
+        *(t++) = hex[*mac >> 4];
+        *(t++) = hex[*mac & 0xF];
+        *(t++) = ':';
 
-    return avahi_timeval_diff(&now, a);
-}
+        l -= 3;
 
-gboolean avahi_domain_cmp(const gchar *a, const gchar *b) {
-    int escaped_a = 0, escaped_b = 0;
-    g_assert(a);
-    g_assert(b);
-
-    for (;;) {
-        /* Check for escape characters "\" */
-        if ((escaped_a = *a == '\\'))
-            a ++;
-
-        if ((escaped_b = *b == '\\'))
-            b++;
-
-        /* Check for string end */
-        if (*a == 0 && *b == 0)
-            return 0;
-        
-        if (*a == 0 && !escaped_b && *b == '.' && *(b+1) == 0)
-            return 0;
-
-        if (!escaped_a && *a == '.' && *(a+1) == 0 && *b == 0)
-            return 0;
-
-        /* Compare characters */
-        if (escaped_a == escaped_b && *a != *b)
-            return *a < *b ? -1 : 1;
-
-        /* Next characters */
-        a++;
-        b++;
-        
+        mac++;
     }
-}
-
-gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
-    return avahi_domain_cmp(a, b) == 0;
-}
 
-guint avahi_domain_hash(const gchar *p) {
-    char t[256];
-    strncpy(t, p, sizeof(t)-1);
-    t[sizeof(t)-1] = 0;
+    if (t > r)
+        *(t-1) = 0;
+    else
+        *r = 0;
 
-    return g_int_hash(t);
+    return r;
 }
 
-void avahi_hexdump(gconstpointer p, guint size) {
-    const guint8 *c = p;
-    g_assert(p);
+char *avahi_strup(char *s) {
+    char *c;
+    assert(s);
 
-    printf("Dumping %u bytes from %p:\n", size, p);
-    
-    while (size > 0) {
-        guint i;
+    for (c = s; *c; c++)
+        *c = (char) toupper(*c);
 
-        for (i = 0; i < 16; i++) { 
-            if (i < size)
-                printf("%02x ", c[i]);
-            else
-                printf("   ");
-        }
+    return s;
+}
 
-        for (i = 0; i < 16; i++) {
-            if (i < size)
-                printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
-            else
-                printf(" ");
-        }
-        
-        printf("\n");
+char *avahi_strdown(char *s) {
+    char *c;
+    assert(s);
 
-        c += 16;
+    for (c = s; *c; c++)
+        *c = (char) tolower(*c);
 
-        if (size <= 16)
-            break;
-        
-        size -= 16;
-    }
+    return s;
 }