1 #include <sys/socket.h>
4 #include <sys/utsname.h>
11 #include "subscribe.h"
13 static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) {
22 g_message("Handling query: %s", txt = flx_key_to_string(k));
25 flx_packet_scheduler_incoming_query(i->scheduler, k);
27 if (k->type == FLX_DNS_TYPE_ANY) {
29 /* Handle ANY query */
31 for (e = s->entries; e; e = e->entry_next)
32 if (flx_key_pattern_match(k, e->record->key))
33 if (flx_interface_match(i, e->interface, e->protocol) && flx_entry_established(s, e, i))
34 flx_interface_post_response(i, a, e->record, e->flags & FLX_SERVER_ENTRY_UNIQUE, FALSE);
37 /* Handle all other queries */
39 for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next)
40 if (flx_interface_match(i, e->interface, e->protocol) && flx_entry_established(s, e, i))
41 flx_interface_post_response(i, a, e->record, e->flags & FLX_SERVER_ENTRY_UNIQUE, FALSE);
45 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
53 for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT); n > 0; n --) {
56 if (!(key = flx_dns_packet_consume_key(p))) {
57 g_warning("Packet too short");
61 handle_query_key(s, key, i, a);
65 /* Known Answer Suppresion */
66 for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT); n > 0; n --) {
68 gboolean unique = FALSE;
70 if (!(record = flx_dns_packet_consume_record(p, &unique))) {
71 g_warning("Packet too short (2)");
75 flx_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
76 flx_record_unref(record);
80 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
88 for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT) +
89 flx_dns_packet_get_field(p, FLX_DNS_FIELD_ARCOUNT); n > 0; n--) {
91 gboolean cache_flush = FALSE;
94 if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
95 g_warning("Packet too short (3)");
99 if (record->key->type == FLX_DNS_TYPE_ANY)
102 g_message("Handling response: %s", txt = flx_record_to_string(record));
105 flx_cache_update(i->cache, record, cache_flush, a);
107 flx_packet_scheduler_incoming_response(i->scheduler, record);
108 flx_record_unref(record);
112 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
121 g_message("new packet recieved.");
123 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
124 g_warning("Recieved packet from invalid interface.");
129 g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol);
133 if (sa->sa_family == AF_INET6) {
134 static const unsigned char ipv4_in_ipv6[] = {
135 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00,
137 0xFF, 0xFF, 0xFF, 0xFF };
139 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
141 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
146 if (flx_dns_packet_check_valid(p) < 0) {
147 g_warning("Recieved invalid packet.");
151 flx_address_from_sockaddr(sa, &a);
153 if (flx_dns_packet_is_query(p)) {
155 if (flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT) == 0 ||
156 flx_dns_packet_get_field(p, FLX_DNS_FIELD_ARCOUNT) != 0) {
157 g_warning("Invalid query packet.");
161 handle_query(s, p, i, &a);
162 g_message("Handled query");
164 if (flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT) != 0 ||
165 flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT) == 0 ||
166 flx_dns_packet_get_field(p, FLX_DNS_FIELD_NSCOUNT) != 0) {
167 g_warning("Invalid response packet.");
171 handle_response(s, p, i, &a);
172 g_message("Handled response");
176 static gboolean work(flxServer *s) {
177 struct sockaddr_in6 sa6;
178 struct sockaddr_in sa;
185 if (s->pollfd_ipv4.revents & G_IO_IN) {
186 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
187 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
188 flx_dns_packet_free(p);
192 if (s->pollfd_ipv6.revents & G_IO_IN) {
193 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
194 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
195 flx_dns_packet_free(p);
202 static gboolean prepare_func(GSource *source, gint *timeout) {
210 static gboolean check_func(GSource *source) {
214 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
217 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
220 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
224 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
230 static void add_default_entries(flxServer *s) {
232 struct utsname utsname;
239 /* Fill in HINFO rr */
240 r = flx_record_new_full(s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO);
242 r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
243 r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
244 flx_server_add(s, 0, 0, AF_UNSPEC, FLX_SERVER_ENTRY_UNIQUE, r);
247 /* Add localhost entries */
248 flx_address_parse("127.0.0.1", AF_INET, &a);
249 flx_server_add_address(s, 0, 0, AF_UNSPEC, FLX_SERVER_ENTRY_UNIQUE|FLX_SERVER_ENTRY_NOPROBE|FLX_SERVER_ENTRY_NOANNOUNCE, "localhost", &a);
251 flx_address_parse("::1", AF_INET6, &a);
252 flx_server_add_address(s, 0, 0, AF_UNSPEC, FLX_SERVER_ENTRY_UNIQUE|FLX_SERVER_ENTRY_NOPROBE|FLX_SERVER_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
255 flxServer *flx_server_new(GMainContext *c) {
259 static GSourceFuncs source_funcs = {
268 s = g_new(flxServer, 1);
270 s->fd_ipv4 = flx_open_socket_ipv4();
271 s->fd_ipv6 = flx_open_socket_ipv6();
273 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
274 g_critical("Failed to create sockets.\n");
280 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
281 else if (s->fd_ipv6 < 0)
282 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
285 g_main_context_ref(s->context = c);
287 s->context = g_main_context_default();
291 FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries);
292 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
293 s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
295 FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions);
296 s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
298 s->monitor = flx_interface_monitor_new(s);
299 s->time_event_queue = flx_time_event_queue_new(s->context);
302 hn = flx_get_host_name();
303 if ((e = strchr(hn, '.')))
306 s->hostname = g_strdup_printf("%s.local.", hn);
309 add_default_entries(s);
311 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
312 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
314 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
315 s->pollfd_ipv4.fd = s->fd_ipv4;
316 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
317 g_source_add_poll(s->source, &s->pollfd_ipv4);
319 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
320 s->pollfd_ipv6.fd = s->fd_ipv6;
321 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
322 g_source_add_poll(s->source, &s->pollfd_ipv6);
324 g_source_attach(s->source, s->context);
329 void flx_server_free(flxServer* s) {
332 flx_interface_monitor_free(s->monitor);
334 flx_server_remove(s, 0);
336 while (s->subscriptions)
337 flx_subscription_free(s->subscriptions);
338 g_hash_table_destroy(s->subscription_hashtable);
340 g_hash_table_destroy(s->rrset_by_id);
341 g_hash_table_destroy(s->rrset_by_key);
343 flx_time_event_queue_free(s->time_event_queue);
352 g_source_destroy(s->source);
353 g_source_unref(s->source);
354 g_main_context_unref(s->context);
359 gint flx_server_get_next_id(flxServer *s) {
362 return s->current_id++;
370 flxServerEntryFlags flags,
373 flxServerEntry *e, *t;
377 g_assert(r->key->type != FLX_DNS_TYPE_ANY);
379 e = g_new(flxServerEntry, 1);
380 e->record = flx_record_ref(r);
382 e->interface = interface;
383 e->protocol = protocol;
386 FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements);
388 FLX_LLIST_PREPEND(flxServerEntry, entry, s->entries, e);
390 /* Insert into hash table indexed by id */
391 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
392 FLX_LLIST_PREPEND(flxServerEntry, by_id, t, e);
393 g_hash_table_replace(s->rrset_by_id, &e->id, t);
395 /* Insert into hash table indexed by name */
396 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
397 FLX_LLIST_PREPEND(flxServerEntry, by_key, t, e);
398 g_hash_table_replace(s->rrset_by_key, e->record->key, t);
400 flx_announce_entry(s, e);
402 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
403 flxServerEntry **e = (flxServerEntry**) state;
408 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
410 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
415 return flx_record_ref((*e)->record);
418 static void free_entry(flxServer*s, flxServerEntry *e) {
423 flx_goodbye_entry(s, e, TRUE);
425 /* Remove from linked list */
426 FLX_LLIST_REMOVE(flxServerEntry, entry, s->entries, e);
428 /* Remove from hash table indexed by id */
429 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
430 FLX_LLIST_REMOVE(flxServerEntry, by_id, t, e);
432 g_hash_table_replace(s->rrset_by_id, &t->id, t);
434 g_hash_table_remove(s->rrset_by_id, &e->id);
436 /* Remove from hash table indexed by name */
437 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
438 FLX_LLIST_REMOVE(flxServerEntry, by_key, t, e);
440 g_hash_table_replace(s->rrset_by_key, t->record->key, t);
442 g_hash_table_remove(s->rrset_by_key, e->record->key);
444 flx_record_unref(e->record);
448 void flx_server_remove(flxServer *s, gint id) {
453 free_entry(s, s->entries);
457 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
462 void flx_server_dump(flxServer *s, FILE *f) {
467 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
469 for (e = s->entries; e; e = e->entry_next) {
472 t = flx_record_to_string(e->record);
473 fprintf(f, "%s\n", t);
477 flx_dump_caches(s->monitor, f);
480 void flx_server_add_ptr(
485 flxServerEntryFlags flags,
493 r = flx_record_new_full(name ? name : s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_PTR);
494 r->data.ptr.name = flx_normalize_name(dest);
495 flx_server_add(s, id, interface, protocol, flags, r);
500 void flx_server_add_address(
505 flxServerEntryFlags flags,
513 name = name ? (n = flx_normalize_name(name)) : s->hostname;
515 if (a->family == AF_INET) {
519 r = flx_record_new_full(name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_A);
520 r->data.a.address = a->data.ipv4;
521 flx_server_add(s, id, interface, protocol, flags, r);
524 reverse = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
526 flx_server_add_ptr(s, id, interface, protocol, flags, reverse, name);
533 r = flx_record_new_full(name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_AAAA);
534 r->data.aaaa.address = a->data.ipv6;
535 flx_server_add(s, id, interface, protocol, flags, r);
538 reverse = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
540 flx_server_add_ptr(s, id, interface, protocol, flags, reverse, name);
543 reverse = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
545 flx_server_add_ptr(s, id, interface, protocol, flags, reverse, name);
552 void flx_server_add_text_va(
557 flxServerEntryFlags flags,
565 r = flx_record_new_full(name ? name : s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT);
566 r->data.txt.string_list = flx_string_list_new_va(va);
567 flx_server_add(s, id, interface, protocol, flags, r);
571 void flx_server_add_text(
576 flxServerEntryFlags flags,
585 flx_server_add_text_va(s, id, interface, protocol, flags, name, va);
589 static void escape_service_name(gchar *d, guint size, const gchar *s) {
594 while (*s && size >= 2) {
595 if (*s == '.' || *s == '\\') {
612 void flx_server_add_service_va(
624 gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
631 escape_service_name(ename, sizeof(ename), name);
634 while (domain[0] == '.')
642 snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
643 snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
645 flx_server_add_ptr(s, id, interface, protocol, FALSE, ptr_name, svc_name);
647 r = flx_record_new_full(svc_name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_SRV);
648 r->data.srv.priority = 0;
649 r->data.srv.weight = 0;
650 r->data.srv.port = port;
651 r->data.srv.name = flx_normalize_name(host);
652 flx_server_add(s, id, interface, protocol, TRUE, r);
655 flx_server_add_text_va(s, id, interface, protocol, FALSE, svc_name, va);
657 snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
658 flx_server_add_ptr(s, id, interface, protocol, FALSE, enum_ptr, ptr_name);
661 void flx_server_add_service(
680 flx_server_add_service_va(s, id, interface, protocol, type, name, domain, host, port, va);
684 static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
685 flxKey *k = userdata;
691 flx_interface_post_query(i, k, FALSE);
694 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) {
698 flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
703 gboolean flush_cache;
706 static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
707 struct tmpdata *tmpdata = userdata;
713 flx_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE);
716 void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record, gboolean flush_cache) {
717 struct tmpdata tmpdata;
722 tmpdata.record = record;
723 tmpdata.flush_cache = flush_cache;
725 flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata);