bug
[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
33 struct dns_cls {
34         struct GNUNET_SCHEDULER_Handle *sched;
35
36         struct GNUNET_NETWORK_Handle *dnsout;
37
38         unsigned short dnsoutport;
39 };
40
41 static struct dns_cls mycls;
42
43 void hijack(unsigned short port) {
44         char port_s[6];
45
46         snprintf(port_s, 6, "%d", port);
47         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
48 }
49
50 void unhijack(unsigned short port) {
51         char port_s[6];
52
53         snprintf(port_s, 6, "%d", port);
54         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
55 }
56
57 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
58 {
59         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received query!\n");
60         struct query_packet* pkt = (struct query_packet*)message;
61
62         GNUNET_SERVER_receive_done(client, GNUNET_OK);
63 }
64
65 /**
66  * Task run during shutdown.
67  *
68  * @param cls unused
69  * @param tc unused
70  */
71 static void
72 cleanup_task (void *cls,
73               const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75         unhijack(mycls.dnsoutport);
76 }
77
78 /**
79  * @param cls closure
80  * @param sched scheduler to use
81  * @param server the initialized server
82  * @param cfg configuration to use
83  */
84 static void
85 run (void *cls,
86      struct GNUNET_SCHEDULER_Handle *sched,
87      struct GNUNET_SERVER_Handle *server,
88      const struct GNUNET_CONFIGURATION_Handle *cfg)
89 {
90   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
91           /* callback, cls, type, size */
92     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
93     {NULL, NULL, 0, 0}
94   };
95
96   mycls.sched = sched;
97
98   mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
99
100   struct sockaddr_in * addr = alloca(sizeof(struct sockaddr_in));
101   memset(addr, 0, sizeof(struct sockaddr_in));
102
103   int err = GNUNET_NETWORK_socket_bind (mycls.dnsout, (struct sockaddr*)addr, sizeof(struct sockaddr_in));
104   err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout), addr, (unsigned int[]){sizeof(struct sockaddr_in)});
105
106   mycls.dnsoutport = htons(addr->sin_port);
107
108   hijack(htons(addr->sin_port));
109
110   GNUNET_SERVER_add_handlers (server, handlers);
111   GNUNET_SCHEDULER_add_delayed (sched,
112                   GNUNET_TIME_UNIT_FOREVER_REL,
113                   &cleanup_task,
114                   cls);
115 }
116
117 /**
118  * The main function for the dns service.
119  *
120  * @param argc number of arguments from the command line
121  * @param argv command line arguments
122  * @return 0 ok, 1 on error
123  */
124 int
125 main (int argc, char *const *argv)
126 {
127   return (GNUNET_OK ==
128           GNUNET_SERVICE_run (argc,
129                               argv,
130                               "gnunet-service-dns",
131                               GNUNET_SERVICE_OPTION_NONE,
132                               &run, NULL)) ? 0 : 1;
133 }