2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file vpn/gnunet-service-dns.c
23 * @author Philipp Tölke
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"
35 #define WE_DO_THIS_TO_GET_THE_PORT_WE_BOUND_TO_IN_ORDER_TO_HIJACK_EVERYTHING_BUT_THIS_PORT 1
36 #define WHY_ON_EARTH_DO_WE_DO_THIS WE_DO_THIS_TO_GET_THE_PORT_WE_BOUND_TO_IN_ORDER_TO_HIJACK_EVERYTHING_BUT_THIS_PORT
39 struct GNUNET_SCHEDULER_Handle *sched;
41 struct GNUNET_NETWORK_Handle *dnsout;
43 unsigned short dnsoutport;
46 static struct dns_cls mycls;
48 void hijack(unsigned short port) {
51 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
52 snprintf(port_s, 6, "%d", port);
53 GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
56 void unhijack(unsigned short port) {
59 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
60 snprintf(port_s, 6, "%d", port);
61 GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
64 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
66 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received query!\n");
67 struct query_packet* pkt = (struct query_packet*)message;
68 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Of length %d\n", ntohs(pkt->hdr.size));
69 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
73 GNUNET_SERVER_receive_done(client, GNUNET_OK);
77 * Task run during shutdown.
83 cleanup_task (void *cls,
84 const struct GNUNET_SCHEDULER_TaskContext *tc)
86 unhijack(mycls.dnsoutport);
91 * @param sched scheduler to use
92 * @param server the initialized server
93 * @param cfg configuration to use
97 struct GNUNET_SCHEDULER_Handle *sched,
98 struct GNUNET_SERVER_Handle *server,
99 const struct GNUNET_CONFIGURATION_Handle *cfg)
101 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
102 /* callback, cls, type, size */
103 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
106 struct sockaddr_in addr;
109 mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
110 if (mycls.dnsout == NULL)
112 memset(&addr, 0, sizeof(struct sockaddr_in));
114 int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
115 (struct sockaddr*)&addr,
116 sizeof(struct sockaddr_in));
118 if (err != GNUNET_YES) {
119 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
122 #if WHY_ON_EARTH_DO_WE_DO_THIS
123 socklen_t addrlen = sizeof(struct sockaddr_in);
124 err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
125 (struct sockaddr*) &addr,
129 mycls.dnsoutport = htons(addr.sin_port);
131 hijack(htons(addr.sin_port));
133 GNUNET_SERVER_add_handlers (server, handlers);
134 GNUNET_SCHEDULER_add_delayed (sched,
135 GNUNET_TIME_UNIT_FOREVER_REL,
141 * The main function for the dns service.
143 * @param argc number of arguments from the command line
144 * @param argv command line arguments
145 * @return 0 ok, 1 on error
148 main (int argc, char *const *argv)
151 GNUNET_SERVICE_run (argc,
153 "gnunet-service-dns",
154 GNUNET_SERVICE_OPTION_NONE,
155 &run, NULL)) ? 0 : 1;