X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fserver.c;h=1511ac4f2932ca0df75393b29a3298f117d142a8;hb=a29887070855153ac64a3503e2f0004c2056f8e8;hp=3f37e1f3d764b680fcee527119fcfea1a6c1f8f0;hpb=602a2b6481587b7da2594db39151ec9380f276df;p=catta diff --git a/avahi-core/server.c b/avahi-core/server.c index 3f37e1f..1511ac4 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -35,6 +35,8 @@ #include "socket.h" #include "subscribe.h" +#define AVAHI_HOST_RR_HOLDOFF_MSEC 1000 + static void free_entry(AvahiServer*s, AvahiEntry *e) { AvahiEntry *t; @@ -102,103 +104,69 @@ static void cleanup_dead(AvahiServer *s) { } } -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); - - 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_dns_packet_free(s->unicast_packet); - s->unicast_packet = NULL; -} +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) { + AvahiKey *k; + AvahiEntry *e; -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 (legacy_unicast) { - - /* 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_dns_packet_append_record(s->unicast_packet, r, FALSE, 10)) - - /* 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); - - /* If there's no space left for this response we simply don't send it */ - - } else { - - if (!avahi_interface_post_response(i, a, r, flush_cache, FALSE) && 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 */ - - for (;;) { - - if (!(s->unicast_packet)) - s->unicast_packet = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE); - - if (avahi_dns_packet_append_record(s->unicast_packet, r, flush_cache, 0)) { + g_assert(i); + g_assert(name); + g_assert(callback); - /* 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); + g_assert(type != AVAHI_DNS_TYPE_ANY); - break; - } + k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type); - 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; - } + 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)) + callback(s, e->record, e->flags & AVAHI_ENTRY_UNIQUE, userdata); - /* Appending the record didn't succeeed, so let's send this packet, and create a new one */ + avahi_key_unref(k); +} - send_unicast_response_packet(s, i, a, port); - - avahi_dns_packet_free(s->unicast_packet); - s->unicast_packet = NULL; - } - +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); + + if (r->key->class == 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); } } } -static void handle_query_key(AvahiServer *s, AvahiDnsPacket *p, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) { +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); + + 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) { AvahiEntry *e; - gchar *txt; +/* gchar *txt; */ g_assert(s); - g_assert(k); g_assert(i); - g_assert(a); - - g_message("Handling query: %s", txt = avahi_key_to_string(k)); - g_free(txt); + g_assert(k); - avahi_packet_scheduler_incoming_query(i->scheduler, k); +/* g_message("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */ +/* g_free(txt); */ - if (k->type == AVAHI_DNS_TYPE_ANY) { + 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)) - post_response(s, p, i, a, port, e->record, e->flags & AVAHI_ENTRY_UNIQUE, legacy_unicast, unicast_response); + avahi_server_prepare_response(s, i, e, unicast_response, FALSE); } else { @@ -206,14 +174,13 @@ static void handle_query_key(AvahiServer *s, AvahiDnsPacket *p, AvahiKey *k, Ava 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); + avahi_server_prepare_response(s, i, e, unicast_response, FALSE); } } static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { g_assert(s); g_assert(e); - if (e->group) { AvahiEntry *k; @@ -232,37 +199,260 @@ static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { s->need_entry_cleanup = TRUE; } +static void withdraw_rrset(AvahiServer *s, AvahiKey *key) { + AvahiEntry *e; + + g_assert(s); + g_assert(key); + + for (e = g_hash_table_lookup(s->entries_by_key, key); e; e = e->by_key_next) + 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; g_assert(s); g_assert(record); g_assert(i); + for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { + gint cmp; + n = e->by_key_next; + + if (e->dead || !avahi_entry_probing(s, e, i)) + continue; + + if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) { + ours = TRUE; + break; + } else if (cmp > 0) + won = TRUE; + else /* cmp < 0 */ + lost = TRUE; + } + t = avahi_record_to_string(record); -/* g_message("PROBE: [%s]", t); */ + if (!ours) { + + if (won) + g_message("Recieved conflicting probe [%s]. Local host won.", t); + else if (lost) { + g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t); + withdraw_rrset(s, record->key); + } + } + + g_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; + AvahiEntry *e, *n, *conflicting_entry = NULL; + gchar *t; + g_assert(s); + g_assert(i); + g_assert(record); + + t = avahi_record_to_string(record); + +/* g_message("CHECKING FOR CONFLICT: [%s]", t); */ + for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { n = e->by_key_next; - if (e->dead || avahi_record_equal_no_ttl(record, e->record)) + if (e->dead || (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique)) continue; - if (avahi_entry_registering(s, e, i)) { - gint cmp; + /* Either our entry or the other is intended to be unique, so let's check */ + + if (avahi_entry_registered(s, e, i)) { - 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); + if (avahi_record_equal_no_ttl(e->record, record)) { + ours = TRUE; /* We have an identical record, so this is no conflict */ + + /* Check wheter there is a TTL conflict */ + if (record->ttl <= e->record->ttl/2) { + /* Refresh */ + g_message("Recieved record with bad TTL [%s]. Refreshing.", t); + avahi_interface_post_response(i, e->record, FALSE, NULL, TRUE); + valid = FALSE; + } + + /* There's no need to check the other entries of this RRset */ + break; + } else { + /* A conflict => we have to return to probe mode */ + conflict = TRUE; + conflicting_entry = e; + } + + } else if (avahi_entry_probing(s, e, i)) { + if (!avahi_record_equal_no_ttl(record, e->record)) { + + /* We are currently registering a matching record, but + * someone else already claimed it, so let's + * withdraw */ + conflict = TRUE; + withdraw_immediately = TRUE; + } + } + } + +/* g_message("ours=%i conflict=%i", ours, conflict); */ + + if (!ours && conflict) { + valid = FALSE; + + if (withdraw_immediately) { + g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t); + withdraw_rrset(s, record->key); + } else { + g_assert(conflicting_entry); + g_message("Recieved conflicting record [%s]. Resetting our record.", t); + avahi_entry_return_to_initial_state(s, conflicting_entry, i); + + /* Local unique records are returned to probin + * state. Local shared records are reannounced. */ } } g_free(t); + + return valid; +} + +static void append_aux_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) { + gboolean *unicast_response = userdata; + + g_assert(s); + g_assert(r); + g_assert(unicast_response); + + avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, TRUE); +} + +static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean unicast_response) { + g_assert(s); + g_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) { + + g_assert(s); + g_assert(i); + g_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); + + while ((r = avahi_record_list_next(s->record_list, NULL, NULL, NULL))) { + + append_aux_records_to_list(s, i, r, FALSE); + + if (avahi_dns_packet_append_record(reply, r, FALSE, 10)) + avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT); + else { + gchar *t = avahi_record_to_string(r); + g_warning("Record [%s] not fitting in legacy unicast packet, dropping.", t); + g_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 { + gboolean 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); + + while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) { + + if (!avahi_interface_post_response(i, r, flush_cache, a, !tc && flush_cache && !auxiliary) && unicast_response) { + + 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 */ + + for (;;) { + + if (!reply) { + g_assert(p); + reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE); + } + + 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) { + guint 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; + reply = avahi_dns_packet_new_reply(p, size, FALSE, TRUE); + + if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) { + avahi_dns_packet_free(reply); + + gchar *t = avahi_record_to_string(r); + g_warning("Record [%s] too large, doesn't fit in any packet!", t); + g_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_record_list_flush(s->record_list); } static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) { @@ -273,8 +463,10 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c g_assert(i); g_assert(a); - g_assert(!s->unicast_packet); +/* g_message("query"); */ + g_assert(avahi_record_list_empty(s->record_list)); + /* Handle the questions */ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) { AvahiKey *key; @@ -282,10 +474,11 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) { g_warning("Packet too short (1)"); - return; + goto fail; } - handle_query_key(s, p, key, i, a, port, legacy_unicast, unicast_response); + avahi_query_scheduler_incoming(i->query_scheduler, key); + avahi_server_prepare_matching_responses(s, i, key, unicast_response); avahi_key_unref(key); } @@ -296,10 +489,14 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c if (!(record = avahi_dns_packet_consume_record(p, &unique))) { g_warning("Packet too short (2)"); - return; + goto fail; } - avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a); + 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_record_unref(record); } @@ -310,7 +507,7 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c if (!(record = avahi_dns_packet_consume_record(p, &unique))) { g_warning("Packet too short (3)"); - return; + goto fail; } if (record->key->type != AVAHI_DNS_TYPE_ANY) @@ -319,75 +516,14 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c avahi_record_unref(record); } - if (s->unicast_packet) - send_unicast_response_packet(s, i, a, port); -} + if (!avahi_record_list_empty(s->record_list)) + avahi_server_generate_response(s, i, p, a, port, legacy_unicast); -static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) { - gboolean valid = TRUE; - AvahiEntry *e, *n; - gchar *t; + return; - g_assert(s); - g_assert(i); - g_assert(record); - - t = avahi_record_to_string(record); - -/* g_message("CHECKING FOR CONFLICT: [%s]", t); */ - - for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { - n = e->by_key_next; - - if (e->dead) - continue; - - if (avahi_entry_registered(s, e, i)) { - - 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 */ - - g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t); - - 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 (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) { - - /* 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); - } - } - } - - g_free(t); +fail: + avahi_record_list_flush(s->record_list); - return valid; } static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) { @@ -397,26 +533,28 @@ static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i g_assert(p); g_assert(i); g_assert(a); + +/* g_message("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; +/* gchar *txt; */ if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { g_warning("Packet too short (4)"); - return; + break; } if (record->key->type != AVAHI_DNS_TYPE_ANY) { - g_message("Handling response: %s", txt = avahi_record_to_string(record)); - g_free(txt); +/* 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_response_scheduler_incoming(i->response_scheduler, record, cache_flush); } } @@ -434,12 +572,13 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * g_assert(sa); g_assert(iface > 0); - if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) { + if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) || + !avahi_interface_relevant(i)) { g_warning("Recieved packet from invalid interface."); return; } - g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); +/* 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[] = { @@ -464,8 +603,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * if (avahi_dns_packet_is_query(p)) { gboolean legacy_unicast = FALSE; - if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) == 0 || - avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { g_warning("Invalid query packet."); return; } @@ -484,7 +622,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * handle_query(s, p, i, &a, port, legacy_unicast); - g_message("Handled query"); +/* g_message("Handled query"); */ } else { if (port != AVAHI_MDNS_PORT) { @@ -494,7 +632,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * 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) + if (s->config.check_response_ttl) return; } @@ -506,7 +644,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * } handle_response(s, p, i, &a); - g_message("Handled response"); +/* g_message("Handled response"); */ } } @@ -514,7 +652,7 @@ static void work(AvahiServer *s) { struct sockaddr_in6 sa6; struct sockaddr_in sa; AvahiDnsPacket *p; - gint iface = -1; + gint iface = 0; guint8 ttl; g_assert(s); @@ -565,31 +703,190 @@ static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer us return TRUE; } -static void add_default_entries(AvahiServer *s) { +static void server_set_state(AvahiServer *s, AvahiServerState state) { + g_assert(s); + + if (s->state == state) + return; + + s->state = state; + + if (s->callback) + s->callback(s, state, s->userdata); +} + +static void withdraw_host_rrs(AvahiServer *s) { + g_assert(s); + + if (s->hinfo_entry_group) { + avahi_entry_group_free(s->hinfo_entry_group); + s->hinfo_entry_group = NULL; + } + + if (s->browse_domain_entry_group) { + avahi_entry_group_free(s->browse_domain_entry_group); + s->browse_domain_entry_group = NULL; + } + + avahi_update_host_rrs(s->monitor, TRUE); + s->n_host_rr_pending = 0; +} + +void avahi_server_decrease_host_rr_pending(AvahiServer *s) { + g_assert(s); + + g_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); + + 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); + + if (state == AVAHI_ENTRY_GROUP_REGISTERING && + s->state == AVAHI_SERVER_REGISTERING) + avahi_server_increase_host_rr_pending(s); + 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 register_hinfo(AvahiServer *s) { struct utsname utsname; - AvahiAddress a; AvahiRecord *r; g_assert(s); + if (!s->config.register_hinfo || s->hinfo_entry_group) + return; + + s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL); + /* Fill in HINFO rr */ - r = avahi_record_new_full(s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); + r = avahi_record_new_full(s->host_name_fqdn, 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_server_add(s, s->hinfo_entry_group, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r); avahi_record_unref(r); + avahi_entry_group_commit(s->hinfo_entry_group); +} + +static void register_localhost(AvahiServer *s) { + AvahiAddress a; + g_assert(s); + /* 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); + avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a); 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); + avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a); } -AvahiServer *avahi_server_new(GMainContext *c) { - gchar *hn; +static void register_browse_domain(AvahiServer *s) { + g_assert(s); + + if (!s->config.announce_domain || s->browse_domain_entry_group) + return; + + s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL); + avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "_browse._dns-sd._udp.local", s->domain_name); + avahi_entry_group_commit(s->browse_domain_entry_group); +} + +static void register_stuff(AvahiServer *s) { + g_assert(s); + + server_set_state(s, AVAHI_SERVER_REGISTERING); + register_hinfo(s); + register_browse_domain(s); + avahi_update_host_rrs(s->monitor, FALSE); + + if (s->n_host_rr_pending == 0) + server_set_state(s, AVAHI_SERVER_RUNNING); +} + +static void update_fqdn(AvahiServer *s) { + g_assert(s); + + g_assert(s->host_name); + g_assert(s->domain_name); + + g_free(s->host_name_fqdn); + s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain_name); +} + +static void register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) { + AvahiServer *s = userdata; + + g_assert(e); + g_assert(s); + + g_assert(e == s->register_time_event); + avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event); + s->register_time_event = NULL; + + if (s->state == AVAHI_SERVER_SLEEPING) + register_stuff(s); +} + +static void delayed_register_stuff(AvahiServer *s) { + GTimeVal tv; + + g_assert(s); + + avahi_elapse_time(&tv, AVAHI_HOST_RR_HOLDOFF_MSEC, 0); + + if (s->register_time_event) + avahi_time_event_queue_update(s->time_event_queue, s->register_time_event, &tv); + else + s->register_time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, register_time_event_callback, s); +} + +void avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) { + g_assert(s); + g_assert(host_name); + + server_set_state(s, AVAHI_SERVER_SLEEPING); + withdraw_host_rrs(s); + + g_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); + + delayed_register_stuff(s); +} + +void avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) { + g_assert(s); + g_assert(domain_name); + + server_set_state(s, AVAHI_SERVER_SLEEPING); + withdraw_host_rrs(s); + + g_free(s->domain_name); + s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local."); + update_fqdn(s); + + delayed_register_stuff(s); +} + +AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, AvahiServerCallback callback, gpointer userdata) { AvahiServer *s; static GSourceFuncs source_funcs = { @@ -602,50 +899,34 @@ AvahiServer *avahi_server_new(GMainContext *c) { }; s = g_new(AvahiServer, 1); - - s->ignore_bad_ttl = FALSE; + s->n_host_rr_pending = 0; s->need_entry_cleanup = s->need_group_cleanup = FALSE; + + if (sc) + avahi_server_config_copy(&s->config, sc); + else + avahi_server_config_init(&s->config); - s->fd_ipv4 = avahi_open_socket_ipv4(); - s->fd_ipv6 = avahi_open_socket_ipv6(); + 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) { - g_critical("Failed to create IP sockets.\n"); + g_critical("Selected neither IPv6 nor IPv4 support, aborting.\n"); + avahi_server_config_free(&s->config); g_free(s); return NULL; } - if (s->fd_ipv4 < 0) + if (s->fd_ipv4 < 0 && s->config.use_ipv4) g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode"); - else if (s->fd_ipv6 < 0) + else if (s->fd_ipv6 < 0 && s->config.use_ipv6) g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode"); if (c) g_main_context_ref(s->context = c); else s->context = g_main_context_default(); - - 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); - AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions); - s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); - - /* Get host name */ - hn = avahi_get_host_name(); - hn[strcspn(hn, ".")] = 0; - - s->hostname = g_strdup_printf("%s.local.", hn); - g_free(hn); - - s->unicast_packet = NULL; - - 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); - /* Prepare IO source registration */ s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*)); *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s; @@ -661,7 +942,41 @@ AvahiServer *avahi_server_new(GMainContext *c) { g_source_add_poll(s->source, &s->pollfd_ipv6); g_source_attach(s->source, s->context); + + s->callback = callback; + s->userdata = userdata; + + 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); + AVAHI_LLIST_HEAD_INIT(AvahiRecordResolver, s->record_resolvers); + s->record_resolver_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); + AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers); + + /* 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->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->register_time_event = NULL; + + s->state = AVAHI_SERVER_INVALID; + + s->monitor = avahi_interface_monitor_new(s); + avahi_interface_monitor_sync(s->monitor); + + register_localhost(s); + + s->hinfo_entry_group = NULL; + s->browse_domain_entry_group = NULL; + register_stuff(s); + return s; } @@ -676,27 +991,35 @@ void avahi_server_free(AvahiServer* s) { while (s->groups) free_group(s, s->groups); - while (s->subscriptions) - avahi_subscription_free(s->subscriptions); - g_hash_table_destroy(s->subscription_hashtable); + while (s->host_name_resolvers) + avahi_host_name_resolver_free(s->host_name_resolvers); + + while (s->record_resolvers) + avahi_record_resolver_free(s->record_resolvers); + g_hash_table_destroy(s->record_resolver_hashtable); g_hash_table_destroy(s->entries_by_key); + if (s->register_time_event) + avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event); avahi_time_event_queue_free(s->time_event_queue); + avahi_record_list_free(s->record_list); + if (s->fd_ipv4 >= 0) close(s->fd_ipv4); if (s->fd_ipv6 >= 0) close(s->fd_ipv6); - - g_free(s->hostname); + + 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); - if (s->unicast_packet) - avahi_dns_packet_free(s->unicast_packet); + avahi_server_config_free(&s->config); g_free(s); } @@ -790,7 +1113,7 @@ void avahi_server_add_ptr( g_assert(dest); - r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); + r = avahi_record_new_full(name ? name : s->host_name_fqdn, 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); @@ -809,7 +1132,7 @@ void avahi_server_add_address( g_assert(s); g_assert(a); - name = name ? (n = avahi_normalize_name(name)) : s->hostname; + name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn; if (a->family == AF_INET) { gchar *reverse; @@ -817,12 +1140,11 @@ void avahi_server_add_address( 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_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, 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); + avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); } else { @@ -831,17 +1153,15 @@ void avahi_server_add_address( 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_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r); avahi_record_unref(r); reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); - g_assert(reverse); - avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); + avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); - g_assert(reverse); - avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); + avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); } @@ -861,7 +1181,7 @@ void avahi_server_add_text_strlst( g_assert(s); - r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); + r = avahi_record_new_full(name ? name : s->host_name_fqdn, 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); @@ -946,10 +1266,10 @@ void avahi_server_add_service_strlst( while (domain[0] == '.') domain++; } else - domain = "local"; + domain = s->domain_name; if (!host) - host = s->hostname; + host = s->host_name_fqdn; snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain); snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain); @@ -986,7 +1306,7 @@ void avahi_server_add_service_va( 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)); + avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va)); } void avahi_server_add_service( @@ -1029,36 +1349,12 @@ void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, Av avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); } -struct tmpdata { - AvahiRecord *record; - gboolean flush_cache; -}; - -static void post_response_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { - struct tmpdata *tmpdata = userdata; - - g_assert(m); - g_assert(i); - g_assert(tmpdata); - - avahi_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE); -} - -void avahi_server_post_response(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *record, gboolean flush_cache) { - struct tmpdata tmpdata; - - g_assert(s); - g_assert(record); - - tmpdata.record = record; - tmpdata.flush_cache = flush_cache; - - avahi_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata); -} - void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) { g_assert(g); + if (g->state == state) + return; + g->state = state; if (g->callback) { @@ -1086,11 +1382,20 @@ 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); + for (e = g->entries; e; e = e->by_group_next) { + avahi_goodbye_entry(g->server, e, TRUE); + e->dead = TRUE; + } + g->dead = TRUE; + g->server->need_group_cleanup = TRUE; + g->server->need_entry_cleanup = TRUE; } void avahi_entry_group_commit(AvahiEntryGroup *g) { @@ -1120,3 +1425,87 @@ AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) { return g->state; } + +void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata) { + g_assert(g); + + g->userdata = userdata; +} + +gpointer avahi_entry_group_get_data(AvahiEntryGroup *g) { + g_assert(g); + + return g->userdata; +} + +const gchar* avahi_server_get_domain_name(AvahiServer *s) { + g_assert(s); + + return s->domain_name; +} + +const gchar* avahi_server_get_host_name(AvahiServer *s) { + g_assert(s); + + return s->host_name; +} + +const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s) { + g_assert(s); + + return s->host_name_fqdn; +} + +gpointer avahi_server_get_data(AvahiServer *s) { + g_assert(s); + + return s->userdata; +} + +void avahi_server_set_data(AvahiServer *s, gpointer userdata) { + g_assert(s); + + s->userdata = userdata; +} + +AvahiServerState avhai_server_get_state(AvahiServer *s) { + g_assert(s); + + return s->state; +} + +AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { + g_assert(c); + + memset(c, 0, sizeof(AvahiServerConfig)); + c->register_hinfo = TRUE; + c->register_addresses = TRUE; + c->use_ipv6 = TRUE; + c->use_ipv4 = TRUE; + c->host_name = NULL; + c->domain_name = NULL; + c->check_response_ttl = TRUE; + c->announce_domain = TRUE; + c->use_iff_running = FALSE; + + return c; +} + +void avahi_server_config_free(AvahiServerConfig *c) { + g_assert(c); + + g_free(c->host_name); + g_free(c->domain_name); +} + +AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) { + g_assert(ret); + g_assert(c); + + *ret = *c; + + ret->host_name = g_strdup(c->host_name); + ret->domain_name = g_strdup(c->domain_name); + + return ret; +}