alternative.h \
util.h \
cdecl.h \
- defs.h
+ defs.h \
+ malloc.h
if ENABLE_DBUS
avahi_commoninclude_HEADERS += dbus.h
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)
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
+#include <assert.h>
#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;
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;
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,
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) {
}
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;
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);
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;
}
***/
#include <sys/socket.h>
-#include <glib.h>
+#include <inttypes.h>
+
#include <avahi-common/cdecl.h>
/** \file address.h Defintions and functions to manipulate IP addresses. */
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 {
/** 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 */
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
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
#include <stdio.h>
#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);
}
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
+#include <assert.h>
#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, " #")))
}
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;
}
USA.
***/
-#include <glib.h>
-
#include <avahi-common/cdecl.h>
/** \file alternative.h Functions to find alternative names for hosts and services in the case of name collision */
* 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
#include <config.h>
#endif
+#include <stdio.h>
+
#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;
}
USA.
***/
-#include <glib.h>
-
/* Some macros for maintaining doubly linked lists */
+#include <assert.h>
+
/* 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
/* 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; \
/* 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; \
--- /dev/null
+/* $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 <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#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;
+}
+
--- /dev/null
+#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 <sys/types.h>
+#include <stdarg.h>
+
+/** 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
#include <config.h>
#endif
-#include <glib.h>
#include <stdio.h>
+#include <assert.h>
#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);
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
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);
#include <string.h>
#include <stdarg.h>
+#include <assert.h>
#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);
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;
while (l) {
n = l->next;
- g_free(l);
+ avahi_free(l);
l = n;
}
}
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);
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);
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;
AvahiStringList *n;
for (n = l; n; n = n->next) {
- guint k;
+ size_t k;
k = n->size;
if (k > 255)
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;
}
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;
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]);
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++;
USA.
***/
-#include <glib.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <stdarg.h>
+
#include <avahi-common/cdecl.h>
/** \file strlst.h Implementation of a data type to store lists of strings */
* 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);
/** 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);
/** 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
/** 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);
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
+/* $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 <config.h>
+#endif
+
+#include <stdio.h>
#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));
}
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
+#include <assert.h>
#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
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;
}
}
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));
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;
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);
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;
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;
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;
i++;
}
- g_assert(i < size);
+ assert(i < size);
*d = 0;
}
/* 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;
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));
}
}
-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)
}
}
-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;
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;
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;
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;
+}
+
USA.
***/
-#include <glib.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <sys/time.h>
#include <avahi-common/cdecl.h>
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
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) {
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);
set_timeout(a, NULL);
next_state(a);
} else {
- GTimeVal tv;
+ struct timeval tv;
avahi_interface_post_probe(a->interface, a->entry->record, FALSE);
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)
static void go_to_initial_state(AvahiAnnouncement *a, gboolean immediately) {
AvahiEntry *e;
- GTimeVal tv;
+ struct timeval tv;
g_assert(a);
e = a->entry;
static void elapse(AvahiTimeEvent *e, void *userdata) {
AvahiRecordBrowser *s = userdata;
- GTimeVal tv;
+ struct timeval tv;
/* gchar *t; */
g_assert(s);
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);
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);
}
} else {
AvahiCacheEntry *e = NULL, *first;
- GTimeVal now;
+ struct timeval now;
- g_get_current_time(&now);
+ gettimeofday(&now, NULL);
/* This is an update request */
}
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;
struct AvahiCacheEntry {
AvahiCache *cache;
AvahiRecord *record;
- GTimeVal timestamp;
- GTimeVal expiry;
+ struct timeval timestamp;
+ struct timeval expiry;
gboolean cache_flush;
AvahiAddress origin;
gboolean chosen; /* Use for packet assembling */
gboolean done;
- GTimeVal delivery;
+ struct timeval delivery;
AvahiRecord *record;
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);
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) {
gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
AvahiProbeJob *pj;
- GTimeVal tv;
+ struct timeval tv;
g_assert(s);
g_assert(record);
AvahiTimeEvent *time_event;
gboolean done;
- GTimeVal delivery;
+ struct timeval delivery;
AvahiKey *key;
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);
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) {
}
gboolean avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, gboolean immediately) {
- GTimeVal tv;
+ struct timeval tv;
AvahiQueryJob *qj;
g_assert(s);
}
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);
}
AvahiAddressResolver *r;
AvahiKey *k;
gchar *n;
- GTimeVal tv;
+ struct timeval tv;
g_assert(server);
g_assert(address);
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);
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;
AvahiTimeEvent *time_event;
AvahiResponseJobState state;
- GTimeVal delivery;
+ struct timeval delivery;
AvahiRecord *record;
gboolean flush_cache;
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);
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) {
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);
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);
}
rj->querier = *querier;
}
- g_get_current_time(&rj->delivery);
+ gettimeofday(&rj->delivery, NULL);
job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
}
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;
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);
}
gint avahi_entry_group_commit(AvahiEntryGroup *g) {
- GTimeVal now;
+ struct timeval now;
g_assert(g);
g_assert(!g->dead);
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 */
AvahiAddress address;
guint16 port;
gint interface;
- GTimeVal elapse_time;
+ struct timeval elapse_time;
AvahiTimeEvent *time_event;
};
guint n_probing;
guint n_register_try;
- GTimeVal register_time;
+ struct timeval register_time;
AvahiTimeEvent *register_time_event;
AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups);
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);
int main(int argc, char *argv[]) {
GMainLoop *loop = NULL;
- GTimeVal tv;
+ struct timeval tv;
q = avahi_time_event_queue_new(NULL, 0);
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);
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 */) {
static gboolean check_func(GSource *source) {
AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
AvahiTimeEvent *e;
- GTimeVal now;
+ struct timeval now;
g_assert(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 */
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;
}
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;
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);
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);
USA.
***/
+#include <sys/types.h>
+
typedef struct AvahiTimeEventQueue AvahiTimeEventQueue;
typedef struct AvahiTimeEvent AvahiTimeEvent;
struct AvahiTimeEvent {
AvahiTimeEventQueue *queue;
AvahiPrioQueueNode *node;
- GTimeVal expiry;
- GTimeVal last_run;
+ struct timeval expiry;
+ struct timeval last_run;
AvahiTimeEventCallback callback;
gpointer userdata;
};
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);
#---------------------------------------------------------------------------
# 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 =