X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=sidebyside;f=avahi-core%2Fserver.c;h=a1486f88935e52148d65d7c70fab7ebe6b9461e2;hb=6b391bb81f0dce0193a722254016b26c12a17643;hp=3f37e1f3d764b680fcee527119fcfea1a6c1f8f0;hpb=602a2b6481587b7da2594db39151ec9380f276df;p=catta diff --git a/avahi-core/server.c b/avahi-core/server.c index 3f37e1f..a1486f8 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -28,445 +28,867 @@ #include #include #include +#include +#include +#include +#include -#include "server.h" -#include "util.h" +#include +#include +#include +#include + +#include "internal.h" #include "iface.h" #include "socket.h" -#include "subscribe.h" +#include "browse.h" +#include "log.h" +#include "util.h" +#include "dns-srv-rr.h" -static void free_entry(AvahiServer*s, AvahiEntry *e) { - AvahiEntry *t; +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(e); + assert(s); + assert(i); + assert(name); + assert(callback); - avahi_goodbye_entry(s, e, TRUE); + assert(type != AVAHI_DNS_TYPE_ANY); - /* Remove from linked list */ - AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e); + if (!(k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type))) + return; /** OOM */ - /* Remove from hash table indexed by name */ - t = g_hash_table_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); - else - g_hash_table_remove(s->entries_by_key, e->record->key); + 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_PUBLISH_UNIQUE, userdata); - /* 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_key_unref(k); } -static void free_group(AvahiServer *s, AvahiEntryGroup *g) { - g_assert(s); - g_assert(g); +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) { + enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_SRV, callback, userdata); + enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_TXT, callback, userdata); + } else if (r->key->type == AVAHI_DNS_TYPE_SRV) { + enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_A, callback, userdata); + enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_AAAA, callback, userdata); + } + } +} - while (g->entries) - free_entry(s, g->entries); +void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, int unicast_response, int auxiliary) { + assert(s); + assert(i); + assert(e); - AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g); - g_free(g); + avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_PUBLISH_UNIQUE, unicast_response, auxiliary); } -static void cleanup_dead(AvahiServer *s) { - AvahiEntryGroup *g, *ng; - AvahiEntry *e, *ne; - g_assert(s); +void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, int unicast_response) { + AvahiEntry *e; +/* char *txt; */ + + assert(s); + assert(i); + assert(k); +/* avahi_log_debug("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */ +/* avahi_free(txt); */ - if (s->need_group_cleanup) { - for (g = s->groups; g; g = ng) { - ng = g->groups_next; - - if (g->dead) - free_group(s, g); + 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_is_registered(s, e, i)) + avahi_server_prepare_response(s, i, e, unicast_response, 0); + + } else { + + /* Handle all other queries */ + + 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) { + 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, 0, 1); + k->dead = 1; + } } - s->need_group_cleanup = FALSE; + e->group->n_probing = 0; + + avahi_s_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION); + } else { + avahi_goodbye_entry(s, e, 0, 1); + e->dead = 1; } - if (s->need_entry_cleanup) { - for (e = s->entries; e; e = ne) { - ne = e->entries_next; + s->need_entry_cleanup = 1; +} + +static void withdraw_rrset(AvahiServer *s, AvahiKey *key) { + AvahiEntry *e; + + assert(s); + assert(key); + + 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; + char *t; + int ours = 0, won = 0, lost = 0; + + assert(s); + assert(record); + assert(i); + + t = avahi_record_to_string(record); + +/* avahi_log_debug("incoming_probe()"); */ + + 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 = 1; + break; + } else { - if (e->dead) - free_entry(s, e); + if (avahi_entry_is_probing(s, e, i)) { + if (cmp > 0) + won = 1; + else /* cmp < 0 */ + lost = 1; + } } + } + + if (!ours) { - s->need_entry_cleanup = FALSE; + if (won) + avahi_log_debug("Recieved conflicting probe [%s]. Local host won.", t); + else if (lost) { + avahi_log_debug("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t); + withdraw_rrset(s, record->key); + }/* else */ +/* avahi_log_debug("Not conflicting probe"); */ } + + avahi_free(t); } -static void send_unicast_response_packet(AvahiServer *s, AvahiInterface *i, const AvahiAddress *a, guint16 port) { - g_assert(s); - g_assert(a); - g_assert(port > 0); - g_assert(s->unicast_packet); +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; + + assert(s); + assert(i); + assert(record); + - if (avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT) != 0) - avahi_interface_send_packet_unicast(i, s->unicast_packet, a, port); +/* avahi_log_debug("CHECKING FOR CONFLICT: [%s]", t); */ - avahi_dns_packet_free(s->unicast_packet); - s->unicast_packet = NULL; -} + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) { + n = e->by_key_next; -static void post_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, AvahiRecord *r, gboolean flush_cache, gboolean legacy_unicast, gboolean unicast_response) { - g_assert(s); - g_assert(a); - g_assert(port > 0); - g_assert(r); + if (e->dead) + continue; - if (legacy_unicast) { + /* Check if the incoming is a goodbye record */ + if (avahi_record_is_goodbye(record)) { - /* Respond with a legacy unicast packet */ - - if (!(s->unicast_packet)) - s->unicast_packet = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , TRUE, TRUE); + if (avahi_record_equal_no_ttl(e->record, record)) { + char *t; - if (avahi_dns_packet_append_record(s->unicast_packet, r, FALSE, 10)) + /* Refresh */ + t = avahi_record_to_string(record); + avahi_log_debug("Recieved goodbye record for one of our records [%s]. Refreshing.", t); + avahi_server_prepare_matching_responses(s, i, e->record->key, 0); - /* Increment the ANCOUNT field */ - - avahi_dns_packet_set_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT, - avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT)+1); + valid = 0; + avahi_free(t); + break; + } - /* If there's no space left for this response we simply don't send it */ + /* If the goodybe packet doesn't match one of our own RRs, we simply ignore it. */ + continue; + } - } else { + if (!(e->flags & AVAHI_PUBLISH_UNIQUE) && !unique) + continue; - if (!avahi_interface_post_response(i, a, r, flush_cache, FALSE) && unicast_response) { + /* 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 = 1; /* We have an identical record, so this is no conflict */ - /* Due to some reasons the record has not been scheduled. - * The client requested an unicast response in that - * case. Therefore we prepare such a response */ - - for (;;) { + /* Check wheter there is a TTL conflict */ + if (record->ttl <= e->record->ttl/2 && + avahi_entry_is_registered(s, e, i)) { + char *t; + /* Refresh */ + t = avahi_record_to_string(record); - if (!(s->unicast_packet)) - s->unicast_packet = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE); + avahi_log_debug("Recieved record with bad TTL [%s]. Refreshing.", t); + avahi_server_prepare_matching_responses(s, i, e->record->key, 0); + valid = 0; - if (avahi_dns_packet_append_record(s->unicast_packet, r, flush_cache, 0)) { + avahi_free(t); + } + + /* There's no need to check the other entries of this RRset */ + break; - /* Appending this record succeeded, so incremeant - * the specific header field, and return to the caller */ - - avahi_dns_packet_set_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT, - avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT)+1); + } else { + + if (avahi_entry_is_registered(s, e, i)) { + + /* A conflict => we have to return to probe mode */ + conflict = 1; + conflicting_entry = e; - break; - } + } else if (avahi_entry_is_probing(s, e, i)) { - if (avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT) == 0) { - g_warning("Record too large, doesn't fit in any packet!"); - return; - } + /* We are currently registering a matching record, but + * someone else already claimed it, so let's + * withdraw */ + conflict = 1; + withdraw_immediately = 1; + } + } + } - /* Appending the record didn't succeeed, so let's send this packet, and create a new one */ +/* avahi_log_debug("ours=%i conflict=%i", ours, conflict); */ - send_unicast_response_packet(s, i, a, port); - - avahi_dns_packet_free(s->unicast_packet); - s->unicast_packet = NULL; - } - + if (!ours && conflict) { + char *t; + + valid = 0; + + t = avahi_record_to_string(record); + + if (withdraw_immediately) { + avahi_log_debug("Recieved conflicting record [%s] with local record to be. Withdrawing.", t); + withdraw_rrset(s, record->key); + } else { + assert(conflicting_entry); + avahi_log_debug("Recieved conflicting record [%s]. Resetting our record.", t); + avahi_entry_return_to_initial_state(s, conflicting_entry, i); + + /* Local unique records are returned to probing + * state. Local shared records are reannounced. */ } + + avahi_free(t); } + + return valid; } -static void handle_query_key(AvahiServer *s, AvahiDnsPacket *p, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) { - AvahiEntry *e; - gchar *txt; +static void append_aux_callback(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) { + int *unicast_response = userdata; + + assert(s); + assert(r); + assert(unicast_response); - g_assert(s); - g_assert(k); - g_assert(i); - g_assert(a); + avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, 1); +} - g_message("Handling query: %s", txt = avahi_key_to_string(k)); - g_free(txt); +static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int unicast_response) { + assert(s); + assert(r); - avahi_packet_scheduler_incoming_query(i->scheduler, k); + avahi_server_enumerate_aux_records(s, i, r, append_aux_callback, &unicast_response); +} - if (k->type == AVAHI_DNS_TYPE_ANY) { +void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port, int legacy_unicast, int immediately) { - /* Handle ANY query */ + assert(s); + assert(i); + assert(!legacy_unicast || (a && port > 0 && p)); + + if (legacy_unicast) { + AvahiDnsPacket *reply; + AvahiRecord *r; + + if (!(reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , 1, 1))) + return; /* OOM */ - 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)) - post_response(s, p, i, a, port, e->record, e->flags & AVAHI_ENTRY_UNIQUE, legacy_unicast, unicast_response); + while ((r = avahi_record_list_next(s->record_list, NULL, NULL, NULL))) { + + append_aux_records_to_list(s, i, r, 0); + + if (avahi_dns_packet_append_record(reply, r, 0, 10)) + avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); + else { + char *t = avahi_record_to_string(r); + avahi_log_warn("Record [%s] not fitting in legacy unicast packet, dropping.", t); + avahi_free(t); + } + + avahi_record_unref(r); + } + + if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0) + avahi_interface_send_packet_unicast(i, reply, a, port); + + avahi_dns_packet_free(reply); } else { + int unicast_response, flush_cache, auxiliary; + AvahiDnsPacket *reply = NULL; + AvahiRecord *r; - /* Handle all other queries */ + /* In case the query packet was truncated never respond + immediately, because known answer suppression records might be + contained in later packets */ + int tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC); - 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)) - post_response(s, p, i, a, port, e->record, e->flags & AVAHI_ENTRY_UNIQUE, legacy_unicast, unicast_response); - } -} + while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) { + + if (!avahi_interface_post_response(i, r, flush_cache, a, immediately || (flush_cache && !tc && !auxiliary)) && unicast_response) { -static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { - g_assert(s); - g_assert(e); + append_aux_records_to_list(s, i, r, unicast_response); + + /* Due to some reasons the record has not been scheduled. + * The client requested an unicast response in that + * case. Therefore we prepare such a response */ - - if (e->group) { - AvahiEntry *k; - - for (k = e->group->entries; k; k = k->by_group_next) { - avahi_goodbye_entry(s, k, FALSE); - k->dead = TRUE; + for (;;) { + + if (!reply) { + 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)) { + + /* Appending this record succeeded, so incremeant + * the specific header field, and return to the caller */ + + avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); + + break; + } + + if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) == 0) { + size_t size; + + /* The record is too large for one packet, so create a larger packet */ + + avahi_dns_packet_free(reply); + 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; + + if (!(reply = avahi_dns_packet_new_reply(p, size, 0, 1))) + break; /* OOM */ + + if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) { + 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); + avahi_free(t); + break; + } else + avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); + } + + /* Appending the record didn't succeeed, so let's send this packet, and create a new one */ + avahi_interface_send_packet_unicast(i, reply, a, port); + avahi_dns_packet_free(reply); + reply = NULL; + } + } + + avahi_record_unref(r); + } + + if (reply) { + if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0) + avahi_interface_send_packet_unicast(i, reply, a, port); + avahi_dns_packet_free(reply); } - - avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION); - } else { - avahi_goodbye_entry(s, e, FALSE); - e->dead = TRUE; } - s->need_entry_cleanup = TRUE; + avahi_record_list_flush(s->record_list); } -static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) { - AvahiEntry *e, *n; - gchar *t; + +static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int flush_cache) { + AvahiInterface *j; - g_assert(s); - g_assert(record); - g_assert(i); + assert(s); + assert(i); + assert(r); - t = avahi_record_to_string(record); + 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, 1); +} + +static void* reflect_cache_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) { + AvahiServer *s = userdata; + + assert(c); + assert(pattern); + assert(e); + assert(s); -/* g_message("PROBE: [%s]", t); */ + 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; - for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { - n = e->by_key_next; + assert(s); + assert(i); + assert(k); - if (e->dead || avahi_record_equal_no_ttl(record, e->record)) - continue; + if (!s->config.enable_reflector) + return; - if (avahi_entry_registering(s, e, i)) { - gint cmp; + 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, 1); - if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) { - withdraw_entry(s, e); - g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t); - } else if (cmp < 0) - g_message("Recieved conflicting probe [%s]. Local host won.", t); + /* Reply from caches of other network. This is needed to + * "work around" known answer suppression. */ + avahi_cache_walk(j->cache, k, reflect_cache_walk_callback, s); } - } +} + +static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) { + AvahiInterface *j; + + assert(s); + assert(i); + assert(r); + + if (!s->config.enable_reflector) + return; - g_free(t); + 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, 1); } -static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) { - guint n; +static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port, int legacy_unicast, int from_local_iface) { + 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); - g_assert(!s->unicast_packet); +/* avahi_log_debug("query"); */ + 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))) { - g_warning("Packet too short (1)"); - return; + avahi_log_warn("Packet too short (1)"); + goto fail; + } + + if (!legacy_unicast && !from_local_iface) { + reflect_query(s, i, key); + avahi_cache_start_poof(i->cache, key, a); } - handle_query_key(s, p, key, i, a, port, legacy_unicast, unicast_response); + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 && + !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC)) + /* Allow our own queries to be suppressed by incoming + * queries only when they do not include known answers */ + avahi_query_scheduler_incoming(i->query_scheduler, key); + + avahi_server_prepare_matching_responses(s, i, key, unicast_response); avahi_key_unref(key); } - /* Known Answer Suppression */ - for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) { - AvahiRecord *record; - gboolean unique = FALSE; + if (!legacy_unicast) { - if (!(record = avahi_dns_packet_consume_record(p, &unique))) { - g_warning("Packet too short (2)"); - return; + /* Known Answer Suppression */ + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) { + AvahiRecord *record; + int unique = 0; + + if (!(record = avahi_dns_packet_consume_record(p, &unique))) { + avahi_log_warn("Packet too short (2)"); + goto fail; + } + + if (handle_conflict(s, i, record, unique, a)) { + avahi_response_scheduler_suppress(i->response_scheduler, record, a); + avahi_record_list_drop(s->record_list, record); + avahi_cache_stop_poof(i->cache, record, a); + } + + avahi_record_unref(record); + } + + /* Probe record */ + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) { + AvahiRecord *record; + int unique = 0; + + if (!(record = avahi_dns_packet_consume_record(p, &unique))) { + avahi_log_warn("Packet too short (3)"); + goto fail; + } + + if (!avahi_key_is_pattern(record->key)) { + if (!from_local_iface) + reflect_probe(s, i, record); + incoming_probe(s, record, i); + } + + avahi_record_unref(record); } - - avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a); - avahi_record_unref(record); } - /* Probe record */ - for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) { + if (!avahi_record_list_is_empty(s->record_list)) + avahi_server_generate_response(s, i, p, a, port, legacy_unicast, is_probe); + + return; + +fail: + avahi_record_list_flush(s->record_list); +} + +static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, int from_local_iface) { + unsigned n; + + 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 unique = FALSE; + int cache_flush = 0; +/* char *txt; */ + + if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { + avahi_log_warn("Packet too short (4)"); + break; + } - if (!(record = avahi_dns_packet_consume_record(p, &unique))) { - g_warning("Packet too short (3)"); - return; + if (!avahi_key_is_pattern(record->key)) { + +/* avahi_log_debug("Handling response: %s", txt = avahi_record_to_string(record)); */ +/* avahi_free(txt); */ + + if (handle_conflict(s, i, record, cache_flush, a)) { + if (!from_local_iface) + reflect_response(s, i, record, cache_flush); + avahi_cache_update(i->cache, record, cache_flush, a); + avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush); + } } + + avahi_record_unref(record); + } + + /* If the incoming response contained a conflicting record, some + 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, 0, 1); +} + +static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) { + unsigned n, idx = (unsigned) -1; + AvahiLegacyUnicastReflectSlot *slot; + + assert(s); - if (record->key->type != AVAHI_DNS_TYPE_ANY) - incoming_probe(s, record, i); + if (!s->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; - avahi_record_unref(record); + if (!s->legacy_unicast_reflect_slots[idx]) + break; } - if (s->unicast_packet) - send_unicast_response_packet(s, i, a, port); + if (idx == (unsigned) -1 || s->legacy_unicast_reflect_slots[idx]) + return NULL; + + if (!(slot = avahi_new(AvahiLegacyUnicastReflectSlot, 1))) + return NULL; /* OOM */ + + s->legacy_unicast_reflect_slots[idx] = slot; + slot->id = s->legacy_unicast_reflect_id++; + slot->server = s; + + return slot; } -static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) { - gboolean valid = TRUE; - AvahiEntry *e, *n; - gchar *t; +static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) { + unsigned idx; + + assert(s); + assert(slot); + + idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; + + assert(s->legacy_unicast_reflect_slots[idx] == slot); + + avahi_time_event_free(slot->time_event); - g_assert(s); - g_assert(i); - g_assert(record); + avahi_free(slot); + s->legacy_unicast_reflect_slots[idx] = NULL; +} - t = avahi_record_to_string(record); +static void free_slots(AvahiServer *s) { + unsigned idx; + assert(s); -/* g_message("CHECKING FOR CONFLICT: [%s]", t); */ + if (!s->legacy_unicast_reflect_slots) + return; - for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { - n = e->by_key_next; + for (idx = 0; idx < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; idx ++) + if (s->legacy_unicast_reflect_slots[idx]) + deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]); - if (e->dead) - continue; - - if (avahi_entry_registered(s, e, i)) { + avahi_free(s->legacy_unicast_reflect_slots); + s->legacy_unicast_reflect_slots = NULL; +} - gboolean equal = avahi_record_equal_no_ttl(record, e->record); - - /* Check whether there is a unique record conflict */ - if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) { - gint cmp; - - /* The lexicographically later data wins. */ - if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) { - g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t); - withdraw_entry(s, e); - } else if (cmp < 0) { - /* Tell the other host that our entry is lexicographically later */ +static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, uint16_t id) { + unsigned idx; + + assert(s); - g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t); + if (!s->legacy_unicast_reflect_slots) + return NULL; + + idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; - valid = FALSE; - avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE); - } - - /* Check wheter there is a TTL conflict */ - } else if (equal && record->ttl <= e->record->ttl/2) { - /* Correct the TTL */ - valid = FALSE; - avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE); - g_message("Recieved record with bad TTL [%s]. Refreshing.", t); - } - - } else if (avahi_entry_registering(s, e, i)) { + if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id) + return NULL; - if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) { + return s->legacy_unicast_reflect_slots[idx]; +} - /* We are currently registering a matching record, but - * someone else already claimed it, so let's - * withdraw */ - - g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t); - withdraw_entry(s, e); - } - } +static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) { + AvahiLegacyUnicastReflectSlot *slot = userdata; + + 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, uint16_t port) { + AvahiLegacyUnicastReflectSlot *slot; + AvahiInterface *j; + + assert(s); + assert(p); + assert(i); + assert(a); + assert(port > 0); + assert(i->protocol == a->proto); + + if (!s->config.enable_reflector) + return; + +/* avahi_log_debug("legacy unicast reflector"); */ + + /* Reflecting legacy unicast queries is a little more complicated + than reflecting normal queries, since we must route the + responses back to the right client. Therefore we must store + some information for finding the right client contact data for + response packets. In contrast to normal queries legacy + unicast query and response packets are reflected untouched and + are not reassembled into larger packets */ + + if (!(slot = allocate_slot(s))) { + /* No slot available, we drop this legacy unicast query */ + avahi_log_warn("No slot available for legacy unicast reflection, dropping query packet."); + return; } - g_free(t); + slot->original_id = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID); + slot->address = *a; + slot->port = port; + slot->interface = i->hardware->index; - return valid; + avahi_elapse_time(&slot->elapse_time, 2000, 0); + 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); + + for (j = s->monitor->interfaces; j; j = j->interface_next) + if (avahi_interface_is_relevant(j) && + j != i && + (s->config.reflect_ipv || j->protocol == i->protocol)) { + + if (j->protocol == AVAHI_PROTO_INET && s->fd_legacy_unicast_ipv4 >= 0) { + avahi_send_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, j->hardware->index, p, NULL, 0); + } else if (j->protocol == AVAHI_PROTO_INET6 && s->fd_legacy_unicast_ipv6 >= 0) + avahi_send_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, j->hardware->index, p, NULL, 0); + } + + /* Reset the id */ + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id); } -static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) { - guint n; +static int originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) { + AvahiAddress a; + assert(s); + assert(sa); + + if (!s->config.enable_reflector) + return 0; - g_assert(s); - g_assert(p); - g_assert(i); - g_assert(a); + avahi_address_from_sockaddr(sa, &a); + + if (!avahi_address_is_local(s->monitor, &a)) + return 0; - 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; + if (sa->sa_family == AF_INET && s->fd_legacy_unicast_ipv4 >= 0) { + struct sockaddr_in lsa; + socklen_t l = sizeof(lsa); - if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { - g_warning("Packet too short (4)"); - return; - } + if (getsockname(s->fd_legacy_unicast_ipv4, (struct sockaddr*) &lsa, &l) != 0) + avahi_log_warn("getsockname(): %s", strerror(errno)); + else + return lsa.sin_port == ((const struct sockaddr_in*) sa)->sin_port; + + } + + if (sa->sa_family == AF_INET6 && s->fd_legacy_unicast_ipv6 >= 0) { + struct sockaddr_in6 lsa; + socklen_t l = sizeof(lsa); + + if (getsockname(s->fd_legacy_unicast_ipv6, (struct sockaddr*) &lsa, &l) != 0) + avahi_log_warn("getsockname(): %s", strerror(errno)); + else + return lsa.sin6_port == ((const struct sockaddr_in6*) sa)->sin6_port; + } + + return 0; +} - if (record->key->type != AVAHI_DNS_TYPE_ANY) { +static int is_mdns_mcast_address(const AvahiAddress *a) { + AvahiAddress b; + assert(a); - g_message("Handling response: %s", txt = avahi_record_to_string(record)); - g_free(txt); - - if (handle_conflict(s, i, record, cache_flush, a)) { - avahi_cache_update(i->cache, record, cache_flush, a); - avahi_packet_scheduler_incoming_response(i->scheduler, record); - } - } - - avahi_record_unref(record); - } + avahi_address_parse(a->proto == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->proto, &b); + return avahi_address_cmp(a, &b) == 0; +} + +static int originates_from_local_iface(AvahiServer *s, AvahiIfIndex iface, const AvahiAddress *a, uint16_t port) { + assert(s); + assert(iface != AVAHI_IF_UNSPEC); + assert(a); + + /* If it isn't the MDNS port it can't be generated by us */ + if (port != AVAHI_MDNS_PORT) + return 0; + + return avahi_interface_has_address(s->monitor, iface, a); } -static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint 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; + int from_local_iface = 0; - g_assert(s); - g_assert(p); - g_assert(sa); - g_assert(iface > 0); - - if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) { - g_warning("Recieved packet from invalid interface."); + assert(s); + assert(p); + assert(sa); + assert(dest); + assert(iface > 0); + + if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) || + !avahi_interface_is_relevant(i)) { + avahi_log_warn("Recieved packet from invalid interface."); return; } - g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); - - if (sa->sa_family == AF_INET6) { - static const guint8 ipv4_in_ipv6[] = { - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0xFF, 0xFF, 0xFF, 0xFF }; +/* avahi_log_debug("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */ + port = avahi_port_from_sockaddr(sa); + avahi_address_from_sockaddr(sa, &a); + + if (avahi_address_is_ipv4_in_ipv6(&a)) /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */ + return; - if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) - return; - } + if (originates_from_local_legacy_unicast_socket(s, sa)) + /* This originates from our local reflector, so let's ignore it */ + return; - if (avahi_dns_packet_check_valid(p) < 0) { - g_warning("Recieved invalid packet."); + /* We don't want to reflect local traffic, so we check if this packet is generated locally. */ + if (s->config.enable_reflector) + from_local_iface = originates_from_local_iface(s, iface, &a, port); + + if (avahi_dns_packet_check_valid_multicast(p) < 0) { + avahi_log_warn("Recieved invalid packet."); return; } - port = avahi_port_from_sockaddr(sa); - avahi_address_from_sockaddr(sa, &a); - 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_QDCOUNT) == 0 || - avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { - g_warning("Invalid query packet."); + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { + avahi_log_warn("Invalid query packet."); return; } @@ -475,648 +897,781 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 || avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) { - g_warning("Invalid legacy unicast query packet."); + avahi_log_warn("Invalid legacy unicast query packet."); return; } - legacy_unicast = TRUE; + legacy_unicast = 1; } - handle_query(s, p, i, &a, port, legacy_unicast); + if (legacy_unicast) + reflect_legacy_unicast_query_packet(s, p, i, &a, port); - g_message("Handled query"); + handle_query_packet(s, p, i, &a, port, legacy_unicast, from_local_iface); + +/* avahi_log_debug("Handled query"); */ } else { - if (port != AVAHI_MDNS_PORT) { - g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol); + avahi_log_warn("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol); return; } - if (ttl != 255) { - g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol); - if (!s->ignore_bad_ttl) - return; + if (ttl != 255 && s->config.check_response_ttl) { + avahi_log_warn("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol); + return; } + if (!is_mdns_mcast_address(dest) && + !avahi_interface_address_on_link(i, &a)) { + avahi_log_warn("Recivied non-local response on interface '%s.%i'.", i->hardware->name, i->protocol); + return; + } + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 || avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 || avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) { - g_warning("Invalid response packet."); + avahi_log_warn("Invalid response packet."); return; } - handle_response(s, p, i, &a); - g_message("Handled response"); + handle_response_packet(s, p, i, &a, from_local_iface); +/* avahi_log_debug("Handled response"); */ } } -static void work(AvahiServer *s) { - struct sockaddr_in6 sa6; - struct sockaddr_in sa; - AvahiDnsPacket *p; - gint iface = -1; - guint8 ttl; - - g_assert(s); +static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface) { + AvahiInterface *i, *j; + AvahiAddress a; + AvahiLegacyUnicastReflectSlot *slot; + + assert(s); + assert(p); + assert(sa); + assert(iface > 0); + + if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) || + !avahi_interface_is_relevant(i)) { + avahi_log_warn("Recieved packet from invalid interface."); + return; + } - if (s->pollfd_ipv4.revents & G_IO_IN) { - if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) { - dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl); - avahi_dns_packet_free(p); - } +/* avahi_log_debug("new legacy unicast packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */ + + avahi_address_from_sockaddr(sa, &a); + + if (avahi_address_is_ipv4_in_ipv6(&a)) + /* 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)) { + avahi_log_warn("Recieved invalid packet."); + return; } - if (s->pollfd_ipv6.revents & G_IO_IN) { - if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) { - dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl); - avahi_dns_packet_free(p); - } + if (!(slot = find_slot(s, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)))) { + avahi_log_warn("Recieved legacy unicast response with unknown id"); + return; } + + if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.proto)) || + !avahi_interface_is_relevant(j)) + return; + + /* Patch the original ID into this response */ + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id); + + /* Forward the response to the correct client */ + avahi_interface_send_packet_unicast(j, p, &slot->address, slot->port); + + /* Undo changes to packet */ + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id); } -static gboolean prepare_func(GSource *source, gint *timeout) { - g_assert(source); - g_assert(timeout); +static void cleanup_dead(AvahiServer *s) { + assert(s); + + avahi_cleanup_dead_entries(s); + avahi_browser_cleanup(s); +} + +static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) { + AvahiServer *s = userdata; + AvahiAddress dest; + AvahiDnsPacket *p; + AvahiIfIndex iface; + uint8_t ttl; + struct sockaddr_in sa; + struct sockaddr_in6 sa6; + + assert(w); + assert(fd >= 0); + + if (events & AVAHI_WATCH_IN) { - *timeout = -1; - return FALSE; + if (fd == s->fd_ipv4) { + dest.proto = 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.proto = 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); + } + } else if (fd == s->fd_legacy_unicast_ipv4) { + dest.proto = 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); + avahi_dns_packet_free(p); + } + } else if (fd == s->fd_legacy_unicast_ipv6) { + dest.proto = 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); + avahi_dns_packet_free(p); + } + } + + cleanup_dead(s); + } else + abort(); } -static gboolean check_func(GSource *source) { - AvahiServer* s; - g_assert(source); +static void server_set_state(AvahiServer *s, AvahiServerState state) { + assert(s); - s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); - g_assert(s); + if (s->state == state) + return; - return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR); + s->state = state; + + if (s->callback) + s->callback(s, state, s->userdata); +} + +static void withdraw_host_rrs(AvahiServer *s) { + assert(s); + + if (s->hinfo_entry_group) + avahi_s_entry_group_reset(s->hinfo_entry_group); + + if (s->browse_domain_entry_group) + avahi_s_entry_group_reset(s->browse_domain_entry_group); + + avahi_interface_monitor_update_rrs(s->monitor, 1); + s->n_host_rr_pending = 0; } -static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { - AvahiServer* s; - g_assert(source); +void avahi_server_decrease_host_rr_pending(AvahiServer *s) { + assert(s); + + assert(s->n_host_rr_pending > 0); - s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); - g_assert(s); + if (--s->n_host_rr_pending == 0) + server_set_state(s, AVAHI_SERVER_RUNNING); +} - work(s); - cleanup_dead(s); +void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) { + assert(s); + assert(g); - return TRUE; + if (state == AVAHI_ENTRY_GROUP_REGISTERING && + s->state == AVAHI_SERVER_REGISTERING) + s->n_host_rr_pending ++; + + else if (state == AVAHI_ENTRY_GROUP_COLLISION && + (s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) { + withdraw_host_rrs(s); + server_set_state(s, AVAHI_SERVER_COLLISION); + + } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED && + s->state == AVAHI_SERVER_REGISTERING) + avahi_server_decrease_host_rr_pending(s); } -static void add_default_entries(AvahiServer *s) { +static void register_hinfo(AvahiServer *s) { struct utsname utsname; - AvahiAddress a; AvahiRecord *r; - g_assert(s); + assert(s); + + if (!s->config.publish_hinfo) + return; + + if (s->hinfo_entry_group) + assert(avahi_s_entry_group_is_empty(s->hinfo_entry_group)); + else + s->hinfo_entry_group = avahi_s_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->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); - 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, NULL, 0, AF_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)); - /* Add localhost entries */ - avahi_address_parse("127.0.0.1", AF_INET, &a); - avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a); + if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UNIQUE, r) < 0) { + avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error)); + return; + } + + avahi_record_unref(r); + } + + if (avahi_s_entry_group_commit(s->hinfo_entry_group) < 0) + avahi_log_warn("Failed to commit HINFO entry group: %s", avahi_strerror(s->error)); - avahi_address_parse("::1", AF_INET6, &a); - avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a); } -AvahiServer *avahi_server_new(GMainContext *c) { - gchar *hn; - AvahiServer *s; +static void register_localhost(AvahiServer *s) { + AvahiAddress a; + assert(s); - static GSourceFuncs source_funcs = { - prepare_func, - check_func, - dispatch_func, - NULL, - NULL, - NULL - }; + /* Add localhost entries */ + avahi_address_parse("127.0.0.1", AVAHI_PROTO_INET, &a); + avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_NO_ANNOUNCE, "localhost", &a); - s = g_new(AvahiServer, 1); + avahi_address_parse("::1", AVAHI_PROTO_INET6, &a); + avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_NO_ANNOUNCE, "ip6-localhost", &a); +} - s->ignore_bad_ttl = FALSE; - s->need_entry_cleanup = s->need_group_cleanup = FALSE; - - s->fd_ipv4 = avahi_open_socket_ipv4(); - s->fd_ipv6 = avahi_open_socket_ipv6(); - - if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) { - g_critical("Failed to create IP sockets.\n"); - g_free(s); - return NULL; - } +static void register_browse_domain(AvahiServer *s) { + assert(s); - if (s->fd_ipv4 < 0) - g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode"); - else if (s->fd_ipv6 < 0) - g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode"); - - if (c) - g_main_context_ref(s->context = c); + if (!s->config.publish_domain) + return; + + if (s->browse_domain_entry_group) + assert(avahi_s_entry_group_is_empty(s->browse_domain_entry_group)); else - s->context = g_main_context_default(); + s->browse_domain_entry_group = avahi_s_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_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); + 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; + } - AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions); - s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); + if (avahi_s_entry_group_commit(s->browse_domain_entry_group) < 0) + avahi_log_warn("Failed to commit browse domain entry group: %s", avahi_strerror(s->error)); +} - /* Get host name */ - hn = avahi_get_host_name(); - hn[strcspn(hn, ".")] = 0; +static void register_stuff(AvahiServer *s) { + assert(s); - s->hostname = g_strdup_printf("%s.local.", hn); - g_free(hn); + server_set_state(s, AVAHI_SERVER_REGISTERING); + s->n_host_rr_pending ++; /** Make sure that the state isn't changed tp AVAHI_SERVER_RUNNING too early */ - s->unicast_packet = NULL; + register_hinfo(s); + register_browse_domain(s); + avahi_interface_monitor_update_rrs(s->monitor, 0); - s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */ - s->monitor = avahi_interface_monitor_new(s); - avahi_interface_monitor_sync(s->monitor); - add_default_entries(s); + s->n_host_rr_pending --; - /* Prepare IO source registration */ - s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*)); - *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s; + if (s->n_host_rr_pending == 0) + server_set_state(s, AVAHI_SERVER_RUNNING); +} - memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4)); - s->pollfd_ipv4.fd = s->fd_ipv4; - s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP; - g_source_add_poll(s->source, &s->pollfd_ipv4); +static void update_fqdn(AvahiServer *s) { + char *n; - memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6)); - s->pollfd_ipv6.fd = s->fd_ipv6; - s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP; - g_source_add_poll(s->source, &s->pollfd_ipv6); + assert(s); + assert(s->host_name); + assert(s->domain_name); - g_source_attach(s->source, s->context); + if (!(n = avahi_strdup_printf("%s.%s", s->host_name, s->domain_name))) + return; /* OOM */ - return s; + avahi_free(s->host_name_fqdn); + s->host_name_fqdn = n; } -void avahi_server_free(AvahiServer* s) { - g_assert(s); - - while(s->entries) - free_entry(s, s->entries); +int avahi_server_set_host_name(AvahiServer *s, const char *host_name) { + assert(s); + assert(host_name); - avahi_interface_monitor_free(s->monitor); + if (host_name && !avahi_is_valid_host_name(host_name)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); - while (s->groups) - free_group(s, s->groups); + withdraw_host_rrs(s); - while (s->subscriptions) - avahi_subscription_free(s->subscriptions); - g_hash_table_destroy(s->subscription_hashtable); + avahi_free(s->host_name); + s->host_name = host_name ? avahi_normalize_name_strdup(host_name) : avahi_get_host_name_strdup(); + s->host_name[strcspn(s->host_name, ".")] = 0; + update_fqdn(s); - g_hash_table_destroy(s->entries_by_key); + register_stuff(s); + return AVAHI_OK; +} - avahi_time_event_queue_free(s->time_event_queue); +int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name) { + assert(s); + assert(domain_name); - if (s->fd_ipv4 >= 0) - close(s->fd_ipv4); - if (s->fd_ipv6 >= 0) - close(s->fd_ipv6); - - g_free(s->hostname); + if (domain_name && !avahi_is_valid_domain_name(domain_name)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); - g_source_destroy(s->source); - g_source_unref(s->source); - g_main_context_unref(s->context); + withdraw_host_rrs(s); - if (s->unicast_packet) - avahi_dns_packet_free(s->unicast_packet); + avahi_free(s->domain_name); + s->domain_name = domain_name ? avahi_normalize_name_strdup(domain_name) : avahi_strdup("local"); + update_fqdn(s); - g_free(s); + register_stuff(s); + return AVAHI_OK; } -void avahi_server_add( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - AvahiRecord *r) { - - AvahiEntry *e, *t; - g_assert(s); - g_assert(r); - - g_assert(r->key->type != AVAHI_DNS_TYPE_ANY); - - e = g_new(AvahiEntry, 1); - e->server = s; - e->record = avahi_record_ref(r); - e->group = g; - e->interface = interface; - e->protocol = protocol; - e->flags = flags; - e->dead = FALSE; +static int valid_server_config(const AvahiServerConfig *sc) { - AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements); + if (sc->host_name && !avahi_is_valid_host_name(sc->host_name)) + return AVAHI_ERR_INVALID_HOST_NAME; + + if (sc->domain_name && !avahi_is_valid_domain_name(sc->domain_name)) + return AVAHI_ERR_INVALID_DOMAIN_NAME; - AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e); + return AVAHI_OK; +} - /* Insert into hash table indexed by name */ - t = g_hash_table_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); +static int setup_sockets(AvahiServer *s) { + assert(s); + + s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4(s->config.disallow_other_stacks) : -1; + s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6(s->config.disallow_other_stacks) : -1; + + if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) + return AVAHI_ERR_NO_NETWORK; - /* Insert into group list */ - if (g) - AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); + 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"); - avahi_announce_entry(s, e); + s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_unicast_socket_ipv4() : -1; + s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_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; } -const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) { - AvahiEntry **e = (AvahiEntry**) state; - g_assert(s); - g_assert(e); - if (!*e) - *e = g ? g->entries : s->entries; +AvahiServer *avahi_server_new(const AvahiPoll *poll_api, const AvahiServerConfig *sc, AvahiServerCallback callback, void* userdata, int *error) { + AvahiServer *s; + int e; - while (*e && (*e)->dead) - *e = g ? (*e)->by_group_next : (*e)->entries_next; - - if (!*e) + if (sc && (e = valid_server_config(sc)) < 0) { + if (error) + *error = e; return NULL; + } + + if (!(s = avahi_new(AvahiServer, 1))) { + if (error) + *error = AVAHI_ERR_NO_MEMORY; - return avahi_record_ref((*e)->record); -} + return NULL; + } -void avahi_server_dump(AvahiServer *s, FILE *f) { - AvahiEntry *e; - g_assert(s); - g_assert(f); + s->poll_api = poll_api; - fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n"); + if (sc) + avahi_server_config_copy(&s->config, sc); + else + avahi_server_config_init(&s->config); - for (e = s->entries; e; e = e->entries_next) { - gchar *t; + if ((e = setup_sockets(s)) < 0) { + if (error) + *error = e; - if (e->dead) - continue; + avahi_server_config_free(&s->config); + avahi_free(s); - t = avahi_record_to_string(e->record); - fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol); - g_free(t); + return NULL; } - avahi_dump_caches(s->monitor, f); -} + + s->n_host_rr_pending = 0; + s->need_entry_cleanup = 0; + s->need_group_cleanup = 0; + s->need_browser_cleanup = 0; + + 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); + AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups); -void avahi_server_add_ptr( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - const gchar *name, - const gchar *dest) { + s->record_browser_hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL); + AVAHI_LLIST_HEAD_INIT(AvahiSRecordBrowser, s->record_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiSHostNameResolver, s->host_name_resolvers); + AVAHI_LLIST_HEAD_INIT(AvahiSAddressResolver, s->address_resolvers); + AVAHI_LLIST_HEAD_INIT(AvahiSDomainBrowser, s->domain_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiSServiceTypeBrowser, s->service_type_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiSServiceBrowser, s->service_browsers); + AVAHI_LLIST_HEAD_INIT(AvahiSServiceResolver, s->service_resolvers); + AVAHI_LLIST_HEAD_INIT(AvahiSDNSServerBrowser, s->dns_server_browsers); + + s->legacy_unicast_reflect_slots = NULL; + s->legacy_unicast_reflect_id = 0; + + if (s->config.enable_wide_area) { + s->wide_area_lookup_engine = avahi_wide_area_engine_new(s); + avahi_wide_area_set_servers(s->wide_area_lookup_engine, s->config.wide_area_servers, s->config.n_wide_area_servers); + } else + s->wide_area_lookup_engine = NULL; - AvahiRecord *r; + s->multicast_lookup_engine = avahi_multicast_lookup_engine_new(s); - g_assert(dest); + do { + s->local_service_cookie = (uint32_t) rand() * (uint32_t) rand(); + } while (s->local_service_cookie == AVAHI_SERVICE_COOKIE_INVALID); + + /* Get host name */ + s->host_name = s->config.host_name ? avahi_normalize_name_strdup(s->config.host_name) : avahi_get_host_name_strdup(); + s->host_name[strcspn(s->host_name, ".")] = 0; + s->domain_name = s->config.domain_name ? avahi_normalize_name_strdup(s->config.domain_name) : avahi_strdup("local"); + s->host_name_fqdn = NULL; + update_fqdn(s); - r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); - r->data.ptr.name = avahi_normalize_name(dest); - avahi_server_add(s, g, interface, protocol, flags, r); - avahi_record_unref(r); -} + s->record_list = avahi_record_list_new(); -void avahi_server_add_address( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - const gchar *name, - AvahiAddress *a) { + s->state = AVAHI_SERVER_INVALID; - gchar *n = NULL; - g_assert(s); - g_assert(a); + s->monitor = avahi_interface_monitor_new(s); + avahi_interface_monitor_sync(s->monitor); - name = name ? (n = avahi_normalize_name(name)) : s->hostname; - - if (a->family == AF_INET) { - gchar *reverse; - AvahiRecord *r; + register_localhost(s); - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); - r->data.a.address = a->data.ipv4; - avahi_server_add(s, g, interface, protocol, flags, r); - avahi_record_unref(r); - - reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4); - g_assert(reverse); - avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); - g_free(reverse); - - } else { - gchar *reverse; - AvahiRecord *r; - - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA); - r->data.aaaa.address = a->data.ipv6; - avahi_server_add(s, g, interface, protocol, flags, r); - avahi_record_unref(r); + s->hinfo_entry_group = NULL; + s->browse_domain_entry_group = NULL; + register_stuff(s); - reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); - g_assert(reverse); - avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); - g_free(reverse); + s->error = AVAHI_OK; - reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); - g_assert(reverse); - avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); - g_free(reverse); - } - - g_free(n); + return s; } -void avahi_server_add_text_strlst( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - const gchar *name, - AvahiStringList *strlst) { +void avahi_server_free(AvahiServer* s) { + assert(s); + + /* Remove all browsers */ + + while (s->dns_server_browsers) + avahi_s_dns_server_browser_free(s->dns_server_browsers); + while (s->host_name_resolvers) + avahi_s_host_name_resolver_free(s->host_name_resolvers); + while (s->address_resolvers) + avahi_s_address_resolver_free(s->address_resolvers); + while (s->domain_browsers) + avahi_s_domain_browser_free(s->domain_browsers); + while (s->service_type_browsers) + avahi_s_service_type_browser_free(s->service_type_browsers); + while (s->service_browsers) + avahi_s_service_browser_free(s->service_browsers); + while (s->service_resolvers) + avahi_s_service_resolver_free(s->service_resolvers); + while (s->record_browsers) + avahi_s_record_browser_destroy(s->record_browsers); + + /* Remove all locally rgeistered stuff */ + + while(s->entries) + avahi_entry_free(s, s->entries); - AvahiRecord *r; + avahi_interface_monitor_free(s->monitor); + + while (s->groups) + avahi_entry_group_free(s, s->groups); + + free_slots(s); + + avahi_hashmap_free(s->entries_by_key); + avahi_record_list_free(s->record_list); + avahi_hashmap_free(s->record_browser_hashmap); + + if (s->wide_area_lookup_engine) + avahi_wide_area_engine_free(s->wide_area_lookup_engine); + avahi_multicast_lookup_engine_free(s->multicast_lookup_engine); + + avahi_time_event_queue_free(s->time_event_queue); - g_assert(s); + /* Free watches */ - r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); - r->data.txt.string_list = strlst; - avahi_server_add(s, g, interface, protocol, flags, r); - avahi_record_unref(r); -} + 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); -void avahi_server_add_text_va( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - const gchar *name, - va_list va) { + /* Free sockets */ + + if (s->fd_ipv4 >= 0) + close(s->fd_ipv4); + if (s->fd_ipv6 >= 0) + close(s->fd_ipv6); + + if (s->fd_legacy_unicast_ipv4 >= 0) + close(s->fd_legacy_unicast_ipv4); + if (s->fd_legacy_unicast_ipv6 >= 0) + close(s->fd_legacy_unicast_ipv6); - g_assert(s); + /* Free other stuff */ + + avahi_free(s->host_name); + avahi_free(s->domain_name); + avahi_free(s->host_name_fqdn); + + avahi_server_config_free(&s->config); - avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va)); + avahi_free(s); } -void avahi_server_add_text( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - AvahiEntryFlags flags, - const gchar *name, - ...) { +const char* avahi_server_get_domain_name(AvahiServer *s) { + assert(s); - va_list va; - - g_assert(s); + return s->domain_name; +} - va_start(va, name); - avahi_server_add_text_va(s, g, interface, protocol, flags, name, va); - va_end(va); +const char* avahi_server_get_host_name(AvahiServer *s) { + assert(s); + + return s->host_name; } -static void escape_service_name(gchar *d, guint size, const gchar *s) { - g_assert(d); - g_assert(size); - g_assert(s); +const char* avahi_server_get_host_name_fqdn(AvahiServer *s) { + assert(s); - while (*s && size >= 2) { - if (*s == '.' || *s == '\\') { - if (size < 3) - break; + return s->host_name_fqdn; +} - *(d++) = '\\'; - size--; - } - - *(d++) = *(s++); - size--; - } +void* avahi_server_get_data(AvahiServer *s) { + assert(s); - g_assert(size > 0); - *(d++) = 0; + return s->userdata; } -void avahi_server_add_service_strlst( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - const gchar *type, - const gchar *name, - const gchar *domain, - const gchar *host, - guint16 port, - AvahiStringList *strlst) { - - gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256]; - AvahiRecord *r; - - g_assert(s); - g_assert(type); - g_assert(name); +void avahi_server_set_data(AvahiServer *s, void* userdata) { + assert(s); - escape_service_name(ename, sizeof(ename), name); + s->userdata = userdata; +} - if (domain) { - while (domain[0] == '.') - domain++; - } else - domain = "local"; +AvahiServerState avahi_server_get_state(AvahiServer *s) { + assert(s); - if (!host) - host = s->hostname; + return s->state; +} - snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain); - snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain); +AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { + assert(c); + + memset(c, 0, sizeof(AvahiServerConfig)); + c->use_ipv6 = 1; + c->use_ipv4 = 1; + c->host_name = NULL; + c->domain_name = NULL; + 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; + c->add_service_cookie = 1; + c->enable_wide_area = 0; + c->n_wide_area_servers = 0; + c->disallow_other_stacks = 0; - avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name); - - r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); - r->data.srv.priority = 0; - r->data.srv.weight = 0; - r->data.srv.port = port; - r->data.srv.name = avahi_normalize_name(host); - avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); - avahi_record_unref(r); + return c; +} - avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst); +void avahi_server_config_free(AvahiServerConfig *c) { + assert(c); - snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain); - avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name); + avahi_free(c->host_name); + avahi_free(c->domain_name); } -void avahi_server_add_service_va( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - const gchar *type, - const gchar *name, - const gchar *domain, - const gchar *host, - guint16 port, - va_list va){ - - g_assert(s); - g_assert(type); - g_assert(name); - - avahi_server_add_service(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va)); -} +AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *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 = h; + ret->domain_name = d; -void avahi_server_add_service( - AvahiServer *s, - AvahiEntryGroup *g, - gint interface, - guchar protocol, - const gchar *type, - const gchar *name, - const gchar *domain, - const gchar *host, - guint16 port, - ... ){ - - va_list va; - - g_assert(s); - g_assert(type); - g_assert(name); - - va_start(va, port); - avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va); - va_end(va); + return ret; } -static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { - AvahiKey *k = userdata; +int avahi_server_errno(AvahiServer *s) { + assert(s); + + return s->error; +} - g_assert(m); - g_assert(i); - g_assert(k); +/* Just for internal use */ +int avahi_server_set_errno(AvahiServer *s, int error) { + assert(s); - avahi_interface_post_query(i, k, FALSE); + return s->error = error; } -void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) { - g_assert(s); - g_assert(key); +uint32_t avahi_server_get_local_service_cookie(AvahiServer *s) { + assert(s); - avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); + return s->local_service_cookie; } -struct tmpdata { - AvahiRecord *record; - gboolean flush_cache; -}; +static AvahiEntry *find_entry(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) { + AvahiEntry *e; + + assert(s); + assert(key); -static void post_response_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { - struct tmpdata *tmpdata = userdata; + for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next) - g_assert(m); - g_assert(i); - g_assert(tmpdata); + if ((e->interface == interface || e->interface <= 0 || interface <= 0) && + (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC) && + (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED || e->group->state == AVAHI_ENTRY_GROUP_REGISTERING)) - avahi_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE); + return e; + + return NULL; } -void avahi_server_post_response(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *record, gboolean flush_cache) { - struct tmpdata tmpdata; +int avahi_server_get_group_of_service(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiSEntryGroup** ret_group) { + AvahiKey *key = NULL; + AvahiEntry *e; + int ret; + char n[AVAHI_DOMAIN_NAME_MAX]; - g_assert(s); - g_assert(record); + assert(s); + assert(name); + assert(type); + assert(ret_group); - tmpdata.record = record; - tmpdata.flush_cache = flush_cache; + if (!AVAHI_IF_VALID(interface)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE); - avahi_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata); -} + if (!AVAHI_IF_VALID(protocol)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL); + + if (!avahi_is_valid_service_name(name)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME); -void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) { - g_assert(g); + if (!avahi_is_valid_service_type_strict(type)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_TYPE); - g->state = state; - - if (g->callback) { - g->callback(g->server, g, state, g->userdata); - return; - } -} + if (domain && !avahi_is_valid_domain_name(domain)) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); -AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) { - AvahiEntryGroup *g; - - g_assert(s); + if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain) < 0)) + return avahi_server_set_errno(s, ret); + + if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) + return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY); - g = g_new(AvahiEntryGroup, 1); - g->server = s; - g->callback = callback; - g->userdata = userdata; - g->dead = FALSE; - g->state = AVAHI_ENTRY_GROUP_UNCOMMITED; - g->n_probing = 0; - AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries); + e = find_entry(s, interface, protocol, key); + avahi_key_unref(key); - AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g); - return g; + if (e) { + *ret_group = e->group; + return AVAHI_OK; + } + + return avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND); } -void avahi_entry_group_free(AvahiEntryGroup *g) { - g_assert(g); - g_assert(g->server); +int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name) { + AvahiKey *key = NULL; + AvahiEntry *e; - g->dead = TRUE; - g->server->need_group_cleanup = TRUE; -} + assert(s); + assert(name); -void avahi_entry_group_commit(AvahiEntryGroup *g) { - g_assert(g); - g_assert(!g->dead); + if (!s->host_name_fqdn) + return 0; + + if (!(key = avahi_key_new(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) + return 0; - if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED) - return; + e = find_entry(s, interface, protocol, key); + avahi_key_unref(key); - avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); - avahi_announce_group(g->server, g); - avahi_entry_group_check_probed(g, FALSE); + if (!e) + return 0; + + return avahi_domain_equal(s->host_name_fqdn, e->record->data.srv.name); } -gboolean avahi_entry_commited(AvahiEntry *e) { - g_assert(e); - g_assert(!e->dead); +int avahi_server_is_record_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *record) { + AvahiEntry *e; + + assert(s); + assert(record); + + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = e->by_key_next) + + if ((e->interface == interface || e->interface <= 0 || interface <= 0) && + (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC) && + (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED || e->group->state == AVAHI_ENTRY_GROUP_REGISTERING) && + avahi_record_equal_no_ttl(record, e->record)) + return 1; - return !e->group || - e->group->state == AVAHI_ENTRY_GROUP_REGISTERING || - e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED; + return 0; } -AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) { - g_assert(g); - g_assert(!g->dead); +/** Set the wide area DNS servers */ +int avahi_server_set_wide_area_servers(AvahiServer *s, const AvahiAddress *a, unsigned n) { + assert(s); + + if (!s->wide_area_lookup_engine) + return avahi_server_set_errno(s, AVAHI_ERR_INVALID_CONFIG); - return g->state; + avahi_wide_area_set_servers(s->wide_area_lookup_engine, a, n); + return AVAHI_OK; }