X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-common%2Futil.c;h=a977bb2a04f144666b71735d72a5edc9d254e572;hb=1c9effb5f570b2f46720d626d56b650dd0ce7a35;hp=baf552796d9fcdd4bfdfe93877af1c8862fc9833;hpb=9962a048634c590db23a00db1d01daada779844c;p=catta diff --git a/avahi-common/util.c b/avahi-common/util.c index baf5527..a977bb2 100644 --- a/avahi-common/util.c +++ b/avahi-common/util.c @@ -84,13 +84,12 @@ gchar *avahi_normalize_name(const gchar *s) { unescape_uneeded(s, tmp, sizeof(tmp)); - if ((l = strlen(tmp)) == 0) - return g_strdup("."); + l = strlen(tmp); - if (tmp[l-1] == '.') - return g_strdup(tmp); + while (l > 0 && tmp[l-1] == '.') + tmp[--l] = 0; - return g_strdup_printf("%s.", tmp); + return g_strdup(tmp); } gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { @@ -112,16 +111,56 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { return 0; } -glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { +AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { g_assert(a); g_assert(b); if (avahi_timeval_compare(a, b) < 0) - return avahi_timeval_diff(b, a); + return - avahi_timeval_diff(b, a); - return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; + return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; } +GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) { + AvahiUsec u; + g_assert(a); + + u = usec + a->tv_usec; + + if (u < 0) { + a->tv_usec = (glong) (1000000 + (u % 1000000)); + a->tv_sec += (glong) (-1 + (u / 1000000)); + } else { + a->tv_usec = (glong) (u % 1000000); + a->tv_sec += (glong) (u / 1000000); + } + + return a; +} + +AvahiUsec avahi_age(const GTimeVal *a) { + GTimeVal now; + + g_assert(a); + + g_get_current_time(&now); + + return avahi_timeval_diff(&now, a); +} + +GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { + g_assert(tv); + + g_get_current_time(tv); + + if (msec) + avahi_timeval_add(tv, (AvahiUsec) msec*1000); + + if (jitter) + avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000); + + return tv; +} gint avahi_set_cloexec(gint fd) { gint n; @@ -169,30 +208,6 @@ gint avahi_wait_for_write(gint fd) { return 0; } -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { - g_assert(tv); - - g_get_current_time(tv); - - 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; -} - -glong avahi_age(const GTimeVal *a) { - GTimeVal now; - - g_assert(a); - - g_get_current_time(&now); - - return avahi_timeval_diff(&now, a); -} - /* Read the first label from string *name, unescape "\" and write it to dest */ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) { guint i = 0; @@ -399,3 +414,97 @@ gchar *avahi_format_mac_address(const guint8* mac, guint size) { *(--t) = 0; return r; } + +gboolean avahi_valid_service_type(const gchar *t) { + const gchar *p; + g_assert(t); + + if (strlen(t) < 5) + return FALSE; + + if (*t != '_') + return FALSE; + + if (!(p = strchr(t, '.'))) + return FALSE; + + if (p - t > 63 || p - t < 2) + return FALSE; + + if (*(++p) != '_') + return FALSE; + + if (strchr(p, '.')) + return FALSE; + + if (strlen(p) > 63 || strlen(p) < 2) + return FALSE; + + return TRUE; +} + +gboolean avahi_valid_domain_name(const gchar *t) { + const gchar *p, *dp; + gboolean dot = FALSE; + + g_assert(t); + + if (*t == 0) + return FALSE; + + /* Domains may not start with a dot */ + if (*t == '.') + return FALSE; + + dp = t; + + for (p = t; *p; p++) { + + if (*p == '.') { + if (dot) /* Two subsequent dots */ + return FALSE; + + if (p - dp > 63) + return FALSE; + + dot = TRUE; + dp = p + 1; + } else + dot = FALSE; + + } + + if (p - dp > 63) + return FALSE; + + /* A trailing dot IS allowed */ + + return TRUE; +} + +gboolean avahi_valid_service_name(const gchar *t) { + g_assert(t); + + if (*t == 0) + return FALSE; + + if (strlen(t) > 63) + return FALSE; + + return TRUE; +} + +gboolean avahi_valid_host_name(const gchar *t) { + g_assert(t); + + if (*t == 0) + return FALSE; + + if (strlen(t) > 63) + return FALSE; + + if (strchr(t, '.')) + return FALSE; + + return TRUE; +}