#include <errno.h>
#include <stdio.h>
#include <assert.h>
+#include <stdlib.h>
#include <avahi-common/domain.h>
#include <avahi-common/timeval.h>
#include <avahi-common/malloc.h>
+#include <avahi-common/error.h>
-#include "server.h"
+#include "internal.h"
#include "iface.h"
#include "socket.h"
#include "browse.h"
#include "log.h"
#include "util.h"
-
-#define AVAHI_RR_HOLDOFF_MSEC 1000
-#define AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT 60000
-#define AVAHI_RR_RATE_LIMIT_COUNT 15
-
-static void free_entry(AvahiServer*s, AvahiEntry *e) {
- AvahiEntry *t;
-
- assert(s);
- assert(e);
-
- avahi_goodbye_entry(s, e, 1);
-
- /* Remove from linked list */
- AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
-
- /* Remove from hash table indexed by name */
- t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
- AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
- if (t)
- avahi_hashmap_replace(s->entries_by_key, t->record->key, t);
- else
- avahi_hashmap_remove(s->entries_by_key, e->record->key);
-
- /* Remove from associated group */
- if (e->group)
- AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
-
- avahi_record_unref(e->record);
- avahi_free(e);
-}
-
-static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
- assert(s);
- assert(g);
-
- while (g->entries)
- free_entry(s, g->entries);
-
- if (g->register_time_event)
- avahi_time_event_free(g->register_time_event);
-
- AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
- avahi_free(g);
-}
-
-static void cleanup_dead(AvahiServer *s) {
- assert(s);
-
- if (s->need_group_cleanup) {
- AvahiEntryGroup *g, *next;
-
- for (g = s->groups; g; g = next) {
- next = g->groups_next;
-
- if (g->dead)
- free_group(s, g);
- }
-
- s->need_group_cleanup = 0;
- }
-
- if (s->need_entry_cleanup) {
- AvahiEntry *e, *next;
-
- for (e = s->entries; e; e = next) {
- next = e->entries_next;
-
- if (e->dead)
- free_entry(s, e);
- }
-
- s->need_entry_cleanup = 0;
- }
-
- if (s->need_browser_cleanup)
- avahi_browser_cleanup(s);
-}
+#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_ENTRY_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) {
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) {
} 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);
}
}
assert(i);
assert(e);
- avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_ENTRY_UNIQUE, unicast_response, auxiliary);
+ avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_PUBLISH_UNIQUE, unicast_response, auxiliary);
}
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 */
avahi_server_prepare_response(s, i, e, unicast_response, 0);
} else {
+ AvahiEntry *e;
/* Handle all other queries */
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);
+ avahi_goodbye_entry(s, k, 0, 1);
k->dead = 1;
}
- }
e->group->n_probing = 0;
- avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
+ avahi_s_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
} else {
- avahi_goodbye_entry(s, e, 0);
+ avahi_goodbye_entry(s, e, 0, 1);
e->dead = 1;
}
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;
}
}
-
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) {
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;
- 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_PUBLISH_UNIQUE) && !unique)
continue;
/* Either our entry or the other is intended to be unique, so let's check */
}
}
-/* avahi_log_debug("ours=%i conflict=%i", ours, conflict); */
-
if (!ours && conflict) {
char *t;
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 */
avahi_interface_post_probe(j, r, 1);
}
-static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port, int legacy_unicast) {
+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;
goto fail;
}
- if (!legacy_unicast)
+ if (!legacy_unicast && !from_local_iface) {
reflect_query(s, i, key);
+ avahi_cache_start_poof(i->cache, key, a);
+ }
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))
avahi_key_unref(key);
}
- /* 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 (!legacy_unicast) {
- 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);
+ /* 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);
}
- 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)) {
- reflect_probe(s, i, record);
- incoming_probe(s, record, i);
+ /* 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_record_unref(record);
}
if (!avahi_record_list_is_empty(s->record_list))
avahi_record_list_flush(s->record_list);
}
-static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
+static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, int from_local_iface) {
unsigned n;
assert(s);
/* avahi_free(txt); */
if (handle_conflict(s, i, record, cache_flush, a)) {
- reflect_response(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);
avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush);
}
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;
if (!(slot = avahi_new(AvahiLegacyUnicastReflectSlot, 1)))
return NULL; /* OOM */
- slot = s->legacy_unicast_reflect_slots[idx];
+ s->legacy_unicast_reflect_slots[idx] = slot;
slot->id = s->legacy_unicast_reflect_id++;
slot->server = s;
+
return 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);
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]);
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;
assert(i);
assert(a);
assert(port > 0);
- assert(i->protocol == a->family);
+ assert(i->protocol == a->proto);
if (!s->config.enable_reflector)
return;
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_relevant(j) &&
+ if (avahi_interface_is_relevant(j) &&
j != i &&
(s->config.reflect_ipv || j->protocol == i->protocol)) {
struct sockaddr_in lsa;
socklen_t l = sizeof(lsa);
- if (getsockname(s->fd_legacy_unicast_ipv4, &lsa, &l) != 0)
+ 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;
struct sockaddr_in6 lsa;
socklen_t l = sizeof(lsa);
- if (getsockname(s->fd_legacy_unicast_ipv6, &lsa, &l) != 0)
+ 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;
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;
}
+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, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, int ttl) {
AvahiInterface *i;
AvahiAddress a;
uint16_t port;
+ int from_local_iface = 0;
assert(s);
assert(p);
assert(dest);
assert(iface > 0);
- if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
- !avahi_interface_relevant(i)) {
+ 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 packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
+/* avahi_log_debug("new packet received on interface '%s.%i'.", i->hardware->name, i->protocol); */
port = avahi_port_from_sockaddr(sa);
avahi_address_from_sockaddr(sa, &a);
/* This originates from our local reflector, so let's ignore it */
return;
- if (avahi_dns_packet_is_valid(p) < 0) {
+ /* 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;
}
if (legacy_unicast)
reflect_legacy_unicast_query_packet(s, p, i, &a, port);
- handle_query_packet(s, p, i, &a, port, legacy_unicast);
+ handle_query_packet(s, p, i, &a, port, legacy_unicast, from_local_iface);
/* avahi_log_debug("Handled query"); */
} else {
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);
+ avahi_log_warn("Received non-local response on interface '%s.%i'.", i->hardware->name, i->protocol);
return;
}
return;
}
- handle_response_packet(s, p, i, &a);
+ handle_response_packet(s, p, i, &a, from_local_iface);
/* avahi_log_debug("Handled response"); */
}
}
-static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface, int ttl) {
+static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface) {
AvahiInterface *i, *j;
AvahiAddress a;
- uint16_t port;
AvahiLegacyUnicastReflectSlot *slot;
assert(s);
assert(sa);
assert(iface > 0);
- if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
- !avahi_interface_relevant(i)) {
+ 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_log_debug("new legacy unicast packet received 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 (avahi_dns_packet_is_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
+ if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
avahi_log_warn("Recieved invalid packet.");
return;
}
return;
}
- if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.family)) ||
- !avahi_interface_relevant(j))
+ 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->id);
}
+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;
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);
+ dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface);
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);
+ dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface);
avahi_dns_packet_free(p);
}
}
cleanup_dead(s);
- } else {
- assert(0);
- }
+ } else
+ abort();
}
static void server_set_state(AvahiServer *s, AvahiServerState state) {
assert(s);
if (s->hinfo_entry_group)
- avahi_entry_group_reset(s->hinfo_entry_group);
+ avahi_s_entry_group_reset(s->hinfo_entry_group);
if (s->browse_domain_entry_group)
- avahi_entry_group_reset(s->browse_domain_entry_group);
+ avahi_s_entry_group_reset(s->browse_domain_entry_group);
- avahi_update_host_rrs(s->monitor, 1);
+ avahi_interface_monitor_update_rrs(s->monitor, 1);
s->n_host_rr_pending = 0;
}
server_set_state(s, AVAHI_SERVER_RUNNING);
}
-void avahi_server_increase_host_rr_pending(AvahiServer *s) {
- assert(s);
-
- s->n_host_rr_pending ++;
-}
-
-void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
assert(s);
assert(g);
if (state == AVAHI_ENTRY_GROUP_REGISTERING &&
s->state == AVAHI_SERVER_REGISTERING)
- avahi_server_increase_host_rr_pending(s);
+ s->n_host_rr_pending ++;
else if (state == AVAHI_ENTRY_GROUP_COLLISION &&
(s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) {
return;
if (s->hinfo_entry_group)
- assert(avahi_entry_group_is_empty(s->hinfo_entry_group));
+ assert(avahi_s_entry_group_is_empty(s->hinfo_entry_group));
else
- s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
+ 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));
r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine));
r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname));
- if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_UNIQUE, r) < 0) {
+ 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_entry_group_commit(s->hinfo_entry_group) < 0)
+ 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));
}
/* 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_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
+ avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_NO_ANNOUNCE, "localhost", &a);
avahi_address_parse("::1", AVAHI_PROTO_INET6, &a);
- avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
+ avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_NO_ANNOUNCE, "ip6-localhost", &a);
}
static void register_browse_domain(AvahiServer *s) {
return;
if (s->browse_domain_entry_group)
- assert(avahi_entry_group_is_empty(s->browse_domain_entry_group));
+ assert(avahi_s_entry_group_is_empty(s->browse_domain_entry_group));
else
- s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
+ 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;
}
- if (avahi_entry_group_commit(s->browse_domain_entry_group) < 0)
+ 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));
}
assert(s);
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 */
+
register_hinfo(s);
register_browse_domain(s);
- avahi_update_host_rrs(s->monitor, 0);
+ avahi_interface_monitor_update_rrs(s->monitor, 0);
+ s->n_host_rr_pending --;
+
if (s->n_host_rr_pending == 0)
server_set_state(s, AVAHI_SERVER_RUNNING);
}
withdraw_host_rrs(s);
avahi_free(s->host_name);
- s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_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);
withdraw_host_rrs(s);
avahi_free(s->domain_name);
- s->domain_name = domain_name ? avahi_normalize_name(domain_name) : avahi_strdup("local");
+ s->domain_name = domain_name ? avahi_normalize_name_strdup(domain_name) : avahi_strdup("local");
update_fqdn(s);
register_stuff(s);
static int setup_sockets(AvahiServer *s) {
assert(s);
- s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1;
- s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1;
+ 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;
else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode");
- s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1;
- s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1;
-
- s->watch_ipv4 = s->watch_ipv6 = s->watch_legacy_unicast_ipv4 = s->watch_legacy_unicast_ipv6 = NULL;
+ 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;
}
-AvahiServer *avahi_server_new(AvahiPoll *poll_api, const AvahiServerConfig *sc, AvahiServerCallback callback, void* userdata, int *error) {
+AvahiServer *avahi_server_new(const AvahiPoll *poll_api, const AvahiServerConfig *sc, AvahiServerCallback callback, void* userdata, int *error) {
AvahiServer *s;
int e;
- if ((e = valid_server_config(sc)) < 0) {
+ if (sc && (e = valid_server_config(sc)) < 0) {
if (error)
*error = e;
return NULL;
return NULL;
}
-
+
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->hinfo_entry_group = NULL;
+ s->browse_domain_entry_group = NULL;
+ s->error = AVAHI_OK;
+ s->state = AVAHI_SERVER_INVALID;
+
s->callback = callback;
s->userdata = userdata;
-
+
+ s->time_event_queue = avahi_time_event_queue_new(poll_api);
+
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);
s->record_browser_hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
- AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, s->record_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers);
- AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers);
- AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, s->service_resolvers);
- AVAHI_LLIST_HEAD_INIT(AvahiDNSServerBrowser, s->dns_server_browsers);
+ 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;
+
+ s->record_list = avahi_record_list_new();
/* Get host name */
- s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_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(s->config.domain_name) : avahi_strdup("local");
+ 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);
- s->record_list = avahi_record_list_new();
+ do {
+ s->local_service_cookie = (uint32_t) rand() * (uint32_t) rand();
+ } while (s->local_service_cookie == AVAHI_SERVICE_COOKIE_INVALID);
- s->state = AVAHI_SERVER_INVALID;
+ 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;
+ s->multicast_lookup_engine = avahi_multicast_lookup_engine_new(s);
+
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);
-
- s->error = AVAHI_OK;
return s;
}
void avahi_server_free(AvahiServer* s) {
assert(s);
- while(s->entries)
- free_entry(s, s->entries);
-
- avahi_interface_monitor_free(s->monitor);
-
- while (s->groups)
- free_group(s, s->groups);
-
- free_slots(s);
+ /* Remove all browsers */
while (s->dns_server_browsers)
- avahi_dns_server_browser_free(s->dns_server_browsers);
+ avahi_s_dns_server_browser_free(s->dns_server_browsers);
while (s->host_name_resolvers)
- avahi_host_name_resolver_free(s->host_name_resolvers);
+ avahi_s_host_name_resolver_free(s->host_name_resolvers);
while (s->address_resolvers)
- avahi_address_resolver_free(s->address_resolvers);
+ avahi_s_address_resolver_free(s->address_resolvers);
while (s->domain_browsers)
- avahi_domain_browser_free(s->domain_browsers);
+ avahi_s_domain_browser_free(s->domain_browsers);
while (s->service_type_browsers)
- avahi_service_type_browser_free(s->service_type_browsers);
+ avahi_s_service_type_browser_free(s->service_type_browsers);
while (s->service_browsers)
- avahi_service_browser_free(s->service_browsers);
+ avahi_s_service_browser_free(s->service_browsers);
while (s->service_resolvers)
- avahi_service_resolver_free(s->service_resolvers);
+ avahi_s_service_resolver_free(s->service_resolvers);
while (s->record_browsers)
- avahi_record_browser_destroy(s->record_browsers);
+ avahi_s_record_browser_destroy(s->record_browsers);
- avahi_hashmap_free(s->record_browser_hashmap);
- avahi_hashmap_free(s->entries_by_key);
+ /* Remove all locally rgeistered stuff */
- avahi_time_event_queue_free(s->time_event_queue);
+ while(s->entries)
+ avahi_entry_free(s, s->entries);
+
+ 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);
+
+ /* Free watches */
+
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);
+
+ /* 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);
-
+
+ /* Free other stuff */
+
avahi_free(s->host_name);
avahi_free(s->domain_name);
avahi_free(s->host_name_fqdn);
avahi_free(s);
}
-static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) {
- AvahiEntry *e;
-
+const char* avahi_server_get_domain_name(AvahiServer *s) {
assert(s);
- assert(r);
-
- for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
- if (e->dead)
- continue;
-
- if (!(flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_UNIQUE))
- continue;
-
- if ((flags & AVAHI_ENTRY_ALLOWMUTIPLE) && (e->flags & AVAHI_ENTRY_ALLOWMUTIPLE) )
- continue;
-
- if (interface <= 0 ||
- e->interface <= 0 ||
- e->interface == interface ||
- protocol == AVAHI_PROTO_UNSPEC ||
- e->protocol == AVAHI_PROTO_UNSPEC ||
- e->protocol == protocol)
-
- return -1;
-
- }
- return 0;
+ return s->domain_name;
}
-int avahi_server_add(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- AvahiRecord *r) {
-
- AvahiEntry *e, *t;
-
+const char* avahi_server_get_host_name(AvahiServer *s) {
assert(s);
- assert(r);
-
- if (r->ttl == 0)
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_TTL);
-
- if (avahi_key_is_pattern(r->key))
- return avahi_server_set_errno(s, AVAHI_ERR_IS_PATTERN);
-
- if (!avahi_record_is_valid(r))
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_RECORD);
-
- if (check_record_conflict(s, interface, protocol, r, flags) < 0)
- return avahi_server_set_errno(s, AVAHI_ERR_LOCAL_COLLISION);
-
- if (!(e = avahi_new(AvahiEntry, 1)))
- return avahi_server_set_errno(s, AVAHI_ERR_NO_NETWORK);
-
- e->server = s;
- e->record = avahi_record_ref(r);
- e->group = g;
- e->interface = interface;
- e->protocol = protocol;
- e->flags = flags;
- e->dead = 0;
-
- AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
- AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
+ return s->host_name;
+}
- /* Insert into hash table indexed by name */
- t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
- AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
- avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
+const char* avahi_server_get_host_name_fqdn(AvahiServer *s) {
+ assert(s);
- /* Insert into group list */
- if (g)
- AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
+ return s->host_name_fqdn;
+}
- avahi_announce_entry(s, e);
+void* avahi_server_get_data(AvahiServer *s) {
+ assert(s);
- return 0;
+ return s->userdata;
}
-const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
- AvahiEntry **e = (AvahiEntry**) state;
+void avahi_server_set_data(AvahiServer *s, void* userdata) {
assert(s);
- assert(e);
-
- if (!*e)
- *e = g ? g->entries : s->entries;
-
- while (*e && (*e)->dead)
- *e = g ? (*e)->by_group_next : (*e)->entries_next;
-
- if (!*e)
- return NULL;
- return avahi_record_ref((*e)->record);
+ s->userdata = userdata;
}
-int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
- AvahiEntry *e;
-
+AvahiServerState avahi_server_get_state(AvahiServer *s) {
assert(s);
- assert(callback);
-
- callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
- for (e = s->entries; e; e = e->entries_next) {
- char *t;
- char ln[256];
-
- if (e->dead)
- continue;
-
- if (!(t = avahi_record_to_string(e->record)))
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
-
- snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
- avahi_free(t);
-
- callback(ln, userdata);
- }
-
- avahi_dump_caches(s->monitor, callback, userdata);
- return AVAHI_OK;
-}
-
-int avahi_server_add_ptr(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- uint32_t ttl,
- const char *name,
- const char *dest) {
-
- AvahiRecord *r;
- int ret;
-
- assert(s);
- assert(dest);
-
- if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl)))
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
-
- r->data.ptr.name = avahi_normalize_name(dest);
- ret = avahi_server_add(s, g, interface, protocol, flags, r);
- avahi_record_unref(r);
- return ret;
+ return s->state;
}
-int avahi_server_add_address(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- const char *name,
- AvahiAddress *a) {
-
- char *n = NULL;
- int ret = AVAHI_OK;
- assert(s);
- assert(a);
-
- if (name) {
- if (!(n = avahi_normalize_name(name)))
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
-
- name = n;
- } else
- name = s->host_name_fqdn;
+AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) {
+ assert(c);
- if (!avahi_is_valid_domain_name(name)) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
- goto fail;
- }
+ 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;
- if (a->family == AVAHI_PROTO_INET) {
- char *reverse;
- AvahiRecord *r;
-
- if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
- }
-
- r->data.a.address = a->data.ipv4;
- ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
- avahi_record_unref(r);
-
- if (ret < 0)
- goto fail;
-
- if (!(reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
- }
-
- ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
- avahi_free(reverse);
+ return c;
+}
- } else {
- char *reverse;
- AvahiRecord *r;
+void avahi_server_config_free(AvahiServerConfig *c) {
+ assert(c);
- assert(a->family == 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);
- goto fail;
- }
-
- r->data.aaaa.address = a->data.ipv6;
- ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
- avahi_record_unref(r);
+ avahi_free(c->host_name);
+ avahi_free(c->domain_name);
+}
- if (ret < 0)
- goto fail;
+AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) {
+ char *d = NULL, *h = NULL;
+ assert(ret);
+ assert(c);
- if (!(reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
- }
-
- ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
- avahi_free(reverse);
+ if (c->host_name)
+ if (!(h = avahi_strdup(c->host_name)))
+ return NULL;
- if (ret < 0)
- goto fail;
-
- if (!(reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
+ if (c->domain_name)
+ if (!(d = avahi_strdup(c->domain_name))) {
+ avahi_free(h);
+ return NULL;
}
-
- ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
- avahi_free(reverse);
- }
-
-fail:
- avahi_free(n);
+ *ret = *c;
+ ret->host_name = h;
+ ret->domain_name = d;
return ret;
}
-static int server_add_txt_strlst_nocopy(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- uint32_t ttl,
- const char *name,
- AvahiStringList *strlst) {
-
- AvahiRecord *r;
- int ret;
-
+int avahi_server_errno(AvahiServer *s) {
assert(s);
- if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl)))
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
-
- r->data.txt.string_list = strlst;
- ret = avahi_server_add(s, g, interface, protocol, flags, r);
- avahi_record_unref(r);
-
- return ret;
+ return s->error;
}
-int avahi_server_add_txt_strlst(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- uint32_t ttl,
- const char *name,
- AvahiStringList *strlst) {
-
+/* Just for internal use */
+int avahi_server_set_errno(AvahiServer *s, int error) {
assert(s);
- return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_copy(strlst));
+ return s->error = error;
}
-int avahi_server_add_txt_va(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- uint32_t ttl,
- const char *name,
- va_list va) {
-
+uint32_t avahi_server_get_local_service_cookie(AvahiServer *s) {
assert(s);
- return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_new_va(va));
+ return s->local_service_cookie;
}
-int avahi_server_add_txt(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiEntryFlags flags,
- uint32_t ttl,
- const char *name,
- ...) {
-
- va_list va;
- int ret;
+static AvahiEntry *find_entry(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) {
+ AvahiEntry *e;
assert(s);
+ assert(key);
- va_start(va, name);
- ret = avahi_server_add_txt_va(s, g, interface, protocol, flags, ttl, name, va);
- va_end(va);
-
- return ret;
-}
-
-static void escape_service_name(char *d, size_t size, const char *s) {
- assert(d);
- assert(size);
- assert(s);
+ for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next)
- while (*s && size >= 2) {
- if (*s == '.' || *s == '\\') {
- if (size < 3)
- break;
+ 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))
- *(d++) = '\\';
- size--;
- }
-
- *(d++) = *(s++);
- size--;
- }
+ return e;
- assert(size > 0);
- *(d++) = 0;
+ return NULL;
}
-static int server_add_service_strlst_nocopy(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *name,
- const char *type,
- const char *domain,
- const char *host,
- uint16_t port,
- AvahiStringList *strlst) {
-
- char ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
- char *t = NULL, *d = NULL, *h = NULL;
- AvahiRecord *r = NULL;
- int ret = AVAHI_OK;
+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];
assert(s);
- assert(type);
assert(name);
+ 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(type))
+ 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);
- if (host && !avahi_is_valid_domain_name(host))
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
-
- if (port == 0)
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
-
- escape_service_name(ename, sizeof(ename), name);
-
- if (!domain)
- domain = s->domain_name;
-
- if (!host)
- host = s->host_name_fqdn;
-
- if (!(d = avahi_normalize_name(domain)) ||
- !(t = avahi_normalize_name(type)) ||
- !(h = avahi_normalize_name(host))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
- }
-
- snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d);
- snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d);
-
- if ((ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
- goto fail;
-
- if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
- ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- goto fail;
- }
-
- r->data.srv.priority = 0;
- r->data.srv.weight = 0;
- r->data.srv.port = port;
- r->data.srv.name = h;
- h = NULL;
- ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
- avahi_record_unref(r);
-
- if (ret < 0)
- goto fail;
-
- ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
- strlst = NULL;
-
- if (ret < 0)
- goto fail;
-
- snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d);
- ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name);
-
-fail:
-
- avahi_free(d);
- avahi_free(t);
- avahi_free(h);
-
- avahi_string_list_free(strlst);
-
- return ret;
-}
-
-int avahi_server_add_service_strlst(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *name,
- const char *type,
- const char *domain,
- const char *host,
- uint16_t port,
- AvahiStringList *strlst) {
-
- assert(s);
- assert(type);
- assert(name);
-
- return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_copy(strlst));
-}
-
-int avahi_server_add_service_va(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *name,
- const char *type,
- const char *domain,
- const char *host,
- uint16_t port,
- va_list va){
-
- assert(s);
- assert(type);
- assert(name);
-
- return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_new_va(va));
-}
-
-int avahi_server_add_service(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *name,
- const char *type,
- const char *domain,
- const char *host,
- uint16_t port,
- ... ){
-
- va_list va;
- int ret;
-
- assert(s);
- assert(type);
- assert(name);
-
- va_start(va, port);
- ret = avahi_server_add_service_va(s, g, interface, protocol, name, type, domain, host, port, va);
- va_end(va);
- return ret;
-}
-
-static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
- static const char hex[] = "0123456789abcdef";
- int b = 0;
- const uint8_t *k = p;
-
- while (sl > 1 && pl > 0) {
- *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
-
- if (b) {
- k++;
- pl--;
- }
+ if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain) < 0))
+ return avahi_server_set_errno(s, ret);
- b = !b;
-
- sl--;
- }
-
- if (sl > 0)
- *s = 0;
-}
-
-int avahi_server_add_dns_server_address(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *domain,
- AvahiDNSServerType type,
- const AvahiAddress *address,
- uint16_t port /** should be 53 */) {
-
- AvahiRecord *r;
- int ret;
- char n[64] = "ip-";
-
- assert(s);
- assert(address);
- assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
- assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6);
-
- if (domain && !avahi_is_valid_domain_name(domain))
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
-
- if (port == 0)
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
-
- if (address->family == 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;
- } else {
- hexstring(n+3, sizeof(n)-3, &address->data, 6);
- r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
- r->data.aaaa.address = address->data.ipv6;
- }
-
- if (!r)
+ if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV)))
return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
-
- ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
- avahi_record_unref(r);
-
- if (ret < 0)
- return ret;
-
- return avahi_server_add_dns_server_name(s, g, interface, protocol, domain, type, n, port);
-}
-
-int avahi_server_add_dns_server_name(
- AvahiServer *s,
- AvahiEntryGroup *g,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char *domain,
- AvahiDNSServerType type,
- const char *name,
- uint16_t port /** should be 53 */) {
-
- int ret = -1;
- char t[256], *d = NULL, *n = NULL;
- AvahiRecord *r;
-
- assert(s);
- assert(name);
- assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
- if (port == 0)
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
-
- if (!avahi_is_valid_domain_name(name))
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
-
- if (domain && !avahi_is_valid_domain_name(domain))
- return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
+ e = find_entry(s, interface, protocol, key);
+ avahi_key_unref(key);
-
- if (!domain)
- domain = s->domain_name;
-
- if (!(n = avahi_normalize_name(name)) ||
- !(d = avahi_normalize_name(domain))) {
- avahi_free(n);
- avahi_free(d);
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- }
-
- snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d);
- avahi_free(d);
-
- if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
- avahi_free(n);
- return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
+ if (e) {
+ *ret_group = e->group;
+ return AVAHI_OK;
}
-
- r->data.srv.priority = 0;
- r->data.srv.weight = 0;
- r->data.srv.port = port;
- r->data.srv.name = n;
- ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_NULL, r);
- avahi_record_unref(r);
- return ret;
+ return avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
}
-static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
- AvahiKey *k = userdata;
-
- assert(m);
- assert(i);
- assert(k);
-
- avahi_interface_post_query(i, k, 0);
-}
-
-void avahi_server_post_query(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) {
- assert(s);
- assert(key);
-
- avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
-}
-
-void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
- assert(g);
-
- if (g->state == state)
- return;
-
- assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
-
- g->state = state;
-
- if (g->callback)
- g->callback(g->server, g, state, g->userdata);
-}
-
-AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, void* userdata) {
- AvahiEntryGroup *g;
-
- assert(s);
-
- if (!(g = avahi_new(AvahiEntryGroup, 1))) {
- avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
- return NULL;
- }
-
- g->server = s;
- g->callback = callback;
- g->userdata = userdata;
- g->dead = 0;
- g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
- g->n_probing = 0;
- g->n_register_try = 0;
- g->register_time_event = NULL;
- g->register_time.tv_sec = 0;
- g->register_time.tv_usec = 0;
- AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
-
- AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
- return g;
-}
-
-void avahi_entry_group_free(AvahiEntryGroup *g) {
+int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name) {
+ AvahiKey *key = NULL;
AvahiEntry *e;
-
- assert(g);
- assert(g->server);
-
- for (e = g->entries; e; e = e->by_group_next) {
- if (!e->dead) {
- avahi_goodbye_entry(g->server, e, 1);
- e->dead = 1;
- }
- }
-
- if (g->register_time_event) {
- avahi_time_event_free(g->register_time_event);
- g->register_time_event = NULL;
- }
-
- g->dead = 1;
-
- g->server->need_group_cleanup = 1;
- g->server->need_entry_cleanup = 1;
-}
-
-static void entry_group_commit_real(AvahiEntryGroup *g) {
- assert(g);
- gettimeofday(&g->register_time, NULL);
-
- avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
-
- if (!g->dead) {
- avahi_announce_group(g->server, g);
- avahi_entry_group_check_probed(g, 0);
- }
-}
-
-static void entry_group_register_time_event_callback(AvahiTimeEvent *e, void* userdata) {
- AvahiEntryGroup *g = userdata;
- assert(g);
-
-/* avahi_log_debug("Holdoff passed, waking up and going on."); */
-
- avahi_time_event_free(g->register_time_event);
- g->register_time_event = NULL;
-
- /* Holdoff time passed, so let's start probing */
- entry_group_commit_real(g);
-}
+ assert(s);
+ assert(name);
-int avahi_entry_group_commit(AvahiEntryGroup *g) {
- struct timeval now;
+ if (!s->host_name_fqdn)
+ return 0;
- assert(g);
- assert(!g->dead);
-
- if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
- return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
-
- g->n_register_try++;
-
- avahi_timeval_add(&g->register_time,
- 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
- AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
- AVAHI_RR_HOLDOFF_MSEC));
-
- gettimeofday(&now, NULL);
-
- if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
- /* Holdoff time passed, so let's start probing */
-/* avahi_log_debug("Holdoff passed, directly going on."); */
-
- entry_group_commit_real(g);
- } else {
-/* avahi_log_debug("Holdoff not passed, sleeping."); */
-
- /* Holdoff time has not yet passed, so let's wait */
- assert(!g->register_time_event);
- g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
-
- avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
- }
-
- return AVAHI_OK;
-}
+ if (!(key = avahi_key_new(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV)))
+ return 0;
-void avahi_entry_group_reset(AvahiEntryGroup *g) {
- AvahiEntry *e;
- assert(g);
-
- if (g->register_time_event) {
- avahi_time_event_free(g->register_time_event);
- g->register_time_event = NULL;
- }
-
- for (e = g->entries; e; e = e->by_group_next) {
- if (!e->dead) {
- avahi_goodbye_entry(g->server, e, 1);
- e->dead = 1;
- }
- }
+ e = find_entry(s, interface, protocol, key);
+ avahi_key_unref(key);
- if (g->register_time_event) {
- avahi_time_event_free(g->register_time_event);
- g->register_time_event = NULL;
- }
+ if (!e)
+ return 0;
- g->server->need_entry_cleanup = 1;
- g->n_probing = 0;
-
- avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
-}
-
-int avahi_entry_is_commited(AvahiEntry *e) {
- assert(e);
- assert(!e->dead);
-
- return !e->group ||
- e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
- e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
-}
-
-AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {
- assert(g);
- assert(!g->dead);
-
- return g->state;
-}
-
-void avahi_entry_group_set_data(AvahiEntryGroup *g, void* userdata) {
- assert(g);
-
- g->userdata = userdata;
-}
-
-void* avahi_entry_group_get_data(AvahiEntryGroup *g) {
- assert(g);
-
- return g->userdata;
+ return avahi_domain_equal(s->host_name_fqdn, e->record->data.srv.name);
}
-int avahi_entry_group_is_empty(AvahiEntryGroup *g) {
+int avahi_server_is_record_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *record) {
AvahiEntry *e;
- assert(g);
-
- /* Look for an entry that is not dead */
- for (e = g->entries; e; e = e->by_group_next)
- if (!e->dead)
- return 0;
-
- return 1;
-}
-
-const char* avahi_server_get_domain_name(AvahiServer *s) {
- assert(s);
-
- return s->domain_name;
-}
-const char* avahi_server_get_host_name(AvahiServer *s) {
assert(s);
-
- return s->host_name;
-}
-
-const char* avahi_server_get_host_name_fqdn(AvahiServer *s) {
- assert(s);
-
- return s->host_name_fqdn;
-}
-
-void* avahi_server_get_data(AvahiServer *s) {
- assert(s);
-
- return s->userdata;
-}
-
-void avahi_server_set_data(AvahiServer *s, void* userdata) {
- assert(s);
-
- s->userdata = userdata;
-}
-
-AvahiServerState avahi_server_get_state(AvahiServer *s) {
- assert(s);
-
- return s->state;
-}
-
-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;
+ assert(record);
- return c;
-}
-
-void avahi_server_config_free(AvahiServerConfig *c) {
- assert(c);
+ for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = e->by_key_next)
- avahi_free(c->host_name);
- avahi_free(c->domain_name);
-}
+ 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;
-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;
-
- return ret;
+ return 0;
}
-int avahi_server_errno(AvahiServer *s) {
+/** Set the wide area DNS servers */
+int avahi_server_set_wide_area_servers(AvahiServer *s, const AvahiAddress *a, unsigned n) {
assert(s);
-
- return s->error;
-}
-/* Just for internal use */
-int avahi_server_set_errno(AvahiServer *s, int error) {
- assert(s);
+ if (!s->wide_area_lookup_engine)
+ return avahi_server_set_errno(s, AVAHI_ERR_INVALID_CONFIG);
- return s->error = error;
+ avahi_wide_area_set_servers(s->wide_area_lookup_engine, a, n);
+ return AVAHI_OK;
}
-