Receive DNS-Responses and print them
[oweals/gnunet.git] / src / vpn / gnunet-service-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-service-dns.c
23  * @author Philipp Tölke
24  */
25 #include "platform.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_service_lib.h"
28 #include "gnunet_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.h"
34
35
36 struct dns_cls {
37         struct GNUNET_SCHEDULER_Handle *sched;
38
39         struct GNUNET_NETWORK_Handle *dnsout;
40
41         unsigned short dnsoutport;
42 };
43
44 static struct dns_cls mycls;
45
46 void hijack(unsigned short port) {
47         char port_s[6];
48
49         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
50         snprintf(port_s, 6, "%d", port);
51         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
52 }
53
54 void unhijack(unsigned short port) {
55         char port_s[6];
56
57         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
58         snprintf(port_s, 6, "%d", port);
59         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
60 }
61
62 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
63 {
64         struct query_packet* pkt = (struct query_packet*)message;
65         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received query of length %d\n", ntohs(pkt->hdr.size));
66         struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
67
68         struct sockaddr_in dest;
69         memset(&dest, 0, sizeof dest);
70         dest.sin_port = htons(53);
71         dest.sin_addr.s_addr = pkt->orig_to;
72         /* TODO:
73          * State merken, damit die Antwort korrekt zurückgeschickt werden kann
74          */
75
76         int r = GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
77         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send %d bytes to socket\n", r);
78
79         GNUNET_SERVER_receive_done(client, GNUNET_OK);
80 }
81
82 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
83         unsigned char buf[65536];
84         struct dns_pkt* dns = (struct dns_pkt*)buf;
85
86         if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
87                 return;
88
89         int r;
90         r = GNUNET_NETWORK_socket_recv(mycls.dnsout, buf, 65536);
91
92         pkt_printf_dns(dns);
93
94         GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
95 }
96
97
98 /**
99  * Task run during shutdown.
100  *
101  * @param cls unused
102  * @param tc unused
103  */
104 static void
105 cleanup_task (void *cls,
106               const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108         unhijack(mycls.dnsoutport);
109 }
110
111 /**
112  * @param cls closure
113  * @param sched scheduler to use
114  * @param server the initialized server
115  * @param cfg configuration to use
116  */
117 static void
118 run (void *cls,
119      struct GNUNET_SCHEDULER_Handle *sched,
120      struct GNUNET_SERVER_Handle *server,
121      const struct GNUNET_CONFIGURATION_Handle *cfg)
122 {
123   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
124           /* callback, cls, type, size */
125     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
126     {NULL, NULL, 0, 0}
127   };
128   struct sockaddr_in addr;
129
130   mycls.sched = sched;
131   mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
132   if (mycls.dnsout == NULL) 
133     return;
134   memset(&addr, 0, sizeof(struct sockaddr_in));
135
136   int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
137                                         (struct sockaddr*)&addr, 
138                                         sizeof(struct sockaddr_in));
139
140   if (err != GNUNET_YES) {
141         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
142         return;
143   }
144   socklen_t addrlen = sizeof(struct sockaddr_in);
145   err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
146                     (struct sockaddr*) &addr, 
147                     &addrlen);
148
149   mycls.dnsoutport = htons(addr.sin_port);
150
151   hijack(htons(addr.sin_port));
152
153         GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
154
155   GNUNET_SERVER_add_handlers (server, handlers);
156   GNUNET_SCHEDULER_add_delayed (sched,
157                   GNUNET_TIME_UNIT_FOREVER_REL,
158                   &cleanup_task,
159                   cls);
160 }
161
162 /**
163  * The main function for the dns service.
164  *
165  * @param argc number of arguments from the command line
166  * @param argv command line arguments
167  * @return 0 ok, 1 on error
168  */
169 int
170 main (int argc, char *const *argv)
171 {
172   return (GNUNET_OK ==
173           GNUNET_SERVICE_run (argc,
174                               argv,
175                               "dns",
176                               GNUNET_SERVICE_OPTION_NONE,
177                               &run, NULL)) ? 0 : 1;
178 }