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