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