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