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