]> git.meshlink.io Git - catta/blob - server.c
* add cache management
[catta] / server.c
1 #include <sys/socket.h>
2 #include <arpa/inet.h>
3 #include <string.h>
4 #include <sys/utsname.h>
5 #include <unistd.h>
6
7 #include "server.h"
8 #include "util.h"
9 #include "iface.h"
10 #include "socket.h"
11
12 static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) {
13     flxEntry *e;
14     gchar *txt;
15     
16     g_assert(s);
17     g_assert(k);
18     g_assert(i);
19     g_assert(a);
20
21     g_message("Handling query: %s", txt = flx_key_to_string(k));
22     g_free(txt);
23
24     for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) {
25
26         if ((e->interface <= 0 || e->interface == i->index) &&
27             (e->protocol == AF_UNSPEC || e->protocol == a->family)) {
28
29             flx_interface_post_response(i, a->family, e->record);
30         }
31     }
32 }
33
34 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
35     guint n;
36     
37     g_assert(s);
38     g_assert(p);
39     g_assert(i);
40     g_assert(a);
41
42     for (n = flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT); n > 0; n --) {
43         flxKey *key;
44
45         if (!(key = flx_dns_packet_consume_key(p))) {
46             g_warning("Packet too short");
47             return;
48         }
49
50         handle_query_key(s, key, i, a);
51         flx_key_unref(key);
52     }
53 }
54
55 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
56     guint n;
57     
58     g_assert(s);
59     g_assert(p);
60     g_assert(i);
61     g_assert(a);
62     
63     for (n = flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) +
64              flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT); n > 0; n--) {
65         flxRecord *record;
66         gboolean cache_flush = FALSE;
67         gchar *txt;
68         
69         if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
70             g_warning("Packet too short");
71             return;
72         }
73
74         g_message("Handling response: %s", txt = flx_record_to_string(record));
75         g_free(txt);
76
77         flx_cache_update(a->family == AF_INET ? i->ipv4_cache : i->ipv6_cache, record, cache_flush, a);
78         flx_packet_scheduler_drop_response(a->family == AF_INET ? i->ipv4_scheduler : i->ipv6_scheduler, record);
79         flx_record_unref(record);
80     }
81 }
82
83 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
84     flxInterface *i;
85     flxAddress a;
86     
87     g_assert(s);
88     g_assert(p);
89     g_assert(sa);
90     g_assert(iface > 0);
91
92     g_message("new packet recieved.");
93
94     if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) {
95         g_warning("Recieved packet from invalid interface.");
96         return;
97     }
98
99     if (ttl != 255) {
100         g_warning("Recieved packet with invalid TTL on interface '%s'.", i->name);
101         return;
102     }
103
104     if (sa->sa_family == AF_INET6) {
105         static const unsigned char ipv4_in_ipv6[] = {
106             0x00, 0x00, 0x00, 0x00,
107             0x00, 0x00, 0x00, 0x00,
108             0xFF, 0xFF, 0xFF, 0xFF };
109
110         if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
111
112             /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
113             return;
114         }
115     }
116
117     if (flx_dns_packet_check_valid(p) < 0) {
118         g_warning("Recieved invalid packet.");
119         return;
120     }
121
122     flx_address_from_sockaddr(sa, &a);
123
124     if (flx_dns_packet_is_query(p)) {
125
126         if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 ||
127             flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 ||
128             flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
129             g_warning("Invalid query packet.");
130             return;
131         }
132                 
133         handle_query(s, p, i, &a);    
134         g_message("Handled query");
135     } else {
136         if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) != 0 ||
137             flx_dns_packet_get_field(p, DNS_FIELD_ANCOUNT) == 0 ||
138             flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) {
139             g_warning("Invalid response packet.");
140             return;
141         }
142
143         handle_response(s, p, i, &a);
144         g_message("Handled response");
145     }
146 }
147
148 static gboolean work(flxServer *s) {
149     struct sockaddr_in6 sa6;
150     struct sockaddr_in sa;
151     flxDnsPacket *p;
152     gint iface = -1;
153     guint8 ttl;
154         
155     g_assert(s);
156
157     if (s->pollfd_ipv4.revents & G_IO_IN) {
158         if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl)))
159             dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
160     }
161
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     }
166
167     return TRUE;
168 }
169
170 static gboolean prepare_func(GSource *source, gint *timeout) {
171     g_assert(source);
172     g_assert(timeout);
173     
174     *timeout = -1;
175     return FALSE;
176 }
177
178 static gboolean check_func(GSource *source) {
179     flxServer* s;
180     g_assert(source);
181
182     s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
183     g_assert(s);
184     
185     return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
186 }
187
188 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
189     flxServer* s;
190     g_assert(source);
191
192     s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
193     g_assert(s);
194     
195     return work(s);
196 }
197
198 static void add_default_entries(flxServer *s) {
199     gint length = 0;
200     struct utsname utsname;
201     gchar *hinfo;
202     flxAddress a;
203     
204     g_assert(s);
205     
206     /* Fill in HINFO rr */
207     uname(&utsname);
208     hinfo = g_strdup_printf("%s%c%s%n", g_strup(utsname.machine), 0, g_strup(utsname.sysname), &length);
209     
210     flx_server_add_full(s, 0, 0, AF_UNSPEC, TRUE,
211                         s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO, hinfo, length+1, FLX_DEFAULT_TTL);
212
213     g_free(hinfo);
214
215     /* Add localhost entries */
216     flx_address_parse("127.0.0.1", AF_INET, &a);
217     flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "localhost", &a);
218
219     flx_address_parse("::1", AF_INET6, &a);
220     flx_server_add_address(s, 0, 0, AF_UNSPEC, TRUE, "ip6-localhost", &a);
221 }
222
223 flxServer *flx_server_new(GMainContext *c) {
224     gchar *hn, *e;
225     flxServer *s;
226     
227     static GSourceFuncs source_funcs = {
228         prepare_func,
229         check_func,
230         dispatch_func,
231         NULL,
232         NULL,
233         NULL
234     };
235
236     s = g_new(flxServer, 1);
237
238     s->fd_ipv4 = flx_open_socket_ipv4();
239     s->fd_ipv6 = flx_open_socket_ipv6();
240     
241     if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
242         g_critical("Failed to create sockets.\n");
243         g_free(s);
244         return NULL;
245     }
246
247     if (s->fd_ipv4 < 0)
248         g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
249     else if (s->fd_ipv6 < 0)
250         g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
251     
252     if (c)
253         g_main_context_ref(s->context = c);
254     else
255         s->context = g_main_context_default();
256     
257     s->current_id = 1;
258     s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal);
259     s->rrset_by_name = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
260
261     FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
262
263     s->monitor = flx_interface_monitor_new(s);
264     s->time_event_queue = flx_time_event_queue_new(s->context);
265     
266     /* Get host name */
267     hn = flx_get_host_name();
268     if ((e = strchr(hn, '.')))
269         *e = 0;
270
271     s->hostname = g_strdup_printf("%s.local.", hn);
272     g_free(hn);
273
274     add_default_entries(s);
275
276     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
277     *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
278
279     memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
280     s->pollfd_ipv4.fd = s->fd_ipv4;
281     s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
282     g_source_add_poll(s->source, &s->pollfd_ipv4);
283     
284     memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
285     s->pollfd_ipv6.fd = s->fd_ipv6;
286     s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
287     g_source_add_poll(s->source, &s->pollfd_ipv6);
288
289     g_source_attach(s->source, s->context);
290     
291     return s;
292 }
293
294 void flx_server_free(flxServer* s) {
295     g_assert(s);
296
297     flx_interface_monitor_free(s->monitor);
298     
299     flx_server_remove(s, 0);
300     
301     g_hash_table_destroy(s->rrset_by_id);
302     g_hash_table_destroy(s->rrset_by_name);
303
304     flx_time_event_queue_free(s->time_event_queue);
305
306     if (s->fd_ipv4 >= 0)
307         close(s->fd_ipv4);
308     if (s->fd_ipv6 >= 0)
309         close(s->fd_ipv6);
310     
311     g_free(s->hostname);
312
313     g_source_destroy(s->source);
314     g_source_unref(s->source);
315     g_main_context_unref(s->context);
316
317     g_free(s);
318 }
319
320 gint flx_server_get_next_id(flxServer *s) {
321     g_assert(s);
322
323     return s->current_id++;
324 }
325
326 void flx_server_add(
327     flxServer *s,
328     gint id,
329     gint interface,
330     guchar protocol,
331     gboolean unique,
332     flxRecord *r) {
333     
334     flxEntry *e, *t;
335     g_assert(s);
336     g_assert(r);
337
338     e = g_new(flxEntry, 1);
339     e->record = flx_record_ref(r);
340     e->id = id;
341     e->interface = interface;
342     e->protocol = protocol;
343     e->unique = unique;
344
345     FLX_LLIST_PREPEND(flxEntry, entry, s->entries, e);
346
347     /* Insert into hash table indexed by id */
348     t = g_hash_table_lookup(s->rrset_by_id, &e->id);
349     FLX_LLIST_PREPEND(flxEntry, by_id, t, e);
350     g_hash_table_replace(s->rrset_by_id, &e->id, t);
351     
352     /* Insert into hash table indexed by name */
353     t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
354     FLX_LLIST_PREPEND(flxEntry, by_name, t, e);
355     g_hash_table_replace(s->rrset_by_name, e->record->key, t);
356 }
357
358 void flx_server_add_full(
359     flxServer *s,
360     gint id,
361     gint interface,
362     guchar protocol,
363     gboolean unique,
364     const gchar *name,
365     guint16 class,
366     guint16 type,
367     gconstpointer data,
368     guint size,
369     guint32 ttl) {
370     
371     flxRecord *r;
372     g_assert(s);
373     g_assert(data);
374     g_assert(size);
375
376     r = flx_record_new_full(name ? name : s->hostname, class, type, data, size, ttl);
377     flx_server_add(s, id, interface, protocol, unique, r);
378     flx_record_unref(r);
379 }
380
381 const flxRecord *flx_server_iterate(flxServer *s, gint id, void **state) {
382     flxEntry **e = (flxEntry**) state;
383     g_assert(s);
384     g_assert(e);
385
386     if (e)
387         *e = id > 0 ? (*e)->by_id_next : (*e)->entry_next;
388     else
389         *e = id > 0 ? g_hash_table_lookup(s->rrset_by_id, &id) : s->entries;
390         
391     if (!*e)
392         return NULL;
393
394     return flx_record_ref((*e)->record);
395 }
396
397 static void free_entry(flxServer*s, flxEntry *e) {
398     flxEntry *t;
399     
400     g_assert(e);
401
402     /* Remove from linked list */
403     FLX_LLIST_REMOVE(flxEntry, entry, s->entries, e);
404
405     /* Remove from hash table indexed by id */
406     t = g_hash_table_lookup(s->rrset_by_id, &e->id);
407     FLX_LLIST_REMOVE(flxEntry, by_id, t, e);
408     if (t)
409         g_hash_table_replace(s->rrset_by_id, &t->id, t);
410     else
411         g_hash_table_remove(s->rrset_by_id, &e->id);
412     
413     /* Remove from hash table indexed by name */
414     t = g_hash_table_lookup(s->rrset_by_name, e->record->key);
415     FLX_LLIST_REMOVE(flxEntry, by_name, t, e);
416     if (t)
417         g_hash_table_replace(s->rrset_by_name, t->record->key, t);
418     else
419         g_hash_table_remove(s->rrset_by_name, e->record->key);
420
421     flx_record_unref(e->record);
422     g_free(e);
423 }
424
425 void flx_server_remove(flxServer *s, gint id) {
426     g_assert(s);
427
428     if (id <= 0) {
429         while (s->entries)
430             free_entry(s, s->entries);
431     } else {
432         flxEntry *e;
433
434         while ((e = g_hash_table_lookup(s->rrset_by_id, &id)))
435             free_entry(s, e);
436     }
437 }
438
439 void flx_server_dump(flxServer *s, FILE *f) {
440     flxEntry *e;
441     g_assert(s);
442     g_assert(f);
443
444     fprintf(f, ";;; ZONE DUMP FOLLOWS ;;;\n");
445
446     for (e = s->entries; e; e = e->entry_next) {
447         gchar *t;
448
449         t = flx_record_to_string(e->record);
450         fprintf(f, "%s\n", t);
451         g_free(t);
452     }
453
454     flx_dump_caches(s, f);
455 }
456
457 void flx_server_add_address(
458     flxServer *s,
459     gint id,
460     gint interface,
461     guchar protocol,
462     gboolean unique,
463     const gchar *name,
464     flxAddress *a) {
465
466     gchar *n;
467     g_assert(s);
468     g_assert(a);
469
470     n = name ? flx_normalize_name(name) : s->hostname;
471     
472     if (a->family == AF_INET) {
473         gchar *r;
474         
475         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);
476
477         r = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
478         g_assert(r);
479         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);
480         g_free(r);
481         
482     } else {
483         gchar *r;
484             
485         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);
486
487         r = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
488         g_assert(r);
489         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);
490         g_free(r);
491     
492         r = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
493         g_assert(r);
494         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);
495         g_free(r);
496     }
497     
498     g_free(n);
499 }
500
501 void flx_server_add_text(
502     flxServer *s,
503     gint id,
504     gint interface,
505     guchar protocol,
506     gboolean unique,
507     const gchar *name,
508     const gchar *text) {
509     
510     g_assert(s);
511     g_assert(text);
512
513     flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, text, strlen(text), FLX_DEFAULT_TTL);
514 }
515
516 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *k) {
517     g_assert(s);
518     g_assert(k);
519
520     if (interface <= 0) {
521         flxInterface *i;
522
523         for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next)
524             flx_interface_post_query(i, protocol, k);
525         
526     } else {
527         flxInterface *i;
528
529         if (!(i = flx_interface_monitor_get_interface(s->monitor, interface)))
530             return;
531
532         flx_interface_post_query(i, protocol, k);
533     }
534 }