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) {
20 for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) {
22 if ((e->interface <= 0 || e->interface == i->index) &&
23 (e->protocol == AF_UNSPEC || e->protocol == a->family)) {
25 flx_interface_post_response(i, a->family, 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); n > 0; n--) {
61 gboolean cache_flush = FALSE;
63 if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
64 g_warning("Packet too short");
68 flx_cache_update(a->family == AF_INET ? i->ipv4_cache : i->ipv6_cache, record, cache_flush, a);
69 flx_packet_scheduler_drop_response(a->family == AF_INET ? i->ipv4_scheduler : i->ipv6_scheduler, record);
70 flx_record_unref(record);
74 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
83 g_message("new packet recieved.");
85 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) {
86 g_warning("Recieved packet from invalid interface.");
91 g_warning("Recieved packet with invalid TTL on interface '%s'.", i->name);
95 if (sa->sa_family == AF_INET6) {
96 static const unsigned char ipv4_in_ipv6[] = {
97 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00,
99 0xFF, 0xFF, 0xFF, 0xFF };
101 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
103 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
108 if (flx_dns_packet_check_valid(p) < 0) {
109 g_warning("Recieved invalid packet.");
113 flx_address_from_sockaddr(sa, &a);
116 if (flx_dns_packet_is_query(p)) {
118 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
119 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
120 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
121 g_warning("Invalid query packet.");
125 handle_query(s, p, i, &a);
126 g_message("Handled query");
128 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
129 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
130 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0 ||
131 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0) {
132 g_warning("Invalid response packet.");
136 handle_response(s, p, i, &a);
137 g_message("Handled response");
141 static gboolean work(flxServer *s) {
142 struct sockaddr_in6 sa6;
143 struct sockaddr_in sa;
150 if (s->pollfd_ipv4.revents & G_IO_IN) {
151 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
152 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
155 if (s->pollfd_ipv6.revents & G_IO_IN) {
156 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl)))
157 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
163 static gboolean prepare_func(GSource *source, gint *timeout) {
171 static gboolean check_func(GSource *source) {
175 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
178 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
181 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
185 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
191 static void add_default_entries(flxServer *s) {
193 struct utsname utsname;
199 /* Fill in HINFO rr */
201 hinfo = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
203 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
204 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length+1, FLX_DEFAULT_TTL);
208 /* Add localhost entries */
209 flx_address_parse("127.0.0.1", AF_INET, &a);
210 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
212 flx_address_parse("::1", AF_INET6, &a);
213 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
216 flxServer *flx_server_new(GMainContext *c) {
220 static GSourceFuncs source_funcs = {
229 s = g_new(flxServer, 1);
231 s->fd_ipv4 = flx_open_socket_ipv4();
232 s->fd_ipv6 = flx_open_socket_ipv6();
234 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
235 g_critical("Failed to create sockets.\n");
241 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
242 else if (s->fd_ipv6 < 0)
243 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
246 g_main_context_ref(s->context = c);
248 s->context = g_main_context_default();
251 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
252 s->rrset_by_name = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
254 FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
256 s->monitor = flx_interface_monitor_new(s);
257 s->time_event_queue = flx_time_event_queue_new(s->context);
260 hn = flx_get_host_name();
261 if ((e = strchr(hn, '.')))
264 s->hostname = g_strdup_printf("%s.local.", hn);
267 add_default_entries(s);
269 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
270 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
272 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
273 s->pollfd_ipv4.fd = s->fd_ipv4;
274 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
275 g_source_add_poll(s->source, &s->pollfd_ipv4);
277 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
278 s->pollfd_ipv6.fd = s->fd_ipv6;
279 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
280 g_source_add_poll(s->source, &s->pollfd_ipv6);
282 g_source_attach(s->source, s->context);
287 void flx_server_free(flxServer* s) {
290 flx_interface_monitor_free(s->monitor);
292 flx_server_remove(s, 0);
294 g_hash_table_destroy(s->rrset_by_id);
295 g_hash_table_destroy(s->rrset_by_name);
297 flx_time_event_queue_free(s->time_event_queue);
306 g_source_destroy(s->source);
307 g_source_unref(s->source);
308 g_main_context_unref(s->context);
313 gint flx_server_get_next_id(flxServer *s) {
316 return s->current_id++;
331 e = g_new(flxEntry, 1);
332 e->record = flx_record_ref(r);
334 e->interface = interface;
335 e->protocol = protocol;
338 FLX_LLIST_PREPEND(flxEntry, entry, s->entries, e);
340 /* Insert into hash table indexed by id */
341 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
342 FLX_LLIST_PREPEND(flxEntry, by_id, t, e);
343 g_hash_table_replace(s->rrset_by_id, &e->id, t);
345 /* Insert into hash table indexed by name */
346 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
347 FLX_LLIST_PREPEND(flxEntry, by_name, t, e);
348 g_hash_table_replace(s->rrset_by_name, e->record->key, t);
351 void flx_server_add_full(
369 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
370 flx_server_add(s, id, interface, protocol, unique, r);
374 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
375 flxEntry **e = (flxEntry**) state;
380 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
382 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
387 return flx_record_ref((*e)->record);
390 static void free_entry(flxServer*s, flxEntry *e) {
395 /* Remove from linked list */
396 FLX_LLIST_REMOVE(flxEntry, entry, s->entries, e);
398 /* Remove from hash table indexed by id */
399 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
400 FLX_LLIST_REMOVE(flxEntry, by_id, t, e);
402 g_hash_table_replace(s->rrset_by_id, &t->id, t);
404 g_hash_table_remove(s->rrset_by_id, &e->id);
406 /* Remove from hash table indexed by name */
407 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
408 FLX_LLIST_REMOVE(flxEntry, by_name, t, e);
410 g_hash_table_replace(s->rrset_by_name, t->record->key, t);
412 g_hash_table_remove(s->rrset_by_name, e->record->key);
414 flx_record_unref(e->record);
418 void flx_server_remove(flxServer *s, gint id) {
423 free_entry(s, s->entries);
427 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
432 void flx_server_dump(flxServer *s, FILE *f) {
437 fprintf(f, ";;; ZONE DUMP FOLLOWS ;;;\n");
439 for (e = s->entries; e; e = e->entry_next) {
442 t = flx_record_to_string(e->record);
443 fprintf(f, "%s\n", t);
447 flx_dump_caches(s, f);
450 void flx_server_add_address(
463 n = name ? flx_normalize_name(name) : s->hostname;
465 if (a->family == AF_INET) {
468 flx_server_add_full(s, id, interface, protocol, unique, n, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_A, &a->ipv4, sizeof(a->ipv4), FLX_DEFAULT_TTL);
470 r = flx_reverse_lookup_name_ipv4(&a->ipv4);
472 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);
478 flx_server_add_full(s, id, interface, protocol, unique, n, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_AAAA, &a->ipv6, sizeof(a->ipv6), FLX_DEFAULT_TTL);
480 r = flx_reverse_lookup_name_ipv6_arpa(&a->ipv6);
482 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 r = flx_reverse_lookup_name_ipv6_int(&a->ipv6);
487 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 void flx_server_add_text(
506 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, text, strlen(text), FLX_DEFAULT_TTL);
509 void flx_server_send_query(flxServer *s, gint interface, guchar protocol, flxKey *k) {
513 if (interface <= 0) {
516 for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
517 flx_interface_post_query(i, protocol, k);
522 if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
525 flx_interface_post_query(i, protocol, k);