From 4f0a5e7572a4257894b4bfede42c26d65152609e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 13 Aug 2005 21:25:09 +0000 Subject: * strip glib from avahi-core * implement glib memory allocator * add new documentation file MALLOC * initialize pseudo-RNG from /dev/urandom in avahi-daemon * remove some gcc 4.0 warnings * beef up watch system with real timeouts * move GCC __attribute__ macros into its own header avahi-common/gccmacro.h * make use of GCC's sentinel attribute where it make sense * add malloc() implementations that abort on OOM and enable them by default git-svn-id: file:///home/lennart/svn/public/avahi/trunk@308 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/Makefile.am | 18 +- avahi-core/announce.c | 188 +++--- avahi-core/announce.h | 18 +- avahi-core/avahi-reflector.c | 29 +- avahi-core/avahi-test.c | 92 +-- avahi-core/browse-dns-server.c | 68 +- avahi-core/browse-domain.c | 58 +- avahi-core/browse-service-type.c | 48 +- avahi-core/browse-service.c | 58 +- avahi-core/browse.c | 158 ++--- avahi-core/browse.h | 2 +- avahi-core/cache.c | 186 +++--- avahi-core/cache.h | 19 +- avahi-core/conformance-test.c | 43 +- avahi-core/core.h | 233 ++++--- avahi-core/dns-test.c | 15 +- avahi-core/dns.c | 310 ++++----- avahi-core/dns.h | 83 ++- avahi-core/hashmap.c | 34 +- avahi-core/hashmap.h | 11 +- avahi-core/iface.c | 333 ++++++---- avahi-core/iface.h | 54 +- avahi-core/log.c | 14 +- avahi-core/log.h | 30 +- avahi-core/netlink.c | 160 +++-- avahi-core/netlink.h | 12 +- avahi-core/prioq-test.c | 36 +- avahi-core/prioq.c | 106 +-- avahi-core/prioq.h | 19 +- avahi-core/probe-sched.c | 153 +++-- avahi-core/probe-sched.h | 2 +- avahi-core/query-sched.c | 161 +++-- avahi-core/query-sched.h | 2 +- avahi-core/resolve-address.c | 85 ++- avahi-core/resolve-host-name.c | 55 +- avahi-core/resolve-service.c | 85 +-- avahi-core/response-sched.c | 174 ++--- avahi-core/response-sched.h | 4 +- avahi-core/rr.c | 39 +- avahi-core/rr.h | 6 +- avahi-core/rrlist.c | 4 +- avahi-core/server.c | 1348 ++++++++++++++++++++------------------ avahi-core/server.h | 59 +- avahi-core/socket.c | 128 ++-- avahi-core/socket.h | 25 +- avahi-core/timeeventq-test.c | 40 +- avahi-core/timeeventq.c | 238 +++---- avahi-core/timeeventq.h | 30 +- avahi-core/util.c | 9 +- 49 files changed, 2699 insertions(+), 2383 deletions(-) (limited to 'avahi-core') diff --git a/avahi-core/Makefile.am b/avahi-core/Makefile.am index e0e7d86..a300560 100644 --- a/avahi-core/Makefile.am +++ b/avahi-core/Makefile.am @@ -19,13 +19,12 @@ AM_CFLAGS=-I$(top_srcdir) -# GLIB 2.0 -AM_CFLAGS+=$(GLIB20_CFLAGS) -AM_LDADD=$(GLIB20_LIBS) - # Import stuff from avahi-common COMMON_LDADD=../avahi-common/libavahi-common.la +# Import stuff from avahi-glib +GLIB_LDADD=../avahi-glib/libavahi-glib.la + # This cool debug trap works on i386/gcc only AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' @@ -88,13 +87,13 @@ prioq_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) avahi_test_SOURCES = \ avahi-test.c -avahi_test_CFLAGS = $(AM_CFLAGS) -avahi_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) libavahi-core.la +avahi_test_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) +avahi_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) libavahi-core.la $(GLIB_LDADD) $(GLIB20_LIBS) conformance_test_SOURCES = \ conformance-test.c -conformance_test_CFLAGS = $(AM_CFLAGS) -conformance_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) libavahi-core.la +conformance_test_CFLAGS = $(AM_CFLAGS) $(GLIB20_CFLAGS) +conformance_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) libavahi-core.la $(GLIB_LDADD) $(GLIB20_LIBS) avahi_reflector_SOURCES = \ avahi-reflector.c @@ -114,7 +113,8 @@ dns_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) timeeventq_test_SOURCES = \ timeeventq-test.c \ timeeventq.h timeeventq.c \ - prioq.h prioq.c + prioq.h prioq.c \ + log.c log.h timeeventq_test_CFLAGS = $(AM_CFLAGS) timeeventq_test_LDADD = $(AM_LDADD) $(COMMON_LDADD) diff --git a/avahi-core/announce.c b/avahi-core/announce.c index 9a6d505..b6ded71 100644 --- a/avahi-core/announce.c +++ b/avahi-core/announce.c @@ -24,50 +24,53 @@ #endif #include +#include + #include "announce.h" +#include "log.h" #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250 #define AVAHI_PROBE_JITTER_MSEC 250 #define AVAHI_PROBE_INTERVAL_MSEC 250 static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) { - g_assert(s); - g_assert(a); + assert(s); + assert(a); if (a->time_event) - avahi_time_event_queue_remove(s->time_event_queue, a->time_event); + avahi_time_event_free(a->time_event); AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a); AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a); - g_free(a); + avahi_free(a); } static void elapse_announce(AvahiTimeEvent *e, void *userdata); static void set_timeout(AvahiAnnouncement *a, const struct timeval *tv) { - g_assert(a); + assert(a); if (!tv) { if (a->time_event) { - avahi_time_event_queue_remove(a->server->time_event_queue, a->time_event); + avahi_time_event_free(a->time_event); a->time_event = NULL; } } else { if (a->time_event) - avahi_time_event_queue_update(a->server->time_event_queue, a->time_event, tv); + avahi_time_event_update(a->time_event, tv); else - a->time_event = avahi_time_event_queue_add(a->server->time_event_queue, tv, elapse_announce, a); + a->time_event = avahi_time_event_new(a->server->time_event_queue, tv, elapse_announce, a); } } static void next_state(AvahiAnnouncement *a); -void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) { +void avahi_entry_group_check_probed(AvahiEntryGroup *g, int immediately) { AvahiEntry *e; - g_assert(g); - g_assert(!g->dead); + assert(g); + assert(!g->dead); /* Check whether all group members have been probed */ @@ -105,28 +108,28 @@ void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) { } static void next_state(AvahiAnnouncement *a) { - g_assert(a); + assert(a); /* avahi_log_debug("%i -- %u", a->state, a->n_iteration); */ if (a->state == AVAHI_WAITING) { - g_assert(a->entry->group); + assert(a->entry->group); - avahi_entry_group_check_probed(a->entry->group, TRUE); + avahi_entry_group_check_probed(a->entry->group, 1); } else if (a->state == AVAHI_PROBING) { if (a->n_iteration >= 4) { /* Probing done */ -/* gchar *t; */ +/* char *t; */ /* avahi_log_debug("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record)); */ -/* g_free(t); */ +/* avahi_free(t); */ if (a->entry->group) { - g_assert(a->entry->group->n_probing); + assert(a->entry->group->n_probing); a->entry->group->n_probing--; } @@ -142,7 +145,7 @@ static void next_state(AvahiAnnouncement *a) { } else { struct timeval tv; - avahi_interface_post_probe(a->interface, a->entry->record, FALSE); + avahi_interface_post_probe(a->interface, a->entry->record, 0); avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0); set_timeout(a, &tv); @@ -154,18 +157,18 @@ static void next_state(AvahiAnnouncement *a) { if (a->entry->flags & AVAHI_ENTRY_UNIQUE) /* Send the whole rrset at once */ - avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, FALSE); + avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, 0); else - avahi_server_prepare_response(a->server, a->interface, a->entry, FALSE, FALSE); + avahi_server_prepare_response(a->server, a->interface, a->entry, 0, 0); - avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, FALSE, FALSE); + avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0); if (++a->n_iteration >= 4) { -/* gchar *t; */ +/* char *t; */ /* Announcing done */ /* avahi_log_debug("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record)); */ -/* g_free(t); */ +/* avahi_free(t); */ a->state = AVAHI_ESTABLISHED; @@ -183,7 +186,7 @@ static void next_state(AvahiAnnouncement *a) { } static void elapse_announce(AvahiTimeEvent *e, void *userdata) { - g_assert(e); + assert(e); next_state(userdata); } @@ -191,9 +194,9 @@ static void elapse_announce(AvahiTimeEvent *e, void *userdata) { AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { AvahiAnnouncement *a; - g_assert(s); - g_assert(e); - g_assert(i); + assert(s); + assert(e); + assert(i); for (a = e->announcements; a; a = a->by_entry_next) if (a->interface == i) @@ -202,11 +205,11 @@ AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiIn return NULL; } -static void go_to_initial_state(AvahiAnnouncement *a, gboolean immediately) { +static void go_to_initial_state(AvahiAnnouncement *a, int immediately) { AvahiEntry *e; struct timeval tv; - g_assert(a); + assert(a); e = a->entry; if ((e->flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_NOPROBE)) @@ -239,24 +242,28 @@ static void go_to_initial_state(AvahiAnnouncement *a, gboolean immediately) { static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) { AvahiAnnouncement *a; -/* gchar *t; */ +/* char *t; */ - g_assert(s); - g_assert(i); - g_assert(e); - g_assert(!e->dead); + assert(s); + assert(i); + assert(e); + assert(!e->dead); /* avahi_log_debug("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */ -/* g_free(t); */ +/* avahi_free(t); */ - if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_commited(e)) + if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e)) return; /* We don't want duplicate announcements */ if (avahi_get_announcement(s, e, i)) return; - a = g_new(AvahiAnnouncement, 1); + if ((!(a = avahi_new(AvahiAnnouncement, 1)))) { + avahi_log_error(__FILE__": Out of memory."); + return; + } + a->server = s; a->interface = i; a->entry = e; @@ -265,17 +272,17 @@ static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) { AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a); AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a); - go_to_initial_state(a, FALSE); + go_to_initial_state(a, 0); /* avahi_log_debug("New announcement on interface %s.%i for entry [%s] state=%i", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record), a->state); */ -/* g_free(t); */ +/* avahi_free(t); */ } void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) { AvahiEntry *e; - g_assert(s); - g_assert(i); + assert(s); + assert(i); if (!i->announcing) return; @@ -285,21 +292,21 @@ void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) { new_announcement(s, i, e); } -static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { +static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { AvahiEntry *e = userdata; - g_assert(m); - g_assert(i); - g_assert(e); - g_assert(!e->dead); + assert(m); + assert(i); + assert(e); + assert(!e->dead); new_announcement(m->server, i, e); } void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) { - g_assert(s); - g_assert(e); - g_assert(!e->dead); + assert(s); + assert(e); + assert(!e->dead); avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e); } @@ -307,24 +314,24 @@ void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) { void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g) { AvahiEntry *e; - g_assert(s); - g_assert(g); + assert(s); + assert(g); for (e = g->entries; e; e = e->by_group_next) if (!e->dead) avahi_announce_entry(s, e); } -gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { +int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { AvahiAnnouncement *a; - g_assert(s); - g_assert(e); - g_assert(i); - g_assert(!e->dead); + assert(s); + assert(e); + assert(i); + assert(!e->dead); if (!(a = avahi_get_announcement(s, e, i))) - return FALSE; + return 0; return a->state == AVAHI_ANNOUNCING || @@ -332,16 +339,16 @@ gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i (a->state == AVAHI_WAITING && !(e->flags & AVAHI_ENTRY_UNIQUE)); } -gboolean avahi_entry_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { +int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { AvahiAnnouncement *a; - g_assert(s); - g_assert(e); - g_assert(i); - g_assert(!e->dead); + assert(s); + assert(e); + assert(i); + assert(!e->dead); if (!(a = avahi_get_announcement(s, e, i))) - return FALSE; + return 0; /* avahi_log_debug("state: %i", a->state); */ @@ -353,9 +360,9 @@ gboolean avahi_entry_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { AvahiAnnouncement *a; - g_assert(s); - g_assert(e); - g_assert(i); + assert(s); + assert(e); + assert(i); if (!(a = avahi_get_announcement(s, e, i))) return; @@ -363,33 +370,35 @@ void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInt if (a->state == AVAHI_PROBING && a->entry->group) a->entry->group->n_probing--; - go_to_initial_state(a, TRUE); + go_to_initial_state(a, 1); } static AvahiRecord *make_goodbye_record(AvahiRecord *r) { -/* gchar *t; */ +/* char *t; */ AvahiRecord *g; - g_assert(r); + assert(r); /* avahi_log_debug("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */ -/* g_free(t); */ +/* avahi_free(t); */ - g = avahi_record_copy(r); - g_assert(g->ref == 1); + if (!(g = avahi_record_copy(r))) + return NULL; /* OOM */ + + assert(g->ref == 1); g->ttl = 0; return g; } -static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { +static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { AvahiEntry *e = userdata; AvahiRecord *g; - g_assert(m); - g_assert(i); - g_assert(e); - g_assert(!e->dead); + assert(m); + assert(i); + assert(e); + assert(!e->dead); if (!avahi_interface_match(i, e->interface, e->protocol)) return; @@ -397,17 +406,19 @@ static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, g if (e->flags & AVAHI_ENTRY_NOANNOUNCE) return; - if (!avahi_entry_registered(m->server, e, i)) + if (!avahi_entry_is_registered(m->server, e, i)) return; - g = make_goodbye_record(e->record); - avahi_interface_post_response(i, g, e->flags & AVAHI_ENTRY_UNIQUE, NULL, TRUE); + if (!(g = make_goodbye_record(e->record))) + return; /* OOM */ + + avahi_interface_post_response(i, g, e->flags & AVAHI_ENTRY_UNIQUE, NULL, 1); avahi_record_unref(g); } -void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye) { - g_assert(s); - g_assert(i); +void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int goodbye) { + assert(s); + assert(i); /* avahi_log_debug("goodbye interface: %s.%u", i->hardware->name, i->protocol); */ @@ -423,12 +434,11 @@ void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye remove_announcement(s, i->announcements); /* avahi_log_debug("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */ - } -void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) { - g_assert(s); - g_assert(e); +void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int goodbye) { + assert(s); + assert(e); /* avahi_log_debug("goodbye entry: %p", e); */ @@ -442,10 +452,10 @@ void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) { } -void avahi_goodbye_all(AvahiServer *s, gboolean goodbye) { +void avahi_goodbye_all(AvahiServer *s, int goodbye) { AvahiEntry *e; - g_assert(s); + assert(s); /* avahi_log_debug("goodbye all"); */ diff --git a/avahi-core/announce.h b/avahi-core/announce.h index 9aee574..d5928be 100644 --- a/avahi-core/announce.h +++ b/avahi-core/announce.h @@ -22,8 +22,6 @@ USA. ***/ -#include - typedef struct AvahiAnnouncement AvahiAnnouncement; #include @@ -46,8 +44,8 @@ struct AvahiAnnouncement { AvahiTimeEvent *time_event; AvahiAnnouncementState state; - guint n_iteration; - guint sec_delay; + unsigned n_iteration; + unsigned sec_delay; AVAHI_LLIST_FIELDS(AvahiAnnouncement, by_interface); AVAHI_LLIST_FIELDS(AvahiAnnouncement, by_entry); @@ -59,15 +57,15 @@ void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g); void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); -void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately); +void avahi_entry_group_check_probed(AvahiEntryGroup *g, int immediately); -gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); -gboolean avahi_entry_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); +int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); +int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); -void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean send_goodbye); -void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean send_goodbye); +void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye); +void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye); -void avahi_goodbye_all(AvahiServer *s, gboolean send_goodbye); +void avahi_goodbye_all(AvahiServer *s, int send_goodbye); AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i); diff --git a/avahi-core/avahi-reflector.c b/avahi-core/avahi-reflector.c index 0a91b87..33ae7e1 100644 --- a/avahi-core/avahi-reflector.c +++ b/avahi-core/avahi-reflector.c @@ -28,29 +28,32 @@ #include #include +#include #include int main(int argc, char*argv[]) { AvahiServer *server; AvahiServerConfig config; - GMainLoop *loop; - gint error; + int error; + AvahiSimplePoll *simple_poll; + simple_poll = avahi_simple_poll_new(); + avahi_server_config_init(&config); - config.publish_hinfo = FALSE; - config.publish_addresses = FALSE; - config.publish_workstation = FALSE; - config.publish_domain = FALSE; - config.use_ipv6 = FALSE; - config.enable_reflector = TRUE; + config.publish_hinfo = 0; + config.publish_addresses = 0; + config.publish_workstation = 0; + config.publish_domain = 0; + config.use_ipv6 = 0; + config.enable_reflector = 1; - server = avahi_server_new(NULL, &config, NULL, NULL, &error); + server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error); avahi_server_config_free(&config); - loop = g_main_loop_new(NULL, FALSE); - - g_main_loop_run(loop); - g_main_loop_unref(loop); + for (;;) + if (avahi_simple_poll_iterate(simple_poll, -1) != 0) + break; avahi_server_free(server); + avahi_simple_poll_free(simple_poll); } diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c index 7156d06..cecd5bf 100644 --- a/avahi-core/avahi-test.c +++ b/avahi-core/avahi-test.c @@ -28,74 +28,79 @@ #include #include #include +#include +#include +#include +#include #include #include + static AvahiEntryGroup *group = NULL; static AvahiServer *server = NULL; -static gchar *service_name = NULL; +static char *service_name = NULL; -static gboolean quit_timeout(gpointer data) { +static int quit_timeout(void* data) { g_main_loop_quit(data); - return FALSE; + return 0; } -static void dump_line(const gchar *text, gpointer userdata) { +static void dump_line(const char *text, void* userdata) { printf("%s\n", text); } -static gboolean dump_timeout(gpointer data) { +static int dump_timeout(void* data) { AvahiServer *Avahi = data; avahi_server_dump(Avahi, dump_line, NULL); - return TRUE; + return 1; } -static void record_browser_callback(AvahiRecordBrowser *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { - gchar *t; +static void record_browser_callback(AvahiRecordBrowser *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { + char *t; - g_assert(r); - g_assert(record); - g_assert(interface > 0); - g_assert(protocol != AVAHI_PROTO_UNSPEC); + assert(r); + assert(record); + assert(interface > 0); + assert(protocol != AVAHI_PROTO_UNSPEC); avahi_log_debug("SUBSCRIPTION: record [%s] on %i.%i is %s", t = avahi_record_to_string(record), interface, protocol, event == AVAHI_BROWSER_NEW ? "new" : "remove"); - g_free(t); + avahi_free(t); } static void remove_entries(void); -static void create_entries(gboolean new_name); +static void create_entries(int new_name); -static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { +static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata) { avahi_log_debug("entry group state: %i", state); if (state == AVAHI_ENTRY_GROUP_COLLISION) { remove_entries(); - create_entries(TRUE); + create_entries(1); avahi_log_debug("Service name conflict, retrying with <%s>", service_name); } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) { avahi_log_debug("Service established under name <%s>", service_name); } } -static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) { +static void server_callback(AvahiServer *s, AvahiServerState state, void* userdata) { avahi_log_debug("server state: %i", state); if (state == AVAHI_SERVER_RUNNING) { avahi_log_debug("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s)); - create_entries(FALSE); + create_entries(0); } else if (state == AVAHI_SERVER_COLLISION) { - gchar *n; + char *n; remove_entries(); n = avahi_alternative_host_name(avahi_server_get_host_name(s)); avahi_log_debug("Host name conflict, retrying with <%s>", n); avahi_server_set_host_name(s, n); - g_free(n); + avahi_free(n); } } @@ -104,7 +109,7 @@ static void remove_entries(void) { avahi_entry_group_reset(group); } -static void create_entries(gboolean new_name) { +static void create_entries(int new_name) { AvahiAddress a; remove_entries(); @@ -112,13 +117,13 @@ static void create_entries(gboolean new_name) { if (!group) group = avahi_entry_group_new(server, entry_group_callback, NULL); - g_assert(avahi_entry_group_is_empty(group)); + assert(avahi_entry_group_is_empty(group)); if (!service_name) service_name = g_strdup("Test Service"); else if (new_name) { - gchar *n = avahi_alternative_service_name(service_name); - g_free(service_name); + char *n = avahi_alternative_service_name(service_name); + avahi_free(service_name); service_name = n; } @@ -152,8 +157,8 @@ fail: group = NULL; } -static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) { - gchar t[64]; +static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const char *hostname, const AvahiAddress *a, void* userdata) { + char t[64]; if (a) avahi_address_snprint(t, sizeof(t), a); @@ -161,45 +166,45 @@ static void hnr_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProt avahi_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout"); } -static void ar_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) { - gchar t[64]; +static void ar_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, const char *hostname, void* userdata) { + char t[64]; avahi_address_snprint(t, sizeof(t), a); avahi_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", event == AVAHI_RESOLVER_FOUND ? "found" : "timeout"); } -static void db_callback(AvahiDomainBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) { +static void db_callback(AvahiDomainBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) { avahi_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } -static void stb_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *service_type, const gchar *domain, gpointer userdata) { +static void stb_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *service_type, const char *domain, void* userdata) { avahi_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } -static void sb_callback(AvahiServiceBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *service_type, const gchar *domain, gpointer userdata) { +static void sb_callback(AvahiServiceBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *service_type, const char *domain, void* userdata) { avahi_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } -static void sr_callback(AvahiServiceResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *name, const gchar*service_type, const gchar*domain_name, const gchar*hostname, const AvahiAddress *a, guint16 port, AvahiStringList *txt, gpointer userdata) { +static void sr_callback(AvahiServiceResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char*service_type, const char*domain_name, const char*hostname, const AvahiAddress *a, uint16_t port, AvahiStringList *txt, void* userdata) { if (event == AVAHI_RESOLVER_TIMEOUT) avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s> [timeout]", iface, protocol, name, service_type, domain_name); else { - gchar t[64], *s; + char t[64], *s; avahi_address_snprint(t, sizeof(t), a); s = avahi_string_list_to_string(txt); avahi_log_debug("SR: (%i.%i) <%s> as %s in <%s>: %s/%s:%i (%s) [found]", iface, protocol, name, service_type, domain_name, hostname, t, port, s); - g_free(s); + avahi_free(s); } } -static void dsb_callback(AvahiDNSServerBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar*hostname, const AvahiAddress *a, guint16 port, gpointer userdata) { - gchar t[64]; +static void dsb_callback(AvahiDNSServerBrowser *b, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const char*hostname, const AvahiAddress *a, uint16_t port, void* userdata) { + char t[64]; avahi_address_snprint(t, sizeof(t), a); avahi_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname, t, port, event == AVAHI_BROWSER_NEW ? "new" : "remove"); } @@ -217,11 +222,15 @@ int main(int argc, char *argv[]) { AvahiServiceBrowser *sb; AvahiServiceResolver *sr; AvahiDNSServerBrowser *dsb; - gint error; + AvahiGLibPoll *glib_poll; + int error; + + avahi_set_allocator(avahi_glib_allocator()); + glib_poll = avahi_glib_poll_new(NULL); avahi_server_config_init(&config); /* config.host_name = g_strdup("test"); */ - server = avahi_server_new(NULL, &config, server_callback, NULL, &error); + server = avahi_server_new(avahi_glib_poll_get(glib_poll), &config, server_callback, NULL, &error); avahi_server_config_free(&config); k = avahi_key_new("_http._tcp.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); @@ -246,7 +255,7 @@ int main(int argc, char *argv[]) { g_timeout_add(1000*5, dump_timeout, server); g_timeout_add(1000*60, quit_timeout, loop); - loop = g_main_loop_new(NULL, FALSE); + loop = g_main_loop_new(NULL, 0); g_main_loop_run(loop); g_main_loop_unref(loop); @@ -264,7 +273,10 @@ int main(int argc, char *argv[]) { if (server) avahi_server_free(server); - g_free(service_name); + if (glib_poll) + avahi_glib_poll_free(glib_poll); + + avahi_free(service_name); return 0; } diff --git a/avahi-core/browse-dns-server.c b/avahi-core/browse-dns-server.c index 8f87887..720b491 100644 --- a/avahi-core/browse-dns-server.c +++ b/avahi-core/browse-dns-server.c @@ -26,6 +26,8 @@ #include #include +#include + #include "browse.h" #include "log.h" #include "rr.h" @@ -46,14 +48,14 @@ struct AvahiDNSServerInfo { struct AvahiDNSServerBrowser { AvahiServer *server; - gchar *domain_name; + char *domain_name; AvahiRecordBrowser *record_browser; AvahiDNSServerBrowserCallback callback; - gpointer userdata; + void* userdata; AvahiProtocol aprotocol; - guint n_info; + unsigned n_info; AVAHI_LLIST_FIELDS(AvahiDNSServerBrowser, browser); AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info); @@ -62,8 +64,8 @@ struct AvahiDNSServerBrowser { static AvahiDNSServerInfo* get_server_info(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) { AvahiDNSServerInfo *i; - g_assert(b); - g_assert(r); + assert(b); + assert(r); for (i = b->info; i; i = i->info_next) if (i->interface == interface && @@ -75,8 +77,8 @@ static AvahiDNSServerInfo* get_server_info(AvahiDNSServerBrowser *b, AvahiIfInde } static void server_info_free(AvahiDNSServerBrowser *b, AvahiDNSServerInfo *i) { - g_assert(b); - g_assert(i); + assert(b); + assert(i); avahi_record_unref(i->srv_record); if (i->host_name_resolver) @@ -84,18 +86,18 @@ static void server_info_free(AvahiDNSServerBrowser *b, AvahiDNSServerInfo *i) { AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i); - g_assert(b->n_info >= 1); + assert(b->n_info >= 1); b->n_info--; - g_free(i); + avahi_free(i); } -static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) { +static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) { AvahiDNSServerInfo *i = userdata; - g_assert(r); - g_assert(host_name); - g_assert(i); + assert(r); + assert(host_name); + assert(i); if (event == AVAHI_RESOLVER_FOUND) { i->address = *a; @@ -107,14 +109,14 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex i i->host_name_resolver = NULL; } -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiDNSServerBrowser *b = userdata; - g_assert(rr); - g_assert(record); - g_assert(b); + assert(rr); + assert(record); + assert(b); - g_assert(record->key->type == AVAHI_DNS_TYPE_SRV); + assert(record->key->type == AVAHI_DNS_TYPE_SRV); if (event == AVAHI_BROWSER_NEW) { AvahiDNSServerInfo *i; @@ -125,7 +127,9 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac if (b->n_info >= 10) return; - i = g_new(AvahiDNSServerInfo, 1); + if (!(i = avahi_new(AvahiDNSServerInfo, 1))) + return; /* OOM */ + i->browser = b; i->interface = interface; i->protocol = protocol; @@ -148,21 +152,25 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac } } -AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiDNSServerBrowserCallback callback, gpointer userdata) { +AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiDNSServerBrowserCallback callback, void* userdata) { AvahiDNSServerBrowser *b; AvahiKey *k; - gchar *n = NULL; + char *n = NULL; - g_assert(server); - g_assert(callback); - g_assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE); + assert(server); + assert(callback); + assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE); if (domain && !avahi_is_valid_domain_name(domain)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME); return NULL; } - b = g_new(AvahiDNSServerBrowser, 1); + if (!(b = avahi_new(AvahiDNSServerBrowser, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + b->server = server; b->domain_name = avahi_normalize_name(domain ? domain : "local"); b->callback = callback; @@ -173,9 +181,9 @@ AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIf AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info); AVAHI_LLIST_PREPEND(AvahiDNSServerBrowser, browser, server->dns_server_browsers, b); - n = g_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name); + n = avahi_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name); k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); - g_free(n); + avahi_free(n); b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b); avahi_key_unref(k); @@ -189,7 +197,7 @@ AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIf } void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b) { - g_assert(b); + assert(b); while (b->info) server_info_free(b, b->info); @@ -198,7 +206,7 @@ void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b) { if (b->record_browser) avahi_record_browser_free(b->record_browser); - g_free(b->domain_name); - g_free(b); + avahi_free(b->domain_name); + avahi_free(b); } diff --git a/avahi-core/browse-domain.c b/avahi-core/browse-domain.c index 09e1e50..44ff291 100644 --- a/avahi-core/browse-domain.c +++ b/avahi-core/browse-domain.c @@ -24,50 +24,56 @@ #endif #include +#include + #include "browse.h" struct AvahiDomainBrowser { AvahiServer *server; - gchar *domain_name; + char *domain_name; AvahiRecordBrowser *record_browser; AvahiDomainBrowserCallback callback; - gpointer userdata; + void* userdata; AVAHI_LLIST_FIELDS(AvahiDomainBrowser, browser); }; -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiDomainBrowser *b = userdata; - gchar *n; + char *n; - g_assert(rr); - g_assert(record); - g_assert(b); + assert(rr); + assert(record); + assert(b); - g_assert(record->key->type == AVAHI_DNS_TYPE_PTR); + assert(record->key->type == AVAHI_DNS_TYPE_PTR); n = avahi_normalize_name(record->data.ptr.name); b->callback(b, interface, protocol, event, n, b->userdata); - g_free(n); + avahi_free(n); } -AvahiDomainBrowser *avahi_domain_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *domain, AvahiDomainBrowserType type, AvahiDomainBrowserCallback callback, gpointer userdata) { +AvahiDomainBrowser *avahi_domain_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType type, AvahiDomainBrowserCallback callback, void* userdata) { AvahiDomainBrowser *b; AvahiKey *k; - gchar *n = NULL; + char *n = NULL; - g_assert(server); - g_assert(callback); - g_assert(type >= AVAHI_DOMAIN_BROWSER_BROWSE && type <= AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY); + assert(server); + assert(callback); + assert(type >= AVAHI_DOMAIN_BROWSER_BROWSE && type <= AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY); if (domain && !avahi_is_valid_domain_name(domain)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME); return NULL; } - b = g_new(AvahiDomainBrowser, 1); + if (!(b = avahi_new(AvahiDomainBrowser, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + b->server = server; b->domain_name = avahi_normalize_name(domain ? domain : "local"); b->callback = callback; @@ -77,30 +83,30 @@ AvahiDomainBrowser *avahi_domain_browser_new(AvahiServer *server, AvahiIfIndex i switch (type) { case AVAHI_DOMAIN_BROWSER_BROWSE: - n = g_strdup_printf("b._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("b._dns-sd._udp.%s", b->domain_name); break; case AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT: - n = g_strdup_printf("db._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("db._dns-sd._udp.%s", b->domain_name); break; case AVAHI_DOMAIN_BROWSER_REGISTER: - n = g_strdup_printf("r._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("r._dns-sd._udp.%s", b->domain_name); break; case AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT: - n = g_strdup_printf("dr._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("dr._dns-sd._udp.%s", b->domain_name); break; case AVAHI_DOMAIN_BROWSER_BROWSE_LEGACY: - n = g_strdup_printf("lb._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("lb._dns-sd._udp.%s", b->domain_name); break; case AVAHI_DOMAIN_BROWSER_MAX: - g_assert(FALSE); + assert(0); break; } - g_assert(n); + assert(n); k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - g_free(n); + avahi_free(n); b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b); avahi_key_unref(k); @@ -114,13 +120,13 @@ AvahiDomainBrowser *avahi_domain_browser_new(AvahiServer *server, AvahiIfIndex i } void avahi_domain_browser_free(AvahiDomainBrowser *b) { - g_assert(b); + assert(b); AVAHI_LLIST_REMOVE(AvahiDomainBrowser, browser, b->server->domain_browsers, b); if (b->record_browser) avahi_record_browser_free(b->record_browser); - g_free(b->domain_name); - g_free(b); + avahi_free(b->domain_name); + avahi_free(b); } diff --git a/avahi-core/browse-service-type.c b/avahi-core/browse-service-type.c index 61364e9..303182e 100644 --- a/avahi-core/browse-service-type.c +++ b/avahi-core/browse-service-type.c @@ -26,30 +26,32 @@ #include #include +#include + #include "browse.h" #include "log.h" struct AvahiServiceTypeBrowser { AvahiServer *server; - gchar *domain_name; + char *domain_name; AvahiRecordBrowser *record_browser; AvahiServiceTypeBrowserCallback callback; - gpointer userdata; + void* userdata; AVAHI_LLIST_FIELDS(AvahiServiceTypeBrowser, browser); }; -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiServiceTypeBrowser *b = userdata; - gchar *n, *e, *c; + char *n, *e, *c; - g_assert(rr); - g_assert(record); - g_assert(b); + assert(rr); + assert(record); + assert(b); - g_assert(record->key->type == AVAHI_DNS_TYPE_PTR); + assert(record->key->type == AVAHI_DNS_TYPE_PTR); n = avahi_normalize_name(record->data.ptr.name); @@ -62,7 +64,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac if (*c == 0) goto fail; - g_assert(*c == '.'); + assert(*c == '.'); e = c; c++; } @@ -73,29 +75,33 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac goto fail; b->callback(b, interface, protocol, event, n, c, b->userdata); - g_free(n); + avahi_free(n); return; fail: avahi_log_warn("Invalid service type '%s'", n); - g_free(n); + avahi_free(n); } -AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gint interface, AvahiProtocol protocol, const gchar *domain, AvahiServiceTypeBrowserCallback callback, gpointer userdata) { +AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void* userdata) { AvahiServiceTypeBrowser *b; AvahiKey *k; - gchar *n = NULL; + char *n = NULL; - g_assert(server); - g_assert(callback); + assert(server); + assert(callback); if (domain && !avahi_is_valid_domain_name(domain)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME); return NULL; } - b = g_new(AvahiServiceTypeBrowser, 1); + if (!(b = avahi_new(AvahiServiceTypeBrowser, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + b->server = server; b->domain_name = avahi_normalize_name(domain ? domain : "local"); b->callback = callback; @@ -103,9 +109,9 @@ AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gin AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, browser, server->service_type_browsers, b); - n = g_strdup_printf("_services._dns-sd._udp.%s", b->domain_name); + n = avahi_strdup_printf("_services._dns-sd._udp.%s", b->domain_name); k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - g_free(n); + avahi_free(n); b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b); avahi_key_unref(k); @@ -117,15 +123,15 @@ AvahiServiceTypeBrowser *avahi_service_type_browser_new(AvahiServer *server, gin } void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b) { - g_assert(b); + assert(b); AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, browser, b->server->service_type_browsers, b); if (b->record_browser) avahi_record_browser_free(b->record_browser); - g_free(b->domain_name); - g_free(b); + avahi_free(b->domain_name); + avahi_free(b); } diff --git a/avahi-core/browse-service.c b/avahi-core/browse-service.c index 8b3aa4c..7ed6ded 100644 --- a/avahi-core/browse-service.c +++ b/avahi-core/browse-service.c @@ -26,36 +26,38 @@ #include #include +#include + #include "browse.h" #include "log.h" struct AvahiServiceBrowser { AvahiServer *server; - gchar *domain_name; - gchar *service_type; + char *domain_name; + char *service_type; AvahiRecordBrowser *record_browser; AvahiServiceBrowserCallback callback; - gpointer userdata; + void* userdata; AVAHI_LLIST_FIELDS(AvahiServiceBrowser, browser); }; -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiServiceBrowser *b = userdata; - gchar *n, *e, *c, *s; - gchar service[128]; + char *n, *e, *c, *s; + char service[128]; - g_assert(rr); - g_assert(record); - g_assert(b); + assert(rr); + assert(record); + assert(b); - g_assert(record->key->type == AVAHI_DNS_TYPE_PTR); + assert(record->key->type == AVAHI_DNS_TYPE_PTR); c = n = avahi_normalize_name(record->data.ptr.name); - if (!(avahi_unescape_label((const gchar**) &c, service, sizeof(service)))) + if (!(avahi_unescape_label((const char**) &c, service, sizeof(service)))) goto fail; for (s = e = c; *c == '_';) { @@ -64,7 +66,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac if (*c == 0) goto fail; - g_assert(*c == '.'); + assert(*c == '.'); e = c; c++; } @@ -75,23 +77,23 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac goto fail; b->callback(b, interface, protocol, event, service, s, c, b->userdata); - g_free(n); + avahi_free(n); return; fail: avahi_log_warn("Invalid service '%s'", n); - g_free(n); + avahi_free(n); } -AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *service_type, const gchar *domain, AvahiServiceBrowserCallback callback, gpointer userdata) { +AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *service_type, const char *domain, AvahiServiceBrowserCallback callback, void* userdata) { AvahiServiceBrowser *b; AvahiKey *k; - gchar *n = NULL; + char *n = NULL; - g_assert(server); - g_assert(callback); - g_assert(service_type); + assert(server); + assert(callback); + assert(service_type); if (!avahi_is_valid_service_type(service_type)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE); @@ -103,7 +105,11 @@ AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex return NULL; } - b = g_new(AvahiServiceBrowser, 1); + if (!(b = avahi_new(AvahiServiceBrowser, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + b->server = server; b->domain_name = avahi_normalize_name(domain ? domain : "local"); b->service_type = avahi_normalize_name(service_type); @@ -111,9 +117,9 @@ AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex b->userdata = userdata; AVAHI_LLIST_PREPEND(AvahiServiceBrowser, browser, server->service_browsers, b); - n = g_strdup_printf("%s.%s", b->service_type, b->domain_name); + n = avahi_strdup_printf("%s.%s", b->service_type, b->domain_name); k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - g_free(n); + avahi_free(n); b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b); @@ -128,14 +134,14 @@ AvahiServiceBrowser *avahi_service_browser_new(AvahiServer *server, AvahiIfIndex } void avahi_service_browser_free(AvahiServiceBrowser *b) { - g_assert(b); + assert(b); AVAHI_LLIST_REMOVE(AvahiServiceBrowser, browser, b->server->service_browsers, b); if (b->record_browser) avahi_record_browser_free(b->record_browser); - g_free(b->domain_name); - g_free(b->service_type); - g_free(b); + avahi_free(b->domain_name); + avahi_free(b->service_type); + avahi_free(b); } diff --git a/avahi-core/browse.c b/avahi-core/browse.c index f089cdd..d54e4ee 100644 --- a/avahi-core/browse.c +++ b/avahi-core/browse.c @@ -24,34 +24,36 @@ #endif #include +#include + #include "browse.h" #include "log.h" struct AvahiRecordBrowser { - gboolean dead; + int dead; AvahiServer *server; AvahiKey *key; AvahiIfIndex interface; AvahiProtocol protocol; - guint sec_delay; + unsigned sec_delay; - AvahiTimeEvent *time_event; + AvahiTimeEvent *query_time_event; + AvahiTimeEvent *scan_time_event; AvahiRecordBrowserCallback callback; - gpointer userdata; - guint scan_idle_source; + void* userdata; AVAHI_LLIST_FIELDS(AvahiRecordBrowser, browser); AVAHI_LLIST_FIELDS(AvahiRecordBrowser, by_key); }; -static void elapse(AvahiTimeEvent *e, void *userdata) { +static void elapse_callback(AvahiTimeEvent *e, void *userdata) { AvahiRecordBrowser *s = userdata; struct timeval tv; -/* gchar *t; */ +/* char *t; */ - g_assert(s); + assert(s); avahi_server_post_query(s->server, s->interface, s->protocol, s->key); @@ -61,10 +63,10 @@ static void elapse(AvahiTimeEvent *e, void *userdata) { s->sec_delay = 60*60; /* avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay); */ -/* g_free(t); */ +/* avahi_free(t); */ avahi_elapse_time(&tv, s->sec_delay*1000, 0); - avahi_time_event_queue_update(s->server->time_event_queue, s->time_event, &tv); + avahi_time_event_update(s->query_time_event, &tv); } struct cbdata { @@ -72,13 +74,13 @@ struct cbdata { AvahiInterface *interface; }; -static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) { +static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) { struct cbdata *cbdata = userdata; - g_assert(c); - g_assert(pattern); - g_assert(e); - g_assert(cbdata); + assert(c); + assert(pattern); + assert(e); + assert(cbdata); if (cbdata->record_browser->dead) return NULL; @@ -94,35 +96,39 @@ static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCache return NULL; } -static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { - AvahiRecordBrowser *s = userdata; - struct cbdata cbdata = { s, i }; +static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { + AvahiRecordBrowser *b = userdata; + struct cbdata cbdata = { b, i }; - g_assert(m); - g_assert(i); - g_assert(s); + assert(m); + assert(i); + assert(b); - avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata); + if (!b->dead) + avahi_cache_walk(i->cache, b->key, scan_cache_callback, &cbdata); } -static gboolean scan_idle_callback(gpointer data) { - AvahiRecordBrowser *b = data; - g_assert(b); +static void scan_callback(AvahiTimeEvent *e, void *userdata) { + AvahiRecordBrowser *b = userdata; + assert(b); /* Scan the caches */ - avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b); - b->scan_idle_source = (guint) -1; + if (!b->dead) + avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b); - return FALSE; + if (b->scan_time_event) { + avahi_time_event_free(b->scan_time_event); + b->scan_time_event = NULL; + } } -AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, gpointer userdata) { +AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, void* userdata) { AvahiRecordBrowser *b, *t; struct timeval tv; - g_assert(server); - g_assert(key); - g_assert(callback); + assert(server); + assert(key); + assert(callback); if (avahi_key_is_pattern(key)) { avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN); @@ -134,8 +140,12 @@ AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex i return NULL; } - b = g_new(AvahiRecordBrowser, 1); - b->dead = FALSE; + if (!(b = avahi_new(AvahiRecordBrowser, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + + b->dead = 0; b->server = server; b->key = avahi_key_ref(key); b->interface = interface; @@ -147,68 +157,68 @@ AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex i avahi_server_post_query(b->server, b->interface, b->protocol, b->key); avahi_elapse_time(&tv, b->sec_delay*1000, 0); - b->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, b); + b->query_time_event = avahi_time_event_new(server->time_event_queue, &tv, elapse_callback, b); AVAHI_LLIST_PREPEND(AvahiRecordBrowser, browser, server->record_browsers, b); /* Add the new entry to the record_browser hash table */ - t = g_hash_table_lookup(server->record_browser_hashtable, key); + t = avahi_hashmap_lookup(server->record_browser_hashmap, key); AVAHI_LLIST_PREPEND(AvahiRecordBrowser, by_key, t, b); - g_hash_table_replace(server->record_browser_hashtable, key, t); + avahi_hashmap_replace(server->record_browser_hashmap, key, t); /* The currenlty cached entries are scanned a bit later */ - b->scan_idle_source = g_idle_add_full(G_PRIORITY_HIGH, scan_idle_callback, b, NULL); + b->scan_time_event = avahi_time_event_new(server->time_event_queue, NULL, scan_callback, b); + assert(b->scan_time_event); return b; } void avahi_record_browser_free(AvahiRecordBrowser *b) { - g_assert(b); - g_assert(!b->dead); - - b->dead = TRUE; - b->server->need_browser_cleanup = TRUE; + assert(b); + assert(!b->dead); - if (b->time_event) { - avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event); - b->time_event = NULL; + b->dead = 1; + b->server->need_browser_cleanup = 1; - if (b->scan_idle_source != (guint) -1) { - g_source_remove(b->scan_idle_source); - b->scan_idle_source = (guint) -1; - } + if (b->query_time_event) { + avahi_time_event_free(b->query_time_event); + b->query_time_event = NULL; + } + if (b->scan_time_event) { + avahi_time_event_free(b->scan_time_event); + b->scan_time_event = NULL; } } void avahi_record_browser_destroy(AvahiRecordBrowser *b) { AvahiRecordBrowser *t; - g_assert(b); + assert(b); AVAHI_LLIST_REMOVE(AvahiRecordBrowser, browser, b->server->record_browsers, b); - t = g_hash_table_lookup(b->server->record_browser_hashtable, b->key); + t = avahi_hashmap_lookup(b->server->record_browser_hashmap, b->key); AVAHI_LLIST_REMOVE(AvahiRecordBrowser, by_key, t, b); if (t) - g_hash_table_replace(b->server->record_browser_hashtable, t->key, t); + avahi_hashmap_replace(b->server->record_browser_hashmap, t->key, t); else - g_hash_table_remove(b->server->record_browser_hashtable, b->key); + avahi_hashmap_remove(b->server->record_browser_hashmap, b->key); - if (b->time_event) - avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event); - avahi_key_unref(b->key); + if (b->query_time_event) + avahi_time_event_free(b->query_time_event); + if (b->scan_time_event) + avahi_time_event_free(b->scan_time_event); - if (b->scan_idle_source != (guint) -1) - g_source_remove(b->scan_idle_source); + avahi_key_unref(b->key); - g_free(b); + avahi_free(b); } void avahi_browser_cleanup(AvahiServer *server) { AvahiRecordBrowser *b; AvahiRecordBrowser *n; - g_assert(server); + assert(server); for (b = server->record_browsers; b; b = n) { n = b->browser_next; @@ -217,39 +227,39 @@ void avahi_browser_cleanup(AvahiServer *server) { avahi_record_browser_destroy(b); } - server->need_browser_cleanup = FALSE; + server->need_browser_cleanup = 0; } void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) { AvahiRecordBrowser *b; - g_assert(server); - g_assert(record); + assert(server); + assert(record); - for (b = g_hash_table_lookup(server->record_browser_hashtable, record->key); b; b = b->by_key_next) + for (b = avahi_hashmap_lookup(server->record_browser_hashmap, record->key); b; b = b->by_key_next) if (!b->dead && avahi_interface_match(i, b->interface, b->protocol)) b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata); } -gboolean avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) { +int avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) { AvahiRecordBrowser *b; - g_assert(server); - g_assert(k); + assert(server); + assert(k); - for (b = g_hash_table_lookup(server->record_browser_hashtable, k); b; b = b->by_key_next) + for (b = avahi_hashmap_lookup(server->record_browser_hashmap, k); b; b = b->by_key_next) if (!b->dead && avahi_interface_match(i, b->interface, b->protocol)) - return TRUE; + return 1; - return FALSE; + return 0; } void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) { AvahiRecordBrowser *b; - g_assert(s); - g_assert(i); + assert(s); + assert(i); for (b = s->record_browsers; b; b = b->browser_next) if (avahi_interface_match(i, b->interface, b->protocol)) - avahi_interface_post_query(i, b->key, FALSE); + avahi_interface_post_query(i, b->key, 0); } diff --git a/avahi-core/browse.h b/avahi-core/browse.h index 2c9a2ab..21f102a 100644 --- a/avahi-core/browse.h +++ b/avahi-core/browse.h @@ -30,7 +30,7 @@ void avahi_browser_cleanup(AvahiServer *server); void avahi_browser_notify(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event); -gboolean avahi_is_subscribed(AvahiServer *s, AvahiInterface *i, AvahiKey *k); +int avahi_is_subscribed(AvahiServer *s, AvahiInterface *i, AvahiKey *k); void avahi_record_browser_destroy(AvahiRecordBrowser *b); diff --git a/avahi-core/cache.c b/avahi-core/cache.c index ebe78fc..924b8c9 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -24,8 +24,11 @@ #endif #include +#include #include +#include + #include "cache.h" #include "log.h" @@ -34,42 +37,51 @@ static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) { AvahiCacheEntry *t; - g_assert(c); - g_assert(e); + assert(c); + assert(e); /* avahi_log_debug("removing from cache: %p %p", c, e); */ /* Remove from hash table */ - t = g_hash_table_lookup(c->hash_table, e->record->key); + t = avahi_hashmap_lookup(c->hashmap, e->record->key); AVAHI_LLIST_REMOVE(AvahiCacheEntry, by_key, t, e); if (t) - g_hash_table_replace(c->hash_table, t->record->key, t); + avahi_hashmap_replace(c->hashmap, t->record->key, t); else - g_hash_table_remove(c->hash_table, e->record->key); + avahi_hashmap_remove(c->hashmap, e->record->key); /* Remove from linked list */ AVAHI_LLIST_REMOVE(AvahiCacheEntry, entry, c->entries, e); if (e->time_event) - avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event); + avahi_time_event_free(e->time_event); avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_REMOVE); avahi_record_unref(e->record); - g_free(e); + avahi_free(e); - g_assert(c->n_entries-- >= 1); + assert(c->n_entries-- >= 1); } AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { AvahiCache *c; - g_assert(server); + assert(server); - c = g_new(AvahiCache, 1); + if (!(c = avahi_new(AvahiCache, 1))) { + avahi_log_error(__FILE__": Out of memory."); + return NULL; /* OOM */ + } + c->server = server; c->interface = iface; - c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); + + if (!(c->hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL))) { + avahi_log_error(__FILE__": Out of memory."); + avahi_free(c); + return NULL; /* OOM */ + } AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries); c->n_entries = 0; @@ -78,32 +90,32 @@ AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { } void avahi_cache_free(AvahiCache *c) { - g_assert(c); + assert(c); while (c->entries) remove_entry(c, c->entries); - g_assert(c->n_entries == 0); + assert(c->n_entries == 0); - g_hash_table_destroy(c->hash_table); + avahi_hashmap_free(c->hashmap); - g_free(c); + avahi_free(c); } AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) { - g_assert(c); - g_assert(k); + assert(c); + assert(k); - g_assert(!avahi_key_is_pattern(k)); + assert(!avahi_key_is_pattern(k)); - return g_hash_table_lookup(c->hash_table, k); + return avahi_hashmap_lookup(c->hashmap, k); } -gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata) { - gpointer ret; +void* avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, void* userdata) { + void* ret; - g_assert(c); - g_assert(pattern); - g_assert(cb); + assert(c); + assert(pattern); + assert(cb); if (avahi_key_is_pattern(pattern)) { AvahiCacheEntry *e, *n; @@ -130,10 +142,10 @@ gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallba return NULL; } -static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { - g_assert(c); - g_assert(pattern); - g_assert(e); +static void* lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { + assert(c); + assert(pattern); + assert(e); if (avahi_record_equal_no_ttl(e->record, userdata)) return e; @@ -142,20 +154,20 @@ static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCa } AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) { - g_assert(c); - g_assert(r); + assert(c); + assert(r); return avahi_cache_walk(c, r->key, lookup_record_callback, r); } -static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent); +static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent); static void elapse_func(AvahiTimeEvent *t, void *userdata) { AvahiCacheEntry *e = userdata; -/* gchar *txt; */ +/* char *txt; */ - g_assert(t); - g_assert(e); + assert(t); + assert(e); /* txt = avahi_record_to_string(e->record); */ @@ -163,7 +175,7 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { remove_entry(e->cache, e); /* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */ } else { - guint percent = 0; + unsigned percent = 0; switch (e->state) { case AVAHI_CACHE_VALID: @@ -189,44 +201,44 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) { ; } - g_assert(percent > 0); + assert(percent > 0); /* Request a cache update, if we are subscribed to this entry */ if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) { /* avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */ - avahi_interface_post_query(e->cache->interface, e->record->key, TRUE); + avahi_interface_post_query(e->cache->interface, e->record->key, 1); } /* Check again later */ next_expiry(e->cache, e, percent); } -/* g_free(txt); */ +/* avahi_free(txt); */ } static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) { - g_assert(c); - g_assert(e); + assert(c); + assert(e); if (e->time_event) - avahi_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry); + avahi_time_event_update(e->time_event, &e->expiry); else - e->time_event = avahi_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e); + e->time_event = avahi_time_event_new(c->server->time_event_queue, &e->expiry, elapse_func, e); } -static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) { - AvahiUsec usec; - g_assert(c); - g_assert(e); - g_assert(percent > 0 && percent <= 100); -/* gchar *txt; */ +static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) { + AvahiUsec usec, left, right; + + assert(c); + assert(e); + assert(percent > 0 && percent <= 100); + + usec = (AvahiUsec) e->record->ttl * 10000; - usec = ((AvahiUsec) e->record->ttl) * 10000; + left = usec * percent; + right = usec * (percent+2); /* 2% jitter */ - /* 2% jitter */ - usec = (AvahiUsec) g_random_double_range((gdouble) (usec*percent), (gdouble) (usec*(percent+2))); -/* g_message("next expiry: %lli (%s)", usec / 1000000, txt = avahi_record_to_string(e->record)); */ -/* g_free(txt); */ + usec = left + (AvahiUsec) ((double) (right-left) * rand() / (RAND_MAX+1.0)); e->expiry = e->timestamp; avahi_timeval_add(&e->expiry, usec); @@ -237,8 +249,8 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) { } static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) { - g_assert(c); - g_assert(e); + assert(c); + assert(e); e->state = AVAHI_CACHE_FINAL; gettimeofday(&e->expiry, NULL); @@ -246,11 +258,11 @@ static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) { update_time_event(c, e); } -void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, const AvahiAddress *a) { -/* gchar *txt; */ +void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const AvahiAddress *a) { +/* char *txt; */ - g_assert(c); - g_assert(r && r->ref >= 1); + assert(c); + assert(r && r->ref >= 1); /* txt = avahi_record_to_string(r); */ @@ -298,7 +310,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con /* We need to update the hash table key if we replace the * record */ if (e->by_key_prev == NULL) - g_hash_table_replace(c->hash_table, r->key, e); + avahi_hashmap_replace(c->hashmap, r->key, e); /* Update the record */ avahi_record_unref(e->record); @@ -314,20 +326,24 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES) return; - c->n_entries++; - - e = g_new(AvahiCacheEntry, 1); + if (!(e = avahi_new(AvahiCacheEntry, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return; + } + e->cache = c; e->time_event = NULL; e->record = avahi_record_ref(r); /* Append to hash table */ AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, first, e); - g_hash_table_replace(c->hash_table, e->record->key, first); + avahi_hashmap_replace(c->hashmap, e->record->key, first); /* Append to linked list */ AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e); + c->n_entries++; + /* Notify subscribers */ avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_NEW); } @@ -339,54 +355,60 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, con e->cache_flush = cache_flush; } -/* g_free(txt); */ +/* avahi_free(txt); */ } struct dump_data { AvahiDumpCallback callback; - gpointer userdata; + void* userdata; }; -static void dump_callback(gpointer key, gpointer data, gpointer userdata) { +static void dump_callback(void* key, void* data, void* userdata) { AvahiCacheEntry *e = data; AvahiKey *k = key; struct dump_data *dump_data = userdata; - g_assert(k); - g_assert(e); - g_assert(data); + assert(k); + assert(e); + assert(data); for (; e; e = e->by_key_next) { - gchar *t = avahi_record_to_string(e->record); + char *t; + + if (!(t = avahi_record_to_string(e->record))) + continue; /* OOM */ + dump_data->callback(t, dump_data->userdata); - g_free(t); + avahi_free(t); } } -void avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, gpointer userdata) { +int avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, void* userdata) { struct dump_data data; - g_assert(c); - g_assert(callback); + assert(c); + assert(callback); callback(";;; CACHE DUMP FOLLOWS ;;;", userdata); data.callback = callback; data.userdata = userdata; - g_hash_table_foreach(c->hash_table, dump_callback, &data); + avahi_hashmap_foreach(c->hashmap, dump_callback, &data); + + return 0; } -gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { +int avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { struct timeval now; - AvahiUsec age; + unsigned age; - g_assert(c); - g_assert(e); + assert(c); + assert(e); gettimeofday(&now, NULL); - age = avahi_timeval_diff(&now, &e->timestamp)/1000000; + age = (unsigned) (avahi_timeval_diff(&now, &e->timestamp)/1000000); /* avahi_log_debug("age: %lli, ttl/2: %u", age, e->record->ttl); */ @@ -394,7 +416,7 @@ gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { } void avahi_cache_flush(AvahiCache *c) { - g_assert(c); + assert(c); while (c->entries) remove_entry(c, c->entries); diff --git a/avahi-core/cache.h b/avahi-core/cache.h index 2f4caa6..fa723e9 100644 --- a/avahi-core/cache.h +++ b/avahi-core/cache.h @@ -22,14 +22,13 @@ USA. ***/ -#include - typedef struct AvahiCache AvahiCache; #include #include "prioq.h" #include "server.h" #include "timeeventq.h" +#include "hashmap.h" typedef enum { AVAHI_CACHE_VALID, @@ -46,7 +45,7 @@ struct AvahiCacheEntry { AvahiRecord *record; struct timeval timestamp; struct timeval expiry; - gboolean cache_flush; + int cache_flush; AvahiAddress origin; @@ -62,11 +61,11 @@ struct AvahiCache { AvahiInterface *interface; - GHashTable *hash_table; + AvahiHashmap *hashmap; AVAHI_LLIST_HEAD(AvahiCacheEntry, entries); - guint n_entries; + unsigned n_entries; }; AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *interface); @@ -75,14 +74,14 @@ void avahi_cache_free(AvahiCache *c); AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k); AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r); -void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, const AvahiAddress *a); +void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const AvahiAddress *a); -void avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, gpointer userdata); +int avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, void* userdata); -typedef gpointer AvahiCacheWalkCallback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata); -gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata); +typedef void* AvahiCacheWalkCallback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata); +void* avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, void* userdata); -gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e); +int avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e); void avahi_cache_flush(AvahiCache *c); diff --git a/avahi-core/conformance-test.c b/avahi-core/conformance-test.c index c14a761..ebf9083 100644 --- a/avahi-core/conformance-test.c +++ b/avahi-core/conformance-test.c @@ -29,34 +29,38 @@ #include #include #include +#include #include +#include +#include + #include "core.h" #include "log.h" -static gchar *name = NULL; +static char *name = NULL; static AvahiEntryGroup *group = NULL; static int try = 0; static AvahiServer *avahi = NULL; -static void dump_line(const gchar *text, gpointer userdata) { +static void dump_line(const char *text, void* userdata) { printf("%s\n", text); } -static gboolean dump_timeout(gpointer data) { +static int dump_timeout(void* data) { avahi_server_dump(avahi, dump_line, NULL); - return TRUE; + return 1; } -static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata); +static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata); -static void create_service(const gchar *t) { - gchar *n; +static void create_service(const char *t) { + char *n; - g_assert(t || name); + assert(t || name); n = t ? g_strdup(t) : avahi_alternative_service_name(name); - g_free(name); + avahi_free(name); name = n; if (group) @@ -70,17 +74,17 @@ static void create_service(const gchar *t) { try++; } -static gboolean rename_timeout(gpointer data) { +static int rename_timeout(void* data) { if (access("flag", F_OK) == 0) { create_service("New - Bonjour Service Name"); - return FALSE; + return 0; } - return TRUE; + return 1; } -static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { +static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata) { if (state == AVAHI_ENTRY_GROUP_COLLISION) create_service(NULL); else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) { @@ -89,7 +93,7 @@ static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryG } } -static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) { +static void server_callback(AvahiServer *s, AvahiServerState state, void* userdata) { avahi_log_debug("server state: %i", state); if (state == AVAHI_SERVER_RUNNING) { @@ -101,10 +105,15 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use int main(int argc, char *argv[]) { GMainLoop *loop = NULL; gint error; + AvahiGLibPoll *glib_poll; + + avahi_set_allocator(avahi_glib_allocator()); - avahi = avahi_server_new(NULL, NULL, server_callback, NULL, &error); + glib_poll = avahi_glib_poll_new(NULL); - loop = g_main_loop_new(NULL, FALSE); + avahi = avahi_server_new(avahi_glib_poll_get(glib_poll), NULL, server_callback, NULL, &error); + + loop = g_main_loop_new(NULL, 0); g_timeout_add(1000*5, dump_timeout, avahi); g_timeout_add(1000*5, rename_timeout, avahi); g_main_loop_run(loop); @@ -113,6 +122,8 @@ int main(int argc, char *argv[]) { if (group) avahi_entry_group_free(group); avahi_server_free(avahi); + + avahi_glib_poll_free(glib_poll); return 0; } diff --git a/avahi-core/core.h b/avahi-core/core.h index f51bd6b..4431d80 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -22,8 +22,6 @@ USA. ***/ -#include - /** \file core.h The Avahi Multicast DNS and DNS Service Discovery implmentation. */ /** \example publish-service.c Example how to register a DNS-SD @@ -57,6 +55,7 @@ AVAHI_C_DECL_END #include #include #include +#include #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_BEGIN @@ -72,34 +71,34 @@ typedef enum { } AvahiEntryFlags; /** Prototype for callback functions which are called whenever the state of an AvahiServer object changes */ -typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, gpointer userdata); +typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, void* userdata); /** Prototype for callback functions which are called whenever the state of an AvahiEntryGroup object changes */ -typedef void (*AvahiEntryGroupCallback) (AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata); +typedef void (*AvahiEntryGroupCallback) (AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata); /** Stores configuration options for a server instance */ typedef struct AvahiServerConfig { - gchar *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */ - gchar *domain_name; /**< Default domain name. If left empty defaults to .local */ - gboolean use_ipv4; /**< Enable IPv4 support */ - gboolean use_ipv6; /**< Enable IPv6 support */ - gboolean publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */ - gboolean publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */ - gboolean publish_workstation; /**< Register a _workstation._tcp service */ - gboolean publish_domain; /**< Announce the local domain for browsing */ - gboolean check_response_ttl; /**< If enabled the server ignores all incoming responses with IP TTL != 255. Newer versions of the RFC do no longer contain this check, so it is disabled by default. */ - gboolean use_iff_running; /**< Require IFF_RUNNING on local network interfaces. This is the official way to check for link beat. Unfortunately this doesn't work with all drivers. So bettere leave this off. */ - gboolean enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */ - gboolean reflect_ipv; /**< if enable_reflector is TRUE, enable/disable reflecting between IPv4 and IPv6 */ + char *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */ + char *domain_name; /**< Default domain name. If left empty defaults to .local */ + int use_ipv4; /**< Enable IPv4 support */ + int use_ipv6; /**< Enable IPv6 support */ + int publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */ + int publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */ + int publish_workstation; /**< Register a _workstation._tcp service */ + int publish_domain; /**< Announce the local domain for browsing */ + int check_response_ttl; /**< If enabled the server ignores all incoming responses with IP TTL != 255. Newer versions of the RFC do no longer contain this check, so it is disabled by default. */ + int use_iff_running; /**< Require IFF_RUNNING on local network interfaces. This is the official way to check for link beat. Unfortunately this doesn't work with all drivers. So bettere leave this off. */ + int enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */ + int reflect_ipv; /**< if enable_reflector is 1, enable/disable reflecting between IPv4 and IPv6 */ } AvahiServerConfig; /** Allocate a new mDNS responder object. */ AvahiServer *avahi_server_new( - GMainContext *c, /**< The GLIB main loop context to attach to */ + AvahiPoll *api, /**< The main loop adapter */ const AvahiServerConfig *sc, /**< If non-NULL a pointer to a configuration structure for the server. The server makes an internal deep copy of this structure, so you may free it using avahi_server_config_done() immediately after calling this function. */ AvahiServerCallback callback, /**< A callback which is called whenever the state of the server changes */ - gpointer userdata, /**< An opaque pointer which is passed to the callback function */ - gint *error); + void* userdata, /**< An opaque pointer which is passed to the callback function */ + int *error); /** Free an mDNS responder object */ void avahi_server_free(AvahiServer* s); @@ -111,7 +110,7 @@ void avahi_server_free(AvahiServer* s); * release this memory make sure to call * avahi_server_config_done(). If you want to replace any strings in * the structure be sure to free the strings filled in by this - * function with g_free() first and allocate the replacements with + * function with avahi_free() first and allocate the replacements with * g_malloc() (or g_strdup()).*/ AvahiServerConfig* avahi_server_config_init( AvahiServerConfig *c /**< A structure which shall be filled in */ ); @@ -128,15 +127,15 @@ void avahi_server_config_free(AvahiServerConfig *c); * return value points to an internally allocated string. Be sure to * make a copy of the string before calling any other library * functions. */ -const gchar* avahi_server_get_domain_name(AvahiServer *s); +const char* avahi_server_get_domain_name(AvahiServer *s); /** Return the currently chosen host name. The return value points to a internally allocated string. */ -const gchar* avahi_server_get_host_name(AvahiServer *s); +const char* avahi_server_get_host_name(AvahiServer *s); /** Return the currently chosen host name as a FQDN ("fully qualified * domain name", i.e. the concatenation of the host and domain * name). The return value points to a internally allocated string. */ -const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s); +const char* avahi_server_get_host_name_fqdn(AvahiServer *s); /** Change the host name of a running mDNS responder. This will drop all automicatilly generated RRs and readd them with the new @@ -146,17 +145,17 @@ function is intended to be called when a host name conflict is reported using AvahiServerCallback. The caller should readd all user defined RRs too since they otherwise continue to point to the outdated host name..*/ -gint avahi_server_set_host_name(AvahiServer *s, const gchar *host_name); +int avahi_server_set_host_name(AvahiServer *s, const char *host_name); /** Change the domain name of a running mDNS responder. The same rules * as with avahi_server_set_host_name() apply. */ -gint avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name); +int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name); /** Return the opaque user data pointer attached to a server object */ -gpointer avahi_server_get_data(AvahiServer *s); +void* avahi_server_get_data(AvahiServer *s); /** Change the opaque user data pointer attached to a server object */ -void avahi_server_set_data(AvahiServer *s, gpointer userdata); +void avahi_server_set_data(AvahiServer *s, void* userdata); /** Return the current state of the server object */ AvahiServerState avahi_server_get_state(AvahiServer *s); @@ -170,41 +169,41 @@ AvahiServerState avahi_server_get_state(AvahiServer *s); const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state); /** Callback prototype for avahi_server_dump() */ -typedef void (*AvahiDumpCallback)(const gchar *text, gpointer userdata); +typedef void (*AvahiDumpCallback)(const char *text, void* userdata); /** Dump the current server status by calling "callback" for each line. */ -void avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, gpointer userdata); +int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata); /** Create a new entry group. The specified callback function is * called whenever the state of the group changes. Use entry group * objects to keep track of you RRs. Add new RRs to a group using * avahi_server_add_xxx(). Make sure to call avahi_entry_group_commit() * to start the registration process for your RRs */ -AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata); +AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, void* userdata); /** Free an entry group. All RRs assigned to the group are removed from the server */ void avahi_entry_group_free(AvahiEntryGroup *g); /** Commit an entry group. This starts the probing and registration process for all RRs in the group */ -gint avahi_entry_group_commit(AvahiEntryGroup *g); +int avahi_entry_group_commit(AvahiEntryGroup *g); /** Remove all entries from the entry group and reset the state to AVAHI_ENTRY_GROUP_UNCOMMITED. */ void avahi_entry_group_reset(AvahiEntryGroup *g); -/** Return TRUE if the entry group is empty, i.e. has no records attached. */ -gboolean avahi_entry_group_is_empty(AvahiEntryGroup *g); +/** Return 1 if the entry group is empty, i.e. has no records attached. */ +int avahi_entry_group_is_empty(AvahiEntryGroup *g); /** Return the current state of the specified entry group */ AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g); /** Change the opaque user data pointer attached to an entry group object */ -void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata); +void avahi_entry_group_set_data(AvahiEntryGroup *g, void* userdata); /** Return the opaque user data pointer currently set for the entry group object */ -gpointer avahi_entry_group_get_data(AvahiEntryGroup *g); +void* avahi_entry_group_get_data(AvahiEntryGroup *g); /** Add a new resource record to the server. Returns 0 on success, negative otherwise. */ -gint avahi_server_add( +int avahi_server_add( AvahiServer *s, /**< The server object to add this record to */ AvahiEntryGroup *g, /**< An entry group object if this new record shall be attached to one, or NULL. If you plan to remove the record sometime later you a required to pass an entry group object here. */ AvahiIfIndex interface, /**< A numeric index of a network interface to attach this record to, or AVAHI_IF_UNSPEC to attach this record to all interfaces */ @@ -213,51 +212,51 @@ gint avahi_server_add( AvahiRecord *r /**< The record to add. This function increases the reference counter of this object. */ ); /** Add a PTR RR to the server. See avahi_server_add() for more information. */ -gint avahi_server_add_ptr( +int avahi_server_add_ptr( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, /**< DNS TTL for this record */ - const gchar *name, /**< PTR record name */ - const gchar *dest /**< pointer destination */ ); + uint32_t ttl, /**< DNS TTL for this record */ + const char *name, /**< PTR record name */ + const char *dest /**< pointer destination */ ); /** Add a PTR RR to the server. See avahi_server_add() for more information. */ -gint avahi_server_add_txt( +int avahi_server_add_txt( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, /**< DNS TTL for this record */ - const gchar *name, /**< TXT record name */ + uint32_t ttl, /**< DNS TTL for this record */ + const char *name, /**< TXT record name */ ... /**< Text record data, terminated by NULL */); /** Add a PTR RR to the server. Mostly identical to * avahi_server_add_text but takes a va_list instead of a variable * number of arguments */ -gint avahi_server_add_txt_va( +int avahi_server_add_txt_va( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, va_list va); /** Add a PTR RR to the server. Mostly identical to * avahi_server_add_text but takes an AvahiStringList record instead of a variable * number of arguments. */ -gint avahi_server_add_txt_strlst( +int avahi_server_add_txt_strlst( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, AvahiStringList *strlst /**< TXT decord data as a AvahiString. This routine makes a deep copy of this object. */ ); /** Add an IP address mapping to the server. This will add both the @@ -267,13 +266,13 @@ gint avahi_server_add_txt_strlst( * the other RR is deleted from the server or not. Therefore, you have * to free the AvahiEntryGroup and create a new one before * proceeding. */ -gint avahi_server_add_address( +int avahi_server_add_address( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - const gchar *name, + const char *name, AvahiAddress *a); /** Add an DNS-SD service to the Server. This will add all required @@ -282,42 +281,42 @@ gint avahi_server_add_address( * but it is not defined if the other RR is deleted from the server or * not. Therefore, you have to free the AvahiEntryGroup and create a * new one before proceeding. */ -gint avahi_server_add_service( +int avahi_server_add_service( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, /**< Service name, e.g. "Lennart's Files" */ - const gchar *type, /**< DNS-SD type, e.g. "_http._tcp" */ - const gchar *domain, - const gchar *host, /**< Host name where this servcie resides, or NULL if on the local host */ - guint16 port, /**< Port number of the service */ + const char *name, /**< Service name, e.g. "Lennart's Files" */ + const char *type, /**< DNS-SD type, e.g. "_http._tcp" */ + const char *domain, + const char *host, /**< Host name where this servcie resides, or NULL if on the local host */ + uint16_t port, /**< Port number of the service */ ... /**< Text records, terminated by NULL */); /** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */ -gint avahi_server_add_service_va( +int avahi_server_add_service_va( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, va_list va); /** Mostly identical to avahi_server_add_service(), but takes an AvahiStringList object for the TXT records. The AvahiStringList object is copied. */ -gint avahi_server_add_service_strlst( +int avahi_server_add_service_strlst( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, AvahiStringList *strlst); /** The type of DNS server */ @@ -329,28 +328,28 @@ typedef enum { /** Publish the specified unicast DNS server address via mDNS. You may * browse for records create this way wit * avahi_dns_server_browser_new(). */ -gint avahi_server_add_dns_server_address( +int avahi_server_add_dns_server_address( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDNSServerType type, const AvahiAddress *address, - guint16 port /** should be 53 */); + uint16_t port /** should be 53 */); /** Similar to avahi_server_add_dns_server_address(), but specify a host name instead of an address. The specified host name should be resolvable via mDNS */ -gint avahi_server_add_dns_server_name( +int avahi_server_add_dns_server_name( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDNSServerType type, - const gchar *name, - guint16 port /** should be 53 */); + const char *name, + uint16_t port /** should be 53 */); /** A browsing object for arbitrary RRs */ typedef struct AvahiRecordBrowser AvahiRecordBrowser; @@ -362,7 +361,7 @@ typedef void (*AvahiRecordBrowserCallback)( AvahiProtocol protocol, /**< Protocol number the record was found. */ AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */ AvahiRecord *record, /**< The record that was found */ - gpointer userdata /**< Arbitrary user data passed to avahi_record_browser_new() */ ); + void* userdata /**< Arbitrary user data passed to avahi_record_browser_new() */ ); /** Create a new browsing object for arbitrary RRs */ AvahiRecordBrowser *avahi_record_browser_new( @@ -371,7 +370,7 @@ AvahiRecordBrowser *avahi_record_browser_new( AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */ AvahiKey *key, /**< The search key */ AvahiRecordBrowserCallback callback, /**< The callback to call on browsing events */ - gpointer userdata /**< Arbitrary use suppliable data which is passed to the callback */); + void* userdata /**< Arbitrary use suppliable data which is passed to the callback */); /** Free an AvahiRecordBrowser object */ void avahi_record_browser_free(AvahiRecordBrowser *b); @@ -385,19 +384,19 @@ typedef void (*AvahiHostNameResolverCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, /**< Resolving event */ - const gchar *host_name, /**< Host name which should be resolved. May differ in case from the query */ + const char *host_name, /**< Host name which should be resolved. May differ in case from the query */ const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */ - gpointer userdata); + void* userdata); /** Create an AvahiHostNameResolver object for resolving a host name to an adddress. See AvahiRecordBrowser for more info on the paramters. */ AvahiHostNameResolver *avahi_host_name_resolver_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *host_name, /**< The host name to look for */ + const char *host_name, /**< The host name to look for */ AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */ AvahiHostNameResolverCallback calback, - gpointer userdata); + void* userdata); /** Free a AvahiHostNameResolver object */ void avahi_host_name_resolver_free(AvahiHostNameResolver *r); @@ -412,8 +411,8 @@ typedef void (*AvahiAddressResolverCallback)( AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *a, - const gchar *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */ - gpointer userdata); + const char *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */ + void* userdata); /** Create an AvahiAddressResolver object. See AvahiRecordBrowser for more info on the paramters. */ AvahiAddressResolver *avahi_address_resolver_new( @@ -422,7 +421,7 @@ AvahiAddressResolver *avahi_address_resolver_new( AvahiProtocol protocol, const AvahiAddress *address, AvahiAddressResolverCallback calback, - gpointer userdata); + void* userdata); /** Free an AvahiAddressResolver object */ void avahi_address_resolver_free(AvahiAddressResolver *r); @@ -436,18 +435,18 @@ typedef void (*AvahiDomainBrowserCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, - const gchar *domain, - gpointer userdata); + const char *domain, + void* userdata); /** Create a new AvahiDomainBrowser object */ AvahiDomainBrowser *avahi_domain_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDomainBrowserType type, AvahiDomainBrowserCallback callback, - gpointer userdata); + void* userdata); /** Free an AvahiDomainBrowser object */ void avahi_domain_browser_free(AvahiDomainBrowser *b); @@ -461,18 +460,18 @@ typedef void (*AvahiServiceTypeBrowserCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, - const gchar *type, - const gchar *domain, - gpointer userdata); + const char *type, + const char *domain, + void* userdata); /** Create a new AvahiServiceTypeBrowser object. */ AvahiServiceTypeBrowser *avahi_service_type_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiServiceTypeBrowserCallback callback, - gpointer userdata); + void* userdata); /** Free an AvahiServiceTypeBrowser object */ void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b); @@ -486,20 +485,20 @@ typedef void (*AvahiServiceBrowserCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, - const gchar *name /**< Service name, e.g. "Lennart's Files" */, - const gchar *type /**< DNS-SD type, e.g. "_http._tcp" */, - const gchar *domain /**< Domain of this service, e.g. "local" */, - gpointer userdata); + const char *name /**< Service name, e.g. "Lennart's Files" */, + const char *type /**< DNS-SD type, e.g. "_http._tcp" */, + const char *domain /**< Domain of this service, e.g. "local" */, + void* userdata); /** Create a new AvahiServiceBrowser object. */ AvahiServiceBrowser *avahi_service_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *service_type /** DNS-SD service type, e.g. "_http._tcp" */, - const gchar *domain, + const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */, + const char *domain, AvahiServiceBrowserCallback callback, - gpointer userdata); + void* userdata); /** Free an AvahiServiceBrowser object */ void avahi_service_browser_free(AvahiServiceBrowser *b); @@ -513,26 +512,26 @@ typedef void (*AvahiServiceResolverCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, - const gchar *name, /**< Service name */ - const gchar *type, /**< Service Type */ - const gchar *domain, - const gchar *host_name, /**< Host name of the service */ + const char *name, /**< Service name */ + const char *type, /**< Service Type */ + const char *domain, + const char *host_name, /**< Host name of the service */ const AvahiAddress *a, /**< The resolved host name */ - guint16 port, /**< Service name */ + uint16_t port, /**< Service name */ AvahiStringList *txt, /**< TXT record data */ - gpointer userdata); + void* userdata); /** Create a new AvahiServiceResolver object */ AvahiServiceResolver *avahi_service_resolver_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, + const char *name, + const char *type, + const char *domain, AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */ AvahiServiceResolverCallback calback, - gpointer userdata); + void* userdata); /** Free an AvahiServiceResolver object */ void avahi_service_resolver_free(AvahiServiceResolver *r); @@ -548,27 +547,27 @@ typedef void (*AvahiDNSServerBrowserCallback)( AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, - const gchar *host_name, /**< Host name of the DNS server, probably useless */ + const char *host_name, /**< Host name of the DNS server, probably useless */ const AvahiAddress *a, /**< Address of the DNS server */ - guint16 port, /**< Port number of the DNS servers, probably 53 */ - gpointer userdata); + uint16_t port, /**< Port number of the DNS servers, probably 53 */ + void* userdata); /** Create a new AvahiDNSServerBrowser object */ AvahiDNSServerBrowser *avahi_dns_server_browser_new( AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, /**< Address protocol for the DNS server */ AvahiDNSServerBrowserCallback callback, - gpointer userdata); + void* userdata); /** Free an AvahiDNSServerBrowser object */ void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b); /** Return the last error code */ -gint avahi_server_errno(AvahiServer *s); +int avahi_server_errno(AvahiServer *s); #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END diff --git a/avahi-core/dns-test.c b/avahi-core/dns-test.c index bf141f5..e5369a9 100644 --- a/avahi-core/dns-test.c +++ b/avahi-core/dns-test.c @@ -23,14 +23,17 @@ #include #endif +#include + #include + #include "dns.h" #include "log.h" #include "util.h" int main(int argc, char *argv[]) { - gchar t[256]; - const gchar *a, *b, *c, *d; + char t[256]; + const char *a, *b, *c, *d; AvahiDnsPacket *p; p = avahi_dns_packet_new(0); @@ -44,19 +47,19 @@ int main(int argc, char *argv[]) { avahi_dns_packet_consume_name(p, t, sizeof(t)); avahi_log_debug(">%s<", t); - g_assert(avahi_domain_equal(a, t)); + assert(avahi_domain_equal(a, t)); avahi_dns_packet_consume_name(p, t, sizeof(t)); avahi_log_debug(">%s<", t); - g_assert(avahi_domain_equal(b, t)); + assert(avahi_domain_equal(b, t)); avahi_dns_packet_consume_name(p, t, sizeof(t)); avahi_log_debug(">%s<", t); - g_assert(avahi_domain_equal(c, t)); + assert(avahi_domain_equal(c, t)); avahi_dns_packet_consume_name(p, t, sizeof(t)); avahi_log_debug(">%s<", t); - g_assert(avahi_domain_equal(d, t)); + assert(avahi_domain_equal(d, t)); avahi_dns_packet_free(p); return 0; diff --git a/avahi-core/dns.c b/avahi-core/dns.c index 9663fc4..9bc7aec 100644 --- a/avahi-core/dns.c +++ b/avahi-core/dns.c @@ -28,13 +28,17 @@ #include #include #include +#include #include +#include + #include "dns.h" +#include "log.h" -AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { +AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu) { AvahiDnsPacket *p; - guint max_size; + size_t max_size; if (mtu <= 0) max_size = AVAHI_DNS_PACKET_MAX_SIZE; @@ -46,7 +50,9 @@ AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE) max_size = AVAHI_DNS_PACKET_HEADER_SIZE; - p = g_malloc(sizeof(AvahiDnsPacket) + max_size); + if (!(p = avahi_malloc(sizeof(AvahiDnsPacket) + max_size))) + return p; + p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; p->max_size = max_size; p->name_table = NULL; @@ -55,37 +61,43 @@ AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { return p; } -AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) { +AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu) { AvahiDnsPacket *p; - p = avahi_dns_packet_new(mtu); + if (!(p = avahi_dns_packet_new(mtu))) + return NULL; + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); return p; } -AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa) { +AvahiDnsPacket* avahi_dns_packet_new_response(unsigned mtu, int aa) { AvahiDnsPacket *p; - p = avahi_dns_packet_new(mtu); + if (!(p = avahi_dns_packet_new(mtu))) + return NULL; + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0)); return p; } -AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa) { +AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa) { AvahiDnsPacket *r; - g_assert(p); + assert(p); - r = avahi_dns_packet_new_response(mtu, aa); + if (!(r = avahi_dns_packet_new_response(mtu, aa))) + return NULL; if (copy_queries) { - guint n, saved_rindex; + unsigned saved_rindex; + uint32_t n; saved_rindex = p->rindex; p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) { AvahiKey *k; - gboolean unicast_response; + int unicast_response; if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) { avahi_dns_packet_append_key(r, k, unicast_response); @@ -109,66 +121,67 @@ AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolea void avahi_dns_packet_free(AvahiDnsPacket *p) { - g_assert(p); + assert(p); if (p->name_table) - g_hash_table_destroy(p->name_table); + avahi_hashmap_free(p->name_table); - g_free(p); + avahi_free(p); } -void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint idx, guint16 v) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); - ((guint16*) AVAHI_DNS_PACKET_DATA(p))[idx] = g_htons(v); + ((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx] = htons(v); } -guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint idx) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); - return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[idx]); + return ntohs(((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx]); } -void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint idx) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); avahi_dns_packet_set_field(p, idx, avahi_dns_packet_get_field(p, idx) + 1); } -guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { - guint8 *d, *saved_ptr = NULL; - guint saved_size; +uint8_t* avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name) { + uint8_t *d, *saved_ptr = NULL; + size_t saved_size; - g_assert(p); - g_assert(name); + assert(p); + assert(name); saved_size = p->size; saved_ptr = avahi_dns_packet_extend(p, 0); while (*name) { - guint8* prev; - const gchar *pname; - gchar label[64]; + uint8_t* prev; + const char *pname; + char label[64], *u; + /* Check whether we can compress this name. */ - if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) { - guint idx; + if (p->name_table && (prev = avahi_hashmap_lookup(p->name_table, name))) { + unsigned idx; - g_assert(prev >= AVAHI_DNS_PACKET_DATA(p)); - idx = (guint) (prev - AVAHI_DNS_PACKET_DATA(p)); + assert(prev >= AVAHI_DNS_PACKET_DATA(p)); + idx = (unsigned) (prev - AVAHI_DNS_PACKET_DATA(p)); - g_assert(idx < p->size); + assert(idx < p->size); if (idx < 0x4000) { - guint16 *t; - if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16)))) + uint16_t *t; + if (!(t = (uint16_t*) avahi_dns_packet_extend(p, sizeof(uint16_t)))) return NULL; - *t = g_htons((0xC000 | idx)); + *t = htons((0xC000 | idx)); return saved_ptr; } } @@ -183,9 +196,12 @@ guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { if (!p->name_table) /* This works only for normalized domain names */ - p->name_table = g_hash_table_new_full((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal, g_free, NULL); + p->name_table = avahi_hashmap_new(avahi_string_hash, avahi_string_equal, avahi_free, NULL); - g_hash_table_insert(p->name_table, g_strdup(pname), d); + if (!(u = avahi_strdup(pname))) + avahi_log_error("avahi_strdup() failed."); + else + avahi_hashmap_insert(p->name_table, u, d); } if (!(d = avahi_dns_packet_extend(p, 1))) @@ -200,35 +216,35 @@ fail: return NULL; } -guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) { - guint8 *d; - g_assert(p); +uint8_t* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v) { + uint8_t *d; + assert(p); - if (!(d = avahi_dns_packet_extend(p, sizeof(guint16)))) + if (!(d = avahi_dns_packet_extend(p, sizeof(uint16_t)))) return NULL; - *((guint16*) d) = g_htons(v); + *((uint16_t*) d) = htons(v); return d; } -guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) { - guint8 *d; - g_assert(p); +uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v) { + uint8_t *d; + assert(p); - if (!(d = avahi_dns_packet_extend(p, sizeof(guint32)))) + if (!(d = avahi_dns_packet_extend(p, sizeof(uint32_t)))) return NULL; - *((guint32*) d) = g_htonl(v); + *((uint32_t*) d) = htonl(v); return d; } -guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint l) { - guint8* d; +uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *b, size_t l) { + uint8_t* d; - g_assert(p); - g_assert(b); - g_assert(l); + assert(p); + assert(b); + assert(l); if (!(d = avahi_dns_packet_extend(p, l))) return NULL; @@ -237,12 +253,12 @@ guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint return d; } -guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { - guint8* d; - guint k; +uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s) { + uint8_t* d; + size_t k; - g_assert(p); - g_assert(s); + assert(p); + assert(s); if ((k = strlen(s)) >= 255) k = 255; @@ -250,16 +266,16 @@ guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { if (!(d = avahi_dns_packet_extend(p, k+1))) return NULL; - *d = (guint8) k; + *d = (uint8_t) k; memcpy(d+1, s, k); return d; } -guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { - guint8 *d; +uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, size_t l) { + uint8_t *d; - g_assert(p); + assert(p); if (p->size+l > p->max_size) return NULL; @@ -270,9 +286,9 @@ guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { return d; } -gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { - guint16 flags; - g_assert(p); +int avahi_dns_packet_is_valid(AvahiDnsPacket *p) { + uint16_t flags; + assert(p); if (p->size < 12) return -1; @@ -285,20 +301,20 @@ gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { return 0; } -gint avahi_dns_packet_is_query(AvahiDnsPacket *p) { - g_assert(p); +int avahi_dns_packet_is_query(AvahiDnsPacket *p) { + assert(p); return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR); } -static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint l) { - gint ret = 0; +static int consume_labels(AvahiDnsPacket *p, unsigned idx, char *ret_name, size_t l) { + int ret = 0; int compressed = 0; int first_label = 1; - g_assert(p && ret_name && l); + assert(p && ret_name && l); for (;;) { - guint8 n; + uint8_t n; if (idx+1 > p->size) return -1; @@ -325,7 +341,7 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint if (idx + n > p->size) return -1; - if ((guint) n + 1 > l) + if ((size_t) n + 1 > l) return -1; if (!first_label) { @@ -347,7 +363,7 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint if (idx+2 > p->size) return -1; - idx = ((guint) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1]; + idx = ((unsigned) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1]; if (!compressed) ret += 2; @@ -358,8 +374,8 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint } } -gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) { - gint r; +int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l) { + int r; if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0) return -1; @@ -368,36 +384,36 @@ gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) return 0; } -gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) { - g_assert(p); - g_assert(ret_v); +int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v) { + assert(p); + assert(ret_v); - if (p->rindex + sizeof(guint16) > p->size) + if (p->rindex + sizeof(uint16_t) > p->size) return -1; - *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint16); + *ret_v = ntohs(*((uint16_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(uint16_t); return 0; } -gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) { - g_assert(p); - g_assert(ret_v); +int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v) { + assert(p); + assert(ret_v); - if (p->rindex + sizeof(guint32) > p->size) + if (p->rindex + sizeof(uint32_t) > p->size) return -1; - *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint32); + *ret_v = ntohl(*((uint32_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(uint32_t); return 0; } -gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l) { - g_assert(p); - g_assert(ret_data); - g_assert(l > 0); +int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void * ret_data, size_t l) { + assert(p); + assert(ret_data); + assert(l > 0); if (p->rindex + l > p->size) return -1; @@ -408,12 +424,12 @@ gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint return 0; } -gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) { - guint k; +int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l) { + size_t k; - g_assert(p); - g_assert(ret_string); - g_assert(l > 0); + assert(p); + assert(ret_string); + assert(l > 0); if (p->rindex >= p->size) return -1; @@ -428,15 +444,14 @@ gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1); ret_string[l-1] = 0; - p->rindex += 1+k; return 0; } -gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { - g_assert(p); +const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { + assert(p); if (p->rindex > p->size) return NULL; @@ -444,8 +459,8 @@ gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { return AVAHI_DNS_PACKET_DATA(p) + p->rindex; } -gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) { - g_assert(p); +int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length) { + assert(p); if (p->rindex + length > p->size) return -1; @@ -454,16 +469,16 @@ gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) { return 0; } -AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush) { - gchar name[257], buf[257]; - guint16 type, class; - guint32 ttl; - guint16 rdlength; +AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush) { + char name[257], buf[257]; + uint16_t type, class; + uint32_t ttl; + uint16_t rdlength; AvahiRecord *r = NULL; - gconstpointer start; + const void* start; - g_assert(p); - g_assert(ret_cache_flush); + assert(p); + assert(ret_cache_flush); /* avahi_log_debug("consume_record()"); */ @@ -482,7 +497,8 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca start = avahi_dns_packet_get_rptr(p); - r = avahi_record_new_full(name, class, type, ttl); + if (!(r = avahi_record_new_full(name, class, type, ttl))) + return NULL; switch (type) { case AVAHI_DNS_TYPE_PTR: @@ -493,7 +509,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) goto fail; - r->data.ptr.name = g_strdup(buf); + r->data.ptr.name = avahi_strdup(buf); break; @@ -507,7 +523,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) goto fail; - r->data.srv.name = g_strdup(buf); + r->data.srv.name = avahi_strdup(buf); break; case AVAHI_DNS_TYPE_HINFO: @@ -517,12 +533,12 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) goto fail; - r->data.hinfo.cpu = g_strdup(buf); + r->data.hinfo.cpu = avahi_strdup(buf); if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) goto fail; - r->data.hinfo.os = g_strdup(buf); + r->data.hinfo.os = avahi_strdup(buf); break; case AVAHI_DNS_TYPE_TXT: @@ -563,7 +579,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (rdlength > 0) { - r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength); + r->data.generic.data = avahi_memdup(avahi_dns_packet_get_rptr(p), rdlength); if (avahi_dns_packet_skip(p, rdlength) < 0) goto fail; @@ -572,10 +588,10 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca break; } -/* avahi_log_debug("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */ +/* avahi_log_debug("%i == %u ?", (uint8_t*) avahi_dns_packet_get_rptr(p) - (uint8_t*) start, rdlength); */ /* Check if we read enough data */ - if ((const guint8*) avahi_dns_packet_get_rptr(p) - (const guint8*) start != rdlength) + if ((const uint8_t*) avahi_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength) goto fail; return r; @@ -587,12 +603,12 @@ fail: return NULL; } -AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) { - gchar name[256]; - guint16 type, class; +AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response) { + char name[256]; + uint16_t type, class; - g_assert(p); - g_assert(ret_unicast_response); + assert(p); + assert(ret_unicast_response); if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 || avahi_dns_packet_consume_uint16(p, &type) < 0 || @@ -605,12 +621,12 @@ AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_ return avahi_key_new(name, class, type); } -guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) { - guint8 *t; - guint size; +uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response) { + uint8_t *t; + size_t size; - g_assert(p); - g_assert(k); + assert(p); + assert(k); size = p->size; @@ -624,12 +640,12 @@ guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean uni return t; } -guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) { - guint8 *t, *l, *start; - guint size; +uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl) { + uint8_t *t, *l, *start; + size_t size; - g_assert(p); - g_assert(r); + assert(p); + assert(r); size = p->size; @@ -671,8 +687,8 @@ guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboole case AVAHI_DNS_TYPE_TXT: { - guint8 *data; - guint n; + uint8_t *data; + size_t n; n = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0); @@ -713,11 +729,11 @@ guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboole size = avahi_dns_packet_extend(p, 0) - start; - g_assert(size <= 0xFFFF); + assert(size <= 0xFFFF); /* avahi_log_debug("appended %u", size); */ - * (guint16*) l = g_htons((guint16) size); + * (uint16_t*) l = htons((uint16_t) size); return t; @@ -727,16 +743,16 @@ fail: return NULL; } -gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) { - g_assert(p); +int avahi_dns_packet_is_empty(AvahiDnsPacket *p) { + assert(p); return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE; } -guint avahi_dns_packet_space(AvahiDnsPacket *p) { - g_assert(p); +size_t avahi_dns_packet_space(AvahiDnsPacket *p) { + assert(p); - g_assert(p->size <= p->max_size); + assert(p->size <= p->max_size); return p->max_size - p->size; } diff --git a/avahi-core/dns.h b/avahi-core/dns.h index bb132c1..9d93d33 100644 --- a/avahi-core/dns.h +++ b/avahi-core/dns.h @@ -22,59 +22,58 @@ USA. ***/ -#include - #include "rr.h" +#include "hashmap.h" #define AVAHI_DNS_PACKET_MAX_SIZE 9000 #define AVAHI_DNS_PACKET_HEADER_SIZE 12 #define AVAHI_DNS_PACKET_EXTRA_SIZE 48 typedef struct AvahiDnsPacket { - guint size, rindex, max_size; - GHashTable *name_table; /* for name compression */ + size_t size, rindex, max_size; + AvahiHashmap *name_table; /* for name compression */ } AvahiDnsPacket; -#define AVAHI_DNS_PACKET_DATA(p) (((guint8*) p) + sizeof(AvahiDnsPacket)) +#define AVAHI_DNS_PACKET_DATA(p) (((uint8_t*) p) + sizeof(AvahiDnsPacket)) -AvahiDnsPacket* avahi_dns_packet_new(guint mtu); -AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu); -AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa); +AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu); +AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu); +AvahiDnsPacket* avahi_dns_packet_new_response(unsigned mtu, int aa); -AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa); +AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa); void avahi_dns_packet_free(AvahiDnsPacket *p); -void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint idx, guint16 v); -guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint idx); -void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint idx); +void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v); +uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx); +void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx); -guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l); +uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, unsigned l); -guint8 *avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v); -guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v); -guint8 *avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name); -guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer, guint l); -guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response); -guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl); -guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s); +uint8_t *avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v); +uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v); +uint8_t *avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name); +uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *d, size_t l); +uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response); +uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl); +uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s); -gint avahi_dns_packet_is_query(AvahiDnsPacket *p); -gint avahi_dns_packet_check_valid(AvahiDnsPacket *p); +int avahi_dns_packet_is_query(AvahiDnsPacket *p); +int avahi_dns_packet_is_valid(AvahiDnsPacket *p); -gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v); -gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v); -gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l); -gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l); -AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response); -AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush); -gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l); +int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v); +int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v); +int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l); +int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void* ret_data, size_t l); +AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response); +AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush); +int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l); -gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p); +const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p); -gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length); +int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length); -gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p); -guint avahi_dns_packet_space(AvahiDnsPacket *p); +int avahi_dns_packet_is_empty(AvahiDnsPacket *p); +size_t avahi_dns_packet_space(AvahiDnsPacket *p); #define AVAHI_DNS_FIELD_ID 0 #define AVAHI_DNS_FIELD_FLAGS 1 @@ -90,15 +89,15 @@ guint avahi_dns_packet_space(AvahiDnsPacket *p); #define AVAHI_DNS_FLAG_AA (1 << 10) #define AVAHI_DNS_FLAGS(qr, opcode, aa, tc, rd, ra, z, ad, cd, rcode) \ - (((guint16) !!qr << 15) | \ - ((guint16) (opcode & 15) << 11) | \ - ((guint16) !!aa << 10) | \ - ((guint16) !!tc << 9) | \ - ((guint16) !!rd << 8) | \ - ((guint16) !!ra << 7) | \ - ((guint16) !!ad << 5) | \ - ((guint16) !!cd << 4) | \ - ((guint16) (rd & 15))) + (((uint16_t) !!qr << 15) | \ + ((uint16_t) (opcode & 15) << 11) | \ + ((uint16_t) !!aa << 10) | \ + ((uint16_t) !!tc << 9) | \ + ((uint16_t) !!rd << 8) | \ + ((uint16_t) !!ra << 7) | \ + ((uint16_t) !!ad << 5) | \ + ((uint16_t) !!cd << 4) | \ + ((uint16_t) (rd & 15))) #endif diff --git a/avahi-core/hashmap.c b/avahi-core/hashmap.c index 0c44d4f..24df1f2 100644 --- a/avahi-core/hashmap.c +++ b/avahi-core/hashmap.c @@ -203,7 +203,6 @@ int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) { return 0; } - void avahi_hashmap_remove(AvahiHashmap *m, const void *key) { Entry *e; @@ -215,6 +214,18 @@ void avahi_hashmap_remove(AvahiHashmap *m, const void *key) { entry_free(m, e, 0); } +void avahi_hashmap_foreach(AvahiHashmap *m, AvahiHashmapForeachCallback callback, void *userdata) { + Entry *e, *next; + assert(m); + assert(callback); + + for (e = m->entries_list; e; e = next) { + next = e->entries_next; + + callback(e->key, e->value, userdata); + } +} + unsigned avahi_string_hash(const void *data) { const char *p = data; unsigned hash = 0; @@ -231,19 +242,14 @@ int avahi_string_equal(const void *a, const void *b) { return strcmp(p, q) == 0; } -unsigned avahi_domain_hash(const void *data) { - unsigned hash = 0; - const char *s; - - for (;;) { - char c[65]; +unsigned avahi_int_hash(const void *data) { + const int *i = data; + + return (unsigned) *i; +} - if (!avahi_unescape_label(&s, c, sizeof(c))) - return hash; +int avahi_int_equal(const void *a, const void *b) { + const int *_a = a, *_b = b; - if (!c[0]) - continue; - - hash += avahi_string_hash(avahi_strdown(c)); - } + return *_a == *_b; } diff --git a/avahi-core/hashmap.h b/avahi-core/hashmap.h index c3000ba..f546dfc 100644 --- a/avahi-core/hashmap.h +++ b/avahi-core/hashmap.h @@ -37,14 +37,19 @@ AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_fu void avahi_hashmap_free(AvahiHashmap *m); void* avahi_hashmap_lookup(AvahiHashmap *m, const void *key); void* avahi_hashmap_steal(AvahiHashmap *m, const void *key); -int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *data); -int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *data); +int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value); +int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value); void avahi_hashmap_remove(AvahiHashmap *m, const void *key); +typedef void (*AvahiHashmapForeachCallback)(void *key, void *value, void *userdata); + +void avahi_hashmap_foreach(AvahiHashmap *m, AvahiHashmapForeachCallback callback, void *userdata); + unsigned avahi_string_hash(const void *data); int avahi_string_equal(const void *a, const void *b); -unsigned avahi_domain_hash(const void *data); +unsigned avahi_int_hash(const void *data); +int avahi_int_equal(const void *a, const void *b); AVAHI_C_DECL_END diff --git a/avahi-core/iface.c b/avahi-core/iface.c index 4376008..8671044 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -32,6 +32,8 @@ #include #include +#include + #include "iface.h" #include "netlink.h" #include "dns.h" @@ -40,9 +42,9 @@ #include "util.h" #include "log.h" -static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove_rrs) { - g_assert(m); - g_assert(a); +static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove_rrs) { + assert(m); + assert(a); if (avahi_interface_address_relevant(a) && !remove_rrs && @@ -54,10 +56,13 @@ static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a if (!a->entry_group) a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + if (!a->entry_group) /* OOM */ + return; + if (avahi_entry_group_is_empty(a->entry_group)) { if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) { - avahi_log_warn(__FILE__": avahi_server_add_address() failed."); + avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error)); avahi_entry_group_free(a->entry_group); a->entry_group = NULL; return; @@ -79,21 +84,21 @@ static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a } } -static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove_rrs) { +static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove_rrs) { AvahiInterfaceAddress *a; - g_assert(m); - g_assert(i); + assert(m); + assert(i); for (a = i->addresses; a; a = a->address_next) update_address_rr(m, a, remove_rrs); } -static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove_rrs) { +static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove_rrs) { AvahiInterface *i; - g_assert(m); - g_assert(hw); + assert(m); + assert(hw); for (i = hw->interfaces; i; i = i->by_hardware_next) update_interface_rr(m, i, remove_rrs); @@ -106,12 +111,21 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h if (!hw->entry_group) hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + if (!hw->entry_group) + return; /* OOM */ + if (avahi_entry_group_is_empty(hw->entry_group)) { - gchar *name; - gchar *t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size); - - name = g_strdup_printf("%s [%s]", m->server->host_name, t); - g_free(t); + char *name; + char *t; + + if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size))) + return; /* OOM */ + + name = avahi_strdup_printf("%s [%s]", m->server->host_name, t); + avahi_free(t); + + if (!name) + return; /* OOM */ if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) { avahi_log_warn(__FILE__": avahi_server_add_service() failed."); @@ -120,7 +134,7 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h } else avahi_entry_group_commit(hw->entry_group); - g_free(name); + avahi_free(name); } } else { @@ -136,29 +150,29 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h } static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) { - g_assert(m); - g_assert(a); - g_assert(a->interface); + assert(m); + assert(a); + assert(a->interface); - update_address_rr(m, a, TRUE); + update_address_rr(m, a, 1); AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a); if (a->entry_group) avahi_entry_group_free(a->entry_group); - g_free(a); + avahi_free(a); } -static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) { - g_assert(m); - g_assert(i); +static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, int send_goodbye) { + assert(m); + assert(i); avahi_goodbye_interface(m->server, i, send_goodbye); avahi_response_scheduler_force(i->response_scheduler); - g_assert(!i->announcements); + assert(!i->announcements); - update_interface_rr(m, i, TRUE); + update_interface_rr(m, i, 1); while (i->addresses) free_address(m, i->addresses); @@ -171,14 +185,14 @@ static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i); AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i); - g_free(i); + avahi_free(i); } -static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) { - g_assert(m); - g_assert(hw); +static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int send_goodbye) { + assert(m); + assert(hw); - update_hw_interface_rr(m, hw, TRUE); + update_hw_interface_rr(m, hw, 1); while (hw->interfaces) free_interface(m, hw->interfaces, send_goodbye); @@ -187,18 +201,18 @@ static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gb avahi_entry_group_free(hw->entry_group); AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw); - g_hash_table_remove(m->hash_table, &hw->index); + avahi_hashmap_remove(m->hashmap, &hw->index); - g_free(hw->name); - g_free(hw); + avahi_free(hw->name); + avahi_free(hw); } static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) { AvahiInterfaceAddress *ia; - g_assert(m); - g_assert(i); - g_assert(raddr); + assert(m); + assert(i); + assert(raddr); for (ia = i->addresses; ia; ia = ia->address_next) if (avahi_address_cmp(&ia->address, raddr) == 0) @@ -207,10 +221,10 @@ static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterfa return NULL; } -static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) { +static int netlink_list_items(AvahiNetlink *nl, uint16_t type, unsigned *ret_seq) { struct nlmsghdr *n; struct rtgenmsg *gen; - guint8 req[1024]; + uint8_t req[1024]; memset(&req, 0, sizeof(req)); n = (struct nlmsghdr*) req; @@ -229,15 +243,17 @@ static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) { static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) { AvahiInterface *i; - g_assert(m); - g_assert(hw); - g_assert(protocol != AVAHI_PROTO_UNSPEC); + assert(m); + assert(hw); + assert(protocol != AVAHI_PROTO_UNSPEC); - i = g_new(AvahiInterface, 1); + if (!(i = avahi_new(AvahiInterface, 1))) + goto fail; /* OOM */ + i->monitor = m; i->hardware = hw; i->protocol = protocol; - i->announcing = FALSE; + i->announcing = 0; AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses); AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements); @@ -247,15 +263,33 @@ static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiP i->query_scheduler = avahi_query_scheduler_new(i); i->probe_scheduler = avahi_probe_scheduler_new(i); + if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler) + goto fail; /* OOM */ + AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i); AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i); + + return; +fail: + + if (i) { + if (i->cache) + avahi_cache_free(i->cache); + if (i->response_scheduler) + avahi_response_scheduler_free(i->response_scheduler); + if (i->query_scheduler) + avahi_query_scheduler_free(i->query_scheduler); + if (i->probe_scheduler) + avahi_probe_scheduler_free(i->probe_scheduler); + } + } static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) { - gboolean b; + int b; - g_assert(m); - g_assert(i); + assert(m); + assert(i); b = avahi_interface_relevant(i); @@ -267,7 +301,7 @@ static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i if (i->protocol == AVAHI_PROTO_INET6) avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index); - i->announcing = TRUE; + i->announcing = 1; avahi_announce_interface(m->server, i); avahi_browser_new_interface(m->server, i); } else if (!b && i->announcing) { @@ -278,32 +312,32 @@ static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i if (i->protocol == AVAHI_PROTO_INET6) avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index); - avahi_goodbye_interface(m->server, i, FALSE); + avahi_goodbye_interface(m->server, i, 0); avahi_response_scheduler_clear(i->response_scheduler); avahi_query_scheduler_clear(i->query_scheduler); avahi_probe_scheduler_clear(i->probe_scheduler); avahi_cache_flush(i->cache); - i->announcing = FALSE; + i->announcing = 0; } } static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) { AvahiInterface *i; - g_assert(m); - g_assert(hw); + assert(m); + assert(hw); for (i = hw->interfaces; i; i = i->by_hardware_next) check_interface_relevant(m, i); } -static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { +static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, void* userdata) { AvahiInterfaceMonitor *m = userdata; - g_assert(m); - g_assert(n); - g_assert(m->netlink == nl); + assert(m); + assert(n); + assert(m->netlink == nl); if (n->nlmsg_type == RTM_NEWLINK) { struct ifinfomsg *ifinfomsg = NLMSG_DATA(n); @@ -314,8 +348,11 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user if (ifinfomsg->ifi_family != AF_UNSPEC) return; - if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) { - hw = g_new(AvahiHwInterface, 1); + if (!(hw = avahi_hashmap_lookup(m->hashmap, &ifinfomsg->ifi_index))) { + + if (!(hw = avahi_new(AvahiHwInterface, 1))) + return; /* OOM */ + hw->monitor = m; hw->name = NULL; hw->flags = 0; @@ -327,7 +364,7 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces); AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw); - g_hash_table_insert(m->hash_table, &hw->index, hw); + avahi_hashmap_insert(m->hashmap, &hw->index, hw); if (m->server->fd_ipv4 >= 0) new_interface(m, hw, AVAHI_PROTO_INET); @@ -343,12 +380,12 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user while (RTA_OK(a, l)) { switch(a->rta_type) { case IFLA_IFNAME: - g_free(hw->name); - hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a)); + avahi_free(hw->name); + hw->name = avahi_strndup(RTA_DATA(a), RTA_PAYLOAD(a)); break; case IFLA_MTU: - g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int)); + assert(RTA_PAYLOAD(a) == sizeof(unsigned int)); hw->mtu = *((unsigned int*) RTA_DATA(a)); break; @@ -368,7 +405,7 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user a = RTA_NEXT(a, l); } - update_hw_interface_rr(m, hw, FALSE); + update_hw_interface_rr(m, hw, 0); check_hw_interface_relevant(m, hw); } else if (n->nlmsg_type == RTM_DELLINK) { @@ -381,8 +418,8 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifinfomsg->ifi_index))) return; - update_hw_interface_rr(m, hw, TRUE); - free_hw_interface(m, hw, FALSE); + update_hw_interface_rr(m, hw, 1); + free_hw_interface(m, hw, 0); } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) { @@ -391,7 +428,7 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user struct rtattr *a = NULL; size_t l; AvahiAddress raddr; - gboolean raddr_valid = FALSE; + int raddr_valid = 0; if (ifaddrmsg->ifa_family != AVAHI_PROTO_INET && ifaddrmsg->ifa_family != AVAHI_PROTO_INET6) return; @@ -413,7 +450,7 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user return; memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a)); - raddr_valid = TRUE; + raddr_valid = 1; break; @@ -431,7 +468,9 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user AvahiInterfaceAddress *addr; if (!(addr = get_address(m, i, &raddr))) { - addr = g_new(AvahiInterfaceAddress, 1); + if (!(addr = avahi_new(AvahiInterfaceAddress, 1))) + return; /* OOM */ + addr->monitor = m; addr->address = raddr; addr->interface = i; @@ -444,14 +483,14 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user addr->scope = ifaddrmsg->ifa_scope; addr->prefix_len = ifaddrmsg->ifa_prefixlen; - update_address_rr(m, addr, FALSE); + update_address_rr(m, addr, 0); } else { AvahiInterfaceAddress *addr; if (!(addr = get_address(m, i, &raddr))) return; - update_address_rr(m, addr, TRUE); + update_address_rr(m, addr, 1); free_address(m, addr); } @@ -482,12 +521,14 @@ static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer user AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) { AvahiInterfaceMonitor *m = NULL; - m = g_new0(AvahiInterfaceMonitor, 1); + if (!(m = avahi_new0(AvahiInterfaceMonitor, 1))) + return NULL; /* OOM */ + m->server = s; - if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, netlink_callback, m))) + if (!(m->netlink = avahi_netlink_new(s->poll_api, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, netlink_callback, m))) goto fail; - m->hash_table = g_hash_table_new(g_int_hash, g_int_equal); + m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL); AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces); AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces); @@ -505,30 +546,30 @@ fail: } void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) { - g_assert(m); + assert(m); while (m->list != LIST_DONE) { - if (!avahi_netlink_work(m->netlink, TRUE)) + if (!avahi_netlink_work(m->netlink, 1)) break; } } void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) { - g_assert(m); + assert(m); while (m->hw_interfaces) - free_hw_interface(m, m->hw_interfaces, TRUE); + free_hw_interface(m, m->hw_interfaces, 1); - g_assert(!m->interfaces); + assert(!m->interfaces); if (m->netlink) avahi_netlink_free(m->netlink); - if (m->hash_table) - g_hash_table_destroy(m->hash_table); + if (m->hashmap) + avahi_hashmap_free(m->hashmap); - g_free(m); + avahi_free(m); } @@ -536,9 +577,9 @@ AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw; AvahiInterface *i; - g_assert(m); - g_assert(idx > 0); - g_assert(protocol != AVAHI_PROTO_UNSPEC); + assert(m); + assert(idx > 0); + assert(protocol != AVAHI_PROTO_UNSPEC); if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx))) return NULL; @@ -551,22 +592,21 @@ AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, } AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) { - g_assert(m); - g_assert(idx > 0); + assert(m); + assert(idx > 0); - return g_hash_table_lookup(m->hash_table, &idx); + return avahi_hashmap_lookup(m->hashmap, &idx); } - -void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) { - g_assert(i); - g_assert(p); +void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) { + assert(i); + assert(p); /* char t[64]; */ if (!avahi_interface_relevant(i)) return; - g_assert(!a || a->family == i->protocol); + assert(!a || a->family == i->protocol); /* if (a) */ /* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */ @@ -580,67 +620,70 @@ void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, c } void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) { - g_assert(i); - g_assert(p); + assert(i); + assert(p); avahi_interface_send_packet_unicast(i, p, NULL, 0); } -gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) { - g_assert(i); - g_assert(key); +int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) { + assert(i); + assert(key); if (avahi_interface_relevant(i)) return avahi_query_scheduler_post(i->query_scheduler, key, immediately); - return FALSE; + return 0; } -gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) { - g_assert(i); - g_assert(record); +int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) { + assert(i); + assert(record); if (avahi_interface_relevant(i)) return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately); - return FALSE; + return 0; } -gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) { - g_assert(i); - g_assert(record); +int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) { + assert(i); + assert(record); if (avahi_interface_relevant(i)) return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately); - return FALSE; + return 0; } -void avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, gpointer userdata) { +int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) { AvahiInterface *i; - g_assert(m); + assert(m); for (i = m->interfaces; i; i = i->interface_next) { if (avahi_interface_relevant(i)) { char ln[256]; snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%i ;;;", i->hardware->name, i->protocol); callback(ln, userdata); - avahi_cache_dump(i->cache, callback, userdata); + if (avahi_cache_dump(i->cache, callback, userdata) < 0) + return -1; } } + + return 0; } -gboolean avahi_interface_relevant(AvahiInterface *i) { +int avahi_interface_relevant(AvahiInterface *i) { AvahiInterfaceAddress *a; - gboolean relevant_address; + int relevant_address; - g_assert(i); + assert(i); - relevant_address = FALSE; + relevant_address = 0; for (a = i->addresses; a; a = a->address_next) if (avahi_interface_address_relevant(a)) { - relevant_address = TRUE; + relevant_address = 1; break; } @@ -660,28 +703,28 @@ gboolean avahi_interface_relevant(AvahiInterface *i) { relevant_address; } -gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { - g_assert(a); +int avahi_interface_address_relevant(AvahiInterfaceAddress *a) { + assert(a); return a->scope == RT_SCOPE_UNIVERSE; } -gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) { - g_assert(i); +int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) { + assert(i); if (idx > 0 && idx != i->hardware->index) - return FALSE; + return 0; if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol) - return FALSE; + return 0; - return TRUE; + return 1; } -void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) { - g_assert(m); - g_assert(callback); +void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) { + assert(m); + assert(callback); if (interface > 0) { if (protocol != AVAHI_PROTO_UNSPEC) { @@ -709,59 +752,59 @@ void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interfa } } -void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove_rrs) { +void avahi_update_host_rrs(AvahiInterfaceMonitor *m, int remove_rrs) { AvahiHwInterface *hw; - g_assert(m); + assert(m); for (hw = m->hw_interfaces; hw; hw = hw->hardware_next) update_hw_interface_rr(m, hw, remove_rrs); } -gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) { +int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) { AvahiInterface *i; AvahiInterfaceAddress *ia; - g_assert(m); - g_assert(a); + assert(m); + assert(a); for (i = m->interfaces; i; i = i->interface_next) for (ia = i->addresses; ia; ia = ia->address_next) if (avahi_address_cmp(a, &ia->address) == 0) - return TRUE; + return 1; - return FALSE; + return 0; } -gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) { +int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) { AvahiInterfaceAddress *ia; - g_assert(i); - g_assert(a); + assert(i); + assert(a); if (a->family != i->protocol) - return FALSE; + return 0; for (ia = i->addresses; ia; ia = ia->address_next) { if (a->family == AVAHI_PROTO_INET) { - guint32 m; + uint32_t m; - m = ~(((guint32) -1) >> ia->prefix_len); + m = ~(((uint32_t) -1) >> ia->prefix_len); - if ((g_ntohl(a->data.ipv4.address) & m) == (g_ntohl(ia->address.data.ipv4.address) & m)) - return TRUE; + if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m)) + return 1; } else { - guint j; - guchar pl; - g_assert(a->family == AVAHI_PROTO_INET6); + unsigned j; + unsigned char pl; + assert(a->family == AVAHI_PROTO_INET6); pl = ia->prefix_len; for (j = 0; j < 16; j++) { - guint8 m; + uint8_t m; if (pl == 0) - return TRUE; + return 1; if (pl >= 8) { m = 0xFF; @@ -777,5 +820,5 @@ gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress * } } - return FALSE; + return 0; } diff --git a/avahi-core/iface.h b/avahi-core/iface.h index 47c54ba..627e13d 100644 --- a/avahi-core/iface.h +++ b/avahi-core/iface.h @@ -22,8 +22,6 @@ USA. ***/ -#include - typedef struct AvahiInterfaceMonitor AvahiInterfaceMonitor; typedef struct AvahiInterfaceAddress AvahiInterfaceAddress; typedef struct AvahiInterface AvahiInterface; @@ -31,6 +29,7 @@ typedef struct AvahiHwInterface AvahiHwInterface; #include #include + #include "server.h" #include "netlink.h" #include "cache.h" @@ -45,12 +44,12 @@ typedef struct AvahiHwInterface AvahiHwInterface; struct AvahiInterfaceMonitor { AvahiServer *server; AvahiNetlink *netlink; - GHashTable *hash_table; + AvahiHashmap *hashmap; AVAHI_LLIST_HEAD(AvahiInterface, interfaces); AVAHI_LLIST_HEAD(AvahiHwInterface, hw_interfaces); - guint query_addr_seq, query_link_seq; + unsigned query_addr_seq, query_link_seq; enum { LIST_IFACE, @@ -63,13 +62,13 @@ struct AvahiHwInterface { AVAHI_LLIST_FIELDS(AvahiHwInterface, hardware); AvahiInterfaceMonitor *monitor; - gchar *name; + char *name; AvahiIfIndex index; - guint flags; - guint mtu; + unsigned flags; + unsigned mtu; - guint8 mac_address[AVAHI_MAX_MAC_ADDRESS]; - guint mac_address_size; + uint8_t mac_address[AVAHI_MAX_MAC_ADDRESS]; + size_t mac_address_size; AvahiEntryGroup *entry_group; @@ -83,7 +82,7 @@ struct AvahiInterface { AvahiHwInterface *hardware; AvahiProtocol protocol; - gboolean announcing; + int announcing; AvahiCache *cache; AvahiQueryScheduler *query_scheduler; @@ -98,9 +97,9 @@ struct AvahiInterfaceAddress { AVAHI_LLIST_FIELDS(AvahiInterfaceAddress, address); AvahiInterfaceMonitor *monitor; - guchar flags; - guchar scope; - guchar prefix_len; + unsigned char flags; + unsigned char scope; + unsigned char prefix_len; AvahiAddress address; AvahiEntryGroup *entry_group; @@ -113,31 +112,30 @@ void avahi_interface_monitor_free(AvahiInterfaceMonitor *m); void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m); AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol); -AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint idx); +AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, int idx); void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p); -void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port); +void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port); -gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *k, gboolean immediately); -gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately); -gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *p, gboolean immediately); +int avahi_interface_post_query(AvahiInterface *i, AvahiKey *k, int immediately); +int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately); +int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *p, int immediately); -void avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, gpointer userdata); +int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata); -gboolean avahi_interface_relevant(AvahiInterface *i); -gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a); +int avahi_interface_relevant(AvahiInterface *i); +int avahi_interface_address_relevant(AvahiInterfaceAddress *a); -gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol); +int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol); -typedef void (*AvahiInterfaceMonitorWalkCallback)(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata); +typedef void (*AvahiInterfaceMonitorWalkCallback)(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata); -void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata); - -void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove_rrs); +void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata); -gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a); +void avahi_update_host_rrs(AvahiInterfaceMonitor *m, int remove_rrs); -gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a); +int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a); +int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a); #endif diff --git a/avahi-core/log.c b/avahi-core/log.c index 09b87da..bfd4021 100644 --- a/avahi-core/log.c +++ b/avahi-core/log.c @@ -34,7 +34,7 @@ void avahi_set_log_function(AvahiLogFunction function) { log_function = function; } -void avahi_log_ap(AvahiLogLevel level, const gchar*format, va_list ap) { +void avahi_log_ap(AvahiLogLevel level, const char*format, va_list ap) { char txt[256]; vsnprintf(txt, sizeof(txt), format, ap); @@ -45,42 +45,42 @@ void avahi_log_ap(AvahiLogLevel level, const gchar*format, va_list ap) { fprintf(stderr, "%s\n", txt); } -void avahi_log(AvahiLogLevel level, const gchar*format, ...) { +void avahi_log(AvahiLogLevel level, const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(level, format, ap); va_end(ap); } -void avahi_log_error(const gchar*format, ...) { +void avahi_log_error(const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(AVAHI_LOG_ERROR, format, ap); va_end(ap); } -void avahi_log_warn(const gchar*format, ...) { +void avahi_log_warn(const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(AVAHI_LOG_WARN, format, ap); va_end(ap); } -void avahi_log_notice(const gchar*format, ...) { +void avahi_log_notice(const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(AVAHI_LOG_NOTICE, format, ap); va_end(ap); } -void avahi_log_info(const gchar*format, ...) { +void avahi_log_info(const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(AVAHI_LOG_INFO, format, ap); va_end(ap); } -void avahi_log_debug(const gchar*format, ...) { +void avahi_log_debug(const char*format, ...) { va_list ap; va_start(ap, format); avahi_log_ap(AVAHI_LOG_DEBUG, format, ap); diff --git a/avahi-core/log.h b/avahi-core/log.h index 064e418..bb32ccc 100644 --- a/avahi-core/log.h +++ b/avahi-core/log.h @@ -23,23 +23,14 @@ ***/ #include -#include + #include +#include /** \file log.h Extensible logging subsystem */ AVAHI_C_DECL_BEGIN -#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 - -#define AVAHI_GCC_PRINTF_ATTR12 AVAHI_GCC_PRINTF_ATTR(1,2) -#define AVAHI_GCC_PRINTF_ATTR23 AVAHI_GCC_PRINTF_ATTR(2,3) - /** Log level for avahi_log_xxx() */ typedef enum { AVAHI_LOG_ERROR = 0, /**< Error messages */ @@ -51,7 +42,7 @@ typedef enum { } AvahiLogLevel; /** Prototype for a user supplied log function */ -typedef void (*AvahiLogFunction)(AvahiLogLevel level, const gchar *txt); +typedef void (*AvahiLogFunction)(AvahiLogLevel level, const char *txt); /** Set a user supplied log function, replacing the default which * prints to log messages unconditionally to STDERR. Pass NULL for @@ -59,25 +50,26 @@ typedef void (*AvahiLogFunction)(AvahiLogLevel level, const gchar *txt); void avahi_set_log_function(AvahiLogFunction function); /** Issue a log message using a va_list object */ -void avahi_log_ap(AvahiLogLevel level, const gchar *format, va_list ap); +void avahi_log_ap(AvahiLogLevel level, const char *format, va_list ap); /** Issue a log message by passing a log level and a format string */ -void avahi_log(AvahiLogLevel level, const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR23; +void avahi_log(AvahiLogLevel level, const char*format, ...) AVAHI_GCC_PRINTF_ATTR23; /** Shortcut for avahi_log(AVAHI_LOG_ERROR, ...) */ -void avahi_log_error(const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR12; +void avahi_log_error(const char*format, ...) AVAHI_GCC_PRINTF_ATTR12; /** Shortcut for avahi_log(AVAHI_LOG_WARN, ...) */ -void avahi_log_warn(const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR12; +void avahi_log_warn(const char*format, ...) AVAHI_GCC_PRINTF_ATTR12; /** Shortcut for avahi_log(AVAHI_LOG_NOTICE, ...) */ -void avahi_log_notice(const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR12; +void avahi_log_notice(const char*format, ...) AVAHI_GCC_PRINTF_ATTR12; /** Shortcut for avahi_log(AVAHI_LOG_INFO, ...) */ -void avahi_log_info(const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR12; +void avahi_log_info(const char*format, ...) AVAHI_GCC_PRINTF_ATTR12; /** Shortcut for avahi_log(AVAHI_LOG_DEBUG, ...) */ -void avahi_log_debug(const gchar*format, ...) AVAHI_GCC_PRINTF_ATTR12; +void avahi_log_debug(const char*format, ...) AVAHI_GCC_PRINTF_ATTR12; + AVAHI_C_DECL_END diff --git a/avahi-core/netlink.c b/avahi-core/netlink.c index 119e195..92d614f 100644 --- a/avahi-core/netlink.c +++ b/avahi-core/netlink.c @@ -27,24 +27,26 @@ #include #include #include +#include +#include #include "netlink.h" #include "log.h" struct AvahiNetlink { - GMainContext *context; - gint fd; - guint seq; - GPollFD poll_fd; - GSource *source; - void (*callback) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata); - gpointer userdata; - guint8* buffer; - guint buffer_length; + int fd; + unsigned seq; + AvahiNetlinkCallback callback; + void* userdata; + uint8_t* buffer; + size_t buffer_length; + + AvahiPoll *poll_api; + AvahiWatch *watch; }; -gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) { - g_assert(nl); +int avahi_netlink_work(AvahiNetlink *nl, int block) { + assert(nl); for (;;) { ssize_t bytes; @@ -54,10 +56,10 @@ gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) { if ((bytes = recv(nl->fd, nl->buffer, nl->buffer_length, block ? 0 : MSG_DONTWAIT)) < 0) { if (errno == EAGAIN || errno == EINTR) - return TRUE; + return 1; - avahi_log_warn("NETLINK: recv() failed: %s", strerror(errno)); - return FALSE; + avahi_log_error(__FILE__": recv() failed: %s", strerror(errno)); + return 0; } break; @@ -68,8 +70,8 @@ gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) { if (nl->callback) { for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) { if (!NLMSG_OK(p, (size_t) bytes)) { - avahi_log_warn("NETLINK: packet truncated"); - return FALSE; + avahi_log_warn(__FILE__": packet truncated"); + return 0; } nl->callback(nl, p, nl->userdata); @@ -77,56 +79,30 @@ gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) { } if (block) - return TRUE; + return 1; } } -static gboolean prepare_func(GSource *source, gint *timeout) { - g_assert(source); - g_assert(timeout); - - *timeout = -1; - return FALSE; -} - -static gboolean check_func(GSource *source) { - AvahiNetlink* nl; - g_assert(source); +static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) { + AvahiNetlink *nl = userdata; - nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource))); - g_assert(nl); - - return nl->poll_fd.revents & (G_IO_IN|G_IO_HUP|G_IO_ERR); -} + assert(w); + assert(nl); + assert(fd == nl->fd); -static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { - AvahiNetlink* nl; - g_assert(source); - - nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource))); - g_assert(nl); - - return avahi_netlink_work(nl, FALSE); + avahi_netlink_work(nl, 0); } -AvahiNetlink *avahi_netlink_new(GMainContext *context, gint priority, guint32 groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata), gpointer userdata) { - int fd; +AvahiNetlink *avahi_netlink_new(AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) { + int fd = -1; struct sockaddr_nl addr; - AvahiNetlink *nl; - - static GSourceFuncs source_funcs = { - prepare_func, - check_func, - dispatch_func, - NULL, - NULL, - NULL - }; - - g_assert(cb); + AvahiNetlink *nl = NULL; + + assert(poll_api); + assert(cb); if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) { - g_critical("NETLINK: socket(PF_NETLINK): %s", strerror(errno)); + avahi_log_error(__FILE__": socket(PF_NETLINK): %s", strerror(errno)); return NULL; } @@ -136,54 +112,70 @@ AvahiNetlink *avahi_netlink_new(GMainContext *context, gint priority, guint32 gr addr.nl_pid = getpid(); if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - close(fd); - g_critical("bind(): %s", strerror(errno)); - return NULL; + avahi_log_error(__FILE__": bind(): %s", strerror(errno)); + goto fail; + } + + if (!(nl = avahi_new(AvahiNetlink, 1))) { + avahi_log_error(__FILE__": avahi_new() failed."); + goto fail; } - nl = g_new(AvahiNetlink, 1); - g_main_context_ref(nl->context = context ? context : g_main_context_default()); + nl->poll_api = poll_api; nl->fd = fd; nl->seq = 0; nl->callback = cb; nl->userdata = userdata; - nl->buffer = g_new(guint8, nl->buffer_length = 64*1024); - nl->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiNetlink*)); - *((AvahiNetlink**) (((guint8*) nl->source) + sizeof(GSource))) = nl; + if (!(nl->buffer = avahi_new(uint8_t, nl->buffer_length = 64*1024))) { + avahi_log_error(__FILE__": avahi_new() failed."); + goto fail; + } - g_source_set_priority(nl->source, priority); - - memset(&nl->poll_fd, 0, sizeof(GPollFD)); - nl->poll_fd.fd = fd; - nl->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP; - g_source_add_poll(nl->source, &nl->poll_fd); - - g_source_attach(nl->source, nl->context); + if (!(nl->watch = poll_api->watch_new(poll_api, fd, AVAHI_WATCH_IN, socket_event, nl))) { + avahi_log_error(__FILE__": Failed to create watch."); + goto fail; + } return nl; + +fail: + + if (fd >= 0) + close(fd); + + if (nl) { + if (nl->buffer) + avahi_free(nl); + + avahi_free(nl); + } + + return NULL; } void avahi_netlink_free(AvahiNetlink *nl) { - g_assert(nl); - - g_source_destroy(nl->source); - g_source_unref(nl->source); - g_main_context_unref(nl->context); - close(nl->fd); - g_free(nl->buffer); - g_free(nl); + assert(nl); + + if (nl->watch) + nl->poll_api->watch_free(nl->watch); + + if (nl->fd >= 0) + close(nl->fd); + + avahi_free(nl->buffer); + avahi_free(nl); } -int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, guint *ret_seq) { - g_assert(nl); - g_assert(m); +int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq) { + assert(nl); + assert(m); m->nlmsg_seq = nl->seq++; m->nlmsg_flags |= NLM_F_ACK; if (send(nl->fd, m, m->nlmsg_len, 0) < 0) { - avahi_log_warn("NETLINK: send(): %s\n", strerror(errno)); + avahi_log_error(__FILE__": send(): %s\n", strerror(errno)); return -1; } diff --git a/avahi-core/netlink.h b/avahi-core/netlink.h index 748eb5d..4768b6e 100644 --- a/avahi-core/netlink.h +++ b/avahi-core/netlink.h @@ -26,15 +26,15 @@ #include #include -#include +#include typedef struct AvahiNetlink AvahiNetlink; -AvahiNetlink *avahi_netlink_new(GMainContext *c, gint priority, guint32 groups, void (*cb) (AvahiNetlink *n, struct nlmsghdr *m, gpointer userdata), gpointer userdata); -void avahi_netlink_free(AvahiNetlink *n); - -int avahi_netlink_send(AvahiNetlink *n, struct nlmsghdr *m, guint *ret_seq); +typedef void (*AvahiNetlinkCallback)(AvahiNetlink *n, struct nlmsghdr *m, void* userdata); -gboolean avahi_netlink_work(AvahiNetlink *n, gboolean block); +AvahiNetlink *avahi_netlink_new(AvahiPoll *poll_api, uint32_t groups, AvahiNetlinkCallback callback, void* userdata); +void avahi_netlink_free(AvahiNetlink *n); +int avahi_netlink_send(AvahiNetlink *n, struct nlmsghdr *m, unsigned *ret_seq); +int avahi_netlink_work(AvahiNetlink *n, int block); #endif diff --git a/avahi-core/prioq-test.c b/avahi-core/prioq-test.c index 28c734b..0e43702 100644 --- a/avahi-core/prioq-test.c +++ b/avahi-core/prioq-test.c @@ -26,16 +26,20 @@ #include #include #include +#include #include "prioq.h" -static gint compare_int(gconstpointer a, gconstpointer b) { - gint i = GPOINTER_TO_INT(a), j = GPOINTER_TO_INT(b); +#define POINTER_TO_INT(p) ((int) (p)) +#define INT_TO_POINTER(i) ((void*) (i)) + +static int compare_int(const void* a, const void* b) { + int i = POINTER_TO_INT(a), j = POINTER_TO_INT(b); return i < j ? -1 : (i > j ? 1 : 0); } -static int compare_ptr(gconstpointer a, gconstpointer b) { +static int compare_ptr(const void* a, const void* b) { return a < b ? -1 : (a > b ? 1 : 0); } @@ -44,28 +48,28 @@ static void rec(AvahiPrioQueueNode *n) { return; if (n->left) - g_assert(n->left->parent == n); + assert(n->left->parent == n); if (n->right) - g_assert(n->right->parent == n); + assert(n->right->parent == n); if (n->parent) { - g_assert(n->parent->left == n || n->parent->right == n); + assert(n->parent->left == n || n->parent->right == n); if (n->parent->left == n) - g_assert(n->next == n->parent->right); + assert(n->next == n->parent->right); } if (!n->next) { - g_assert(n->queue->last == n); + assert(n->queue->last == n); if (n->parent && n->parent->left == n) - g_assert(n->parent->right == NULL); + assert(n->parent->right == NULL); } if (n->parent) { - int a = GPOINTER_TO_INT(n->parent->data), b = GPOINTER_TO_INT(n->data); + int a = POINTER_TO_INT(n->parent->data), b = POINTER_TO_INT(n->data); if (a > b) { printf("%i <= %i: NO\n", a, b); abort(); @@ -78,7 +82,7 @@ static void rec(AvahiPrioQueueNode *n) { int main(int argc, char *argv[]) { AvahiPrioQueue *q, *q2; - gint i; + int i; q = avahi_prio_queue_new(compare_int); q2 = avahi_prio_queue_new(compare_ptr); @@ -86,15 +90,15 @@ int main(int argc, char *argv[]) { srand(time(NULL)); for (i = 0; i < 10000; i++) - avahi_prio_queue_put(q2, avahi_prio_queue_put(q, GINT_TO_POINTER(random() & 0xFFFF))); + avahi_prio_queue_put(q2, avahi_prio_queue_put(q, INT_TO_POINTER(random() & 0xFFFF))); while (q2->root) { rec(q->root); rec(q2->root); - g_assert(q->n_nodes == q2->n_nodes); + assert(q->n_nodes == q2->n_nodes); - printf("%i\n", GPOINTER_TO_INT(((AvahiPrioQueueNode*)q2->root->data)->data)); + printf("%i\n", POINTER_TO_INT(((AvahiPrioQueueNode*)q2->root->data)->data)); avahi_prio_queue_remove(q, q2->root->data); avahi_prio_queue_remove(q2, q2->root); @@ -103,11 +107,11 @@ int main(int argc, char *argv[]) { /* prev = 0; */ /* while (q->root) { */ -/* gint v = GPOINTER_TO_INT(q->root->data); */ +/* int v = GPOINTER_TO_INT(q->root->data); */ /* rec(q->root); */ /* printf("%i\n", v); */ /* avahi_prio_queue_remove(q, q->root); */ -/* g_assert(v >= prev); */ +/* assert(v >= prev); */ /* prev = v; */ /* } */ diff --git a/avahi-core/prioq.c b/avahi-core/prioq.c index ba98d20..8d91d87 100644 --- a/avahi-core/prioq.c +++ b/avahi-core/prioq.c @@ -23,39 +23,47 @@ #include #endif +#include +#include + +#include + #include "prioq.h" -AvahiPrioQueue* avahi_prio_queue_new(gint (*compare) (gconstpointer a, gconstpointer b)) { +AvahiPrioQueue* avahi_prio_queue_new(AvahiPQCompareFunc compare) { AvahiPrioQueue *q; - g_assert(compare); + assert(compare); - q = g_new(AvahiPrioQueue, 1); + if (!(q = avahi_new(AvahiPrioQueue, 1))) + return NULL; /* OOM */ + q->root = q->last = NULL; q->n_nodes = 0; q->compare = compare; + return q; } void avahi_prio_queue_free(AvahiPrioQueue *q) { - g_assert(q); + assert(q); while (q->last) avahi_prio_queue_remove(q, q->last); - g_assert(!q->n_nodes); - g_free(q); + assert(!q->n_nodes); + avahi_free(q); } -static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, guint x, guint y) { - guint r; +static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, unsigned x, unsigned y) { + unsigned r; AvahiPrioQueueNode *n; - g_assert(q); + assert(q); n = q->root; - g_assert(n); + assert(n); for (r = 0; r < y; r++) { - g_assert(n); + assert(n); if ((x >> (y-r-1)) & 1) n = n->right; @@ -63,19 +71,19 @@ static AvahiPrioQueueNode* get_node_at_xy(AvahiPrioQueue *q, guint x, guint y) { n = n->left; } - g_assert(n->x == x); - g_assert(n->y == y); + assert(n->x == x); + assert(n->y == y); return n; } static void exchange_nodes(AvahiPrioQueue *q, AvahiPrioQueueNode *a, AvahiPrioQueueNode *b) { AvahiPrioQueueNode *l, *r, *p, *ap, *an, *bp, *bn; - gint t; - g_assert(q); - g_assert(a); - g_assert(b); - g_assert(a != b); + unsigned t; + assert(q); + assert(a); + assert(b); + assert(a != b); /* Swap positions */ t = a->x; a->x = b->x; b->x = t; @@ -250,8 +258,9 @@ static void exchange_nodes(AvahiPrioQueue *q, AvahiPrioQueueNode *a, AvahiPrioQu /* Move a node to the correct position */ void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) { - g_assert(q); - g_assert(n); + assert(q); + assert(n); + assert(n->queue == q); /* Move up until the position is OK */ while (n->parent && q->compare(n->parent->data, n->data) > 0) @@ -263,7 +272,7 @@ void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) { if (!(min = n->left)) { /* No children */ - g_assert(!n->right); + assert(!n->right); break; } @@ -280,22 +289,24 @@ void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n) { } } -AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) { +AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, void* data) { AvahiPrioQueueNode *n; - g_assert(q); + assert(q); - n = g_new(AvahiPrioQueueNode, 1); + if (!(n = avahi_new(AvahiPrioQueueNode, 1))) + return NULL; /* OOM */ + n->queue = q; n->data = data; if (q->last) { - g_assert(q->root); - g_assert(q->n_nodes); + assert(q->root); + assert(q->n_nodes); n->y = q->last->y; n->x = q->last->x+1; - if (n->x >= ((guint) 1 << n->y)) { + if (n->x >= ((unsigned) 1 << n->y)) { n->x = 0; n->y++; } @@ -303,7 +314,7 @@ AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) { q->last->next = n; n->prev = q->last; - g_assert(n->y > 0); + assert(n->y > 0); n->parent = get_node_at_xy(q, n->x/2, n->y-1); if (n->x & 1) @@ -311,8 +322,8 @@ AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) { else n->parent->left = n; } else { - g_assert(!q->root); - g_assert(!q->n_nodes); + assert(!q->root); + assert(!q->n_nodes); n->y = n->x = 0; q->root = n; @@ -329,8 +340,9 @@ AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data) { } void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n) { - g_assert(q); - g_assert(n); + assert(q); + assert(n); + assert(q == n->queue); if (n != q->last) { AvahiPrioQueueNode *replacement = q->last; @@ -340,39 +352,39 @@ void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n) { return; } - g_assert(n == q->last); - g_assert(!n->next); - g_assert(!n->left); - g_assert(!n->right); + assert(n == q->last); + assert(!n->next); + assert(!n->left); + assert(!n->right); q->last = n->prev; if (n->prev) { n->prev->next = NULL; - g_assert(n->parent); + assert(n->parent); } else - g_assert(!n->parent); + assert(!n->parent); if (n->parent) { - g_assert(n->prev); + assert(n->prev); if (n->parent->left == n) { - g_assert(n->parent->right == NULL); + assert(n->parent->right == NULL); n->parent->left = NULL; } else { - g_assert(n->parent->right == n); - g_assert(n->parent->left != NULL); + assert(n->parent->right == n); + assert(n->parent->left != NULL); n->parent->right = NULL; } } else { - g_assert(q->root == n); - g_assert(!n->prev); - g_assert(q->n_nodes == 1); + assert(q->root == n); + assert(!n->prev); + assert(q->n_nodes == 1); q->root = NULL; } - g_free(n); + avahi_free(n); - g_assert(q->n_nodes > 0); + assert(q->n_nodes > 0); q->n_nodes--; } diff --git a/avahi-core/prioq.h b/avahi-core/prioq.h index e6105d8..ace92f6 100644 --- a/avahi-core/prioq.h +++ b/avahi-core/prioq.h @@ -22,31 +22,28 @@ USA. ***/ -#include - typedef struct AvahiPrioQueue AvahiPrioQueue; - typedef struct AvahiPrioQueueNode AvahiPrioQueueNode; +typedef int (*AvahiPQCompareFunc)(const void* a, const void* b); + struct AvahiPrioQueue { AvahiPrioQueueNode *root, *last; - - guint n_nodes; - gint (*compare) (gconstpointer a, gconstpointer b); + unsigned n_nodes; + AvahiPQCompareFunc compare; }; struct AvahiPrioQueueNode { AvahiPrioQueue *queue; - gpointer data; - guint x, y; - + void* data; + unsigned x, y; AvahiPrioQueueNode *left, *right, *parent, *next, *prev; }; -AvahiPrioQueue* avahi_prio_queue_new(gint (*compare) (gconstpointer a, gconstpointer b)); +AvahiPrioQueue* avahi_prio_queue_new(AvahiPQCompareFunc compare); void avahi_prio_queue_free(AvahiPrioQueue *q); -AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, gpointer data); +AvahiPrioQueueNode* avahi_prio_queue_put(AvahiPrioQueue *q, void* data); void avahi_prio_queue_remove(AvahiPrioQueue *q, AvahiPrioQueueNode *n); void avahi_prio_queue_shuffle(AvahiPrioQueue *q, AvahiPrioQueueNode *n); diff --git a/avahi-core/probe-sched.c b/avahi-core/probe-sched.c index a375c7b..8a4d8b0 100644 --- a/avahi-core/probe-sched.c +++ b/avahi-core/probe-sched.c @@ -25,6 +25,7 @@ #include #include +#include #include "probe-sched.h" #include "log.h" @@ -38,8 +39,8 @@ struct AvahiProbeJob { AvahiProbeScheduler *scheduler; AvahiTimeEvent *time_event; - gboolean chosen; /* Use for packet assembling */ - gboolean done; + int chosen; /* Use for packet assembling */ + int done; struct timeval delivery; AvahiRecord *record; @@ -55,17 +56,21 @@ struct AvahiProbeScheduler { AVAHI_LLIST_HEAD(AvahiProbeJob, history); }; -static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) { +static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, int done) { AvahiProbeJob *pj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); - pj = g_new(AvahiProbeJob, 1); + if (!(pj = avahi_new(AvahiProbeJob, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; /* OOM */ + } + pj->scheduler = s; pj->record = avahi_record_ref(record); pj->time_event = NULL; - pj->chosen = FALSE; + pj->chosen = 0; if ((pj->done = done)) AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj); @@ -76,10 +81,10 @@ static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gbool } static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) { - g_assert(pj); + assert(pj); if (pj->time_event) - avahi_time_event_queue_remove(s->time_event_queue, pj->time_event); + avahi_time_event_free(pj->time_event); if (pj->done) AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj); @@ -87,35 +92,35 @@ static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) { AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj); avahi_record_unref(pj->record); - g_free(pj); + avahi_free(pj); } -static void elapse_callback(AvahiTimeEvent *e, gpointer data); +static void elapse_callback(AvahiTimeEvent *e, void* data); -static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) { +static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, unsigned msec, unsigned jitter) { struct timeval tv; - g_assert(s); - g_assert(pj); + assert(s); + assert(pj); avahi_elapse_time(&tv, msec, jitter); if (pj->time_event) - avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv); + avahi_time_event_update(pj->time_event, &tv); else - pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj); + pj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, pj); } static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) { - g_assert(s); - g_assert(pj); + assert(s); + assert(pj); - g_assert(!pj->done); + assert(!pj->done); AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj); AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj); - pj->done = TRUE; + pj->done = 1; job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0); gettimeofday(&pj->delivery, NULL); @@ -124,9 +129,13 @@ static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) { AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) { AvahiProbeScheduler *s; - g_assert(i); + assert(i); - s = g_new(AvahiProbeScheduler, 1); + if (!(s = avahi_new(AvahiProbeScheduler, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; + } + s->interface = i; s->time_event_queue = i->monitor->server->time_event_queue; @@ -137,14 +146,14 @@ AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) { } void avahi_probe_scheduler_free(AvahiProbeScheduler *s) { - g_assert(s); + assert(s); avahi_probe_scheduler_clear(s); - g_free(s); + avahi_free(s); } void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) { - g_assert(s); + assert(s); while (s->jobs) job_free(s, s->jobs); @@ -152,16 +161,16 @@ void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) { job_free(s, s->history); } -static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) { - guint size; +static int packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) { + size_t size; AvahiKey *k; - gboolean b; + int b; - g_assert(s); - g_assert(p); - g_assert(pj); + assert(s); + assert(p); + assert(pj); - g_assert(!pj->chosen); + assert(!pj->chosen); /* Estimate the size for this record */ size = @@ -170,15 +179,17 @@ static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p /* Too large */ if (size > avahi_dns_packet_space(p)) - return FALSE; + return 0; /* Create the probe query */ - k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY); - b = !!avahi_dns_packet_append_key(p, k, FALSE); - g_assert(b); + if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) + return 0; /* OOM */ + + b = !!avahi_dns_packet_append_key(p, k, 0); + assert(b); /* Mark this job for addition to the packet */ - pj->chosen = TRUE; + pj->chosen = 1; /* Scan for more jobs whith matching key pattern */ for (pj = s->jobs; pj; pj = pj->jobs_next) { @@ -194,21 +205,21 @@ static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p break; /* Mark this job for addition to the packet */ - pj->chosen = TRUE; + pj->chosen = 1; } avahi_key_unref(k); - return TRUE; + return 1; } -static void elapse_callback(AvahiTimeEvent *e, gpointer data) { +static void elapse_callback(AvahiTimeEvent *e, void* data) { AvahiProbeJob *pj = data, *next; AvahiProbeScheduler *s; AvahiDnsPacket *p; - guint n; + unsigned n; - g_assert(pj); + assert(pj); s = pj->scheduler; if (pj->done) { @@ -217,14 +228,15 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { return; } - p = avahi_dns_packet_new_query(s->interface->hardware->mtu); + if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu))) + return; /* OOM */ n = 1; /* Add the import probe */ if (!packet_add_probe_query(s, p, pj)) { - guint size; + size_t size; AvahiKey *k; - gboolean b; + int b; avahi_dns_packet_free(p); @@ -237,11 +249,16 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { if (size > AVAHI_DNS_PACKET_MAX_SIZE) size = AVAHI_DNS_PACKET_MAX_SIZE; - - p = avahi_dns_packet_new_query(size); - k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY); - b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0); + if (!(p = avahi_dns_packet_new_query(size))) + return; /* OOM */ + + if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) { + avahi_dns_packet_free(p); + return; /* OOM */ + } + + b = avahi_dns_packet_append_key(p, k, 0) && avahi_dns_packet_append_record(p, pj->record, 0, 0); avahi_key_unref(k); if (b) { @@ -281,12 +298,12 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { if (!pj->chosen) continue; - if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) { + if (!avahi_dns_packet_append_record(p, pj->record, 0, 0)) { avahi_log_warn("Bad probe size estimate!"); /* Unmark all following jobs */ for (; pj; pj = pj->jobs_next) - pj->chosen = FALSE; + pj->chosen = 0; break; } @@ -306,11 +323,11 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) { AvahiProbeJob *pj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); for (pj = s->jobs; pj; pj = pj->jobs_next) { - g_assert(!pj->done); + assert(!pj->done); if (avahi_record_equal_no_ttl(pj->record, record)) return pj; @@ -322,11 +339,11 @@ static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *re static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) { AvahiProbeJob *pj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); for (pj = s->history; pj; pj = pj->jobs_next) { - g_assert(pj->done); + assert(pj->done); if (avahi_record_equal_no_ttl(pj->record, record)) { /* Check whether this entry is outdated */ @@ -344,16 +361,16 @@ static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *reco return NULL; } -gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) { +int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately) { AvahiProbeJob *pj; struct timeval tv; - g_assert(s); - g_assert(record); - g_assert(!avahi_key_is_pattern(record->key)); + assert(s); + assert(record); + assert(!avahi_key_is_pattern(record->key)); if ((pj = find_history_job(s, record))) - return FALSE; + return 0; avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0); @@ -362,19 +379,21 @@ gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, if (avahi_timeval_compare(&tv, &pj->delivery) < 0) { /* If the new entry should be scheduled earlier, update the old entry */ pj->delivery = tv; - avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery); + avahi_time_event_update(pj->time_event, &pj->delivery); } - return TRUE; + return 1; } else { /* Create a new job and schedule it */ - pj = job_new(s, record, FALSE); + if (!(pj = job_new(s, record, 0))) + return 0; /* OOM */ + pj->delivery = tv; - pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj); + pj->time_event = avahi_time_event_new(s->time_event_queue, &pj->delivery, elapse_callback, pj); /* avahi_log_debug("Accepted new probe job."); */ - return TRUE; + return 1; } } diff --git a/avahi-core/probe-sched.h b/avahi-core/probe-sched.h index aae0f3a..3af1319 100644 --- a/avahi-core/probe-sched.h +++ b/avahi-core/probe-sched.h @@ -31,6 +31,6 @@ AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i); void avahi_probe_scheduler_free(AvahiProbeScheduler *s); void avahi_probe_scheduler_clear(AvahiProbeScheduler *s); -gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately); +int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately); #endif diff --git a/avahi-core/query-sched.c b/avahi-core/query-sched.c index 9a26dba..87553d6 100644 --- a/avahi-core/query-sched.c +++ b/avahi-core/query-sched.c @@ -24,7 +24,10 @@ #endif #include +#include + #include "query-sched.h" +#include "log.h" #define AVAHI_QUERY_HISTORY_MSEC 100 #define AVAHI_QUERY_DEFER_MSEC 100 @@ -36,7 +39,7 @@ struct AvahiQueryJob { AvahiQueryScheduler *scheduler; AvahiTimeEvent *time_event; - gboolean done; + int done; struct timeval delivery; AvahiKey *key; @@ -60,13 +63,17 @@ struct AvahiQueryScheduler { AVAHI_LLIST_HEAD(AvahiKnownAnswer, known_answers); }; -static AvahiQueryJob* job_new(AvahiQueryScheduler *s, AvahiKey *key, gboolean done) { +static AvahiQueryJob* job_new(AvahiQueryScheduler *s, AvahiKey *key, int done) { AvahiQueryJob *qj; - g_assert(s); - g_assert(key); + assert(s); + assert(key); - qj = g_new(AvahiQueryJob, 1); + if (!(qj = avahi_new(AvahiQueryJob, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; + } + qj->scheduler = s; qj->key = avahi_key_ref(key); qj->time_event = NULL; @@ -80,11 +87,11 @@ static AvahiQueryJob* job_new(AvahiQueryScheduler *s, AvahiKey *key, gboolean do } static void job_free(AvahiQueryScheduler *s, AvahiQueryJob *qj) { - g_assert(s); - g_assert(qj); + assert(s); + assert(qj); if (qj->time_event) - avahi_time_event_queue_remove(s->time_event_queue, qj->time_event); + avahi_time_event_free(qj->time_event); if (qj->done) AVAHI_LLIST_REMOVE(AvahiQueryJob, jobs, s->history, qj); @@ -92,35 +99,35 @@ static void job_free(AvahiQueryScheduler *s, AvahiQueryJob *qj) { AVAHI_LLIST_REMOVE(AvahiQueryJob, jobs, s->jobs, qj); avahi_key_unref(qj->key); - g_free(qj); + avahi_free(qj); } -static void elapse_callback(AvahiTimeEvent *e, gpointer data); +static void elapse_callback(AvahiTimeEvent *e, void* data); -static void job_set_elapse_time(AvahiQueryScheduler *s, AvahiQueryJob *qj, guint msec, guint jitter) { +static void job_set_elapse_time(AvahiQueryScheduler *s, AvahiQueryJob *qj, unsigned msec, unsigned jitter) { struct timeval tv; - g_assert(s); - g_assert(qj); + assert(s); + assert(qj); avahi_elapse_time(&tv, msec, jitter); if (qj->time_event) - avahi_time_event_queue_update(s->time_event_queue, qj->time_event, &tv); + avahi_time_event_update(qj->time_event, &tv); else - qj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, qj); + qj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, qj); } static void job_mark_done(AvahiQueryScheduler *s, AvahiQueryJob *qj) { - g_assert(s); - g_assert(qj); + assert(s); + assert(qj); - g_assert(!qj->done); + assert(!qj->done); AVAHI_LLIST_REMOVE(AvahiQueryJob, jobs, s->jobs, qj); AVAHI_LLIST_PREPEND(AvahiQueryJob, jobs, s->history, qj); - qj->done = TRUE; + qj->done = 1; job_set_elapse_time(s, qj, AVAHI_QUERY_HISTORY_MSEC, 0); gettimeofday(&qj->delivery, NULL); @@ -128,9 +135,13 @@ static void job_mark_done(AvahiQueryScheduler *s, AvahiQueryJob *qj) { AvahiQueryScheduler *avahi_query_scheduler_new(AvahiInterface *i) { AvahiQueryScheduler *s; - g_assert(i); + assert(i); - s = g_new(AvahiQueryScheduler, 1); + if (!(s = avahi_new(AvahiQueryScheduler, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; /* OOM */ + } + s->interface = i; s->time_event_queue = i->monitor->server->time_event_queue; @@ -142,15 +153,15 @@ AvahiQueryScheduler *avahi_query_scheduler_new(AvahiInterface *i) { } void avahi_query_scheduler_free(AvahiQueryScheduler *s) { - g_assert(s); + assert(s); - g_assert(!s->known_answers); + assert(!s->known_answers); avahi_query_scheduler_clear(s); - g_free(s); + avahi_free(s); } void avahi_query_scheduler_clear(AvahiQueryScheduler *s) { - g_assert(s); + assert(s); while (s->jobs) job_free(s, s->jobs); @@ -158,19 +169,23 @@ void avahi_query_scheduler_clear(AvahiQueryScheduler *s) { job_free(s, s->history); } -static gpointer known_answer_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) { +static void* known_answer_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) { AvahiQueryScheduler *s = userdata; AvahiKnownAnswer *ka; - g_assert(c); - g_assert(pattern); - g_assert(e); - g_assert(s); + assert(c); + assert(pattern); + assert(e); + assert(s); if (avahi_cache_entry_half_ttl(c, e)) return NULL; - ka = g_new0(AvahiKnownAnswer, 1); + if (!(ka = avahi_new0(AvahiKnownAnswer, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; + } + ka->scheduler = s; ka->record = avahi_record_ref(e->record); @@ -178,34 +193,34 @@ static gpointer known_answer_walk_callback(AvahiCache *c, AvahiKey *pattern, Ava return NULL; } -static gboolean packet_add_query_job(AvahiQueryScheduler *s, AvahiDnsPacket *p, AvahiQueryJob *qj) { - g_assert(s); - g_assert(p); - g_assert(qj); +static int packet_add_query_job(AvahiQueryScheduler *s, AvahiDnsPacket *p, AvahiQueryJob *qj) { + assert(s); + assert(p); + assert(qj); - if (!avahi_dns_packet_append_key(p, qj->key, FALSE)) - return FALSE; + if (!avahi_dns_packet_append_key(p, qj->key, 0)) + return 0; /* Add all matching known answers to the list */ avahi_cache_walk(s->interface->cache, qj->key, known_answer_walk_callback, s); job_mark_done(s, qj); - return TRUE; + return 1; } static void append_known_answers_and_send(AvahiQueryScheduler *s, AvahiDnsPacket *p) { AvahiKnownAnswer *ka; - guint n; - g_assert(s); - g_assert(p); + unsigned n; + assert(s); + assert(p); n = 0; while ((ka = s->known_answers)) { - gboolean too_large = FALSE; + int too_large = 0; - while (!avahi_dns_packet_append_record(p, ka->record, FALSE, 0)) { + while (!avahi_dns_packet_append_record(p, ka->record, 0, 0)) { if (avahi_dns_packet_is_empty(p)) { /* The record is too large to fit into one packet, so @@ -213,7 +228,7 @@ static void append_known_answers_and_send(AvahiQueryScheduler *s, AvahiDnsPacket the owner of the record send it as a response. This has the advantage of a cache refresh. */ - too_large = TRUE; + too_large = 1; break; } @@ -228,7 +243,7 @@ static void append_known_answers_and_send(AvahiQueryScheduler *s, AvahiDnsPacket AVAHI_LLIST_REMOVE(AvahiKnownAnswer, known_answer, s->known_answers, ka); avahi_record_unref(ka->record); - g_free(ka); + avahi_free(ka); if (!too_large) n++; @@ -239,14 +254,14 @@ static void append_known_answers_and_send(AvahiQueryScheduler *s, AvahiDnsPacket avahi_dns_packet_free(p); } -static void elapse_callback(AvahiTimeEvent *e, gpointer data) { +static void elapse_callback(AvahiTimeEvent *e, void* data) { AvahiQueryJob *qj = data; AvahiQueryScheduler *s; AvahiDnsPacket *p; - guint n; - gboolean b; + unsigned n; + int b; - g_assert(qj); + assert(qj); s = qj->scheduler; if (qj->done) { @@ -255,11 +270,13 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { return; } - g_assert(!s->known_answers); + assert(!s->known_answers); + + if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu))) + return; /* OOM */ - p = avahi_dns_packet_new_query(s->interface->hardware->mtu); b = packet_add_query_job(s, p, qj); - g_assert(b); /* An query must always fit in */ + assert(b); /* An query must always fit in */ n = 1; /* Try to fill up packet with more queries, if available */ @@ -280,11 +297,11 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { static AvahiQueryJob* find_scheduled_job(AvahiQueryScheduler *s, AvahiKey *key) { AvahiQueryJob *qj; - g_assert(s); - g_assert(key); + assert(s); + assert(key); for (qj = s->jobs; qj; qj = qj->jobs_next) { - g_assert(!qj->done); + assert(!qj->done); if (avahi_key_equal(qj->key, key)) return qj; @@ -296,11 +313,11 @@ static AvahiQueryJob* find_scheduled_job(AvahiQueryScheduler *s, AvahiKey *key) static AvahiQueryJob* find_history_job(AvahiQueryScheduler *s, AvahiKey *key) { AvahiQueryJob *qj; - g_assert(s); - g_assert(key); + assert(s); + assert(key); for (qj = s->history; qj; qj = qj->jobs_next) { - g_assert(qj->done); + assert(qj->done); if (avahi_key_equal(qj->key, key)) { /* Check whether this entry is outdated */ @@ -318,16 +335,16 @@ static AvahiQueryJob* find_history_job(AvahiQueryScheduler *s, AvahiKey *key) { return NULL; } -gboolean avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, gboolean immediately) { +int avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, int immediately) { struct timeval tv; AvahiQueryJob *qj; - g_assert(s); - g_assert(key); + assert(s); + assert(key); if ((qj = find_history_job(s, key))) { /* avahi_log_debug("Query suppressed by local duplicate suppression (history)"); */ - return FALSE; + return 0; } avahi_elapse_time(&tv, immediately ? 0 : AVAHI_QUERY_DEFER_MSEC, 0); @@ -341,26 +358,28 @@ gboolean avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, gbool /* If the new entry should be scheduled earlier, * update the old entry */ qj->delivery = tv; - avahi_time_event_queue_update(s->time_event_queue, qj->time_event, &qj->delivery); + avahi_time_event_update(qj->time_event, &qj->delivery); } - return TRUE; + return 1; } else { /* avahi_log_debug("Accepted new query job.\n"); */ - qj = job_new(s, key, FALSE); + if (!(qj = job_new(s, key, 0))) + return 0; /* OOM */ + qj->delivery = tv; - qj->time_event = avahi_time_event_queue_add(s->time_event_queue, &qj->delivery, elapse_callback, qj); + qj->time_event = avahi_time_event_new(s->time_event_queue, &qj->delivery, elapse_callback, qj); - return TRUE; + return 1; } } void avahi_query_scheduler_incoming(AvahiQueryScheduler *s, AvahiKey *key) { AvahiQueryJob *qj; - g_assert(s); - g_assert(key); + assert(s); + assert(key); /* This function is called whenever an incoming query was * receieved. We drop scheduled queries that match. The keyword is @@ -372,7 +391,9 @@ void avahi_query_scheduler_incoming(AvahiQueryScheduler *s, AvahiKey *key) { return; } - qj = job_new(s, key, TRUE); + if (!(qj = job_new(s, key, 1))) + return; /* OOM */ + gettimeofday(&qj->delivery, NULL); job_set_elapse_time(s, qj, AVAHI_QUERY_HISTORY_MSEC, 0); } diff --git a/avahi-core/query-sched.h b/avahi-core/query-sched.h index 2c373b1..7b281b6 100644 --- a/avahi-core/query-sched.h +++ b/avahi-core/query-sched.h @@ -31,7 +31,7 @@ AvahiQueryScheduler *avahi_query_scheduler_new(AvahiInterface *i); void avahi_query_scheduler_free(AvahiQueryScheduler *s); void avahi_query_scheduler_clear(AvahiQueryScheduler *s); -gboolean avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, gboolean immediately); +int avahi_query_scheduler_post(AvahiQueryScheduler *s, AvahiKey *key, int immediately); void avahi_query_scheduler_incoming(AvahiQueryScheduler *s, AvahiKey *key); #endif diff --git a/avahi-core/resolve-address.c b/avahi-core/resolve-address.c index 6bd7cf2..e210285 100644 --- a/avahi-core/resolve-address.c +++ b/avahi-core/resolve-address.c @@ -24,6 +24,8 @@ #endif #include +#include + #include "browse.h" struct AvahiAddressResolver { @@ -33,7 +35,7 @@ struct AvahiAddressResolver { AvahiRecordBrowser *record_browser; AvahiAddressResolverCallback callback; - gpointer userdata; + void* userdata; AvahiTimeEvent *time_event; @@ -41,30 +43,30 @@ struct AvahiAddressResolver { }; static void finish(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) { - g_assert(r); + assert(r); avahi_record_browser_free(r->record_browser); r->record_browser = NULL; if (r->time_event) { - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); r->time_event = NULL; } r->callback(r, interface, protocol, event, &r->address, record ? record->data.ptr.name : NULL, r->userdata); } -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiAddressResolver *r = userdata; - g_assert(rr); - g_assert(record); - g_assert(r); + assert(rr); + assert(record); + assert(r); if (!(event == AVAHI_BROWSER_NEW)) return; - g_assert(record->key->type == AVAHI_DNS_TYPE_PTR); + assert(record->key->type == AVAHI_DNS_TYPE_PTR); finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record); } @@ -72,42 +74,63 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac static void time_event_callback(AvahiTimeEvent *e, void *userdata) { AvahiAddressResolver *r = userdata; - g_assert(e); - g_assert(r); + assert(e); + assert(r); finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL); } -AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const AvahiAddress *address, AvahiAddressResolverCallback callback, gpointer userdata) { +AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const AvahiAddress *address, AvahiAddressResolverCallback callback, void* userdata) { AvahiAddressResolver *r; AvahiKey *k; - gchar *n; + char *n; struct timeval tv; - g_assert(server); - g_assert(address); - g_assert(callback); + assert(server); + assert(address); + assert(callback); + + assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6); + + if (address->family == AVAHI_PROTO_INET) + n = avahi_reverse_lookup_name_ipv4(&address->data.ipv4); + else + n = avahi_reverse_lookup_name_ipv6_arpa(&address->data.ipv6); + + if (!n) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + + k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); + avahi_free(n); - g_assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6); + if (!k) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } - r = g_new(AvahiAddressResolver, 1); + if (!(r = avahi_new(AvahiAddressResolver, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + avahi_key_unref(k); + return NULL; + } + r->server = server; r->address = *address; r->callback = callback; r->userdata = userdata; - avahi_elapse_time(&tv, 1000, 0); - r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r); - + r->record_browser = NULL; AVAHI_LLIST_PREPEND(AvahiAddressResolver, resolver, server->address_resolvers, r); - - if (address->family == AVAHI_PROTO_INET) - n = avahi_reverse_lookup_name_ipv4(&address->data.ipv4); - else - n = avahi_reverse_lookup_name_ipv6_arpa(&address->data.ipv6); - - k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - g_free(n); + + avahi_elapse_time(&tv, 1000, 0); + if (!(r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + avahi_address_resolver_free(r); + avahi_key_unref(k); + return NULL; + } r->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r); avahi_key_unref(k); @@ -121,7 +144,7 @@ AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, AvahiIfInd } void avahi_address_resolver_free(AvahiAddressResolver *r) { - g_assert(r); + assert(r); AVAHI_LLIST_REMOVE(AvahiAddressResolver, resolver, r->server->address_resolvers, r); @@ -129,7 +152,7 @@ void avahi_address_resolver_free(AvahiAddressResolver *r) { avahi_record_browser_free(r->record_browser); if (r->time_event) - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); - g_free(r); + avahi_free(r); } diff --git a/avahi-core/resolve-host-name.c b/avahi-core/resolve-host-name.c index 1e6dc9f..5ea1697 100644 --- a/avahi-core/resolve-host-name.c +++ b/avahi-core/resolve-host-name.c @@ -25,17 +25,20 @@ #include #include +#include + #include "browse.h" +#include "log.h" struct AvahiHostNameResolver { AvahiServer *server; - gchar *host_name; + char *host_name; AvahiRecordBrowser *record_browser_a; AvahiRecordBrowser *record_browser_aaaa; AvahiHostNameResolverCallback callback; - gpointer userdata; + void* userdata; AvahiTimeEvent *time_event; @@ -45,7 +48,7 @@ struct AvahiHostNameResolver { static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) { AvahiAddress a; - g_assert(r); + assert(r); if (r->record_browser_a) { avahi_record_browser_free(r->record_browser_a); @@ -58,7 +61,7 @@ static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtoc } if (r->time_event) { - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); r->time_event = NULL; } @@ -75,53 +78,57 @@ static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtoc break; default: - g_assert(FALSE); + assert(0); } } r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata); } -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiHostNameResolver *r = userdata; - g_assert(rr); - g_assert(record); - g_assert(r); + assert(rr); + assert(record); + assert(r); if (!(event == AVAHI_BROWSER_NEW)) return; - g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA); + assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA); finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record); } static void time_event_callback(AvahiTimeEvent *e, void *userdata) { AvahiHostNameResolver *r = userdata; - g_assert(e); - g_assert(r); + assert(e); + assert(r); finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL); } -AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) { +AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiHostNameResolverCallback callback, void* userdata) { AvahiHostNameResolver *r; AvahiKey *k; struct timeval tv; - g_assert(server); - g_assert(host_name); - g_assert(callback); + assert(server); + assert(host_name); + assert(callback); - g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6); + assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6); if (!avahi_is_valid_domain_name(host_name)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME); return NULL; } - r = g_new(AvahiHostNameResolver, 1); + if (!(r = avahi_new(AvahiHostNameResolver, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + r->server = server; r->host_name = avahi_normalize_name(host_name); r->callback = callback; @@ -130,7 +137,7 @@ AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIf r->record_browser_a = r->record_browser_aaaa = NULL; avahi_elapse_time(&tv, 1000, 0); - r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r); + r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r); AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r); @@ -154,7 +161,7 @@ AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIf goto fail; } - g_assert(r->record_browser_aaaa || r->record_browser_a); + assert(r->record_browser_aaaa || r->record_browser_a); return r; @@ -164,7 +171,7 @@ fail: } void avahi_host_name_resolver_free(AvahiHostNameResolver *r) { - g_assert(r); + assert(r); AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r); @@ -175,8 +182,8 @@ void avahi_host_name_resolver_free(AvahiHostNameResolver *r) { avahi_record_browser_free(r->record_browser_aaaa); if (r->time_event) - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); - g_free(r->host_name); - g_free(r); + avahi_free(r->host_name); + avahi_free(r); } diff --git a/avahi-core/resolve-service.c b/avahi-core/resolve-service.c index 9a142ae..194c245 100644 --- a/avahi-core/resolve-service.c +++ b/avahi-core/resolve-service.c @@ -24,16 +24,19 @@ #endif #include +#include #include #include +#include + #include "browse.h" struct AvahiServiceResolver { AvahiServer *server; - gchar *service_name; - gchar *service_type; - gchar *domain_name; + char *service_name; + char *service_type; + char *domain_name; AvahiProtocol address_protocol; AvahiIfIndex interface; @@ -47,7 +50,7 @@ struct AvahiServiceResolver { AvahiRecord *srv_record, *txt_record, *address_record; AvahiServiceResolverCallback callback; - gpointer userdata; + void* userdata; AvahiTimeEvent *time_event; @@ -55,7 +58,7 @@ struct AvahiServiceResolver { }; static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) { - g_assert(r); + assert(r); if (r->record_browser_a) { avahi_record_browser_free(r->record_browser_a); @@ -78,7 +81,7 @@ static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) { } if (r->time_event) { - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); r->time_event = NULL; } @@ -86,12 +89,12 @@ static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) { r->callback(r, r->interface, r->protocol, event, r->service_name, r->service_type, r->domain_name, NULL, NULL, 0, NULL, r->userdata); else { AvahiAddress a; - gchar sn[256], st[256]; + char sn[256], st[256]; size_t i; - g_assert(r->srv_record); - g_assert(r->txt_record); - g_assert(r->address_record); + assert(r->srv_record); + assert(r->txt_record); + assert(r->address_record); switch (r->address_record->key->type) { case AVAHI_DNS_TYPE_A: @@ -105,11 +108,11 @@ static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) { break; default: - g_assert(FALSE); + assert(0); } - g_snprintf(sn, sizeof(sn), r->service_name); - g_snprintf(st, sizeof(st), r->service_type); + snprintf(sn, sizeof(sn), r->service_name); + snprintf(st, sizeof(st), r->service_type); if ((i = strlen(sn)) > 0 && sn[i-1] == '.') sn[i-1] = 0; @@ -122,12 +125,12 @@ static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) { } } -static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) { +static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) { AvahiServiceResolver *r = userdata; - g_assert(rr); - g_assert(record); - g_assert(r); + assert(rr); + assert(record); + assert(r); if (!(event == AVAHI_BROWSER_NEW)) return; @@ -149,7 +152,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac if (!r->srv_record) { r->srv_record = avahi_record_ref(record); - g_assert(!r->record_browser_a && !r->record_browser_aaaa); + assert(!r->record_browser_a && !r->record_browser_aaaa); if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) { AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); @@ -177,7 +180,7 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac break; default: - g_assert(FALSE); + assert(0); } if (r->txt_record && r->srv_record && r->address_record) @@ -187,25 +190,25 @@ static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interfac static void time_event_callback(AvahiTimeEvent *e, void *userdata) { AvahiServiceResolver *r = userdata; - g_assert(e); - g_assert(r); + assert(e); + assert(r); finish(r, AVAHI_RESOLVER_TIMEOUT); } -AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *name, const gchar *type, const gchar *domain, AvahiProtocol aprotocol, AvahiServiceResolverCallback callback, gpointer userdata) { +AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiProtocol aprotocol, AvahiServiceResolverCallback callback, void* userdata) { AvahiServiceResolver *r; AvahiKey *k; struct timeval tv; - gchar t[256], *n; + char t[256], *n; size_t l; - g_assert(server); - g_assert(name); - g_assert(type); - g_assert(callback); + assert(server); + assert(name); + assert(type); + assert(callback); - g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6); + assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6); if (!avahi_is_valid_service_name(name)) { avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_NAME); @@ -222,9 +225,13 @@ AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfInd return NULL; } - r = g_new(AvahiServiceResolver, 1); + if (!(r = avahi_new(AvahiServiceResolver, 1))) { + avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY); + return NULL; + } + r->server = server; - r->service_name = g_strdup(name); + r->service_name = avahi_strdup(name); r->service_type = avahi_normalize_name(type); r->domain_name = avahi_normalize_name(domain); r->callback = callback; @@ -236,11 +243,11 @@ AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfInd n = t; l = sizeof(t); - avahi_escape_label((const guint8*) name, strlen(name), &n, &l); - g_snprintf(n, l, ".%s.%s", r->service_type, r->domain_name); + avahi_escape_label((const uint8_t*) name, strlen(name), &n, &l); + snprintf(n, l, ".%s.%s", r->service_type, r->domain_name); avahi_elapse_time(&tv, 1000, 0); - r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r); + r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r); AVAHI_LLIST_PREPEND(AvahiServiceResolver, resolver, server->service_resolvers, r); @@ -268,12 +275,12 @@ AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, AvahiIfInd } void avahi_service_resolver_free(AvahiServiceResolver *r) { - g_assert(r); + assert(r); AVAHI_LLIST_REMOVE(AvahiServiceResolver, resolver, r->server->service_resolvers, r); if (r->time_event) - avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event); + avahi_time_event_free(r->time_event); if (r->record_browser_srv) avahi_record_browser_free(r->record_browser_srv); @@ -291,8 +298,8 @@ void avahi_service_resolver_free(AvahiServiceResolver *r) { if (r->address_record) avahi_record_unref(r->address_record); - g_free(r->service_name); - g_free(r->service_type); - g_free(r->domain_name); - g_free(r); + avahi_free(r->service_name); + avahi_free(r->service_type); + avahi_free(r->domain_name); + avahi_free(r); } diff --git a/avahi-core/response-sched.c b/avahi-core/response-sched.c index efb6677..08598fc 100644 --- a/avahi-core/response-sched.c +++ b/avahi-core/response-sched.c @@ -24,6 +24,8 @@ #endif #include +#include + #include "response-sched.h" #include "log.h" @@ -48,9 +50,9 @@ struct AvahiResponseJob { struct timeval delivery; AvahiRecord *record; - gboolean flush_cache; + int flush_cache; AvahiAddress querier; - gboolean querier_valid; + int querier_valid; AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs); }; @@ -67,15 +69,19 @@ struct AvahiResponseScheduler { static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) { AvahiResponseJob *rj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); - rj = g_new(AvahiResponseJob, 1); + if (!(rj = avahi_new(AvahiResponseJob, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; + } + rj->scheduler = s; rj->record = avahi_record_ref(record); rj->time_event = NULL; - rj->flush_cache = FALSE; - rj->querier_valid = FALSE; + rj->flush_cache = 0; + rj->querier_valid = 0; if ((rj->state = state) == AVAHI_SCHEDULED) AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->jobs, rj); @@ -88,11 +94,11 @@ static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, } static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) { - g_assert(s); - g_assert(rj); + assert(s); + assert(rj); if (rj->time_event) - avahi_time_event_queue_remove(s->time_event_queue, rj->time_event); + avahi_time_event_free(rj->time_event); if (rj->state == AVAHI_SCHEDULED) AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj); @@ -102,30 +108,30 @@ static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) { AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->suppressed, rj); avahi_record_unref(rj->record); - g_free(rj); + avahi_free(rj); } -static void elapse_callback(AvahiTimeEvent *e, gpointer data); +static void elapse_callback(AvahiTimeEvent *e, void* data); -static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, guint msec, guint jitter) { +static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, unsigned msec, unsigned jitter) { struct timeval tv; - g_assert(s); - g_assert(rj); + assert(s); + assert(rj); avahi_elapse_time(&tv, msec, jitter); if (rj->time_event) - avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &tv); + avahi_time_event_update(rj->time_event, &tv); else - rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, rj); + rj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, rj); } static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) { - g_assert(s); - g_assert(rj); + assert(s); + assert(rj); - g_assert(rj->state == AVAHI_SCHEDULED); + assert(rj->state == AVAHI_SCHEDULED); AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj); AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj); @@ -139,9 +145,13 @@ static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) { AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) { AvahiResponseScheduler *s; - g_assert(i); + assert(i); - s = g_new(AvahiResponseScheduler, 1); + if (!(s = avahi_new(AvahiResponseScheduler, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; + } + s->interface = i; s->time_event_queue = i->monitor->server->time_event_queue; @@ -153,14 +163,14 @@ AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) { } void avahi_response_scheduler_free(AvahiResponseScheduler *s) { - g_assert(s); + assert(s); avahi_response_scheduler_clear(s); - g_free(s); + avahi_free(s); } void avahi_response_scheduler_clear(AvahiResponseScheduler *s) { - g_assert(s); + assert(s); while (s->jobs) job_free(s, s->jobs); @@ -170,40 +180,41 @@ void avahi_response_scheduler_clear(AvahiResponseScheduler *s) { job_free(s, s->suppressed); } -static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) { +static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) { AvahiResponseJob *rj = userdata; - g_assert(r); - g_assert(rj); + assert(r); + assert(rj); - avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, FALSE); + avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, 0); } -static gboolean packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) { - g_assert(s); - g_assert(p); - g_assert(rj); +static int packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) { + assert(s); + assert(p); + assert(rj); /* Try to add this record to the packet */ if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0)) - return FALSE; + return 0; /* Ok, this record will definitely be sent, so schedule the * auxilliary packets, too */ avahi_server_enumerate_aux_records(s->interface->monitor->server, s->interface, rj->record, enumerate_aux_records_callback, rj); job_mark_done(s, rj); - return TRUE; + return 1; } static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) { AvahiDnsPacket *p; - guint n; + unsigned n; - g_assert(s); - g_assert(rj); + assert(s); + assert(rj); - p = avahi_dns_packet_new_response(s->interface->hardware->mtu, TRUE); + if (!(p = avahi_dns_packet_new_response(s->interface->hardware->mtu, 1))) + return; /* OOM */ n = 1; /* Put it in the packet. */ @@ -219,7 +230,7 @@ static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj } } else { - guint size; + size_t size; avahi_dns_packet_free(p); @@ -229,7 +240,8 @@ static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj if (size > AVAHI_DNS_PACKET_MAX_SIZE) size = AVAHI_DNS_PACKET_MAX_SIZE; - p = avahi_dns_packet_new_response(size, TRUE); + if (!(p = avahi_dns_packet_new_response(size, 1))) + return; /* OOM */ if (!packet_add_response_job(s, p, rj)) { avahi_dns_packet_free(p); @@ -245,10 +257,10 @@ static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj avahi_dns_packet_free(p); } -static void elapse_callback(AvahiTimeEvent *e, gpointer data) { +static void elapse_callback(AvahiTimeEvent *e, void* data) { AvahiResponseJob *rj = data; - g_assert(rj); + assert(rj); if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED) job_free(rj->scheduler, rj); /* Lets drop this entry */ @@ -259,11 +271,11 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) { static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) { AvahiResponseJob *rj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); for (rj = s->jobs; rj; rj = rj->jobs_next) { - g_assert(rj->state == AVAHI_SCHEDULED); + assert(rj->state == AVAHI_SCHEDULED); if (avahi_record_equal_no_ttl(rj->record, record)) return rj; @@ -275,11 +287,11 @@ static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiReco static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) { AvahiResponseJob *rj; - g_assert(s); - g_assert(record); + assert(s); + assert(record); for (rj = s->history; rj; rj = rj->jobs_next) { - g_assert(rj->state == AVAHI_DONE); + assert(rj->state == AVAHI_DONE); if (avahi_record_equal_no_ttl(rj->record, record)) { /* Check whether this entry is outdated */ @@ -302,13 +314,13 @@ static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) { AvahiResponseJob *rj; - g_assert(s); - g_assert(record); - g_assert(querier); + assert(s); + assert(record); + assert(querier); for (rj = s->suppressed; rj; rj = rj->jobs_next) { - g_assert(rj->state == AVAHI_SUPPRESSED); - g_assert(rj->querier_valid); + assert(rj->state == AVAHI_SUPPRESSED); + assert(rj->querier_valid); if (avahi_record_equal_no_ttl(rj->record, record) && avahi_address_cmp(&rj->querier, querier) == 0) { @@ -327,19 +339,19 @@ static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRec return NULL; } -gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) { +int avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) { AvahiResponseJob *rj; struct timeval tv; -/* gchar *t; */ +/* char *t; */ - g_assert(s); - g_assert(record); + assert(s); + assert(record); - g_assert(!avahi_key_is_pattern(record->key)); + assert(!avahi_key_is_pattern(record->key)); /* t = avahi_record_to_string(record); */ /* avahi_log_debug("post %i %s", immediately, t); */ -/* g_free(t); */ +/* avahi_free(t); */ /* Check whether this response is suppressed */ if (querier && @@ -348,7 +360,7 @@ gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *r rj->record->ttl >= record->ttl/2) { /* avahi_log_debug("Response suppressed by known answer suppression."); */ - return FALSE; + return 0; } /* Check if we already sent this response recently */ @@ -358,7 +370,7 @@ gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *r rj->record->ttl >= record->ttl/2 && (rj->flush_cache || !flush_cache)) { /* avahi_log_debug("Response suppressed by local duplicate suppression (history)"); */ - return FALSE; + return 0; } /* Outdated ... */ @@ -375,41 +387,43 @@ gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *r /* Update the time if the new is prior to the old */ if (avahi_timeval_compare(&tv, &rj->delivery) < 0) { rj->delivery = tv; - avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &rj->delivery); + avahi_time_event_update(rj->time_event, &rj->delivery); } /* Update the flush cache bit */ if (flush_cache) - rj->flush_cache = TRUE; + rj->flush_cache = 1; /* Update the querier field */ if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0)) - rj->querier_valid = FALSE; + rj->querier_valid = 0; /* Update record data (just for the TTL) */ avahi_record_unref(rj->record); rj->record = avahi_record_ref(record); - return TRUE; + return 1; } else { /* avahi_log_debug("Accepted new response job."); */ /* Create a new job and schedule it */ - rj = job_new(s, record, AVAHI_SCHEDULED); + if (!(rj = job_new(s, record, AVAHI_SCHEDULED))) + return 0; /* OOM */ + rj->delivery = tv; - rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &rj->delivery, elapse_callback, rj); + rj->time_event = avahi_time_event_new(s->time_event_queue, &rj->delivery, elapse_callback, rj); rj->flush_cache = flush_cache; if ((rj->querier_valid = !!querier)) rj->querier = *querier; - return TRUE; + return 1; } } -void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache) { +void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache) { AvahiResponseJob *rj; - g_assert(s); + assert(s); /* This function is called whenever an incoming response was * receieved. We drop scheduled responses which match here. The @@ -435,10 +449,11 @@ void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *r rj->record = avahi_record_ref(record); } else /* Found no existing history job, so let's create a new one */ - rj = job_new(s, record, AVAHI_DONE); + if (!(rj = job_new(s, record, AVAHI_DONE))) + return; /* OOM */ rj->flush_cache = flush_cache; - rj->querier_valid = FALSE; + rj->querier_valid = 0; gettimeofday(&rj->delivery, NULL); job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0); @@ -447,9 +462,9 @@ void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *r void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) { AvahiResponseJob *rj; - g_assert(s); - g_assert(record); - g_assert(querier); + assert(s); + assert(record); + assert(querier); if ((rj = find_scheduled_job(s, record))) { @@ -472,8 +487,9 @@ void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *r } else { /* Create a new entry */ - rj = job_new(s, record, AVAHI_SUPPRESSED); - rj->querier_valid = TRUE; + if (!(rj = job_new(s, record, AVAHI_SUPPRESSED))) + return; /* OOM */ + rj->querier_valid = 1; rj->querier = *querier; } @@ -482,7 +498,7 @@ void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *r } void avahi_response_scheduler_force(AvahiResponseScheduler *s) { - g_assert(s); + assert(s); /* Send all scheduled responses immediately */ while (s->jobs) diff --git a/avahi-core/response-sched.h b/avahi-core/response-sched.h index 3760a44..68c4a9d 100644 --- a/avahi-core/response-sched.h +++ b/avahi-core/response-sched.h @@ -32,8 +32,8 @@ void avahi_response_scheduler_free(AvahiResponseScheduler *s); void avahi_response_scheduler_clear(AvahiResponseScheduler *s); void avahi_response_scheduler_force(AvahiResponseScheduler *s); -gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately); -void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache); +int avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately); +void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache); void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier); #endif diff --git a/avahi-core/rr.c b/avahi-core/rr.c index 821ca8f..704f5be 100644 --- a/avahi-core/rr.c +++ b/avahi-core/rr.c @@ -36,6 +36,7 @@ #include "rr.h" #include "log.h" #include "util.h" +#include "hashmap.h" AvahiKey *avahi_key_new(const char *name, uint16_t class, uint16_t type) { AvahiKey *k; @@ -263,12 +264,12 @@ char *avahi_record_to_string(const AvahiRecord *r) { return s; } -gboolean avahi_key_equal(const AvahiKey *a, const AvahiKey *b) { +int avahi_key_equal(const AvahiKey *a, const AvahiKey *b) { assert(a); assert(b); if (a == b) - return TRUE; + return 1; /* g_message("equal: %p %p", a, b); */ @@ -277,7 +278,7 @@ gboolean avahi_key_equal(const AvahiKey *a, const AvahiKey *b) { a->clazz == b->clazz; } -gboolean avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) { +int avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) { assert(pattern); assert(k); @@ -286,14 +287,14 @@ gboolean avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) { assert(!avahi_key_is_pattern(k)); if (pattern == k) - return TRUE; + return 1; return avahi_domain_equal(pattern->name, k->name) && (pattern->type == k->type || pattern->type == AVAHI_DNS_TYPE_ANY) && (pattern->clazz == k->clazz || pattern->clazz == AVAHI_DNS_CLASS_ANY); } -gboolean avahi_key_is_pattern(const AvahiKey *k) { +int avahi_key_is_pattern(const AvahiKey *k) { assert(k); return @@ -301,7 +302,7 @@ gboolean avahi_key_is_pattern(const AvahiKey *k) { k->clazz == AVAHI_DNS_CLASS_ANY; } -guint avahi_key_hash(const AvahiKey *k) { +unsigned avahi_key_hash(const AvahiKey *k) { assert(k); return @@ -310,7 +311,7 @@ guint avahi_key_hash(const AvahiKey *k) { k->clazz; } -static gboolean rdata_equal(const AvahiRecord *a, const AvahiRecord *b) { +static int rdata_equal(const AvahiRecord *a, const AvahiRecord *b) { assert(a); assert(b); assert(a->key->type == b->key->type); @@ -357,12 +358,12 @@ static gboolean rdata_equal(const AvahiRecord *a, const AvahiRecord *b) { } -gboolean avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) { +int avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) { assert(a); assert(b); if (a == b) - return TRUE; + return 1; return avahi_key_equal(a->key, b->key) && @@ -446,7 +447,7 @@ size_t avahi_key_get_estimate_size(AvahiKey *k) { } size_t avahi_record_get_estimate_size(AvahiRecord *r) { - guint n; + size_t n; assert(r); n = avahi_key_get_estimate_size(r->key) + 4 + 2; @@ -552,12 +553,16 @@ int avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) { case AVAHI_DNS_TYPE_TXT: { - guint8 *ma, *mb; - guint asize, bsize; + uint8_t *ma = NULL, *mb = NULL; + size_t asize, bsize; - if (!(ma = avahi_new(guint8, asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0)))) + asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0); + bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0); + + if (asize > 0 && !(ma = avahi_new(uint8_t, asize))) goto fail; - if (!(mb = g_new(guint8, bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0)))) { + + if (bsize > 0 && !(mb = avahi_new(uint8_t, bsize))) { avahi_free(ma); goto fail; } @@ -593,7 +598,7 @@ int avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) { fail: - avahi_log_error("Out of memory"); + avahi_log_error(__FILE__": Out of memory"); return -1; /* or whatever ... */ } @@ -607,9 +612,9 @@ int avahi_key_is_valid(AvahiKey *k) { assert(k); if (!avahi_is_valid_domain_name(k->name)) - return FALSE; + return 0; - return TRUE; + return 1; } int avahi_record_is_valid(AvahiRecord *r) { diff --git a/avahi-core/rr.h b/avahi-core/rr.h index 5349076..9fdcb0e 100644 --- a/avahi-core/rr.h +++ b/avahi-core/rr.h @@ -169,12 +169,12 @@ const char *avahi_dns_class_to_string(uint16_t clazz); * returned pointer points to a read only internal string. */ const char *avahi_dns_type_to_string(uint16_t type); -/** Create a textual representation of the specified key. g_free() the +/** Create a textual representation of the specified key. avahi_free() the * result! */ char *avahi_key_to_string(const AvahiKey *k); /** Create a textual representation of the specified record, similar - * in style to BIND zone file data. g_free() the result! */ + * in style to BIND zone file data. avahi_free() the result! */ char *avahi_record_to_string(const AvahiRecord *r); /** Check whether two records are equal (regardless of the TTL */ @@ -196,7 +196,7 @@ size_t avahi_record_get_estimate_size(AvahiRecord *r); * zero if equal. */ int avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b); -/** Return TRUE if the specified record is an mDNS goodbye record. i.e. TTL is zero. */ +/** Return 1 if the specified record is an mDNS goodbye record. i.e. TTL is zero. */ int avahi_record_is_goodbye(AvahiRecord *r); /** Check whether the specified key is valid */ diff --git a/avahi-core/rrlist.c b/avahi-core/rrlist.c index b05e1c6..fce21ff 100644 --- a/avahi-core/rrlist.c +++ b/avahi-core/rrlist.c @@ -106,7 +106,7 @@ AvahiRecord* avahi_record_list_next(AvahiRecordList *l, int *flush_cache, int *u AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i); AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->read, i); - i->read = TRUE; + i->read = 1; return r; } @@ -146,7 +146,7 @@ void avahi_record_list_push(AvahiRecordList *l, AvahiRecord *r, int flush_cache, i->flush_cache = flush_cache; i->auxiliary = auxiliary; i->record = avahi_record_ref(r); - i->read = FALSE; + i->read = 0; AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->unread, i); } diff --git a/avahi-core/server.c b/avahi-core/server.c index 1a36e42..a3fac4b 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -30,15 +30,18 @@ #include #include #include +#include #include #include +#include #include "server.h" #include "iface.h" #include "socket.h" #include "browse.h" #include "log.h" +#include "util.h" #define AVAHI_RR_HOLDOFF_MSEC 1000 #define AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT 60000 @@ -47,101 +50,103 @@ static void free_entry(AvahiServer*s, AvahiEntry *e) { AvahiEntry *t; - g_assert(s); - g_assert(e); + assert(s); + assert(e); - avahi_goodbye_entry(s, e, TRUE); + avahi_goodbye_entry(s, e, 1); /* Remove from linked list */ AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e); /* Remove from hash table indexed by name */ - t = g_hash_table_lookup(s->entries_by_key, e->record->key); + t = avahi_hashmap_lookup(s->entries_by_key, e->record->key); AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e); if (t) - g_hash_table_replace(s->entries_by_key, t->record->key, t); + avahi_hashmap_replace(s->entries_by_key, t->record->key, t); else - g_hash_table_remove(s->entries_by_key, e->record->key); + avahi_hashmap_remove(s->entries_by_key, e->record->key); /* Remove from associated group */ if (e->group) AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e); avahi_record_unref(e->record); - g_free(e); + avahi_free(e); } static void free_group(AvahiServer *s, AvahiEntryGroup *g) { - g_assert(s); - g_assert(g); + assert(s); + assert(g); while (g->entries) free_entry(s, g->entries); if (g->register_time_event) - avahi_time_event_queue_remove(s->time_event_queue, g->register_time_event); + avahi_time_event_free(g->register_time_event); AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g); - g_free(g); + avahi_free(g); } static void cleanup_dead(AvahiServer *s) { - AvahiEntryGroup *g, *ng; - AvahiEntry *e, *ne; - g_assert(s); - + assert(s); if (s->need_group_cleanup) { - for (g = s->groups; g; g = ng) { - ng = g->groups_next; + AvahiEntryGroup *g, *next; + + for (g = s->groups; g; g = next) { + next = g->groups_next; if (g->dead) free_group(s, g); } - s->need_group_cleanup = FALSE; + s->need_group_cleanup = 0; } if (s->need_entry_cleanup) { - for (e = s->entries; e; e = ne) { - ne = e->entries_next; + AvahiEntry *e, *next; + + for (e = s->entries; e; e = next) { + next = e->entries_next; if (e->dead) free_entry(s, e); } - s->need_entry_cleanup = FALSE; + s->need_entry_cleanup = 0; } if (s->need_browser_cleanup) avahi_browser_cleanup(s); } -static void enum_aux_records(AvahiServer *s, AvahiInterface *i, const gchar *name, guint16 type, void (*callback)(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata), gpointer userdata) { +static void enum_aux_records(AvahiServer *s, AvahiInterface *i, const char *name, uint16_t type, void (*callback)(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata), void* userdata) { AvahiKey *k; AvahiEntry *e; - g_assert(s); - g_assert(i); - g_assert(name); - g_assert(callback); + assert(s); + assert(i); + assert(name); + assert(callback); - g_assert(type != AVAHI_DNS_TYPE_ANY); + assert(type != AVAHI_DNS_TYPE_ANY); - k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type); + if (!(k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type))) + return; /** OOM */ - for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next) - if (!e->dead && avahi_entry_registered(s, e, i)) + for (e = avahi_hashmap_lookup(s->entries_by_key, k); e; e = e->by_key_next) + if (!e->dead && avahi_entry_is_registered(s, e, i)) callback(s, e->record, e->flags & AVAHI_ENTRY_UNIQUE, userdata); avahi_key_unref(k); } -void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata), gpointer userdata) { - g_assert(s); - g_assert(i); - g_assert(r); - g_assert(callback); +void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata), void* userdata) { + assert(s); + assert(i); + assert(r); + assert(callback); if (r->key->clazz == AVAHI_DNS_CLASS_IN) { if (r->key->type == AVAHI_DNS_TYPE_PTR) { @@ -154,54 +159,54 @@ void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, Avahi } } -void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, gboolean unicast_response, gboolean auxiliary) { - g_assert(s); - g_assert(i); - g_assert(e); +void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, int unicast_response, int auxiliary) { + assert(s); + assert(i); + assert(e); avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_ENTRY_UNIQUE, unicast_response, auxiliary); } -void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, gboolean unicast_response) { +void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, int unicast_response) { AvahiEntry *e; -/* gchar *txt; */ +/* char *txt; */ - g_assert(s); - g_assert(i); - g_assert(k); + assert(s); + assert(i); + assert(k); /* avahi_log_debug("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */ -/* g_free(txt); */ +/* avahi_free(txt); */ if (avahi_key_is_pattern(k)) { /* Handle ANY query */ for (e = s->entries; e; e = e->entries_next) - if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i)) - avahi_server_prepare_response(s, i, e, unicast_response, FALSE); + if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_is_registered(s, e, i)) + avahi_server_prepare_response(s, i, e, unicast_response, 0); } else { /* Handle all other queries */ - for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next) - if (!e->dead && avahi_entry_registered(s, e, i)) - avahi_server_prepare_response(s, i, e, unicast_response, FALSE); + for (e = avahi_hashmap_lookup(s->entries_by_key, k); e; e = e->by_key_next) + if (!e->dead && avahi_entry_is_registered(s, e, i)) + avahi_server_prepare_response(s, i, e, unicast_response, 0); } } static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { - g_assert(s); - g_assert(e); + assert(s); + assert(e); if (e->group) { AvahiEntry *k; for (k = e->group->entries; k; k = k->by_group_next) { if (!k->dead) { - avahi_goodbye_entry(s, k, FALSE); - k->dead = TRUE; + avahi_goodbye_entry(s, k, 0); + k->dead = 1; } } @@ -209,54 +214,54 @@ static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION); } else { - avahi_goodbye_entry(s, e, FALSE); - e->dead = TRUE; + avahi_goodbye_entry(s, e, 0); + e->dead = 1; } - s->need_entry_cleanup = TRUE; + s->need_entry_cleanup = 1; } static void withdraw_rrset(AvahiServer *s, AvahiKey *key) { AvahiEntry *e; - g_assert(s); - g_assert(key); + assert(s); + assert(key); - for (e = g_hash_table_lookup(s->entries_by_key, key); e; e = e->by_key_next) + for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next) if (!e->dead) withdraw_entry(s, e); } static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) { AvahiEntry *e, *n; - gchar *t; - gboolean ours = FALSE, won = FALSE, lost = FALSE; + char *t; + int ours = 0, won = 0, lost = 0; - g_assert(s); - g_assert(record); - g_assert(i); + assert(s); + assert(record); + assert(i); t = avahi_record_to_string(record); /* avahi_log_debug("incoming_probe()"); */ - for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { - gint cmp; + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) { + int cmp; n = e->by_key_next; if (e->dead) continue; if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) { - ours = TRUE; + ours = 1; break; } else { - if (avahi_entry_probing(s, e, i)) { + if (avahi_entry_is_probing(s, e, i)) { if (cmp > 0) - won = TRUE; + won = 1; else /* cmp < 0 */ - lost = TRUE; + lost = 1; } } } @@ -273,21 +278,21 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface * /* avahi_log_debug("Not conflicting probe"); */ } - g_free(t); + avahi_free(t); } -static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) { - gboolean valid = TRUE, ours = FALSE, conflict = FALSE, withdraw_immediately = FALSE; +static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, int unique, const AvahiAddress *a) { + int valid = 1, ours = 0, conflict = 0, withdraw_immediately = 0; AvahiEntry *e, *n, *conflicting_entry = NULL; - g_assert(s); - g_assert(i); - g_assert(record); + assert(s); + assert(i); + assert(record); /* avahi_log_debug("CHECKING FOR CONFLICT: [%s]", t); */ - for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) { n = e->by_key_next; if (e->dead || (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique)) @@ -296,20 +301,20 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord * /* Either our entry or the other is intended to be unique, so let's check */ if (avahi_record_equal_no_ttl(e->record, record)) { - ours = TRUE; /* We have an identical record, so this is no conflict */ + ours = 1; /* We have an identical record, so this is no conflict */ /* Check wheter there is a TTL conflict */ if (record->ttl <= e->record->ttl/2 && - avahi_entry_registered(s, e, i)) { - gchar *t; + avahi_entry_is_registered(s, e, i)) { + char *t; /* Refresh */ t = avahi_record_to_string(record); avahi_log_debug("Recieved record with bad TTL [%s]. Refreshing.", t); - avahi_server_prepare_matching_responses(s, i, e->record->key, FALSE); - valid = FALSE; + avahi_server_prepare_matching_responses(s, i, e->record->key, 0); + valid = 0; - g_free(t); + avahi_free(t); } /* There's no need to check the other entries of this RRset */ @@ -317,19 +322,19 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord * } else { - if (avahi_entry_registered(s, e, i)) { + if (avahi_entry_is_registered(s, e, i)) { /* A conflict => we have to return to probe mode */ - conflict = TRUE; + conflict = 1; conflicting_entry = e; - } else if (avahi_entry_probing(s, e, i)) { + } else if (avahi_entry_is_probing(s, e, i)) { /* We are currently registering a matching record, but * someone else already claimed it, so let's * withdraw */ - conflict = TRUE; - withdraw_immediately = TRUE; + conflict = 1; + withdraw_immediately = 1; } } } @@ -337,9 +342,9 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord * /* avahi_log_debug("ours=%i conflict=%i", ours, conflict); */ if (!ours && conflict) { - gchar *t; + char *t; - valid = FALSE; + valid = 0; t = avahi_record_to_string(record); @@ -347,7 +352,7 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord * avahi_log_debug("Recieved conflicting record [%s] with local record to be. Withdrawing.", t); withdraw_rrset(s, record->key); } else { - g_assert(conflicting_entry); + assert(conflicting_entry); avahi_log_debug("Recieved conflicting record [%s]. Resetting our record.", t); avahi_entry_return_to_initial_state(s, conflicting_entry, i); @@ -355,51 +360,52 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord * * state. Local shared records are reannounced. */ } - g_free(t); + avahi_free(t); } return valid; } -static void append_aux_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) { - gboolean *unicast_response = userdata; +static void append_aux_callback(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) { + int *unicast_response = userdata; - g_assert(s); - g_assert(r); - g_assert(unicast_response); + assert(s); + assert(r); + assert(unicast_response); - avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, TRUE); + avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, 1); } -static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean unicast_response) { - g_assert(s); - g_assert(r); +static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int unicast_response) { + assert(s); + assert(r); avahi_server_enumerate_aux_records(s, i, r, append_aux_callback, &unicast_response); } -void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean immediately) { +void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port, int legacy_unicast, int immediately) { - g_assert(s); - g_assert(i); - g_assert(!legacy_unicast || (a && port > 0 && p)); + assert(s); + assert(i); + assert(!legacy_unicast || (a && port > 0 && p)); if (legacy_unicast) { AvahiDnsPacket *reply; AvahiRecord *r; - reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , TRUE, TRUE); + if (!(reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , 1, 1))) + return; /* OOM */ while ((r = avahi_record_list_next(s->record_list, NULL, NULL, NULL))) { - append_aux_records_to_list(s, i, r, FALSE); + append_aux_records_to_list(s, i, r, 0); - if (avahi_dns_packet_append_record(reply, r, FALSE, 10)) + if (avahi_dns_packet_append_record(reply, r, 0, 10)) avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); else { - gchar *t = avahi_record_to_string(r); + char *t = avahi_record_to_string(r); avahi_log_warn("Record [%s] not fitting in legacy unicast packet, dropping.", t); - g_free(t); + avahi_free(t); } avahi_record_unref(r); @@ -411,14 +417,14 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP avahi_dns_packet_free(reply); } else { - gboolean unicast_response, flush_cache, auxiliary; + int unicast_response, flush_cache, auxiliary; AvahiDnsPacket *reply = NULL; AvahiRecord *r; /* In case the query packet was truncated never respond immediately, because known answer suppression records might be contained in later packets */ - gboolean tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC); + int tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC); while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) { @@ -433,8 +439,10 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP for (;;) { if (!reply) { - g_assert(p); - reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE); + assert(p); + + if (!(reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, 0, 0))) + break; /* OOM */ } if (avahi_dns_packet_append_record(reply, r, flush_cache, 0)) { @@ -448,7 +456,7 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP } if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) == 0) { - guint size; + size_t size; /* The record is too large for one packet, so create a larger packet */ @@ -456,14 +464,16 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP size = avahi_record_get_estimate_size(r) + AVAHI_DNS_PACKET_HEADER_SIZE; if (size > AVAHI_DNS_PACKET_MAX_SIZE) size = AVAHI_DNS_PACKET_MAX_SIZE; - reply = avahi_dns_packet_new_reply(p, size, FALSE, TRUE); + + if (!(reply = avahi_dns_packet_new_reply(p, size, 0, 1))) + break; /* OOM */ if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) { - gchar *t; + char *t; avahi_dns_packet_free(reply); t = avahi_record_to_string(r); avahi_log_warn("Record [%s] too large, doesn't fit in any packet!", t); - g_free(t); + avahi_free(t); break; } else avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); @@ -490,39 +500,39 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP } -static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean flush_cache) { +static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int flush_cache) { AvahiInterface *j; - g_assert(s); - g_assert(i); - g_assert(r); + assert(s); + assert(i); + assert(r); if (!s->config.enable_reflector) return; for (j = s->monitor->interfaces; j; j = j->interface_next) if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) - avahi_interface_post_response(j, r, flush_cache, NULL, TRUE); + avahi_interface_post_response(j, r, flush_cache, NULL, 1); } -static gpointer reflect_cache_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) { +static void* reflect_cache_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) { AvahiServer *s = userdata; - g_assert(c); - g_assert(pattern); - g_assert(e); - g_assert(s); + assert(c); + assert(pattern); + assert(e); + assert(s); - avahi_record_list_push(s->record_list, e->record, e->cache_flush, FALSE, FALSE); + avahi_record_list_push(s->record_list, e->record, e->cache_flush, 0, 0); return NULL; } static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) { AvahiInterface *j; - g_assert(s); - g_assert(i); - g_assert(k); + assert(s); + assert(i); + assert(k); if (!s->config.enable_reflector) return; @@ -530,7 +540,7 @@ static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) { for (j = s->monitor->interfaces; j; j = j->interface_next) if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) { /* Post the query to other networks */ - avahi_interface_post_query(j, k, TRUE); + avahi_interface_post_query(j, k, 1); /* Reply from caches of other network. This is needed to * "work around" known answer suppression. */ @@ -542,37 +552,37 @@ static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) { static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) { AvahiInterface *j; - g_assert(s); - g_assert(i); - g_assert(r); + assert(s); + assert(i); + assert(r); if (!s->config.enable_reflector) return; for (j = s->monitor->interfaces; j; j = j->interface_next) if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) - avahi_interface_post_probe(j, r, TRUE); + avahi_interface_post_probe(j, r, 1); } -static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) { - guint n; - gboolean is_probe; +static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port, int legacy_unicast) { + size_t n; + int is_probe; - g_assert(s); - g_assert(p); - g_assert(i); - g_assert(a); + assert(s); + assert(p); + assert(i); + assert(a); /* avahi_log_debug("query"); */ - g_assert(avahi_record_list_is_empty(s->record_list)); + assert(avahi_record_list_is_empty(s->record_list)); is_probe = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) > 0; /* Handle the questions */ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) { AvahiKey *key; - gboolean unicast_response = FALSE; + int unicast_response = 0; if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) { avahi_log_warn("Packet too short (1)"); @@ -595,7 +605,7 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac /* Known Answer Suppression */ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) { AvahiRecord *record; - gboolean unique = FALSE; + int unique = 0; if (!(record = avahi_dns_packet_consume_record(p, &unique))) { avahi_log_warn("Packet too short (2)"); @@ -613,7 +623,7 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac /* Probe record */ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) { AvahiRecord *record; - gboolean unique = FALSE; + int unique = 0; if (!(record = avahi_dns_packet_consume_record(p, &unique))) { avahi_log_warn("Packet too short (3)"); @@ -638,20 +648,20 @@ fail: } static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) { - guint n; + unsigned n; - g_assert(s); - g_assert(p); - g_assert(i); - g_assert(a); + assert(s); + assert(p); + assert(i); + assert(a); /* avahi_log_debug("response"); */ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) { AvahiRecord *record; - gboolean cache_flush = FALSE; -/* gchar *txt; */ + int cache_flush = 0; +/* char *txt; */ if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { avahi_log_warn("Packet too short (4)"); @@ -661,7 +671,7 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter if (!avahi_key_is_pattern(record->key)) { /* avahi_log_debug("Handling response: %s", txt = avahi_record_to_string(record)); */ -/* g_free(txt); */ +/* avahi_free(txt); */ if (handle_conflict(s, i, record, cache_flush, a)) { reflect_response(s, i, record, cache_flush); @@ -677,17 +687,17 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter records have been scheduling for sending. We need to flush them here. */ if (!avahi_record_list_is_empty(s->record_list)) - avahi_server_generate_response(s, i, NULL, NULL, 0, FALSE, TRUE); + avahi_server_generate_response(s, i, NULL, NULL, 0, 0, 1); } static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) { - guint n, idx = (guint) -1; + unsigned n, idx = (unsigned) -1; AvahiLegacyUnicastReflectSlot *slot; - g_assert(s); + assert(s); if (!s->legacy_unicast_reflect_slots) - s->legacy_unicast_reflect_slots = g_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS); + s->legacy_unicast_reflect_slots = avahi_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS); for (n = 0; n < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; n++, s->legacy_unicast_reflect_id++) { idx = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; @@ -696,34 +706,37 @@ static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) { break; } - if (idx == (guint) -1 || s->legacy_unicast_reflect_slots[idx]) + if (idx == (unsigned) -1 || s->legacy_unicast_reflect_slots[idx]) return NULL; - slot = s->legacy_unicast_reflect_slots[idx] = g_new(AvahiLegacyUnicastReflectSlot, 1); + if (!(slot = avahi_new(AvahiLegacyUnicastReflectSlot, 1))) + return NULL; /* OOM */ + + slot = s->legacy_unicast_reflect_slots[idx]; slot->id = s->legacy_unicast_reflect_id++; slot->server = s; return slot; } static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) { - guint idx; + unsigned idx; - g_assert(s); - g_assert(slot); + assert(s); + assert(slot); idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; - g_assert(s->legacy_unicast_reflect_slots[idx] == slot); + assert(s->legacy_unicast_reflect_slots[idx] == slot); - avahi_time_event_queue_remove(s->time_event_queue, slot->time_event); + avahi_time_event_free(slot->time_event); - g_free(slot); + avahi_free(slot); s->legacy_unicast_reflect_slots[idx] = NULL; } static void free_slots(AvahiServer *s) { - guint idx; - g_assert(s); + unsigned idx; + assert(s); if (!s->legacy_unicast_reflect_slots) return; @@ -732,14 +745,14 @@ static void free_slots(AvahiServer *s) { if (s->legacy_unicast_reflect_slots[idx]) deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]); - g_free(s->legacy_unicast_reflect_slots); + avahi_free(s->legacy_unicast_reflect_slots); s->legacy_unicast_reflect_slots = NULL; } -static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, guint16 id) { - guint idx; +static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, uint16_t id) { + unsigned idx; - g_assert(s); + assert(s); if (!s->legacy_unicast_reflect_slots) return NULL; @@ -755,23 +768,23 @@ static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, guint16 id) { static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) { AvahiLegacyUnicastReflectSlot *slot = userdata; - g_assert(e); - g_assert(slot); - g_assert(slot->time_event == e); + assert(e); + assert(slot); + assert(slot->time_event == e); deallocate_slot(slot->server, slot); } -static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port) { +static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port) { AvahiLegacyUnicastReflectSlot *slot; AvahiInterface *j; - g_assert(s); - g_assert(p); - g_assert(i); - g_assert(a); - g_assert(port > 0); - g_assert(i->protocol == a->family); + assert(s); + assert(p); + assert(i); + assert(a); + assert(port > 0); + assert(i->protocol == a->family); if (!s->config.enable_reflector) return; @@ -798,7 +811,7 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket * slot->interface = i->hardware->index; avahi_elapse_time(&slot->elapse_time, 2000, 0); - slot->time_event = avahi_time_event_queue_add(s->time_event_queue, &slot->elapse_time, legacy_unicast_reflect_slot_timeout, slot); + slot->time_event = avahi_time_event_new(s->time_event_queue, &slot->elapse_time, legacy_unicast_reflect_slot_timeout, slot); /* Patch the packet with our new locally generatedt id */ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id); @@ -818,18 +831,18 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket * avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id); } -static gboolean originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) { +static int originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) { AvahiAddress a; - g_assert(s); - g_assert(sa); + assert(s); + assert(sa); if (!s->config.enable_reflector) - return FALSE; + return 0; avahi_address_from_sockaddr(sa, &a); if (!avahi_address_is_local(s->monitor, &a)) - return FALSE; + return 0; if (sa->sa_family == AF_INET && s->fd_legacy_unicast_ipv4 >= 0) { struct sockaddr_in lsa; @@ -852,27 +865,27 @@ static gboolean originates_from_local_legacy_unicast_socket(AvahiServer *s, cons return lsa.sin6_port == ((const struct sockaddr_in6*) sa)->sin6_port; } - return FALSE; + return 0; } -static gboolean is_mdns_mcast_address(const AvahiAddress *a) { +static int is_mdns_mcast_address(const AvahiAddress *a) { AvahiAddress b; - g_assert(a); + assert(a); avahi_address_parse(a->family == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->family, &b); return avahi_address_cmp(a, &b) == 0; } -static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, gint ttl) { +static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, int ttl) { AvahiInterface *i; AvahiAddress a; - guint16 port; + uint16_t port; - g_assert(s); - g_assert(p); - g_assert(sa); - g_assert(dest); - g_assert(iface > 0); + assert(s); + assert(p); + assert(sa); + assert(dest); + assert(iface > 0); if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) || !avahi_interface_relevant(i)) { @@ -893,13 +906,13 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock /* This originates from our local reflector, so let's ignore it */ return; - if (avahi_dns_packet_check_valid(p) < 0) { + if (avahi_dns_packet_is_valid(p) < 0) { avahi_log_warn("Recieved invalid packet."); return; } if (avahi_dns_packet_is_query(p)) { - gboolean legacy_unicast = FALSE; + int legacy_unicast = 0; if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { avahi_log_warn("Invalid query packet."); @@ -915,7 +928,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock return; } - legacy_unicast = TRUE; + legacy_unicast = 1; } if (legacy_unicast) @@ -953,16 +966,16 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock } } -static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface, gint ttl) { +static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface, int ttl) { AvahiInterface *i, *j; AvahiAddress a; - guint16 port; + uint16_t port; AvahiLegacyUnicastReflectSlot *slot; - g_assert(s); - g_assert(p); - g_assert(sa); - g_assert(iface > 0); + assert(s); + assert(p); + assert(sa); + assert(iface > 0); if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) || !avahi_interface_relevant(i)) { @@ -979,7 +992,7 @@ static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, co /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */ return; - if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) { + if (avahi_dns_packet_is_valid(p) < 0 || avahi_dns_packet_is_query(p)) { avahi_log_warn("Recieved invalid packet."); return; } @@ -1003,93 +1016,57 @@ static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, co avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id); } -static void work(AvahiServer *s) { - struct sockaddr_in6 sa6; - struct sockaddr_in sa; +static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) { + AvahiServer *s = userdata; AvahiAddress dest; AvahiDnsPacket *p; - gint iface = 0; - guint8 ttl; - - g_assert(s); + AvahiIfIndex iface; + uint8_t ttl; + struct sockaddr_in sa; + struct sockaddr_in6 sa6; - if (s->fd_ipv4 >= 0 && (s->pollfd_ipv4.revents & G_IO_IN)) { - dest.family = AVAHI_PROTO_INET; - if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) { - dispatch_packet(s, p, (struct sockaddr*) &sa, &dest, iface, ttl); - avahi_dns_packet_free(p); - } - } + assert(w); + assert(fd >= 0); - if (s->fd_ipv6 >= 0 && (s->pollfd_ipv6.revents & G_IO_IN)) { - dest.family = AVAHI_PROTO_INET6; - if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) { - dispatch_packet(s, p, (struct sockaddr*) &sa6, &dest, iface, ttl); - avahi_dns_packet_free(p); - } - } + if (events & AVAHI_WATCH_IN) { + + if (fd == s->fd_ipv4) { + dest.family = AVAHI_PROTO_INET; + if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) { + dispatch_packet(s, p, (struct sockaddr*) &sa, &dest, iface, ttl); + avahi_dns_packet_free(p); + } + } else if (fd == s->fd_ipv6) { + dest.family = AVAHI_PROTO_INET6; - if (s->fd_legacy_unicast_ipv4 >= 0 && (s->pollfd_legacy_unicast_ipv4.revents & G_IO_IN)) { - dest.family = AVAHI_PROTO_INET; - if ((p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) { - dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface, ttl); - avahi_dns_packet_free(p); + if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) { + dispatch_packet(s, p, (struct sockaddr*) &sa6, &dest, iface, ttl); + avahi_dns_packet_free(p); + } + } else if (fd == s->fd_legacy_unicast_ipv4) { + dest.family = AVAHI_PROTO_INET; + + if ((p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) { + dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface, ttl); + avahi_dns_packet_free(p); + } + } else if (fd == s->fd_legacy_unicast_ipv6) { + dest.family = AVAHI_PROTO_INET6; + + if ((p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) { + dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface, ttl); + avahi_dns_packet_free(p); + } } - } - if (s->fd_legacy_unicast_ipv6 >= 0 && (s->pollfd_legacy_unicast_ipv6.revents & G_IO_IN)) { - dest.family = AVAHI_PROTO_INET6; - if ((p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) { - dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface, ttl); - avahi_dns_packet_free(p); - } + cleanup_dead(s); + } else { + assert(0); } } -static gboolean prepare_func(GSource *source, gint *timeout) { - g_assert(source); - g_assert(timeout); - - *timeout = -1; - return FALSE; -} - -static gboolean check_func(GSource *source) { - AvahiServer* s; - gushort revents = 0; - - g_assert(source); - - s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); - g_assert(s); - - if (s->fd_ipv4 >= 0) - revents |= s->pollfd_ipv4.revents; - if (s->fd_ipv6 >= 0) - revents |= s->pollfd_ipv6.revents; - if (s->fd_legacy_unicast_ipv4 >= 0) - revents |= s->pollfd_legacy_unicast_ipv4.revents; - if (s->fd_legacy_unicast_ipv6 >= 0) - revents |= s->pollfd_legacy_unicast_ipv6.revents; - - return !!(revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)); -} - -static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { - AvahiServer* s; - g_assert(source); - - s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); - g_assert(s); - - work(s); - cleanup_dead(s); - - return TRUE; -} - static void server_set_state(AvahiServer *s, AvahiServerState state) { - g_assert(s); + assert(s); if (s->state == state) return; @@ -1101,7 +1078,7 @@ static void server_set_state(AvahiServer *s, AvahiServerState state) { } static void withdraw_host_rrs(AvahiServer *s) { - g_assert(s); + assert(s); if (s->hinfo_entry_group) avahi_entry_group_reset(s->hinfo_entry_group); @@ -1109,28 +1086,28 @@ static void withdraw_host_rrs(AvahiServer *s) { if (s->browse_domain_entry_group) avahi_entry_group_reset(s->browse_domain_entry_group); - avahi_update_host_rrs(s->monitor, TRUE); + avahi_update_host_rrs(s->monitor, 1); s->n_host_rr_pending = 0; } void avahi_server_decrease_host_rr_pending(AvahiServer *s) { - g_assert(s); + assert(s); - g_assert(s->n_host_rr_pending > 0); + assert(s->n_host_rr_pending > 0); if (--s->n_host_rr_pending == 0) server_set_state(s, AVAHI_SERVER_RUNNING); } void avahi_server_increase_host_rr_pending(AvahiServer *s) { - g_assert(s); + assert(s); s->n_host_rr_pending ++; } void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) { - g_assert(s); - g_assert(g); + assert(s); + assert(g); if (state == AVAHI_ENTRY_GROUP_REGISTERING && s->state == AVAHI_SERVER_REGISTERING) @@ -1150,30 +1127,43 @@ static void register_hinfo(AvahiServer *s) { struct utsname utsname; AvahiRecord *r; - g_assert(s); + assert(s); if (!s->config.publish_hinfo) return; if (s->hinfo_entry_group) - g_assert(avahi_entry_group_is_empty(s->hinfo_entry_group)); + assert(avahi_entry_group_is_empty(s->hinfo_entry_group)); else s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL); + + if (!s->hinfo_entry_group) { + avahi_log_warn("Failed to create HINFO entry group: %s", avahi_strerror(s->error)); + return; + } /* Fill in HINFO rr */ - r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME); - uname(&utsname); - r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine)); - r->data.hinfo.os = g_strdup(g_strup(utsname.sysname)); - avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_UNIQUE, r); - avahi_record_unref(r); + if ((r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME))) { + uname(&utsname); + r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine)); + r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname)); + + if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_UNIQUE, r) < 0) { + avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error)); + return; + } + + avahi_record_unref(r); + } + + if (avahi_entry_group_commit(s->hinfo_entry_group) < 0) + avahi_log_warn("Failed to commit HINFO entry group: %s", avahi_strerror(s->error)); - avahi_entry_group_commit(s->hinfo_entry_group); } static void register_localhost(AvahiServer *s) { AvahiAddress a; - g_assert(s); + assert(s); /* Add localhost entries */ avahi_address_parse("127.0.0.1", AVAHI_PROTO_INET, &a); @@ -1184,52 +1174,66 @@ static void register_localhost(AvahiServer *s) { } static void register_browse_domain(AvahiServer *s) { - g_assert(s); + assert(s); if (!s->config.publish_domain) return; if (s->browse_domain_entry_group) - g_assert(avahi_entry_group_is_empty(s->browse_domain_entry_group)); + assert(avahi_entry_group_is_empty(s->browse_domain_entry_group)); else s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL); + + if (!s->browse_domain_entry_group) { + avahi_log_warn("Failed to create browse domain entry group: %s", avahi_strerror(s->error)); + return; + } - avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, AVAHI_DEFAULT_TTL, "b._dns-sd._udp.local", s->domain_name); - avahi_entry_group_commit(s->browse_domain_entry_group); + if (avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, AVAHI_DEFAULT_TTL, "b._dns-sd._udp.local", s->domain_name) < 0) { + avahi_log_warn("Failed to add browse domain RR: %s", avahi_strerror(s->error)); + return; + } + + if (avahi_entry_group_commit(s->browse_domain_entry_group) < 0) + avahi_log_warn("Failed to commit browse domain entry group: %s", avahi_strerror(s->error)); } static void register_stuff(AvahiServer *s) { - g_assert(s); + assert(s); server_set_state(s, AVAHI_SERVER_REGISTERING); register_hinfo(s); register_browse_domain(s); - avahi_update_host_rrs(s->monitor, FALSE); + avahi_update_host_rrs(s->monitor, 0); if (s->n_host_rr_pending == 0) server_set_state(s, AVAHI_SERVER_RUNNING); } static void update_fqdn(AvahiServer *s) { - g_assert(s); + char *n; - g_assert(s->host_name); - g_assert(s->domain_name); + assert(s); + assert(s->host_name); + assert(s->domain_name); + + if (!(n = avahi_strdup_printf("%s.%s", s->host_name, s->domain_name))) + return; /* OOM */ - g_free(s->host_name_fqdn); - s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain_name); + avahi_free(s->host_name_fqdn); + s->host_name_fqdn = n; } -gint avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) { - g_assert(s); - g_assert(host_name); +int avahi_server_set_host_name(AvahiServer *s, const char *host_name) { + assert(s); + assert(host_name); if (host_name && !avahi_is_valid_host_name(host_name)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); withdraw_host_rrs(s); - g_free(s->host_name); + avahi_free(s->host_name); s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_host_name(); s->host_name[strcspn(s->host_name, ".")] = 0; update_fqdn(s); @@ -1238,35 +1242,24 @@ gint avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) { return AVAHI_OK; } -gint avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) { - g_assert(s); - g_assert(domain_name); +int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name) { + assert(s); + assert(domain_name); if (domain_name && !avahi_is_valid_domain_name(domain_name)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); withdraw_host_rrs(s); - g_free(s->domain_name); - s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local"); + avahi_free(s->domain_name); + s->domain_name = domain_name ? avahi_normalize_name(domain_name) : avahi_strdup("local"); update_fqdn(s); register_stuff(s); return AVAHI_OK; } -static void prepare_pollfd(AvahiServer *s, GPollFD *pollfd, gint fd) { - g_assert(s); - g_assert(pollfd); - g_assert(fd >= 0); - - memset(pollfd, 0, sizeof(GPollFD)); - pollfd->fd = fd; - pollfd->events = G_IO_IN|G_IO_ERR|G_IO_HUP; - g_source_add_poll(s->source, pollfd); -} - -static gint valid_server_config(const AvahiServerConfig *sc) { +static int valid_server_config(const AvahiServerConfig *sc) { if (sc->host_name && !avahi_is_valid_host_name(sc->host_name)) return AVAHI_ERR_INVALID_HOST_NAME; @@ -1277,81 +1270,87 @@ static gint valid_server_config(const AvahiServerConfig *sc) { return AVAHI_OK; } -AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, AvahiServerCallback callback, gpointer userdata, gint *error) { - AvahiServer *s; - gint e; +static int setup_sockets(AvahiServer *s) { + assert(s); + + s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1; + s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1; - static GSourceFuncs source_funcs = { - prepare_func, - check_func, - dispatch_func, - NULL, - NULL, - NULL - }; + if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) + return AVAHI_ERR_NO_NETWORK; + if (s->fd_ipv4 < 0 && s->config.use_ipv4) + avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode"); + else if (s->fd_ipv6 < 0 && s->config.use_ipv6) + avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode"); + + s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1; + s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1; + + s->watch_ipv4 = s->watch_ipv6 = s->watch_legacy_unicast_ipv4 = s->watch_legacy_unicast_ipv6 = NULL; + + if (s->fd_ipv4 >= 0) + s->watch_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_ipv4, AVAHI_WATCH_IN, socket_event, s); + if (s->fd_ipv6 >= 0) + s->watch_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_ipv6, AVAHI_WATCH_IN, socket_event, s); + if (s->fd_legacy_unicast_ipv4 >= 0) + s->watch_legacy_unicast_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv4, AVAHI_WATCH_IN, socket_event, s); + if (s->fd_legacy_unicast_ipv6 >= 0) + s->watch_legacy_unicast_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv6, AVAHI_WATCH_IN, socket_event, s); + + return 0; +} + +AvahiServer *avahi_server_new(AvahiPoll *poll_api, const AvahiServerConfig *sc, AvahiServerCallback callback, void* userdata, int *error) { + AvahiServer *s; + int e; + if ((e = valid_server_config(sc)) < 0) { if (error) *error = e; return NULL; } - s = g_new(AvahiServer, 1); - s->n_host_rr_pending = 0; - s->need_entry_cleanup = s->need_group_cleanup = s->need_browser_cleanup = FALSE; + if (!(s = avahi_new(AvahiServer, 1))) { + if (error) + *error = AVAHI_ERR_NO_MEMORY; + + return NULL; + } + + s->poll_api = poll_api; if (sc) avahi_server_config_copy(&s->config, sc); else avahi_server_config_init(&s->config); - - s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1; - s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1; - - if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) { - avahi_server_config_free(&s->config); - g_free(s); + if ((e = setup_sockets(s)) < 0) { if (error) - *error = AVAHI_ERR_NO_NETWORK; + *error = e; + + avahi_server_config_free(&s->config); + avahi_free(s); return NULL; } - if (s->fd_ipv4 < 0 && s->config.use_ipv4) - avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode"); - else if (s->fd_ipv6 < 0 && s->config.use_ipv6) - avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode"); - - s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1; - s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1; - - g_main_context_ref(s->context = (c ? c : g_main_context_default())); - - /* Prepare IO source registration */ - s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*)); - *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s; - - if (s->fd_ipv4 >= 0) - prepare_pollfd(s, &s->pollfd_ipv4, s->fd_ipv4); - if (s->fd_ipv6 >= 0) - prepare_pollfd(s, &s->pollfd_ipv6, s->fd_ipv6); - if (s->fd_legacy_unicast_ipv4 >= 0) - prepare_pollfd(s, &s->pollfd_legacy_unicast_ipv4, s->fd_legacy_unicast_ipv4); - if (s->fd_legacy_unicast_ipv6 >= 0) - prepare_pollfd(s, &s->pollfd_legacy_unicast_ipv6, s->fd_legacy_unicast_ipv6); + s->n_host_rr_pending = 0; + s->need_entry_cleanup = 0; + s->need_group_cleanup = 0; + s->need_browser_cleanup = 0; - g_source_attach(s->source, s->context); + s->time_event_queue = avahi_time_event_queue_new(poll_api); s->callback = callback; s->userdata = userdata; + s->entries_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL); AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries); - s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups); + s->record_browser_hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL); AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, s->record_browsers); - s->record_browser_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers); AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers); AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers); @@ -1366,14 +1365,12 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah /* Get host name */ s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name(); s->host_name[strcspn(s->host_name, ".")] = 0; - s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : g_strdup("local"); + s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : avahi_strdup("local"); s->host_name_fqdn = NULL; update_fqdn(s); s->record_list = avahi_record_list_new(); - s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */ - s->state = AVAHI_SERVER_INVALID; s->monitor = avahi_interface_monitor_new(s); @@ -1391,7 +1388,7 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah } void avahi_server_free(AvahiServer* s) { - g_assert(s); + assert(s); while(s->entries) free_entry(s, s->entries); @@ -1419,13 +1416,22 @@ void avahi_server_free(AvahiServer* s) { avahi_service_resolver_free(s->service_resolvers); while (s->record_browsers) avahi_record_browser_destroy(s->record_browsers); - g_hash_table_destroy(s->record_browser_hashtable); - - g_hash_table_destroy(s->entries_by_key); + + avahi_hashmap_free(s->record_browser_hashmap); + avahi_hashmap_free(s->entries_by_key); avahi_time_event_queue_free(s->time_event_queue); avahi_record_list_free(s->record_list); + + if (s->watch_ipv4) + s->poll_api->watch_free(s->watch_ipv4); + if (s->watch_ipv6) + s->poll_api->watch_free(s->watch_ipv6); + if (s->watch_legacy_unicast_ipv4) + s->poll_api->watch_free(s->watch_legacy_unicast_ipv4); + if (s->watch_legacy_unicast_ipv6) + s->poll_api->watch_free(s->watch_legacy_unicast_ipv6); if (s->fd_ipv4 >= 0) close(s->fd_ipv4); @@ -1436,26 +1442,22 @@ void avahi_server_free(AvahiServer* s) { if (s->fd_legacy_unicast_ipv6 >= 0) close(s->fd_legacy_unicast_ipv6); - g_free(s->host_name); - g_free(s->domain_name); - g_free(s->host_name_fqdn); - - g_source_destroy(s->source); - g_source_unref(s->source); - g_main_context_unref(s->context); + avahi_free(s->host_name); + avahi_free(s->domain_name); + avahi_free(s->host_name_fqdn); avahi_server_config_free(&s->config); - g_free(s); + avahi_free(s); } -static gint check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) { +static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) { AvahiEntry *e; - g_assert(s); - g_assert(r); + assert(s); + assert(r); - for (e = g_hash_table_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) { + for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) { if (e->dead) continue; @@ -1479,7 +1481,7 @@ static gint check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiP return 0; } -gint avahi_server_add( +int avahi_server_add( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, @@ -1489,8 +1491,8 @@ gint avahi_server_add( AvahiEntry *e, *t; - g_assert(s); - g_assert(r); + assert(s); + assert(r); if (r->ttl == 0) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_TTL); @@ -1504,23 +1506,25 @@ gint avahi_server_add( if (check_record_conflict(s, interface, protocol, r, flags) < 0) return avahi_server_set_errno(s, AVAHI_ERR_LOCAL_COLLISION); - e = g_new(AvahiEntry, 1); + if (!(e = avahi_new(AvahiEntry, 1))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_NETWORK); + e->server = s; e->record = avahi_record_ref(r); e->group = g; e->interface = interface; e->protocol = protocol; e->flags = flags; - e->dead = FALSE; + e->dead = 0; AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements); AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e); /* Insert into hash table indexed by name */ - t = g_hash_table_lookup(s->entries_by_key, e->record->key); + t = avahi_hashmap_lookup(s->entries_by_key, e->record->key); AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e); - g_hash_table_replace(s->entries_by_key, e->record->key, t); + avahi_hashmap_replace(s->entries_by_key, e->record->key, t); /* Insert into group list */ if (g) @@ -1533,8 +1537,8 @@ gint avahi_server_add( const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) { AvahiEntry **e = (AvahiEntry**) state; - g_assert(s); - g_assert(e); + assert(s); + assert(e); if (!*e) *e = g ? g->entries : s->entries; @@ -1548,80 +1552,95 @@ const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void return avahi_record_ref((*e)->record); } -void avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, gpointer userdata) { +int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) { AvahiEntry *e; - g_assert(s); - g_assert(callback); + assert(s); + assert(callback); callback(";;; ZONE DUMP FOLLOWS ;;;", userdata); for (e = s->entries; e; e = e->entries_next) { - gchar *t; - gchar ln[256]; + char *t; + char ln[256]; if (e->dead) continue; - t = avahi_record_to_string(e->record); - g_snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol); - g_free(t); + if (!(t = avahi_record_to_string(e->record))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + + snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol); + avahi_free(t); callback(ln, userdata); } avahi_dump_caches(s->monitor, callback, userdata); + return AVAHI_OK; } -gint avahi_server_add_ptr( +int avahi_server_add_ptr( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, - const gchar *dest) { + uint32_t ttl, + const char *name, + const char *dest) { AvahiRecord *r; - gint ret; + int ret; - g_assert(s); - g_assert(dest); + assert(s); + assert(dest); - r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl); + if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + r->data.ptr.name = avahi_normalize_name(dest); ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); return ret; } -gint avahi_server_add_address( +int avahi_server_add_address( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - const gchar *name, + const char *name, AvahiAddress *a) { - gchar *n = NULL; - gint ret = AVAHI_OK; - g_assert(s); - g_assert(a); + char *n = NULL; + int ret = AVAHI_OK; + assert(s); + assert(a); - name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn; + if (name) { + if (!(n = avahi_normalize_name(name))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + + name = n; + } else + name = s->host_name_fqdn; if (!avahi_is_valid_domain_name(name)) { - avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); + ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); goto fail; } if (a->family == AVAHI_PROTO_INET) { - gchar *reverse; + char *reverse; AvahiRecord *r; - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME); + if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + r->data.a.address = a->data.ipv4; ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); @@ -1629,15 +1648,25 @@ gint avahi_server_add_address( if (ret < 0) goto fail; - reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4); + if (!(reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); - g_free(reverse); + avahi_free(reverse); } else { - gchar *reverse; + char *reverse; AvahiRecord *r; + + assert(a->family == AVAHI_PROTO_INET6); - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME); + if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + r->data.aaaa.address = a->data.ipv6; ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); @@ -1645,41 +1674,51 @@ gint avahi_server_add_address( if (ret < 0) goto fail; - reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); + if (!(reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); - g_free(reverse); + avahi_free(reverse); if (ret < 0) goto fail; - reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); + if (!(reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); - g_free(reverse); + avahi_free(reverse); } fail: - g_free(n); + avahi_free(n); return ret; } -static gint server_add_txt_strlst_nocopy( +static int server_add_txt_strlst_nocopy( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, AvahiStringList *strlst) { AvahiRecord *r; - gint ret; + int ret; + + assert(s); - g_assert(s); + if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); - r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl); r->data.txt.string_list = strlst; ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); @@ -1687,50 +1726,50 @@ static gint server_add_txt_strlst_nocopy( return ret; } -gint avahi_server_add_txt_strlst( +int avahi_server_add_txt_strlst( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, AvahiStringList *strlst) { - g_assert(s); + assert(s); return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_copy(strlst)); } -gint avahi_server_add_txt_va( +int avahi_server_add_txt_va( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, va_list va) { - g_assert(s); + assert(s); return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_new_va(va)); } -gint avahi_server_add_txt( +int avahi_server_add_txt( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, - guint32 ttl, - const gchar *name, + uint32_t ttl, + const char *name, ...) { va_list va; - gint ret; + int ret; - g_assert(s); + assert(s); va_start(va, name); ret = avahi_server_add_txt_va(s, g, interface, protocol, flags, ttl, name, va); @@ -1739,10 +1778,10 @@ gint avahi_server_add_txt( return ret; } -static void escape_service_name(gchar *d, guint size, const gchar *s) { - g_assert(d); - g_assert(size); - g_assert(s); +static void escape_service_name(char *d, size_t size, const char *s) { + assert(d); + assert(size); + assert(s); while (*s && size >= 2) { if (*s == '.' || *s == '\\') { @@ -1757,30 +1796,30 @@ static void escape_service_name(gchar *d, guint size, const gchar *s) { size--; } - g_assert(size > 0); + assert(size > 0); *(d++) = 0; } -static gint server_add_service_strlst_nocopy( +static int server_add_service_strlst_nocopy( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, AvahiStringList *strlst) { - gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256]; - gchar *t, *d; + char ptr_name[256], svc_name[256], ename[64], enum_ptr[256]; + char *t = NULL, *d = NULL, *h = NULL; AvahiRecord *r = NULL; - gint ret = 0; + int ret = AVAHI_OK; - g_assert(s); - g_assert(type); - g_assert(name); + assert(s); + assert(type); + assert(name); if (!avahi_is_valid_service_name(name)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME); @@ -1805,20 +1844,29 @@ static gint server_add_service_strlst_nocopy( if (!host) host = s->host_name_fqdn; - d = avahi_normalize_name(domain); - t = avahi_normalize_name(type); - - g_snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d); - g_snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d); + if (!(d = avahi_normalize_name(domain)) || + !(t = avahi_normalize_name(type)) || + !(h = avahi_normalize_name(host))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + + snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d); + snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d); if ((ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0) goto fail; - r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME); + if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) { + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + goto fail; + } + r->data.srv.priority = 0; r->data.srv.weight = 0; r->data.srv.port = port; - r->data.srv.name = avahi_normalize_name(host); + r->data.srv.name = h; + h = NULL; ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); avahi_record_unref(r); @@ -1831,75 +1879,76 @@ static gint server_add_service_strlst_nocopy( if (ret < 0) goto fail; - g_snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d); + snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d); ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name); fail: - g_free(d); - g_free(t); + avahi_free(d); + avahi_free(t); + avahi_free(h); avahi_string_list_free(strlst); return ret; } -gint avahi_server_add_service_strlst( +int avahi_server_add_service_strlst( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, AvahiStringList *strlst) { - g_assert(s); - g_assert(type); - g_assert(name); + assert(s); + assert(type); + assert(name); return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_copy(strlst)); } -gint avahi_server_add_service_va( +int avahi_server_add_service_va( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, va_list va){ - g_assert(s); - g_assert(type); - g_assert(name); + assert(s); + assert(type); + assert(name); return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_new_va(va)); } -gint avahi_server_add_service( +int avahi_server_add_service( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host, - guint16 port, + const char *name, + const char *type, + const char *domain, + const char *host, + uint16_t port, ... ){ va_list va; - gint ret; + int ret; - g_assert(s); - g_assert(type); - g_assert(name); + assert(s); + assert(type); + assert(name); va_start(va, port); ret = avahi_server_add_service_va(s, g, interface, protocol, name, type, domain, host, port, va); @@ -1907,10 +1956,10 @@ gint avahi_server_add_service( return ret; } -static void hexstring(gchar *s, size_t sl, const void *p, size_t pl) { - static const gchar hex[] = "0123456789abcdef"; - gboolean b = FALSE; - const guint8 *k = p; +static void hexstring(char *s, size_t sl, const void *p, size_t pl) { + static const char hex[] = "0123456789abcdef"; + int b = 0; + const uint8_t *k = p; while (sl > 1 && pl > 0) { *(s++) = hex[(b ? *k : *k >> 4) & 0xF]; @@ -1929,24 +1978,24 @@ static void hexstring(gchar *s, size_t sl, const void *p, size_t pl) { *s = 0; } -gint avahi_server_add_dns_server_address( +int avahi_server_add_dns_server_address( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDNSServerType type, const AvahiAddress *address, - guint16 port /** should be 53 */) { + uint16_t port /** should be 53 */) { AvahiRecord *r; - gint ret; - gchar n[64] = "ip-"; + int ret; + char n[64] = "ip-"; - g_assert(s); - g_assert(address); - g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE); - g_assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6); + assert(s); + assert(address); + assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE); + assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6); if (domain && !avahi_is_valid_domain_name(domain)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); @@ -1963,6 +2012,9 @@ gint avahi_server_add_dns_server_address( r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.aaaa.address = address->data.ipv6; } + + if (!r) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); @@ -1973,72 +2025,86 @@ gint avahi_server_add_dns_server_address( return avahi_server_add_dns_server_name(s, g, interface, protocol, domain, type, n, port); } -gint avahi_server_add_dns_server_name( +int avahi_server_add_dns_server_name( AvahiServer *s, AvahiEntryGroup *g, AvahiIfIndex interface, AvahiProtocol protocol, - const gchar *domain, + const char *domain, AvahiDNSServerType type, - const gchar *name, - guint16 port /** should be 53 */) { + const char *name, + uint16_t port /** should be 53 */) { - gint ret = -1; - gchar t[256], *d; + int ret = -1; + char t[256], *d = NULL, *n = NULL; AvahiRecord *r; - g_assert(s); - g_assert(name); - g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE); + assert(s); + assert(name); + assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE); + + if (port == 0) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT); + + if (!avahi_is_valid_domain_name(name)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); if (domain && !avahi_is_valid_domain_name(domain)) return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); - if (port == 0) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT); if (!domain) domain = s->domain_name; - d = avahi_normalize_name(domain); - g_snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d); - g_free(d); + if (!(n = avahi_normalize_name(name)) || + !(d = avahi_normalize_name(domain))) { + avahi_free(n); + avahi_free(d); + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + } + + snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d); + avahi_free(d); + + if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) { + avahi_free(n); + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + } - r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.srv.priority = 0; r->data.srv.weight = 0; r->data.srv.port = port; - r->data.srv.name = avahi_normalize_name(name); + r->data.srv.name = n; ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_NULL, r); avahi_record_unref(r); return ret; } -static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { +static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { AvahiKey *k = userdata; - g_assert(m); - g_assert(i); - g_assert(k); + assert(m); + assert(i); + assert(k); - avahi_interface_post_query(i, k, FALSE); + avahi_interface_post_query(i, k, 0); } void avahi_server_post_query(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) { - g_assert(s); - g_assert(key); + assert(s); + assert(key); avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); } void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) { - g_assert(g); + assert(g); if (g->state == state) return; - g_assert(state <= AVAHI_ENTRY_GROUP_COLLISION); + assert(state <= AVAHI_ENTRY_GROUP_COLLISION); g->state = state; @@ -2046,16 +2112,20 @@ void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState sta g->callback(g->server, g, state, g->userdata); } -AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) { +AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, void* userdata) { AvahiEntryGroup *g; - g_assert(s); + assert(s); - g = g_new(AvahiEntryGroup, 1); + if (!(g = avahi_new(AvahiEntryGroup, 1))) { + avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); + return NULL; + } + g->server = s; g->callback = callback; g->userdata = userdata; - g->dead = FALSE; + g->dead = 0; g->state = AVAHI_ENTRY_GROUP_UNCOMMITED; g->n_probing = 0; g->n_register_try = 0; @@ -2071,29 +2141,29 @@ AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback c void avahi_entry_group_free(AvahiEntryGroup *g) { AvahiEntry *e; - g_assert(g); - g_assert(g->server); + assert(g); + assert(g->server); for (e = g->entries; e; e = e->by_group_next) { if (!e->dead) { - avahi_goodbye_entry(g->server, e, TRUE); - e->dead = TRUE; + avahi_goodbye_entry(g->server, e, 1); + e->dead = 1; } } if (g->register_time_event) { - avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event); + avahi_time_event_free(g->register_time_event); g->register_time_event = NULL; } - g->dead = TRUE; + g->dead = 1; - g->server->need_group_cleanup = TRUE; - g->server->need_entry_cleanup = TRUE; + g->server->need_group_cleanup = 1; + g->server->need_entry_cleanup = 1; } static void entry_group_commit_real(AvahiEntryGroup *g) { - g_assert(g); + assert(g); gettimeofday(&g->register_time, NULL); @@ -2101,28 +2171,28 @@ static void entry_group_commit_real(AvahiEntryGroup *g) { if (!g->dead) { avahi_announce_group(g->server, g); - avahi_entry_group_check_probed(g, FALSE); + avahi_entry_group_check_probed(g, 0); } } -static void entry_group_register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) { +static void entry_group_register_time_event_callback(AvahiTimeEvent *e, void* userdata) { AvahiEntryGroup *g = userdata; - g_assert(g); + assert(g); /* avahi_log_debug("Holdoff passed, waking up and going on."); */ - avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event); + avahi_time_event_free(g->register_time_event); g->register_time_event = NULL; /* Holdoff time passed, so let's start probing */ entry_group_commit_real(g); } -gint avahi_entry_group_commit(AvahiEntryGroup *g) { +int avahi_entry_group_commit(AvahiEntryGroup *g) { struct timeval now; - g_assert(g); - g_assert(!g->dead); + assert(g); + assert(!g->dead); if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION) return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE); @@ -2145,8 +2215,8 @@ gint avahi_entry_group_commit(AvahiEntryGroup *g) { /* avahi_log_debug("Holdoff not passed, sleeping."); */ /* Holdoff time has not yet passed, so let's wait */ - g_assert(!g->register_time_event); - g->register_time_event = avahi_time_event_queue_add(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g); + assert(!g->register_time_event); + g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g); avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); } @@ -2156,34 +2226,34 @@ gint avahi_entry_group_commit(AvahiEntryGroup *g) { void avahi_entry_group_reset(AvahiEntryGroup *g) { AvahiEntry *e; - g_assert(g); + assert(g); if (g->register_time_event) { - avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event); + avahi_time_event_free(g->register_time_event); g->register_time_event = NULL; } for (e = g->entries; e; e = e->by_group_next) { if (!e->dead) { - avahi_goodbye_entry(g->server, e, TRUE); - e->dead = TRUE; + avahi_goodbye_entry(g->server, e, 1); + e->dead = 1; } } if (g->register_time_event) { - avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event); + avahi_time_event_free(g->register_time_event); g->register_time_event = NULL; } - g->server->need_entry_cleanup = TRUE; + g->server->need_entry_cleanup = 1; g->n_probing = 0; avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED); } -gboolean avahi_entry_commited(AvahiEntry *e) { - g_assert(e); - g_assert(!e->dead); +int avahi_entry_is_commited(AvahiEntry *e) { + assert(e); + assert(!e->dead); return !e->group || e->group->state == AVAHI_ENTRY_GROUP_REGISTERING || @@ -2191,120 +2261,130 @@ gboolean avahi_entry_commited(AvahiEntry *e) { } AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) { - g_assert(g); - g_assert(!g->dead); + assert(g); + assert(!g->dead); return g->state; } -void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata) { - g_assert(g); +void avahi_entry_group_set_data(AvahiEntryGroup *g, void* userdata) { + assert(g); g->userdata = userdata; } -gpointer avahi_entry_group_get_data(AvahiEntryGroup *g) { - g_assert(g); +void* avahi_entry_group_get_data(AvahiEntryGroup *g) { + assert(g); return g->userdata; } -gboolean avahi_entry_group_is_empty(AvahiEntryGroup *g) { +int avahi_entry_group_is_empty(AvahiEntryGroup *g) { AvahiEntry *e; - g_assert(g); + assert(g); /* Look for an entry that is not dead */ for (e = g->entries; e; e = e->by_group_next) if (!e->dead) - return FALSE; + return 0; - return TRUE; + return 1; } -const gchar* avahi_server_get_domain_name(AvahiServer *s) { - g_assert(s); +const char* avahi_server_get_domain_name(AvahiServer *s) { + assert(s); return s->domain_name; } -const gchar* avahi_server_get_host_name(AvahiServer *s) { - g_assert(s); +const char* avahi_server_get_host_name(AvahiServer *s) { + assert(s); return s->host_name; } -const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s) { - g_assert(s); +const char* avahi_server_get_host_name_fqdn(AvahiServer *s) { + assert(s); return s->host_name_fqdn; } -gpointer avahi_server_get_data(AvahiServer *s) { - g_assert(s); +void* avahi_server_get_data(AvahiServer *s) { + assert(s); return s->userdata; } -void avahi_server_set_data(AvahiServer *s, gpointer userdata) { - g_assert(s); +void avahi_server_set_data(AvahiServer *s, void* userdata) { + assert(s); s->userdata = userdata; } AvahiServerState avahi_server_get_state(AvahiServer *s) { - g_assert(s); + assert(s); return s->state; } AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { - g_assert(c); + assert(c); memset(c, 0, sizeof(AvahiServerConfig)); - c->use_ipv6 = TRUE; - c->use_ipv4 = TRUE; + c->use_ipv6 = 1; + c->use_ipv4 = 1; c->host_name = NULL; c->domain_name = NULL; - c->check_response_ttl = FALSE; - c->publish_hinfo = TRUE; - c->publish_addresses = TRUE; - c->publish_workstation = TRUE; - c->publish_domain = TRUE; - c->use_iff_running = FALSE; - c->enable_reflector = FALSE; - c->reflect_ipv = FALSE; + c->check_response_ttl = 0; + c->publish_hinfo = 1; + c->publish_addresses = 1; + c->publish_workstation = 1; + c->publish_domain = 1; + c->use_iff_running = 0; + c->enable_reflector = 0; + c->reflect_ipv = 0; return c; } void avahi_server_config_free(AvahiServerConfig *c) { - g_assert(c); + assert(c); - g_free(c->host_name); - g_free(c->domain_name); + avahi_free(c->host_name); + avahi_free(c->domain_name); } AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) { - g_assert(ret); - g_assert(c); - + char *d = NULL, *h = NULL; + assert(ret); + assert(c); + + if (c->host_name) + if (!(h = avahi_strdup(c->host_name))) + return NULL; + + if (c->domain_name) + if (!(d = avahi_strdup(c->domain_name))) { + avahi_free(h); + return NULL; + } + *ret = *c; - - ret->host_name = g_strdup(c->host_name); - ret->domain_name = g_strdup(c->domain_name); + ret->host_name = h; + ret->domain_name = d; return ret; } -gint avahi_server_errno(AvahiServer *s) { - g_assert(s); +int avahi_server_errno(AvahiServer *s) { + assert(s); return s->error; } /* Just for internal use */ -gint avahi_server_set_errno(AvahiServer *s, gint error) { - g_assert(s); +int avahi_server_set_errno(AvahiServer *s, int error) { + assert(s); return s->error = error; } diff --git a/avahi-core/server.h b/avahi-core/server.h index f4c7d92..78a403e 100644 --- a/avahi-core/server.h +++ b/avahi-core/server.h @@ -23,6 +23,8 @@ ***/ #include +#include + #include "core.h" #include "iface.h" #include "prioq.h" @@ -31,6 +33,7 @@ #include "browse.h" #include "dns.h" #include "rrlist.h" +#include "hashmap.h" #define AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS 100 @@ -39,10 +42,10 @@ typedef struct AvahiLegacyUnicastReflectSlot AvahiLegacyUnicastReflectSlot; struct AvahiLegacyUnicastReflectSlot { AvahiServer *server; - guint16 id, original_id; + uint16_t id, original_id; AvahiAddress address; - guint16 port; - gint interface; + uint16_t port; + int interface; struct timeval elapse_time; AvahiTimeEvent *time_event; }; @@ -51,7 +54,7 @@ struct AvahiEntry { AvahiServer *server; AvahiEntryGroup *group; - gboolean dead; + int dead; AvahiEntryFlags flags; AvahiRecord *record; @@ -67,15 +70,15 @@ struct AvahiEntry { struct AvahiEntryGroup { AvahiServer *server; - gboolean dead; + int dead; AvahiEntryGroupState state; - gpointer userdata; + void* userdata; AvahiEntryGroupCallback callback; - guint n_probing; + unsigned n_probing; - guint n_register_try; + unsigned n_register_try; struct timeval register_time; AvahiTimeEvent *register_time_event; @@ -84,18 +87,18 @@ struct AvahiEntryGroup { }; struct AvahiServer { - GMainContext *context; + AvahiPoll *poll_api; + AvahiInterfaceMonitor *monitor; - AvahiServerConfig config; AVAHI_LLIST_HEAD(AvahiEntry, entries); - GHashTable *entries_by_key; + AvahiHashmap *entries_by_key; AVAHI_LLIST_HEAD(AvahiEntryGroup, groups); AVAHI_LLIST_HEAD(AvahiRecordBrowser, record_browsers); - GHashTable *record_browser_hashtable; + AvahiHashmap *record_browser_hashmap; AVAHI_LLIST_HEAD(AvahiHostNameResolver, host_name_resolvers); AVAHI_LLIST_HEAD(AvahiAddressResolver, address_resolvers); AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers); @@ -104,56 +107,56 @@ struct AvahiServer { AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers); AVAHI_LLIST_HEAD(AvahiDNSServerBrowser, dns_server_browsers); - gboolean need_entry_cleanup, need_group_cleanup, need_browser_cleanup; + int need_entry_cleanup, need_group_cleanup, need_browser_cleanup; AvahiTimeEventQueue *time_event_queue; - gchar *host_name, *host_name_fqdn, *domain_name; + char *host_name, *host_name_fqdn, *domain_name; - gint fd_ipv4, fd_ipv6, + int fd_ipv4, fd_ipv6, /* The following two sockets two are used for reflection only */ fd_legacy_unicast_ipv4, fd_legacy_unicast_ipv6; - GPollFD pollfd_ipv4, pollfd_ipv6, pollfd_legacy_unicast_ipv4, pollfd_legacy_unicast_ipv6; - GSource *source; + AvahiWatch *watch_ipv4, *watch_ipv6, + *watch_legacy_unicast_ipv4, *watch_legacy_unicast_ipv6; AvahiServerState state; AvahiServerCallback callback; - gpointer userdata; + void* userdata; AvahiEntryGroup *hinfo_entry_group; AvahiEntryGroup *browse_domain_entry_group; - guint n_host_rr_pending; + unsigned n_host_rr_pending; /* Used for assembling responses */ AvahiRecordList *record_list; /* Used for reflection of legacy unicast packets */ AvahiLegacyUnicastReflectSlot **legacy_unicast_reflect_slots; - guint16 legacy_unicast_reflect_id; + uint16_t legacy_unicast_reflect_id; - gint error; + int error; }; -gboolean avahi_server_entry_match_interface(AvahiEntry *e, AvahiInterface *i); +int avahi_server_entry_match_interface(AvahiEntry *e, AvahiInterface *i); void avahi_server_post_query(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key); -void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, gboolean unicast_response, gboolean auxiliary); -void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, gboolean unicast_response); -void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean is_probe); +void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, int unicast_response, int auxiliary); +void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, int unicast_response); +void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port, int legacy_unicast, int is_probe); void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state); -gboolean avahi_entry_commited(AvahiEntry *e); +int avahi_entry_is_commited(AvahiEntry *e); -void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata), gpointer userdata); +void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata), void* userdata); void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata); void avahi_server_decrease_host_rr_pending(AvahiServer *s); void avahi_server_increase_host_rr_pending(AvahiServer *s); -gint avahi_server_set_errno(AvahiServer *s, gint error); +int avahi_server_set_errno(AvahiServer *s, int error); #endif diff --git a/avahi-core/socket.c b/avahi-core/socket.c index fc312d5..b2d6e6f 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "dns.h" #include "fdutil.h" @@ -42,30 +43,27 @@ #include "log.h" static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) { - g_assert(ret_sa); + assert(ret_sa); memset(ret_sa, 0, sizeof(struct sockaddr_in)); - ret_sa->sin_family = AF_INET; ret_sa->sin_port = htons(AVAHI_MDNS_PORT); inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr); } static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) { - - g_assert(ret_sa); + assert(ret_sa); memset(ret_sa, 0, sizeof(struct sockaddr_in6)); - ret_sa->sin6_family = AF_INET6; ret_sa->sin6_port = htons(AVAHI_MDNS_PORT); inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr); } -static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, guint16 port) { - g_assert(ret_sa); - g_assert(a); - g_assert(port > 0); +static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) { + assert(ret_sa); + assert(a); + assert(port > 0); memset(ret_sa, 0, sizeof(struct sockaddr_in)); ret_sa->sin_family = AF_INET; @@ -73,10 +71,10 @@ static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address)); } -static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, guint16 port) { - g_assert(ret_sa); - g_assert(a); - g_assert(port > 0); +static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) { + assert(ret_sa); + assert(a); + assert(port > 0); memset(ret_sa, 0, sizeof(struct sockaddr_in6)); ret_sa->sin6_family = AF_INET6; @@ -84,7 +82,7 @@ static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address)); } -int avahi_mdns_mcast_join_ipv4(gint fd, gint idx) { +int avahi_mdns_mcast_join_ipv4(int fd, int idx) { struct ip_mreqn mreq; struct sockaddr_in sa; @@ -102,7 +100,7 @@ int avahi_mdns_mcast_join_ipv4(gint fd, gint idx) { return 0; } -int avahi_mdns_mcast_join_ipv6(gint fd, gint idx) { +int avahi_mdns_mcast_join_ipv6(int fd, int idx) { struct ipv6_mreq mreq6; struct sockaddr_in6 sa6; @@ -120,7 +118,7 @@ int avahi_mdns_mcast_join_ipv6(gint fd, gint idx) { return 0; } -int avahi_mdns_mcast_leave_ipv4(gint fd, gint idx) { +int avahi_mdns_mcast_leave_ipv4(int fd, int idx) { struct ip_mreqn mreq; struct sockaddr_in sa; @@ -138,7 +136,7 @@ int avahi_mdns_mcast_leave_ipv4(gint fd, gint idx) { return 0; } -int avahi_mdns_mcast_leave_ipv6(gint fd, gint idx) { +int avahi_mdns_mcast_leave_ipv6(int fd, int idx) { struct ipv6_mreq mreq6; struct sockaddr_in6 sa6; @@ -156,12 +154,12 @@ int avahi_mdns_mcast_leave_ipv6(gint fd, gint idx) { return 0; } -static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { - gint yes; +static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { + int yes; - g_assert(fd >= 0); - g_assert(sa); - g_assert(l > 0); + assert(fd >= 0); + assert(sa); + assert(l > 0); if (bind(fd, sa, l) < 0) { @@ -200,7 +198,7 @@ static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { return 0; } -gint avahi_open_socket_ipv4(void) { +int avahi_open_socket_ipv4(void) { struct sockaddr_in local; int fd = -1, ttl, yes; @@ -265,7 +263,7 @@ fail: return -1; } -gint avahi_open_socket_ipv6(void) { +int avahi_open_socket_ipv6(void) { struct sockaddr_in6 sa, local; int fd = -1, ttl, yes; @@ -338,9 +336,9 @@ fail: return -1; } -static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) { - g_assert(fd >= 0); - g_assert(msg); +static int sendmsg_loop(int fd, struct msghdr *msg, int flags) { + assert(fd >= 0); + assert(msg); for (;;) { @@ -359,7 +357,7 @@ static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) { return 0; } -gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, guint16 port) { +int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) { struct sockaddr_in sa; struct msghdr msg; struct iovec io; @@ -367,10 +365,10 @@ gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, cons struct in_pktinfo *pkti; uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)]; - g_assert(fd >= 0); - g_assert(p); - g_assert(avahi_dns_packet_check_valid(p) >= 0); - g_assert(!a || port > 0); + assert(fd >= 0); + assert(p); + assert(avahi_dns_packet_is_valid(p) >= 0); + assert(!a || port > 0); if (!a) mdns_mcast_group_ipv4(&sa); @@ -402,7 +400,7 @@ gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, cons return sendmsg_loop(fd, &msg, 0 /*MSG_DONTROUTE*/); } -gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, guint16 port) { +int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) { struct sockaddr_in6 sa; struct msghdr msg; struct iovec io; @@ -410,9 +408,9 @@ gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, cons struct in6_pktinfo *pkti; uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)]; - g_assert(fd >= 0); - g_assert(p); - g_assert(avahi_dns_packet_check_valid(p) >= 0); + assert(fd >= 0); + assert(p); + assert(avahi_dns_packet_is_valid(p) >= 0); if (!a) mdns_mcast_group_ipv6(&sa); @@ -444,21 +442,21 @@ gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, cons return sendmsg_loop(fd, &msg, 0 /*MSG_DONTROUTE*/); } -AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, gint *ret_iface, guint8* ret_ttl) { +AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) { AvahiDnsPacket *p= NULL; struct msghdr msg; struct iovec io; uint8_t aux[1024]; ssize_t l; struct cmsghdr *cmsg; - gboolean found_ttl = FALSE, found_iface = FALSE; - gint ms; + int found_ttl = 0, found_iface = 0; + int ms; - g_assert(fd >= 0); - g_assert(ret_sa); - g_assert(ret_dest_address); - g_assert(ret_iface); - g_assert(ret_ttl); + assert(fd >= 0); + assert(ret_sa); + assert(ret_dest_address); + assert(ret_iface); + assert(ret_ttl); if (ioctl(fd, FIONREAD, &ms) < 0) { avahi_log_warn("ioctl(): %s", strerror(errno)); @@ -491,8 +489,8 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, goto fail; } - g_assert(!(msg.msg_flags & MSG_CTRUNC)); - g_assert(!(msg.msg_flags & MSG_TRUNC)); + assert(!(msg.msg_flags & MSG_CTRUNC)); + assert(!(msg.msg_flags & MSG_TRUNC)); p->size = (size_t) l; *ret_ttl = 0; @@ -507,20 +505,20 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, if (cmsg->cmsg_type == IP_TTL) { *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg)); - found_ttl = TRUE; + found_ttl = 1; } else if (cmsg->cmsg_type == IP_PKTINFO) { struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); - *ret_iface = (gint) i->ipi_ifindex; + *ret_iface = (int) i->ipi_ifindex; ret_dest_address->address = i->ipi_addr.s_addr; - found_iface = TRUE; + found_iface = 1; } } } /* avahi_log_debug("ttl=%u iface=%i", *ret_ttl, *ret_iface); */ - g_assert(found_iface); - g_assert(found_ttl); + assert(found_iface); + assert(found_ttl); return p; @@ -531,22 +529,22 @@ fail: return NULL; } -AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, gint *ret_iface, guint8* ret_ttl) { +AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) { AvahiDnsPacket *p = NULL; struct msghdr msg; struct iovec io; uint8_t aux[64]; ssize_t l; - gint ms; + int ms; struct cmsghdr *cmsg; - gboolean found_ttl = FALSE, found_iface = FALSE; + int found_ttl = 0, found_iface = 0; - g_assert(fd >= 0); - g_assert(ret_sa); - g_assert(ret_dest_address); - g_assert(ret_iface); - g_assert(ret_ttl); + assert(fd >= 0); + assert(ret_sa); + assert(ret_dest_address); + assert(ret_iface); + assert(ret_ttl); if (ioctl(fd, FIONREAD, &ms) < 0) { avahi_log_warn("ioctl(): %s", strerror(errno)); @@ -579,19 +577,19 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) { *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg)); - found_ttl = TRUE; + found_ttl = 1; } if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) { struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); *ret_iface = i->ipi6_ifindex; memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16); - found_iface = TRUE; + found_iface = 1; } } - g_assert(found_iface); - g_assert(found_ttl); + assert(found_iface); + assert(found_ttl); return p; @@ -602,7 +600,7 @@ fail: return NULL; } -gint avahi_open_legacy_unicast_socket_ipv4(void) { +int avahi_open_legacy_unicast_socket_ipv4(void) { struct sockaddr_in local; int fd = -1, yes; @@ -650,7 +648,7 @@ fail: return -1; } -gint avahi_open_legacy_unicast_socket_ipv6(void) { +int avahi_open_legacy_unicast_socket_ipv6(void) { struct sockaddr_in local; int fd = -1, yes; diff --git a/avahi-core/socket.h b/avahi-core/socket.h index 0643d79..d795156 100644 --- a/avahi-core/socket.h +++ b/avahi-core/socket.h @@ -23,6 +23,7 @@ ***/ #include +#include #include "dns.h" @@ -30,22 +31,22 @@ #define AVAHI_IPV4_MCAST_GROUP "224.0.0.251" #define AVAHI_IPV6_MCAST_GROUP "ff02::fb" -gint avahi_open_socket_ipv4(void); -gint avahi_open_socket_ipv6(void); +int avahi_open_socket_ipv4(void); +int avahi_open_socket_ipv6(void); -gint avahi_open_legacy_unicast_socket_ipv4(void); -gint avahi_open_legacy_unicast_socket_ipv6(void); +int avahi_open_legacy_unicast_socket_ipv4(void); +int avahi_open_legacy_unicast_socket_ipv6(void); -gint avahi_send_dns_packet_ipv4(gint fd, gint iface, AvahiDnsPacket *p, const AvahiIPv4Address *a, guint16 port); -gint avahi_send_dns_packet_ipv6(gint fd, gint iface, AvahiDnsPacket *p, const AvahiIPv6Address *a, guint16 port); +int avahi_send_dns_packet_ipv4(int fd, int iface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port); +int avahi_send_dns_packet_ipv6(int fd, int iface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port); -AvahiDnsPacket *avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in*ret_sa, AvahiIPv4Address *ret_dest_address, gint *ret_iface, guint8 *ret_ttl); -AvahiDnsPacket *avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6*ret_sa, AvahiIPv6Address *ret_dest_address, gint *ret_iface, guint8 *ret_ttl); +AvahiDnsPacket *avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in*ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t *ret_ttl); +AvahiDnsPacket *avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6*ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t *ret_ttl); -int avahi_mdns_mcast_join_ipv4(gint fd, gint idx); -int avahi_mdns_mcast_join_ipv6(gint fd, gint idx); +int avahi_mdns_mcast_join_ipv4(int fd, int idx); +int avahi_mdns_mcast_join_ipv6(int fd, int idx); -int avahi_mdns_mcast_leave_ipv4(gint fd, gint idx); -int avahi_mdns_mcast_leave_ipv6(gint fd, gint idx); +int avahi_mdns_mcast_leave_ipv4(int fd, int idx); +int avahi_mdns_mcast_leave_ipv6(int fd, int idx); #endif diff --git a/avahi-core/timeeventq-test.c b/avahi-core/timeeventq-test.c index 613d438..46a948a 100644 --- a/avahi-core/timeeventq-test.c +++ b/avahi-core/timeeventq-test.c @@ -23,37 +23,47 @@ #include #endif -#include +#include +#include #include +#include + #include "timeeventq.h" +#include "log.h" + +#define POINTER_TO_INT(p) ((int) (p)) +#define INT_TO_POINTER(i) ((void*) (i)) static AvahiTimeEventQueue *q = NULL; -static void callback(AvahiTimeEvent*e, gpointer userdata) { +static void callback(AvahiTimeEvent*e, void* userdata) { struct timeval tv = {0, 0}; - g_assert(e); - g_message("callback(%i)", GPOINTER_TO_INT(userdata)); + assert(e); + avahi_log_info("callback(%i)", POINTER_TO_INT(userdata)); avahi_elapse_time(&tv, 1000, 100); - avahi_time_event_queue_update(q, e, &tv); + avahi_time_event_update(e, &tv); } int main(int argc, char *argv[]) { - GMainLoop *loop = NULL; struct timeval tv; - - q = avahi_time_event_queue_new(NULL, 0); + AvahiSimplePoll *s; + + s = avahi_simple_poll_new(); + + q = avahi_time_event_queue_new(avahi_simple_poll_get(s)); + + avahi_time_event_new(q, avahi_elapse_time(&tv, 5000, 100), callback, INT_TO_POINTER(1)); + avahi_time_event_new(q, avahi_elapse_time(&tv, 5000, 100), callback, INT_TO_POINTER(2)); - avahi_time_event_queue_add(q, avahi_elapse_time(&tv, 5000, 100), callback, GINT_TO_POINTER(1)); - avahi_time_event_queue_add(q, avahi_elapse_time(&tv, 5000, 100), callback, GINT_TO_POINTER(2)); + avahi_log_info("starting"); - g_message("starting"); - - loop = g_main_loop_new(NULL, FALSE); - g_main_loop_run(loop); - g_main_loop_unref(loop); + for (;;) + if (avahi_simple_poll_iterate(s, -1) != 0) + break; avahi_time_event_queue_free(q); + avahi_simple_poll_free(s); return 0; } diff --git a/avahi-core/timeeventq.c b/avahi-core/timeeventq.c index 88e1779..b069ffd 100644 --- a/avahi-core/timeeventq.c +++ b/avahi-core/timeeventq.c @@ -23,12 +23,32 @@ #include #endif +#include +#include + #include -#include "timeeventq.h" +#include -static gint compare(gconstpointer _a, gconstpointer _b) { +#include "timeeventq.h" +#include "log.h" + +struct AvahiTimeEvent { + AvahiTimeEventQueue *queue; + AvahiPrioQueueNode *node; + struct timeval expiry; + struct timeval last_run; + AvahiTimeEventCallback callback; + void* userdata; +}; + +struct AvahiTimeEventQueue { + AvahiPoll *poll_api; + AvahiPrioQueue *prioq; +}; + +static int compare(const void* _a, const void* _b) { const AvahiTimeEvent *a = _a, *b = _b; - gint ret; + int ret; if ((ret = avahi_timeval_compare(&a->expiry, &b->expiry)) != 0) return ret; @@ -38,198 +58,158 @@ static gint compare(gconstpointer _a, gconstpointer _b) { return avahi_timeval_compare(&a->last_run, &b->last_run); } -static void source_get_timeval(GSource *source, struct timeval *tv) { - GTimeVal gtv; - - g_assert(source); - g_assert(tv); - - g_source_get_current_time(source, >v); - tv->tv_sec = gtv.tv_sec; - tv->tv_usec = gtv.tv_usec; -} - -static gboolean prepare_func(GSource *source, gint *timeout) { - AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; - AvahiTimeEvent *e; - struct timeval now; - - g_assert(source); - g_assert(timeout); - - if (!q->prioq->root) { - *timeout = -1; - return FALSE; - } - - e = q->prioq->root->data; - g_assert(e); - - source_get_timeval(source, &now); - - if (avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */ - avahi_timeval_compare(&now, &e->last_run) != 0 /* Not yet run */) { - *timeout = -1; - return TRUE; - } +static void expiration_event(AvahiPoll *poll_api, void *userdata); - *timeout = (gint) (avahi_timeval_diff(&e->expiry, &now)/1000); +static void update_wakeup(AvahiTimeEventQueue *q) { + assert(q); - /* Wait at least 1 msec */ - if (*timeout <= 0) - *timeout = 1; - - return FALSE; + if (q->prioq->root) { + AvahiTimeEvent *e = q->prioq->root->data; + q->poll_api->set_wakeup(q->poll_api, &e->expiry, expiration_event, q); + } else + q->poll_api->set_wakeup(q->poll_api, NULL, NULL, NULL); } -static gboolean check_func(GSource *source) { - AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; - AvahiTimeEvent *e; +void expiration_event(AvahiPoll *poll_api, void *userdata) { struct timeval now; + AvahiTimeEventQueue *q = userdata; + AvahiTimeEvent *e; - g_assert(source); - - if (!q->prioq->root) - return FALSE; - - e = q->prioq->root->data; - g_assert(e); - - source_get_timeval(source, &now); + gettimeofday(&now, NULL); - return - avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */ - avahi_timeval_compare(&now, &e->last_run) != 0; /* Not yet run */ -} - -static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { - AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source; - struct timeval now; + if ((e = avahi_time_event_queue_root(q))) { - g_assert(source); + /* Check if expired */ + if (avahi_timeval_compare(&now, &e->expiry) >= 0) { - source_get_timeval(source, &now); + /* Make sure to move the entry away from the front */ + e->last_run = now; + avahi_prio_queue_shuffle(q->prioq, e->node); - while (q->prioq->root) { - AvahiTimeEvent *e = q->prioq->root->data; - - /* Not yet expired */ - if (avahi_timeval_compare(&now, &e->expiry) < 0) - break; - - /* Already ran */ - if (avahi_timeval_compare(&now, &e->last_run) == 0) - break; - - /* Make sure to move the entry away from the front */ - e->last_run = now; - avahi_prio_queue_shuffle(q->prioq, e->node); - - /* Run it */ - g_assert(e->callback); - e->callback(e, e->userdata); + /* Run it */ + assert(e->callback); + e->callback(e, e->userdata); + } } - return TRUE; + update_wakeup(q); } static void fix_expiry_time(AvahiTimeEvent *e) { struct timeval now; - g_assert(e); + assert(e); - source_get_timeval(&e->queue->source, &now); + gettimeofday(&now, NULL); if (avahi_timeval_compare(&now, &e->expiry) > 0) e->expiry = now; - } -AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority) { +AvahiTimeEventQueue* avahi_time_event_queue_new(AvahiPoll *poll_api) { AvahiTimeEventQueue *q; - static GSourceFuncs source_funcs = { - prepare_func, - check_func, - dispatch_func, - NULL, - NULL, - NULL - }; + if (!(q = avahi_new(AvahiTimeEventQueue, 1))) { + avahi_log_error(__FILE__": Out of memory"); + goto oom; + } - q = (AvahiTimeEventQueue*) g_source_new(&source_funcs, sizeof(AvahiTimeEventQueue)); - q->prioq = avahi_prio_queue_new(compare); + if (!(q->prioq = avahi_prio_queue_new(compare))) + goto oom; - g_source_set_priority((GSource*) q, priority); - - g_source_attach(&q->source, context); - + q->poll_api = poll_api; return q; + +oom: + + if (q) + avahi_free(q); + + return NULL; } void avahi_time_event_queue_free(AvahiTimeEventQueue *q) { - g_assert(q); + assert(q); while (q->prioq->root) - avahi_time_event_queue_remove(q, q->prioq->root->data); + avahi_time_event_free(q->prioq->root->data); avahi_prio_queue_free(q->prioq); - g_source_destroy(&q->source); - g_source_unref(&q->source); + avahi_free(q); } -AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const struct timeval *timeval, AvahiTimeEventCallback callback, gpointer userdata) { +AvahiTimeEvent* avahi_time_event_new( + AvahiTimeEventQueue *q, + const struct timeval *timeval, + AvahiTimeEventCallback callback, + void* userdata) { + AvahiTimeEvent *e; - g_assert(q); - g_assert(timeval); - g_assert(callback); - g_assert(userdata); + assert(q); + assert(callback); + assert(userdata); - e = g_new(AvahiTimeEvent, 1); + if (!(e = avahi_new(AvahiTimeEvent, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return NULL; /* OOM */ + } + e->queue = q; e->callback = callback; e->userdata = userdata; - e->expiry = *timeval; + if (timeval) + e->expiry = *timeval; + else { + e->expiry.tv_sec = 0; + e->expiry.tv_usec = 0; + } + fix_expiry_time(e); e->last_run.tv_sec = 0; e->last_run.tv_usec = 0; - e->node = avahi_prio_queue_put(q->prioq, e); - + if (!(e->node = avahi_prio_queue_put(q->prioq, e))) { + avahi_free(e); + return NULL; + } + + update_wakeup(q); return e; } -void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e) { - g_assert(q); - g_assert(e); - g_assert(e->queue == q); +void avahi_time_event_free(AvahiTimeEvent *e) { + AvahiTimeEventQueue *q; + assert(e); + + q = e->queue; avahi_prio_queue_remove(q->prioq, e->node); - g_free(e); + avahi_free(e); + + update_wakeup(q); } -void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const struct timeval *timeval) { - g_assert(q); - g_assert(e); - g_assert(e->queue == q); - g_assert(timeval); +void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) { + assert(e); + assert(timeval); e->expiry = *timeval; fix_expiry_time(e); - - avahi_prio_queue_shuffle(q->prioq, e->node); + avahi_prio_queue_shuffle(e->queue->prioq, e->node); + + update_wakeup(e->queue); } AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) { - g_assert(q); + assert(q); return q->prioq->root ? q->prioq->root->data : NULL; } AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) { - g_assert(e); + assert(e); return e->node->next->data; } diff --git a/avahi-core/timeeventq.h b/avahi-core/timeeventq.h index 65f1420..b99c81f 100644 --- a/avahi-core/timeeventq.h +++ b/avahi-core/timeeventq.h @@ -27,31 +27,23 @@ typedef struct AvahiTimeEventQueue AvahiTimeEventQueue; typedef struct AvahiTimeEvent AvahiTimeEvent; -#include "prioq.h" - -typedef void (*AvahiTimeEventCallback)(AvahiTimeEvent *e, gpointer userdata); +#include -struct AvahiTimeEvent { - AvahiTimeEventQueue *queue; - AvahiPrioQueueNode *node; - struct timeval expiry; - struct timeval last_run; - AvahiTimeEventCallback callback; - gpointer userdata; -}; +#include "prioq.h" -struct AvahiTimeEventQueue { - GSource source; - AvahiPrioQueue *prioq; -}; +typedef void (*AvahiTimeEventCallback)(AvahiTimeEvent *e, void* userdata); -AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority); +AvahiTimeEventQueue* avahi_time_event_queue_new(AvahiPoll *poll_api); void avahi_time_event_queue_free(AvahiTimeEventQueue *q); -AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const struct timeval *timeval, AvahiTimeEventCallback callback, gpointer userdata); -void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e); +AvahiTimeEvent* avahi_time_event_new( + AvahiTimeEventQueue *q, + const struct timeval *timeval, + AvahiTimeEventCallback callback, + void* userdata); -void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const struct timeval *timeval); +void avahi_time_event_free(AvahiTimeEvent *e); +void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval); AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q); AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e); diff --git a/avahi-core/util.c b/avahi-core/util.c index 4fe8737..77632fd 100644 --- a/avahi-core/util.c +++ b/avahi-core/util.c @@ -29,8 +29,7 @@ #include #include -#include - +#include #include "util.h" void avahi_hexdump(const void* p, size_t size) { @@ -73,7 +72,8 @@ char *avahi_format_mac_address(const uint8_t* mac, size_t size) { unsigned i; static const char hex[] = "0123456789abcdef"; - t = r = g_new(char, size > 0 ? size*3 : 1); + if (!(t = r = avahi_new(char, size > 0 ? size*3 : 1))) + return NULL; if (size <= 0) { *r = 0; @@ -92,8 +92,6 @@ char *avahi_format_mac_address(const uint8_t* mac, size_t size) { return r; } - - char *avahi_strdown(char *s) { char *c; @@ -114,4 +112,3 @@ char *avahi_strup(char *s) { return s; } - -- cgit