1 #include <sys/socket.h>
4 #include <sys/utsname.h>
12 static void post_response(flxServer *s, flxRecord *r, gint iface, const flxAddress *a) {
20 if ((i = flx_interface_monitor_get_interface(s->monitor, iface)))
21 flx_interface_send_response(i, a->family, r);
25 static void handle_query_key(flxServer *s, flxKey *k, gint iface, const flxAddress *a) {
32 for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) {
34 if ((e->interface <= 0 || e->interface == iface) &&
35 (e->protocol == AF_UNSPEC || e->protocol == a->family)) {
36 post_response(s, e->record, iface, a);
42 static void handle_query(flxServer *s, flxDnsPacket *p, gint iface, const flxAddress *a) {
49 for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
53 if (!(key = flx_dns_packet_consume_key(p))) {
54 g_warning("Packet too short");
58 handle_query_key(s, key, iface, a);
63 static void add_response_to_cache(flxCache *c, flxDnsPacket *p, const flxAddress *a) {
69 for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT); n > 0; n--) {
72 gboolean cache_flush = FALSE;
74 if (!(rr = flx_dns_packet_consume_record(p, &cache_flush))) {
75 g_warning("Packet too short");
79 flx_cache_update(c, rr, cache_flush, a);
84 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
93 g_message("new packet recieved.");
95 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) {
96 g_warning("Recieved packet from invalid interface.");
101 g_warning("Recieved packet with invalid TTL on interface '%s'.", i->name);
105 if (sa->sa_family == AF_INET6) {
106 static const unsigned char ipv4_in_ipv6[] = {
107 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00,
109 0xFF, 0xFF, 0xFF, 0xFF };
111 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
113 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
118 if (flx_dns_packet_check_valid(p) < 0) {
119 g_warning("Recieved invalid packet.");
124 flx_address_from_sockaddr(sa, &a);
126 if (flx_dns_packet_is_query(p)) {
128 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
129 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
130 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
131 g_warning("Invalid query packet.");
135 handle_query(s, p, iface, &a);
136 g_message("Handled query");
140 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
141 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
142 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0 ||
143 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0) {
144 g_warning("Invalid response packet.");
148 c = a.family == AF_INET ? i->ipv4_cache : i->ipv6_cache;
149 add_response_to_cache(c, p, &a);
151 g_message("Handled responnse");
155 static gboolean work(flxServer *s) {
156 struct sockaddr_in6 sa6;
157 struct sockaddr_in sa;
164 if (s->pollfd_ipv4.revents & G_IO_IN) {
165 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
166 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
169 if (s->pollfd_ipv6.revents & G_IO_IN) {
170 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl)))
171 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
177 static gboolean prepare_func(GSource *source, gint *timeout) {
185 static gboolean check_func(GSource *source) {
189 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
192 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
195 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
199 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
205 static void add_default_entries(flxServer *s) {
207 struct utsname utsname;
213 /* Fill in HINFO rr */
215 hinfo = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
217 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
218 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length+1, FLX_DEFAULT_TTL);
222 /* Add localhost entries */
223 flx_address_parse("127.0.0.1", AF_INET, &a);
224 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
226 flx_address_parse("::1", AF_INET6, &a);
227 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
230 flxServer *flx_server_new(GMainContext *c) {
234 static GSourceFuncs source_funcs = {
243 s = g_new(flxServer, 1);
245 s->fd_ipv4 = flx_open_socket_ipv4();
246 s->fd_ipv6 = flx_open_socket_ipv6();
248 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
249 g_critical("Failed to create sockets.\n");
255 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
256 else if (s->fd_ipv6 < 0)
257 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
260 g_main_context_ref(s->context = c);
262 s->context = g_main_context_default();
265 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
266 s->rrset_by_name = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
268 FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
270 s->monitor = flx_interface_monitor_new(s);
271 s->time_event_queue = flx_time_event_queue_new(s->context);
274 hn = flx_get_host_name();
275 if ((e = strchr(hn, '.')))
278 s->hostname = g_strdup_printf("%s.local.", hn);
281 add_default_entries(s);
283 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
284 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
286 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
287 s->pollfd_ipv4.fd = s->fd_ipv4;
288 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
289 g_source_add_poll(s->source, &s->pollfd_ipv4);
291 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
292 s->pollfd_ipv6.fd = s->fd_ipv6;
293 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
294 g_source_add_poll(s->source, &s->pollfd_ipv6);
296 g_source_attach(s->source, s->context);
301 void flx_server_free(flxServer* s) {
304 flx_interface_monitor_free(s->monitor);
306 flx_server_remove(s, 0);
308 g_hash_table_destroy(s->rrset_by_id);
309 g_hash_table_destroy(s->rrset_by_name);
311 flx_time_event_queue_free(s->time_event_queue);
320 g_source_destroy(s->source);
321 g_source_unref(s->source);
322 g_main_context_unref(s->context);
327 gint flx_server_get_next_id(flxServer *s) {
330 return s->current_id++;
345 e = g_new(flxEntry, 1);
346 e->record = flx_record_ref(r);
348 e->interface = interface;
349 e->protocol = protocol;
352 FLX_LLIST_PREPEND(flxEntry, entry, s->entries, e);
354 /* Insert into hash table indexed by id */
355 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
356 FLX_LLIST_PREPEND(flxEntry, by_id, t, e);
357 g_hash_table_replace(s->rrset_by_id, &e->id, t);
359 /* Insert into hash table indexed by name */
360 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
361 FLX_LLIST_PREPEND(flxEntry, by_name, t, e);
362 g_hash_table_replace(s->rrset_by_name, e->record->key, t);
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 flxEntry **e = (flxEntry**) 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, flxEntry *e) {
409 /* Remove from linked list */
410 FLX_LLIST_REMOVE(flxEntry, entry, s->entries, e);
412 /* Remove from hash table indexed by id */
413 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
414 FLX_LLIST_REMOVE(flxEntry, by_id, t, e);
416 g_hash_table_replace(s->rrset_by_id, &t->id, t);
418 g_hash_table_remove(s->rrset_by_id, &e->id);
420 /* Remove from hash table indexed by name */
421 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
422 FLX_LLIST_REMOVE(flxEntry, by_name, t, e);
424 g_hash_table_replace(s->rrset_by_name, t->record->key, t);
426 g_hash_table_remove(s->rrset_by_name, e->record->key);
428 flx_record_unref(e->record);
432 void flx_server_remove(flxServer *s, gint id) {
437 free_entry(s, s->entries);
441 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
446 void flx_server_dump(flxServer *s, FILE *f) {
451 fprintf(f, ";;; ZONE DUMP FOLLOWS ;;;\n");
453 for (e = s->entries; e; e = e->entry_next) {
456 t = flx_record_to_string(e->record);
457 fprintf(f, "%s\n", t);
461 flx_dump_caches(s, f);
464 void flx_server_add_address(
477 n = name ? flx_normalize_name(name) : s->hostname;
479 if (a->family == AF_INET) {
482 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);
484 r = flx_reverse_lookup_name_ipv4(&a->ipv4);
486 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 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);
494 r = flx_reverse_lookup_name_ipv6_arpa(&a->ipv6);
496 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);
499 r = flx_reverse_lookup_name_ipv6_int(&a->ipv6);
501 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);
508 void flx_server_add_text(
520 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, text, strlen(text), FLX_DEFAULT_TTL);
523 void flx_server_send_query(flxServer *s, gint interface, guchar protocol, flxKey *k) {
527 if (interface <= 0) {
530 for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
531 flx_interface_send_query(i, protocol, k);
536 if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
539 flx_interface_send_query(i, protocol, k);