#include <errno.h>
#include <stdio.h>
#include <assert.h>
+#include <stdlib.h>
#include <avahi-common/domain.h>
#include <avahi-common/timeval.h>
for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) {
n = e->by_key_next;
- if (e->dead || (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique))
+ if (e->dead)
+ continue;
+
+ /* Check if the incoming is a goodbye record */
+ if (avahi_record_is_goodbye(record)) {
+
+ if (avahi_record_equal_no_ttl(e->record, record)) {
+ char *t;
+
+ /* 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);
+
+ valid = 0;
+ avahi_free(t);
+ break;
+ }
+
+ /* If the goodybe packet doesn't match one of our own RRs, we simply ignore it. */
+ continue;
+ }
+
+ if (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique)
continue;
/* Either our entry or the other is intended to be unique, so let's check */
assert(i);
assert(a);
assert(port > 0);
- assert(i->protocol == a->family);
+ assert(i->protocol == a->proto);
if (!s->config.enable_reflector)
return;
AvahiAddress b;
assert(a);
- avahi_address_parse(a->family == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->family, &b);
+ 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;
}
assert(dest);
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, avahi_af_to_proto(sa->sa_family))) ||
!avahi_interface_relevant(i)) {
avahi_log_warn("Recieved packet from invalid interface.");
return;
assert(sa);
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, avahi_af_to_proto(sa->sa_family))) ||
!avahi_interface_relevant(i)) {
avahi_log_warn("Recieved packet from invalid interface.");
return;
return;
}
- if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.family)) ||
+ if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.proto)) ||
!avahi_interface_relevant(j))
return;
if (events & AVAHI_WATCH_IN) {
if (fd == s->fd_ipv4) {
- dest.family = AVAHI_PROTO_INET;
+ 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.family = AVAHI_PROTO_INET6;
+ 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.family = AVAHI_PROTO_INET;
+ 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, ttl);
avahi_dns_packet_free(p);
}
} else if (fd == s->fd_legacy_unicast_ipv6) {
- dest.family = AVAHI_PROTO_INET6;
+ 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, ttl);
s->legacy_unicast_reflect_slots = NULL;
s->legacy_unicast_reflect_id = 0;
+
+ 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(s->config.host_name) : avahi_get_host_name();
goto fail;
}
- if (a->family == AVAHI_PROTO_INET) {
+ if (a->proto == AVAHI_PROTO_INET) {
char *reverse;
AvahiRecord *r;
char *reverse;
AvahiRecord *r;
- assert(a->family == AVAHI_PROTO_INET6);
+ assert(a->proto == AVAHI_PROTO_INET6);
if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
*(d++) = 0;
}
+static AvahiStringList *add_magic_cookie(
+ AvahiServer *s,
+ AvahiStringList *strlst) {
+
+ assert(s);
+
+ if (!s->config.add_service_cookie)
+ return strlst;
+
+ if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
+ /* This string list already contains a magic cookie */
+ return strlst;
+
+ return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
+}
+
static int server_add_service_strlst_nocopy(
AvahiServer *s,
AvahiSEntryGroup *g,
if (ret < 0)
goto fail;
+ strlst = add_magic_cookie(s, strlst);
+
ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
strlst = NULL;
assert(s);
assert(address);
assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
- assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6);
+ assert(address->proto == AVAHI_PROTO_INET || address->proto == AVAHI_PROTO_INET6);
if (port == 0)
return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
if (domain && !avahi_is_valid_domain_name(domain))
return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
- if (address->family == AVAHI_PROTO_INET) {
+ if (address->proto == AVAHI_PROTO_INET) {
hexstring(n+3, sizeof(n)-3, &address->data, 4);
r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
r->data.a.address = address->data.ipv4;
c->use_iff_running = 0;
c->enable_reflector = 0;
c->reflect_ipv = 0;
+ c->add_service_cookie = 1;
return c;
}
return s->error = error;
}
+uint32_t avahi_server_get_local_service_cookie(AvahiServer *s) {
+ assert(s);
+
+ return s->local_service_cookie;
+}
+
+int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char*domain) {
+ AvahiKey *key = NULL;
+ char *d, *t;
+ char ename[64], n[256];
+ int ret;
+ AvahiEntry *e;
+
+ assert(s);
+ assert(name);
+ assert(type);
+ assert(domain);
+
+ if (!avahi_is_valid_service_name(name))
+ return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME);
+
+ if (!avahi_is_valid_service_type(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);
+
+ escape_service_name(ename, sizeof(ename), name);
+
+ if (!(d = avahi_normalize_name(domain)) ||
+ !(t = avahi_normalize_name(type))) {
+ ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+ snprintf(n, sizeof(n), "%s.%s.%s", ename, t, d);
+
+ if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) {
+ ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
+ goto fail;
+ }
+
+ ret = 0;
+
+ for (e = avahi_hashmap_lookup(s->entries_by_key, 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)) {
+ ret = 1;
+ break;
+ }
+ }
+
+ avahi_key_unref(key);
+
+ avahi_free(d);
+ avahi_free(t);
+
+ return ret;
+
+fail:
+ if (d)
+ avahi_free(d);
+
+ if (t)
+ avahi_free(t);
+
+ if (key)
+ avahi_key_unref(key);
+
+ return ret;
+}