1 #include <sys/socket.h>
4 #include <sys/utsname.h>
11 #include "subscribe.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);
76 flx_packet_scheduler_drop_response(i->scheduler, record);
77 flx_record_unref(record);
81 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
90 g_message("new packet recieved.");
92 if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
93 g_warning("Recieved packet from invalid interface.");
98 g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol);
102 if (sa->sa_family == AF_INET6) {
103 static const unsigned char ipv4_in_ipv6[] = {
104 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00,
106 0xFF, 0xFF, 0xFF, 0xFF };
108 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
110 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
115 if (flx_dns_packet_check_valid(p) < 0) {
116 g_warning("Recieved invalid packet.");
120 flx_address_from_sockaddr(sa, &a);
122 if (flx_dns_packet_is_query(p)) {
124 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
125 flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
126 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
127 g_warning("Invalid query packet.");
131 handle_query(s, p, i, &a);
132 g_message("Handled query");
134 if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
135 flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
136 flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
137 g_warning("Invalid response packet.");
141 handle_response(s, p, i, &a);
142 g_message("Handled response");
146 static gboolean work(flxServer *s) {
147 struct sockaddr_in6 sa6;
148 struct sockaddr_in sa;
155 if (s->pollfd_ipv4.revents & G_IO_IN) {
156 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
157 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
158 flx_dns_packet_free(p);
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);
165 flx_dns_packet_free(p);
172 static gboolean prepare_func(GSource *source, gint *timeout) {
180 static gboolean check_func(GSource *source) {
184 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
187 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
190 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
194 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
200 static void add_default_entries(flxServer *s) {
202 struct utsname utsname;
208 /* Fill in HINFO rr */
210 hinfo = g_strdup_printf("%c%s%c%s%n",
211 strlen(utsname.machine), g_strup(utsname.machine),
212 strlen(utsname.sysname), g_strup(utsname.sysname),
215 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
216 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length, FLX_DEFAULT_TTL);
220 /* Add localhost entries */
221 flx_address_parse("127.0.0.1", AF_INET, &a);
222 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
224 flx_address_parse("::1", AF_INET6, &a);
225 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
228 flxServer *flx_server_new(GMainContext *c) {
232 static GSourceFuncs source_funcs = {
241 s = g_new(flxServer, 1);
243 s->fd_ipv4 = flx_open_socket_ipv4();
244 s->fd_ipv6 = flx_open_socket_ipv6();
246 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
247 g_critical("Failed to create sockets.\n");
253 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
254 else if (s->fd_ipv6 < 0)
255 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
258 g_main_context_ref(s->context = c);
260 s->context = g_main_context_default();
264 FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries);
265 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
266 s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
268 FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions);
269 s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
271 s->monitor = flx_interface_monitor_new(s);
272 s->time_event_queue = flx_time_event_queue_new(s->context);
275 hn = flx_get_host_name();
276 if ((e = strchr(hn, '.')))
279 s->hostname = g_strdup_printf("%s.local.", hn);
282 add_default_entries(s);
284 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
285 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
287 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
288 s->pollfd_ipv4.fd = s->fd_ipv4;
289 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
290 g_source_add_poll(s->source, &s->pollfd_ipv4);
292 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
293 s->pollfd_ipv6.fd = s->fd_ipv6;
294 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
295 g_source_add_poll(s->source, &s->pollfd_ipv6);
297 g_source_attach(s->source, s->context);
302 void flx_server_free(flxServer* s) {
305 flx_interface_monitor_free(s->monitor);
307 flx_server_remove(s, 0);
309 while (s->subscriptions)
310 flx_subscription_free(s->subscriptions);
311 g_hash_table_destroy(s->subscription_hashtable);
313 g_hash_table_destroy(s->rrset_by_id);
314 g_hash_table_destroy(s->rrset_by_key);
316 flx_time_event_queue_free(s->time_event_queue);
325 g_source_destroy(s->source);
326 g_source_unref(s->source);
327 g_main_context_unref(s->context);
332 gint flx_server_get_next_id(flxServer *s) {
335 return s->current_id++;
346 flxServerEntry *e, *t;
350 e = g_new(flxServerEntry, 1);
351 e->record = flx_record_ref(r);
353 e->interface = interface;
354 e->protocol = protocol;
357 FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements);
359 FLX_LLIST_PREPEND(flxServerEntry, entry, s->entries, e);
361 /* Insert into hash table indexed by id */
362 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
363 FLX_LLIST_PREPEND(flxServerEntry, by_id, t, e);
364 g_hash_table_replace(s->rrset_by_id, &e->id, t);
366 /* Insert into hash table indexed by name */
367 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
368 FLX_LLIST_PREPEND(flxServerEntry, by_key, t, e);
369 g_hash_table_replace(s->rrset_by_key, e->record->key, t);
371 flx_announce_entry(s, e);
374 void flx_server_add_full(
392 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
393 flx_server_add(s, id, interface, protocol, unique, r);
397 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
398 flxServerEntry **e = (flxServerEntry**) state;
403 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
405 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
410 return flx_record_ref((*e)->record);
413 static void free_entry(flxServer*s, flxServerEntry *e) {
418 flx_goodbye_entry(s, e, TRUE);
420 /* Remove from linked list */
421 FLX_LLIST_REMOVE(flxServerEntry, entry, s->entries, e);
423 /* Remove from hash table indexed by id */
424 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
425 FLX_LLIST_REMOVE(flxServerEntry, by_id, t, e);
427 g_hash_table_replace(s->rrset_by_id, &t->id, t);
429 g_hash_table_remove(s->rrset_by_id, &e->id);
431 /* Remove from hash table indexed by name */
432 t = g_hash_table_lookup(s->rrset_by_key, e->record->key);
433 FLX_LLIST_REMOVE(flxServerEntry, by_key, t, e);
435 g_hash_table_replace(s->rrset_by_key, t->record->key, t);
437 g_hash_table_remove(s->rrset_by_key, e->record->key);
439 flx_record_unref(e->record);
443 void flx_server_remove(flxServer *s, gint id) {
448 free_entry(s, s->entries);
452 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
457 void flx_server_dump(flxServer *s, FILE *f) {
462 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
464 for (e = s->entries; e; e = e->entry_next) {
467 t = flx_record_to_string(e->record);
468 fprintf(f, "%s\n", t);
472 flx_dump_caches(s->monitor, f);
475 void flx_server_add_address(
488 n = name ? flx_normalize_name(name) : s->hostname;
490 if (a->family == AF_INET) {
493 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);
495 r = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
497 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);
503 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);
505 r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
507 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 r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
512 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);
519 void flx_server_add_text(
534 if ((l = strlen(text)) > 255)
539 memcpy(buf+1, text, l);
541 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, buf, l+1, FLX_DEFAULT_TTL);
544 static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
545 flxKey *k = userdata;
551 flx_interface_post_query(i, k);
554 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) {
558 flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
561 static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
562 flxRecord *r = userdata;
568 flx_interface_post_response(i, r);
571 void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record) {
575 flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, record);