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