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/announce.c | 188 ++++++++++++++++++++++++++------------------------ 1 file changed, 99 insertions(+), 89 deletions(-) (limited to 'avahi-core/announce.c') 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"); */ -- cgit