From e63a65b3955b173a3e8d6b78c6377a518a9922d6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Aug 2005 23:45:42 +0000 Subject: [PATCH] * drop glib from avahi-common * add new module with avahi_malloc() and friends git-svn-id: file:///home/lennart/svn/public/avahi/trunk@298 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/Makefile.am | 15 +- avahi-common/address.c | 70 ++++---- avahi-common/address.h | 31 ++-- avahi-common/alternative-test.c | 13 +- avahi-common/alternative.c | 56 ++++--- avahi-common/alternative.h | 6 +- avahi-common/domain-test.c | 25 +-- avahi-common/llist.h | 12 +- avahi-common/malloc.c | 171 +++++++++++++++++++ avahi-common/malloc.h | 75 +++++++++ avahi-common/strlst-test.c | 23 +-- avahi-common/strlst.c | 86 +++++----- avahi-common/strlst.h | 30 ++-- avahi-common/timeval-test.c | 34 +++- avahi-common/util.c | 282 ++++++++++++++++++-------------- avahi-common/util.h | 52 +++--- avahi-core/announce.c | 10 +- avahi-core/browse.c | 4 +- avahi-core/cache.c | 10 +- avahi-core/cache.h | 4 +- avahi-core/probe-sched.c | 8 +- avahi-core/query-sched.c | 10 +- avahi-core/resolve-address.c | 2 +- avahi-core/resolve-host-name.c | 2 +- avahi-core/resolve-service.c | 2 +- avahi-core/response-sched.c | 12 +- avahi-core/server.c | 8 +- avahi-core/server.h | 4 +- avahi-core/timeeventq-test.c | 4 +- avahi-core/timeeventq.c | 31 ++-- avahi-core/timeeventq.h | 10 +- doxygen.cfg | 2 +- 32 files changed, 733 insertions(+), 371 deletions(-) create mode 100644 avahi-common/malloc.c create mode 100644 avahi-common/malloc.h diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am index ad8d1a7..3b025be 100644 --- a/avahi-common/Makefile.am +++ b/avahi-common/Makefile.am @@ -34,7 +34,8 @@ avahi_commoninclude_HEADERS = \ alternative.h \ util.h \ cdecl.h \ - defs.h + defs.h \ + malloc.h if ENABLE_DBUS avahi_commoninclude_HEADERS += dbus.h @@ -52,34 +53,40 @@ lib_LTLIBRARIES = \ libavahi-common.la libavahi_common_la_SOURCES = \ + malloc.c malloc.h \ address.c address.h \ - strlst.c strlst.h \ alternative.c alternative.h \ - util.c util.h \ - error.c error.h + error.c error.h \ + strlst.c strlst.h \ + util.c util.h libavahi_common_la_CFLAGS = $(AM_CFLAGS) libavahi_common_la_LIBADD = $(AM_LDADD) strlst_test_SOURCES = \ strlst.c strlst.h \ + malloc.c malloc.h \ strlst-test.c strlst_test_CFLAGS = $(AM_CFLAGS) strlst_test_LDADD = $(AM_LDADD) alternative_test_SOURCES = \ alternative.c alternative.h \ + malloc.c malloc.h \ + util.c util.h \ alternative-test.c alternative_test_CFLAGS = $(AM_CFLAGS) alternative_test_LDADD = $(AM_LDADD) domain_test_SOURCES = \ util.c util.h \ + malloc.c malloc.h \ domain-test.c domain_test_CFLAGS = $(AM_CFLAGS) domain_test_LDADD = $(AM_LDADD) timeval_test_SOURCES = \ util.c util.h \ + malloc.c malloc.h \ timeval-test.c timeval_test_CFLAGS = $(AM_CFLAGS) timeval_test_LDADD = $(AM_LDADD) diff --git a/avahi-common/address.c b/avahi-common/address.c index 8f1848e..c638b41 100644 --- a/avahi-common/address.c +++ b/avahi-common/address.c @@ -27,11 +27,14 @@ #include #include #include +#include #include "address.h" +#include "util.h" +#include "malloc.h" -guint avahi_address_get_size(const AvahiAddress *a) { - g_assert(a); +size_t avahi_address_get_size(const AvahiAddress *a) { + assert(a); if (a->family == AVAHI_PROTO_INET) return 4; @@ -41,9 +44,9 @@ guint avahi_address_get_size(const AvahiAddress *a) { return 0; } -gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) { - g_assert(a); - g_assert(b); +int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) { + assert(a); + assert(b); if (a->family != b->family) return -1; @@ -51,23 +54,24 @@ gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) { return memcmp(a->data.data, b->data.data, avahi_address_get_size(a)); } -gchar *avahi_address_snprint(char *s, guint length, const AvahiAddress *a) { - g_assert(s); - g_assert(length); - g_assert(a); - return (gchar*) inet_ntop(a->family, a->data.data, s, length); +char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) { + assert(s); + assert(length); + assert(a); + + return (char*) inet_ntop(a->family, a->data.data, s, length); } -gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) { - guint32 n = ntohl(a->address); - g_assert(a); +char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) { + uint32_t n = ntohl(a->address); + assert(a); - return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24); + return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24); } -static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *suffix) { +static char *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const char *suffix) { - return g_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s", + return avahi_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s", a->address[15] & 0xF, a->address[15] >> 4, a->address[14] & 0xF, @@ -103,17 +107,17 @@ static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *s suffix); } -gchar *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) { +char *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) { return reverse_lookup_name_ipv6(a, "ip6.arpa"); } -gchar *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) { +char *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) { return reverse_lookup_name_ipv6(a, "ip6.int"); } -AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAddress *ret_addr) { - g_assert(ret_addr); - g_assert(s); +AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol family, AvahiAddress *ret_addr) { + assert(ret_addr); + assert(s); if (family == AVAHI_PROTO_UNSPEC) { if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) { @@ -134,10 +138,10 @@ AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAdd } AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) { - g_assert(sa); - g_assert(ret_addr); + assert(sa); + assert(ret_addr); - g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); + assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); ret_addr->family = sa->sa_family; @@ -149,10 +153,10 @@ AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddres return ret_addr; } -guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) { - g_assert(sa); +uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) { + assert(sa); - g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); + assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); if (sa->sa_family == AF_INET) return ntohs(((const struct sockaddr_in*) sa)->sin_port); @@ -160,16 +164,18 @@ guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) { return ntohs(((const struct sockaddr_in6*) sa)->sin6_port); } -gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) { - static const guint8 ipv4_in_ipv6[] = { +int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) { + + static const uint8_t ipv4_in_ipv6[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0xFF, 0xFF, 0xFF, 0xFF }; + 0xFF, 0xFF, 0xFF, 0xFF + }; - g_assert(a); + assert(a); if (a->family != AVAHI_PROTO_INET6) - return FALSE; + return 0; return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0; } diff --git a/avahi-common/address.h b/avahi-common/address.h index 3b34551..8056ac9 100644 --- a/avahi-common/address.h +++ b/avahi-common/address.h @@ -23,7 +23,8 @@ ***/ #include -#include +#include + #include /** \file address.h Defintions and functions to manipulate IP addresses. */ @@ -31,10 +32,10 @@ AVAHI_C_DECL_BEGIN /** Protocol family specification, takes the values AVAHI_INET, AVAHI_INET6, AVAHI_UNSPEC */ -typedef guchar AvahiProtocol; +typedef unsigned char AvahiProtocol; /** Numeric network interface index. Takes OS dependent values and the special constant AVAHI_IF_UNSPEC */ -typedef gint AvahiIfIndex; +typedef int AvahiIfIndex; /** Values for AvahiProtocol */ enum { @@ -50,12 +51,12 @@ enum { /** An IPv4 address */ typedef struct { - guint32 address; /**< Address data in network byte order. */ + uint32_t address; /**< Address data in network byte order. */ } AvahiIPv4Address; /** An IPv6 address */ typedef struct { - guint8 address[16]; /**< Address data */ + uint8_t address[16]; /**< Address data */ } AvahiIPv6Address; /** Protocol (address family) independent address structure */ @@ -65,18 +66,18 @@ typedef struct { union { AvahiIPv6Address ipv6; /** Address when IPv6 */ AvahiIPv4Address ipv4; /** Address when IPv4 */ - guint8 data[1]; /** Type independant data field */ + uint8_t data[1]; /** Type independant data field */ } data; } AvahiAddress; /** Return the address data size of the specified address. (4 for IPv4, 16 for IPv6) */ -guint avahi_address_get_size(const AvahiAddress *a); +size_t avahi_address_get_size(const AvahiAddress *a); /** Compare two addresses. Returns 0 when equal, a negative value when a < b, a positive value when a > b. */ -gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b); +int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b); /** Convert the specified address *a to a human readable character string */ -gchar *avahi_address_snprint(char *ret_s, guint length, const AvahiAddress *a); +char *avahi_address_snprint(char *ret_s, size_t length, const AvahiAddress *a); /** Convert the specifeid human readable character string to an * address structure. Set af to AVAHI_UNSPEC for automatic address @@ -87,20 +88,20 @@ AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol af, AvahiAddress AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr); /** Return the port number of a sockaddr structure (either IPv4 or IPv6) */ -guint16 avahi_port_from_sockaddr(const struct sockaddr* sa); +uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa); /** Generate the DNS reverse lookup name for an IPv4 address. g_free() the result! */ -gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a); +char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a); /** Generate the modern DNS reverse lookup name for an IPv6 address, ending in ipv6.arpa. g_free() the result! */ -gchar* avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a); +char* avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a); /** Generate the historic DNS reverse lookup name for an IPv6 address, ending in ipv6.int. g_free() the result! */ -gchar* avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a); +char* avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a); /** Check whether the specified IPv6 address is in fact an - * encapsulated IPv4 address */ -gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a); + * encapsulated IPv4 address, returns 1 if yes, 0 otherwise */ +int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a); AVAHI_C_DECL_END diff --git a/avahi-common/alternative-test.c b/avahi-common/alternative-test.c index 9e26d24..7f0223a 100644 --- a/avahi-common/alternative-test.c +++ b/avahi-common/alternative-test.c @@ -26,23 +26,24 @@ #include #include "alternative.h" +#include "malloc.h" int main(int argc, char *argv[]) { - gchar *r = NULL; - gint i, k; + char *r = NULL; + int i, k; for (k = 0; k < 2; k++) { for (i = 0; i < 20; i++) { - gchar *n; + char *n; - n = i == 0 ? g_strdup("gurke") : (k ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r)); - g_free(r); + n = i == 0 ? avahi_strdup("gurke") : (k ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r)); + avahi_free(r); r = n; printf("%s\n", r); } } - g_free(r); + avahi_free(r); } diff --git a/avahi-common/alternative.c b/avahi-common/alternative.c index b1b52de..53a1e6c 100644 --- a/avahi-common/alternative.c +++ b/avahi-common/alternative.c @@ -26,38 +26,47 @@ #include #include #include +#include #include "alternative.h" +#include "malloc.h" +#include "util.h" -gchar * avahi_alternative_host_name(const gchar *s) { - const gchar *p, *e = NULL; - gchar *c, *r; - gint n; +char * avahi_alternative_host_name(const char *s) { + const char *p, *e; + char *r; - g_assert(s); + assert(s); + + e = s; for (p = s; *p; p++) if (!isdigit(*p)) e = p+1; - if (e && *e) - n = atoi(e)+1; - else - n = 2; + if (*e) { + char *c; + + if (!(c = avahi_strndup(s, e-s))) + return NULL; - c = e ? g_strndup(s, e-s) : g_strdup(s); - r = g_strdup_printf("%s%i", c, n); - g_free(c); + r = avahi_strdup_printf("%s%i", c, atoi(e)+1); + avahi_free(c); + + } else + r = avahi_strdup_printf("%s2", s); return r; } -gchar *avahi_alternative_service_name(const gchar *s) { - const gchar *e; - g_assert(s); +char *avahi_alternative_service_name(const char *s) { + const char *e; + char *r; + + assert(s); if ((e = strstr(s, " #"))) { - const gchar *n, *p; + const char *n, *p; e += 2; while ((n = strstr(e, " #"))) @@ -71,10 +80,15 @@ gchar *avahi_alternative_service_name(const gchar *s) { } if (e) { - gchar *r, *c = g_strndup(s, e-s); - r = g_strdup_printf("%s%i", c, atoi(e)+1); - g_free(c); - return r; + char *c; + + if (!(c = avahi_strndup(s, e-s))) + return NULL; + + r = avahi_strdup_printf("%s%i", c, atoi(e)+1); + avahi_free(c); } else - return g_strdup_printf("%s #2", s); + r = avahi_strdup_printf("%s #2", s); + + return r; } diff --git a/avahi-common/alternative.h b/avahi-common/alternative.h index c1bf8cb..3d7b2fe 100644 --- a/avahi-common/alternative.h +++ b/avahi-common/alternative.h @@ -22,8 +22,6 @@ USA. ***/ -#include - #include /** \file alternative.h Functions to find alternative names for hosts and services in the case of name collision */ @@ -34,13 +32,13 @@ AVAHI_C_DECL_BEGIN * original host name, "2" is appended, Afterwards the number is * increased on each call. (i.e. "foo" becomes "foo2" becomes "foo3" * and so on.) g_free() the result. */ -gchar *avahi_alternative_host_name(const gchar *s); +char *avahi_alternative_host_name(const char *s); /** Find an alternative for the specified service name. If called with an original service name, " #2" is appended. Afterwards the number is increased on each call (i.e. "foo" becomes "foo #2" becomes "foo #3" and so on.) g_free() the result. */ -gchar *avahi_alternative_service_name(const gchar *s); +char *avahi_alternative_service_name(const char *s); AVAHI_C_DECL_END diff --git a/avahi-common/domain-test.c b/avahi-common/domain-test.c index 240f960..a9ad0b6 100644 --- a/avahi-common/domain-test.c +++ b/avahi-common/domain-test.c @@ -23,26 +23,29 @@ #include #endif +#include + #include "util.h" +#include "malloc.h" int main(int argc, char *argv[]) { - gchar *s; + char *s; - g_message("host name: %s", s = avahi_get_host_name()); - g_free(s); + printf("host name: %s\n", s = avahi_get_host_name()); + avahi_free(s); - g_message("%s", s = avahi_normalize_name("foo.foo.")); - g_free(s); + printf("%s\n", s = avahi_normalize_name("foo.foo.")); + avahi_free(s); - g_message("%s", s = avahi_normalize_name("\\f\\o\\\\o\\..\\f\\ \\o\\o.")); - g_free(s); + printf("%s\n", s = avahi_normalize_name("\\f\\o\\\\o\\..\\f\\ \\o\\o.")); + avahi_free(s); - g_message("%i", avahi_domain_equal("\\aaa bbb\\.cccc\\\\.dee.fff.", "aaa\\ bbb\\.cccc\\\\.dee.fff")); - g_message("%i", avahi_domain_equal("\\A", "a")); + printf("%i\n", avahi_domain_equal("\\aaa bbb\\.cccc\\\\.dee.fff.", "aaa\\ bbb\\.cccc\\\\.dee.fff")); + printf("%i\n", avahi_domain_equal("\\A", "a")); - g_message("%i", avahi_domain_equal("a", "aaa")); + printf("%i\n", avahi_domain_equal("a", "aaa")); - g_message("%u = %u", avahi_domain_hash("\\Aaaab\\\\."), avahi_domain_hash("aaaa\\b\\\\")); + printf("%u = %u\n", avahi_domain_hash("\\Aaaab\\\\."), avahi_domain_hash("aaaa\\b\\\\")); return 0; } diff --git a/avahi-common/llist.h b/avahi-common/llist.h index 7c28f31..4f48b55 100644 --- a/avahi-common/llist.h +++ b/avahi-common/llist.h @@ -22,10 +22,10 @@ USA. ***/ -#include - /* Some macros for maintaining doubly linked lists */ +#include + /* The head of the linked list. Use this in the structure that shall * contain the head of the linked list */ #define AVAHI_LLIST_HEAD(t,name) t *name @@ -39,14 +39,14 @@ /* Initialize a list item */ #define AVAHI_LLIST_INIT(t,name,item) do { \ t *_item = (item); \ - g_assert(_item); \ + assert(_item); \ _item->name##_prev = _item->name##_next = NULL; \ } while(0) /* Prepend an item to the list */ #define AVAHI_LLIST_PREPEND(t,name,head,item) do { \ t **_head = &(head), *_item = (item); \ - g_assert(_item); \ + assert(_item); \ if ((_item->name##_next = *_head)) \ _item->name##_next->name##_prev = _item; \ _item->name##_prev = NULL; \ @@ -56,13 +56,13 @@ /* Remove an item from the list */ #define AVAHI_LLIST_REMOVE(t,name,head,item) do { \ t **_head = &(head), *_item = (item); \ - g_assert(_item); \ + assert(_item); \ if (_item->name##_next) \ _item->name##_next->name##_prev = _item->name##_prev; \ if (_item->name##_prev) \ _item->name##_prev->name##_next = _item->name##_next; \ else {\ - g_assert(*_head == _item); \ + assert(*_head == _item); \ *_head = _item->name##_next; \ } \ _item->name##_next = _item->name##_prev = NULL; \ diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c new file mode 100644 index 0000000..19e1e59 --- /dev/null +++ b/avahi-common/malloc.c @@ -0,0 +1,171 @@ +/* $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 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include "malloc.h" + +static const AvahiAllocator *allocator = NULL; + +void *avahi_malloc(size_t size) { + + if (size <= 0) + return NULL; + + if (!allocator) + return malloc(size); + + assert(allocator->malloc); + return allocator->malloc(size); +} + +void *avahi_malloc0(size_t size) { + void *p; + + if (size <= 0) + return NULL; + + if (!allocator) + return calloc(1, size); + + if (allocator->calloc) + return allocator->calloc(1, size); + + assert(allocator->malloc); + if ((p = allocator->malloc(size))) + memset(p, 0, size); + + return p; +} + +void avahi_free(void *p) { + + if (!p) + return; + + if (!allocator) { + free(p); + return; + } + + assert(allocator->free); + allocator->free(p); +} + +void *avahi_realloc(void *p, size_t size) { + + if (!allocator) + return realloc(p, size); + + assert(allocator->realloc); + return allocator->realloc(p, size); +} + +char *avahi_strdup(const char *s) { + char *r; + size_t size; + + if (!s) + return NULL; + + size = strlen(s); + if (!(r = avahi_malloc(size+1))) + return NULL; + + memcpy(r, s, size+1); + return r; +} + +char *avahi_strndup(const char *s, size_t max) { + char *r; + size_t size; + + if (!s) + return NULL; + + size = strlen(s); + + if (size > max) + size = max; + + if (!(r = avahi_malloc(size+1))) + return NULL; + + memcpy(r, s, size); + r[size] = 0; + return r; +} + +/* Change the allocator */ +void avahi_set_allocator(const AvahiAllocator *a) { + allocator = a; +} + +char *avahi_strdup_vprintf(const char *fmt, va_list ap) { + size_t len = 100; + char *buf; + + assert(fmt); + + if (!(buf = avahi_malloc(len))) + return NULL; + + for (;;) { + int n; + char *nbuf; + + n = vsnprintf(buf, len, fmt, ap); + + if (n >= 0 && n < (int) len) + return buf; + + if (n >= 0) + len = n+1; + else + len *= 2; + + if (!(nbuf = avahi_realloc(buf, len))) { + avahi_free(buf); + return NULL; + } + + buf = nbuf; + } +} + +char *avahi_strdup_printf(const char *fmt, ... ) { + char *s; + va_list ap; + + va_start(ap, fmt); + s = avahi_strdup_vprintf(fmt, ap); + va_end(ap); + + return s; +} + diff --git a/avahi-common/malloc.h b/avahi-common/malloc.h new file mode 100644 index 0000000..11ac9cf --- /dev/null +++ b/avahi-common/malloc.h @@ -0,0 +1,75 @@ +#ifndef foomallochfoo +#define foomallochfoo + +/* $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 + USA. +***/ + +#include +#include + +/** Allocate some memory, just like the libc malloc() */ +void *avahi_malloc(size_t size); + +/** Similar to avahi_malloc() but set the memory to zero */ +void *avahi_malloc0(size_t size); + +/** Free some memory */ +void avahi_free(void *p); + +/** Similar to libc's realloc() */ +void *avahi_realloc(void *p, size_t size); + +/** Allocate n new structures of the specified type. */ +#define avahi_new(type, n) (type*) avahi_malloc(n*sizeof(type)) + +/** Same as avahi_new() but set the memory to zero */ +#define avahi_new0(type, n) (type*) avahi_malloc0(n*sizeof(type)) + +/* Just like libc's strdup() */ +char *avahi_strdup(const char *s); + +/* Just like libc's strndup() */ +char *avahi_strndup(const char *s, size_t l); + +/** Wraps allocator functions */ +typedef struct AvahiAllocator AvahiAllocator; +struct AvahiAllocator { + void* (*malloc)(size_t size); + void (*free)(void *p); + void* (*realloc)(void *p, size_t size); + void* (*calloc)(size_t nmemb, size_t size); /**< May be NULL */ +}; + +/* Change the allocator. May be NULL to return to default (libc) + * allocators. The structure is not copied! */ +void avahi_set_allocator(const AvahiAllocator *a); + + +char *avahi_strdup_vprintf(const char *fmt, va_list ap); + +#ifdef __GNUC__ +char *avahi_strdup_printf(const char *fmt, ... ) __attribute__ ((format(printf, 1, 2))); +#else +char *avahi_strdup_printf(const char *fmt, ... ); +#endif + + +#endif diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c index 58ca60d..12aa623 100644 --- a/avahi-common/strlst-test.c +++ b/avahi-common/strlst-test.c @@ -23,16 +23,17 @@ #include #endif -#include #include +#include #include "strlst.h" +#include "malloc.h" int main(int argc, char *argv[]) { - gchar *t; - guint8 data[1024]; + char *t; + uint8_t data[1024]; AvahiStringList *a = NULL, *b; - guint size, n; + size_t size, n; a = avahi_string_list_new("prefix", "a", "b", NULL); @@ -40,18 +41,18 @@ int main(int argc, char *argv[]) { a = avahi_string_list_add(a, "foo"); a = avahi_string_list_add(a, "bar"); a = avahi_string_list_add(a, "quux"); - a = avahi_string_list_add_arbitrary(a, (const guint8*) "null\0null", 9); + a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9); a = avahi_string_list_add(a, "end"); t = avahi_string_list_to_string(a); printf("--%s--\n", t); - g_free(t); + avahi_free(t); size = avahi_string_list_serialize(a, data, sizeof(data)); printf("%u\n", size); - for (t = (gchar*) data, n = 0; n < size; n++, t++) { + for (t = (char*) data, n = 0; n < size; n++, t++) { if (*t <= 32) printf("(%u)", *t); else @@ -62,21 +63,21 @@ int main(int argc, char *argv[]) { b = avahi_string_list_parse(data, size); - g_assert(avahi_string_list_equal(a, b)); + assert(avahi_string_list_equal(a, b)); t = avahi_string_list_to_string(b); printf("--%s--\n", t); - g_free(t); + avahi_free(t); avahi_string_list_free(b); b = avahi_string_list_copy(a); - g_assert(avahi_string_list_equal(a, b)); + assert(avahi_string_list_equal(a, b)); t = avahi_string_list_to_string(b); printf("--%s--\n", t); - g_free(t); + avahi_free(t); avahi_string_list_free(a); avahi_string_list_free(b); diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c index 46ed852..8a05e02 100644 --- a/avahi-common/strlst.c +++ b/avahi-common/strlst.c @@ -25,25 +25,30 @@ #include #include +#include #include "strlst.h" +#include "malloc.h" -AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, guint size) { +AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) { AvahiStringList *n; - n = g_malloc(sizeof(AvahiStringList) + size); + if (!(n = avahi_malloc(sizeof(AvahiStringList) + size))) + return NULL; + n->next = l; n->size = size; return n; } -AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint8*text, guint size) { +AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) { AvahiStringList *n; - g_assert(text); + assert(text); - n = avahi_string_list_add_anonymous(l, size); + if (!(n = avahi_string_list_add_anonymous(l, size))) + return NULL; if (size > 0) memcpy(n->text, text, size); @@ -51,20 +56,21 @@ AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint return n; } -AvahiStringList *avahi_string_list_add(AvahiStringList *l, const gchar *text) { - g_assert(text); +AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) { + assert(text); - return avahi_string_list_add_arbitrary(l, (const guint8*) text, strlen(text)); + return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text)); } -AvahiStringList *avahi_string_list_parse(gconstpointer data, guint size) { +AvahiStringList *avahi_string_list_parse(const void* data, size_t size) { AvahiStringList *r = NULL; - const guint8 *c; - g_assert(data); + const uint8_t *c; + + assert(data); c = data; for (;;) { - guint k; + size_t k; if (size < 1) break; @@ -84,7 +90,7 @@ void avahi_string_list_free(AvahiStringList *l) { while (l) { n = l->next; - g_free(l); + avahi_free(l); l = n; } } @@ -102,10 +108,10 @@ AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) { return r; } -gchar* avahi_string_list_to_string(AvahiStringList *l) { +char* avahi_string_list_to_string(AvahiStringList *l) { AvahiStringList *n; - guint s = 0; - gchar *t, *e; + size_t s = 0; + char *t, *e; l = avahi_string_list_reverse(l); @@ -116,19 +122,22 @@ gchar* avahi_string_list_to_string(AvahiStringList *l) { s += n->size+2; } - t = e = g_new(gchar, s+1); + if (!(t = e = avahi_new(char, s+1))) { + l = avahi_string_list_reverse(l); + return NULL; + } for (n = l; n; n = n->next) { if (n != l) *(e++) = ' '; *(e++) = '"'; - strncpy(e, (gchar*) n->text, n->size); + strncpy(e, (char*) n->text, n->size); e[n->size] = 0; e = strchr(e, 0); *(e++) = '"'; - g_assert(e); + assert(e); } l = avahi_string_list_reverse(l); @@ -138,20 +147,20 @@ gchar* avahi_string_list_to_string(AvahiStringList *l) { return t; } -guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size) { - guint used = 0; +size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) { + size_t used = 0; if (data) { - guint8 *c; + uint8_t *c; AvahiStringList *n; - g_assert(data); + assert(data); l = avahi_string_list_reverse(l); c = data; for (n = l; n; n = n->next) { - guint k; + size_t k; if (size < 1) break; @@ -174,7 +183,7 @@ guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size) AvahiStringList *n; for (n = l; n; n = n->next) { - guint k; + size_t k; k = n->size; if (k > 255) @@ -187,20 +196,20 @@ guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size) return used; } -gboolean avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) { +int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) { for (;;) { if (!a && !b) - return TRUE; + return 1; if (!a || !b) - return FALSE; + return 0; if (a->size != b->size) - return FALSE; + return 0; if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0) - return FALSE; + return 0; a = a->next; b = b->next; @@ -218,16 +227,15 @@ AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) { } AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) { - const gchar *txt; + const char *txt; - while ((txt = va_arg(va, const gchar*))) + while ((txt = va_arg(va, const char*))) r = avahi_string_list_add(r, txt); return r; } - -AvahiStringList *avahi_string_list_new(const gchar *txt, ...) { +AvahiStringList *avahi_string_list_new(const char *txt, ...) { va_list va; AvahiStringList *r = NULL; @@ -255,11 +263,11 @@ AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) { return avahi_string_list_reverse(r); } -AvahiStringList *avahi_string_list_new_from_array(const gchar *array[], gint length) { +AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) { AvahiStringList *r = NULL; - gint i; + int i; - g_assert(array); + assert(array); for (i = 0; length >= 0 ? i < length : !!array[i]; i++) r = avahi_string_list_add(r, array[i]); @@ -267,8 +275,8 @@ AvahiStringList *avahi_string_list_new_from_array(const gchar *array[], gint len return r; } -guint avahi_string_list_length(const AvahiStringList *l) { - guint n = 0; +unsigned avahi_string_list_length(const AvahiStringList *l) { + unsigned n = 0; for (; l; l = l->next) n++; diff --git a/avahi-common/strlst.h b/avahi-common/strlst.h index a4a4202..78cffb4 100644 --- a/avahi-common/strlst.h +++ b/avahi-common/strlst.h @@ -22,7 +22,10 @@ USA. ***/ -#include +#include +#include +#include + #include /** \file strlst.h Implementation of a data type to store lists of strings */ @@ -37,14 +40,14 @@ AVAHI_C_DECL_BEGIN * primarily for storing DNS TXT record data. */ typedef struct AvahiStringList { struct AvahiStringList *next; /**< Pointe to the next linked list element */ - guint size; /**< Size of text[] */ - guint8 text[1]; /**< Character data */ + size_t size; /**< Size of text[] */ + uint8_t text[1]; /**< Character data */ } AvahiStringList; /** Create a new string list by taking a variable list of NUL * terminated strings. The strings are copied using g_strdup(). The * argument list must be terminated by a NULL pointer. */ -AvahiStringList *avahi_string_list_new(const gchar *txt, ...); +AvahiStringList *avahi_string_list_new(const char *txt, ...); /** Same as avahi_string_list_new() but pass a va_list structure */ AvahiStringList *avahi_string_list_new_va(va_list va); @@ -52,7 +55,7 @@ AvahiStringList *avahi_string_list_new_va(va_list va); /** Create a new string list from a string array. The strings are * copied using g_strdup(). length should contain the length of the * array, or -1 if the array is NULL terminated*/ -AvahiStringList *avahi_string_list_new_from_array(const gchar **array, gint length); +AvahiStringList *avahi_string_list_new_from_array(const char **array, int length); /** Free a string list */ void avahi_string_list_free(AvahiStringList *l); @@ -60,18 +63,18 @@ void avahi_string_list_free(AvahiStringList *l); /** Append a NUL terminated string to the specified string list. The * passed string is copied using g_strdup(). Returns the new list * start. */ -AvahiStringList *avahi_string_list_add(AvahiStringList *l, const gchar *text); +AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text); /** Append an arbitrary length byte string to the list. Returns the * new list start. */ -AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint8 *text, guint size); +AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t *text, size_t size); /** Append a new entry to the string list. The string is not filled with data. The caller should fill in string data afterwards by writing it to l->text, where l is the pointer returned by this function. This function exists solely to optimize a few operations where otherwise superfluous string copying would be necessary. */ -AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, guint size); +AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size); /** Same as avahi_string_list_add(), but takes a variable number of * NUL terminated strings. The argument list must be terminated by a @@ -85,17 +88,17 @@ AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va); /** Convert the string list object to a single character string, * seperated by spaces and enclosed in "". g_free() the result! This * function doesn't work well with string that contain NUL bytes. */ -gchar* avahi_string_list_to_string(AvahiStringList *l); +char* avahi_string_list_to_string(AvahiStringList *l); /** Serialize the string list object in a way that is compatible with * the storing of DNS TXT records. Strings longer than 255 bytes are truncated. */ -guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size); +size_t avahi_string_list_serialize(AvahiStringList *l, void * data, size_t size); /** Inverse of avahi_string_list_serialize() */ -AvahiStringList *avahi_string_list_parse(gconstpointer data, guint size); +AvahiStringList *avahi_string_list_parse(const void *data, size_t size); /** Compare to string lists */ -gboolean avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b); +int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b); /** Copy a string list */ AvahiStringList *avahi_string_list_copy(const AvahiStringList *l); @@ -104,8 +107,7 @@ AvahiStringList *avahi_string_list_copy(const AvahiStringList *l); AvahiStringList* avahi_string_list_reverse(AvahiStringList *l); /** Return the number of elements in the string list */ -guint avahi_string_list_length(const AvahiStringList *l); - +unsigned avahi_string_list_length(const AvahiStringList *l); AVAHI_C_DECL_END diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c index a0392eb..6f44034 100644 --- a/avahi-common/timeval-test.c +++ b/avahi-common/timeval-test.c @@ -1,15 +1,41 @@ +/* $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 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include #include "util.h" int main(int argc, char *argv[]) { - GTimeVal a = { 5, 5 }, b; + struct timeval a = { 5, 5 }, b; b = a; - g_message("%li.%li", a.tv_sec, a.tv_usec); + printf("%li.%li\n", a.tv_sec, a.tv_usec); avahi_timeval_add(&a, -50); - g_message("%li.%li", a.tv_sec, a.tv_usec); + printf("%li.%li\n", a.tv_sec, a.tv_usec); - g_message("%lli", avahi_timeval_diff(&a, &b)); + printf("%lli\n", avahi_timeval_diff(&a, &b)); } diff --git a/avahi-common/util.c b/avahi-common/util.c index a977bb2..cc80fe5 100644 --- a/avahi-common/util.c +++ b/avahi-common/util.c @@ -31,10 +31,12 @@ #include #include #include +#include #include "util.h" +#include "malloc.h" -gchar *avahi_get_host_name(void) { +char *avahi_get_host_name(void) { #ifdef HOST_NAME_MAX char t[HOST_NAME_MAX]; #else @@ -45,29 +47,29 @@ gchar *avahi_get_host_name(void) { return avahi_normalize_name(t); } -static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) { - gboolean escaped = FALSE; +static char *unescape_uneeded(const char *src, char *ret_dest, size_t size) { + int escaped = 0; - g_assert(src); - g_assert(ret_dest); - g_assert(size > 0); + assert(src); + assert(ret_dest); + assert(size > 0); for (; *src; src++) { if (!escaped && *src == '\\') - escaped = TRUE; + escaped = 1; else if (escaped && (*src == '.' || *src == '\\')) { if ((size -= 2) <= 1) break; *(ret_dest++) = '\\'; *(ret_dest++) = *src; - escaped = FALSE; + escaped = 0; } else { if (--size <= 1) break; *(ret_dest++) = *src; - escaped = FALSE; + escaped = 0; } } @@ -77,10 +79,11 @@ static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) { return ret_dest; } -gchar *avahi_normalize_name(const gchar *s) { - gchar tmp[256]; - guint l; - g_assert(s); +char *avahi_normalize_name(const char *s) { + char tmp[256]; + size_t l; + + assert(s); unescape_uneeded(s, tmp, sizeof(tmp)); @@ -89,12 +92,12 @@ gchar *avahi_normalize_name(const gchar *s) { while (l > 0 && tmp[l-1] == '.') tmp[--l] = 0; - return g_strdup(tmp); + return avahi_strdup(tmp); } -gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { - g_assert(a); - g_assert(b); +int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) { + assert(a); + assert(b); if (a->tv_sec < b->tv_sec) return -1; @@ -111,9 +114,9 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) { return 0; } -AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { - g_assert(a); - g_assert(b); +AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) { + assert(a); + assert(b); if (avahi_timeval_compare(a, b) < 0) return - avahi_timeval_diff(b, a); @@ -121,51 +124,52 @@ AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) { return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; } -GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) { +struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) { AvahiUsec u; - g_assert(a); + assert(a); u = usec + a->tv_usec; if (u < 0) { - a->tv_usec = (glong) (1000000 + (u % 1000000)); - a->tv_sec += (glong) (-1 + (u / 1000000)); + a->tv_usec = (long) (1000000 + (u % 1000000)); + a->tv_sec += (long) (-1 + (u / 1000000)); } else { - a->tv_usec = (glong) (u % 1000000); - a->tv_sec += (glong) (u / 1000000); + a->tv_usec = (long) (u % 1000000); + a->tv_sec += (long) (u / 1000000); } return a; } -AvahiUsec avahi_age(const GTimeVal *a) { - GTimeVal now; +AvahiUsec avahi_age(const struct timeval *a) { + struct timeval now; - g_assert(a); + assert(a); - g_get_current_time(&now); + gettimeofday(&now, NULL); return avahi_timeval_diff(&now, a); } -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) { - g_assert(tv); - g_get_current_time(tv); +struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) { + assert(tv); + + gettimeofday(tv, NULL); if (msec) avahi_timeval_add(tv, (AvahiUsec) msec*1000); if (jitter) - avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000); + avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0))); return tv; } -gint avahi_set_cloexec(gint fd) { - gint n; +int avahi_set_cloexec(int fd) { + int n; - g_assert(fd >= 0); + assert(fd >= 0); if ((n = fcntl(fd, F_GETFD)) < 0) return -1; @@ -176,10 +180,10 @@ gint avahi_set_cloexec(gint fd) { return fcntl(fd, F_SETFD, n|FD_CLOEXEC); } -gint avahi_set_nonblock(gint fd) { - gint n; - - g_assert(fd >= 0); +int avahi_set_nonblock(int fd) { + int n; + + assert(fd >= 0); if ((n = fcntl(fd, F_GETFL)) < 0) return -1; @@ -190,32 +194,29 @@ gint avahi_set_nonblock(gint fd) { return fcntl(fd, F_SETFL, n|O_NONBLOCK); } -gint avahi_wait_for_write(gint fd) { +int avahi_wait_for_write(int fd) { fd_set fds; - gint r; + int 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)); - + if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) return -1; - } - g_assert(r > 0); + assert(r > 0); return 0; } /* 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; - gchar *d; +char *avahi_unescape_label(const char **name, char *dest, size_t size) { + unsigned i = 0; + char *d; - g_assert(dest); - g_assert(size > 0); - g_assert(name); + assert(dest); + assert(size > 0); + assert(name); if (!**name) return NULL; @@ -245,7 +246,7 @@ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) { i++; } - g_assert(i < size); + assert(i < size); *d = 0; @@ -253,14 +254,14 @@ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) { } /* Escape "\" and ".", append \0 */ -gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) { - gchar *r; +char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size) { + char *r; - g_assert(src); - g_assert(ret_name); - g_assert(*ret_name); - g_assert(ret_size); - g_assert(*ret_size > 0); + assert(src); + assert(ret_name); + assert(*ret_name); + assert(ret_size); + assert(*ret_size > 0); r = *ret_name; @@ -288,41 +289,41 @@ gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, return r; } -gboolean avahi_domain_equal(const gchar *a, const gchar *b) { - g_assert(a); - g_assert(b); +int avahi_domain_equal(const char *a, const char *b) { + assert(a); + assert(b); if (a == b) - return TRUE; + return 1; for (;;) { - gchar ca[65], cb[65], *pa, *pb; + char ca[65], cb[65], *pa, *pb; pa = avahi_unescape_label(&a, ca, sizeof(ca)); pb = avahi_unescape_label(&b, cb, sizeof(cb)); if (!pa && !pb) - return TRUE; + return 1; else if ((pa && !pb) || (!pa && pb)) - return FALSE; + return 0; - if (g_ascii_strcasecmp(pa, pb)) - return FALSE; + if (strcasecmp(pa, pb)) + return 0; } - return TRUE; + return 1; } -gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) { - g_assert(a); - g_assert(b); +int avahi_binary_domain_cmp(const char *a, const char *b) { + assert(a); + assert(b); if (a == b) return 0; for (;;) { - gchar ca[65], cb[65], *pa, *pb; - gint r; + char ca[65], cb[65], *pa, *pb; + int r; pa = avahi_unescape_label(&a, ca, sizeof(ca)); pb = avahi_unescape_label(&b, cb, sizeof(cb)); @@ -339,14 +340,14 @@ gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) { } } -void avahi_hexdump(gconstpointer p, guint size) { - const guint8 *c = p; - g_assert(p); +void avahi_hexdump(const void* p, size_t size) { + const uint8_t *c = p; + assert(p); printf("Dumping %u bytes from %p:\n", size, p); while (size > 0) { - guint i; + unsigned i; for (i = 0; i < 16; i++) { if (i < size) @@ -373,11 +374,20 @@ void avahi_hexdump(gconstpointer p, guint size) { } } -guint avahi_domain_hash(const gchar *s) { - guint hash = 0; +unsigned avahi_strhash(const char *p) { + unsigned hash = 0; + + for (; *p; p++) + hash = 31 * hash + *p; + + return hash; +} + +unsigned avahi_domain_hash(const char *s) { + unsigned hash = 0; for (;;) { - gchar c[65], *m; + char c[65]; if (!avahi_unescape_label(&s, c, sizeof(c))) return hash; @@ -385,18 +395,16 @@ guint avahi_domain_hash(const gchar *s) { if (!c[0]) continue; - m = g_ascii_strdown(c, -1); - hash += g_str_hash(m); - g_free(m); + hash += avahi_strhash(avahi_strdown(c)); } } -gchar *avahi_format_mac_address(const guint8* mac, guint size) { - gchar *r, *t; - guint i; - static const gchar hex[] = "0123456789abcdef"; +char *avahi_format_mac_address(const uint8_t* mac, size_t size) { + char *r, *t; + unsigned i; + static const char hex[] = "0123456789abcdef"; - t = r = g_new(gchar, size > 0 ? size*3 : 1); + t = r = avahi_new(char, size > 0 ? size*3 : 1); if (size <= 0) { *r = 0; @@ -415,46 +423,46 @@ gchar *avahi_format_mac_address(const guint8* mac, guint size) { return r; } -gboolean avahi_valid_service_type(const gchar *t) { - const gchar *p; - g_assert(t); +int avahi_valid_service_type(const char *t) { + const char *p; + assert(t); if (strlen(t) < 5) - return FALSE; + return 0; if (*t != '_') - return FALSE; + return 0; if (!(p = strchr(t, '.'))) - return FALSE; + return 0; if (p - t > 63 || p - t < 2) - return FALSE; + return 0; if (*(++p) != '_') - return FALSE; + return 0; if (strchr(p, '.')) - return FALSE; + return 0; if (strlen(p) > 63 || strlen(p) < 2) - return FALSE; + return 0; - return TRUE; + return 1; } -gboolean avahi_valid_domain_name(const gchar *t) { - const gchar *p, *dp; - gboolean dot = FALSE; +int avahi_valid_domain_name(const char *t) { + const char *p, *dp; + int dot = 0; - g_assert(t); + assert(t); if (*t == 0) - return FALSE; + return 0; /* Domains may not start with a dot */ if (*t == '.') - return FALSE; + return 0; dp = t; @@ -462,49 +470,71 @@ gboolean avahi_valid_domain_name(const gchar *t) { if (*p == '.') { if (dot) /* Two subsequent dots */ - return FALSE; + return 0; if (p - dp > 63) - return FALSE; + return 0; - dot = TRUE; + dot = 1; dp = p + 1; } else - dot = FALSE; + dot = 0; } if (p - dp > 63) - return FALSE; + return 0; /* A trailing dot IS allowed */ - return TRUE; + return 1; } -gboolean avahi_valid_service_name(const gchar *t) { - g_assert(t); +int avahi_valid_service_name(const char *t) { + assert(t); if (*t == 0) - return FALSE; + return 0; if (strlen(t) > 63) - return FALSE; + return 0; - return TRUE; + return 1; } -gboolean avahi_valid_host_name(const gchar *t) { - g_assert(t); +int avahi_valid_host_name(const char *t) { + assert(t); if (*t == 0) - return FALSE; + return 0; if (strlen(t) > 63) - return FALSE; + return 0; if (strchr(t, '.')) - return FALSE; + return 0; - return TRUE; + return 1; } + +char *avahi_strdown(char *s) { + char *c; + + assert(s); + + for (c = s; *c; c++) + *c = (char) tolower(*c); + + return s; +} + +char *avahi_strup(char *s) { + char *c; + assert(s); + + for (c = s; *c; c++) + *c = (char) toupper(*c); + + return s; +} + diff --git a/avahi-common/util.h b/avahi-common/util.h index 0d4fb4b..65252e3 100644 --- a/avahi-common/util.h +++ b/avahi-common/util.h @@ -22,48 +22,54 @@ USA. ***/ -#include +#include +#include +#include #include AVAHI_C_DECL_BEGIN -typedef gint64 AvahiUsec; +typedef int64_t AvahiUsec; -gchar *avahi_normalize_name(const gchar *s); /* g_free() the result! */ -gchar *avahi_get_host_name(void); /* g_free() the result! */ +char *avahi_normalize_name(const char *s); /* avahi_free() the result! */ +char *avahi_get_host_name(void); /* avahi_free() the result! */ -gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b); -AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b); -GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec); +int avahi_timeval_compare(const struct timeval *a, const struct timeval *b); +AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b); +struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec); -AvahiUsec avahi_age(const GTimeVal *a); -GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter); +AvahiUsec avahi_age(const struct timeval *a); +struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter); -gint avahi_set_cloexec(gint fd); -gint avahi_set_nonblock(gint fd); -gint avahi_wait_for_write(gint fd); +int avahi_set_cloexec(int fd); +int avahi_set_nonblock(int fd); +int avahi_wait_for_write(int fd); -gboolean avahi_domain_equal(const gchar *a, const gchar *b); -gint avahi_binary_domain_cmp(const gchar *a, const gchar *b); +int avahi_domain_equal(const char *a, const char *b); +int avahi_binary_domain_cmp(const char *a, const char *b); -void avahi_hexdump(gconstpointer p, guint size); +void avahi_hexdump(const void *p, size_t size); /* Read the first label from the textual domain name *name, unescape * it and write it to dest, *name is changed to point to the next label*/ -gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size); +char *avahi_unescape_label(const char **name, char *dest, size_t size); /* Escape the domain name in *src and write it to *ret_name */ -gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size); +char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size); -guint avahi_domain_hash(const gchar *s); +unsigned avahi_strhash(const char *p); +unsigned avahi_domain_hash(const char *s); -gchar *avahi_format_mac_address(const guint8* mac, guint size); +char *avahi_format_mac_address(const uint8_t* mac, size_t size); -gboolean avahi_valid_service_type(const gchar *t); -gboolean avahi_valid_domain_name(const gchar *t); -gboolean avahi_valid_service_name(const gchar *t); -gboolean avahi_valid_host_name(const gchar *t); +int avahi_valid_service_type(const char *t); +int avahi_valid_domain_name(const char *t); +int avahi_valid_service_name(const char *t); +int avahi_valid_host_name(const char *t); + +char *avahi_strdown(char *s); +char *avahi_strup(char *s); AVAHI_C_DECL_END diff --git a/avahi-core/announce.c b/avahi-core/announce.c index fe461de..5d9e8cf 100644 --- a/avahi-core/announce.c +++ b/avahi-core/announce.c @@ -45,7 +45,7 @@ static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) { static void elapse_announce(AvahiTimeEvent *e, void *userdata); -static void set_timeout(AvahiAnnouncement *a, const GTimeVal *tv) { +static void set_timeout(AvahiAnnouncement *a, const struct timeval *tv) { g_assert(a); if (!tv) { @@ -95,7 +95,7 @@ void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) { a->n_iteration = 1; next_state(a); } else { - GTimeVal tv; + struct timeval tv; a->n_iteration = 0; avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC); set_timeout(a, &tv); @@ -140,7 +140,7 @@ static void next_state(AvahiAnnouncement *a) { set_timeout(a, NULL); next_state(a); } else { - GTimeVal tv; + struct timeval tv; avahi_interface_post_probe(a->interface, a->entry->record, FALSE); @@ -171,7 +171,7 @@ static void next_state(AvahiAnnouncement *a) { set_timeout(a, NULL); } else { - GTimeVal tv; + struct timeval tv; avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC); if (a->n_iteration < 10) @@ -204,7 +204,7 @@ AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiIn static void go_to_initial_state(AvahiAnnouncement *a, gboolean immediately) { AvahiEntry *e; - GTimeVal tv; + struct timeval tv; g_assert(a); e = a->entry; diff --git a/avahi-core/browse.c b/avahi-core/browse.c index 5885cb4..42c13da 100644 --- a/avahi-core/browse.c +++ b/avahi-core/browse.c @@ -48,7 +48,7 @@ struct AvahiRecordBrowser { static void elapse(AvahiTimeEvent *e, void *userdata) { AvahiRecordBrowser *s = userdata; - GTimeVal tv; + struct timeval tv; /* gchar *t; */ g_assert(s); @@ -118,7 +118,7 @@ static gboolean scan_idle_callback(gpointer data) { AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, gpointer userdata) { AvahiRecordBrowser *b, *t; - GTimeVal tv; + struct timeval tv; g_assert(server); g_assert(key); diff --git a/avahi-core/cache.c b/avahi-core/cache.c index 174fceb..2f3296f 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -241,7 +241,7 @@ static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) { g_assert(e); e->state = AVAHI_CACHE_FINAL; - g_get_current_time(&e->expiry); + gettimeofday(&e->expiry, NULL); avahi_timeval_add(&e->expiry, 1000000); /* 1s */ update_time_event(c, e); } @@ -264,9 +264,9 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con } else { AvahiCacheEntry *e = NULL, *first; - GTimeVal now; + struct timeval now; - g_get_current_time(&now); + gettimeofday(&now, NULL); /* This is an update request */ @@ -378,13 +378,13 @@ void avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, gpointer userda } gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { - GTimeVal now; + struct timeval now; AvahiUsec age; g_assert(c); g_assert(e); - g_get_current_time(&now); + gettimeofday(&now, NULL); age = avahi_timeval_diff(&now, &e->timestamp)/1000000; diff --git a/avahi-core/cache.h b/avahi-core/cache.h index de685f4..2f4caa6 100644 --- a/avahi-core/cache.h +++ b/avahi-core/cache.h @@ -44,8 +44,8 @@ typedef struct AvahiCacheEntry AvahiCacheEntry; struct AvahiCacheEntry { AvahiCache *cache; AvahiRecord *record; - GTimeVal timestamp; - GTimeVal expiry; + struct timeval timestamp; + struct timeval expiry; gboolean cache_flush; AvahiAddress origin; diff --git a/avahi-core/probe-sched.c b/avahi-core/probe-sched.c index d9c6599..ad23234 100644 --- a/avahi-core/probe-sched.c +++ b/avahi-core/probe-sched.c @@ -38,7 +38,7 @@ struct AvahiProbeJob { gboolean chosen; /* Use for packet assembling */ gboolean done; - GTimeVal delivery; + struct timeval delivery; AvahiRecord *record; @@ -91,7 +91,7 @@ static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) { static void elapse_callback(AvahiTimeEvent *e, gpointer data); static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) { - GTimeVal tv; + struct timeval tv; g_assert(s); g_assert(pj); @@ -116,7 +116,7 @@ static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) { pj->done = TRUE; job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0); - g_get_current_time(&pj->delivery); + gettimeofday(&pj->delivery, NULL); } AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) { @@ -344,7 +344,7 @@ static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *reco gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) { AvahiProbeJob *pj; - GTimeVal tv; + struct timeval tv; g_assert(s); g_assert(record); diff --git a/avahi-core/query-sched.c b/avahi-core/query-sched.c index d5135cb..129b15e 100644 --- a/avahi-core/query-sched.c +++ b/avahi-core/query-sched.c @@ -37,7 +37,7 @@ struct AvahiQueryJob { AvahiTimeEvent *time_event; gboolean done; - GTimeVal delivery; + struct timeval delivery; AvahiKey *key; @@ -98,7 +98,7 @@ static void job_free(AvahiQueryScheduler *s, AvahiQueryJob *qj) { static void elapse_callback(AvahiTimeEvent *e, gpointer data); static void job_set_elapse_time(AvahiQueryScheduler *s, AvahiQueryJob *qj, guint msec, guint jitter) { - GTimeVal tv; + struct timeval tv; g_assert(s); g_assert(qj); @@ -123,7 +123,7 @@ static void job_mark_done(AvahiQueryScheduler *s, AvahiQueryJob *qj) { qj->done = TRUE; job_set_elapse_time(s, qj, AVAHI_QUERY_HISTORY_MSEC, 0); - g_get_current_time(&qj->delivery); + gettimeofday(&qj->delivery, NULL); } AvahiQueryScheduler *avahi_query_scheduler_new(AvahiInterface *i) { @@ -319,7 +319,7 @@ static AvahiQueryJob* find_history_job(AvahiQueryScheduler *s, AvahiKey *key) { } gboolean avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, gboolean immediately) { - GTimeVal tv; + struct timeval tv; AvahiQueryJob *qj; g_assert(s); @@ -373,7 +373,7 @@ void avahi_query_scheduler_incoming(AvahiQueryScheduler *s, AvahiKey *key) { } qj = job_new(s, key, TRUE); - g_get_current_time(&qj->delivery); + gettimeofday(&qj->delivery, NULL); job_set_elapse_time(s, qj, AVAHI_QUERY_HISTORY_MSEC, 0); } diff --git a/avahi-core/resolve-address.c b/avahi-core/resolve-address.c index 89f9bcf..c386875 100644 --- a/avahi-core/resolve-address.c +++ b/avahi-core/resolve-address.c @@ -82,7 +82,7 @@ AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, AvahiIfInd AvahiAddressResolver *r; AvahiKey *k; gchar *n; - GTimeVal tv; + struct timeval tv; g_assert(server); g_assert(address); diff --git a/avahi-core/resolve-host-name.c b/avahi-core/resolve-host-name.c index c18722d..c01c1a4 100644 --- a/avahi-core/resolve-host-name.c +++ b/avahi-core/resolve-host-name.c @@ -107,7 +107,7 @@ static void time_event_callback(AvahiTimeEvent *e, void *userdata) { AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) { AvahiHostNameResolver *r; AvahiKey *k; - GTimeVal tv; + struct timeval tv; g_assert(server); g_assert(host_name); diff --git a/avahi-core/resolve-service.c b/avahi-core/resolve-service.c index 2ad38f8..135543f 100644 --- a/avahi-core/resolve-service.c +++ b/avahi-core/resolve-service.c @@ -195,7 +195,7 @@ static void time_event_callback(AvahiTimeEvent *e, void *userdata) { AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *name, const gchar *type, const gchar *domain, AvahiProtocol aprotocol, AvahiServiceResolverCallback callback, gpointer userdata) { AvahiServiceResolver *r; AvahiKey *k; - GTimeVal tv; + struct timeval tv; gchar t[256], *n; size_t l; diff --git a/avahi-core/response-sched.c b/avahi-core/response-sched.c index 6d1269e..e1024a6 100644 --- a/avahi-core/response-sched.c +++ b/avahi-core/response-sched.c @@ -45,7 +45,7 @@ struct AvahiResponseJob { AvahiTimeEvent *time_event; AvahiResponseJobState state; - GTimeVal delivery; + struct timeval delivery; AvahiRecord *record; gboolean flush_cache; @@ -108,7 +108,7 @@ static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) { static void elapse_callback(AvahiTimeEvent *e, gpointer data); static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, guint msec, guint jitter) { - GTimeVal tv; + struct timeval tv; g_assert(s); g_assert(rj); @@ -134,7 +134,7 @@ static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) { job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0); - g_get_current_time(&rj->delivery); + gettimeofday(&rj->delivery, NULL); } AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) { @@ -329,7 +329,7 @@ static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRec gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) { AvahiResponseJob *rj; - GTimeVal tv; + struct timeval tv; /* gchar *t; */ g_assert(s); @@ -440,7 +440,7 @@ void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *r rj->flush_cache = flush_cache; rj->querier_valid = FALSE; - g_get_current_time(&rj->delivery); + gettimeofday(&rj->delivery, NULL); job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0); } @@ -477,7 +477,7 @@ void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *r rj->querier = *querier; } - g_get_current_time(&rj->delivery); + gettimeofday(&rj->delivery, NULL); job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0); } diff --git a/avahi-core/server.c b/avahi-core/server.c index 4d5a4ec..2bbc1f0 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -2036,7 +2036,7 @@ void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState sta if (g->state == state) return; - g_assert(state >= AVAHI_ENTRY_GROUP_UNCOMMITED && state <= AVAHI_ENTRY_GROUP_COLLISION); + g_assert(state <= AVAHI_ENTRY_GROUP_COLLISION); g->state = state; @@ -2093,7 +2093,7 @@ void avahi_entry_group_free(AvahiEntryGroup *g) { static void entry_group_commit_real(AvahiEntryGroup *g) { g_assert(g); - g_get_current_time(&g->register_time); + gettimeofday(&g->register_time, NULL); avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); @@ -2117,7 +2117,7 @@ static void entry_group_register_time_event_callback(AvahiTimeEvent *e, gpointer } gint avahi_entry_group_commit(AvahiEntryGroup *g) { - GTimeVal now; + struct timeval now; g_assert(g); g_assert(!g->dead); @@ -2132,7 +2132,7 @@ gint avahi_entry_group_commit(AvahiEntryGroup *g) { AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT : AVAHI_RR_HOLDOFF_MSEC)); - g_get_current_time(&now); + gettimeofday(&now, NULL); if (avahi_timeval_compare(&g->register_time, &now) <= 0) { /* Holdoff time passed, so let's start probing */ diff --git a/avahi-core/server.h b/avahi-core/server.h index 5810dfa..f4c7d92 100644 --- a/avahi-core/server.h +++ b/avahi-core/server.h @@ -43,7 +43,7 @@ struct AvahiLegacyUnicastReflectSlot { AvahiAddress address; guint16 port; gint interface; - GTimeVal elapse_time; + struct timeval elapse_time; AvahiTimeEvent *time_event; }; @@ -76,7 +76,7 @@ struct AvahiEntryGroup { guint n_probing; guint n_register_try; - GTimeVal register_time; + struct timeval register_time; AvahiTimeEvent *register_time_event; AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups); diff --git a/avahi-core/timeeventq-test.c b/avahi-core/timeeventq-test.c index 33cd2b1..91eceb6 100644 --- a/avahi-core/timeeventq-test.c +++ b/avahi-core/timeeventq-test.c @@ -31,7 +31,7 @@ static AvahiTimeEventQueue *q = NULL; static void callback(AvahiTimeEvent*e, gpointer userdata) { - GTimeVal tv = {0, 0}; + struct timeval tv = {0, 0}; g_assert(e); g_message("callback(%i)", GPOINTER_TO_INT(userdata)); avahi_elapse_time(&tv, 1000, 100); @@ -40,7 +40,7 @@ static void callback(AvahiTimeEvent*e, gpointer userdata) { int main(int argc, char *argv[]) { GMainLoop *loop = NULL; - GTimeVal tv; + struct timeval tv; q = avahi_time_event_queue_new(NULL, 0); diff --git a/avahi-core/timeeventq.c b/avahi-core/timeeventq.c index 60d9fe0..fbdb6d5 100644 --- a/avahi-core/timeeventq.c +++ b/avahi-core/timeeventq.c @@ -38,10 +38,21 @@ static gint compare(gconstpointer _a, gconstpointer _b) { return avahi_timeval_compare(&a->last_run, &b->last_run); } +static void source_get_timeval(GSource *source, struct timeval *tv) { + GTimeVal gtv; + + g_assert(source); + g_assert(tv); + + g_source_get_current_time(source, >v); + tv->tv_sec = gtv.tv_sec; + tv->tv_usec = gtv.tv_usec; +} + static gboolean prepare_func(GSource *source, gint *timeout) { AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; AvahiTimeEvent *e; - GTimeVal now; + struct timeval now; g_assert(source); g_assert(timeout); @@ -54,7 +65,7 @@ static gboolean prepare_func(GSource *source, gint *timeout) { e = q->prioq->root->data; g_assert(e); - g_source_get_current_time(source, &now); + source_get_timeval(source, &now); if (avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */ avahi_timeval_compare(&now, &e->last_run) != 0 /* Not yet run */) { @@ -74,7 +85,7 @@ static gboolean prepare_func(GSource *source, gint *timeout) { static gboolean check_func(GSource *source) { AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; AvahiTimeEvent *e; - GTimeVal now; + struct timeval now; g_assert(source); @@ -84,7 +95,7 @@ static gboolean check_func(GSource *source) { e = q->prioq->root->data; g_assert(e); - g_source_get_current_time(source, &now); + source_get_timeval(source, &now); return avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */ @@ -93,11 +104,11 @@ static gboolean check_func(GSource *source) { static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; - GTimeVal now; + struct timeval now; g_assert(source); - g_source_get_current_time(source, &now); + source_get_timeval(source, &now); while (q->prioq->root) { AvahiTimeEvent *e = q->prioq->root->data; @@ -123,10 +134,10 @@ static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer us } static void fix_expiry_time(AvahiTimeEvent *e) { - GTimeVal now; + struct timeval now; g_assert(e); - g_source_get_current_time(&e->queue->source, &now); + source_get_timeval(&e->queue->source, &now); if (avahi_timeval_compare(&now, &e->expiry) > 0) e->expiry = now; @@ -166,7 +177,7 @@ void avahi_time_event_queue_free(AvahiTimeEventQueue *q) { g_source_unref(&q->source); } -AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const GTimeVal *timeval, AvahiTimeEventCallback callback, gpointer userdata) { +AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const struct timeval *timeval, AvahiTimeEventCallback callback, gpointer userdata) { AvahiTimeEvent *e; g_assert(q); @@ -199,7 +210,7 @@ void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e) { g_free(e); } -void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const GTimeVal *timeval) { +void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const struct timeval *timeval) { g_assert(q); g_assert(e); g_assert(e->queue == q); diff --git a/avahi-core/timeeventq.h b/avahi-core/timeeventq.h index a2544ff..65f1420 100644 --- a/avahi-core/timeeventq.h +++ b/avahi-core/timeeventq.h @@ -22,6 +22,8 @@ USA. ***/ +#include + typedef struct AvahiTimeEventQueue AvahiTimeEventQueue; typedef struct AvahiTimeEvent AvahiTimeEvent; @@ -32,8 +34,8 @@ typedef void (*AvahiTimeEventCallback)(AvahiTimeEvent *e, gpointer userdata); struct AvahiTimeEvent { AvahiTimeEventQueue *queue; AvahiPrioQueueNode *node; - GTimeVal expiry; - GTimeVal last_run; + struct timeval expiry; + struct timeval last_run; AvahiTimeEventCallback callback; gpointer userdata; }; @@ -46,10 +48,10 @@ struct AvahiTimeEventQueue { AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority); void avahi_time_event_queue_free(AvahiTimeEventQueue *q); -AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const GTimeVal *timeval, AvahiTimeEventCallback callback, gpointer userdata); +AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const struct timeval *timeval, AvahiTimeEventCallback callback, gpointer userdata); void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e); -void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const GTimeVal *timeval); +void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const struct timeval *timeval); AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q); AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e); diff --git a/doxygen.cfg b/doxygen.cfg index a2e3678..8ec1080 100644 --- a/doxygen.cfg +++ b/doxygen.cfg @@ -67,7 +67,7 @@ WARN_LOGFILE = #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- -INPUT = $(SRCDIR)/avahi-common/cdecl.h $(SRCDIR)/avahi-core/core.h $(SRCDIR)/avahi-common/address.h $(SRCDIR)/avahi-core/rr.h $(SRCDIR)/avahi-common/strlst.h $(SRCDIR)/avahi-common/alternative.h $(SRCDIR)/avahi-core/log.h $(SRCDIR)/avahi-common/defs.h $(SRCDIR)/avahi-client/client.h $(SRCDIR)/avahi-common/error.h +INPUT = $(SRCDIR)/avahi-common/cdecl.h $(SRCDIR)/avahi-core/core.h $(SRCDIR)/avahi-common/address.h $(SRCDIR)/avahi-core/rr.h $(SRCDIR)/avahi-common/strlst.h $(SRCDIR)/avahi-common/alternative.h $(SRCDIR)/avahi-core/log.h $(SRCDIR)/avahi-common/defs.h $(SRCDIR)/avahi-client/client.h $(SRCDIR)/avahi-common/error.h $(SRCDIR)/avahi-common/util.h $(SRCDIR)/avahi-common/malloc.h $(SRCDIR)/avahi-common/util.h FILE_PATTERNS = RECURSIVE = NO EXCLUDE = -- 2.39.2