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