#include <arpa/inet.h>
#include <string.h>
#include <sys/utsname.h>
+#include <unistd.h>
#include "server.h"
#include "util.h"
#include "iface.h"
+#include "socket.h"
+
+static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) {
+ flxEntry *e;
+
+ g_assert(s);
+ g_assert(k);
+ g_assert(i);
+ g_assert(a);
+
+ for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) {
+
+ if ((e->interface <= 0 || e->interface == i->index) &&
+ (e->protocol == AF_UNSPEC || e->protocol == a->family)) {
+
+ flx_interface_post_response(i, a->family, e->record);
+ }
+ }
+}
+
+static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
+ guint n;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(i);
+ g_assert(a);
+
+ for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
+ flxKey *key;
+
+ if (!(key = flx_dns_packet_consume_key(p))) {
+ g_warning("Packet too short");
+ return;
+ }
+
+ handle_query_key(s, key, i, a);
+ flx_key_unref(key);
+ }
+}
+
+static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
+ guint n;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(i);
+ g_assert(a);
+
+ for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT); n > 0; n--) {
+ flxRecord *record;
+ gboolean cache_flush = FALSE;
+
+ if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
+ g_warning("Packet too short");
+ return;
+ }
+
+ flx_cache_update(a->family == AF_INET ? i->ipv4_cache : i->ipv6_cache, record, cache_flush, a);
+ flx_packet_scheduler_drop_response(a->family == AF_INET ? i->ipv4_scheduler : i->ipv6_scheduler, record);
+ flx_record_unref(record);
+ }
+}
+
+static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
+ flxInterface *i;
+ flxAddress a;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(sa);
+ g_assert(iface > 0);
+
+ g_message("new packet recieved.");
+
+ if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) {
+ g_warning("Recieved packet from invalid interface.");
+ return;
+ }
+
+ if (ttl != 255) {
+ g_warning("Recieved packet with invalid TTL on interface '%s'.", i->name);
+ return;
+ }
+
+ if (sa->sa_family == AF_INET6) {
+ static const unsigned char ipv4_in_ipv6[] = {
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0xFF, 0xFF, 0xFF, 0xFF };
+
+ if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
+
+ /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+ return;
+ }
+ }
+
+ if (flx_dns_packet_check_valid(p) < 0) {
+ g_warning("Recieved invalid packet.");
+ return;
+ }
+
+ flx_address_from_sockaddr(sa, &a);
+
+
+ if (flx_dns_packet_is_query(p)) {
+
+ if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
+ flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
+ flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
+ g_warning("Invalid query packet.");
+ return;
+ }
+
+ handle_query(s, p, i, &a);
+ g_message("Handled query");
+ } else {
+ if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
+ flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
+ flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0 ||
+ flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0) {
+ g_warning("Invalid response packet.");
+ return;
+ }
+
+ handle_response(s, p, i, &a);
+ g_message("Handled response");
+ }
+}
+
+static gboolean work(flxServer *s) {
+ struct sockaddr_in6 sa6;
+ struct sockaddr_in sa;
+ flxDnsPacket *p;
+ gint iface = -1;
+ guint8 ttl;
+
+ g_assert(s);
+
+ if (s->pollfd_ipv4.revents & G_IO_IN) {
+ if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
+ dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
+ }
+
+ if (s->pollfd_ipv6.revents & G_IO_IN) {
+ if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl)))
+ dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
+ }
+
+ return TRUE;
+}
+
+static gboolean prepare_func(GSource *source, gint *timeout) {
+ g_assert(source);
+ g_assert(timeout);
+
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean check_func(GSource *source) {
+ flxServer* s;
+ g_assert(source);
+
+ s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
+ g_assert(s);
+
+ return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
+}
+
+static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
+ flxServer* s;
+ g_assert(source);
+
+ s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
+ g_assert(s);
+
+ return work(s);
+}
static void add_default_entries(flxServer *s) {
gint length = 0;
flxServer *flx_server_new(GMainContext *c) {
gchar *hn, *e;
flxServer *s;
+
+ static GSourceFuncs source_funcs = {
+ prepare_func,
+ check_func,
+ dispatch_func,
+ NULL,
+ NULL,
+ NULL
+ };
s = g_new(flxServer, 1);
+ s->fd_ipv4 = flx_open_socket_ipv4();
+ s->fd_ipv6 = flx_open_socket_ipv6();
+
+ if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
+ g_critical("Failed to create sockets.\n");
+ g_free(s);
+ return NULL;
+ }
+
+ if (s->fd_ipv4 < 0)
+ g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
+ else if (s->fd_ipv6 < 0)
+ g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
+
if (c)
g_main_context_ref(s->context = c);
else
add_default_entries(s);
+ s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
+ *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
+
+ memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
+ s->pollfd_ipv4.fd = s->fd_ipv4;
+ s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
+ g_source_add_poll(s->source, &s->pollfd_ipv4);
+
+ memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
+ s->pollfd_ipv6.fd = s->fd_ipv6;
+ s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
+ g_source_add_poll(s->source, &s->pollfd_ipv6);
+
+ g_source_attach(s->source, s->context);
+
return s;
}
g_hash_table_destroy(s->rrset_by_name);
flx_time_event_queue_free(s->time_event_queue);
- g_main_context_unref(s->context);
+
+ if (s->fd_ipv4 >= 0)
+ close(s->fd_ipv4);
+ if (s->fd_ipv6 >= 0)
+ close(s->fd_ipv6);
g_free(s->hostname);
+
+ g_source_destroy(s->source);
+ g_source_unref(s->source);
+ g_main_context_unref(s->context);
+
g_free(s);
}
g_assert(s);
g_assert(f);
+ fprintf(f, ";;; ZONE DUMP FOLLOWS ;;;\n");
+
for (e = s->entries; e; e = e->entry_next) {
gchar *t;
fprintf(f, "%s\n", t);
g_free(t);
}
+
+ flx_dump_caches(s, f);
}
void flx_server_add_address(
flxInterface *i;
for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
- flx_interface_send_query(i, protocol, k);
+ flx_interface_post_query(i, protocol, k);
} else {
flxInterface *i;
if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
return;
- flx_interface_send_query(i, protocol, k);
+ flx_interface_post_query(i, protocol, k);
}
}