split up daemon-vpn in multiple files, remove exit-functionality
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
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-daemon-vpn-dns.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
27 #include <gnunet_common.h>
28 #include <gnunet_client_lib.h>
29 #include <gnunet_os_lib.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_protocols.h>
32 #include <gnunet_server_lib.h>
33 #include <gnunet_container_lib.h>
34 #include <block_dns.h>
35
36 #include "gnunet-daemon-vpn-dns.h"
37 #include "gnunet-daemon-vpn.h"
38 #include "gnunet-daemon-vpn-helper.h"
39 #include "gnunet-service-dns-p.h"
40 #include "gnunet-vpn-packet.h"
41
42 /**
43  * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
44  * to the service-dns
45  * {{{
46  */
47 size_t
48 send_query(void* cls, size_t size, void* buf) {
49     size_t len;
50     /*
51      * Send the rehijack-message
52      */
53     if (restart_hijack == 1)
54       {
55         restart_hijack = 0;
56         /*
57          * The message is just a header
58          */
59         GNUNET_assert(sizeof(struct GNUNET_MessageHeader) <= size);
60         struct GNUNET_MessageHeader* hdr = buf;
61         len = sizeof(struct GNUNET_MessageHeader);
62         hdr->size = htons(len);
63         hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK);
64       }
65     else if (head != NULL)
66       {
67         struct query_packet_list* query = head;
68         len = ntohs(query->pkt.hdr.size);
69
70         GNUNET_assert(len <= size);
71
72         memcpy(buf, &query->pkt.hdr, len);
73
74         GNUNET_CONTAINER_DLL_remove (head, tail, query);
75
76         GNUNET_free(query);
77       }
78     else
79       {
80         GNUNET_break(0);
81         len = 0;
82       }
83
84     /*
85      * Check whether more data is to be sent
86      */
87     if (head != NULL)
88       {
89         GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
90       }
91     else if (restart_hijack == 1)
92       {
93         GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
94       }
95
96     return len;
97 }
98 /* }}} */
99
100
101 /**
102  * Connect to the service-dns
103  */
104 void
105 connect_to_service_dns (void *cls,
106                         const struct GNUNET_SCHEDULER_TaskContext *tc) {
107     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
108       return;
109     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
110     GNUNET_assert (dns_connection == NULL);
111     dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
112     /* This would most likely be a misconfiguration */
113     GNUNET_assert(dns_connection != NULL);
114     GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
115
116     /* If a packet is already in the list, schedule to send it */
117     if (head != NULL)
118       GNUNET_CLIENT_notify_transmit_ready(dns_connection,
119                                           ntohs(head->pkt.hdr.size),
120                                           GNUNET_TIME_UNIT_FOREVER_REL,
121                                           GNUNET_YES,
122                                           &send_query,
123                                           NULL);
124     else if (restart_hijack == 1)
125       {
126         GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
127       }
128 }
129
130 /**
131  * This receives packets from the service-dns and schedules process_answer to
132  * handle it
133  */
134 void
135 dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg) {
136     /* the service disconnected, reconnect after short wait */
137     if (msg == NULL)
138       {
139         GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
140         dns_connection = NULL;
141         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
142                                       &connect_to_service_dns,
143                                       NULL);
144         return;
145       }
146
147     /* the service did something strange, reconnect immediately */
148     if (msg->type != htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS))
149       {
150         GNUNET_break (0);
151         GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO);
152         dns_connection = NULL;
153         GNUNET_SCHEDULER_add_now (&connect_to_service_dns,
154                                   NULL);
155         return;
156       }
157     void *pkt = GNUNET_malloc(ntohs(msg->size));
158
159     memcpy(pkt, msg, ntohs(msg->size));
160
161     GNUNET_SCHEDULER_add_now(process_answer, pkt);
162     GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL);
163 }
164