X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=server.c;h=b17b14293806e1197eae655b582971993fec773a;hb=7dce450bdc23ea306a61e00f914481e29ebcb176;hp=6b3c2dd61c3c899714bba2c154004d59b4b3eea0;hpb=4aa744ffac20c7b5e18cb3b23e5dbac8221c0043;p=catta diff --git a/server.c b/server.c index 6b3c2dd..b17b142 100644 --- a/server.c +++ b/server.c @@ -10,8 +10,75 @@ #include "socket.h" #include "subscribe.h" -static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) { - flxServerEntry *e; +static void free_entry(AvahiServer*s, AvahiEntry *e) { + AvahiEntry *t; + + g_assert(s); + g_assert(e); + + avahi_goodbye_entry(s, e, TRUE); + + /* Remove from linked list */ + AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e); + + /* Remove from hash table indexed by name */ + t = g_hash_table_lookup(s->entries_by_key, e->record->key); + AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e); + if (t) + g_hash_table_replace(s->entries_by_key, t->record->key, t); + else + g_hash_table_remove(s->entries_by_key, e->record->key); + + /* Remove from associated group */ + if (e->group) + AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e); + + avahi_record_unref(e->record); + g_free(e); +} + +static void free_group(AvahiServer *s, AvahiEntryGroup *g) { + g_assert(s); + g_assert(g); + + while (g->entries) + free_entry(s, g->entries); + + AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g); + g_free(g); +} + +static void cleanup_dead(AvahiServer *s) { + AvahiEntryGroup *g, *ng; + AvahiEntry *e, *ne; + g_assert(s); + + + if (s->need_group_cleanup) { + for (g = s->groups; g; g = ng) { + ng = g->groups_next; + + if (g->dead) + free_group(s, g); + } + + s->need_group_cleanup = FALSE; + } + + if (s->need_entry_cleanup) { + for (e = s->entries; e; e = ne) { + ne = e->entries_next; + + if (e->dead) + free_entry(s, e); + } + + s->need_entry_cleanup = FALSE; + } +} + +static void handle_query_key(AvahiServer *s, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) { + AvahiEntry *e; gchar *txt; g_assert(s); @@ -19,17 +86,84 @@ static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flx g_assert(i); g_assert(a); - g_message("Handling query: %s", txt = flx_key_to_string(k)); + g_message("Handling query: %s", txt = avahi_key_to_string(k)); g_free(txt); - flx_packet_scheduler_incoming_query(i->scheduler, k); + avahi_packet_scheduler_incoming_query(i->scheduler, k); + + if (k->type == AVAHI_DNS_TYPE_ANY) { + + /* Handle ANY query */ + + for (e = s->entries; e; e = e->entries_next) + if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i)) + avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE); + } else { + + /* Handle all other queries */ + + for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next) + if (!e->dead && avahi_entry_registered(s, e, i)) + avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE); + } +} + +static void withdraw_entry(AvahiServer *s, AvahiEntry *e) { + g_assert(s); + g_assert(e); + + + if (e->group) { + AvahiEntry *k; + + for (k = e->group->entries; k; k = k->by_group_next) { + avahi_goodbye_entry(s, k, FALSE); + k->dead = TRUE; + } + + avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION); + } else { + avahi_goodbye_entry(s, e, FALSE); + e->dead = TRUE; + } + + s->need_entry_cleanup = TRUE; +} - for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next) - if (flx_interface_match(i, e->interface, e->protocol)) - flx_interface_post_response(i, e->record, FALSE); +static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) { + AvahiEntry *e, *n; + gchar *t; + + g_assert(s); + g_assert(record); + g_assert(i); + + t = avahi_record_to_string(record); + +/* g_message("PROBE: [%s]", t); */ + + for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { + n = e->by_key_next; + + if (e->dead || avahi_record_equal_no_ttl(record, e->record)) + continue; + + if (avahi_entry_registering(s, e, i)) { + gint cmp; + + if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) { + withdraw_entry(s, e); + g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t); + } else if (cmp < 0) + g_message("Recieved conflicting probe [%s]. Local host won.", t); + + } + } + + g_free(t); } -static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) { +static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) { guint n; g_assert(s); @@ -37,20 +171,119 @@ static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const f g_assert(i); g_assert(a); - for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) { - flxKey *key; + /* Handle the questions */ + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) { + AvahiKey *key; + gboolean unicast_response = FALSE; + + if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) { + g_warning("Packet too short (1)"); + return; + } + + handle_query_key(s, key, i, a, port, legacy_unicast, unicast_response); + avahi_key_unref(key); + } + + /* Known Answer Suppression */ + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) { + AvahiRecord *record; + gboolean unique = FALSE; - if (!(key = flx_dns_packet_consume_key(p))) { - g_warning("Packet too short"); + if (!(record = avahi_dns_packet_consume_record(p, &unique))) { + g_warning("Packet too short (2)"); return; } - handle_query_key(s, key, i, a); - flx_key_unref(key); + avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a); + avahi_record_unref(record); + } + + /* Probe record */ + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) { + AvahiRecord *record; + gboolean unique = FALSE; + + if (!(record = avahi_dns_packet_consume_record(p, &unique))) { + g_warning("Packet too short (3)"); + return; + } + + if (record->key->type != AVAHI_DNS_TYPE_ANY) + incoming_probe(s, record, i); + + avahi_record_unref(record); + } +} + +static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) { + gboolean valid = TRUE; + AvahiEntry *e, *n; + gchar *t; + + g_assert(s); + g_assert(i); + g_assert(record); + + t = avahi_record_to_string(record); + +/* g_message("CHECKING FOR CONFLICT: [%s]", t); */ + + for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) { + n = e->by_key_next; + + if (e->dead) + continue; + + if (avahi_entry_registered(s, e, i)) { + + gboolean equal = avahi_record_equal_no_ttl(record, e->record); + + /* Check whether there is a unique record conflict */ + if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) { + gint cmp; + + /* The lexicographically later data wins. */ + if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) { + g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t); + withdraw_entry(s, e); + } else if (cmp < 0) { + /* Tell the other host that our entry is lexicographically later */ + + g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t); + + valid = FALSE; + avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE); + } + + /* Check wheter there is a TTL conflict */ + } else if (equal && record->ttl <= e->record->ttl/2) { + /* Correct the TTL */ + valid = FALSE; + avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE); + g_message("Recieved record with bad TTL [%s]. Refreshing.", t); + } + + } else if (avahi_entry_registering(s, e, i)) { + + if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) { + + /* We are currently registering a matching record, but + * someone else already claimed it, so let's + * withdraw */ + + g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t); + withdraw_entry(s, e); + } + } } + + g_free(t); + + return valid; } -static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) { +static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) { guint n; g_assert(s); @@ -58,83 +291,109 @@ static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, cons g_assert(i); g_assert(a); - for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) + - flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT); n > 0; n--) { - flxRecord *record; + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) + + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) { + AvahiRecord *record; gboolean cache_flush = FALSE; gchar *txt; - if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) { - g_warning("Packet too short"); + if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) { + g_warning("Packet too short (4)"); return; } - g_message("Handling response: %s", txt = flx_record_to_string(record)); - g_free(txt); + if (record->key->type != AVAHI_DNS_TYPE_ANY) { - flx_cache_update(i->cache, record, cache_flush, a); - - flx_packet_scheduler_incoming_response(i->scheduler, record); - flx_record_unref(record); + g_message("Handling response: %s", txt = avahi_record_to_string(record)); + g_free(txt); + + if (handle_conflict(s, i, record, cache_flush, a)) { + avahi_cache_update(i->cache, record, cache_flush, a); + avahi_packet_scheduler_incoming_response(i->scheduler, record); + } + } + + avahi_record_unref(record); } } -static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) { - flxInterface *i; - flxAddress a; +static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) { + AvahiInterface *i; + AvahiAddress a; + guint16 port; g_assert(s); g_assert(p); g_assert(sa); g_assert(iface > 0); - g_message("new packet recieved."); - - if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) { + if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) { g_warning("Recieved packet from invalid interface."); return; } - if (ttl != 255) { - g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol); - return; - } + g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); if (sa->sa_family == AF_INET6) { - static const unsigned char ipv4_in_ipv6[] = { + static const guint8 ipv4_in_ipv6[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }; - if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) { + /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */ - /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */ + if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) return; - } } - if (flx_dns_packet_check_valid(p) < 0) { + if (avahi_dns_packet_check_valid(p) < 0) { g_warning("Recieved invalid packet."); return; } - flx_address_from_sockaddr(sa, &a); + port = avahi_port_from_sockaddr(sa); + avahi_address_from_sockaddr(sa, &a); - if (flx_dns_packet_is_query(p)) { + if (avahi_dns_packet_is_query(p)) { + gboolean legacy_unicast = FALSE; - if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 || - flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 || - flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) { + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) == 0 || + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) { g_warning("Invalid query packet."); return; } - - handle_query(s, p, i, &a); + + if (port != AVAHI_MDNS_PORT) { + /* Legacy Unicast */ + + if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 || + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) { + g_warning("Invalid legacy unicast query packet."); + return; + } + + legacy_unicast = TRUE; + } + + handle_query(s, p, i, &a, port, legacy_unicast); + g_message("Handled query"); } else { - if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 || - flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 || - flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) { + + if (port != AVAHI_MDNS_PORT) { + g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol); + return; + } + + if (ttl != 255) { + g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol); + if (!s->ignore_bad_ttl) + return; + } + + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 || + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 || + avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) { g_warning("Invalid response packet."); return; } @@ -144,30 +403,28 @@ static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, } } -static gboolean work(flxServer *s) { +static void work(AvahiServer *s) { struct sockaddr_in6 sa6; struct sockaddr_in sa; - flxDnsPacket *p; + AvahiDnsPacket *p; gint iface = -1; guint8 ttl; g_assert(s); if (s->pollfd_ipv4.revents & G_IO_IN) { - if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) { + if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) { dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl); - flx_dns_packet_free(p); + avahi_dns_packet_free(p); } } if (s->pollfd_ipv6.revents & G_IO_IN) { - if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) { + if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) { dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl); - flx_dns_packet_free(p); + avahi_dns_packet_free(p); } } - - return TRUE; } static gboolean prepare_func(GSource *source, gint *timeout) { @@ -179,56 +436,56 @@ static gboolean prepare_func(GSource *source, gint *timeout) { } static gboolean check_func(GSource *source) { - flxServer* s; + AvahiServer* s; g_assert(source); - s = *((flxServer**) (((guint8*) source) + sizeof(GSource))); + s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); g_assert(s); return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR); } static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) { - flxServer* s; + AvahiServer* s; g_assert(source); - s = *((flxServer**) (((guint8*) source) + sizeof(GSource))); + s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource))); g_assert(s); - - return work(s); + + work(s); + cleanup_dead(s); + + return TRUE; } -static void add_default_entries(flxServer *s) { +static void add_default_entries(AvahiServer *s) { gint length = 0; struct utsname utsname; gchar *hinfo; - flxAddress a; + AvahiAddress a; + AvahiRecord *r; g_assert(s); /* Fill in HINFO rr */ + r = avahi_record_new_full(s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); uname(&utsname); - hinfo = g_strdup_printf("%c%s%c%s%n", - strlen(utsname.machine), g_strup(utsname.machine), - strlen(utsname.sysname), g_strup(utsname.sysname), - &length); - - flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE, - s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length, FLX_DEFAULT_TTL); - - g_free(hinfo); + r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine)); + r->data.hinfo.os = g_strdup(g_strup(utsname.sysname)); + avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r); + avahi_record_unref(r); /* Add localhost entries */ - flx_address_parse("127.0.0.1", AF_INET, &a); - flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a); + avahi_address_parse("127.0.0.1", AF_INET, &a); + avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a); - flx_address_parse("::1", AF_INET6, &a); - flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a); + avahi_address_parse("::1", AF_INET6, &a); + avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a); } -flxServer *flx_server_new(GMainContext *c) { +AvahiServer *avahi_server_new(GMainContext *c) { gchar *hn, *e; - flxServer *s; + AvahiServer *s; static GSourceFuncs source_funcs = { prepare_func, @@ -239,13 +496,16 @@ flxServer *flx_server_new(GMainContext *c) { NULL }; - s = g_new(flxServer, 1); + s = g_new(AvahiServer, 1); - s->fd_ipv4 = flx_open_socket_ipv4(); - s->fd_ipv6 = flx_open_socket_ipv6(); + s->ignore_bad_ttl = FALSE; + s->need_entry_cleanup = s->need_group_cleanup = FALSE; + + s->fd_ipv4 = avahi_open_socket_ipv4(); + s->fd_ipv6 = avahi_open_socket_ipv6(); if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) { - g_critical("Failed to create sockets.\n"); + g_critical("Failed to create IP sockets.\n"); g_free(s); return NULL; } @@ -260,30 +520,28 @@ flxServer *flx_server_new(GMainContext *c) { else s->context = g_main_context_default(); - s->current_id = 1; - - FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries); - s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal); - s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal); + AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries); + s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); + AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups); - FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions); - s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal); + AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions); + s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); - s->monitor = flx_interface_monitor_new(s); - s->time_event_queue = flx_time_event_queue_new(s->context); - /* Get host name */ - hn = flx_get_host_name(); - if ((e = strchr(hn, '.'))) - *e = 0; + hn = avahi_get_host_name(); + hn[strcspn(hn, ".")] = 0; s->hostname = g_strdup_printf("%s.local.", hn); g_free(hn); + s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */ + s->monitor = avahi_interface_monitor_new(s); + avahi_interface_monitor_sync(s->monitor); add_default_entries(s); - - s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*)); - *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s; + + /* Prepare IO source registration */ + s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*)); + *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s; memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4)); s->pollfd_ipv4.fd = s->fd_ipv4; @@ -296,25 +554,28 @@ flxServer *flx_server_new(GMainContext *c) { g_source_add_poll(s->source, &s->pollfd_ipv6); g_source_attach(s->source, s->context); - + return s; } -void flx_server_free(flxServer* s) { +void avahi_server_free(AvahiServer* s) { g_assert(s); - flx_interface_monitor_free(s->monitor); - - flx_server_remove(s, 0); + while(s->entries) + free_entry(s, s->entries); + + avahi_interface_monitor_free(s->monitor); + + while (s->groups) + free_group(s, s->groups); while (s->subscriptions) - flx_subscription_free(s->subscriptions); + avahi_subscription_free(s->subscriptions); g_hash_table_destroy(s->subscription_hashtable); - - g_hash_table_destroy(s->rrset_by_id); - g_hash_table_destroy(s->rrset_by_key); - flx_time_event_queue_free(s->time_event_queue); + g_hash_table_destroy(s->entries_by_key); + + avahi_time_event_queue_free(s->time_event_queue); if (s->fd_ipv4 >= 0) close(s->fd_ipv4); @@ -330,248 +591,424 @@ void flx_server_free(flxServer* s) { g_free(s); } -gint flx_server_get_next_id(flxServer *s) { - g_assert(s); - - return s->current_id++; -} - -void flx_server_add( - flxServer *s, - gint id, +void avahi_server_add( + AvahiServer *s, + AvahiEntryGroup *g, gint interface, guchar protocol, - gboolean unique, - flxRecord *r) { + AvahiEntryFlags flags, + AvahiRecord *r) { - flxServerEntry *e, *t; + AvahiEntry *e, *t; g_assert(s); g_assert(r); - e = g_new(flxServerEntry, 1); - e->record = flx_record_ref(r); - e->id = id; + g_assert(r->key->type != AVAHI_DNS_TYPE_ANY); + + e = g_new(AvahiEntry, 1); + e->server = s; + e->record = avahi_record_ref(r); + e->group = g; e->interface = interface; e->protocol = protocol; - e->unique = unique; + e->flags = flags; + e->dead = FALSE; - FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements); + AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements); - FLX_LLIST_PREPEND(flxServerEntry, entry, s->entries, e); + AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e); - /* Insert into hash table indexed by id */ - t = g_hash_table_lookup(s->rrset_by_id, &e->id); - FLX_LLIST_PREPEND(flxServerEntry, by_id, t, e); - g_hash_table_replace(s->rrset_by_id, &e->id, t); - /* Insert into hash table indexed by name */ - t = g_hash_table_lookup(s->rrset_by_key, e->record->key); - FLX_LLIST_PREPEND(flxServerEntry, by_key, t, e); - g_hash_table_replace(s->rrset_by_key, e->record->key, t); - - flx_announce_entry(s, e); -} + t = g_hash_table_lookup(s->entries_by_key, e->record->key); + AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e); + g_hash_table_replace(s->entries_by_key, e->record->key, t); -void flx_server_add_full( - flxServer *s, - gint id, - gint interface, - guchar protocol, - gboolean unique, - const gchar *name, - guint16 class, - guint16 type, - gconstpointer data, - guint size, - guint32 ttl) { - - flxRecord *r; - g_assert(s); - g_assert(data); - g_assert(size); + /* Insert into group list */ + if (g) + AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); - r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl); - flx_server_add(s, id, interface, protocol, unique, r); - flx_record_unref(r); + avahi_announce_entry(s, e); } - -const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) { - flxServerEntry **e = (flxServerEntry**) state; +const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) { + AvahiEntry **e = (AvahiEntry**) state; g_assert(s); g_assert(e); - if (e) - *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next; - else - *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries; + 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 flx_record_ref((*e)->record); -} - -static void free_entry(flxServer*s, flxServerEntry *e) { - flxServerEntry *t; - - g_assert(e); - - flx_goodbye_entry(s, e, TRUE); - - /* Remove from linked list */ - FLX_LLIST_REMOVE(flxServerEntry, entry, s->entries, e); - - /* Remove from hash table indexed by id */ - t = g_hash_table_lookup(s->rrset_by_id, &e->id); - FLX_LLIST_REMOVE(flxServerEntry, by_id, t, e); - if (t) - g_hash_table_replace(s->rrset_by_id, &t->id, t); - else - g_hash_table_remove(s->rrset_by_id, &e->id); - - /* Remove from hash table indexed by name */ - t = g_hash_table_lookup(s->rrset_by_key, e->record->key); - FLX_LLIST_REMOVE(flxServerEntry, by_key, t, e); - if (t) - g_hash_table_replace(s->rrset_by_key, t->record->key, t); - else - g_hash_table_remove(s->rrset_by_key, e->record->key); - - flx_record_unref(e->record); - g_free(e); -} - -void flx_server_remove(flxServer *s, gint id) { - g_assert(s); - - if (id <= 0) { - while (s->entries) - free_entry(s, s->entries); - } else { - flxServerEntry *e; - - while ((e = g_hash_table_lookup(s->rrset_by_id, &id))) - free_entry(s, e); - } + return avahi_record_ref((*e)->record); } -void flx_server_dump(flxServer *s, FILE *f) { - flxServerEntry *e; +void avahi_server_dump(AvahiServer *s, FILE *f) { + AvahiEntry *e; g_assert(s); g_assert(f); fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n"); - for (e = s->entries; e; e = e->entry_next) { + for (e = s->entries; e; e = e->entries_next) { gchar *t; - t = flx_record_to_string(e->record); - fprintf(f, "%s\n", t); + if (e->dead) + continue; + + t = avahi_record_to_string(e->record); + fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol); g_free(t); } - flx_dump_caches(s->monitor, f); + avahi_dump_caches(s->monitor, f); +} + +void avahi_server_add_ptr( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + AvahiEntryFlags flags, + const gchar *name, + const gchar *dest) { + + AvahiRecord *r; + + g_assert(dest); + + r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); + r->data.ptr.name = avahi_normalize_name(dest); + avahi_server_add(s, g, interface, protocol, flags, r); + avahi_record_unref(r); } -void flx_server_add_address( - flxServer *s, - gint id, +void avahi_server_add_address( + AvahiServer *s, + AvahiEntryGroup *g, gint interface, guchar protocol, - gboolean unique, + AvahiEntryFlags flags, const gchar *name, - flxAddress *a) { + AvahiAddress *a) { - gchar *n; + gchar *n = NULL; g_assert(s); g_assert(a); - n = name ? flx_normalize_name(name) : s->hostname; + name = name ? (n = avahi_normalize_name(name)) : s->hostname; if (a->family == AF_INET) { - gchar *r; - - flx_server_add_full(s, id, interface, protocol, unique, n, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_A, &a->data.ipv4, sizeof(a->data.ipv4), FLX_DEFAULT_TTL); + gchar *reverse; + AvahiRecord *r; - r = flx_reverse_lookup_name_ipv4(&a->data.ipv4); - g_assert(r); - flx_server_add_full(s, id, interface, protocol, unique, r, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_PTR, n, strlen(n)+1, FLX_DEFAULT_TTL); - g_free(r); + r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); + r->data.a.address = a->data.ipv4; + avahi_server_add(s, g, interface, protocol, flags, r); + avahi_record_unref(r); + + reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4); + g_assert(reverse); + avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); + g_free(reverse); } else { - gchar *r; + gchar *reverse; + AvahiRecord *r; - flx_server_add_full(s, id, interface, protocol, unique, n, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_AAAA, &a->data.ipv6, sizeof(a->data.ipv6), FLX_DEFAULT_TTL); - - r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); - g_assert(r); - flx_server_add_full(s, id, interface, protocol, unique, r, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_PTR, n, strlen(n)+1, FLX_DEFAULT_TTL); - g_free(r); + r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA); + r->data.aaaa.address = a->data.ipv6; + avahi_server_add(s, g, interface, protocol, flags, r); + avahi_record_unref(r); + + reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); + g_assert(reverse); + avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); + g_free(reverse); - r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6); - g_assert(r); - flx_server_add_full(s, id, interface, protocol, unique, r, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_PTR, n, strlen(n)+1, FLX_DEFAULT_TTL); - g_free(r); + reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); + g_assert(reverse); + avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name); + g_free(reverse); } g_free(n); } -void flx_server_add_text( - flxServer *s, - gint id, +void avahi_server_add_text_strlst( + AvahiServer *s, + AvahiEntryGroup *g, gint interface, guchar protocol, - gboolean unique, + AvahiEntryFlags flags, const gchar *name, - const gchar *text) { + AvahiStringList *strlst) { + + AvahiRecord *r; - gchar buf[256]; - guint l; + g_assert(s); + + r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); + r->data.txt.string_list = strlst; + avahi_server_add(s, g, interface, protocol, flags, r); + avahi_record_unref(r); +} + +void avahi_server_add_text_va( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + AvahiEntryFlags flags, + const gchar *name, + va_list va) { g_assert(s); - g_assert(text); - if ((l = strlen(text)) > 255) - buf[0] = 255; - else - buf[0] = (gchar) l; + avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va)); +} + +void avahi_server_add_text( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + AvahiEntryFlags flags, + const gchar *name, + ...) { + + va_list va; + + g_assert(s); + + va_start(va, name); + avahi_server_add_text_va(s, g, interface, protocol, flags, name, va); + va_end(va); +} + +static void escape_service_name(gchar *d, guint size, const gchar *s) { + g_assert(d); + g_assert(size); + g_assert(s); + + while (*s && size >= 2) { + if (*s == '.' || *s == '\\') { + if (size < 3) + break; + + *(d++) = '\\'; + size--; + } + + *(d++) = *(s++); + size--; + } + + g_assert(size > 0); + *(d++) = 0; +} + +void avahi_server_add_service_strlst( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + const gchar *type, + const gchar *name, + const gchar *domain, + const gchar *host, + guint16 port, + AvahiStringList *strlst) { + + gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256]; + AvahiRecord *r; + + g_assert(s); + g_assert(type); + g_assert(name); + + escape_service_name(ename, sizeof(ename), name); + + if (domain) { + while (domain[0] == '.') + domain++; + } else + domain = "local"; + + if (!host) + host = s->hostname; + + snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain); + snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain); + + avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name); + + r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); + r->data.srv.priority = 0; + r->data.srv.weight = 0; + r->data.srv.port = port; + r->data.srv.name = avahi_normalize_name(host); + avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); + avahi_record_unref(r); + + avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst); + + snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain); + avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name); +} + +void avahi_server_add_service_va( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + const gchar *type, + const gchar *name, + const gchar *domain, + const gchar *host, + guint16 port, + va_list va){ + + g_assert(s); + g_assert(type); + g_assert(name); + + avahi_server_add_service(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va)); +} + +void avahi_server_add_service( + AvahiServer *s, + AvahiEntryGroup *g, + gint interface, + guchar protocol, + const gchar *type, + const gchar *name, + const gchar *domain, + const gchar *host, + guint16 port, + ... ){ - memcpy(buf+1, text, l); + va_list va; + + g_assert(s); + g_assert(type); + g_assert(name); - flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, buf, l+1, FLX_DEFAULT_TTL); + va_start(va, port); + avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va); + va_end(va); } -static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) { - flxKey *k = userdata; +static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { + AvahiKey *k = userdata; g_assert(m); g_assert(i); g_assert(k); - flx_interface_post_query(i, k, FALSE); + avahi_interface_post_query(i, k, FALSE); } -void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) { +void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) { g_assert(s); g_assert(key); - flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); + avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); } -static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) { - flxRecord *r = userdata; +struct tmpdata { + AvahiRecord *record; + gboolean flush_cache; +}; + +static void post_response_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { + struct tmpdata *tmpdata = userdata; g_assert(m); g_assert(i); - g_assert(r); + g_assert(tmpdata); - flx_interface_post_response(i, r, FALSE); + avahi_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE); } -void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record) { +void avahi_server_post_response(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *record, gboolean flush_cache) { + struct tmpdata tmpdata; + g_assert(s); g_assert(record); - flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, record); + tmpdata.record = record; + tmpdata.flush_cache = flush_cache; + + avahi_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata); +} + +void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) { + g_assert(g); + + g->state = state; + + if (g->callback) { + g->callback(g->server, g, state, g->userdata); + return; + } +} + +AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) { + AvahiEntryGroup *g; + + g_assert(s); + + g = g_new(AvahiEntryGroup, 1); + g->server = s; + g->callback = callback; + g->userdata = userdata; + g->dead = FALSE; + g->state = AVAHI_ENTRY_GROUP_UNCOMMITED; + g->n_probing = 0; + AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries); + + AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g); + return g; +} + +void avahi_entry_group_free(AvahiEntryGroup *g) { + g_assert(g); + g_assert(g->server); + + g->dead = TRUE; + g->server->need_group_cleanup = TRUE; +} + +void avahi_entry_group_commit(AvahiEntryGroup *g) { + AvahiEntry *e; + + g_assert(g); + g_assert(!g->dead); + + if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED) + return; + + avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); + avahi_announce_group(g->server, g); + avahi_entry_group_check_probed(g, FALSE); +} + +gboolean avahi_entry_commited(AvahiEntry *e) { + g_assert(e); + g_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) { + g_assert(g); + g_assert(!g->dead); + + return g->state; }