diff options
Diffstat (limited to 'avahi-core/server.c')
-rw-r--r-- | avahi-core/server.c | 1348 |
1 files changed, 714 insertions, 634 deletions
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 <unistd.h> #include <errno.h> #include <stdio.h> +#include <assert.h> #include <avahi-common/domain.h> #include <avahi-common/timeval.h> +#include <avahi-common/malloc.h> #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; } |