X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fserver.c;h=5bfbc5096502c090700e93a6f493bf466833cf44;hb=451be30b300b390b46738af3fd24fc942ba3158f;hp=fe5b45ffb175f3b6d9bd4c8f049588783d46b387;hpb=2a4c962c16dd14529bbc3352507fb8625d6e34c6;p=catta diff --git a/avahi-core/server.c b/avahi-core/server.c index fe5b45f..5bfbc50 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -23,7 +23,9 @@ #include #endif +#include #include +#include #include #include #include @@ -45,26 +47,39 @@ #include "log.h" #include "util.h" #include "dns-srv-rr.h" +#include "addr-util.h" +#include "domain-util.h" +#include "rr-util.h" 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; - assert(s); assert(i); assert(name); assert(callback); - assert(type != AVAHI_DNS_TYPE_ANY); - - if (!(k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type))) - return; /** OOM */ - - 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); + if (type == AVAHI_DNS_TYPE_ANY) { + AvahiEntry *e; + + for (e = s->entries; e; e = e->entries_next) + if (!e->dead && + avahi_entry_is_registered(s, e, i) && + e->record->key->clazz == AVAHI_DNS_CLASS_IN && + avahi_domain_equal(name, e->record->key->name)) + callback(s, e->record, e->flags & AVAHI_PUBLISH_UNIQUE, userdata); - avahi_key_unref(k); + } else { + AvahiEntry *e; + AvahiKey *k; + + if (!(k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type))) + return; /** OOM */ + + 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); + + avahi_key_unref(k); + } } 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) { @@ -72,6 +87,8 @@ void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, Avahi assert(i); assert(r); assert(callback); + + /* Call the specified callback far all records referenced by the one specified in *r */ if (r->key->clazz == AVAHI_DNS_CLASS_IN) { if (r->key->type == AVAHI_DNS_TYPE_PTR) { @@ -80,7 +97,8 @@ void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, Avahi } 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); - } + } else if (r->key->type == AVAHI_DNS_TYPE_CNAME) + enum_aux_records(s, i, r->data.cname.name, AVAHI_DNS_TYPE_ANY, callback, userdata); } } @@ -93,17 +111,14 @@ void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry } 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); */ + /* Push all records that match the specified key to the record list */ if (avahi_key_is_pattern(k)) { + AvahiEntry *e; /* Handle ANY query */ @@ -112,6 +127,7 @@ void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, avahi_server_prepare_response(s, i, e, unicast_response, 0); } else { + AvahiEntry *e; /* Handle all other queries */ @@ -119,21 +135,40 @@ void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, if (!e->dead && avahi_entry_is_registered(s, e, i)) avahi_server_prepare_response(s, i, e, unicast_response, 0); } + + /* Look for CNAME records */ + + if ((k->clazz == AVAHI_DNS_CLASS_IN || k->clazz == AVAHI_DNS_CLASS_ANY) + && k->type != AVAHI_DNS_TYPE_CNAME && k->type != AVAHI_DNS_TYPE_ANY) { + + AvahiKey *cname_key; + + if (!(cname_key = avahi_key_new(k->name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_CNAME))) + return; + + avahi_server_prepare_matching_responses(s, i, cname_key, unicast_response); + avahi_key_unref(cname_key); + } } static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { assert(s); assert(e); + + /* Withdraw the specified entry, and if is part of an entry group, + * put that into COLLISION state */ + + if (e->dead) + return; if (e->group) { AvahiEntry *k; - for (k = e->group->entries; k; k = k->by_group_next) { + for (k = e->group->entries; k; k = k->by_group_next) if (!k->dead) { avahi_goodbye_entry(s, k, 0, 1); k->dead = 1; } - } e->group->n_probing = 0; @@ -152,24 +187,22 @@ static void withdraw_rrset(AvahiServer *s, AvahiKey *key) { 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); + /* Withdraw an entry RRSset */ + + for (e = avahi_hashmap_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; - 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()"); */ - + /* Handle incoming probes and check if they conflict our own probes */ + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) { int cmp; n = e->by_key_next; @@ -192,20 +225,20 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface * } if (!ours) { - + char *t = avahi_record_to_string(record); + 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); } - - avahi_free(t); } -static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, int unique, const AvahiAddress *a) { +static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, int unique) { int valid = 1, ours = 0, conflict = 0, withdraw_immediately = 0; AvahiEntry *e, *n, *conflicting_entry = NULL; @@ -213,9 +246,8 @@ static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *recor assert(i); assert(record); - -/* avahi_log_debug("CHECKING FOR CONFLICT: [%s]", t); */ - + /* Check whether an incoming record conflicts with one of our own */ + for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) { n = e->by_key_next; @@ -286,8 +318,6 @@ static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *recor } } -/* avahi_log_debug("ours=%i conflict=%i", ours, conflict); */ - if (!ours && conflict) { char *t; @@ -374,15 +404,25 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP int tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC); while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) { - - if (!avahi_interface_post_response(i, r, flush_cache, a, immediately || (flush_cache && !tc && !auxiliary)) && unicast_response) { - append_aux_records_to_list(s, i, r, unicast_response); - + int im = immediately; + + /* Only send the response immediately if it contains a + * unique entry AND it is not in reply to a truncated + * packet AND it is not an auxiliary record AND all other + * responses for this record are unique too. */ + + if (flush_cache && !tc && !auxiliary && avahi_record_list_all_flush_cache(s->record_list)) + im = 1; + + if (!avahi_interface_post_response(i, r, flush_cache, a, im) && 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 */ + append_aux_records_to_list(s, i, r, unicast_response); + for (;;) { if (!reply) { @@ -409,8 +449,8 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP 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 (size > AVAHI_DNS_PACKET_SIZE_MAX) + size = AVAHI_DNS_PACKET_SIZE_MAX; if (!(reply = avahi_dns_packet_new_reply(p, size, 0, 1))) break; /* OOM */ @@ -446,7 +486,6 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP avahi_record_list_flush(s->record_list); } - static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int flush_cache) { AvahiInterface *j; @@ -487,7 +526,7 @@ static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) { for (j = s->monitor->interfaces; j; j = j->interface_next) if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) { /* Post the query to other networks */ - avahi_interface_post_query(j, k, 1); + avahi_interface_post_query(j, k, 1, NULL); /* Reply from caches of other network. This is needed to * "work around" known answer suppression. */ @@ -520,8 +559,6 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac assert(i); assert(a); -/* 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; @@ -532,7 +569,7 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac int unicast_response = 0; if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) { - avahi_log_warn("Packet too short (1)"); + avahi_log_warn(__FILE__": Packet too short or invalid while reading question key. (Maybe an UTF8 problem?)"); goto fail; } @@ -559,11 +596,11 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac int unique = 0; if (!(record = avahi_dns_packet_consume_record(p, &unique))) { - avahi_log_warn("Packet too short (2)"); + avahi_log_warn(__FILE__": Packet too short or invalid while reading known answer record. (Maybe an UTF8 problem?)"); goto fail; } - if (handle_conflict(s, i, record, unique, a)) { + if (handle_conflict(s, i, record, unique)) { 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); @@ -578,7 +615,7 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac int unique = 0; if (!(record = avahi_dns_packet_consume_record(p, &unique))) { - avahi_log_warn("Packet too short (3)"); + avahi_log_warn(__FILE__": Packet too short or invalid while reading probe record. (Maybe an UTF8 problem?)"); goto fail; } @@ -609,8 +646,6 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter 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; @@ -618,16 +653,13 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter /* char *txt; */ if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { - avahi_log_warn("Packet too short (4)"); + avahi_log_warn(__FILE__": Packet too short or invalid while reading response record. (Maybe an UTF8 problem?)"); break; } 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 (handle_conflict(s, i, record, cache_flush)) { if (!from_local_iface) reflect_response(s, i, record, cache_flush); avahi_cache_update(i->cache, record, cache_flush, a); @@ -652,10 +684,10 @@ static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) { assert(s); if (!s->legacy_unicast_reflect_slots) - s->legacy_unicast_reflect_slots = avahi_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS); + s->legacy_unicast_reflect_slots = avahi_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX); - 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; + for (n = 0; n < AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX; n++, s->legacy_unicast_reflect_id++) { + idx = s->legacy_unicast_reflect_id % AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX; if (!s->legacy_unicast_reflect_slots[idx]) break; @@ -680,7 +712,7 @@ static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) assert(s); assert(slot); - idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; + idx = slot->id % AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX; assert(s->legacy_unicast_reflect_slots[idx] == slot); @@ -697,7 +729,7 @@ static void free_slots(AvahiServer *s) { if (!s->legacy_unicast_reflect_slots) return; - for (idx = 0; idx < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; idx ++) + for (idx = 0; idx < AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX; idx ++) if (s->legacy_unicast_reflect_slots[idx]) deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]); @@ -713,7 +745,7 @@ static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, uint16_t id) { if (!s->legacy_unicast_reflect_slots) return NULL; - idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; + idx = id % AVAHI_LEGACY_UNICAST_REFLECT_SLOTS_MAX; if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id) return NULL; @@ -745,8 +777,6 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket * 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 @@ -769,7 +799,7 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket * 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 */ + /* Patch the packet with our new locally generatet id */ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id); for (j = s->monitor->interfaces; j; j = j->interface_next) @@ -778,47 +808,45 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket * (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); + avahi_send_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, j->hardware->index, p, NULL, 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, NULL, 0); } /* Reset the id */ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id); } -static int originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) { - AvahiAddress a; +static int originates_from_local_legacy_unicast_socket(AvahiServer *s, const AvahiAddress *address, uint16_t port) { assert(s); - assert(sa); + assert(address); + assert(port > 0); if (!s->config.enable_reflector) return 0; - avahi_address_from_sockaddr(sa, &a); - - if (!avahi_address_is_local(s->monitor, &a)) + if (!avahi_address_is_local(s->monitor, address)) return 0; - if (sa->sa_family == AF_INET && s->fd_legacy_unicast_ipv4 >= 0) { + if (address->proto == AVAHI_PROTO_INET && s->fd_legacy_unicast_ipv4 >= 0) { struct sockaddr_in lsa; socklen_t l = sizeof(lsa); 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; + return lsa.sin_port == port; } - if (sa->sa_family == AF_INET6 && s->fd_legacy_unicast_ipv6 >= 0) { + if (address->proto == AVAHI_PROTO_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 lsa.sin6_port == port; } return 0; @@ -844,40 +872,34 @@ static int originates_from_local_iface(AvahiServer *s, AvahiIfIndex iface, const return avahi_interface_has_address(s->monitor, iface, a); } -static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, int ttl) { +static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const AvahiAddress *src_address, uint16_t port, const AvahiAddress *dst_address, AvahiIfIndex iface, int ttl) { AvahiInterface *i; - AvahiAddress a; - uint16_t port; int from_local_iface = 0; assert(s); assert(p); - assert(sa); - assert(dest); + assert(src_address); + assert(dst_address); assert(iface > 0); + assert(src_address->proto == dst_address->proto); - if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) || + if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, src_address->proto)) || !avahi_interface_is_relevant(i)) { avahi_log_warn("Recieved packet from invalid interface."); return; } -/* 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)) + if (avahi_address_is_ipv4_in_ipv6(src_address)) /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */ return; - if (originates_from_local_legacy_unicast_socket(s, sa)) + if (originates_from_local_legacy_unicast_socket(s, src_address, port)) /* This originates from our local reflector, so let's ignore it */ return; /* 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); + from_local_iface = originates_from_local_iface(s, iface, src_address, port); if (avahi_dns_packet_check_valid_multicast(p) < 0) { avahi_log_warn("Recieved invalid packet."); @@ -905,11 +927,10 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock } if (legacy_unicast) - reflect_legacy_unicast_query_packet(s, p, i, &a, port); + reflect_legacy_unicast_query_packet(s, p, i, src_address, port); - handle_query_packet(s, p, i, &a, port, legacy_unicast, from_local_iface); + handle_query_packet(s, p, i, src_address, port, legacy_unicast, from_local_iface); -/* avahi_log_debug("Handled query"); */ } else { if (port != AVAHI_MDNS_PORT) { avahi_log_warn("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol); @@ -921,9 +942,9 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock 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); + if (!is_mdns_mcast_address(dst_address) && + !avahi_interface_address_on_link(i, src_address)) { + avahi_log_warn("Received non-local response on interface '%s.%i'.", i->hardware->name, i->protocol); return; } @@ -934,34 +955,16 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sock return; } - handle_response_packet(s, p, i, &a, from_local_iface); -/* avahi_log_debug("Handled response"); */ + handle_response_packet(s, p, i, src_address, from_local_iface); } } -static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface) { - AvahiInterface *i, *j; - AvahiAddress a; +static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p) { + AvahiInterface *j; 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; - } - -/* 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."); @@ -994,52 +997,63 @@ static void cleanup_dead(AvahiServer *s) { avahi_browser_cleanup(s); } -static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) { +static void mcast_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) { AvahiServer *s = userdata; - AvahiAddress dest; - AvahiDnsPacket *p; + AvahiAddress dest, src; + AvahiDnsPacket *p = NULL; AvahiIfIndex iface; + uint16_t port; uint8_t ttl; - struct sockaddr_in sa; - struct sockaddr_in6 sa6; assert(w); assert(fd >= 0); + assert(events & AVAHI_WATCH_IN); + + if (fd == s->fd_ipv4) { + dest.proto = src.proto = AVAHI_PROTO_INET; + p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &src.data.ipv4, &port, &dest.data.ipv4, &iface, &ttl); + } else { + assert(fd == s->fd_ipv6); + dest.proto = src.proto = AVAHI_PROTO_INET6; + p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &src.data.ipv6, &port, &dest.data.ipv6, &iface, &ttl); + } + + if (p) { + if (iface == AVAHI_IF_UNSPEC) + iface = avahi_find_interface_for_address(s->monitor, &dest); + + if (iface != AVAHI_IF_UNSPEC) + dispatch_packet(s, p, &src, port, &dest, iface, ttl); + else + avahi_log_error("Incoming packet recieved on address that isn't local."); + + avahi_dns_packet_free(p); + + cleanup_dead(s); + } +} + +static void legacy_unicast_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) { + AvahiServer *s = userdata; + AvahiDnsPacket *p = NULL; - if (events & AVAHI_WATCH_IN) { + assert(w); + assert(fd >= 0); + assert(events & AVAHI_WATCH_IN); - 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 (fd == s->fd_legacy_unicast_ipv4) + p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, NULL, NULL, NULL, NULL, NULL); + else { + assert(fd == s->fd_legacy_unicast_ipv6); + p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, NULL, NULL, NULL, NULL, NULL); + } - 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); - } - } + if (p) { + dispatch_legacy_unicast_packet(s, p); + avahi_dns_packet_free(p); cleanup_dead(s); - } else - abort(); + } } static void server_set_state(AvahiServer *s, AvahiServerState state) { @@ -1050,6 +1064,8 @@ static void server_set_state(AvahiServer *s, AvahiServerState state) { s->state = state; + avahi_interface_monitor_update_rrs(s->monitor, 0); + if (s->callback) s->callback(s, state, s->userdata); } @@ -1076,7 +1092,7 @@ void avahi_server_decrease_host_rr_pending(AvahiServer *s) { server_set_state(s, AVAHI_SERVER_RUNNING); } -void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) { +void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { assert(s); assert(g); @@ -1115,13 +1131,20 @@ static void register_hinfo(AvahiServer *s) { /* Fill in HINFO rr */ if ((r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME))) { - uname(&utsname); - r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine)); - r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname)); - if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UNIQUE, r) < 0) { - avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error)); - return; + if (uname(&utsname) < 0) + avahi_log_warn("uname() failed: %s\n", avahi_strerror(errno)); + else { + + r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine)); + r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname)); + + avahi_log_info("Registering HINFO record with values '%s'/'%s'.", r->data.hinfo.cpu, r->data.hinfo.os); + + 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); @@ -1150,6 +1173,9 @@ static void register_browse_domain(AvahiServer *s) { if (!s->config.publish_domain) return; + if (avahi_domain_equal(s->domain_name, "local")) + return; + if (s->browse_domain_entry_group) assert(avahi_s_entry_group_is_empty(s->browse_domain_entry_group)); else @@ -1200,17 +1226,27 @@ static void update_fqdn(AvahiServer *s) { } int avahi_server_set_host_name(AvahiServer *s, const char *host_name) { + char *hn = NULL; assert(s); - assert(host_name); + + AVAHI_CHECK_VALIDITY(s, !host_name || avahi_is_valid_host_name(host_name), AVAHI_ERR_INVALID_HOST_NAME); - if (host_name && !avahi_is_valid_host_name(host_name)) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME); + if (!host_name) { + hn = avahi_get_host_name_strdup(); + hn[strcspn(hn, ".")] = 0; + host_name = hn; + } + if (avahi_domain_equal(s->host_name, host_name) && s->state != AVAHI_SERVER_COLLISION) { + avahi_free(hn); + return avahi_server_set_errno(s, AVAHI_ERR_NO_CHANGE); + } + withdraw_host_rrs(s); 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; + s->host_name = hn ? hn : avahi_strdup(host_name); + update_fqdn(s); register_stuff(s); @@ -1218,19 +1254,30 @@ int avahi_server_set_host_name(AvahiServer *s, const char *host_name) { } int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name) { + char *dn = NULL; assert(s); - assert(domain_name); - if (domain_name && !avahi_is_valid_domain_name(domain_name)) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); + AVAHI_CHECK_VALIDITY(s, !domain_name || avahi_is_valid_domain_name(domain_name), AVAHI_ERR_INVALID_DOMAIN_NAME); + + if (!domain_name) { + dn = avahi_strdup("local"); + domain_name = dn; + } + + if (avahi_domain_equal(s->domain_name, domain_name)) { + avahi_free(dn); + return avahi_server_set_errno(s, AVAHI_ERR_NO_CHANGE); + } withdraw_host_rrs(s); avahi_free(s->domain_name); - s->domain_name = domain_name ? avahi_normalize_name_strdup(domain_name) : avahi_strdup("local"); + s->domain_name = avahi_normalize_name_strdup(domain_name); update_fqdn(s); register_stuff(s); + + avahi_free(dn); return AVAHI_OK; } @@ -1268,14 +1315,14 @@ static int setup_sockets(AvahiServer *s) { 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); + s->watch_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_ipv4, AVAHI_WATCH_IN, mcast_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); + s->watch_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_ipv6, AVAHI_WATCH_IN, mcast_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); + s->watch_legacy_unicast_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv4, AVAHI_WATCH_IN, legacy_unicast_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); + s->watch_legacy_unicast_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv6, AVAHI_WATCH_IN, legacy_unicast_socket_event, s); return 0; } @@ -1510,6 +1557,9 @@ AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { c->enable_wide_area = 0; c->n_wide_area_servers = 0; c->disallow_other_stacks = 0; + c->browse_domains = NULL; + c->disable_publishing = 0; + c->allow_point_to_point = 0; return c; } @@ -1519,10 +1569,12 @@ void avahi_server_config_free(AvahiServerConfig *c) { avahi_free(c->host_name); avahi_free(c->domain_name); + avahi_string_list_free(c->browse_domains); } AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) { char *d = NULL, *h = NULL; + AvahiStringList *l = NULL; assert(ret); assert(c); @@ -1535,10 +1587,17 @@ AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiS avahi_free(h); return NULL; } + + if (!(l = avahi_string_list_copy(c->browse_domains)) && c->browse_domains) { + avahi_free(h); + avahi_free(d); + return NULL; + } *ret = *c; ret->host_name = h; ret->domain_name = d; + ret->browse_domains = l; return ret; } @@ -1590,20 +1649,11 @@ int avahi_server_get_group_of_service(AvahiServer *s, AvahiIfIndex interface, Av assert(type); assert(ret_group); - if (!AVAHI_IF_VALID(interface)) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE); - - 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); - - if (!avahi_is_valid_service_type_strict(type)) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_TYPE); - - if (domain && !avahi_is_valid_domain_name(domain)) - return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME); + AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE); + AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL); + AVAHI_CHECK_VALIDITY(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME); + AVAHI_CHECK_VALIDITY(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE); + AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME); if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain) < 0)) return avahi_server_set_errno(s, ret);