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 for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next)
28 if (flx_interface_match(i, e->interface, e->protocol))
29 flx_interface_post_response(i, e->record, FALSE);
32 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
40 for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
43 if (!(key = flx_dns_packet_consume_key(p))) {
44 g_warning("Packet too short");
48 handle_query_key(s, key, i, a);
53 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
61 for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) +
62 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT); n > 0; n--) {
64 gboolean cache_flush = FALSE;
67 if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
68 g_warning("Packet too short");
72 g_message("Handling response: %s", txt = flx_record_to_string(record));
75 flx_cache_update(i->cache, record, cache_flush, a);
77 flx_packet_scheduler_incoming_response(i->scheduler, record);
78 flx_record_unref(record);
82 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
91 g_message("new packet recieved.");
93 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
94 g_warning("Recieved packet from invalid interface.");
99 g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol);
103 if (sa->sa_family == AF_INET6) {
104 static const unsigned char ipv4_in_ipv6[] = {
105 0x00, 0x00, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00,
107 0xFF, 0xFF, 0xFF, 0xFF };
109 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
111 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
116 if (flx_dns_packet_check_valid(p) < 0) {
117 g_warning("Recieved invalid packet.");
121 flx_address_from_sockaddr(sa, &a);
123 if (flx_dns_packet_is_query(p)) {
125 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
126 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
127 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
128 g_warning("Invalid query packet.");
132 handle_query(s, p, i, &a);
133 g_message("Handled query");
135 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
136 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
137 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
138 g_warning("Invalid response packet.");
142 handle_response(s, p, i, &a);
143 g_message("Handled response");
147 static gboolean work(flxServer *s) {
148 struct sockaddr_in6 sa6;
149 struct sockaddr_in sa;
156 if (s->pollfd_ipv4.revents & G_IO_IN) {
157 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
158 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
159 flx_dns_packet_free(p);
163 if (s->pollfd_ipv6.revents & G_IO_IN) {
164 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
165 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
166 flx_dns_packet_free(p);
173 static gboolean prepare_func(GSource *source, gint *timeout) {
181 static gboolean check_func(GSource *source) {
185 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
188 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
191 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
195 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
201 static void add_default_entries(flxServer *s) {
203 struct utsname utsname;
209 /* Fill in HINFO rr */
211 hinfo = g_strdup_printf("%c%s%c%s%n",
212 strlen(utsname.machine), g_strup(utsname.machine),
213 strlen(utsname.sysname), g_strup(utsname.sysname),
216 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
217 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length, FLX_DEFAULT_TTL);
221 /* Add localhost entries */
222 flx_address_parse("127.0.0.1", AF_INET, &a);
223 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
225 flx_address_parse("::1", AF_INET6, &a);
226 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
229 flxServer *flx_server_new(GMainContext *c) {
233 static GSourceFuncs source_funcs = {
242 s = g_new(flxServer, 1);
244 s->fd_ipv4 = flx_open_socket_ipv4();
245 s->fd_ipv6 = flx_open_socket_ipv6();
247 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
248 g_critical("Failed to create sockets.\n");
254 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
255 else if (s->fd_ipv6 < 0)
256 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
259 g_main_context_ref(s->context = c);
261 s->context = g_main_context_default();
265 FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries);
266 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
267 s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
269 FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions);
270 s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
272 s->monitor = flx_interface_monitor_new(s);
273 s->time_event_queue = flx_time_event_queue_new(s->context);
276 hn = flx_get_host_name();
277 if ((e = strchr(hn, '.')))
280 s->hostname = g_strdup_printf("%s.local.", hn);
283 add_default_entries(s);
285 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
286 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
288 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
289 s->pollfd_ipv4.fd = s->fd_ipv4;
290 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
291 g_source_add_poll(s->source, &s->pollfd_ipv4);
293 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
294 s->pollfd_ipv6.fd = s->fd_ipv6;
295 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
296 g_source_add_poll(s->source, &s->pollfd_ipv6);
298 g_source_attach(s->source, s->context);
303 void flx_server_free(flxServer* s) {
306 flx_interface_monitor_free(s->monitor);
308 flx_server_remove(s, 0);
310 while (s->subscriptions)
311 flx_subscription_free(s->subscriptions);
312 g_hash_table_destroy(s->subscription_hashtable);
314 g_hash_table_destroy(s->rrset_by_id);
315 g_hash_table_destroy(s->rrset_by_key);
317 flx_time_event_queue_free(s->time_event_queue);
326 g_source_destroy(s->source);
327 g_source_unref(s->source);
328 g_main_context_unref(s->context);
333 gint flx_server_get_next_id(flxServer *s) {
336 return s->current_id++;
347 flxServerEntry *e, *t;
351 e = g_new(flxServerEntry, 1);
352 e->record = flx_record_ref(r);
354 e->interface = interface;
355 e->protocol = protocol;
358 FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements);
360 FLX_LLIST_PREPEND(flxServerEntry, entry, s->entries, e);
362 /* Insert into hash table indexed by id */
363 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
364 FLX_LLIST_PREPEND(flxServerEntry, by_id, t, e);
365 g_hash_table_replace(s->rrset_by_id, &e->id, t);
367 /* Insert into hash table indexed by name */
368 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
369 FLX_LLIST_PREPEND(flxServerEntry, by_key, t, e);
370 g_hash_table_replace(s->rrset_by_key, e->record->key, t);
372 flx_announce_entry(s, e);
375 void flx_server_add_full(
393 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
394 flx_server_add(s, id, interface, protocol, unique, r);
398 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
399 flxServerEntry **e = (flxServerEntry**) state;
404 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
406 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
411 return flx_record_ref((*e)->record);
414 static void free_entry(flxServer*s, flxServerEntry *e) {
419 flx_goodbye_entry(s, e, TRUE);
421 /* Remove from linked list */
422 FLX_LLIST_REMOVE(flxServerEntry, entry, s->entries, e);
424 /* Remove from hash table indexed by id */
425 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
426 FLX_LLIST_REMOVE(flxServerEntry, by_id, t, e);
428 g_hash_table_replace(s->rrset_by_id, &t->id, t);
430 g_hash_table_remove(s->rrset_by_id, &e->id);
432 /* Remove from hash table indexed by name */
433 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
434 FLX_LLIST_REMOVE(flxServerEntry, by_key, t, e);
436 g_hash_table_replace(s->rrset_by_key, t->record->key, t);
438 g_hash_table_remove(s->rrset_by_key, e->record->key);
440 flx_record_unref(e->record);
444 void flx_server_remove(flxServer *s, gint id) {
449 free_entry(s, s->entries);
453 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
458 void flx_server_dump(flxServer *s, FILE *f) {
463 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
465 for (e = s->entries; e; e = e->entry_next) {
468 t = flx_record_to_string(e->record);
469 fprintf(f, "%s\n", t);
473 flx_dump_caches(s->monitor, f);
476 void flx_server_add_address(
489 n = name ? flx_normalize_name(name) : s->hostname;
491 if (a->family == AF_INET) {
494 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);
496 r = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
498 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);
504 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);
506 r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
508 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);
511 r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
513 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);
520 void flx_server_add_text(
535 if ((l = strlen(text)) > 255)
540 memcpy(buf+1, text, l);
542 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, buf, l+1, FLX_DEFAULT_TTL);
545 static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
546 flxKey *k = userdata;
552 flx_interface_post_query(i, k, FALSE);
555 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) {
559 flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
562 static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
563 flxRecord *r = userdata;
569 flx_interface_post_response(i, r, FALSE);
572 void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record) {
576 flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, record);