1 #include <sys/socket.h>
4 #include <sys/utsname.h>
12 static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) {
21 g_message("Handling query: %s", txt = flx_key_to_string(k));
24 for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) {
26 if ((e->interface <= 0 || e->interface == i->index) &&
27 (e->protocol == AF_UNSPEC || e->protocol == a->family)) {
29 flx_interface_post_response(i, a->family, e->record);
34 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
42 for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
45 if (!(key = flx_dns_packet_consume_key(p))) {
46 g_warning("Packet too short");
50 handle_query_key(s, key, i, a);
55 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
63 for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) +
64 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT); n > 0; n--) {
66 gboolean cache_flush = FALSE;
69 if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
70 g_warning("Packet too short");
74 g_message("Handling response: %s", txt = flx_record_to_string(record));
77 flx_cache_update(a->family == AF_INET ? i->ipv4_cache : i->ipv6_cache, record, cache_flush, a);
78 flx_packet_scheduler_drop_response(a->family == AF_INET ? i->ipv4_scheduler : i->ipv6_scheduler, record);
79 flx_record_unref(record);
83 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
92 g_message("new packet recieved.");
94 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) {
95 g_warning("Recieved packet from invalid interface.");
100 g_warning("Recieved packet with invalid TTL on interface '%s'.", i->name);
104 if (sa->sa_family == AF_INET6) {
105 static const unsigned char ipv4_in_ipv6[] = {
106 0x00, 0x00, 0x00, 0x00,
107 0x00, 0x00, 0x00, 0x00,
108 0xFF, 0xFF, 0xFF, 0xFF };
110 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
112 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
117 if (flx_dns_packet_check_valid(p) < 0) {
118 g_warning("Recieved invalid packet.");
122 flx_address_from_sockaddr(sa, &a);
124 if (flx_dns_packet_is_query(p)) {
126 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
127 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
128 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
129 g_warning("Invalid query packet.");
133 handle_query(s, p, i, &a);
134 g_message("Handled query");
136 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
137 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
138 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
139 g_warning("Invalid response packet.");
143 handle_response(s, p, i, &a);
144 g_message("Handled response");
148 static gboolean work(flxServer *s) {
149 struct sockaddr_in6 sa6;
150 struct sockaddr_in sa;
157 if (s->pollfd_ipv4.revents & G_IO_IN) {
158 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
159 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
162 if (s->pollfd_ipv6.revents & G_IO_IN) {
163 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl)))
164 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
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("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
210 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
211 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length+1, FLX_DEFAULT_TTL);
215 /* Add localhost entries */
216 flx_address_parse("127.0.0.1", AF_INET, &a);
217 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
219 flx_address_parse("::1", AF_INET6, &a);
220 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
223 flxServer *flx_server_new(GMainContext *c) {
227 static GSourceFuncs source_funcs = {
236 s = g_new(flxServer, 1);
238 s->fd_ipv4 = flx_open_socket_ipv4();
239 s->fd_ipv6 = flx_open_socket_ipv6();
241 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
242 g_critical("Failed to create sockets.\n");
248 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
249 else if (s->fd_ipv6 < 0)
250 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
253 g_main_context_ref(s->context = c);
255 s->context = g_main_context_default();
258 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
259 s->rrset_by_name = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
261 FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
263 s->monitor = flx_interface_monitor_new(s);
264 s->time_event_queue = flx_time_event_queue_new(s->context);
267 hn = flx_get_host_name();
268 if ((e = strchr(hn, '.')))
271 s->hostname = g_strdup_printf("%s.local.", hn);
274 add_default_entries(s);
276 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
277 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
279 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
280 s->pollfd_ipv4.fd = s->fd_ipv4;
281 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
282 g_source_add_poll(s->source, &s->pollfd_ipv4);
284 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
285 s->pollfd_ipv6.fd = s->fd_ipv6;
286 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
287 g_source_add_poll(s->source, &s->pollfd_ipv6);
289 g_source_attach(s->source, s->context);
294 void flx_server_free(flxServer* s) {
297 flx_interface_monitor_free(s->monitor);
299 flx_server_remove(s, 0);
301 g_hash_table_destroy(s->rrset_by_id);
302 g_hash_table_destroy(s->rrset_by_name);
304 flx_time_event_queue_free(s->time_event_queue);
313 g_source_destroy(s->source);
314 g_source_unref(s->source);
315 g_main_context_unref(s->context);
320 gint flx_server_get_next_id(flxServer *s) {
323 return s->current_id++;
338 e = g_new(flxEntry, 1);
339 e->record = flx_record_ref(r);
341 e->interface = interface;
342 e->protocol = protocol;
345 FLX_LLIST_PREPEND(flxEntry, entry, s->entries, e);
347 /* Insert into hash table indexed by id */
348 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
349 FLX_LLIST_PREPEND(flxEntry, by_id, t, e);
350 g_hash_table_replace(s->rrset_by_id, &e->id, t);
352 /* Insert into hash table indexed by name */
353 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
354 FLX_LLIST_PREPEND(flxEntry, by_name, t, e);
355 g_hash_table_replace(s->rrset_by_name, e->record->key, t);
358 void flx_server_add_full(
376 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
377 flx_server_add(s, id, interface, protocol, unique, r);
381 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
382 flxEntry **e = (flxEntry**) state;
387 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
389 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
394 return flx_record_ref((*e)->record);
397 static void free_entry(flxServer*s, flxEntry *e) {
402 /* Remove from linked list */
403 FLX_LLIST_REMOVE(flxEntry, entry, s->entries, e);
405 /* Remove from hash table indexed by id */
406 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
407 FLX_LLIST_REMOVE(flxEntry, by_id, t, e);
409 g_hash_table_replace(s->rrset_by_id, &t->id, t);
411 g_hash_table_remove(s->rrset_by_id, &e->id);
413 /* Remove from hash table indexed by name */
414 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
415 FLX_LLIST_REMOVE(flxEntry, by_name, t, e);
417 g_hash_table_replace(s->rrset_by_name, t->record->key, t);
419 g_hash_table_remove(s->rrset_by_name, e->record->key);
421 flx_record_unref(e->record);
425 void flx_server_remove(flxServer *s, gint id) {
430 free_entry(s, s->entries);
434 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
439 void flx_server_dump(flxServer *s, FILE *f) {
444 fprintf(f, ";;; ZONE DUMP FOLLOWS ;;;\n");
446 for (e = s->entries; e; e = e->entry_next) {
449 t = flx_record_to_string(e->record);
450 fprintf(f, "%s\n", t);
454 flx_dump_caches(s, f);
457 void flx_server_add_address(
470 n = name ? flx_normalize_name(name) : s->hostname;
472 if (a->family == AF_INET) {
475 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);
477 r = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
479 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);
485 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);
487 r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
489 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);
492 r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
494 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 void flx_server_add_text(
513 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, text, strlen(text), FLX_DEFAULT_TTL);
516 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *k) {
520 if (interface <= 0) {
523 for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
524 flx_interface_post_query(i, protocol, k);
529 if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
532 flx_interface_post_query(i, protocol, k);