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