1 #include <sys/socket.h>
4 #include <sys/utsname.h>
12 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
19 flxInterface *i = flx_interface_monitor_get_interface(s->monitor, iface);
20 g_warning("Recieved packet with invalid TTL on interface '%s'.", i ? i->name : "unknown");
24 if (sa->sa_family == AF_INET6) {
25 static const unsigned char ipv4_in_ipv6[] = {
26 0x00, 0x00, 0x00, 0x00,
27 0x00, 0x00, 0x00, 0x00,
28 0xFF, 0xFF, 0xFF, 0xFF };
30 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
32 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
37 g_message("Recieved packet");
41 static gboolean work(flxServer *s) {
42 struct sockaddr_in6 sa6;
43 struct sockaddr_in sa;
50 if (s->pollfd_ipv4.revents & G_IO_IN) {
51 if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
52 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
55 if (s->pollfd_ipv6.revents & G_IO_IN) {
56 if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl)))
57 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
63 static gboolean prepare_func(GSource *source, gint *timeout) {
71 static gboolean check_func(GSource *source) {
75 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
78 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
81 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
85 s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
91 static void add_default_entries(flxServer *s) {
93 struct utsname utsname;
99 /* Fill in HINFO rr */
101 hinfo = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
103 flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
104 s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length+1, FLX_DEFAULT_TTL);
108 /* Add localhost entries */
109 flx_address_parse("127.0.0.1", AF_INET, &a);
110 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
112 flx_address_parse("::1", AF_INET6, &a);
113 flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
116 flxServer *flx_server_new(GMainContext *c) {
120 static GSourceFuncs source_funcs = {
129 s = g_new(flxServer, 1);
131 s->fd_ipv4 = flx_open_socket_ipv4();
132 s->fd_ipv6 = flx_open_socket_ipv6();
134 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
135 g_critical("Failed to create sockets.\n");
141 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
142 else if (s->fd_ipv6 < 0)
143 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
146 g_main_context_ref(s->context = c);
148 s->context = g_main_context_default();
151 s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
152 s->rrset_by_name = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
154 FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
156 s->monitor = flx_interface_monitor_new(s);
157 s->time_event_queue = flx_time_event_queue_new(s->context);
160 hn = flx_get_host_name();
161 if ((e = strchr(hn, '.')))
164 s->hostname = g_strdup_printf("%s.local.", hn);
167 add_default_entries(s);
169 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
170 *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
172 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
173 s->pollfd_ipv4.fd = s->fd_ipv4;
174 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
175 g_source_add_poll(s->source, &s->pollfd_ipv4);
177 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
178 s->pollfd_ipv6.fd = s->fd_ipv6;
179 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
180 g_source_add_poll(s->source, &s->pollfd_ipv6);
182 g_source_attach(s->source, s->context);
187 void flx_server_free(flxServer* s) {
190 flx_interface_monitor_free(s->monitor);
192 flx_server_remove(s, 0);
194 g_hash_table_destroy(s->rrset_by_id);
195 g_hash_table_destroy(s->rrset_by_name);
197 flx_time_event_queue_free(s->time_event_queue);
206 g_source_destroy(s->source);
207 g_source_unref(s->source);
208 g_main_context_unref(s->context);
213 gint flx_server_get_next_id(flxServer *s) {
216 return s->current_id++;
231 e = g_new(flxEntry, 1);
232 e->record = flx_record_ref(r);
234 e->interface = interface;
235 e->protocol = protocol;
238 FLX_LLIST_PREPEND(flxEntry, entry, s->entries, e);
240 /* Insert into hash table indexed by id */
241 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
242 FLX_LLIST_PREPEND(flxEntry, by_id, t, e);
243 g_hash_table_replace(s->rrset_by_id, &e->id, t);
245 /* Insert into hash table indexed by name */
246 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
247 FLX_LLIST_PREPEND(flxEntry, by_name, t, e);
248 g_hash_table_replace(s->rrset_by_name, e->record->key, t);
251 void flx_server_add_full(
269 r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
270 flx_server_add(s, id, interface, protocol, unique, r);
274 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
275 flxEntry **e = (flxEntry**) state;
280 *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
282 *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
287 return flx_record_ref((*e)->record);
290 static void free_entry(flxServer*s, flxEntry *e) {
295 /* Remove from linked list */
296 FLX_LLIST_REMOVE(flxEntry, entry, s->entries, e);
298 /* Remove from hash table indexed by id */
299 t = g_hash_table_lookup(s->rrset_by_id, &e->id);
300 FLX_LLIST_REMOVE(flxEntry, by_id, t, e);
302 g_hash_table_replace(s->rrset_by_id, &t->id, t);
304 g_hash_table_remove(s->rrset_by_id, &e->id);
306 /* Remove from hash table indexed by name */
307 t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
308 FLX_LLIST_REMOVE(flxEntry, by_name, t, e);
310 g_hash_table_replace(s->rrset_by_name, t->record->key, t);
312 g_hash_table_remove(s->rrset_by_name, e->record->key);
314 flx_record_unref(e->record);
318 void flx_server_remove(flxServer *s, gint id) {
323 free_entry(s, s->entries);
327 while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
332 void flx_server_dump(flxServer *s, FILE *f) {
337 for (e = s->entries; e; e = e->entry_next) {
340 t = flx_record_to_string(e->record);
341 fprintf(f, "%s\n", t);
346 void flx_server_add_address(
359 n = name ? flx_normalize_name(name) : s->hostname;
361 if (a->family == AF_INET) {
364 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);
366 r = flx_reverse_lookup_name_ipv4(&a->ipv4);
368 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);
374 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);
376 r = flx_reverse_lookup_name_ipv6_arpa(&a->ipv6);
378 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);
381 r = flx_reverse_lookup_name_ipv6_int(&a->ipv6);
383 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);
390 void flx_server_add_text(
402 flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, text, strlen(text), FLX_DEFAULT_TTL);
405 void flx_server_send_query(flxServer *s, gint interface, guchar protocol, flxKey *k) {
409 if (interface <= 0) {
412 for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
413 flx_interface_send_query(i, protocol, k);
418 if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
421 flx_interface_send_query(i, protocol, k);