aboutsummaryrefslogtreecommitdiff
path: root/avahi-common
diff options
context:
space:
mode:
Diffstat (limited to 'avahi-common')
-rw-r--r--avahi-common/.gitignore14
-rw-r--r--avahi-common/Makefile.am129
-rw-r--r--avahi-common/address.c155
-rw-r--r--avahi-common/address.h119
-rw-r--r--avahi-common/alternative-test.c90
-rw-r--r--avahi-common/alternative.c182
-rw-r--r--avahi-common/alternative.h43
-rw-r--r--avahi-common/cdecl.h38
-rw-r--r--avahi-common/dbus-watch-glue.c357
-rw-r--r--avahi-common/dbus-watch-glue.h33
-rw-r--r--avahi-common/dbus.c138
-rw-r--r--avahi-common/dbus.h119
-rw-r--r--avahi-common/defs.h356
-rw-r--r--avahi-common/domain-test.c123
-rw-r--r--avahi-common/domain.c609
-rw-r--r--avahi-common/domain.h129
-rw-r--r--avahi-common/error.c97
-rw-r--r--avahi-common/error.h107
-rw-r--r--avahi-common/gccmacro.h74
-rw-r--r--avahi-common/i18n.c38
-rw-r--r--avahi-common/i18n.h53
-rw-r--r--avahi-common/llist.h75
-rw-r--r--avahi-common/malloc.c257
-rw-r--r--avahi-common/malloc.h96
-rw-r--r--avahi-common/rlist.c62
-rw-r--r--avahi-common/rlist.h49
-rw-r--r--avahi-common/simple-watch.c649
-rw-r--r--avahi-common/simple-watch.h85
-rw-r--r--avahi-common/strlst-test.c127
-rw-r--r--avahi-common/strlst.c505
-rw-r--r--avahi-common/strlst.h180
-rw-r--r--avahi-common/thread-watch.c186
-rw-r--r--avahi-common/thread-watch.h80
-rw-r--r--avahi-common/timeval-test.c43
-rw-r--r--avahi-common/timeval.c123
-rw-r--r--avahi-common/timeval.h54
-rw-r--r--avahi-common/utf8-test.c37
-rw-r--r--avahi-common/utf8.c110
-rw-r--r--avahi-common/utf8.h33
-rw-r--r--avahi-common/watch-test.c115
-rw-r--r--avahi-common/watch.h97
41 files changed, 5966 insertions, 0 deletions
diff --git a/avahi-common/.gitignore b/avahi-common/.gitignore
new file mode 100644
index 0000000..b523810
--- /dev/null
+++ b/avahi-common/.gitignore
@@ -0,0 +1,14 @@
+*.o
+*.lo
+*.la
+Makefile
+Makefile.in
+.deps
+.libs
+alternative-test
+domain-test
+strlst-test
+timeval-test
+utf8-test
+watch-test
+watch-test-thread
diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am
new file mode 100644
index 0000000..79d062f
--- /dev/null
+++ b/avahi-common/Makefile.am
@@ -0,0 +1,129 @@
+# 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 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 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.
+
+AM_CFLAGS=-I$(top_srcdir)
+
+# This cool debug trap works on i386/gcc only
+AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
+
+avahi_commonincludedir=$(includedir)/avahi-common
+
+avahi_commoninclude_HEADERS = \
+ strlst.h \
+ address.h \
+ alternative.h \
+ domain.h \
+ cdecl.h \
+ defs.h \
+ malloc.h \
+ watch.h \
+ timeval.h \
+ simple-watch.h \
+ thread-watch.h \
+ gccmacro.h \
+ error.h \
+ llist.h \
+ rlist.h
+
+if ENABLE_TESTS
+noinst_PROGRAMS = \
+ strlst-test \
+ domain-test \
+ alternative-test \
+ timeval-test \
+ watch-test \
+ watch-test-thread \
+ utf8-test
+endif
+
+lib_LTLIBRARIES = \
+ libavahi-common.la
+
+libavahi_common_la_SOURCES = \
+ malloc.c malloc.h \
+ address.c address.h \
+ alternative.c alternative.h \
+ error.c error.h \
+ strlst.c strlst.h \
+ domain.c domain.h \
+ timeval.c timeval.h \
+ simple-watch.c simple-watch.h \
+ thread-watch.c thread-watch.h \
+ watch.h gccmacro.h \
+ rlist.h rlist.c \
+ utf8.c utf8.h \
+ i18n.c i18n.h
+
+libavahi_common_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) -DAVAHI_LOCALEDIR=\"$(avahilocaledir)\"
+libavahi_common_la_LIBADD = $(AM_LDADD) $(PTHREAD_CFLAGS) $(PTHREAD_LIBS) $(INTLLIBS)
+libavahi_common_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBAVAHI_COMMON_VERSION_INFO)
+
+strlst_test_SOURCES = \
+ strlst.c strlst.h \
+ malloc.c malloc.h \
+ strlst-test.c
+strlst_test_CFLAGS = $(AM_CFLAGS)
+
+alternative_test_SOURCES = \
+ alternative.c alternative.h \
+ malloc.c malloc.h \
+ domain.c domain.h \
+ address.c address.h \
+ alternative-test.c \
+ utf8.c utf8.h
+alternative_test_CFLAGS = $(AM_CFLAGS)
+
+domain_test_SOURCES = \
+ domain.c domain.h \
+ malloc.c malloc.h \
+ address.c address.h \
+ domain-test.c \
+ utf8.c utf8.h
+domain_test_CFLAGS = $(AM_CFLAGS)
+
+watch_test_SOURCES = \
+ timeval.c timeval.h \
+ simple-watch.c simple-watch.h \
+ watch.h \
+ malloc.c malloc.h \
+ watch-test.c
+watch_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
+watch_test_LDADD = $(AM_LDADD) $(PTHREAD_LIBS) $(PTHREAD_CFLAGS)
+
+watch_test_thread_SOURCES = $(watch_test_SOURCES) thread-watch.c thread-watch.h
+watch_test_thread_CFLAGS = $(watch_test_CFLAGS) -DUSE_THREAD
+watch_test_thread_LDADD = $(watch_test_LDADD)
+
+timeval_test_SOURCES = \
+ timeval.c timeval.h \
+ timeval-test.c
+timeval_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
+timeval_test_LDADD = $(AM_LDADD) $(PTHREAD_LIBS) $(PTHREAD_CFLAGS)
+
+utf8_test_SOURCES = \
+ utf8-test.c \
+ utf8.c utf8.h
+utf8_test_CFLAGS = $(AM_CFLAGS)
+utf8_test_LDADD = $(AM_LDADD)
+
+if HAVE_DBUS
+
+noinst_HEADERS = \
+ dbus.h \
+ dbus-watch-glue.h
+
+endif
diff --git a/avahi-common/address.c b/avahi-common/address.c
new file mode 100644
index 0000000..e8f6148
--- /dev/null
+++ b/avahi-common/address.c
@@ -0,0 +1,155 @@
+/***
+ 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 <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include "address.h"
+#include "malloc.h"
+
+static size_t address_get_size(const AvahiAddress *a) {
+ assert(a);
+
+ if (a->proto == AVAHI_PROTO_INET)
+ return 4;
+ else if (a->proto == AVAHI_PROTO_INET6)
+ return 16;
+
+ return 0;
+}
+
+int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
+ assert(a);
+ assert(b);
+
+ if (a->proto != b->proto)
+ return -1;
+
+ return memcmp(a->data.data, b->data.data, address_get_size(a));
+}
+
+char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
+ assert(s);
+ assert(length);
+ assert(a);
+
+ if (!(inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length)))
+ return NULL;
+
+ return s;
+}
+
+char* avahi_reverse_lookup_name(const AvahiAddress *a, char *ret_s, size_t length) {
+ assert(ret_s);
+ assert(length > 0);
+ assert(a);
+
+ if (a->proto == AVAHI_PROTO_INET) {
+ uint32_t n = ntohl(a->data.ipv4.address);
+ snprintf(
+ ret_s, length,
+ "%u.%u.%u.%u.in-addr.arpa",
+ n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
+ } else {
+ assert(a->proto == AVAHI_PROTO_INET6);
+
+ snprintf(
+ ret_s, length,
+ "%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.ip6.arpa",
+ a->data.ipv6.address[15] & 0xF, a->data.ipv6.address[15] >> 4,
+ a->data.ipv6.address[14] & 0xF, a->data.ipv6.address[14] >> 4,
+ a->data.ipv6.address[13] & 0xF, a->data.ipv6.address[13] >> 4,
+ a->data.ipv6.address[12] & 0xF, a->data.ipv6.address[12] >> 4,
+ a->data.ipv6.address[11] & 0xF, a->data.ipv6.address[11] >> 4,
+ a->data.ipv6.address[10] & 0xF, a->data.ipv6.address[10] >> 4,
+ a->data.ipv6.address[ 9] & 0xF, a->data.ipv6.address[ 9] >> 4,
+ a->data.ipv6.address[ 8] & 0xF, a->data.ipv6.address[ 8] >> 4,
+ a->data.ipv6.address[ 7] & 0xF, a->data.ipv6.address[ 7] >> 4,
+ a->data.ipv6.address[ 6] & 0xF, a->data.ipv6.address[ 6] >> 4,
+ a->data.ipv6.address[ 5] & 0xF, a->data.ipv6.address[ 5] >> 4,
+ a->data.ipv6.address[ 4] & 0xF, a->data.ipv6.address[ 4] >> 4,
+ a->data.ipv6.address[ 3] & 0xF, a->data.ipv6.address[ 3] >> 4,
+ a->data.ipv6.address[ 2] & 0xF, a->data.ipv6.address[ 2] >> 4,
+ a->data.ipv6.address[ 1] & 0xF, a->data.ipv6.address[ 1] >> 4,
+ a->data.ipv6.address[ 0] & 0xF, a->data.ipv6.address[ 0] >> 4);
+ }
+
+ return ret_s;
+}
+
+AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) {
+ assert(ret_addr);
+ assert(s);
+
+ if (proto == AVAHI_PROTO_UNSPEC) {
+ if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
+ if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
+ return NULL;
+ else
+ ret_addr->proto = AVAHI_PROTO_INET6;
+ } else
+ ret_addr->proto = AVAHI_PROTO_INET;
+ } else {
+ if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0)
+ return NULL;
+
+ ret_addr->proto = proto;
+ }
+
+ return ret_addr;
+}
+
+int avahi_proto_to_af(AvahiProtocol proto) {
+ if (proto == AVAHI_PROTO_INET)
+ return AF_INET;
+ if (proto == AVAHI_PROTO_INET6)
+ return AF_INET6;
+
+ assert(proto == AVAHI_PROTO_UNSPEC);
+ return AF_UNSPEC;
+}
+
+AvahiProtocol avahi_af_to_proto(int af) {
+ if (af == AF_INET)
+ return AVAHI_PROTO_INET;
+ if (af == AF_INET6)
+ return AVAHI_PROTO_INET6;
+
+ assert(af == AF_UNSPEC);
+ return AVAHI_PROTO_UNSPEC;
+}
+
+const char* avahi_proto_to_string(AvahiProtocol proto) {
+ if (proto == AVAHI_PROTO_INET)
+ return "IPv4";
+ if (proto == AVAHI_PROTO_INET6)
+ return "IPv6";
+
+ assert(proto == AVAHI_PROTO_UNSPEC);
+ return "UNSPEC";
+}
diff --git a/avahi-common/address.h b/avahi-common/address.h
new file mode 100644
index 0000000..a14104f
--- /dev/null
+++ b/avahi-common/address.h
@@ -0,0 +1,119 @@
+#ifndef fooaddresshfoo
+#define fooaddresshfoo
+
+/***
+ 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.
+***/
+
+/** \file address.h Definitions and functions to manipulate IP addresses. */
+
+#include <inttypes.h>
+#include <sys/types.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** Protocol family specification, takes the values AVAHI_PROTO_INET, AVAHI_PROTO_INET6, AVAHI_PROTO_UNSPEC */
+typedef int AvahiProtocol;
+
+/** Numeric network interface index. Takes OS dependent values and the special constant AVAHI_IF_UNSPEC */
+typedef int AvahiIfIndex;
+
+/** Values for AvahiProtocol */
+enum {
+ AVAHI_PROTO_INET = 0, /**< IPv4 */
+ AVAHI_PROTO_INET6 = 1, /**< IPv6 */
+ AVAHI_PROTO_UNSPEC = -1 /**< Unspecified/all protocol(s) */
+};
+
+/** Special values for AvahiIfIndex */
+enum {
+ AVAHI_IF_UNSPEC = -1 /**< Unspecified/all interface(s) */
+};
+
+/** Maximum size of an address in string form */
+#define AVAHI_ADDRESS_STR_MAX 40 /* IPv6 Max = 4*8 + 7 + 1 for NUL */
+
+/** Return TRUE if the specified interface index is valid */
+#define AVAHI_IF_VALID(ifindex) (((ifindex) >= 0) || ((ifindex) == AVAHI_IF_UNSPEC))
+
+/** Return TRUE if the specified protocol is valid */
+#define AVAHI_PROTO_VALID(protocol) (((protocol) == AVAHI_PROTO_INET) || ((protocol) == AVAHI_PROTO_INET6) || ((protocol) == AVAHI_PROTO_UNSPEC))
+
+/** An IPv4 address */
+typedef struct AvahiIPv4Address {
+ uint32_t address; /**< Address data in network byte order. */
+} AvahiIPv4Address;
+
+/** An IPv6 address */
+typedef struct AvahiIPv6Address {
+ uint8_t address[16]; /**< Address data */
+} AvahiIPv6Address;
+
+/** Protocol (address family) independent address structure */
+typedef struct AvahiAddress {
+ AvahiProtocol proto; /**< Address family */
+
+ union {
+ AvahiIPv6Address ipv6; /**< Address when IPv6 */
+ AvahiIPv4Address ipv4; /**< Address when IPv4 */
+ uint8_t data[1]; /**< Type-independent data field */
+ } data;
+} AvahiAddress;
+
+/** @{ \name Comparison */
+
+/** Compare two addresses. Returns 0 when equal, a negative value when a < b, a positive value when a > b. */
+int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b);
+
+/** @} */
+
+/** @{ \name String conversion */
+
+/** Convert the specified address *a to a human readable character string, use AVAHI_ADDRESS_STR_MAX to allocate an array of the right size */
+char *avahi_address_snprint(char *ret_s, size_t length, const AvahiAddress *a);
+
+/** Convert the specified human readable character string to an
+ * address structure. Set af to AVAHI_UNSPEC for automatic address
+ * family detection. */
+AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol af, AvahiAddress *ret_addr);
+
+/** @} */
+
+/** \cond fulldocs */
+/** Generate the DNS reverse lookup name for an IPv4 or IPv6 address. */
+char* avahi_reverse_lookup_name(const AvahiAddress *a, char *ret_s, size_t length);
+/** \endcond */
+
+/** @{ \name Protocol/address family handling */
+
+/** Map AVAHI_PROTO_xxx constants to Unix AF_xxx constants */
+int avahi_proto_to_af(AvahiProtocol proto);
+
+/** Map Unix AF_xxx constants to AVAHI_PROTO_xxx constants */
+AvahiProtocol avahi_af_to_proto(int af);
+
+/** Return a textual representation of the specified protocol number. i.e. "IPv4", "IPv6" or "UNSPEC" */
+const char* avahi_proto_to_string(AvahiProtocol proto);
+
+/** @} */
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/alternative-test.c b/avahi-common/alternative-test.c
new file mode 100644
index 0000000..9255435
--- /dev/null
+++ b/avahi-common/alternative-test.c
@@ -0,0 +1,90 @@
+/***
+ 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 "alternative.h"
+#include "malloc.h"
+#include "domain.h"
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+ const char* const test_strings[] = {
+ "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
+ "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXüüüüüüü",
+ "gurke",
+ "-",
+ " #",
+ "1",
+ "#0",
+ " #0",
+ " #1",
+ "#-1",
+ " #-1",
+ "-0",
+ "--0",
+ "-1",
+ "--1",
+ "-2",
+ "gurke1",
+ "gurke0",
+ "gurke-2",
+ "gurke #0",
+ "gurke #1",
+ "gurke #",
+ "gurke#1",
+ "gurke-",
+ "gurke---",
+ "gurke #",
+ "gurke ###",
+ NULL
+ };
+
+ char *r = NULL;
+ int i, j, k;
+
+ for (k = 0; test_strings[k]; k++) {
+
+ printf(">>>>>%s<<<<\n", test_strings[k]);
+
+ for (j = 0; j < 2; j++) {
+
+ for (i = 0; i <= 100; i++) {
+ char *n;
+
+ n = i == 0 ? avahi_strdup(test_strings[k]) : (j ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r));
+ avahi_free(r);
+ r = n;
+
+ if (j)
+ assert(avahi_is_valid_service_name(n));
+ else
+ assert(avahi_is_valid_host_name(n));
+
+ printf("%s\n", r);
+ }
+ }
+ }
+
+ avahi_free(r);
+ return 0;
+}
diff --git a/avahi-common/alternative.c b/avahi-common/alternative.c
new file mode 100644
index 0000000..b3d39f0
--- /dev/null
+++ b/avahi-common/alternative.c
@@ -0,0 +1,182 @@
+/***
+ 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 <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "alternative.h"
+#include "malloc.h"
+#include "domain.h"
+#include "utf8.h"
+
+static void drop_incomplete_utf8(char *c) {
+ char *e;
+
+ e = strchr(c, 0) - 1;
+
+ while (e >= c) {
+
+ if (avahi_utf8_valid(c))
+ break;
+
+ assert(*e & 128);
+ *e = 0;
+
+ e--;
+ }
+}
+
+char *avahi_alternative_host_name(const char *s) {
+ const char *e;
+ char *r;
+
+ assert(s);
+
+ if (!avahi_is_valid_host_name(s))
+ return NULL;
+
+ if ((e = strrchr(s, '-'))) {
+ const char *p;
+
+ e++;
+
+ for (p = e; *p; p++)
+ if (!isdigit(*p)) {
+ e = NULL;
+ break;
+ }
+
+ if (e && (*e == '0' || *e == 0))
+ e = NULL;
+ }
+
+ if (e) {
+ char *c, *m;
+ size_t l;
+ int n;
+
+ n = atoi(e)+1;
+ if (!(m = avahi_strdup_printf("%i", n)))
+ return NULL;
+
+ l = e-s-1;
+
+ if (l >= AVAHI_LABEL_MAX-1-strlen(m)-1)
+ l = AVAHI_LABEL_MAX-1-strlen(m)-1;
+
+ if (!(c = avahi_strndup(s, l))) {
+ avahi_free(m);
+ return NULL;
+ }
+
+ drop_incomplete_utf8(c);
+
+ r = avahi_strdup_printf("%s-%s", c, m);
+ avahi_free(c);
+ avahi_free(m);
+
+ } else {
+ char *c;
+
+ if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-2)))
+ return NULL;
+
+ drop_incomplete_utf8(c);
+
+ r = avahi_strdup_printf("%s-2", c);
+ avahi_free(c);
+ }
+
+ assert(avahi_is_valid_host_name(r));
+
+ return r;
+}
+
+char *avahi_alternative_service_name(const char *s) {
+ const char *e;
+ char *r;
+
+ assert(s);
+
+ if (!avahi_is_valid_service_name(s))
+ return NULL;
+
+ if ((e = strstr(s, " #"))) {
+ const char *n, *p;
+ e += 2;
+
+ while ((n = strstr(e, " #")))
+ e = n + 2;
+
+ for (p = e; *p; p++)
+ if (!isdigit(*p)) {
+ e = NULL;
+ break;
+ }
+
+ if (e && (*e == '0' || *e == 0))
+ e = NULL;
+ }
+
+ if (e) {
+ char *c, *m;
+ size_t l;
+ int n;
+
+ n = atoi(e)+1;
+ if (!(m = avahi_strdup_printf("%i", n)))
+ return NULL;
+
+ l = e-s-2;
+
+ if (l >= AVAHI_LABEL_MAX-1-strlen(m)-2)
+ l = AVAHI_LABEL_MAX-1-strlen(m)-2;
+
+ if (!(c = avahi_strndup(s, l))) {
+ avahi_free(m);
+ return NULL;
+ }
+
+ drop_incomplete_utf8(c);
+
+ r = avahi_strdup_printf("%s #%s", c, m);
+ avahi_free(c);
+ avahi_free(m);
+ } else {
+ char *c;
+
+ if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-3)))
+ return NULL;
+
+ drop_incomplete_utf8(c);
+
+ r = avahi_strdup_printf("%s #2", c);
+ avahi_free(c);
+ }
+
+ assert(avahi_is_valid_service_name(r));
+
+ return r;
+}
diff --git a/avahi-common/alternative.h b/avahi-common/alternative.h
new file mode 100644
index 0000000..9b044de
--- /dev/null
+++ b/avahi-common/alternative.h
@@ -0,0 +1,43 @@
+#ifndef fooalternativehfoo
+#define fooalternativehfoo
+
+/***
+ 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.
+***/
+
+/** \file alternative.h Functions to find alternative names for hosts and services in the case of name collision */
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** Find an alternative for the specified host name. If called with an
+ * original host name, "-2" is appended, afterwards the number is
+ * increased on each call. (i.e. "foo" becomes "foo-2" becomes "foo-3"
+ * and so on.) avahi_free() the result. */
+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.) avahi_free() the result. */
+char *avahi_alternative_service_name(const char *s);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/cdecl.h b/avahi-common/cdecl.h
new file mode 100644
index 0000000..aef6aba
--- /dev/null
+++ b/avahi-common/cdecl.h
@@ -0,0 +1,38 @@
+#ifndef foocdeclhfoo
+#define foocdeclhfoo
+
+/***
+ 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.
+***/
+
+/** \file cdecl.h C++ compatibility */
+#ifdef __cplusplus
+/** If using C++ this macro enables C mode, otherwise does nothing */
+#define AVAHI_C_DECL_BEGIN extern "C" {
+/** If using C++ this macro switches back to C++ mode, otherwise does nothing */
+#define AVAHI_C_DECL_END }
+
+#else
+/** If using C++ this macro enables C mode, otherwise does nothing */
+#define AVAHI_C_DECL_BEGIN
+/** If using C++ this macro switches back to C++ mode, otherwise does nothing */
+#define AVAHI_C_DECL_END
+
+#endif
+
+#endif
diff --git a/avahi-common/dbus-watch-glue.c b/avahi-common/dbus-watch-glue.c
new file mode 100644
index 0000000..b18f555
--- /dev/null
+++ b/avahi-common/dbus-watch-glue.c
@@ -0,0 +1,357 @@
+/***
+ 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 <assert.h>
+#include <stdio.h>
+
+#include "malloc.h"
+#include "timeval.h"
+#include "dbus-watch-glue.h"
+
+static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
+ AvahiWatchEvent e = 0;
+
+ if (f & DBUS_WATCH_READABLE)
+ e |= AVAHI_WATCH_IN;
+ if (f & DBUS_WATCH_WRITABLE)
+ e |= AVAHI_WATCH_OUT;
+ if (f & DBUS_WATCH_ERROR)
+ e |= AVAHI_WATCH_ERR;
+ if (f & DBUS_WATCH_HANGUP)
+ e |= AVAHI_WATCH_HUP;
+
+ return e;
+}
+
+static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
+ unsigned int f = 0;
+
+ if (e & AVAHI_WATCH_IN)
+ f |= DBUS_WATCH_READABLE;
+ if (e & AVAHI_WATCH_OUT)
+ f |= DBUS_WATCH_WRITABLE;
+ if (e & AVAHI_WATCH_ERR)
+ f |= DBUS_WATCH_ERROR;
+ if (e & AVAHI_WATCH_HUP)
+ f |= DBUS_WATCH_HANGUP;
+
+ return f;
+}
+
+typedef struct {
+ DBusConnection *connection;
+ const AvahiPoll *poll_api;
+ AvahiTimeout *dispatch_timeout;
+ int ref;
+} ConnectionData;
+
+static ConnectionData *connection_data_ref(ConnectionData *d) {
+ assert(d);
+ assert(d->ref >= 1);
+
+ d->ref++;
+ return d;
+}
+
+static void connection_data_unref(ConnectionData *d) {
+ assert(d);
+ assert(d->ref >= 1);
+
+ if (--d->ref <= 0) {
+ d->poll_api->timeout_free(d->dispatch_timeout);
+ avahi_free(d);
+ }
+}
+
+static void request_dispatch(ConnectionData *d, int enable) {
+ static const struct timeval tv = { 0, 0 };
+ assert(d);
+
+ if (enable) {
+ assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
+ d->poll_api->timeout_update(d->dispatch_timeout, &tv);
+ } else
+ d->poll_api->timeout_update(d->dispatch_timeout, NULL);
+}
+
+static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
+ ConnectionData *d = userdata;
+ assert(t);
+ assert(d);
+
+ connection_data_ref(d);
+ dbus_connection_ref(d->connection);
+
+ if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)
+ /* If there's still data, request that this handler is called again */
+ request_dispatch(d, 1);
+ else
+ request_dispatch(d, 0);
+
+ dbus_connection_unref(d->connection);
+ connection_data_unref(d);
+}
+
+static void watch_callback(AvahiWatch *avahi_watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
+ DBusWatch *dbus_watch = userdata;
+
+ assert(avahi_watch);
+ assert(dbus_watch);
+
+ dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
+ /* Ignore the return value */
+}
+
+static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
+ AvahiWatch *avahi_watch;
+ dbus_bool_t b;
+
+ assert(dbus_watch);
+
+ avahi_watch = dbus_watch_get_data(dbus_watch);
+
+ b = dbus_watch_get_enabled(dbus_watch);
+
+ if (b && !avahi_watch) {
+
+ if (!(avahi_watch = poll_api->watch_new(
+ poll_api,
+#if (DBUS_VERSION_MAJOR == 1 && DBUS_VERSION_MINOR == 1 && DBUS_VERSION_MICRO >= 1) || (DBUS_VERSION_MAJOR == 1 && DBUS_VERSION_MINOR > 1) || (DBUS_VERSION_MAJOR > 1)
+ dbus_watch_get_unix_fd(dbus_watch),
+#else
+ dbus_watch_get_fd(dbus_watch),
+#endif
+ translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
+ watch_callback,
+ dbus_watch)))
+ return FALSE;
+
+ dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
+
+ } else if (!b && avahi_watch) {
+
+ poll_api->watch_free(avahi_watch);
+ dbus_watch_set_data(dbus_watch, NULL, NULL);
+
+ } else if (avahi_watch) {
+
+ /* Update flags */
+ poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
+ }
+
+ return TRUE;
+}
+
+static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
+ ConnectionData *d = userdata;
+
+ assert(dbus_watch);
+ assert(d);
+
+ return update_watch(d->poll_api, dbus_watch);
+}
+
+static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
+ ConnectionData *d = userdata;
+ AvahiWatch *avahi_watch;
+
+ assert(dbus_watch);
+ assert(d);
+
+ if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
+ d->poll_api->watch_free(avahi_watch);
+ dbus_watch_set_data(dbus_watch, NULL, NULL);
+ }
+}
+
+static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
+ ConnectionData *d = userdata;
+
+ assert(dbus_watch);
+ assert(d);
+
+ update_watch(d->poll_api, dbus_watch);
+}
+
+typedef struct TimeoutData {
+ const AvahiPoll *poll_api;
+ AvahiTimeout *avahi_timeout;
+ DBusTimeout *dbus_timeout;
+ int ref;
+} TimeoutData;
+
+static TimeoutData* timeout_data_ref(TimeoutData *t) {
+ assert(t);
+ assert(t->ref >= 1);
+
+ t->ref++;
+ return t;
+}
+
+static void timeout_data_unref(TimeoutData *t) {
+ assert(t);
+ assert(t->ref >= 1);
+
+ if (--t->ref <= 0) {
+ if (t->avahi_timeout)
+ t->poll_api->timeout_free(t->avahi_timeout);
+
+ avahi_free(t);
+ }
+}
+
+static void update_timeout(TimeoutData *timeout) {
+ assert(timeout);
+ assert(timeout->ref >= 1);
+
+ if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
+ struct timeval tv;
+ avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
+ timeout->poll_api->timeout_update(timeout->
+ avahi_timeout, &tv);
+ } else
+ timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
+
+}
+
+static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
+ TimeoutData *timeout = userdata;
+
+ assert(avahi_timeout);
+ assert(timeout);
+
+ timeout_data_ref(timeout);
+
+ dbus_timeout_handle(timeout->dbus_timeout);
+ /* Ignore the return value */
+
+ if (timeout->avahi_timeout)
+ update_timeout(timeout);
+
+ timeout_data_unref(timeout);
+}
+
+static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
+ TimeoutData *timeout;
+ ConnectionData *d = userdata;
+ struct timeval tv;
+ dbus_bool_t b;
+
+ assert(dbus_timeout);
+ assert(d);
+
+ if (!(timeout = avahi_new(TimeoutData, 1)))
+ return FALSE;
+
+ timeout->dbus_timeout = dbus_timeout;
+ timeout->poll_api = d->poll_api;
+ timeout->ref = 1;
+
+ if ((b = dbus_timeout_get_enabled(dbus_timeout)))
+ avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
+
+ if (!(timeout->avahi_timeout = d->poll_api->timeout_new(
+ d->poll_api,
+ b ? &tv : NULL,
+ timeout_callback,
+ timeout))) {
+ avahi_free(timeout);
+ return FALSE;
+ }
+
+ dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);
+ return TRUE;
+}
+
+static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
+ ConnectionData *d = userdata;
+ TimeoutData *timeout;
+
+ assert(dbus_timeout);
+ assert(d);
+
+ timeout = dbus_timeout_get_data(dbus_timeout);
+ assert(timeout);
+
+ d->poll_api->timeout_free(timeout->avahi_timeout);
+ timeout->avahi_timeout = NULL;
+}
+
+static void timeout_toggled(DBusTimeout *dbus_timeout, AVAHI_GCC_UNUSED void *userdata) {
+ TimeoutData *timeout;
+
+ assert(dbus_timeout);
+ timeout = dbus_timeout_get_data(dbus_timeout);
+ assert(timeout);
+
+ update_timeout(timeout);
+}
+
+static void dispatch_status(AVAHI_GCC_UNUSED DBusConnection *connection, DBusDispatchStatus new_status, void *userdata) {
+ ConnectionData *d = userdata;
+
+ if (new_status == DBUS_DISPATCH_DATA_REMAINS)
+ request_dispatch(d, 1);
+ }
+
+int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
+ ConnectionData *d = NULL;
+
+ assert(c);
+ assert(poll_api);
+
+ if (!(d = avahi_new(ConnectionData, 1)))
+ goto fail;;
+
+ d->poll_api = poll_api;
+ d->connection = c;
+ d->ref = 1;
+
+ if (!(d->dispatch_timeout = poll_api->timeout_new(poll_api, NULL, dispatch_timeout_callback, d)))
+ goto fail;
+
+ if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
+ goto fail;
+
+ if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
+ goto fail;
+
+ dbus_connection_set_dispatch_status_function(c, dispatch_status, connection_data_ref(d), (DBusFreeFunction)connection_data_unref);
+
+ if (dbus_connection_get_dispatch_status(c) == DBUS_DISPATCH_DATA_REMAINS)
+ request_dispatch(d, 1);
+
+ connection_data_unref(d);
+
+ return 0;
+
+fail:
+
+ if (d) {
+ d->poll_api->timeout_free(d->dispatch_timeout);
+
+ avahi_free(d);
+ }
+
+ return -1;
+}
diff --git a/avahi-common/dbus-watch-glue.h b/avahi-common/dbus-watch-glue.h
new file mode 100644
index 0000000..138ea97
--- /dev/null
+++ b/avahi-common/dbus-watch-glue.h
@@ -0,0 +1,33 @@
+#ifndef foodbuswatchgluehfoo
+#define foodbuswatchgluehfoo
+
+/***
+ 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 <dbus/dbus.h>
+
+#include <avahi-common/watch.h>
+
+AVAHI_C_DECL_BEGIN
+
+int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/dbus.c b/avahi-common/dbus.c
new file mode 100644
index 0000000..f19c77c
--- /dev/null
+++ b/avahi-common/dbus.c
@@ -0,0 +1,138 @@
+/***
+ 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 <avahi-common/error.h>
+#include <avahi-common/dbus.h>
+
+static const char * const table[- AVAHI_ERR_MAX] = {
+ AVAHI_DBUS_ERR_OK,
+ AVAHI_DBUS_ERR_FAILURE,
+ AVAHI_DBUS_ERR_BAD_STATE,
+ AVAHI_DBUS_ERR_INVALID_HOST_NAME,
+ AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME,
+ AVAHI_DBUS_ERR_NO_NETWORK,
+ AVAHI_DBUS_ERR_INVALID_TTL,
+ AVAHI_DBUS_ERR_IS_PATTERN,
+ AVAHI_DBUS_ERR_COLLISION,
+ AVAHI_DBUS_ERR_INVALID_RECORD,
+
+ AVAHI_DBUS_ERR_INVALID_SERVICE_NAME,
+ AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE,
+ AVAHI_DBUS_ERR_INVALID_PORT,
+ AVAHI_DBUS_ERR_INVALID_KEY,
+ AVAHI_DBUS_ERR_INVALID_ADDRESS,
+ AVAHI_DBUS_ERR_TIMEOUT,
+ AVAHI_DBUS_ERR_TOO_MANY_CLIENTS,
+ AVAHI_DBUS_ERR_TOO_MANY_OBJECTS,
+ AVAHI_DBUS_ERR_TOO_MANY_ENTRIES,
+ AVAHI_DBUS_ERR_OS,
+
+ AVAHI_DBUS_ERR_ACCESS_DENIED,
+ AVAHI_DBUS_ERR_INVALID_OPERATION,
+ AVAHI_DBUS_ERR_DBUS_ERROR,
+ AVAHI_DBUS_ERR_DISCONNECTED,
+ AVAHI_DBUS_ERR_NO_MEMORY,
+ AVAHI_DBUS_ERR_INVALID_OBJECT,
+ AVAHI_DBUS_ERR_NO_DAEMON,
+ AVAHI_DBUS_ERR_INVALID_INTERFACE,
+ AVAHI_DBUS_ERR_INVALID_PROTOCOL,
+ AVAHI_DBUS_ERR_INVALID_FLAGS,
+
+ AVAHI_DBUS_ERR_NOT_FOUND,
+ AVAHI_DBUS_ERR_INVALID_CONFIG,
+ AVAHI_DBUS_ERR_VERSION_MISMATCH,
+ AVAHI_DBUS_ERR_INVALID_SERVICE_SUBTYPE,
+ AVAHI_DBUS_ERR_INVALID_PACKET,
+ AVAHI_DBUS_ERR_INVALID_DNS_ERROR,
+ AVAHI_DBUS_ERR_DNS_FORMERR,
+ AVAHI_DBUS_ERR_DNS_SERVFAIL,
+ AVAHI_DBUS_ERR_DNS_NXDOMAIN,
+ AVAHI_DBUS_ERR_DNS_NOTIMP,
+
+ AVAHI_DBUS_ERR_DNS_REFUSED,
+ AVAHI_DBUS_ERR_DNS_YXDOMAIN,
+ AVAHI_DBUS_ERR_DNS_YXRRSET,
+ AVAHI_DBUS_ERR_DNS_NXRRSET,
+ AVAHI_DBUS_ERR_DNS_NOTAUTH,
+ AVAHI_DBUS_ERR_DNS_NOTZONE,
+ AVAHI_DBUS_ERR_INVALID_RDATA,
+ AVAHI_DBUS_ERR_INVALID_DNS_CLASS,
+ AVAHI_DBUS_ERR_INVALID_DNS_TYPE,
+ AVAHI_DBUS_ERR_NOT_SUPPORTED,
+
+ AVAHI_DBUS_ERR_NOT_PERMITTED,
+ AVAHI_DBUS_ERR_INVALID_ARGUMENT,
+ AVAHI_DBUS_ERR_IS_EMPTY,
+ AVAHI_DBUS_ERR_NO_CHANGE
+};
+
+struct error_map {
+ const char *dbus_error;
+ int avahi_error;
+};
+
+static struct error_map error_map[] = {
+ { DBUS_ERROR_FAILED, AVAHI_ERR_FAILURE },
+ { DBUS_ERROR_NO_MEMORY, AVAHI_ERR_NO_MEMORY },
+ { DBUS_ERROR_SERVICE_UNKNOWN, AVAHI_ERR_NO_DAEMON },
+ { DBUS_ERROR_BAD_ADDRESS, AVAHI_ERR_NO_DAEMON },
+ { DBUS_ERROR_NOT_SUPPORTED, AVAHI_ERR_NOT_SUPPORTED },
+ { DBUS_ERROR_LIMITS_EXCEEDED, AVAHI_ERR_TOO_MANY_OBJECTS },
+ { DBUS_ERROR_ACCESS_DENIED, AVAHI_ERR_ACCESS_DENIED },
+ { DBUS_ERROR_AUTH_FAILED, AVAHI_ERR_ACCESS_DENIED },
+ { DBUS_ERROR_NO_SERVER, AVAHI_ERR_NO_DAEMON },
+ { DBUS_ERROR_TIMEOUT, AVAHI_ERR_TIMEOUT },
+ { DBUS_ERROR_NO_NETWORK, AVAHI_ERR_NO_NETWORK },
+ { DBUS_ERROR_DISCONNECTED, AVAHI_ERR_DISCONNECTED },
+ { DBUS_ERROR_INVALID_ARGS, AVAHI_ERR_INVALID_ARGUMENT },
+ { DBUS_ERROR_TIMED_OUT, AVAHI_ERR_TIMEOUT },
+ { NULL, 0 }
+};
+
+int avahi_error_dbus_to_number(const char *s) {
+ int e;
+ const struct error_map *m;
+
+ assert(s);
+
+ for (e = -1; e > AVAHI_ERR_MAX; e--)
+ if (strcmp(s, table[-e]) == 0)
+ return e;
+
+ for (m = error_map; m->dbus_error; m++)
+ if (strcmp(m->dbus_error, s) == 0)
+ return m->avahi_error;
+
+ return AVAHI_ERR_DBUS_ERROR;
+}
+
+const char *avahi_error_number_to_dbus(int error) {
+ assert(error > AVAHI_ERR_MAX);
+ assert(error < 1);
+
+ return table[-error];
+}
diff --git a/avahi-common/dbus.h b/avahi-common/dbus.h
new file mode 100644
index 0000000..ea3e8a2
--- /dev/null
+++ b/avahi-common/dbus.h
@@ -0,0 +1,119 @@
+#ifndef foodbushfoo
+#define foodbushfoo
+
+/***
+ 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.
+***/
+
+/** \file dbus.h Some definitions for the D-Bus interface */
+
+#include <avahi-common/cdecl.h>
+#include <dbus/dbus.h>
+
+AVAHI_C_DECL_BEGIN
+
+#define AVAHI_DBUS_NAME "org.freedesktop.Avahi"
+#define AVAHI_DBUS_INTERFACE_SERVER AVAHI_DBUS_NAME".Server"
+#define AVAHI_DBUS_PATH_SERVER "/"
+#define AVAHI_DBUS_INTERFACE_ENTRY_GROUP AVAHI_DBUS_NAME".EntryGroup"
+#define AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER AVAHI_DBUS_NAME".DomainBrowser"
+#define AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER AVAHI_DBUS_NAME".ServiceTypeBrowser"
+#define AVAHI_DBUS_INTERFACE_SERVICE_BROWSER AVAHI_DBUS_NAME".ServiceBrowser"
+#define AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER AVAHI_DBUS_NAME".AddressResolver"
+#define AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER AVAHI_DBUS_NAME".HostNameResolver"
+#define AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER AVAHI_DBUS_NAME".ServiceResolver"
+#define AVAHI_DBUS_INTERFACE_RECORD_BROWSER AVAHI_DBUS_NAME".RecordBrowser"
+
+/** The D-Bus API version identifier. The first byte specifies the API
+release, the second byte specifies the revision. If the revision
+number is increased the API has been extended but is downwards
+compatible. If the release changes compatibility is lost.
+
+Avahi 0.6 implements API version 0x0201;
+Avahi 0.6.1 implements API version 0x0202 */
+#define AVAHI_DBUS_API_VERSION ((uint32_t) 0x0203)
+
+#define AVAHI_DBUS_ERR_OK "org.freedesktop.Avahi.Success"
+#define AVAHI_DBUS_ERR_FAILURE "org.freedesktop.Avahi.Failure"
+#define AVAHI_DBUS_ERR_BAD_STATE "org.freedesktop.Avahi.BadStateError"
+#define AVAHI_DBUS_ERR_INVALID_HOST_NAME "org.freedesktop.Avahi.InvalidHostNameError"
+#define AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME "org.freedesktop.Avahi.InvalidDomainNameError"
+#define AVAHI_DBUS_ERR_NO_NETWORK "org.freedesktop.Avahi.NoNetworkError"
+#define AVAHI_DBUS_ERR_INVALID_TTL "org.freedesktop.Avahi.InvalidTTLError"
+#define AVAHI_DBUS_ERR_IS_PATTERN "org.freedesktop.Avahi.IsPatternError"
+#define AVAHI_DBUS_ERR_COLLISION "org.freedesktop.Avahi.CollisionError"
+#define AVAHI_DBUS_ERR_INVALID_RECORD "org.freedesktop.Avahi.InvalidRecordError"
+
+#define AVAHI_DBUS_ERR_INVALID_SERVICE_NAME "org.freedesktop.Avahi.InvalidServiceNameError"
+#define AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE "org.freedesktop.Avahi.InvalidServiceTypeError"
+#define AVAHI_DBUS_ERR_INVALID_PORT "org.freedesktop.Avahi.InvalidPortError"
+#define AVAHI_DBUS_ERR_INVALID_KEY "org.freedesktop.Avahi.InvalidKeyError"
+#define AVAHI_DBUS_ERR_INVALID_ADDRESS "org.freedesktop.Avahi.InvalidAddressError"
+#define AVAHI_DBUS_ERR_TIMEOUT "org.freedesktop.Avahi.TimeoutError"
+#define AVAHI_DBUS_ERR_TOO_MANY_CLIENTS "org.freedesktop.Avahi.TooManyClientsError"
+#define AVAHI_DBUS_ERR_TOO_MANY_OBJECTS "org.freedesktop.Avahi.TooManyObjectsError"
+#define AVAHI_DBUS_ERR_TOO_MANY_ENTRIES "org.freedesktop.Avahi.TooManyEntriesError"
+#define AVAHI_DBUS_ERR_OS "org.freedesktop.Avahi.OSError"
+
+#define AVAHI_DBUS_ERR_ACCESS_DENIED DBUS_ERROR_ACCESS_DENIED
+#define AVAHI_DBUS_ERR_INVALID_OPERATION "org.freedesktop.Avahi.InvalidOperationError"
+#define AVAHI_DBUS_ERR_DBUS_ERROR "org.freedesktop.Avahi.DBusError"
+#define AVAHI_DBUS_ERR_DISCONNECTED "org.freedesktop.Avahi.DisconnectedError"
+#define AVAHI_DBUS_ERR_NO_MEMORY "org.freedesktop.Avahi.NoMemoryError"
+#define AVAHI_DBUS_ERR_INVALID_OBJECT "org.freedesktop.Avahi.InvalidObjectError"
+#define AVAHI_DBUS_ERR_NO_DAEMON "org.freedesktop.Avahi.NoDaemonError"
+#define AVAHI_DBUS_ERR_INVALID_INTERFACE "org.freedesktop.Avahi.InvalidInterfaceError"
+#define AVAHI_DBUS_ERR_INVALID_PROTOCOL "org.freedesktop.Avahi.InvalidInterfaceProtocolError"
+#define AVAHI_DBUS_ERR_INVALID_FLAGS "org.freedesktop.Avahi.InvalidFlagsError"
+
+#define AVAHI_DBUS_ERR_NOT_FOUND "org.freedesktop.Avahi.NotFoundError"
+#define AVAHI_DBUS_ERR_INVALID_CONFIG "org.freedesktop.Avahi.InvalidConfigurationError"
+#define AVAHI_DBUS_ERR_VERSION_MISMATCH "org.freedesktop.Avahi.VersionMismatchError"
+#define AVAHI_DBUS_ERR_INVALID_SERVICE_SUBTYPE "org.freedesktop.Avahi.InvalidServiceSubtypeError"
+#define AVAHI_DBUS_ERR_INVALID_PACKET "org.freedesktop.Avahi.InvalidPacketError"
+#define AVAHI_DBUS_ERR_INVALID_DNS_ERROR "org.freedesktop.Avahi.InvalidDNSErrorError"
+#define AVAHI_DBUS_ERR_DNS_FORMERR "org.freedesktop.Avahi.DNSFORMERR"
+#define AVAHI_DBUS_ERR_DNS_SERVFAIL "org.freedesktop.Avahi.DNSSERVFAIL"
+#define AVAHI_DBUS_ERR_DNS_NXDOMAIN "org.freedesktop.Avahi.DNSNXDOMAIN"
+#define AVAHI_DBUS_ERR_DNS_NOTIMP "org.freedesktop.Avahi.DNSNOTIMP"
+
+#define AVAHI_DBUS_ERR_DNS_REFUSED "org.freedesktop.Avahi.DNSREFUSED"
+#define AVAHI_DBUS_ERR_DNS_YXDOMAIN "org.freedesktop.Avahi.DNSYXDOMAIN"
+#define AVAHI_DBUS_ERR_DNS_YXRRSET "org.freedesktop.Avahi.DNSYXRRSET"
+#define AVAHI_DBUS_ERR_DNS_NXRRSET "org.freedesktop.Avahi.DNSNXRRSET"
+#define AVAHI_DBUS_ERR_DNS_NOTAUTH "org.freedesktop.Avahi.DNSNOTAUTH"
+#define AVAHI_DBUS_ERR_DNS_NOTZONE "org.freedesktop.Avahi.DNSNOTZONE"
+#define AVAHI_DBUS_ERR_INVALID_RDATA "org.freedesktop.Avahi.InvalidRDataError"
+#define AVAHI_DBUS_ERR_INVALID_DNS_CLASS "org.freedesktop.Avahi.InvalidDNSClassError"
+#define AVAHI_DBUS_ERR_INVALID_DNS_TYPE "org.freedesktop.Avahi.InvalidDNSTypeError"
+#define AVAHI_DBUS_ERR_NOT_SUPPORTED "org.freedesktop.Avahi.NotSupportedError"
+
+#define AVAHI_DBUS_ERR_NOT_PERMITTED "org.freedesktop.Avahi.NotPermittedError"
+#define AVAHI_DBUS_ERR_INVALID_ARGUMENT "org.freedesktop.Avahi.InvalidArgumentError"
+#define AVAHI_DBUS_ERR_IS_EMPTY "org.freedesktop.Avahi.IsEmptyError"
+#define AVAHI_DBUS_ERR_NO_CHANGE "org.freedesktop.Avahi.NoChangeError"
+
+/** Convert a DBus error string into an Avahi error number */
+int avahi_error_dbus_to_number(const char *s);
+
+/** Convert an Avahi error number into a DBus error string. Result should not be freed */
+const char * avahi_error_number_to_dbus(int error);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/defs.h b/avahi-common/defs.h
new file mode 100644
index 0000000..bb73a9d
--- /dev/null
+++ b/avahi-common/defs.h
@@ -0,0 +1,356 @@
+#ifndef foodefshfoo
+#define foodefshfoo
+
+/***
+ 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.
+***/
+
+/** \file defs.h Some common definitions */
+
+#include <avahi-common/cdecl.h>
+
+/** \mainpage
+ *
+ * \section choose_api Choosing an API
+ *
+ * Avahi provides three programming APIs for integration of
+ * mDNS/DNS-SD features into your C progams:
+ *
+ * \li <b>avahi-core</b>: an API for embedding a complete mDNS/DNS-SD stack
+ * into your software. This is intended for developers of embedded
+ * appliances only. We dissuade from using this API in normal desktop
+ * applications since it is not a good idea to run multiple mDNS
+ * stacks simultaneously on the same host.
+ * \li <b>the D-Bus API</b>: an extensive D-Bus interface for browsing and
+ * registering mDNS/DNS-SD services using avahi-daemon. We recommend
+ * using this API for software written in any language other than
+ * C (e.g. Python).
+ * \li <b>avahi-client</b>: a simplifying C wrapper around the D-Bus API. We
+ * recommend using this API in C or C++ progams. The D-Bus internals
+ * are hidden completely.
+ * \li <b>avahi-gobject</b>: an object-oriented C wrapper based on
+ * GLib's GObject. We recommd using this API for GNOME/Gtk programs.
+ *
+ * All three APIs are very similar, however avahi-core is the most powerful.
+ *
+ * In addition to the three APIs described above Avahi supports two
+ * compatibility libraries:
+ *
+ * \li <b>avahi-compat-libdns_sd</b>: the original Bonjour API as documented
+ * in the header file "dns_sd.h" by Apple Computer, Inc.
+ *
+ * \li <b>avahi-compat-howl</b>: the HOWL API as released with HOWL 0.9.8 by
+ * Porchdog Software.
+ *
+ * Please note that these compatibility layers are incomplete and
+ * generally a waste of resources. We strongly encourage everyone to
+ * use our native APIs for newly written programs and to port older
+ * programs to avahi-client!
+ *
+ * The native APIs (avahi-client and avahi-core) can be integrated
+ * into external event loops. We provide adapters for the following
+ * event loop implementations:
+ *
+ * \li <b>avahi-glib</b>: The GLIB main loop as used by GTk+/GNOME
+ *
+ * \li <b>avahi-qt</b>: The Qt main loop as used by Qt/KDE
+ *
+ * Finally, we provide a high-level Gtk+ GUI dialog called
+ * <b>avahi-ui</b> for user-friendly browsing for services.
+ *
+ * The doxygen-generated API documentation covers avahi-client
+ * (including its auxiliary APIs), the event loop adapters and
+ * avahi-ui. For the other APIs please consult the original
+ * documentation (for the compatibility APIs) or the header files.
+ *
+ * Please note that the doxygen-generated API documentation of the
+ * native Avahi API is not complete. A few definitions that are part
+ * of the Avahi API have been removed from this documentation, either
+ * because they are only relevant in a very few low-level applications
+ * or because they are considered obsolete. Please consult the C header
+ * files for all definitions that are part of the Avahi API. Please
+ * note that these hidden definitions are considered part of the Avahi
+ * API and will stay available in the API in the future.
+ *
+ * \section error_reporting Error Reporting
+ *
+ * Some notes on the Avahi error handling:
+ *
+ * - Error codes are negative integers and defined as AVAHI_ERR_xx
+ * - If a function returns some kind of non-negative integer value on
+ * success, a failure is indicated by returning the error code
+ * directly.
+ * - If a function returns a pointer of some kind on success, a
+ * failure is indicated by returning NULL
+ * - The last error number may be retrieved by calling
+ * avahi_client_errno()
+ * - Just like the libc errno variable the Avahi errno is NOT reset to
+ * AVAHI_OK if a function call succeeds.
+ * - You may convert a numeric error code into a human readable string
+ * using avahi_strerror()
+ * - The constructor function avahi_client_new() returns the error
+ * code in a call-by-reference argument
+ *
+ * \section event_loop Event Loop Abstraction
+ *
+ * Avahi uses a simple event loop abstraction layer. A table AvahiPoll
+ * which contains function pointers for user defined timeout and I/O
+ * condition event source implementations needs to be passed to
+ * avahi_client_new(). An adapter for this abstraction layer is
+ * available for the GLib main loop in the object AvahiGLibPoll. A
+ * simple stand-alone implementation is available under the name
+ * AvahiSimplePoll. An adpater for the Qt main loop is available from
+ * avahi_qt_poll_get().
+ *
+ * \section good_publish How to Register Services
+ *
+ * - Subscribe to server state changes. Pass a callback function
+ * pointer to avahi_client_new(). It will be called
+ * whenever the server state changes.
+ * - Only register your services when the server is in state
+ * AVAHI_SERVER_RUNNING. If you register your services in other server
+ * states they might not be accessible since the local host name might not necessarily
+ * be established.
+ * - Remove your services when the server enters
+ * AVAHI_SERVER_COLLISION or AVAHI_SERVER_REGISTERING state. Your
+ * services may not be reachable anymore since the local host name is
+ * no longer established or is currently in the process of being
+ * established.
+ * - When registering services, use the following algorithm:
+ * - Create a new entry group (i.e. avahi_entry_group_new())
+ * - Add your service(s)/additional RRs/subtypes (e.g. avahi_entry_group_add_service())
+ * - Commit the entry group (i.e. avahi_entry_group_commit())
+ * - Subscribe to entry group state changes.
+ * - If the entry group enters AVAHI_ENTRY_GROUP_COLLISION state the
+ * services of the entry group are automatically removed from the
+ * server. You may immediately add your services back to the entry
+ * group (but with new names, perhaps using
+ * avahi_alternative_service_name()) and commit again. Please do not
+ * free the entry group and create a new one. This would inhibit some
+ * traffic limiting algorithms in mDNS.
+ * - When you need to modify your services (i.e. change the TXT data
+ * or the port number), use the AVAHI_PUBLISH_UPDATE flag. Please do
+ * not free the entry group and create a new one. This would inhibit
+ * some traffic limiting algorithms in mDNS. When changing just the
+ * TXT data avahi_entry_group_update_txt() is a shortcut for
+ * AVAHI_PUBLISH_UPDATE. Please note that you cannot use
+ * AVAHI_PUBLISH_UPDATE when changing the service name! Renaming a
+ * DNS-SD service is identical to deleting and creating a new one, and
+ * that's exactly what you should do in that case. First call
+ * avahi_entry_group_reset() to remove it and then read it normally.
+ *
+ * \section good_browse How to Browse for Services
+ *
+ * - For normal applications you need to call avahi_service_browser_new()
+ * for the service type you want to browse for. Use
+ * avahi_service_resolver_new() to acquire service data for a service
+ * name.
+ * - You can use avahi_domain_browser_new() to get a list of announced
+ * browsing domains. Please note that not all domains whith services
+ * on the LAN are mandatorily announced.
+ * - There is no need to subscribe to server state changes.
+ *
+ * \section daemon_dies How to Write a Client That Can Deal with Daemon Restarts
+ *
+ * With Avahi it is possible to write client applications that can
+ * deal with Avahi daemon restarts. To accomplish that make sure to
+ * pass AVAHI_CLIENT_NO_FAIL to avahi_client_new()'s flags
+ * parameter. That way avahi_client_new() will succeed even when the
+ * daemon is not running. In that case the object will enter
+ * AVAHI_CLIENT_CONNECTING state. As soon as the daemon becomes
+ * available the object will enter one of the AVAHI_CLIENT_S_xxx
+ * states. Make sure to not create browsers or entry groups before the
+ * client object has entered one of those states. As usual you will be
+ * informed about state changes with the callback function supplied to
+ * avahi_client_new(). If the client is forced to disconnect from the
+ * server it will enter AVAHI_CLIENT_FAILURE state with
+ * avahi_client_errno() == AVAHI_ERR_DISCONNECTED. Free the
+ * AvahiClient object in that case (and all its associated objects
+ * such as entry groups and browser objects prior to that) and
+ * reconnect to the server anew - again with passing
+ * AVAHI_CLIENT_NO_FAIL to avahi_client_new().
+ *
+ * We encourage implementing this in all software where service
+ * discovery is not an integral part of application. e.g. use it in
+ * all kinds of background daemons, but not necessarily in software
+ * like iChat compatible IM software.
+ *
+ * For now AVAHI_CLIENT_NO_FAIL cannot deal with D-Bus daemon restarts.
+ *
+ * \section domains How to Deal Properly with Browsing Domains
+ *
+ * Due to the introduction of wide-area DNS-SD the correct handling of
+ * domains becomes more important for Avahi enabled applications. All
+ * applications that offer the user a list of services discovered with
+ * Avahi should offer some kind of editable drop down box where the
+ * user can either enter his own domain or select one of those offered
+ * by AvahiDomainBrowser. The default domain to browse should be the
+ * one returned by avahi_client_get_domain_name(). The list of domains
+ * returned by AvahiDomainBrowser is assembled by the browsing domains
+ * configured in the daemon's configuration file, the domains
+ * announced inside the default domain, the domains set with the
+ * environment variable $AVAHI_BROWSE_DOMAINS (colon-seperated) on the
+ * client side and the domains set in the XDG configuration file
+ * ~/.config/avahi/browse-domains on the client side (seperated by
+ * newlines). File managers offering some kind of "Network
+ * Neighborhood" folder should show the entries of the default domain
+ * right inside that and offer subfolders for the browsing domains
+ * returned by AvahiDomainBrowser.
+ */
+
+AVAHI_C_DECL_BEGIN
+
+/** @{ \name States */
+
+/** States of a server object */
+typedef enum {
+ AVAHI_SERVER_INVALID, /**< Invalid state (initial) */
+ AVAHI_SERVER_REGISTERING, /**< Host RRs are being registered */
+ AVAHI_SERVER_RUNNING, /**< All host RRs have been established */
+ AVAHI_SERVER_COLLISION, /**< There is a collision with a host RR. All host RRs have been withdrawn, the user should set a new host name via avahi_server_set_host_name() */
+ AVAHI_SERVER_FAILURE /**< Some fatal failure happened, the server is unable to proceed */
+} AvahiServerState;
+
+/** States of an entry group object */
+typedef enum {
+ AVAHI_ENTRY_GROUP_UNCOMMITED, /**< The group has not yet been commited, the user must still call avahi_entry_group_commit() */
+ AVAHI_ENTRY_GROUP_REGISTERING, /**< The entries of the group are currently being registered */
+ AVAHI_ENTRY_GROUP_ESTABLISHED, /**< The entries have successfully been established */
+ AVAHI_ENTRY_GROUP_COLLISION, /**< A name collision for one of the entries in the group has been detected, the entries have been withdrawn */
+ AVAHI_ENTRY_GROUP_FAILURE /**< Some kind of failure happened, the entries have been withdrawn */
+} AvahiEntryGroupState;
+
+/** @} */
+
+/** @{ \name Flags */
+
+/** Some flags for publishing functions */
+typedef enum {
+ AVAHI_PUBLISH_UNIQUE = 1, /**< For raw records: The RRset is intended to be unique */
+ AVAHI_PUBLISH_NO_PROBE = 2, /**< For raw records: Though the RRset is intended to be unique no probes shall be sent */
+ AVAHI_PUBLISH_NO_ANNOUNCE = 4, /**< For raw records: Do not announce this RR to other hosts */
+ AVAHI_PUBLISH_ALLOW_MULTIPLE = 8, /**< For raw records: Allow multiple local records of this type, even if they are intended to be unique */
+/** \cond fulldocs */
+ AVAHI_PUBLISH_NO_REVERSE = 16, /**< For address records: don't create a reverse (PTR) entry */
+ AVAHI_PUBLISH_NO_COOKIE = 32, /**< For service records: do not implicitly add the local service cookie to TXT data */
+/** \endcond */
+ AVAHI_PUBLISH_UPDATE = 64, /**< Update existing records instead of adding new ones */
+/** \cond fulldocs */
+ AVAHI_PUBLISH_USE_WIDE_AREA = 128, /**< Register the record using wide area DNS (i.e. unicast DNS update) */
+ AVAHI_PUBLISH_USE_MULTICAST = 256 /**< Register the record using multicast DNS */
+/** \endcond */
+} AvahiPublishFlags;
+
+/** Some flags for lookup functions */
+typedef enum {
+/** \cond fulldocs */
+ AVAHI_LOOKUP_USE_WIDE_AREA = 1, /**< Force lookup via wide area DNS */
+ AVAHI_LOOKUP_USE_MULTICAST = 2, /**< Force lookup via multicast DNS */
+/** \endcond */
+ AVAHI_LOOKUP_NO_TXT = 4, /**< When doing service resolving, don't lookup TXT record */
+ AVAHI_LOOKUP_NO_ADDRESS = 8 /**< When doing service resolving, don't lookup A/AAAA record */
+} AvahiLookupFlags;
+
+/** Some flags for lookup callback functions */
+typedef enum {
+ AVAHI_LOOKUP_RESULT_CACHED = 1, /**< This response originates from the cache */
+ AVAHI_LOOKUP_RESULT_WIDE_AREA = 2, /**< This response originates from wide area DNS */
+ AVAHI_LOOKUP_RESULT_MULTICAST = 4, /**< This response originates from multicast DNS */
+ AVAHI_LOOKUP_RESULT_LOCAL = 8, /**< This record/service resides on and was announced by the local host. Only available in service and record browsers and only on AVAHI_BROWSER_NEW. */
+ AVAHI_LOOKUP_RESULT_OUR_OWN = 16, /**< This service belongs to the same local client as the browser object. Only available in avahi-client, and only for service browsers and only on AVAHI_BROWSER_NEW. */
+ AVAHI_LOOKUP_RESULT_STATIC = 32 /**< The returned data has been defined statically by some configuration option */
+} AvahiLookupResultFlags;
+
+/** @} */
+
+/** @{ \name Events */
+
+/** Type of callback event when browsing */
+typedef enum {
+ AVAHI_BROWSER_NEW, /**< The object is new on the network */
+ AVAHI_BROWSER_REMOVE, /**< The object has been removed from the network */
+ AVAHI_BROWSER_CACHE_EXHAUSTED, /**< One-time event, to notify the user that all entries from the caches have been sent */
+ AVAHI_BROWSER_ALL_FOR_NOW, /**< One-time event, to notify the user that more records will probably not show up in the near future, i.e. all cache entries have been read and all static servers been queried */
+ AVAHI_BROWSER_FAILURE /**< Browsing failed due to some reason which can be retrieved using avahi_server_errno()/avahi_client_errno() */
+} AvahiBrowserEvent;
+
+/** Type of callback event when resolving */
+typedef enum {
+ AVAHI_RESOLVER_FOUND, /**< RR found, resolving successful */
+ AVAHI_RESOLVER_FAILURE /**< Resolving failed due to some reason which can be retrieved using avahi_server_errno()/avahi_client_errno() */
+} AvahiResolverEvent;
+
+/** @} */
+
+/** @{ \name Other definitions */
+
+/** The type of domain to browse for */
+typedef enum {
+ AVAHI_DOMAIN_BROWSER_BROWSE, /**< Browse for a list of available browsing domains */
+ AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT, /**< Browse for the default browsing domain */
+ AVAHI_DOMAIN_BROWSER_REGISTER, /**< Browse for a list of available registering domains */
+ AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT, /**< Browse for the default registering domain */
+ AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY, /**< Legacy browse domain - see DNS-SD spec for more information */
+ AVAHI_DOMAIN_BROWSER_MAX
+} AvahiDomainBrowserType;
+
+/** @} */
+
+/** \cond fulldocs */
+/** For every service a special TXT item is implicitly added, which
+ * contains a random cookie which is private to the local daemon. This
+ * can be used by clients to determine if two services on two
+ * different subnets are effectively the same. */
+#define AVAHI_SERVICE_COOKIE "org.freedesktop.Avahi.cookie"
+
+/** In invalid cookie as special value */
+#define AVAHI_SERVICE_COOKIE_INVALID (0)
+/** \endcond fulldocs */
+
+/** @{ \name DNS RR definitions */
+
+/** DNS record types, see RFC 1035 */
+enum {
+ AVAHI_DNS_TYPE_A = 0x01,
+ AVAHI_DNS_TYPE_NS = 0x02,
+ AVAHI_DNS_TYPE_CNAME = 0x05,
+ AVAHI_DNS_TYPE_SOA = 0x06,
+ AVAHI_DNS_TYPE_PTR = 0x0C,
+ AVAHI_DNS_TYPE_HINFO = 0x0D,
+ AVAHI_DNS_TYPE_MX = 0x0F,
+ AVAHI_DNS_TYPE_TXT = 0x10,
+ AVAHI_DNS_TYPE_AAAA = 0x1C,
+ AVAHI_DNS_TYPE_SRV = 0x21
+};
+
+/** DNS record classes, see RFC 1035 */
+enum {
+ AVAHI_DNS_CLASS_IN = 0x01 /**< Probably the only class we will ever use */
+};
+
+/** @} */
+
+/** The default TTL for RRs which contain a host name of some kind. */
+#define AVAHI_DEFAULT_TTL_HOST_NAME (120)
+
+/** The default TTL for all other records. */
+#define AVAHI_DEFAULT_TTL (75*60)
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/domain-test.c b/avahi-common/domain-test.c
new file mode 100644
index 0000000..cf763ec
--- /dev/null
+++ b/avahi-common/domain-test.c
@@ -0,0 +1,123 @@
+/***
+ 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 <string.h>
+#include <assert.h>
+
+#include "domain.h"
+#include "malloc.h"
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+ char *s;
+ char t[256], r[256];
+ const char *p;
+ size_t size;
+ char name[64], type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
+
+ printf("%s\n", s = avahi_normalize_name_strdup("foo.foo\\046."));
+ avahi_free(s);
+
+ printf("%s\n", s = avahi_normalize_name_strdup("foo.foo\\.foo."));
+ avahi_free(s);
+
+
+ printf("%s\n", s = avahi_normalize_name_strdup("fo\\\\o\\..f oo."));
+ avahi_free(s);
+
+ printf("%i\n", avahi_domain_equal("\\065aa bbb\\.\\046cc.cc\\\\.dee.fff.", "Aaa BBB\\.\\.cc.cc\\\\.dee.fff"));
+ printf("%i\n", avahi_domain_equal("A", "a"));
+
+ printf("%i\n", avahi_domain_equal("a", "aaa"));
+
+ printf("%u = %u\n", avahi_domain_hash("ccc\\065aa.aa\\.b\\\\."), avahi_domain_hash("cccAaa.aa\\.b\\\\"));
+
+
+ avahi_service_name_join(t, sizeof(t), "foo.foo.foo \\.", "_http._tcp", "test.local");
+ printf("<%s>\n", t);
+
+ avahi_service_name_split(t, name, sizeof(name), type, sizeof(type), domain, sizeof(domain));
+ printf("name: <%s>; type: <%s>; domain <%s>\n", name, type, domain);
+
+ avahi_service_name_join(t, sizeof(t), NULL, "_http._tcp", "one.two\\. .local");
+ printf("<%s>\n", t);
+
+ avahi_service_name_split(t, NULL, 0, type, sizeof(type), domain, sizeof(domain));
+ printf("name: <>; type: <%s>; domain <%s>\n", type, domain);
+
+
+ p = "--:---\\\\\\123\\065_äöü\\064\\.\\\\sjöödfhh.sdfjhskjdf";
+ printf("unescaped: <%s>, rest: %s\n", avahi_unescape_label(&p, t, sizeof(t)), p);
+
+ size = sizeof(r);
+ s = r;
+
+ printf("escaped: <%s>\n", avahi_escape_label(t, strlen(t), &s, &size));
+
+ p = r;
+ printf("unescaped: <%s>\n", avahi_unescape_label(&p, t, sizeof(t)));
+
+ assert(avahi_is_valid_service_type_generic("_foo._bar._waldo"));
+ assert(!avahi_is_valid_service_type_strict("_foo._bar._waldo"));
+ assert(!avahi_is_valid_service_subtype("_foo._bar._waldo"));
+
+ assert(avahi_is_valid_service_type_generic("_foo._tcp"));
+ assert(avahi_is_valid_service_type_strict("_foo._tcp"));
+ assert(!avahi_is_valid_service_subtype("_foo._tcp"));
+
+ assert(!avahi_is_valid_service_type_generic("_foo._bar.waldo"));
+ assert(!avahi_is_valid_service_type_strict("_foo._bar.waldo"));
+ assert(!avahi_is_valid_service_subtype("_foo._bar.waldo"));
+
+ assert(!avahi_is_valid_service_type_generic(""));
+ assert(!avahi_is_valid_service_type_strict(""));
+ assert(!avahi_is_valid_service_subtype(""));
+
+ assert(avahi_is_valid_service_type_generic("_foo._sub._bar._tcp"));
+ assert(!avahi_is_valid_service_type_strict("_foo._sub._bar._tcp"));
+ assert(avahi_is_valid_service_subtype("_foo._sub._bar._tcp"));
+
+ printf("%s\n", avahi_get_type_from_subtype("_foo._sub._bar._tcp"));
+
+ assert(!avahi_is_valid_host_name("sf.ooo."));
+ assert(avahi_is_valid_host_name("sfooo."));
+ assert(avahi_is_valid_host_name("sfooo"));
+
+ assert(avahi_is_valid_domain_name("."));
+ assert(avahi_is_valid_domain_name(""));
+
+ assert(avahi_normalize_name(".", t, sizeof(t)));
+ assert(avahi_normalize_name("", t, sizeof(t)));
+
+ assert(!avahi_is_valid_fqdn("."));
+ assert(!avahi_is_valid_fqdn(""));
+ assert(!avahi_is_valid_fqdn("foo"));
+ assert(avahi_is_valid_fqdn("foo.bar"));
+ assert(avahi_is_valid_fqdn("foo.bar."));
+ assert(avahi_is_valid_fqdn("gnurz.foo.bar."));
+ assert(!avahi_is_valid_fqdn("192.168.50.1"));
+ assert(!avahi_is_valid_fqdn("::1"));
+ assert(!avahi_is_valid_fqdn(".192.168.50.1."));
+
+ return 0;
+}
diff --git a/avahi-common/domain.c b/avahi-common/domain.c
new file mode 100644
index 0000000..3b1ab68
--- /dev/null
+++ b/avahi-common/domain.c
@@ -0,0 +1,609 @@
+/***
+ 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 <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "domain.h"
+#include "malloc.h"
+#include "error.h"
+#include "address.h"
+#include "utf8.h"
+
+/* Read the first label from string *name, unescape "\" and write it to dest */
+char *avahi_unescape_label(const char **name, char *dest, size_t size) {
+ unsigned i = 0;
+ char *d;
+
+ assert(dest);
+ assert(size > 0);
+ assert(name);
+
+ d = dest;
+
+ for (;;) {
+ if (i >= size)
+ return NULL;
+
+ if (**name == '.') {
+ (*name)++;
+ break;
+ }
+
+ if (**name == 0)
+ break;
+
+ if (**name == '\\') {
+ /* Escaped character */
+
+ (*name) ++;
+
+ if (**name == 0)
+ /* Ending NUL */
+ return NULL;
+
+ else if (**name == '\\' || **name == '.') {
+ /* Escaped backslash or dot */
+ *(d++) = *((*name) ++);
+ i++;
+ } else if (isdigit(**name)) {
+ int n;
+
+ /* Escaped literal ASCII character */
+
+ if (!isdigit(*(*name+1)) || !isdigit(*(*name+2)))
+ return NULL;
+
+ n = ((uint8_t) (**name - '0') * 100) + ((uint8_t) (*(*name+1) - '0') * 10) + ((uint8_t) (*(*name +2) - '0'));
+
+ if (n > 255 || n == 0)
+ return NULL;
+
+ *(d++) = (char) n;
+ i++;
+
+ (*name) += 3;
+ } else
+ return NULL;
+
+ } else {
+
+ /* Normal character */
+
+ *(d++) = *((*name) ++);
+ i++;
+ }
+ }
+
+ assert(i < size);
+
+ *d = 0;
+
+ if (!avahi_utf8_valid(dest))
+ return NULL;
+
+ return dest;
+}
+
+/* Escape "\" and ".", append \0 */
+char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
+ char *r;
+
+ assert(src);
+ assert(ret_name);
+ assert(*ret_name);
+ assert(ret_size);
+ assert(*ret_size > 0);
+
+ r = *ret_name;
+
+ while (src_length > 0) {
+ if (*src == '.' || *src == '\\') {
+
+ /* Dot or backslash */
+
+ if (*ret_size < 3)
+ return NULL;
+
+ *((*ret_name) ++) = '\\';
+ *((*ret_name) ++) = *src;
+ (*ret_size) -= 2;
+
+ } else if (
+ *src == '_' ||
+ *src == '-' ||
+ (*src >= '0' && *src <= '9') ||
+ (*src >= 'a' && *src <= 'z') ||
+ (*src >= 'A' && *src <= 'Z')) {
+
+ /* Proper character */
+
+ if (*ret_size < 2)
+ return NULL;
+
+ *((*ret_name)++) = *src;
+ (*ret_size) --;
+
+ } else {
+
+ /* Everything else */
+
+ if (*ret_size < 5)
+ return NULL;
+
+ *((*ret_name) ++) = '\\';
+ *((*ret_name) ++) = '0' + (char) ((uint8_t) *src / 100);
+ *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
+ *((*ret_name) ++) = '0' + (char) ((uint8_t) *src % 10);
+
+ (*ret_size) -= 4;
+ }
+
+ src_length --;
+ src++;
+ }
+
+ **ret_name = 0;
+
+ return r;
+}
+
+char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
+ int empty = 1;
+ char *r;
+
+ assert(s);
+ assert(ret_s);
+ assert(size > 0);
+
+ r = ret_s;
+ *ret_s = 0;
+
+ while (*s) {
+ char label[AVAHI_LABEL_MAX];
+
+ if (!(avahi_unescape_label(&s, label, sizeof(label))))
+ return NULL;
+
+ if (label[0] == 0) {
+
+ if (*s == 0 && empty)
+ return ret_s;
+
+ return NULL;
+ }
+
+ if (!empty) {
+ if (size < 1)
+ return NULL;
+
+ *(r++) = '.';
+ size--;
+
+ } else
+ empty = 0;
+
+ avahi_escape_label(label, strlen(label), &r, &size);
+ }
+
+ return ret_s;
+}
+
+char *avahi_normalize_name_strdup(const char *s) {
+ char t[AVAHI_DOMAIN_NAME_MAX];
+ assert(s);
+
+ if (!(avahi_normalize_name(s, t, sizeof(t))))
+ return NULL;
+
+ return avahi_strdup(t);
+}
+
+int avahi_domain_equal(const char *a, const char *b) {
+ assert(a);
+ assert(b);
+
+ if (a == b)
+ return 1;
+
+ for (;;) {
+ char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
+
+ r = avahi_unescape_label(&a, ca, sizeof(ca));
+ assert(r);
+ r = avahi_unescape_label(&b, cb, sizeof(cb));
+ assert(r);
+
+ if (strcasecmp(ca, cb))
+ return 0;
+
+ if (!*a && !*b)
+ return 1;
+ }
+
+ return 1;
+}
+
+int avahi_is_valid_service_type_generic(const char *t) {
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
+ return 0;
+
+ do {
+ char label[AVAHI_LABEL_MAX];
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return 0;
+
+ if (strlen(label) <= 2 || label[0] != '_')
+ return 0;
+
+ } while (*t);
+
+ return 1;
+}
+
+int avahi_is_valid_service_type_strict(const char *t) {
+ char label[AVAHI_LABEL_MAX];
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
+ return 0;
+
+ /* Application name */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return 0;
+
+ if (strlen(label) <= 2 || label[0] != '_')
+ return 0;
+
+ if (!*t)
+ return 0;
+
+ /* _tcp or _udp boilerplate */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return 0;
+
+ if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
+ return 0;
+
+ if (*t)
+ return 0;
+
+ return 1;
+}
+
+const char *avahi_get_type_from_subtype(const char *t) {
+ char label[AVAHI_LABEL_MAX];
+ const char *ret;
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
+ return NULL;
+
+ /* Subtype name */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return NULL;
+
+ if (strlen(label) <= 2 || label[0] != '_')
+ return NULL;
+
+ if (!*t)
+ return NULL;
+
+ /* String "_sub" */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return NULL;
+
+ if (strcasecmp(label, "_sub"))
+ return NULL;
+
+ if (!*t)
+ return NULL;
+
+ ret = t;
+
+ /* Application name */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return NULL;
+
+ if (strlen(label) <= 2 || label[0] != '_')
+ return NULL;
+
+ if (!*t)
+ return NULL;
+
+ /* _tcp or _udp boilerplate */
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return NULL;
+
+ if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
+ return NULL;
+
+ if (*t)
+ return NULL;
+
+ return ret;
+}
+
+int avahi_is_valid_service_subtype(const char *t) {
+ assert(t);
+
+ return !!avahi_get_type_from_subtype(t);
+}
+
+int avahi_is_valid_domain_name(const char *t) {
+ int is_first = 1;
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
+ return 0;
+
+ do {
+ char label[AVAHI_LABEL_MAX];
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return 0;
+
+ /* Explicitly allow the root domain name */
+ if (is_first && label[0] == 0 && *t == 0)
+ return 1;
+
+ is_first = 0;
+
+ if (label[0] == 0)
+ return 0;
+
+ } while (*t);
+
+ return 1;
+}
+
+int avahi_is_valid_service_name(const char *t) {
+ assert(t);
+
+ if (strlen(t) >= AVAHI_LABEL_MAX || !*t)
+ return 0;
+
+ return 1;
+}
+
+int avahi_is_valid_host_name(const char *t) {
+ char label[AVAHI_LABEL_MAX];
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
+ return 0;
+
+ if (!(avahi_unescape_label(&t, label, sizeof(label))))
+ return 0;
+
+ if (strlen(label) < 1)
+ return 0;
+
+ if (*t)
+ return 0;
+
+ return 1;
+}
+
+unsigned avahi_domain_hash(const char *s) {
+ unsigned hash = 0;
+
+ while (*s) {
+ char c[AVAHI_LABEL_MAX], *p, *r;
+
+ r = avahi_unescape_label(&s, c, sizeof(c));
+ assert(r);
+
+ for (p = c; *p; p++)
+ hash = 31 * hash + tolower(*p);
+ }
+
+ return hash;
+}
+
+int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
+ char escaped_name[AVAHI_LABEL_MAX*4];
+ char normalized_type[AVAHI_DOMAIN_NAME_MAX];
+ char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
+
+ assert(p);
+
+ /* Validity checks */
+
+ if ((name && !avahi_is_valid_service_name(name)))
+ return AVAHI_ERR_INVALID_SERVICE_NAME;
+
+ if (!avahi_is_valid_service_type_generic(type))
+ return AVAHI_ERR_INVALID_SERVICE_TYPE;
+
+ if (!avahi_is_valid_domain_name(domain))
+ return AVAHI_ERR_INVALID_DOMAIN_NAME;
+
+ /* Preparation */
+
+ if (name) {
+ size_t l = sizeof(escaped_name);
+ char *e = escaped_name, *r;
+ r = avahi_escape_label(name, strlen(name), &e, &l);
+ assert(r);
+ }
+
+ if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
+ return AVAHI_ERR_INVALID_SERVICE_TYPE;
+
+ if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
+ return AVAHI_ERR_INVALID_DOMAIN_NAME;
+
+ /* Concatenation */
+
+ snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
+
+ return AVAHI_OK;
+}
+
+#ifndef HAVE_STRLCPY
+
+static size_t strlcpy(char *dest, const char *src, size_t n) {
+ assert(dest);
+ assert(src);
+
+ if (n > 0) {
+ strncpy(dest, src, n-1);
+ dest[n-1] = 0;
+ }
+
+ return strlen(src);
+}
+
+#endif
+
+int avahi_service_name_split(const char *p, char *name, size_t name_size, char *type, size_t type_size, char *domain, size_t domain_size) {
+ enum {
+ NAME,
+ TYPE,
+ DOMAIN
+ } state;
+ int type_empty = 1, domain_empty = 1;
+
+ assert(p);
+ assert(type);
+ assert(type_size > 0);
+ assert(domain);
+ assert(domain_size > 0);
+
+ if (name) {
+ assert(name_size > 0);
+ *name = 0;
+ state = NAME;
+ } else
+ state = TYPE;
+
+ *type = *domain = 0;
+
+ while (*p) {
+ char buf[64];
+
+ if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
+ return -1;
+
+ switch (state) {
+ case NAME:
+ strlcpy(name, buf, name_size);
+ state = TYPE;
+ break;
+
+ case TYPE:
+
+ if (buf[0] == '_') {
+
+ if (!type_empty) {
+ if (!type_size)
+ return AVAHI_ERR_NO_MEMORY;
+
+ *(type++) = '.';
+ type_size --;
+
+ } else
+ type_empty = 0;
+
+ if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
+ return AVAHI_ERR_NO_MEMORY;
+
+ break;
+ }
+
+ state = DOMAIN;
+ /* fall through */
+
+ case DOMAIN:
+
+ if (!domain_empty) {
+ if (!domain_size)
+ return AVAHI_ERR_NO_MEMORY;
+
+ *(domain++) = '.';
+ domain_size --;
+ } else
+ domain_empty = 0;
+
+ if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
+ return AVAHI_ERR_NO_MEMORY;
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+int avahi_is_valid_fqdn(const char *t) {
+ char label[AVAHI_LABEL_MAX];
+ char normalized[AVAHI_DOMAIN_NAME_MAX];
+ const char *k = t;
+ AvahiAddress a;
+ assert(t);
+
+ if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
+ return 0;
+
+ if (!avahi_is_valid_domain_name(t))
+ return 0;
+
+ /* Check if there are at least two labels*/
+ if (!(avahi_unescape_label(&k, label, sizeof(label))))
+ return 0;
+
+ if (label[0] == 0 || !k)
+ return 0;
+
+ if (!(avahi_unescape_label(&k, label, sizeof(label))))
+ return 0;
+
+ if (label[0] == 0 || !k)
+ return 0;
+
+ /* Make sure that the name is not an IP address */
+ if (!(avahi_normalize_name(t, normalized, sizeof(normalized))))
+ return 0;
+
+ if (avahi_address_parse(normalized, AVAHI_PROTO_UNSPEC, &a))
+ return 0;
+
+ return 1;
+}
diff --git a/avahi-common/domain.h b/avahi-common/domain.h
new file mode 100644
index 0000000..a0dcd0f
--- /dev/null
+++ b/avahi-common/domain.h
@@ -0,0 +1,129 @@
+#ifndef foodomainhfoo
+#define foodomainhfoo
+
+/***
+ 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.
+***/
+
+/** \file domain.h Domain name handling functions */
+
+#include <inttypes.h>
+#include <sys/types.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** The maximum length of a a fully escaped domain name C string. This
+ * is calculated like this: RFC1034 mandates maximum length of FQDNs
+ * is 255. The maximum label length is 63. To minimize the number of
+ * (non-escaped) dots, we comprise our maximum-length domain name of
+ * four labels á 63 characters plus three inner dots. Escaping the
+ * four labels quadruples their length at maximum. An escaped domain
+ * name has the therefore the maximum length of 63*4*4+3=1011. A
+ * trailing NUL and perhaps two unnecessary dots leading and trailing
+ * the string brings us to 1014. */
+#define AVAHI_DOMAIN_NAME_MAX 1014
+
+/** Maximum size of an unescaped label */
+#define AVAHI_LABEL_MAX 64
+
+/** @{ \name Normalization */
+
+/** Normalize a domain name into canonical form. This drops trailing
+ * dots and removes useless backslash escapes. */
+char *avahi_normalize_name(const char *s, char *ret_s, size_t size);
+
+/** Normalize a domain name into canonical form. This drops trailing
+ * dots and removes useless backslash escapes. avahi_free() the
+ * result! */
+char *avahi_normalize_name_strdup(const char *s);
+
+/** @} */
+
+/** @{ \name Comparison */
+
+/** Return 1 when the specified domain names are equal, 0 otherwise */
+int avahi_domain_equal(const char *a, const char *b);
+
+/** Return some kind of hash value for the domain, useful for using domains as hash table keys. */
+unsigned avahi_domain_hash(const char *name);
+
+/** @} */
+
+/** @{ \name Escaping */
+
+/** 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*/
+char *avahi_unescape_label(const char **name, char *dest, size_t size);
+
+/** Escape the domain name in *src and write it to *ret_name */
+char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size);
+
+/** @} */
+
+/** @{ \name Validity Checks */
+
+/** Return 1 when the specified string contains a valid generic DNS-SD
+ * service type (i.e. a series of words starting with "_"), 0
+ * otherwise */
+int avahi_is_valid_service_type_generic(const char *t);
+
+/** Return 1 when the specified string contains a valid strict DNS-SD
+ * service type (i.e. consisting of only two words, the latter being
+ * either _udp or _tcp), 0 otherwise */
+int avahi_is_valid_service_type_strict(const char *t);
+
+/** Return 1 when the specified string contains a valid DNS-SD service
+ * subtype, 0 otherwise */
+int avahi_is_valid_service_subtype(const char *t);
+
+/** Return 1 when the specified string contains a valid domain name, 0 otherwise */
+int avahi_is_valid_domain_name(const char *t);
+
+/** Return 1 when the specified string contains a valid DNS-SD service name, 0 otherwise */
+int avahi_is_valid_service_name(const char *t);
+
+/** Return 1 when the specified string contains a valid non-FQDN host name (i.e. without dots), 0 otherwise */
+int avahi_is_valid_host_name(const char *t);
+
+/** Return 1 when the specified string contains a valid FQDN host name (i.e. with more than one label and non-numerical), 0 otherwise. \since 0.6.9 */
+int avahi_is_valid_fqdn(const char *t);
+
+/** @} */
+
+/** @{ \name DNS-SD service name handling */
+
+/** Construct a valid complete DNS-SD service name from a name, a type and a domain */
+int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain);
+
+/** Split a full service name into name, type and domain */
+int avahi_service_name_split(const char *p, char *name, size_t name_size, char *type, size_t type_size, char *domain, size_t domain_size);
+
+/** @} */
+
+/** @{ \name DNS-SD Subtype handling */
+
+/** Return a pointer to the type section of a subtype i.e. _foo._sub._bar._tcp => _bar._tcp */
+const char *avahi_get_type_from_subtype(const char *t);
+
+/** @} */
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/error.c b/avahi-common/error.c
new file mode 100644
index 0000000..63c5033
--- /dev/null
+++ b/avahi-common/error.c
@@ -0,0 +1,97 @@
+/***
+ 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 "error.h"
+#include "i18n.h"
+
+const char *avahi_strerror(int error) {
+
+ const char * const msg[- AVAHI_ERR_MAX] = {
+ N_("OK"),
+ N_("Operation failed"),
+ N_("Bad state"),
+ N_("Invalid host name"),
+ N_("Invalid domain name"),
+ N_("No suitable network protocol available"),
+ N_("Invalid DNS TTL"),
+ N_("Resource record key is pattern"),
+ N_("Local name collision"),
+ N_("Invalid record"),
+
+ N_("Invalid service name"),
+ N_("Invalid service type"),
+ N_("Invalid port number"),
+ N_("Invalid record key"),
+ N_("Invalid address"),
+ N_("Timeout reached"),
+ N_("Too many clients"),
+ N_("Too many objects"),
+ N_("Too many entries"),
+ N_("OS Error"),
+
+ N_("Access denied"),
+ N_("Invalid operation"),
+ N_("An unexpected D-Bus error occurred"),
+ N_("Daemon connection failed"),
+ N_("Memory exhausted"),
+ N_("The object passed in was not valid"),
+ N_("Daemon not running"),
+ N_("Invalid interface index"),
+ N_("Invalid protocol specification"),
+ N_("Invalid flags"),
+
+ N_("Not found"),
+ N_("Invalid configuration"),
+ N_("Version mismatch"),
+ N_("Invalid service subtype"),
+ N_("Invalid packet"),
+ N_("Invalid DNS return code"),
+ N_("DNS failure: FORMERR"),
+ N_("DNS failure: SERVFAIL"),
+ N_("DNS failure: NXDOMAIN"),
+ N_("DNS failure: NOTIMP"),
+
+ N_("DNS failure: REFUSED"),
+ N_("DNS failure: YXDOMAIN"),
+ N_("DNS failure: YXRRSET"),
+ N_("DNS failure: NXRRSET"),
+ N_("DNS failure: NOTAUTH"),
+ N_("DNS failure: NOTZONE"),
+ N_("Invalid RDATA"),
+ N_("Invalid DNS type"),
+ N_("Invalid DNS class"),
+ N_("Not supported"),
+
+ N_("Not permitted"),
+ N_("Invalid argument"),
+ N_("Is empty"),
+ N_("The requested operation is invalid because redundant")
+ };
+
+ avahi_init_i18n();
+
+ if (-error < 0 || -error >= -AVAHI_ERR_MAX)
+ return _("Invalid Error Code");
+
+ return _(msg[-error]);
+}
diff --git a/avahi-common/error.h b/avahi-common/error.h
new file mode 100644
index 0000000..94511c9
--- /dev/null
+++ b/avahi-common/error.h
@@ -0,0 +1,107 @@
+#ifndef fooerrorhfoo
+#define fooerrorhfoo
+
+/***
+ 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.
+***/
+
+/** \file error.h Error codes and auxiliary functions */
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** Error codes used by avahi */
+enum {
+ AVAHI_OK = 0, /**< OK */
+ AVAHI_ERR_FAILURE = -1, /**< Generic error code */
+ AVAHI_ERR_BAD_STATE = -2, /**< Object was in a bad state */
+ AVAHI_ERR_INVALID_HOST_NAME = -3, /**< Invalid host name */
+ AVAHI_ERR_INVALID_DOMAIN_NAME = -4, /**< Invalid domain name */
+ AVAHI_ERR_NO_NETWORK = -5, /**< No suitable network protocol available */
+ AVAHI_ERR_INVALID_TTL = -6, /**< Invalid DNS TTL */
+ AVAHI_ERR_IS_PATTERN = -7, /**< RR key is pattern */
+ AVAHI_ERR_COLLISION = -8, /**< Name collision */
+ AVAHI_ERR_INVALID_RECORD = -9, /**< Invalid RR */
+
+ AVAHI_ERR_INVALID_SERVICE_NAME = -10, /**< Invalid service name */
+ AVAHI_ERR_INVALID_SERVICE_TYPE = -11, /**< Invalid service type */
+ AVAHI_ERR_INVALID_PORT = -12, /**< Invalid port number */
+ AVAHI_ERR_INVALID_KEY = -13, /**< Invalid key */
+ AVAHI_ERR_INVALID_ADDRESS = -14, /**< Invalid address */
+ AVAHI_ERR_TIMEOUT = -15, /**< Timeout reached */
+ AVAHI_ERR_TOO_MANY_CLIENTS = -16, /**< Too many clients */
+ AVAHI_ERR_TOO_MANY_OBJECTS = -17, /**< Too many objects */
+ AVAHI_ERR_TOO_MANY_ENTRIES = -18, /**< Too many entries */
+ AVAHI_ERR_OS = -19, /**< OS error */
+
+ AVAHI_ERR_ACCESS_DENIED = -20, /**< Access denied */
+ AVAHI_ERR_INVALID_OPERATION = -21, /**< Invalid operation */
+ AVAHI_ERR_DBUS_ERROR = -22, /**< An unexpected D-Bus error occurred */
+ AVAHI_ERR_DISCONNECTED = -23, /**< Daemon connection failed */
+ AVAHI_ERR_NO_MEMORY = -24, /**< Memory exhausted */
+ AVAHI_ERR_INVALID_OBJECT = -25, /**< The object passed to this function was invalid */
+ AVAHI_ERR_NO_DAEMON = -26, /**< Daemon not running */
+ AVAHI_ERR_INVALID_INTERFACE = -27, /**< Invalid interface */
+ AVAHI_ERR_INVALID_PROTOCOL = -28, /**< Invalid protocol */
+ AVAHI_ERR_INVALID_FLAGS = -29, /**< Invalid flags */
+
+ AVAHI_ERR_NOT_FOUND = -30, /**< Not found */
+ AVAHI_ERR_INVALID_CONFIG = -31, /**< Configuration error */
+ AVAHI_ERR_VERSION_MISMATCH = -32, /**< Verson mismatch */
+ AVAHI_ERR_INVALID_SERVICE_SUBTYPE = -33, /**< Invalid service subtype */
+ AVAHI_ERR_INVALID_PACKET = -34, /**< Invalid packet */
+ AVAHI_ERR_INVALID_DNS_ERROR = -35, /**< Invlaid DNS return code */
+ AVAHI_ERR_DNS_FORMERR = -36, /**< DNS Error: Form error */
+ AVAHI_ERR_DNS_SERVFAIL = -37, /**< DNS Error: Server Failure */
+ AVAHI_ERR_DNS_NXDOMAIN = -38, /**< DNS Error: No such domain */
+ AVAHI_ERR_DNS_NOTIMP = -39, /**< DNS Error: Not implemented */
+
+ AVAHI_ERR_DNS_REFUSED = -40, /**< DNS Error: Operation refused */
+ AVAHI_ERR_DNS_YXDOMAIN = -41,
+ AVAHI_ERR_DNS_YXRRSET = -42,
+ AVAHI_ERR_DNS_NXRRSET = -43,
+ AVAHI_ERR_DNS_NOTAUTH = -44, /**< DNS Error: Not authorized */
+ AVAHI_ERR_DNS_NOTZONE = -45,
+ AVAHI_ERR_INVALID_RDATA = -46, /**< Invalid RDATA */
+ AVAHI_ERR_INVALID_DNS_CLASS = -47, /**< Invalid DNS class */
+ AVAHI_ERR_INVALID_DNS_TYPE = -48, /**< Invalid DNS type */
+ AVAHI_ERR_NOT_SUPPORTED = -49, /**< Not supported */
+
+ AVAHI_ERR_NOT_PERMITTED = -50, /**< Operation not permitted */
+ AVAHI_ERR_INVALID_ARGUMENT = -51, /**< Invalid argument */
+ AVAHI_ERR_IS_EMPTY = -52, /**< Is empty */
+ AVAHI_ERR_NO_CHANGE = -53, /**< The requested operation is invalid because it is redundant */
+
+ /****
+ **** IF YOU ADD A NEW ERROR CODE HERE, PLEASE DON'T FORGET TO ADD
+ **** IT TO THE STRING ARRAY IN avahi_strerror() IN error.c AND
+ **** TO THE ARRAY IN dbus.c AND FINALLY TO dbus.h!
+ ****
+ **** Also remember to update the MAX value below.
+ ****/
+
+ AVAHI_ERR_MAX = -54
+};
+
+/** Return a human readable error string for the specified error code */
+const char *avahi_strerror(int error);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/gccmacro.h b/avahi-common/gccmacro.h
new file mode 100644
index 0000000..4c97111
--- /dev/null
+++ b/avahi-common/gccmacro.h
@@ -0,0 +1,74 @@
+#ifndef foogccmacrohfoo
+#define foogccmacrohfoo
+
+/***
+ 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.
+***/
+
+/** \file gccmacro.h Defines some macros for GCC extensions */
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+#if defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 3)
+#define AVAHI_GCC_ALLOC_SIZE(x) __attribute__ ((__alloc_size__(x)))
+#define AVAHI_GCC_ALLOC_SIZE2(x,y) __attribute__ ((__alloc_size__(x,y)))
+#else
+/** Macro for usage of GCC's alloc_size attribute */
+#define AVAHI_GCC_ALLOC_SIZE(x)
+#define AVAHI_GCC_ALLOC_SIZE2(x,y)
+#endif
+
+#if defined(__GNUC__) && (__GNUC__ >= 4)
+#define AVAHI_GCC_SENTINEL __attribute__ ((sentinel))
+#else
+/** Macro for usage of GCC's sentinel compilation warnings */
+#define AVAHI_GCC_SENTINEL
+#endif
+
+#ifdef __GNUC__
+#define AVAHI_GCC_PRINTF_ATTR(a,b) __attribute__ ((format (printf, a, b)))
+#else
+/** Macro for usage of GCC's printf compilation warnings */
+#define AVAHI_GCC_PRINTF_ATTR(a,b)
+#endif
+
+/** Same as AVAHI_GCC_PRINTF_ATTR but hard coded to arguments 1 and 2 */
+#define AVAHI_GCC_PRINTF_ATTR12 AVAHI_GCC_PRINTF_ATTR(1,2)
+
+/** Same as AVAHI_GCC_PRINTF_ATTR but hard coded to arguments 2 and 3 */
+#define AVAHI_GCC_PRINTF_ATTR23 AVAHI_GCC_PRINTF_ATTR(2,3)
+
+#ifdef __GNUC__
+#define AVAHI_GCC_NORETURN __attribute__((noreturn))
+#else
+/** Macro for no-return functions */
+#define AVAHI_GCC_NORETURN
+#endif
+
+#ifdef __GNUC__
+#define AVAHI_GCC_UNUSED __attribute__ ((unused))
+#else
+/** Macro for not used parameter */
+#define AVAHI_GCC_UNUSED
+#endif
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/i18n.c b/avahi-common/i18n.c
new file mode 100644
index 0000000..51c93e4
--- /dev/null
+++ b/avahi-common/i18n.c
@@ -0,0 +1,38 @@
+/***
+ 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 "i18n.h"
+
+void avahi_init_i18n(void) {
+
+ /* Not really thread safe, but this doesn't matter much since
+ * bindtextdomain is supposed to be idempotent anyway. */
+
+ static int done = 0;
+
+ if (!done) {
+ bindtextdomain(GETTEXT_PACKAGE, AVAHI_LOCALEDIR);
+ bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
+ done = 1;
+ }
+}
diff --git a/avahi-common/i18n.h b/avahi-common/i18n.h
new file mode 100644
index 0000000..2a613e5
--- /dev/null
+++ b/avahi-common/i18n.h
@@ -0,0 +1,53 @@
+#ifndef fooi18nhfoo
+#define fooi18nhfoo
+
+/***
+ 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.
+***/
+
+#if !defined(GETTEXT_PACKAGE)
+#error "Something is very wrong here, config.h needs to be included first"
+#endif
+
+#ifdef ENABLE_NLS
+
+#include <libintl.h>
+
+#define _(String) dgettext(GETTEXT_PACKAGE, String)
+#ifdef gettext_noop
+#define N_(String) gettext_noop(String)
+#else
+#define N_(String) (String)
+#endif
+
+#else /* NLS is disabled */
+
+#define _(String) (String)
+#define N_(String) (String)
+#define textdomain(String) (String)
+#define gettext(String) (String)
+#define dgettext(Domain,String) (String)
+#define dcgettext(Domain,String,Type) (String)
+#define bindtextdomain(Domain,Directory) (Domain)
+#define bind_textdomain_codeset(Domain,Codeset) (Codeset)
+
+#endif /* ENABLE_NLS */
+
+void avahi_init_i18n(void);
+
+#endif
diff --git a/avahi-common/llist.h b/avahi-common/llist.h
new file mode 100644
index 0000000..e37056d
--- /dev/null
+++ b/avahi-common/llist.h
@@ -0,0 +1,75 @@
+#ifndef foollistfoo
+#define foollistfoo
+
+/***
+ 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.
+***/
+
+/** \file llist.h A simple macro based linked list implementation */
+
+#include <assert.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** 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
+
+/** The pointers in the linked list's items. Use this in the item structure */
+#define AVAHI_LLIST_FIELDS(t,name) t *name##_next, *name##_prev
+
+/** Initialize the list's head */
+#define AVAHI_LLIST_HEAD_INIT(t,head) do { (head) = NULL; } while(0)
+
+/** Initialize a list item */
+#define AVAHI_LLIST_INIT(t,name,item) do { \
+ t *_item = (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); \
+ assert(_item); \
+ if ((_item->name##_next = *_head)) \
+ _item->name##_next->name##_prev = _item; \
+ _item->name##_prev = NULL; \
+ *_head = _item; \
+ } while (0)
+
+/** Remove an item from the list */
+#define AVAHI_LLIST_REMOVE(t,name,head,item) do { \
+ t **_head = &(head), *_item = (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 {\
+ assert(*_head == _item); \
+ *_head = _item->name##_next; \
+ } \
+ _item->name##_next = _item->name##_prev = NULL; \
+ } while(0)
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c
new file mode 100644
index 0000000..23b13a9
--- /dev/null
+++ b/avahi-common/malloc.c
@@ -0,0 +1,257 @@
+/***
+ 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 <unistd.h>
+
+#include "malloc.h"
+
+#ifndef va_copy
+#ifdef __va_copy
+#define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
+#else
+#define va_copy(DEST,SRC) memcpy(&(DEST), &(SRC), sizeof(va_list))
+#endif
+#endif
+
+static const AvahiAllocator *allocator = NULL;
+
+static void oom(void) AVAHI_GCC_NORETURN;
+
+static void oom(void) {
+
+ static const char msg[] = "Out of memory, aborting ...\n";
+ const char *n = msg;
+
+ while (strlen(n) > 0) {
+ ssize_t r;
+
+ if ((r = write(2, n, strlen(n))) < 0)
+ break;
+
+ n += r;
+ }
+
+ abort();
+}
+
+/* Default implementation for avahi_malloc() */
+static void* xmalloc(size_t size) {
+ void *p;
+
+ if (size == 0)
+ return NULL;
+
+ if (!(p = malloc(size)))
+ oom();
+
+ return p;
+}
+
+/* Default implementation for avahi_realloc() */
+static void *xrealloc(void *p, size_t size) {
+
+ if (size == 0) {
+ free(p);
+ return NULL;
+ }
+
+ if (!(p = realloc(p, size)))
+ oom();
+
+ return p;
+}
+
+/* Default implementation for avahi_calloc() */
+static void *xcalloc(size_t nmemb, size_t size) {
+ void *p;
+
+ if (size == 0 || nmemb == 0)
+ return NULL;
+
+ if (!(p = calloc(nmemb, size)))
+ oom();
+
+ return p;
+}
+
+void *avahi_malloc(size_t size) {
+
+ if (size <= 0)
+ return NULL;
+
+ if (!allocator)
+ return xmalloc(size);
+
+ assert(allocator->malloc);
+ return allocator->malloc(size);
+}
+
+void *avahi_malloc0(size_t size) {
+ void *p;
+
+ if (size <= 0)
+ return NULL;
+
+ if (!allocator)
+ return xcalloc(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 (size == 0) {
+ avahi_free(p);
+ return NULL;
+ }
+
+ if (!allocator)
+ return xrealloc(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;
+ const char *p;
+
+ if (!s)
+ return NULL;
+
+ for (p = s, size = 0;
+ size < max && *p;
+ p++, size++);
+
+ if (!(r = avahi_new(char, 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 = 80;
+ char *buf;
+
+ assert(fmt);
+
+ if (!(buf = avahi_malloc(len)))
+ return NULL;
+
+ for (;;) {
+ int n;
+ char *nbuf;
+ va_list ap2;
+
+ va_copy (ap2, ap);
+ n = vsnprintf(buf, len, fmt, ap2);
+ va_end (ap2);
+
+ 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;
+
+ assert(fmt);
+
+ va_start(ap, fmt);
+ s = avahi_strdup_vprintf(fmt, ap);
+ va_end(ap);
+
+ return s;
+}
+
+void *avahi_memdup(const void *s, size_t l) {
+ void *p;
+ assert(s);
+
+ if (!(p = avahi_malloc(l)))
+ return NULL;
+
+ memcpy(p, s, l);
+ return p;
+}
diff --git a/avahi-common/malloc.h b/avahi-common/malloc.h
new file mode 100644
index 0000000..ffaac3f
--- /dev/null
+++ b/avahi-common/malloc.h
@@ -0,0 +1,96 @@
+#ifndef foomallochfoo
+#define foomallochfoo
+
+/***
+ 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.
+***/
+
+/** \file malloc.h Memory allocation */
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <limits.h>
+#include <assert.h>
+
+#include <avahi-common/cdecl.h>
+#include <avahi-common/gccmacro.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** Allocate some memory, just like the libc malloc() */
+void *avahi_malloc(size_t size) AVAHI_GCC_ALLOC_SIZE(1);
+
+/** Similar to avahi_malloc() but set the memory to zero */
+void *avahi_malloc0(size_t size) AVAHI_GCC_ALLOC_SIZE(1);
+
+/** Free some memory */
+void avahi_free(void *p);
+
+/** Similar to libc's realloc() */
+void *avahi_realloc(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2);
+
+/** Internal helper for avahi_new() */
+static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new_internal(unsigned n, size_t k) {
+ assert(n < INT_MAX/k);
+ return avahi_malloc(n*k);
+}
+
+/** Allocate n new structures of the specified type. */
+#define avahi_new(type, n) ((type*) avahi_new_internal((n), sizeof(type)))
+
+/** Internal helper for avahi_new0() */
+static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new0_internal(unsigned n, size_t k) {
+ assert(n < INT_MAX/k);
+ return avahi_malloc0(n*k);
+}
+
+/** Same as avahi_new() but set the memory to zero */
+#define avahi_new0(type, n) ((type*) avahi_new0_internal((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);
+
+/** Duplicate the given memory block into a new one allocated with avahi_malloc() */
+void *avahi_memdup(const void *s, size_t l) AVAHI_GCC_ALLOC_SIZE(2);
+
+/** Wraps allocator functions */
+typedef struct AvahiAllocator {
+ void* (*malloc)(size_t size) AVAHI_GCC_ALLOC_SIZE(1);
+ void (*free)(void *p);
+ void* (*realloc)(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2);
+ void* (*calloc)(size_t nmemb, size_t size) AVAHI_GCC_ALLOC_SIZE2(1,2); /**< May be NULL */
+} AvahiAllocator;
+
+/** Change the allocator. May be NULL to return to default (libc)
+ * allocators. The structure is not copied! */
+void avahi_set_allocator(const AvahiAllocator *a);
+
+/** Like sprintf() but store the result in a freshly allocated buffer. Free this with avahi_free() */
+char *avahi_strdup_printf(const char *fmt, ... ) AVAHI_GCC_PRINTF_ATTR12;
+
+/** \cond fulldocs */
+/** Same as avahi_strdup_printf() but take a va_list instead of varargs */
+char *avahi_strdup_vprintf(const char *fmt, va_list ap);
+/** \endcond */
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/rlist.c b/avahi-common/rlist.c
new file mode 100644
index 0000000..17dcb24
--- /dev/null
+++ b/avahi-common/rlist.c
@@ -0,0 +1,62 @@
+/***
+ 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 "rlist.h"
+#include "malloc.h"
+
+AvahiRList* avahi_rlist_prepend(AvahiRList *r, void *data) {
+ AvahiRList *n;
+
+ if (!(n = avahi_new(AvahiRList, 1)))
+ return NULL;
+
+ n->data = data;
+
+ AVAHI_LLIST_PREPEND(AvahiRList, rlist, r, n);
+ return r;
+}
+
+AvahiRList* avahi_rlist_remove(AvahiRList *r, void *data) {
+ AvahiRList *n;
+
+ for (n = r; n; n = n->rlist_next)
+
+ if (n->data == data) {
+ AVAHI_LLIST_REMOVE(AvahiRList, rlist, r, n);
+ avahi_free(n);
+ break;
+ }
+
+ return r;
+}
+
+AvahiRList* avahi_rlist_remove_by_link(AvahiRList *r, AvahiRList *n) {
+ assert(n);
+
+ AVAHI_LLIST_REMOVE(AvahiRList, rlist, r, n);
+ avahi_free(n);
+
+ return r;
+}
diff --git a/avahi-common/rlist.h b/avahi-common/rlist.h
new file mode 100644
index 0000000..9bcc1d5
--- /dev/null
+++ b/avahi-common/rlist.h
@@ -0,0 +1,49 @@
+#ifndef foorlistfoo
+#define foorlistfoo
+
+/***
+ 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.
+***/
+
+/** \file rlist.h A simple linked list implementation */
+
+#include "llist.h"
+
+AVAHI_C_DECL_BEGIN
+
+/** A doubly linked list type */
+typedef struct AvahiRList AvahiRList;
+
+/** A doubly linked list type */
+struct AvahiRList {
+ AVAHI_LLIST_FIELDS(AvahiRList, rlist);
+ void *data;
+};
+
+/** Prepend a new item to the beginning of the list and return the new beginning */
+AvahiRList* avahi_rlist_prepend(AvahiRList *r, void *data);
+
+/** Remove the first occurence of the specified item from the list and return the new beginning */
+AvahiRList* avahi_rlist_remove(AvahiRList *r, void *data);
+
+/** Remove the specified item from the list and return the new beginning */
+AvahiRList* avahi_rlist_remove_by_link(AvahiRList *r, AvahiRList *n);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/simple-watch.c b/avahi-common/simple-watch.c
new file mode 100644
index 0000000..8df18dd
--- /dev/null
+++ b/avahi-common/simple-watch.c
@@ -0,0 +1,649 @@
+/***
+ 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 <sys/poll.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#include "llist.h"
+#include "malloc.h"
+#include "timeval.h"
+#include "simple-watch.h"
+
+struct AvahiWatch {
+ AvahiSimplePoll *simple_poll;
+ int dead;
+
+ int idx;
+ struct pollfd pollfd;
+
+ AvahiWatchCallback callback;
+ void *userdata;
+
+ AVAHI_LLIST_FIELDS(AvahiWatch, watches);
+};
+
+struct AvahiTimeout {
+ AvahiSimplePoll *simple_poll;
+ int dead;
+
+ int enabled;
+ struct timeval expiry;
+
+ AvahiTimeoutCallback callback;
+ void *userdata;
+
+ AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
+};
+
+struct AvahiSimplePoll {
+ AvahiPoll api;
+ AvahiPollFunc poll_func;
+ void *poll_func_userdata;
+
+ struct pollfd* pollfds;
+ int n_pollfds, max_pollfds, rebuild_pollfds;
+
+ int watch_req_cleanup, timeout_req_cleanup;
+ int quit;
+ int events_valid;
+
+ int n_watches;
+ AVAHI_LLIST_HEAD(AvahiWatch, watches);
+ AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
+
+ int wakeup_pipe[2];
+ int wakeup_issued;
+
+ int prepared_timeout;
+
+ enum {
+ STATE_INIT,
+ STATE_PREPARING,
+ STATE_PREPARED,
+ STATE_RUNNING,
+ STATE_RAN,
+ STATE_DISPATCHING,
+ STATE_DISPATCHED,
+ STATE_QUIT,
+ STATE_FAILURE
+ } state;
+};
+
+void avahi_simple_poll_wakeup(AvahiSimplePoll *s) {
+ char c = 'W';
+ assert(s);
+
+ write(s->wakeup_pipe[1], &c, sizeof(c));
+ s->wakeup_issued = 1;
+}
+
+static void clear_wakeup(AvahiSimplePoll *s) {
+ char c[10]; /* Read ten at a time */
+
+ if (!s->wakeup_issued)
+ return;
+
+ s->wakeup_issued = 0;
+
+ for(;;)
+ if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c))
+ break;
+}
+
+static int set_nonblock(int fd) {
+ int n;
+
+ assert(fd >= 0);
+
+ if ((n = fcntl(fd, F_GETFL)) < 0)
+ return -1;
+
+ if (n & O_NONBLOCK)
+ return 0;
+
+ return fcntl(fd, F_SETFL, n|O_NONBLOCK);
+}
+
+static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
+ AvahiWatch *w;
+ AvahiSimplePoll *s;
+
+ assert(api);
+ assert(fd >= 0);
+ assert(callback);
+
+ s = api->userdata;
+ assert(s);
+
+ if (!(w = avahi_new(AvahiWatch, 1)))
+ return NULL;
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(s);
+
+ w->simple_poll = s;
+ w->dead = 0;
+
+ w->pollfd.fd = fd;
+ w->pollfd.events = event;
+ w->pollfd.revents = 0;
+
+ w->callback = callback;
+ w->userdata = userdata;
+
+ w->idx = -1;
+ s->rebuild_pollfds = 1;
+
+ AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w);
+ s->n_watches++;
+
+ return w;
+}
+
+static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
+ assert(w);
+ assert(!w->dead);
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(w->simple_poll);
+
+ w->pollfd.events = events;
+
+ if (w->idx != -1) {
+ assert(w->simple_poll);
+ w->simple_poll->pollfds[w->idx] = w->pollfd;
+ } else
+ w->simple_poll->rebuild_pollfds = 1;
+}
+
+static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
+ assert(w);
+ assert(!w->dead);
+
+ if (w->idx != -1 && w->simple_poll->events_valid)
+ return w->simple_poll->pollfds[w->idx].revents;
+
+ return 0;
+}
+
+static void remove_pollfd(AvahiWatch *w) {
+ assert(w);
+
+ if (w->idx == -1)
+ return;
+
+ w->simple_poll->rebuild_pollfds = 1;
+}
+
+static void watch_free(AvahiWatch *w) {
+ assert(w);
+
+ assert(!w->dead);
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(w->simple_poll);
+
+ remove_pollfd(w);
+
+ w->dead = 1;
+ w->simple_poll->n_watches --;
+ w->simple_poll->watch_req_cleanup = 1;
+}
+
+static void destroy_watch(AvahiWatch *w) {
+ assert(w);
+
+ remove_pollfd(w);
+ AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w);
+
+ if (!w->dead)
+ w->simple_poll->n_watches --;
+
+ avahi_free(w);
+}
+
+static void cleanup_watches(AvahiSimplePoll *s, int all) {
+ AvahiWatch *w, *next;
+ assert(s);
+
+ for (w = s->watches; w; w = next) {
+ next = w->watches_next;
+
+ if (all || w->dead)
+ destroy_watch(w);
+ }
+
+ s->timeout_req_cleanup = 0;
+}
+
+static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
+ AvahiTimeout *t;
+ AvahiSimplePoll *s;
+
+ assert(api);
+ assert(callback);
+
+ s = api->userdata;
+ assert(s);
+
+ if (!(t = avahi_new(AvahiTimeout, 1)))
+ return NULL;
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(s);
+
+ t->simple_poll = s;
+ t->dead = 0;
+
+ if ((t->enabled = !!tv))
+ t->expiry = *tv;
+
+ t->callback = callback;
+ t->userdata = userdata;
+
+ AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, s->timeouts, t);
+ return t;
+}
+
+static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
+ assert(t);
+ assert(!t->dead);
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(t->simple_poll);
+
+ if ((t->enabled = !!tv))
+ t->expiry = *tv;
+}
+
+static void timeout_free(AvahiTimeout *t) {
+ assert(t);
+ assert(!t->dead);
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(t->simple_poll);
+
+ t->dead = 1;
+ t->simple_poll->timeout_req_cleanup = 1;
+}
+
+
+static void destroy_timeout(AvahiTimeout *t) {
+ assert(t);
+
+ AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->simple_poll->timeouts, t);
+
+ avahi_free(t);
+}
+
+static void cleanup_timeouts(AvahiSimplePoll *s, int all) {
+ AvahiTimeout *t, *next;
+ assert(s);
+
+ for (t = s->timeouts; t; t = next) {
+ next = t->timeouts_next;
+
+ if (all || t->dead)
+ destroy_timeout(t);
+ }
+
+ s->timeout_req_cleanup = 0;
+}
+
+AvahiSimplePoll *avahi_simple_poll_new(void) {
+ AvahiSimplePoll *s;
+
+ if (!(s = avahi_new(AvahiSimplePoll, 1)))
+ return NULL;
+
+ if (pipe(s->wakeup_pipe) < 0) {
+ avahi_free(s);
+ return NULL;
+ }
+
+ set_nonblock(s->wakeup_pipe[0]);
+ set_nonblock(s->wakeup_pipe[1]);
+
+ s->api.userdata = s;
+
+ s->api.watch_new = watch_new;
+ s->api.watch_free = watch_free;
+ s->api.watch_update = watch_update;
+ s->api.watch_get_events = watch_get_events;
+
+ s->api.timeout_new = timeout_new;
+ s->api.timeout_free = timeout_free;
+ s->api.timeout_update = timeout_update;
+
+ s->pollfds = NULL;
+ s->max_pollfds = s->n_pollfds = 0;
+ s->rebuild_pollfds = 1;
+ s->quit = 0;
+ s->n_watches = 0;
+ s->events_valid = 0;
+
+ s->watch_req_cleanup = 0;
+ s->timeout_req_cleanup = 0;
+
+ s->prepared_timeout = 0;
+
+ s->state = STATE_INIT;
+
+ s->wakeup_issued = 0;
+
+ avahi_simple_poll_set_func(s, NULL, NULL);
+
+ AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches);
+ AVAHI_LLIST_HEAD_INIT(AvahiTimeout, s->timeouts);
+
+ return s;
+}
+
+void avahi_simple_poll_free(AvahiSimplePoll *s) {
+ assert(s);
+
+ cleanup_timeouts(s, 1);
+ cleanup_watches(s, 1);
+ assert(s->n_watches == 0);
+
+ avahi_free(s->pollfds);
+
+ if (s->wakeup_pipe[0] >= 0)
+ close(s->wakeup_pipe[0]);
+
+ if (s->wakeup_pipe[1] >= 0)
+ close(s->wakeup_pipe[1]);
+
+ avahi_free(s);
+}
+
+static int rebuild(AvahiSimplePoll *s) {
+ AvahiWatch *w;
+ int idx;
+
+ assert(s);
+
+ if (s->n_watches+1 > s->max_pollfds) {
+ struct pollfd *n;
+
+ s->max_pollfds = s->n_watches + 10;
+
+ if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds)))
+ return -1;
+
+ s->pollfds = n;
+ }
+
+
+ s->pollfds[0].fd = s->wakeup_pipe[0];
+ s->pollfds[0].events = POLLIN;
+ s->pollfds[0].revents = 0;
+
+ idx = 1;
+
+ for (w = s->watches; w; w = w->watches_next) {
+
+ if(w->dead)
+ continue;
+
+ assert(w->idx < s->max_pollfds);
+ s->pollfds[w->idx = idx++] = w->pollfd;
+ }
+
+ s->n_pollfds = idx;
+ s->events_valid = 0;
+ s->rebuild_pollfds = 0;
+
+ return 0;
+}
+
+static AvahiTimeout* find_next_timeout(AvahiSimplePoll *s) {
+ AvahiTimeout *t, *n = NULL;
+ assert(s);
+
+ for (t = s->timeouts; t; t = t->timeouts_next) {
+
+ if (t->dead || !t->enabled)
+ continue;
+
+ if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
+ n = t;
+ }
+
+ return n;
+}
+
+static void timeout_callback(AvahiTimeout *t) {
+ assert(t);
+ assert(!t->dead);
+ assert(t->enabled);
+
+ t->enabled = 0;
+ t->callback(t, t->userdata);
+}
+
+int avahi_simple_poll_prepare(AvahiSimplePoll *s, int timeout) {
+ AvahiTimeout *next_timeout;
+
+ assert(s);
+ assert(s->state == STATE_INIT || s->state == STATE_DISPATCHED || s->state == STATE_FAILURE);
+ s->state = STATE_PREPARING;
+
+ /* Clear pending wakeup requests */
+ clear_wakeup(s);
+
+ /* Cleanup things first */
+ if (s->watch_req_cleanup)
+ cleanup_watches(s, 0);
+
+ if (s->timeout_req_cleanup)
+ cleanup_timeouts(s, 0);
+
+ /* Check whether a quit was requested */
+ if (s->quit) {
+ s->state = STATE_QUIT;
+ return 1;
+ }
+
+ /* Do we need to rebuild our array of pollfds? */
+ if (s->rebuild_pollfds)
+ if (rebuild(s) < 0) {
+ s->state = STATE_FAILURE;
+ return -1;
+ }
+
+ /* Calculate the wakeup time */
+ if ((next_timeout = find_next_timeout(s))) {
+ struct timeval now;
+ int t;
+ AvahiUsec usec;
+
+ if (next_timeout->expiry.tv_sec == 0 &&
+ next_timeout->expiry.tv_usec == 0) {
+
+ /* Just a shortcut so that we don't need to call gettimeofday() */
+ timeout = 0;
+ goto finish;
+ }
+
+ gettimeofday(&now, NULL);
+ usec = avahi_timeval_diff(&next_timeout->expiry, &now);
+
+ if (usec <= 0) {
+ /* Timeout elapsed */
+
+ timeout = 0;
+ goto finish;
+ }
+
+ /* Calculate sleep time. We add 1ms because otherwise we'd
+ * wake up too early most of the time */
+ t = (int) (usec / 1000) + 1;
+
+ if (timeout < 0 || timeout > t)
+ timeout = t;
+ }
+
+finish:
+ s->prepared_timeout = timeout;
+ s->state = STATE_PREPARED;
+ return 0;
+}
+
+int avahi_simple_poll_run(AvahiSimplePoll *s) {
+ assert(s);
+ assert(s->state == STATE_PREPARED || s->state == STATE_FAILURE);
+
+ s->state = STATE_RUNNING;
+
+ for (;;) {
+ errno = 0;
+
+ if (s->poll_func(s->pollfds, s->n_pollfds, s->prepared_timeout, s->poll_func_userdata) < 0) {
+
+ if (errno == EINTR)
+ continue;
+
+ s->state = STATE_FAILURE;
+ return -1;
+ }
+
+ break;
+ }
+
+ /* The poll events are now valid again */
+ s->events_valid = 1;
+
+ /* Update state */
+ s->state = STATE_RAN;
+ return 0;
+}
+
+int avahi_simple_poll_dispatch(AvahiSimplePoll *s) {
+ AvahiTimeout *next_timeout;
+ AvahiWatch *w;
+
+ assert(s);
+ assert(s->state == STATE_RAN);
+ s->state = STATE_DISPATCHING;
+
+ /* We execute only on callback in every iteration */
+
+ /* Check whether the wakeup time has been reached now */
+ if ((next_timeout = find_next_timeout(s))) {
+
+ if (next_timeout->expiry.tv_sec == 0 && next_timeout->expiry.tv_usec == 0) {
+
+ /* Just a shortcut so that we don't need to call gettimeofday() */
+ timeout_callback(next_timeout);
+ goto finish;
+ }
+
+ if (avahi_age(&next_timeout->expiry) >= 0) {
+
+ /* Timeout elapsed */
+ timeout_callback(next_timeout);
+ goto finish;
+ }
+ }
+
+ /* Look for some kind of I/O event */
+ for (w = s->watches; w; w = w->watches_next) {
+
+ if (w->dead)
+ continue;
+
+ assert(w->idx >= 0);
+ assert(w->idx < s->n_pollfds);
+
+ if (s->pollfds[w->idx].revents != 0) {
+ w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata);
+ goto finish;
+ }
+ }
+
+finish:
+
+ s->state = STATE_DISPATCHED;
+ return 0;
+}
+
+int avahi_simple_poll_iterate(AvahiSimplePoll *s, int timeout) {
+ int r;
+
+ if ((r = avahi_simple_poll_prepare(s, timeout)) != 0)
+ return r;
+
+ if ((r = avahi_simple_poll_run(s)) != 0)
+ return r;
+
+ if ((r = avahi_simple_poll_dispatch(s)) != 0)
+ return r;
+
+ return 0;
+}
+
+void avahi_simple_poll_quit(AvahiSimplePoll *s) {
+ assert(s);
+
+ s->quit = 1;
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(s);
+}
+
+const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) {
+ assert(s);
+
+ return &s->api;
+}
+
+static int system_poll(struct pollfd *ufds, unsigned int nfds, int timeout, AVAHI_GCC_UNUSED void *userdata) {
+ return poll(ufds, nfds, timeout);
+}
+
+void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func, void *userdata) {
+ assert(s);
+
+ s->poll_func = func ? func : system_poll;
+ s->poll_func_userdata = func ? userdata : NULL;
+
+ /* If there is a background thread running the poll() for us, tell it to exit the poll() */
+ avahi_simple_poll_wakeup(s);
+}
+
+int avahi_simple_poll_loop(AvahiSimplePoll *s) {
+ int r;
+
+ assert(s);
+
+ for (;;)
+ if ((r = avahi_simple_poll_iterate(s, -1)) != 0)
+ if (r >= 0 || errno != EINTR)
+ return r;
+}
diff --git a/avahi-common/simple-watch.h b/avahi-common/simple-watch.h
new file mode 100644
index 0000000..72c1905
--- /dev/null
+++ b/avahi-common/simple-watch.h
@@ -0,0 +1,85 @@
+#ifndef foosimplewatchhfoo
+#define foosimplewatchhfoo
+
+/***
+ 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.
+***/
+
+/** \file simple-watch.h Simple poll() based main loop implementation */
+
+#include <sys/poll.h>
+#include <avahi-common/cdecl.h>
+#include <avahi-common/watch.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** A main loop object. Main loops of this type aren't very flexible
+ * since they only support a single wakeup type. Nevertheless it
+ * should suffice for small test and example applications. */
+typedef struct AvahiSimplePoll AvahiSimplePoll;
+
+/** Create a new main loop object */
+AvahiSimplePoll *avahi_simple_poll_new(void);
+
+/** Free a main loop object */
+void avahi_simple_poll_free(AvahiSimplePoll *s);
+
+/** Return the abstracted poll API object for this main loop
+ * object. The is will return the same pointer each time it is
+ * called. */
+const AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s);
+
+/** Run a single main loop iteration of this main loop. If sleep_time
+is < 0 this will block until any of the registered events happens,
+then it will execute the attached callback function. If sleep_time is
+0 the routine just checks if any event is pending. If yes the attached
+callback function is called, otherwise the function returns
+immediately. If sleep_time > 0 the function will block for at most the
+specified time in msecs. Returns -1 on error, 0 on success and 1 if a
+quit request has been scheduled. Usually this function should be called
+in a loop until it returns a non-zero value*/
+int avahi_simple_poll_iterate(AvahiSimplePoll *s, int sleep_time);
+
+/** Request that the main loop quits. If this is called the next
+ call to avahi_simple_poll_iterate() will return 1 */
+void avahi_simple_poll_quit(AvahiSimplePoll *s);
+
+/** Prototype for a poll() type function */
+typedef int (*AvahiPollFunc)(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata);
+
+/** Replace the internally used poll() function. By default the system's poll() will be used */
+void avahi_simple_poll_set_func(AvahiSimplePoll *s, AvahiPollFunc func, void *userdata);
+
+/** The first stage of avahi_simple_poll_iterate(), use this function only if you know what you do */
+int avahi_simple_poll_prepare(AvahiSimplePoll *s, int timeout);
+
+/** The second stage of avahi_simple_poll_iterate(), use this function only if you know what you do */
+int avahi_simple_poll_run(AvahiSimplePoll *s);
+
+/** The third and final stage of avahi_simple_poll_iterate(), use this function only if you know what you do */
+int avahi_simple_poll_dispatch(AvahiSimplePoll *s);
+
+/** Call avahi_simple_poll_iterate() in a loop and return if it returns non-zero */
+int avahi_simple_poll_loop(AvahiSimplePoll *s);
+
+/** Wakeup the main loop. (for threaded environments) */
+void avahi_simple_poll_wakeup(AvahiSimplePoll *s);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c
new file mode 100644
index 0000000..0945b37
--- /dev/null
+++ b/avahi-common/strlst-test.c
@@ -0,0 +1,127 @@
+/***
+ 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 <assert.h>
+
+#include "strlst.h"
+#include "malloc.h"
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+ char *t, *v;
+ uint8_t data[1024];
+ AvahiStringList *a = NULL, *b, *p;
+ size_t size, n;
+ int r;
+
+ a = avahi_string_list_new("prefix", "a", "b", NULL);
+
+ a = avahi_string_list_add(a, "start");
+ a = avahi_string_list_add(a, "foo=99");
+ a = avahi_string_list_add(a, "bar");
+ a = avahi_string_list_add(a, "");
+ a = avahi_string_list_add(a, "");
+ a = avahi_string_list_add(a, "quux");
+ a = avahi_string_list_add(a, "");
+ a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9);
+ a = avahi_string_list_add_printf(a, "seven=%i %c", 7, 'x');
+ a = avahi_string_list_add_pair(a, "blubb", "blaa");
+ a = avahi_string_list_add_pair(a, "uxknurz", NULL);
+ a = avahi_string_list_add_pair_arbitrary(a, "uxknurz2", (const uint8_t*) "blafasel\0oerks", 14);
+
+ a = avahi_string_list_add(a, "end");
+
+ t = avahi_string_list_to_string(a);
+ printf("--%s--\n", t);
+ avahi_free(t);
+
+ n = avahi_string_list_serialize(a, NULL, 0);
+ size = avahi_string_list_serialize(a, data, sizeof(data));
+ assert(size == n);
+
+ printf("%zu\n", size);
+
+ for (t = (char*) data, n = 0; n < size; n++, t++) {
+ if (*t <= 32)
+ printf("(%u)", *t);
+ else
+ printf("%c", *t);
+ }
+
+ printf("\n");
+
+ assert(avahi_string_list_parse(data, size, &b) == 0);
+
+ printf("equal: %i\n", avahi_string_list_equal(a, b));
+
+ t = avahi_string_list_to_string(b);
+ printf("--%s--\n", t);
+ avahi_free(t);
+
+ avahi_string_list_free(b);
+
+ b = avahi_string_list_copy(a);
+
+ assert(avahi_string_list_equal(a, b));
+
+ t = avahi_string_list_to_string(b);
+ printf("--%s--\n", t);
+ avahi_free(t);
+
+ p = avahi_string_list_find(a, "seven");
+ assert(p);
+
+ r = avahi_string_list_get_pair(p, &t, &v, NULL);
+ assert(r >= 0);
+ assert(t);
+ assert(v);
+
+ printf("<%s>=<%s>\n", t, v);
+ avahi_free(t);
+ avahi_free(v);
+
+ p = avahi_string_list_find(a, "quux");
+ assert(p);
+
+ r = avahi_string_list_get_pair(p, &t, &v, NULL);
+ assert(r >= 0);
+ assert(t);
+ assert(!v);
+
+ printf("<%s>=<%s>\n", t, v);
+ avahi_free(t);
+ avahi_free(v);
+
+ avahi_string_list_free(a);
+ avahi_string_list_free(b);
+
+ n = avahi_string_list_serialize(NULL, NULL, 0);
+ size = avahi_string_list_serialize(NULL, data, sizeof(data));
+ assert(size == 1);
+ assert(size == n);
+
+ assert(avahi_string_list_parse(data, size, &a) == 0);
+ assert(!a);
+
+ return 0;
+}
diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c
new file mode 100644
index 0000000..b861cf8
--- /dev/null
+++ b/avahi-common/strlst.c
@@ -0,0 +1,505 @@
+/***
+ 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 <string.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "strlst.h"
+#include "malloc.h"
+#include "defs.h"
+
+AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
+ AvahiStringList *n;
+
+ if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
+ return NULL;
+
+ n->next = l;
+ n->size = size;
+
+ /* NUL terminate strings, just to make sure */
+ n->text[size] = 0;
+
+ return n;
+}
+
+AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
+ AvahiStringList *n;
+
+ assert(size == 0 || text);
+
+ 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 char *text) {
+ assert(text);
+
+ return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
+}
+
+int avahi_string_list_parse(const void* data, size_t size, AvahiStringList **ret) {
+ const uint8_t *c;
+ AvahiStringList *r = NULL;
+
+ assert(data);
+ assert(ret);
+
+ c = data;
+ while (size > 0) {
+ size_t k;
+
+ k = *(c++);
+ size--;
+
+ if (k > size)
+ goto fail; /* Overflow */
+
+ if (k > 0) { /* Ignore empty strings */
+ AvahiStringList *n;
+
+ if (!(n = avahi_string_list_add_arbitrary(r, c, k)))
+ goto fail; /* OOM */
+
+ r = n;
+ }
+
+ c += k;
+ size -= k;
+ }
+
+ *ret = r;
+
+ return 0;
+
+fail:
+ avahi_string_list_free(r);
+ return -1;
+}
+
+void avahi_string_list_free(AvahiStringList *l) {
+ AvahiStringList *n;
+
+ while (l) {
+ n = l->next;
+ avahi_free(l);
+ l = n;
+ }
+}
+
+AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
+ AvahiStringList *r = NULL, *n;
+
+ while (l) {
+ n = l->next;
+ l->next = r;
+ r = l;
+ l = n;
+ }
+
+ return r;
+}
+
+char* avahi_string_list_to_string(AvahiStringList *l) {
+ AvahiStringList *n;
+ size_t s = 0;
+ char *t, *e;
+
+ for (n = l; n; n = n->next) {
+ if (n != l)
+ s ++;
+
+ s += n->size+2;
+ }
+
+ if (!(t = e = avahi_new(char, s+1)))
+ return NULL;
+
+ l = avahi_string_list_reverse(l);
+
+ for (n = l; n; n = n->next) {
+ if (n != l)
+ *(e++) = ' ';
+
+ *(e++) = '"';
+ strncpy(e, (char*) n->text, n->size);
+ e[n->size] = 0;
+ e = strchr(e, 0);
+ *(e++) = '"';
+
+ assert(e);
+ }
+
+ l = avahi_string_list_reverse(l);
+
+ *e = 0;
+
+ return t;
+}
+
+size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
+ size_t used = 0;
+
+ if (data) {
+ AvahiStringList *n;
+ uint8_t *c;
+
+ l = avahi_string_list_reverse(l);
+ c = data;
+
+ for (n = l; size > 1 && n; n = n->next) {
+ size_t k;
+
+ if ((k = n->size) == 0)
+ /* Skip empty strings */
+ continue;
+
+ if (k > 255)
+ /* Truncate strings at 255 characters */
+ k = 255;
+
+ if (k > size-1)
+ /* Make sure this string fits in */
+ k = size-1;
+
+ *(c++) = (uint8_t) k;
+ memcpy(c, n->text, k);
+ c += k;
+
+ used += 1 + k;
+ size -= 1 + k;
+ }
+
+ l = avahi_string_list_reverse(l);
+
+ if (used == 0 && size > 0) {
+
+ /* Empty lists are treated specially. To comply with
+ * section 6.1 of the DNS-SD spec, we return a single
+ * empty string (i.e. a NUL byte)*/
+
+ *(uint8_t*) data = 0;
+ used = 1;
+ }
+
+ } else {
+ AvahiStringList *n;
+
+ for (n = l; n; n = n->next) {
+ size_t k;
+
+ if ((k = n->size) == 0)
+ continue;
+
+ if (k > 255)
+ k = 255;
+
+ used += 1+k;
+ }
+
+ if (used == 0)
+ used = 1;
+ }
+
+ return used;
+}
+
+int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
+
+ for (;;) {
+ if (!a && !b)
+ return 1;
+
+ if (!a || !b)
+ return 0;
+
+ if (a->size != b->size)
+ return 0;
+
+ if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
+ return 0;
+
+ a = a->next;
+ b = b->next;
+ }
+}
+
+AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
+ va_list va;
+
+ va_start(va, r);
+ r = avahi_string_list_add_many_va(r, va);
+ va_end(va);
+
+ return r;
+}
+
+AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
+ const char *txt;
+
+ while ((txt = va_arg(va, const char*)))
+ r = avahi_string_list_add(r, txt);
+
+ return r;
+}
+
+AvahiStringList *avahi_string_list_new(const char *txt, ...) {
+ va_list va;
+ AvahiStringList *r = NULL;
+
+ if (txt) {
+ r = avahi_string_list_add(r, txt);
+
+ va_start(va, txt);
+ r = avahi_string_list_add_many_va(r, va);
+ va_end(va);
+ }
+
+ return r;
+}
+
+AvahiStringList *avahi_string_list_new_va(va_list va) {
+ return avahi_string_list_add_many_va(NULL, va);
+}
+
+AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
+ AvahiStringList *r = NULL;
+
+ for (; l; l = l->next)
+ if (!(r = avahi_string_list_add_arbitrary(r, l->text, l->size))) {
+ avahi_string_list_free(r);
+ return NULL;
+ }
+
+ return avahi_string_list_reverse(r);
+}
+
+AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
+ AvahiStringList *r = NULL;
+ int i;
+
+ assert(array);
+
+ for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
+ r = avahi_string_list_add(r, array[i]);
+
+ return r;
+}
+
+unsigned avahi_string_list_length(const AvahiStringList *l) {
+ unsigned n = 0;
+
+ for (; l; l = l->next)
+ n++;
+
+ return n;
+}
+
+AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
+ size_t len = 80;
+ AvahiStringList *r;
+
+ assert(format);
+
+ if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
+ return NULL;
+
+ for (;;) {
+ int n;
+ AvahiStringList *nr;
+ va_list va2;
+
+ va_copy(va2, va);
+ n = vsnprintf((char*) r->text, len, format, va2);
+ va_end(va2);
+
+ if (n >= 0 && n < (int) len)
+ break;
+
+ if (n >= 0)
+ len = n+1;
+ else
+ len *= 2;
+
+ if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
+ avahi_free(r);
+ return NULL;
+ }
+
+ r = nr;
+ }
+
+ r->next = l;
+ r->size = strlen((char*) r->text);
+
+ return r;
+}
+
+AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
+ va_list va;
+
+ assert(format);
+
+ va_start(va, format);
+ l = avahi_string_list_add_vprintf(l, format, va);
+ va_end(va);
+
+ return l;
+}
+
+AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
+ size_t n;
+
+ assert(key);
+ n = strlen(key);
+
+ for (; l; l = l->next) {
+ if (strcasecmp((char*) l->text, key) == 0)
+ return l;
+
+ if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
+ return l;
+ }
+
+ return NULL;
+}
+
+AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
+ assert(key);
+
+ if (value)
+ return avahi_string_list_add_printf(l, "%s=%s", key, value);
+ else
+ return avahi_string_list_add(l, key);
+}
+
+AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
+ size_t n;
+ assert(key);
+
+ if (!value)
+ return avahi_string_list_add(l, key);
+
+ n = strlen(key);
+
+ if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
+ return NULL;
+
+ memcpy(l->text, key, n);
+ l->text[n] = '=';
+ memcpy(l->text + n + 1, value, size);
+
+ return l;
+}
+
+int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
+ char *e;
+
+ assert(l);
+
+ if (!(e = memchr(l->text, '=', l->size))) {
+
+ if (key)
+ if (!(*key = avahi_strdup((char*) l->text)))
+ return -1;
+
+ if (value)
+ *value = NULL;
+
+ if (size)
+ *size = 0;
+
+ } else {
+ size_t n;
+
+ if (key)
+ if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
+ return -1;
+
+ e++; /* Advance after '=' */
+
+ n = l->size - (e - (char*) l->text);
+
+ if (value) {
+
+ if (!(*value = avahi_memdup(e, n+1))) {
+ if (key)
+ avahi_free(*key);
+ return -1;
+ }
+
+ (*value)[n] = 0;
+ }
+
+ if (size)
+ *size = n;
+ }
+
+ return 0;
+}
+
+AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
+ assert(l);
+ return l->next;
+}
+
+uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
+ assert(l);
+ return l->text;
+}
+
+size_t avahi_string_list_get_size(AvahiStringList *l) {
+ assert(l);
+ return l->size;
+}
+
+uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l) {
+ AvahiStringList *f;
+ char *value = NULL, *end = NULL;
+ uint32_t ret;
+
+ if (!(f = avahi_string_list_find(l, AVAHI_SERVICE_COOKIE)))
+ return AVAHI_SERVICE_COOKIE_INVALID;
+
+ if (avahi_string_list_get_pair(f, NULL, &value, NULL) < 0 || !value)
+ return AVAHI_SERVICE_COOKIE_INVALID;
+
+ ret = (uint32_t) strtoll(value, &end, 0);
+
+ if (*value && end && *end != 0) {
+ avahi_free(value);
+ return AVAHI_SERVICE_COOKIE_INVALID;
+ }
+
+ avahi_free(value);
+
+ return ret;
+}
diff --git a/avahi-common/strlst.h b/avahi-common/strlst.h
new file mode 100644
index 0000000..94adcea
--- /dev/null
+++ b/avahi-common/strlst.h
@@ -0,0 +1,180 @@
+#ifndef footxtlisthfoo
+#define footxtlisthfoo
+
+/***
+ 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.
+***/
+
+/** \file strlst.h Implementation of a data type to store lists of strings */
+
+#include <sys/types.h>
+#include <inttypes.h>
+#include <stdarg.h>
+
+#include <avahi-common/cdecl.h>
+#include <avahi-common/gccmacro.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** Linked list of strings that can contain any number of binary
+ * characters, including NUL bytes. An empty list is created by
+ * assigning a NULL to a pointer to AvahiStringList. The string list
+ * is stored in reverse order, so that appending to the string list is
+ * effectively a prepending to the linked list. This object is used
+ * primarily for storing DNS TXT record data. */
+typedef struct AvahiStringList {
+ struct AvahiStringList *next; /**< Pointer to the next linked list element */
+ size_t size; /**< Size of text[] */
+ uint8_t text[1]; /**< Character data */
+} AvahiStringList;
+
+/** @{ \name Construction and destruction */
+
+/** 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 char *txt, ...) AVAHI_GCC_SENTINEL;
+
+/** \cond fulldocs */
+/** Same as avahi_string_list_new() but pass a va_list structure */
+AvahiStringList *avahi_string_list_new_va(va_list va);
+/** \endcond */
+
+/** 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 char **array, int length);
+
+/** Free a string list */
+void avahi_string_list_free(AvahiStringList *l);
+
+/** @} */
+
+/** @{ \name Adding strings */
+
+/** 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 char *text);
+
+/** Append a new NUL terminated formatted string to the specified string list */
+AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) AVAHI_GCC_PRINTF_ATTR23;
+
+/** \cond fulldocs */
+/** Append a new NUL terminated formatted string to the specified string list */
+AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va);
+/** \endcond */
+
+/** Append an arbitrary length byte string to the list. Returns the
+ * new list start. */
+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, 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
+ * NULL pointer. Returns the new list start. */
+AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) AVAHI_GCC_SENTINEL;
+
+/** \cond fulldocs */
+/** Same as avahi_string_list_add_many(), but use a va_list
+ * structure. Returns the new list start. */
+AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va);
+/** \endcond */
+
+/** @} */
+
+/** @{ \name String list operations */
+
+/** Convert the string list object to a single character string,
+ * seperated by spaces and enclosed in "". avahi_free() the result! This
+ * function doesn't work well with strings that contain NUL bytes. */
+char* avahi_string_list_to_string(AvahiStringList *l);
+
+/** \cond fulldocs */
+/** 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. */
+size_t avahi_string_list_serialize(AvahiStringList *l, void * data, size_t size);
+
+/** Inverse of avahi_string_list_serialize() */
+int avahi_string_list_parse(const void *data, size_t size, AvahiStringList **ret);
+/** \endcond */
+
+/** Compare to string lists */
+int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b);
+
+/** Copy a string list */
+AvahiStringList *avahi_string_list_copy(const AvahiStringList *l);
+
+/** Reverse the string list. */
+AvahiStringList* avahi_string_list_reverse(AvahiStringList *l);
+
+/** Return the number of elements in the string list */
+unsigned avahi_string_list_length(const AvahiStringList *l);
+
+/** @} */
+
+/** @{ \name Accessing items */
+
+/** Returns the next item in the string list */
+AvahiStringList *avahi_string_list_get_next(AvahiStringList *l);
+
+/** Returns the text for the current item */
+uint8_t *avahi_string_list_get_text(AvahiStringList *l);
+
+/** Returns the size of the current text */
+size_t avahi_string_list_get_size(AvahiStringList *l);
+
+/** @} */
+
+/** @{ \name DNS-SD TXT pair handling */
+
+/** Find the string list entry for the given DNS-SD TXT key */
+AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key);
+
+/** Return the DNS-SD TXT key and value for the specified string list
+ * item. If size is not NULL it will be filled with the length of
+ * value. (for strings containing NUL bytes). If the entry doesn't
+ * contain a value *value will be set to NULL. You need to
+ * avahi_free() the strings returned in *key and *value. */
+int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size);
+
+/** Add a new DNS-SD TXT key value pair to the string list. value may
+ * be NULL in case you want to specify a key without a value */
+AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value);
+
+/** Same as avahi_string_list_add_pair() but allow strings containing NUL bytes in *value. */
+AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size);
+
+/** @} */
+
+/** \cond fulldocs */
+/** Try to find a magic service cookie in the specified DNS-SD string
+ * list. Or return AVAHI_SERVICE_COOKIE_INVALID if none is found. */
+uint32_t avahi_string_list_get_service_cookie(AvahiStringList *l);
+/** \endcond */
+
+AVAHI_C_DECL_END
+
+#endif
+
diff --git a/avahi-common/thread-watch.c b/avahi-common/thread-watch.c
new file mode 100644
index 0000000..c0cadeb
--- /dev/null
+++ b/avahi-common/thread-watch.c
@@ -0,0 +1,186 @@
+/***
+ 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 <sys/poll.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include "llist.h"
+#include "malloc.h"
+#include "timeval.h"
+#include "simple-watch.h"
+#include "thread-watch.h"
+
+struct AvahiThreadedPoll {
+ AvahiSimplePoll *simple_poll;
+ pthread_t thread_id;
+ pthread_mutex_t mutex;
+ int thread_running;
+ int retval;
+};
+
+static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
+ pthread_mutex_t *mutex = userdata;
+ int r;
+
+ /* Before entering poll() we unlock the mutex, so that
+ * avahi_simple_poll_quit() can succeed from another thread. */
+
+ pthread_mutex_unlock(mutex);
+ r = poll(ufds, nfds, timeout);
+ pthread_mutex_lock(mutex);
+
+ return r;
+}
+
+static void* thread(void *userdata){
+ AvahiThreadedPoll *p = userdata;
+ sigset_t mask;
+
+ /* Make sure that signals are delivered to the main thread */
+ sigfillset(&mask);
+ pthread_sigmask(SIG_BLOCK, &mask, NULL);
+
+ pthread_mutex_lock(&p->mutex);
+ p->retval = avahi_simple_poll_loop(p->simple_poll);
+ pthread_mutex_unlock(&p->mutex);
+
+ return NULL;
+}
+
+AvahiThreadedPoll *avahi_threaded_poll_new(void) {
+ AvahiThreadedPoll *p;
+
+ if (!(p = avahi_new(AvahiThreadedPoll, 1)))
+ goto fail; /* OOM */
+
+ if (!(p->simple_poll = avahi_simple_poll_new()))
+ goto fail;
+
+ pthread_mutex_init(&p->mutex, NULL);
+
+ avahi_simple_poll_set_func(p->simple_poll, poll_func, &p->mutex);
+
+ p->thread_running = 0;
+
+ return p;
+
+fail:
+ if (p) {
+ if (p->simple_poll) {
+ avahi_simple_poll_free(p->simple_poll);
+ pthread_mutex_destroy(&p->mutex);
+ }
+
+ avahi_free(p);
+ }
+
+ return NULL;
+}
+
+void avahi_threaded_poll_free(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ if (p->thread_running)
+ avahi_threaded_poll_stop(p);
+
+ if (p->simple_poll)
+ avahi_simple_poll_free(p->simple_poll);
+
+ pthread_mutex_destroy(&p->mutex);
+ avahi_free(p);
+}
+
+const AvahiPoll* avahi_threaded_poll_get(AvahiThreadedPoll *p) {
+ assert(p);
+
+ return avahi_simple_poll_get(p->simple_poll);
+}
+
+int avahi_threaded_poll_start(AvahiThreadedPoll *p) {
+ assert(p);
+
+ assert(!p->thread_running);
+
+ if (pthread_create(&p->thread_id, NULL, thread, p) < 0)
+ return -1;
+
+ p->thread_running = 1;
+
+ return 0;
+}
+
+int avahi_threaded_poll_stop(AvahiThreadedPoll *p) {
+ assert(p);
+
+ if (!p->thread_running)
+ return -1;
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_lock(&p->mutex);
+ avahi_simple_poll_quit(p->simple_poll);
+ pthread_mutex_unlock(&p->mutex);
+
+ pthread_join(p->thread_id, NULL);
+ p->thread_running = 0;
+
+ return p->retval;
+}
+
+void avahi_threaded_poll_quit(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is called from the helper thread */
+ assert(pthread_equal(pthread_self(), p->thread_id));
+
+ avahi_simple_poll_quit(p->simple_poll);
+}
+
+void avahi_threaded_poll_lock(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_lock(&p->mutex);
+}
+
+void avahi_threaded_poll_unlock(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_unlock(&p->mutex);
+}
diff --git a/avahi-common/thread-watch.h b/avahi-common/thread-watch.h
new file mode 100644
index 0000000..dec0cf3
--- /dev/null
+++ b/avahi-common/thread-watch.h
@@ -0,0 +1,80 @@
+#ifndef foothreadedwatchhfoo
+#define foothreadedwatchhfoo
+
+/***
+ 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.
+***/
+
+/** \file thread-watch.h Threaded poll() based main loop implementation */
+
+#include <sys/poll.h>
+#include <avahi-common/cdecl.h>
+#include <avahi-common/watch.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** A main loop object that runs an AvahiSimplePoll in its own thread. \since 0.6.4 */
+typedef struct AvahiThreadedPoll AvahiThreadedPoll;
+
+/** Create a new event loop object. This will allocate the internal
+ * AvahiSimplePoll, but will not start the helper thread. \since 0.6.4 */
+AvahiThreadedPoll *avahi_threaded_poll_new(void);
+
+/** Free an event loop object. This will stop the associated event loop
+ * thread (if it is running). \since 0.6.4 */
+void avahi_threaded_poll_free(AvahiThreadedPoll *p);
+
+/** Return the abstracted poll API object for this event loop
+ * object. The will return the same pointer each time it is
+ * called. \since 0.6.4 */
+const AvahiPoll* avahi_threaded_poll_get(AvahiThreadedPoll *p);
+
+/** Start the event loop helper thread. After the thread has started
+ * you must make sure to access the event loop object
+ * (AvahiThreadedPoll, AvahiPoll and all its associated objects)
+ * synchronized, i.e. with proper locking. You may want to use
+ * avahi_threaded_poll_lock()/avahi_threaded_poll_unlock() for this,
+ * which will lock the the entire event loop. Please note that event
+ * loop callback functions are called from the event loop helper thread
+ * with that lock held, i.e. avahi_threaded_poll_lock() calls are not
+ * required from event callbacks. \since 0.6.4 */
+int avahi_threaded_poll_start(AvahiThreadedPoll *p);
+
+/** Request that the event loop quits and the associated thread
+ stops. Call this from outside the helper thread if you want to shut
+ it down. \since 0.6.4 */
+int avahi_threaded_poll_stop(AvahiThreadedPoll *p);
+
+/** Request that the event loop quits and the associated thread
+ stops. Call this from inside the helper thread if you want to shut it
+ down. \since 0.6.4 */
+void avahi_threaded_poll_quit(AvahiThreadedPoll *p);
+
+/** Lock the main loop object. Use this if you want to access the event
+ * loop objects (such as creating a new event source) from anything
+ * else but the event loop helper thread, i.e. from anything else but event
+ * loop callbacks \since 0.6.4 */
+void avahi_threaded_poll_lock(AvahiThreadedPoll *p);
+
+/** Unlock the event loop object, use this as counterpart to
+ * avahi_threaded_poll_lock() \since 0.6.4 */
+void avahi_threaded_poll_unlock(AvahiThreadedPoll *p);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c
new file mode 100644
index 0000000..387c180
--- /dev/null
+++ b/avahi-common/timeval-test.c
@@ -0,0 +1,43 @@
+/***
+ 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 "gccmacro.h"
+#include "timeval.h"
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+
+ struct timeval a = { 5, 5 }, b;
+
+ b = a;
+
+ printf("%li.%li\n", a.tv_sec, a.tv_usec);
+ avahi_timeval_add(&a, -50);
+
+ printf("%li.%li\n", a.tv_sec, a.tv_usec);
+
+ printf("%lli\n", (long long) avahi_timeval_diff(&a, &b));
+
+ return 0;
+}
diff --git a/avahi-common/timeval.c b/avahi-common/timeval.c
new file mode 100644
index 0000000..cdb0f09
--- /dev/null
+++ b/avahi-common/timeval.c
@@ -0,0 +1,123 @@
+/***
+ 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 <pthread.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "timeval.h"
+
+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;
+
+ if (a->tv_sec > b->tv_sec)
+ return 1;
+
+ if (a->tv_usec < b->tv_usec)
+ return -1;
+
+ if (a->tv_usec > b->tv_usec)
+ return 1;
+
+ return 0;
+}
+
+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;
+}
+
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
+ AvahiUsec u;
+ assert(a);
+
+ u = usec + a->tv_usec;
+
+ if (u < 0) {
+ a->tv_usec = (long) (1000000 + (u % 1000000));
+ a->tv_sec += (long) (-1 + (u / 1000000));
+ } else {
+ a->tv_usec = (long) (u % 1000000);
+ a->tv_sec += (long) (u / 1000000);
+ }
+
+ return a;
+}
+
+AvahiUsec avahi_age(const struct timeval *a) {
+ struct timeval now;
+
+ assert(a);
+
+ gettimeofday(&now, NULL);
+
+ return avahi_timeval_diff(&now, a);
+}
+
+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) {
+ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ static int last_rand;
+ static time_t timestamp = 0;
+
+ time_t now;
+ int r;
+
+ now = time(NULL);
+
+ pthread_mutex_lock(&mutex);
+ if (now >= timestamp + 10) {
+ timestamp = now;
+ last_rand = rand();
+ }
+
+ r = last_rand;
+
+ pthread_mutex_unlock(&mutex);
+
+ /* We use the same jitter for 10 seconds. That way our
+ * time events elapse in bursts which has the advantage that
+ * packet data can be aggregated better */
+
+ avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*r/(RAND_MAX+1.0)));
+ }
+
+ return tv;
+}
+
diff --git a/avahi-common/timeval.h b/avahi-common/timeval.h
new file mode 100644
index 0000000..6470f01
--- /dev/null
+++ b/avahi-common/timeval.h
@@ -0,0 +1,54 @@
+#ifndef footimevalhfoo
+#define footimevalhfoo
+
+/***
+ 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.
+***/
+
+/** \file timeval.h Functions to facilitate timeval handling */
+
+#include <inttypes.h>
+#include <sys/time.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** A numeric data type for storing microsecond values. (signed 64bit integer) */
+typedef int64_t AvahiUsec;
+
+/** Compare two timeval structures and return a negative value when a < b, 0 when a == b and a positive value otherwise */
+int avahi_timeval_compare(const struct timeval *a, const struct timeval *b);
+
+/** Calculate the difference between two timeval structures as microsecond value */
+AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b);
+
+/** Add a number of microseconds to the specified timeval structure and return it. *a is modified. */
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec);
+
+/** Return the difference between the current time and *a. Positive if *a was earlier */
+AvahiUsec avahi_age(const struct timeval *a);
+
+/** Fill *tv with the current time plus "ms" milliseconds plus an
+ * extra jitter of "j" milliseconds. Pass 0 for j if you don't want
+ * the jitter */
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned ms, unsigned j);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/utf8-test.c b/avahi-common/utf8-test.c
new file mode 100644
index 0000000..d12ed58
--- /dev/null
+++ b/avahi-common/utf8-test.c
@@ -0,0 +1,37 @@
+/***
+ 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 <assert.h>
+
+#include <avahi-common/gccmacro.h>
+
+#include "utf8.h"
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+
+ assert(avahi_utf8_valid("hallo"));
+ assert(!avahi_utf8_valid("üxknürz"));
+ assert(avahi_utf8_valid("üxknürz"));
+
+ return 0;
+}
diff --git a/avahi-common/utf8.c b/avahi-common/utf8.c
new file mode 100644
index 0000000..ab10ba0
--- /dev/null
+++ b/avahi-common/utf8.c
@@ -0,0 +1,110 @@
+/* This file is based on the GLIB utf8 validation functions. The
+ * original license text follows. */
+
+/* gutf8.c - Operations on UTF-8 strings.
+ *
+ * Copyright (C) 1999 Tom Tromey
+ * Copyright (C) 2000 Red Hat, Inc.
+ *
+ * This library 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 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; 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 "utf8.h"
+
+#define UNICODE_VALID(Char) \
+ ((Char) < 0x110000 && \
+ (((Char) & 0xFFFFF800) != 0xD800) && \
+ ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
+ ((Char) & 0xFFFE) != 0xFFFE)
+
+
+#define CONTINUATION_CHAR \
+ do { \
+ if ((*(const unsigned char *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
+ goto error; \
+ val <<= 6; \
+ val |= (*(const unsigned char *)p) & 0x3f; \
+ } while(0)
+
+
+const char *
+avahi_utf8_valid (const char *str)
+
+{
+ unsigned val = 0;
+ unsigned min = 0;
+ const char *p;
+
+ for (p = str; *p; p++)
+ {
+ if (*(const unsigned char *)p < 128)
+ /* done */;
+ else
+ {
+ if ((*(const unsigned char *)p & 0xe0) == 0xc0) /* 110xxxxx */
+ {
+ if ( ((*(const unsigned char *)p & 0x1e) == 0))
+ goto error;
+ p++;
+ if ( ((*(const unsigned char *)p & 0xc0) != 0x80)) /* 10xxxxxx */
+ goto error;
+ }
+ else
+ {
+ if ((*(const unsigned char *)p & 0xf0) == 0xe0) /* 1110xxxx */
+ {
+ min = (1 << 11);
+ val = *(const unsigned char *)p & 0x0f;
+ goto TWO_REMAINING;
+ }
+ else if ((*(const unsigned char *)p & 0xf8) == 0xf0) /* 11110xxx */
+ {
+ min = (1 << 16);
+ val = *(const unsigned char *)p & 0x07;
+ }
+ else
+ goto error;
+
+ p++;
+ CONTINUATION_CHAR;
+ TWO_REMAINING:
+ p++;
+ CONTINUATION_CHAR;
+ p++;
+ CONTINUATION_CHAR;
+
+ if ( (val < min))
+ goto error;
+
+ if ( (!UNICODE_VALID(val)))
+ goto error;
+ }
+
+ continue;
+
+ error:
+ return NULL;
+ }
+ }
+
+ return str;
+}
diff --git a/avahi-common/utf8.h b/avahi-common/utf8.h
new file mode 100644
index 0000000..dc1ce4b
--- /dev/null
+++ b/avahi-common/utf8.h
@@ -0,0 +1,33 @@
+#ifndef fooutf8hfoo
+#define fooutf8hfoo
+
+/***
+ 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 <inttypes.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+const char *avahi_utf8_valid(const char *str);
+
+AVAHI_C_DECL_END
+
+#endif
diff --git a/avahi-common/watch-test.c b/avahi-common/watch-test.c
new file mode 100644
index 0000000..6c178ba
--- /dev/null
+++ b/avahi-common/watch-test.c
@@ -0,0 +1,115 @@
+/***
+ 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 <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include "watch.h"
+#include "timeval.h"
+#include "gccmacro.h"
+
+static const AvahiPoll *api = NULL;
+
+#ifndef USE_THREAD
+#include "simple-watch.h"
+static AvahiSimplePoll *simple_poll = NULL;
+#else
+#include "thread-watch.h"
+static AvahiThreadedPoll *threaded_poll = NULL;
+#endif
+
+static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
+
+ if (event & AVAHI_WATCH_IN) {
+ ssize_t r;
+ char c;
+
+ if ((r = read(fd, &c, 1)) <= 0) {
+ fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
+ api->watch_free(w);
+ return;
+ }
+
+ printf("Read: %c\n", c >= 32 && c < 127 ? c : '.');
+ }
+}
+
+static void wakeup(AvahiTimeout *t, AVAHI_GCC_UNUSED void *userdata) {
+ static int i = 0;
+ struct timeval tv;
+
+ printf("Wakeup #%i\n", i++);
+
+ if (i > 10) {
+#ifndef USE_THREAD
+ avahi_simple_poll_quit(simple_poll);
+#else
+ avahi_threaded_poll_quit(threaded_poll);
+#endif
+ } else {
+ avahi_elapse_time(&tv, 1000, 0);
+ api->timeout_update(t, &tv);
+ }
+}
+
+int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
+ struct timeval tv;
+
+#ifndef USE_THREAD
+ simple_poll = avahi_simple_poll_new();
+ assert(simple_poll);
+ api = avahi_simple_poll_get(simple_poll);
+ assert(api);
+#else
+ threaded_poll = avahi_threaded_poll_new();
+ assert(threaded_poll);
+ api = avahi_threaded_poll_get(threaded_poll);
+ assert(api);
+#endif
+
+ api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL);
+
+ avahi_elapse_time(&tv, 1000, 0);
+ api->timeout_new(api, &tv, wakeup, NULL);
+
+#ifndef USE_THREAD
+ /* Our main loop */
+ avahi_simple_poll_loop(simple_poll);
+ avahi_simple_poll_free(simple_poll);
+
+#else
+ avahi_threaded_poll_start(threaded_poll);
+
+ fprintf(stderr, "Now doing some stupid stuff ...\n");
+ sleep(20);
+ fprintf(stderr, "... stupid stuff is done.\n");
+
+ avahi_threaded_poll_free(threaded_poll);
+
+#endif
+
+ return 0;
+}
diff --git a/avahi-common/watch.h b/avahi-common/watch.h
new file mode 100644
index 0000000..86e63d3
--- /dev/null
+++ b/avahi-common/watch.h
@@ -0,0 +1,97 @@
+#ifndef foowatchhfoo
+#define foowatchhfoo
+
+/***
+ 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.
+***/
+
+/** \file watch.h Simplistic main loop abstraction */
+
+#include <sys/poll.h>
+#include <sys/time.h>
+
+#include <avahi-common/cdecl.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** An I/O watch object */
+typedef struct AvahiWatch AvahiWatch;
+
+/** A timeout watch object */
+typedef struct AvahiTimeout AvahiTimeout;
+
+/** An event polling abstraction object */
+typedef struct AvahiPoll AvahiPoll;
+
+/** Type of watch events */
+typedef enum {
+ AVAHI_WATCH_IN = POLLIN, /**< Input event */
+ AVAHI_WATCH_OUT = POLLOUT, /**< Output event */
+ AVAHI_WATCH_ERR = POLLERR, /**< Error event */
+ AVAHI_WATCH_HUP = POLLHUP /**< Hangup event */
+} AvahiWatchEvent;
+
+/** Called whenever an I/O event happens on an I/O watch */
+typedef void (*AvahiWatchCallback)(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata);
+
+/** Called when the timeout is reached */
+typedef void (*AvahiTimeoutCallback)(AvahiTimeout *t, void *userdata);
+
+/** Defines an abstracted event polling API. This may be used to
+ connect Avahi to other main loops. This is loosely based on Unix
+ poll(2). A consumer will call watch_new() for all file descriptors it
+ wants to listen for events on. In addition he can call timeout_new()
+ to define time based events .*/
+struct AvahiPoll {
+
+ /** Some abstract user data usable by the provider of the API */
+ void* userdata;
+
+ /** Create a new watch for the specified file descriptor and for
+ * the specified events. The API will call the callback function
+ * whenever any of the events happens. */
+ AvahiWatch* (*watch_new)(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata);
+
+ /** Update the events to wait for. It is safe to call this function from an AvahiWatchCallback */
+ void (*watch_update)(AvahiWatch *w, AvahiWatchEvent event);
+
+ /** Return the events that happened. It is safe to call this function from an AvahiWatchCallback */
+ AvahiWatchEvent (*watch_get_events)(AvahiWatch *w);
+
+ /** Free a watch. It is safe to call this function from an AvahiWatchCallback */
+ void (*watch_free)(AvahiWatch *w);
+
+ /** Set a wakeup time for the polling loop. The API will call the
+ callback function when the absolute time *tv is reached. If tv is
+ NULL, the timeout is disabled. After the timeout expired the
+ callback function will be called and the timeout is disabled. You
+ can reenable it by calling timeout_update() */
+ AvahiTimeout* (*timeout_new)(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata);
+
+ /** Update the absolute expiration time for a timeout, If tv is
+ * NULL, the timeout is disabled. It is safe to call this function from an AvahiTimeoutCallback */
+ void (*timeout_update)(AvahiTimeout *, const struct timeval *tv);
+
+ /** Free a timeout. It is safe to call this function from an AvahiTimeoutCallback */
+ void (*timeout_free)(AvahiTimeout *t);
+};
+
+AVAHI_C_DECL_END
+
+#endif
+