1 #include <sys/socket.h>
4 #include <sys/utsname.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 for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next)
26 if (flx_interface_match(i, e->interface, e->protocol))
27 flx_interface_post_response(i, e->record);
30 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
38 for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
41 if (!(key = flx_dns_packet_consume_key(p))) {
42 g_warning("Packet too short");
46 handle_query_key(s, key, i, a);
51 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
59 for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) +
60 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT); n > 0; n--) {
62 gboolean cache_flush = FALSE;
65 if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
66 g_warning("Packet too short");
70 g_message("Handling response: %s", txt = flx_record_to_string(record));
73 flx_cache_update(i->cache, record, cache_flush, a);
74 flx_packet_scheduler_drop_response(i->scheduler, record);
75 flx_record_unref(record);
79 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
88 g_message("new packet recieved.");
90 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
91 g_warning("Recieved packet from invalid interface.");
96 g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol);
100 if (sa->sa_family == AF_INET6) {
101 static const unsigned char ipv4_in_ipv6[] = {
102 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00,
104 0xFF, 0xFF, 0xFF, 0xFF };
106 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
108 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
113 if (flx_dns_packet_check_valid(p) < 0) {
114 g_warning("Recieved invalid packet.");
118 flx_address_from_sockaddr(sa, &a);
120 if (flx_dns_packet_is_query(p)) {
122 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
123 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
124 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
125 g_warning("Invalid query packet.");
129 handle_query(s, p, i, &a);
130 g_message("Handled query");
132 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
133 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
134 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
135 g_warning("Invalid response packet.");
139 handle_response(s, p, i, &a);
140 g_message("Handled response");
144 static gboolean work(flxServer *s) {
145 struct sockaddr_in6 sa6;
146 struct sockaddr_in sa;
153 if (s->pollfd_ipv4.revents & G_IO_IN) {
154 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
155 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
156 flx_dns_packet_free(p);
160 if (s->pollfd_ipv6.revents & G_IO_IN) {
161 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
162 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
163 flx_dns_packet_free(p);
170 static gboolean prepare_func(GSource *source, gint *timeout) {
178 static gboolean check_func(GSource *source) {
182 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
185 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
188 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
192 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
198 static void add_default_entries(flxServer *s) {
200 struct utsname utsname;
206 /* Fill in HINFO rr */
208 hinfo = g_strdup_printf("%c%s%c%s%n",
209 strlen(utsname.machine), g_strup(utsname.machine),
210 strlen(utsname.sysname), g_strup(utsname.sysname),
213 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
214 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length, FLX_DEFAULT_TTL);
218 /* Add localhost entries */
219 flx_address_parse("127.0.0.1", AF_INET, &a);
220 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
222 flx_address_parse("::1", AF_INET6, &a);
223 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
226 flxServer *flx_server_new(GMainContext *c) {
230 static GSourceFuncs source_funcs = {
239 s = g_new(flxServer, 1);
241 s->fd_ipv4 = flx_open_socket_ipv4();
242 s->fd_ipv6 = flx_open_socket_ipv6();
244 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
245 g_critical("Failed to create sockets.\n");
251 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
252 else if (s->fd_ipv6 < 0)
253 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
256 g_main_context_ref(s->context = c);
258 s->context = g_main_context_default();
261 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
262 s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
264 FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries);
266 s->monitor = flx_interface_monitor_new(s);
267 s->time_event_queue = flx_time_event_queue_new(s->context);
270 hn = flx_get_host_name();
271 if ((e = strchr(hn, '.')))
274 s->hostname = g_strdup_printf("%s.local.", hn);
277 add_default_entries(s);
279 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
280 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
282 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
283 s->pollfd_ipv4.fd = s->fd_ipv4;
284 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
285 g_source_add_poll(s->source, &s->pollfd_ipv4);
287 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
288 s->pollfd_ipv6.fd = s->fd_ipv6;
289 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
290 g_source_add_poll(s->source, &s->pollfd_ipv6);
292 g_source_attach(s->source, s->context);
297 void flx_server_free(flxServer* s) {
300 flx_interface_monitor_free(s->monitor);
302 flx_server_remove(s, 0);
304 g_hash_table_destroy(s->rrset_by_id);
305 g_hash_table_destroy(s->rrset_by_key);
307 flx_time_event_queue_free(s->time_event_queue);
316 g_source_destroy(s->source);
317 g_source_unref(s->source);
318 g_main_context_unref(s->context);
323 gint flx_server_get_next_id(flxServer *s) {
326 return s->current_id++;
337 flxServerEntry *e, *t;
341 e = g_new(flxServerEntry, 1);
342 e->record = flx_record_ref(r);
344 e->interface = interface;
345 e->protocol = protocol;
348 FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements);
350 FLX_LLIST_PREPEND(flxServerEntry, entry, s->entries, e);
352 /* Insert into hash table indexed by id */
353 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
354 FLX_LLIST_PREPEND(flxServerEntry, by_id, t, e);
355 g_hash_table_replace(s->rrset_by_id, &e->id, t);
357 /* Insert into hash table indexed by name */
358 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
359 FLX_LLIST_PREPEND(flxServerEntry, by_key, t, e);
360 g_hash_table_replace(s->rrset_by_key, e->record->key, t);
362 flx_announce_entry(s, e);
365 void flx_server_add_full(
383 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
384 flx_server_add(s, id, interface, protocol, unique, r);
388 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
389 flxServerEntry **e = (flxServerEntry**) state;
394 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
396 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
401 return flx_record_ref((*e)->record);
404 static void free_entry(flxServer*s, flxServerEntry *e) {
409 flx_goodbye_entry(s, e, TRUE);
411 /* Remove from linked list */
412 FLX_LLIST_REMOVE(flxServerEntry, entry, s->entries, e);
414 /* Remove from hash table indexed by id */
415 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
416 FLX_LLIST_REMOVE(flxServerEntry, by_id, t, e);
418 g_hash_table_replace(s->rrset_by_id, &t->id, t);
420 g_hash_table_remove(s->rrset_by_id, &e->id);
422 /* Remove from hash table indexed by name */
423 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
424 FLX_LLIST_REMOVE(flxServerEntry, by_key, t, e);
426 g_hash_table_replace(s->rrset_by_key, t->record->key, t);
428 g_hash_table_remove(s->rrset_by_key, e->record->key);
430 flx_record_unref(e->record);
434 void flx_server_remove(flxServer *s, gint id) {
439 free_entry(s, s->entries);
443 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
448 void flx_server_dump(flxServer *s, FILE *f) {
453 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
455 for (e = s->entries; e; e = e->entry_next) {
458 t = flx_record_to_string(e->record);
459 fprintf(f, "%s\n", t);
463 flx_dump_caches(s->monitor, f);
466 void flx_server_add_address(
479 n = name ? flx_normalize_name(name) : s->hostname;
481 if (a->family == AF_INET) {
484 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);
486 r = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
488 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);
494 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);
496 r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
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);
501 r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
503 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);
510 void flx_server_add_text(
525 if ((l = strlen(text)) > 255)
530 memcpy(buf+1, text, l);
532 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, buf, l+1, FLX_DEFAULT_TTL);
535 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) {
540 if (protocol != AF_UNSPEC) {
543 if ((i = flx_interface_monitor_get_interface(s->monitor, interface, protocol)))
544 flx_interface_post_query(i, key);
549 if ((hw = flx_interface_monitor_get_hw_interface(s->monitor, interface)))
550 for (i = hw->interfaces; i; i = i->by_hardware_next)
551 if (flx_interface_match(i, interface, protocol))
552 flx_interface_post_query(i, key);
558 for (i = s->monitor->interfaces; i; i = i->interface_next)
559 if (flx_interface_match(i, interface, protocol))
560 flx_interface_post_query(i, key);
564 void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record) {
569 if (protocol != AF_UNSPEC) {
572 if ((i = flx_interface_monitor_get_interface(s->monitor, interface, protocol)))
573 flx_interface_post_response(i, record);
578 if ((hw = flx_interface_monitor_get_hw_interface(s->monitor, interface)))
579 for (i = hw->interfaces; i; i = i->by_hardware_next)
580 if (flx_interface_match(i, interface, protocol))
581 flx_interface_post_response(i, record);
587 for (i = s->monitor->interfaces; i; i = i->interface_next)
588 if (flx_interface_match(i, interface, protocol))
589 flx_interface_post_response(i, record);