]> git.meshlink.io Git - catta/blobdiff - util.c
Rename flx_* to avahi_*
[catta] / util.c
diff --git a/util.c b/util.c
index 3edaa8878398166b13e36bfc3efdba6a404cabc5..270a48f23de5000d0f29df2c72ed1b4b005b755b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -2,16 +2,23 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
+#include <stdio.h>
 
 #include "util.h"
 
-gchar *flx_get_host_name(void) {
+gchar *avahi_get_host_name(void) {
+#ifdef HOST_NAME_MAX
+    char t[HOST_NAME_MAX];
+#else
     char t[256];
+#endif
     gethostname(t, sizeof(t));
-    return g_strndup(t, sizeof(t));
+    t[sizeof(t)-1] = 0;
+    return avahi_normalize_name(t);
 }
 
-gchar *flx_normalize_name(const gchar *s) {
+gchar *avahi_normalize_name(const gchar *s) {
     size_t l;
     g_assert(s);
 
@@ -26,7 +33,7 @@ gchar *flx_normalize_name(const gchar *s) {
     return g_strdup_printf("%s.", s);
 }
 
-gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
+gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
     g_assert(a);
     g_assert(b);
 
@@ -45,18 +52,18 @@ gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
     return 0;
 }
 
-glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
+glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
     g_assert(a);
     g_assert(b);
 
-    if (flx_timeval_compare(a, b) < 0)
-        return flx_timeval_diff(b, a);
+    if (avahi_timeval_compare(a, b) < 0)
+        return avahi_timeval_diff(b, a);
 
     return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
 }
 
 
-gint flx_set_cloexec(gint fd) {
+gint avahi_set_cloexec(gint fd) {
     gint n;
 
     g_assert(fd >= 0);
@@ -70,7 +77,7 @@ gint flx_set_cloexec(gint fd) {
     return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
 }
 
-gint flx_set_nonblock(gint fd) {
+gint avahi_set_nonblock(gint fd) {
     gint n;
 
     g_assert(fd >= 0);
@@ -84,7 +91,7 @@ gint flx_set_nonblock(gint fd) {
     return fcntl(fd, F_SETFL, n|O_NONBLOCK);
 }
 
-gint flx_wait_for_write(gint fd) {
+gint avahi_wait_for_write(gint fd) {
     fd_set fds;
     gint r;
     
@@ -102,7 +109,7 @@ gint flx_wait_for_write(gint fd) {
     return 0;
 }
 
-GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
+GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
     g_assert(tv);
 
     g_get_current_time(tv);
@@ -116,12 +123,92 @@ GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
     return tv;
 }
 
-gint flx_age(const GTimeVal *a) {
+gint avahi_age(const GTimeVal *a) {
     GTimeVal now;
     
     g_assert(a);
 
     g_get_current_time(&now);
 
-    return flx_timeval_diff(&now, a);
+    return avahi_timeval_diff(&now, a);
+}
+
+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++;
+        
+    }
+}
+
+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;
+
+    return g_int_hash(t);
+}
+
+void avahi_hexdump(gconstpointer p, guint size) {
+    const guint8 *c = p;
+    g_assert(p);
+
+    printf("Dumping %u bytes from %p:\n", size, p);
+    
+    while (size > 0) {
+        guint i;
+
+        for (i = 0; i < 16; i++) { 
+            if (i < size)
+                printf("%02x ", c[i]);
+            else
+                printf("   ");
+        }
+
+        for (i = 0; i < 16; i++) {
+            if (i < size)
+                printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
+            else
+                printf(" ");
+        }
+        
+        printf("\n");
+
+        c += 16;
+
+        if (size <= 16)
+            break;
+        
+        size -= 16;
+    }
 }