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"
34 #include "gnunet_container_lib.h"
37 struct GNUNET_SCHEDULER_Handle *sched;
39 struct GNUNET_NETWORK_Handle *dnsout;
41 unsigned short dnsoutport;
43 struct answer_packet_list *head;
44 struct answer_packet_list *tail;
46 static struct dns_cls mycls;
48 struct dns_query_id_state {
50 struct GNUNET_SERVER_Client* client;
52 unsigned local_port:16;
54 static struct dns_query_id_state query_states[65536]; /* This is < 1MiB */
56 void hijack(unsigned short port) {
59 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, 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", port_s, NULL);
64 void unhijack(unsigned short port) {
67 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
68 snprintf(port_s, 6, "%d", port);
69 GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
72 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
74 struct query_packet* pkt = (struct query_packet*)message;
75 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
77 struct sockaddr_in dest;
78 memset(&dest, 0, sizeof dest);
79 dest.sin_port = htons(53);
80 dest.sin_addr.s_addr = pkt->orig_to;
82 query_states[dns->s.id].valid = 1;
83 query_states[dns->s.id].client = client;
84 query_states[dns->s.id].local_ip = pkt->orig_from;
85 query_states[dns->s.id].local_port = pkt->src_port;
87 /* int r = */ GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
89 GNUNET_SERVER_receive_done(client, GNUNET_OK);
92 size_t send_answer(void* cls, size_t size, void* buf) {
93 struct answer_packet_list* query = mycls.head;
94 size_t len = ntohs(query->pkt.hdr.size);
96 GNUNET_assert(len <= size);
98 memcpy(buf, &query->pkt.hdr, len);
100 GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
104 if (mycls.head != NULL) {
105 GNUNET_SERVER_notify_transmit_ready(cls, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, cls);
111 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
112 unsigned char buf[65536];
113 struct dns_pkt* dns = (struct dns_pkt*)buf;
115 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
118 struct sockaddr_in addr;
119 memset(&addr, 0, sizeof addr);
120 unsigned int addrlen = sizeof addr;
123 r = GNUNET_NETWORK_socket_recvfrom(mycls.dnsout, buf, 65536, (struct sockaddr*)&addr, &addrlen);
125 /* if (r < 0) TODO */
127 if (query_states[dns->s.id].valid == 1) {
128 query_states[dns->s.id].valid = 0;
130 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
131 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
132 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
133 answer->pkt.hdr.size = htons(len);
134 answer->pkt.from = addr.sin_addr.s_addr;
135 answer->pkt.to = query_states[dns->s.id].local_ip;
136 answer->pkt.dst_port = query_states[dns->s.id].local_port;
137 memcpy(answer->pkt.data, buf, r);
139 pkt_printf_dns((struct dns_pkt*)buf);
141 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
143 /* struct GNUNET_CONNECTION_TransmitHandle* th = */ GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, query_states[dns->s.id].client);
146 GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
151 * Task run during shutdown.
157 cleanup_task (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 unhijack(mycls.dnsoutport);
165 * @param sched scheduler to use
166 * @param server the initialized server
167 * @param cfg configuration to use
171 struct GNUNET_SCHEDULER_Handle *sched,
172 struct GNUNET_SERVER_Handle *server,
173 const struct GNUNET_CONFIGURATION_Handle *cfg)
175 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
176 /* callback, cls, type, size */
177 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
183 for (i = 0; i < 65536; i++) {
184 query_states[i].valid = 0;
188 struct sockaddr_in addr;
191 mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
192 if (mycls.dnsout == NULL)
194 memset(&addr, 0, sizeof(struct sockaddr_in));
196 int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
197 (struct sockaddr*)&addr,
198 sizeof(struct sockaddr_in));
200 if (err != GNUNET_YES) {
201 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
204 socklen_t addrlen = sizeof(struct sockaddr_in);
205 err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
206 (struct sockaddr*) &addr,
209 mycls.dnsoutport = htons(addr.sin_port);
211 hijack(htons(addr.sin_port));
213 GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
215 GNUNET_SERVER_add_handlers (server, handlers);
216 GNUNET_SCHEDULER_add_delayed (sched,
217 GNUNET_TIME_UNIT_FOREVER_REL,
223 * The main function for the dns service.
225 * @param argc number of arguments from the command line
226 * @param argv command line arguments
227 * @return 0 ok, 1 on error
230 main (int argc, char *const *argv)
233 GNUNET_SERVICE_run (argc,
236 GNUNET_SERVICE_OPTION_NONE,
237 &run, NULL)) ? 0 : 1;