aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-daemon-vpn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vpn/gnunet-daemon-vpn.c')
-rw-r--r--src/vpn/gnunet-daemon-vpn.c1572
1 files changed, 827 insertions, 745 deletions
diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c
index 15ed379df..3673b72c6 100644
--- a/src/vpn/gnunet-daemon-vpn.c
+++ b/src/vpn/gnunet-daemon-vpn.c
@@ -42,15 +42,15 @@
42 42
43const struct GNUNET_CONFIGURATION_Handle *cfg; 43const struct GNUNET_CONFIGURATION_Handle *cfg;
44struct GNUNET_MESH_Handle *mesh_handle; 44struct GNUNET_MESH_Handle *mesh_handle;
45struct GNUNET_CONTAINER_MultiHashMap* hashmap; 45struct GNUNET_CONTAINER_MultiHashMap *hashmap;
46static struct GNUNET_CONTAINER_Heap *heap; 46static struct GNUNET_CONTAINER_Heap *heap;
47 47
48struct tunnel_notify_queue 48struct tunnel_notify_queue
49{ 49{
50 struct tunnel_notify_queue* next; 50 struct tunnel_notify_queue *next;
51 struct tunnel_notify_queue* prev; 51 struct tunnel_notify_queue *prev;
52 size_t len; 52 size_t len;
53 void* cls; 53 void *cls;
54}; 54};
55 55
56/** 56/**
@@ -78,56 +78,63 @@ GNUNET_SCHEDULER_TaskIdentifier shs_task;
78 *{{{ 78 *{{{
79 */ 79 */
80static void 80static void
81cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) { 81cleanup (void *cls
82 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); 82 __attribute__ ((unused)),
83 83 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
84 /* stop the helper */ 84{
85 cleanup_helper(helper_handle); 85 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
86 86
87 /* close the connection to the service-dns */ 87 /* stop the helper */
88 if (dns_connection != NULL) 88 cleanup_helper (helper_handle);
89 { 89
90 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO); 90 /* close the connection to the service-dns */
91 dns_connection = NULL; 91 if (dns_connection != NULL)
92 } 92 {
93 93 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
94 if (mesh_handle != NULL) 94 dns_connection = NULL;
95 { 95 }
96 GNUNET_MESH_disconnect(mesh_handle); 96
97 mesh_handle = NULL; 97 if (mesh_handle != NULL)
98 } 98 {
99 if (GNUNET_SCHEDULER_NO_TASK != shs_task) 99 GNUNET_MESH_disconnect (mesh_handle);
100 { 100 mesh_handle = NULL;
101 GNUNET_SCHEDULER_cancel (shs_task); 101 }
102 shs_task = GNUNET_SCHEDULER_NO_TASK; 102 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
103 } 103 {
104 if (GNUNET_SCHEDULER_NO_TASK != conn_task) 104 GNUNET_SCHEDULER_cancel (shs_task);
105 { 105 shs_task = GNUNET_SCHEDULER_NO_TASK;
106 GNUNET_SCHEDULER_cancel (conn_task); 106 }
107 conn_task = GNUNET_SCHEDULER_NO_TASK; 107 if (GNUNET_SCHEDULER_NO_TASK != conn_task)
108 } 108 {
109 GNUNET_SCHEDULER_cancel (conn_task);
110 conn_task = GNUNET_SCHEDULER_NO_TASK;
111 }
109} 112}
113
110/*}}}*/ 114/*}}}*/
111 115
112/** 116/**
113 * @return the hash of the IP-Address if a mapping exists, NULL otherwise 117 * @return the hash of the IP-Address if a mapping exists, NULL otherwise
114 */ 118 */
115GNUNET_HashCode* 119GNUNET_HashCode *
116address6_mapping_exists(unsigned char addr[]) { 120address6_mapping_exists (unsigned char addr[])
117 GNUNET_HashCode* key = GNUNET_malloc(sizeof(GNUNET_HashCode)); 121{
118 unsigned char* k = (unsigned char*)key; 122 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
119 memset(key, 0, sizeof(GNUNET_HashCode)); 123 unsigned char *k = (unsigned char *) key;
120 unsigned int i;
121 for (i = 0; i < 16; i++)
122 k[15-i] = addr[i];
123 124
124 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(hashmap, key)) 125 memset (key, 0, sizeof (GNUNET_HashCode));
125 return key; 126 unsigned int i;
126 else 127
127 { 128 for (i = 0; i < 16; i++)
128 GNUNET_free(key); 129 k[15 - i] = addr[i];
129 return NULL; 130
130 } 131 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
132 return key;
133 else
134 {
135 GNUNET_free (key);
136 return NULL;
137 }
131} 138}
132 139
133/** 140/**
@@ -137,10 +144,12 @@ GNUNET_HashCode *
137address4_mapping_exists (uint32_t addr) 144address4_mapping_exists (uint32_t addr)
138{ 145{
139 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode)); 146 GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
147
140 memset (key, 0, sizeof (GNUNET_HashCode)); 148 memset (key, 0, sizeof (GNUNET_HashCode));
141 unsigned char *c = (unsigned char *) &addr; 149 unsigned char *c = (unsigned char *) &addr;
142 unsigned char *k = (unsigned char *) key; 150 unsigned char *k = (unsigned char *) key;
143 unsigned int i; 151 unsigned int i;
152
144 for (i = 0; i < 4; i++) 153 for (i = 0; i < 4; i++)
145 k[3 - i] = c[i]; 154 k[3 - i] = c[i];
146 155
@@ -151,106 +160,116 @@ address4_mapping_exists (uint32_t addr)
151 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key)) 160 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
152 return key; 161 return key;
153 else 162 else
154 { 163 {
155 GNUNET_free (key); 164 GNUNET_free (key);
156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n"); 165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
157 return NULL; 166 return NULL;
158 } 167 }
159} 168}
160 169
161static void 170static void
162collect_mappings(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tc) { 171collect_mappings (void *cls
163 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 172 __attribute__ ((unused)),
164 return; 173 const struct GNUNET_SCHEDULER_TaskContext *tc)
174{
175 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
176 return;
165 177
166 struct map_entry* me = GNUNET_CONTAINER_heap_remove_root(heap); 178 struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
167 179
168 /* This is free()ed memory! */ 180 /* This is free()ed memory! */
169 me->heap_node = NULL; 181 me->heap_node = NULL;
170 182
171 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */ 183 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
172 184
173 GNUNET_CONTAINER_multihashmap_remove(hashmap, &me->hash, me); 185 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me);
174 186
175 GNUNET_free(me); 187 GNUNET_free (me);
176} 188}
177 189
178void 190void
179send_icmp4_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { 191send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 192{
181 return; 193 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
194 return;
195
196 struct ip_icmp *request = cls;
182 197
183 struct ip_icmp* request = cls; 198 struct ip_icmp *response = alloca (ntohs (request->shdr.size));
184 199
185 struct ip_icmp* response = alloca(ntohs(request->shdr.size)); 200 GNUNET_assert (response != NULL);
186 GNUNET_assert(response != NULL); 201 memset (response, 0, ntohs (request->shdr.size));
187 memset(response, 0, ntohs(request->shdr.size));
188 202
189 response->shdr.size = request->shdr.size; 203 response->shdr.size = request->shdr.size;
190 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER); 204 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
191 205
192 response->tun.flags = 0; 206 response->tun.flags = 0;
193 response->tun.type = htons(0x0800); 207 response->tun.type = htons (0x0800);
194 208
195 response->ip_hdr.hdr_lngth = 5; 209 response->ip_hdr.hdr_lngth = 5;
196 response->ip_hdr.version = 4; 210 response->ip_hdr.version = 4;
197 response->ip_hdr.proto = 0x01; 211 response->ip_hdr.proto = 0x01;
198 response->ip_hdr.dadr = request->ip_hdr.sadr; 212 response->ip_hdr.dadr = request->ip_hdr.sadr;
199 response->ip_hdr.sadr = request->ip_hdr.dadr; 213 response->ip_hdr.sadr = request->ip_hdr.dadr;
200 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth; 214 response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
201 215
202 response->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&response->ip_hdr, 20); 216 response->ip_hdr.chks =
217 calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
203 218
204 response->icmp_hdr.code = 0; 219 response->icmp_hdr.code = 0;
205 response->icmp_hdr.type = 0x0; 220 response->icmp_hdr.type = 0x0;
206 221
207 /* Magic, more Magic! */ 222 /* Magic, more Magic! */
208 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8; 223 response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
209 224
210 /* Copy the rest of the packet */ 225 /* Copy the rest of the packet */
211 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip_icmp)); 226 memcpy (response + 1, request + 1,
227 ntohs (request->shdr.size) - sizeof (struct ip_icmp));
212 228
213 write_to_helper(response, ntohs(response->shdr.size)); 229 write_to_helper (response, ntohs (response->shdr.size));
214 230
215 GNUNET_free(request); 231 GNUNET_free (request);
216} 232}
217 233
218void 234void
219send_icmp6_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { 235send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
220 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 236{
221 return; 237 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
238 return;
239
240 struct ip6_icmp *request = cls;
222 241
223 struct ip6_icmp* request = cls; 242 struct ip6_icmp *response = alloca (ntohs (request->shdr.size));
224 243
225 struct ip6_icmp* response = alloca(ntohs(request->shdr.size)); 244 GNUNET_assert (response != NULL);
226 GNUNET_assert(response != NULL); 245 memset (response, 0, ntohs (request->shdr.size));
227 memset(response, 0, ntohs(request->shdr.size));
228 246
229 response->shdr.size = request->shdr.size; 247 response->shdr.size = request->shdr.size;
230 response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER); 248 response->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
231 249
232 response->tun.flags = 0; 250 response->tun.flags = 0;
233 response->tun.type = htons(0x86dd); 251 response->tun.type = htons (0x86dd);
234 252
235 response->ip6_hdr.hoplmt = 255; 253 response->ip6_hdr.hoplmt = 255;
236 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth; 254 response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
237 response->ip6_hdr.nxthdr = 0x3a; 255 response->ip6_hdr.nxthdr = 0x3a;
238 response->ip6_hdr.version = 6; 256 response->ip6_hdr.version = 6;
239 memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16); 257 memcpy (&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
240 memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16); 258 memcpy (&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
241 259
242 response->icmp_hdr.code = 0; 260 response->icmp_hdr.code = 0;
243 response->icmp_hdr.type = 0x81; 261 response->icmp_hdr.type = 0x81;
244 262
245 /* Magic, more Magic! */ 263 /* Magic, more Magic! */
246 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1; 264 response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
247 265
248 /* Copy the rest of the packet */ 266 /* Copy the rest of the packet */
249 memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp)); 267 memcpy (response + 1, request + 1,
268 ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
250 269
251 write_to_helper(response, ntohs(response->shdr.size)); 270 write_to_helper (response, ntohs (response->shdr.size));
252 271
253 GNUNET_free(request); 272 GNUNET_free (request);
254} 273}
255 274
256/** 275/**
@@ -261,38 +280,44 @@ static size_t
261send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf) 280send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
262{ 281{
263 struct GNUNET_MESH_Tunnel **tunnel = cls; 282 struct GNUNET_MESH_Tunnel **tunnel = cls;
264 GNUNET_MESH_tunnel_set_data(*tunnel, NULL); 283
284 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
265 struct GNUNET_MessageHeader *hdr = 285 struct GNUNET_MessageHeader *hdr =
266 (struct GNUNET_MessageHeader *) (tunnel + 1); 286 (struct GNUNET_MessageHeader *) (tunnel + 1);
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size); 287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
289 size);
268 GNUNET_assert (size >= ntohs (hdr->size)); 290 GNUNET_assert (size >= ntohs (hdr->size));
269 memcpy (buf, hdr, ntohs (hdr->size)); 291 memcpy (buf, hdr, ntohs (hdr->size));
270 size = ntohs(hdr->size); 292 size = ntohs (hdr->size);
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n"); 293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
272 294
273 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel)) 295 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
274 { 296 {
275 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel); 297 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
276 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel); 298 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
277 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel); 299 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
278 300
279 GNUNET_CONTAINER_DLL_remove(head, tail, element); 301 GNUNET_CONTAINER_DLL_remove (head, tail, element);
280 302
281 GNUNET_MESH_tunnel_set_head(*tunnel, head); 303 GNUNET_MESH_tunnel_set_head (*tunnel, head);
282 GNUNET_MESH_tunnel_set_tail(*tunnel, tail); 304 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
283 305
284 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel, 306 struct GNUNET_MESH_TransmitHandle *th =
285 GNUNET_NO, 307 GNUNET_MESH_notify_transmit_ready (*tunnel,
286 42, 308 GNUNET_NO,
287 GNUNET_TIME_relative_divide 309 42,
288 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 310 GNUNET_TIME_relative_divide
289 (const struct GNUNET_PeerIdentity *) 311 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
290 NULL, element->len, 312 (const struct GNUNET_PeerIdentity *)
291 send_pkt_to_peer_notify_callback, element->cls); 313 NULL, element->len,
292 /* save the handle */ 314 send_pkt_to_peer_notify_callback,
293 GNUNET_MESH_tunnel_set_data(*tunnel, th); 315 element->cls);
294 GNUNET_free(element); 316
295 } 317 /* save the handle */
318 GNUNET_MESH_tunnel_set_data (*tunnel, th);
319 GNUNET_free (element);
320 }
296 GNUNET_free (cls); 321 GNUNET_free (cls);
297 322
298 return size; 323 return size;
@@ -302,77 +327,95 @@ unsigned int
302port_in_ports (uint64_t ports, uint16_t port) 327port_in_ports (uint64_t ports, uint16_t port)
303{ 328{
304 uint16_t *ps = (uint16_t *) & ports; 329 uint16_t *ps = (uint16_t *) & ports;
330
305 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port; 331 return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
306} 332}
307 333
308void 334void
309send_pkt_to_peer (void *cls, 335send_pkt_to_peer (void *cls,
310 const struct GNUNET_PeerIdentity *peer, 336 const struct GNUNET_PeerIdentity *peer,
311 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 337 const struct GNUNET_TRANSPORT_ATS_Information *atsi
338 __attribute__ ((unused)))
312{ 339{
313 /* peer == NULL means that all peers in this request are connected */ 340 /* peer == NULL means that all peers in this request are connected */
314 if (peer == NULL) return; 341 if (peer == NULL)
342 return;
315 struct GNUNET_MESH_Tunnel **tunnel = cls; 343 struct GNUNET_MESH_Tunnel **tunnel = cls;
316 struct GNUNET_MessageHeader *hdr = 344 struct GNUNET_MessageHeader *hdr =
317 (struct GNUNET_MessageHeader *) (tunnel + 1); 345 (struct GNUNET_MessageHeader *) (tunnel + 1);
318 346
319 GNUNET_assert(NULL != tunnel); 347 GNUNET_assert (NULL != tunnel);
320 GNUNET_assert(NULL != *tunnel); 348 GNUNET_assert (NULL != *tunnel);
321 349
322 if (NULL == GNUNET_MESH_tunnel_get_data(*tunnel)) 350 if (NULL == GNUNET_MESH_tunnel_get_data (*tunnel))
323 { 351 {
324 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel, 352 struct GNUNET_MESH_TransmitHandle *th =
325 GNUNET_NO, 353 GNUNET_MESH_notify_transmit_ready (*tunnel,
326 42, 354 GNUNET_NO,
327 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 355 42,
328 (const struct GNUNET_PeerIdentity *)NULL, 356 GNUNET_TIME_relative_divide
329 ntohs(hdr->size), 357 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
330 send_pkt_to_peer_notify_callback, 358 (const struct GNUNET_PeerIdentity *)
331 cls); 359 NULL,
332 GNUNET_MESH_tunnel_set_data(*tunnel, th); 360 ntohs (hdr->size),
333 } 361 send_pkt_to_peer_notify_callback,
362 cls);
363
364 GNUNET_MESH_tunnel_set_data (*tunnel, th);
365 }
334 else 366 else
335 { 367 {
336 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel); 368 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
337 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel); 369 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
338 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof *element); 370 struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
339 371
340 element->cls = cls; 372 element->cls = cls;
341 element->len = ntohs(hdr->size); 373 element->len = ntohs (hdr->size);
342 374
343 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element); 375 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
344 376
345 GNUNET_MESH_tunnel_set_head(*tunnel, head); 377 GNUNET_MESH_tunnel_set_head (*tunnel, head);
346 GNUNET_MESH_tunnel_set_tail(*tunnel, tail); 378 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
347 } 379 }
348} 380}
349 381
350/** 382/**
351 * Create a new Address from an answer-packet 383 * Create a new Address from an answer-packet
352 */ 384 */
353void 385void
354new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */ 386new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
355 char* ipv6addr; 387 const GNUNET_HashCode * service_desc)
356 unsigned long long ipv6prefix; 388{ /* {{{ */
357 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr)); 389 char *ipv6addr;
358 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix)); 390 unsigned long long ipv6prefix;
359 GNUNET_assert(ipv6prefix < 127); 391
360 ipv6prefix = (ipv6prefix + 7)/8; 392 GNUNET_assert (GNUNET_OK ==
361 393 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
362 inet_pton (AF_INET6, ipv6addr, buf); 394 &ipv6addr));
363 GNUNET_free(ipv6addr); 395 GNUNET_assert (GNUNET_OK ==
364 396 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
365 int peer_length = 16 - ipv6prefix - 6; 397 "IPV6PREFIX",
366 if (peer_length <= 0) 398 &ipv6prefix));
367 peer_length = 0; 399 GNUNET_assert (ipv6prefix < 127);
368 400 ipv6prefix = (ipv6prefix + 7) / 8;
369 int service_length = 16 - ipv6prefix - peer_length; 401
370 if (service_length <= 0) 402 inet_pton (AF_INET6, ipv6addr, buf);
371 service_length = 0; 403 GNUNET_free (ipv6addr);
372 404
373 memcpy(buf+ipv6prefix, service_desc, service_length); 405 int peer_length = 16 - ipv6prefix - 6;
374 memcpy(buf+ipv6prefix+service_length, peer, peer_length); 406
407 if (peer_length <= 0)
408 peer_length = 0;
409
410 int service_length = 16 - ipv6prefix - peer_length;
411
412 if (service_length <= 0)
413 service_length = 0;
414
415 memcpy (buf + ipv6prefix, service_desc, service_length);
416 memcpy (buf + ipv6prefix + service_length, peer, peer_length);
375} 417}
418
376/*}}}*/ 419/*}}}*/
377 420
378 421
@@ -384,10 +427,10 @@ new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
384{ /* {{{ */ 427{ /* {{{ */
385 char *ipv6addr; 428 char *ipv6addr;
386 unsigned long long ipv6prefix; 429 unsigned long long ipv6prefix;
430
387 GNUNET_assert (GNUNET_OK == 431 GNUNET_assert (GNUNET_OK ==
388 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", 432 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
389 "IPV6ADDR", 433 "IPV6ADDR", &ipv6addr));
390 &ipv6addr));
391 GNUNET_assert (GNUNET_OK == 434 GNUNET_assert (GNUNET_OK ==
392 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", 435 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
393 "IPV6PREFIX", 436 "IPV6PREFIX",
@@ -402,6 +445,7 @@ new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
402 445
403 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length)); 446 memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
404} 447}
448
405/*}}}*/ 449/*}}}*/
406 450
407/** 451/**
@@ -412,49 +456,53 @@ new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
412{ /* {{{ */ 456{ /* {{{ */
413 char *ipv4addr; 457 char *ipv4addr;
414 char *ipv4mask; 458 char *ipv4mask;
459
415 GNUNET_assert (GNUNET_OK == 460 GNUNET_assert (GNUNET_OK ==
416 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", 461 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
417 "IPV4ADDR", 462 "IPV4ADDR", &ipv4addr));
418 &ipv4addr));
419 GNUNET_assert (GNUNET_OK == 463 GNUNET_assert (GNUNET_OK ==
420 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", 464 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
421 "IPV4MASK", 465 "IPV4MASK", &ipv4mask));
422 &ipv4mask));
423 uint32_t mask; 466 uint32_t mask;
467
424 inet_pton (AF_INET, ipv4addr, buf); 468 inet_pton (AF_INET, ipv4addr, buf);
425 int r = inet_pton (AF_INET, ipv4mask, &mask); 469 int r = inet_pton (AF_INET, ipv4mask, &mask);
426 mask = htonl(mask); 470
427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r, mask); 471 mask = htonl (mask);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r,
473 mask);
428 474
429 GNUNET_free (ipv4addr); 475 GNUNET_free (ipv4addr);
430 476
431 int c; 477 int c;
432 478
433 if (mask) 479 if (mask)
480 {
481 mask = (mask ^ (mask - 1)) >> 1;
482 for (c = 0; mask; c++)
434 { 483 {
435 mask = (mask ^ (mask - 1)) >> 1; 484 mask >>= 1;
436 for (c = 0; mask; c++)
437 {
438 mask >>= 1;
439 }
440 } 485 }
486 }
441 else 487 else
442 { 488 {
443 c = CHAR_BIT * sizeof(mask); 489 c = CHAR_BIT * sizeof (mask);
444 } 490 }
445 491
446 c = 32-c; 492 c = 32 - c;
447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n", ipv4mask, c); 493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n",
494 ipv4mask, c);
448 495
449 GNUNET_free (ipv4mask); 496 GNUNET_free (ipv4mask);
450 497
451 if (c % 8 == 0) 498 if (c % 8 == 0)
452 c = c / 8; 499 c = c / 8;
453 else 500 else
454 GNUNET_assert(0); 501 GNUNET_assert (0);
455 502
456 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4-c)); 503 memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4 - c));
457} 504}
505
458/*}}}*/ 506/*}}}*/
459 507
460/** 508/**
@@ -465,262 +513,281 @@ new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
465 * doing nothing for "real" services. 513 * doing nothing for "real" services.
466 */ 514 */
467void 515void
468process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) { 516process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
469 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 517{
470 return; 518 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
519 return;
471 520
472 struct answer_packet* pkt = cls; 521 struct answer_packet *pkt = cls;
473 struct answer_packet_list* list; 522 struct answer_packet_list *list;
474 523
475 /* This answer is about a .gnunet-service 524 /* This answer is about a .gnunet-service
476 * 525 *
477 * It contains an almost complete DNS-Response, we have to fill in the ip 526 * It contains an almost complete DNS-Response, we have to fill in the ip
478 * at the offset pkt->addroffset 527 * at the offset pkt->addroffset
528 */
529 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
530 {
531 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
532
533 GNUNET_HashCode key;
534
535 memset (&key, 0, sizeof (GNUNET_HashCode));
536
537 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
538 unsigned char *k = (unsigned char *) &key;
539
540 new_ip6addr (c, &pkt->service_descr.peer,
541 &pkt->service_descr.service_descriptor);
542 /*
543 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
479 */ 544 */
480 if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE) 545 unsigned int i;
481 {
482 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
483 546
484 GNUNET_HashCode key; 547 for (i = 0; i < 16; i++)
485 memset(&key, 0, sizeof(GNUNET_HashCode)); 548 k[15 - i] = c[i];
486 549
487 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset); 550 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
488 unsigned char* k = (unsigned char*)&key;
489 new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
490 /*
491 * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
492 */
493 unsigned int i;
494 for (i = 0; i < 16; i++)
495 k[15-i] = c[i];
496 551
497 uint16_t namelen = strlen((char*)pkt->data+12)+1; 552 struct map_entry *value =
553 GNUNET_malloc (sizeof (struct map_entry) + namelen);
554 char *name = (char *) (value + 1);
498 555
499 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen); 556 value->namelen = namelen;
500 char* name = (char*)(value +1); 557 memcpy (name, pkt->data + 12, namelen);
501 558
502 value->namelen = namelen; 559 memcpy (&value->desc, &pkt->service_descr,
503 memcpy(name, pkt->data+12, namelen); 560 sizeof (struct GNUNET_vpn_service_descriptor));
504 561
505 memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor)); 562 memset (value->additional_ports, 0, 8192);
506 563
507 memset(value->additional_ports, 0, 8192); 564 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
508 565
509 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode)); 566 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
567 {
568 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
570
571 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
572 GNUNET_TIME_absolute_get
573 ().abs_value);
574 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
575 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
576 }
577 else
578 GNUNET_free (value);
510 579
511 if (GNUNET_NO ==
512 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
513 {
514 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
516 580
517 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value, 581 list =
518 GNUNET_TIME_absolute_get ().abs_value); 582 GNUNET_malloc (htons (pkt->hdr.size) +
519 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings) 583 2 * sizeof (struct answer_packet_list *));
520 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
521 }
522 else
523 GNUNET_free(value);
524 584
585 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
525 586
526 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*)); 587 }
588 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
589 {
590 GNUNET_HashCode key;
527 591
528 memcpy(&list->pkt, pkt, htons(pkt->hdr.size)); 592 memset (&key, 0, sizeof key);
593 unsigned char *k = (unsigned char *) &key;
594 unsigned char *s = pkt->data + 12;
595 int i = 0;
529 596
530 } 597 /* Whoever designed the reverse IPv6-lookup is batshit insane */
531 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV) 598 for (i = 0; i < 16; i++)
532 { 599 {
533 GNUNET_HashCode key; 600 unsigned char c1 = s[(4 * i) + 1];
534 memset(&key, 0, sizeof key); 601 unsigned char c2 = s[(4 * i) + 3];
535 unsigned char* k = (unsigned char*)&key;
536 unsigned char* s = pkt->data+12;
537 int i = 0;
538 /* Whoever designed the reverse IPv6-lookup is batshit insane */
539 for (i = 0; i < 16; i++)
540 {
541 unsigned char c1 = s[(4*i)+1];
542 unsigned char c2 = s[(4*i)+3];
543 if (c1 <= '9')
544 k[i] = c1 - '0';
545 else
546 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
547 if (c2 <= '9')
548 k[i] += 16*(c2 - '0');
549 else
550 k[i] += 16*(c2 - 87);
551 }
552
553 struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
554 uint16_t offset = ntohs(pkt->addroffset);
555
556 if (map_entry == NULL)
557 {
558 GNUNET_free(pkt);
559 return;
560 }
561
562 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
563 GNUNET_TIME_absolute_get ().abs_value);
564
565
566 unsigned short namelen = htons(map_entry->namelen);
567 char* name = (char*)(map_entry + 1);
568
569 list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
570
571 struct answer_packet* rpkt = &list->pkt;
572
573 /* The offset points to the first byte belonging to the address */
574 memcpy(rpkt, pkt, offset - 1);
575
576 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
577 rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
578
579 memcpy(((char*)rpkt)+offset, &namelen, 2);
580 memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
581 602
582 } 603 if (c1 <= '9')
583 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP) 604 k[i] = c1 - '0';
584 { 605 else
585 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*)); 606 k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
586 memcpy(&list->pkt, pkt, htons(pkt->hdr.size)); 607 if (c2 <= '9')
587 } 608 k[i] += 16 * (c2 - '0');
588 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA) 609 else
589 { 610 k[i] += 16 * (c2 - 87);
590 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; 611 }
591 612
592 GNUNET_HashCode key; 613 struct map_entry *map_entry =
593 memset(&key, 0, sizeof(GNUNET_HashCode)); 614 GNUNET_CONTAINER_multihashmap_get (hashmap, &key);
594 615 uint16_t offset = ntohs (pkt->addroffset);
595 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset); 616
596 new_ip6addr_remote(c, pkt->addr, pkt->addrsize); 617 if (map_entry == NULL)
597 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", 618 {
598 c[0], 619 GNUNET_free (pkt);
599 c[1], 620 return;
600 c[2], 621 }
601 c[3], 622
602 c[4], 623 GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
603 c[5], 624 GNUNET_TIME_absolute_get ().abs_value);
604 c[6], 625
605 c[7], 626
606 c[8], 627 unsigned short namelen = htons (map_entry->namelen);
607 c[9], 628 char *name = (char *) (map_entry + 1);
608 c[10], 629
609 c[11], 630 list =
610 c[12], 631 GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 +
611 c[13], 632 ntohs (namelen));
612 c[14], 633
613 c[15]); 634 struct answer_packet *rpkt = &list->pkt;
614 unsigned char* k = (unsigned char*)&key; 635
615 /* 636 /* The offset points to the first byte belonging to the address */
616 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table) 637 memcpy (rpkt, pkt, offset - 1);
617 */ 638
618 unsigned int i; 639 rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
619 for (i = 0; i < 16; i++) 640 rpkt->hdr.size = ntohs (offset + 2 + ntohs (namelen));
620 k[15-i] = c[i]; 641
621 642 memcpy (((char *) rpkt) + offset, &namelen, 2);
622 uint16_t namelen = strlen((char*)pkt->data+12)+1; 643 memcpy (((char *) rpkt) + offset + 2, name, ntohs (namelen));
623 644
624 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen); 645 }
625 char* name = (char*)(value +1); 646 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
626 647 {
627 value->namelen = namelen; 648 list =
628 memcpy(name, pkt->data+12, namelen); 649 GNUNET_malloc (htons (pkt->hdr.size) +
629 650 2 * sizeof (struct answer_packet_list *));
630 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize); 651 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
631 value->addrlen = pkt->addrsize; 652 }
632 memcpy(&value->addr, &pkt->addr, pkt->addrsize); 653 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
633 memset(value->additional_ports, 0, 8192); 654 {
634 655 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
635 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode)); 656
636 657 GNUNET_HashCode key;
637 if (GNUNET_NO == 658
638 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key)) 659 memset (&key, 0, sizeof (GNUNET_HashCode));
639 { 660
640 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value, 661 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
641 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 662
642 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value, 663 new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
643 GNUNET_TIME_absolute_get ().abs_value); 664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings) 665 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
645 GNUNET_SCHEDULER_add_now(collect_mappings, NULL); 666 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
646 } 667 c[10], c[11], c[12], c[13], c[14], c[15]);
647 else 668 unsigned char *k = (unsigned char *) &key;
648 GNUNET_free(value); 669
649 670 /*
650 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*)); 671 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
651 672 */
652 memcpy(&list->pkt, pkt, htons(pkt->hdr.size)); 673 unsigned int i;
653 } 674
654 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A) 675 for (i = 0; i < 16; i++)
655 { 676 k[15 - i] = c[i];
656 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; 677
657 678 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
658 GNUNET_HashCode key; 679
659 memset(&key, 0, sizeof(GNUNET_HashCode)); 680 struct map_entry *value =
660 681 GNUNET_malloc (sizeof (struct map_entry) + namelen);
661 unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset); 682 char *name = (char *) (value + 1);
662 new_ip4addr_remote(c, pkt->addr, pkt->addrsize); 683
663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", 684 value->namelen = namelen;
664 c[0], 685 memcpy (name, pkt->data + 12, namelen);
665 c[1], 686
666 c[2], 687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
667 c[3]); 688 pkt->addrsize);
668 unsigned char* k = (unsigned char*)&key; 689 value->addrlen = pkt->addrsize;
669 /* 690 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
670 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table) 691 memset (value->additional_ports, 0, 8192);
671 */ 692
672 unsigned int i; 693 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
673 for (i = 0; i < 4; i++) 694
674 k[3-i] = c[i]; 695 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
675 696 {
676 uint16_t namelen = strlen((char*)pkt->data+12)+1; 697 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
677 698 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
678 struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen); 699 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
679 char* name = (char*)(value +1); 700 GNUNET_TIME_absolute_get
680 701 ().abs_value);
681 value->namelen = namelen; 702 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
682 memcpy(name, pkt->data+12, namelen); 703 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
683 704 }
684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
685 value->addrlen = pkt->addrsize;
686 memcpy(&value->addr, &pkt->addr, pkt->addrsize);
687 memset(value->additional_ports, 0, 8192);
688
689 memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
690
691 if (GNUNET_NO ==
692 GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
693 {
694 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
696 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
697 GNUNET_TIME_absolute_get ().abs_value);
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping is saved in the hashmap with key %08x.\n",
699 *((uint32_t*)(&key)));
700 if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
701 GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
702 }
703 else
704 GNUNET_free(value);
705
706 list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
707
708 memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
709 }
710 else 705 else
711 { 706 GNUNET_free (value);
712 GNUNET_break(0); 707
713 GNUNET_free(pkt); 708 list =
714 return; 709 GNUNET_malloc (htons (pkt->hdr.size) +
715 } 710 2 * sizeof (struct answer_packet_list *));
711
712 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
713 }
714 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
715 {
716 pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
716 717
717 GNUNET_free(pkt); 718 GNUNET_HashCode key;
718 719
719 GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list); 720 memset (&key, 0, sizeof (GNUNET_HashCode));
720 721
721 schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL); 722 unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
722 723
724 new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
726 c[0], c[1], c[2], c[3]);
727 unsigned char *k = (unsigned char *) &key;
728
729 /*
730 * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
731 */
732 unsigned int i;
733
734 for (i = 0; i < 4; i++)
735 k[3 - i] = c[i];
736
737 uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
738
739 struct map_entry *value =
740 GNUNET_malloc (sizeof (struct map_entry) + namelen);
741 char *name = (char *) (value + 1);
742
743 value->namelen = namelen;
744 memcpy (name, pkt->data + 12, namelen);
745
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
747 pkt->addrsize);
748 value->addrlen = pkt->addrsize;
749 memcpy (&value->addr, &pkt->addr, pkt->addrsize);
750 memset (value->additional_ports, 0, 8192);
751
752 memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
753
754 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
755 {
756 GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
757 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
758 value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
759 GNUNET_TIME_absolute_get
760 ().abs_value);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Mapping is saved in the hashmap with key %08x.\n",
763 *((uint32_t *) (&key)));
764 if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
765 GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
766 }
767 else
768 GNUNET_free (value);
769
770 list =
771 GNUNET_malloc (htons (pkt->hdr.size) +
772 2 * sizeof (struct answer_packet_list *));
773
774 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
775 }
776 else
777 {
778 GNUNET_break (0);
779 GNUNET_free (pkt);
723 return; 780 return;
781 }
782
783 GNUNET_free (pkt);
784
785 GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
786 answer_proc_tail, list);
787
788 schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
789
790 return;
724} 791}
725 792
726/** 793/**
@@ -787,16 +854,18 @@ testBit (char *bitArray, unsigned int bitIdx)
787static void 854static void
788add_additional_port (struct map_entry *me, uint16_t port) 855add_additional_port (struct map_entry *me, uint16_t port)
789{ 856{
790 setBit(me->additional_ports, port); 857 setBit (me->additional_ports, port);
791} 858}
792 859
793static int 860static int
794receive_udp_back (void *cls 861receive_udp_back (void *cls
795 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel, 862 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
796 void **tunnel_ctx __attribute__ ((unused)), 863 void **tunnel_ctx __attribute__ ((unused)),
797 const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)), 864 const struct GNUNET_PeerIdentity *sender
865 __attribute__ ((unused)),
798 const struct GNUNET_MessageHeader *message, 866 const struct GNUNET_MessageHeader *message,
799 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__ ((unused))) 867 const struct GNUNET_TRANSPORT_ATS_Information *atsi
868 __attribute__ ((unused)))
800{ 869{
801 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 870 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
802 struct remote_addr *s = (struct remote_addr *) desc; 871 struct remote_addr *s = (struct remote_addr *) desc;
@@ -804,167 +873,178 @@ receive_udp_back (void *cls
804 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel); 873 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
805 874
806 if (16 == s->addrlen) 875 if (16 == s->addrlen)
807 { 876 {
808 size_t size = 877 size_t size =
809 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt); 878 sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
879 sizeof (struct udp_pkt);
810 880
811 struct ip6_udp *pkt6 = alloca (size); 881 struct ip6_udp *pkt6 = alloca (size);
812 882
813 GNUNET_assert (pkt6 != NULL); 883 GNUNET_assert (pkt6 != NULL);
814 884
815 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK) 885 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
816 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc); 886 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
817 else 887 else
818 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen); 888 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
819 889
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, 891 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
822 ntohs (message->size), ntohs (pkt->len)); 892 ntohs (message->size), ntohs (pkt->len));
893
894 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
895 pkt6->shdr.size = htons (size);
823 896
824 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); 897 pkt6->tun.flags = 0;
825 pkt6->shdr.size = htons (size); 898 pkt6->tun.type = htons (0x86dd);
826 899
827 pkt6->tun.flags = 0; 900 pkt6->ip6_hdr.version = 6;
828 pkt6->tun.type = htons (0x86dd); 901 pkt6->ip6_hdr.tclass_h = 0;
902 pkt6->ip6_hdr.tclass_l = 0;
903 pkt6->ip6_hdr.flowlbl = 0;
904 pkt6->ip6_hdr.paylgth = pkt->len;
905 pkt6->ip6_hdr.nxthdr = 0x11;
906 pkt6->ip6_hdr.hoplmt = 0xff;
907
908 {
909 char *ipv6addr;
910
911 GNUNET_assert (GNUNET_OK ==
912 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
913 "IPV6ADDR",
914 &ipv6addr));
915 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
916 GNUNET_free (ipv6addr);
917 }
918 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
829 919
830 pkt6->ip6_hdr.version = 6; 920 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
831 pkt6->ip6_hdr.tclass_h = 0;
832 pkt6->ip6_hdr.tclass_l = 0;
833 pkt6->ip6_hdr.flowlbl = 0;
834 pkt6->ip6_hdr.paylgth = pkt->len;
835 pkt6->ip6_hdr.nxthdr = 0x11;
836 pkt6->ip6_hdr.hoplmt = 0xff;
837 921
922 GNUNET_assert (key != NULL);
923
924 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
925
926 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
927 GNUNET_TIME_absolute_get ().abs_value);
928
929 GNUNET_free (key);
930
931 GNUNET_assert (me != NULL);
932 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
933 {
934 GNUNET_assert (me->desc.
935 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
936 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt) &&
937 !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
838 { 938 {
839 char *ipv6addr; 939 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
840 GNUNET_assert (GNUNET_OK ==
841 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
842 "IPV6ADDR",
843 &ipv6addr));
844 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
845 GNUNET_free (ipv6addr);
846 } 940 }
847 memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len)); 941 }
848 942
849 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr); 943 pkt6->udp_hdr.crc = 0;
850 GNUNET_assert (key != NULL); 944 uint32_t sum = 0;
851 945
852 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); 946 sum =
853 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, 947 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
854 GNUNET_TIME_absolute_get (). 948 sum =
855 abs_value); 949 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
856 950 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
857 GNUNET_free (key); 951
858 952 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
859 GNUNET_assert (me != NULL); 953 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
860 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK) 954 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
861 { 955
862 GNUNET_assert (me->desc. 956 sum =
863 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
864 if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt)
865 && !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
866 {
867 add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
868 }
869 }
870
871 pkt6->udp_hdr.crc = 0;
872 uint32_t sum = 0;
873 sum =
874 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
875 16);
876 sum =
877 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
878 16);
879 uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
880 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
881 tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
882 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
883
884 sum =
885 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr, 957 calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
886 ntohs (pkt->len)); 958 ntohs (pkt->len));
887 pkt6->udp_hdr.crc = calculate_checksum_end (sum); 959 pkt6->udp_hdr.crc = calculate_checksum_end (sum);
888 960
889 write_to_helper (pkt6, size); 961 write_to_helper (pkt6, size);
890 } 962 }
891 else 963 else
892 { 964 {
893 size_t size = 965 size_t size =
894 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt); 966 sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
895 967
896 struct ip_udp *pkt4 = alloca (size); 968 struct ip_udp *pkt4 = alloca (size);
897 969
898 GNUNET_assert (pkt4 != NULL); 970 GNUNET_assert (pkt4 != NULL);
899 971
900 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK); 972 GNUNET_assert (ntohs (message->type) ==
901 uint32_t sadr; 973 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
902 new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen); 974 uint32_t sadr;
903 pkt4->ip_hdr.sadr = sadr;
904 975
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 976 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
906 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size, 977 pkt4->ip_hdr.sadr = sadr;
907 ntohs (message->size), ntohs (pkt->len));
908
909 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
910 pkt4->shdr.size = htons (size);
911
912 pkt4->tun.flags = 0;
913 pkt4->tun.type = htons (0x0800);
914
915 pkt4->ip_hdr.version = 4;
916 pkt4->ip_hdr.hdr_lngth = 5;
917 pkt4->ip_hdr.diff_serv = 0;
918 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs(pkt->len));
919 pkt4->ip_hdr.ident = 0;
920 pkt4->ip_hdr.flags = 0;
921 pkt4->ip_hdr.frag_off = 0;
922 pkt4->ip_hdr.ttl = 255;
923 pkt4->ip_hdr.proto = 0x11;
924 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
925 978
926 { 979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 char *ipv4addr; 980 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
928 uint32_t dadr; 981 ntohs (message->size), ntohs (pkt->len));
929 GNUNET_assert (GNUNET_OK ==
930 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
931 "IPV4ADDR",
932 &ipv4addr));
933 inet_pton (AF_INET, ipv4addr, &dadr);
934 GNUNET_free (ipv4addr);
935 pkt4->ip_hdr.dadr = dadr;
936 }
937 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
938 982
939 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr); 983 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
940 GNUNET_assert (key != NULL); 984 pkt4->shdr.size = htons (size);
941 985
942 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); 986 pkt4->tun.flags = 0;
943 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node, 987 pkt4->tun.type = htons (0x0800);
944 GNUNET_TIME_absolute_get ().
945 abs_value);
946 988
947 GNUNET_free (key); 989 pkt4->ip_hdr.version = 4;
990 pkt4->ip_hdr.hdr_lngth = 5;
991 pkt4->ip_hdr.diff_serv = 0;
992 pkt4->ip_hdr.tot_lngth = htons (20 + ntohs (pkt->len));
993 pkt4->ip_hdr.ident = 0;
994 pkt4->ip_hdr.flags = 0;
995 pkt4->ip_hdr.frag_off = 0;
996 pkt4->ip_hdr.ttl = 255;
997 pkt4->ip_hdr.proto = 0x11;
998 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
948 999
949 GNUNET_assert (me != NULL); 1000 {
1001 char *ipv4addr;
1002 uint32_t dadr;
1003
1004 GNUNET_assert (GNUNET_OK ==
1005 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1006 "IPV4ADDR",
1007 &ipv4addr));
1008 inet_pton (AF_INET, ipv4addr, &dadr);
1009 GNUNET_free (ipv4addr);
1010 pkt4->ip_hdr.dadr = dadr;
1011 }
1012 memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
1013
1014 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1015
1016 GNUNET_assert (key != NULL);
1017
1018 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1019
1020 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1021 GNUNET_TIME_absolute_get ().abs_value);
1022
1023 GNUNET_free (key);
950 1024
951 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */ 1025 GNUNET_assert (me != NULL);
952 1026
953 pkt4->ip_hdr.chks = 1027 pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
1028
1029 pkt4->ip_hdr.chks =
954 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4); 1030 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
955 1031
956 write_to_helper (pkt4, size); 1032 write_to_helper (pkt4, size);
957 } 1033 }
958 1034
959 return GNUNET_OK; 1035 return GNUNET_OK;
960} 1036}
961 1037
962static int 1038static int
963receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel, 1039receive_tcp_back (void *cls
964 void **tunnel_ctx __attribute__((unused)), 1040 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
965 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 1041 void **tunnel_ctx
966 const struct GNUNET_MessageHeader *message, 1042 __attribute__ ((unused)),
967 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 1043 const struct GNUNET_PeerIdentity *sender
1044 __attribute__ ((unused)),
1045 const struct GNUNET_MessageHeader *message,
1046 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1047 __attribute__ ((unused)))
968{ 1048{
969 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1049 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
970 struct remote_addr *s = (struct remote_addr *) desc; 1050 struct remote_addr *s = (struct remote_addr *) desc;
@@ -972,164 +1052,169 @@ receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel*
972 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel); 1052 const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
973 1053
974 size_t pktlen = 1054 size_t pktlen =
975 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1055 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
976 sizeof (GNUNET_HashCode); 1056 sizeof (GNUNET_HashCode);
977 1057
978 if (s->addrlen == 16) 1058 if (s->addrlen == 16)
1059 {
1060 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
1061
1062 struct ip6_tcp *pkt6 = alloca (size);
1063
1064 memset (pkt6, 0, size);
1065
1066 GNUNET_assert (pkt6 != NULL);
1067
1068 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
1069 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
1070 else
1071 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
1072
1073 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1074 pkt6->shdr.size = htons (size);
1075
1076 pkt6->tun.flags = 0;
1077 pkt6->tun.type = htons (0x86dd);
1078
1079 pkt6->ip6_hdr.version = 6;
1080 pkt6->ip6_hdr.tclass_h = 0;
1081 pkt6->ip6_hdr.tclass_l = 0;
1082 pkt6->ip6_hdr.flowlbl = 0;
1083 pkt6->ip6_hdr.paylgth = htons (pktlen);
1084 pkt6->ip6_hdr.nxthdr = 0x06;
1085 pkt6->ip6_hdr.hoplmt = 0xff;
1086
979 { 1087 {
980 size_t size = pktlen + sizeof (struct ip6_tcp) - 1; 1088 char *ipv6addr;
1089
1090 GNUNET_assert (GNUNET_OK ==
1091 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1092 "IPV6ADDR",
1093 &ipv6addr));
1094 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
1095 GNUNET_free (ipv6addr);
1096 }
1097 memcpy (&pkt6->tcp_hdr, pkt, pktlen);
981 1098
982 struct ip6_tcp *pkt6 = alloca (size); 1099 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
983 memset (pkt6, 0, size);
984 1100
985 GNUNET_assert (pkt6 != NULL); 1101 GNUNET_assert (key != NULL);
986 1102
987 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK) 1103 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
988 new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
989 else
990 new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
991 1104
992 pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); 1105 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
993 pkt6->shdr.size = htons (size); 1106 GNUNET_TIME_absolute_get ().abs_value);
994 1107
995 pkt6->tun.flags = 0; 1108 GNUNET_free (key);
996 pkt6->tun.type = htons (0x86dd);
997 1109
998 pkt6->ip6_hdr.version = 6; 1110 GNUNET_assert (me != NULL);
999 pkt6->ip6_hdr.tclass_h = 0; 1111 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1000 pkt6->ip6_hdr.tclass_l = 0; 1112 GNUNET_assert (me->desc.
1001 pkt6->ip6_hdr.flowlbl = 0; 1113 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1002 pkt6->ip6_hdr.paylgth = htons (pktlen);
1003 pkt6->ip6_hdr.nxthdr = 0x06;
1004 pkt6->ip6_hdr.hoplmt = 0xff;
1005 1114
1006 { 1115 pkt6->tcp_hdr.crc = 0;
1007 char *ipv6addr; 1116 uint32_t sum = 0;
1008 GNUNET_assert (GNUNET_OK == 1117 uint32_t tmp;
1009 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", 1118
1010 "IPV6ADDR", 1119 sum =
1011 &ipv6addr)); 1120 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
1012 inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr); 1121 sum =
1013 GNUNET_free (ipv6addr); 1122 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
1014 } 1123 tmp = htonl (pktlen);
1015 memcpy (&pkt6->tcp_hdr, pkt, pktlen); 1124 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1016 1125 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1017 GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr); 1126 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1018 GNUNET_assert (key != NULL); 1127
1019 1128 sum =
1020 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1021 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1022 GNUNET_TIME_absolute_get ().
1023 abs_value);
1024
1025 GNUNET_free (key);
1026
1027 GNUNET_assert (me != NULL);
1028 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
1029 GNUNET_assert (me->desc.
1030 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
1031
1032 pkt6->tcp_hdr.crc = 0;
1033 uint32_t sum = 0;
1034 uint32_t tmp;
1035 sum =
1036 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
1037 16);
1038 sum =
1039 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
1040 16);
1041 tmp = htonl (pktlen);
1042 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1043 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
1044 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1045
1046 sum =
1047 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr, 1129 calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
1048 ntohs (pkt6->ip6_hdr.paylgth)); 1130 ntohs (pkt6->ip6_hdr.paylgth));
1049 pkt6->tcp_hdr.crc = calculate_checksum_end (sum); 1131 pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
1050 1132
1051 write_to_helper (pkt6, size); 1133 write_to_helper (pkt6, size);
1052 } 1134 }
1053 else 1135 else
1136 {
1137 size_t size = pktlen + sizeof (struct ip_tcp) - 1;
1138
1139 struct ip_tcp *pkt4 = alloca (size);
1140
1141 GNUNET_assert (pkt4 != NULL);
1142 memset (pkt4, 0, size);
1143
1144 GNUNET_assert (ntohs (message->type) ==
1145 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
1146 uint32_t sadr;
1147
1148 new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
1149 pkt4->ip_hdr.sadr = sadr;
1150
1151 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1152 pkt4->shdr.size = htons (size);
1153
1154 pkt4->tun.flags = 0;
1155 pkt4->tun.type = htons (0x0800);
1156
1157 pkt4->ip_hdr.version = 4;
1158 pkt4->ip_hdr.hdr_lngth = 5;
1159 pkt4->ip_hdr.diff_serv = 0;
1160 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1161 pkt4->ip_hdr.ident = 0;
1162 pkt4->ip_hdr.flags = 0;
1163 pkt4->ip_hdr.frag_off = 0;
1164 pkt4->ip_hdr.ttl = 255;
1165 pkt4->ip_hdr.proto = 0x06;
1166 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1167
1054 { 1168 {
1055 size_t size = pktlen + sizeof (struct ip_tcp) - 1; 1169 char *ipv4addr;
1056 1170 uint32_t dadr;
1057 struct ip_tcp *pkt4 = alloca (size); 1171
1058 GNUNET_assert (pkt4 != NULL); 1172 GNUNET_assert (GNUNET_OK ==
1059 memset (pkt4, 0, size); 1173 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
1060 1174 "IPV4ADDR",
1061 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK); 1175 &ipv4addr));
1062 uint32_t sadr; 1176 inet_pton (AF_INET, ipv4addr, &dadr);
1063 new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen); 1177 GNUNET_free (ipv4addr);
1064 pkt4->ip_hdr.sadr = sadr; 1178 pkt4->ip_hdr.dadr = dadr;
1065 1179 }
1066 pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1067 pkt4->shdr.size = htons (size);
1068
1069 pkt4->tun.flags = 0;
1070 pkt4->tun.type = htons (0x0800);
1071
1072 pkt4->ip_hdr.version = 4;
1073 pkt4->ip_hdr.hdr_lngth = 5;
1074 pkt4->ip_hdr.diff_serv = 0;
1075 pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
1076 pkt4->ip_hdr.ident = 0;
1077 pkt4->ip_hdr.flags = 0;
1078 pkt4->ip_hdr.frag_off = 0;
1079 pkt4->ip_hdr.ttl = 255;
1080 pkt4->ip_hdr.proto = 0x06;
1081 pkt4->ip_hdr.chks = 0; /* Will be calculated later */
1082 1180
1083 { 1181 memcpy (&pkt4->tcp_hdr, pkt, pktlen);
1084 char *ipv4addr; 1182
1085 uint32_t dadr; 1183 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
1086 GNUNET_assert (GNUNET_OK == 1184
1087 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", 1185 GNUNET_assert (key != NULL);
1088 "IPV4ADDR",
1089 &ipv4addr));
1090 inet_pton (AF_INET, ipv4addr, &dadr);
1091 GNUNET_free (ipv4addr);
1092 pkt4->ip_hdr.dadr = dadr;
1093 }
1094 1186
1095 memcpy (&pkt4->tcp_hdr, pkt, pktlen); 1187 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
1096 1188
1097 GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr); 1189 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1098 GNUNET_assert (key != NULL); 1190 GNUNET_TIME_absolute_get ().abs_value);
1099 1191
1100 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key); 1192 GNUNET_free (key);
1101 GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
1102 GNUNET_TIME_absolute_get ().
1103 abs_value);
1104 1193
1105 GNUNET_free (key); 1194 GNUNET_assert (me != NULL);
1195 pkt4->tcp_hdr.crc = 0;
1196 uint32_t sum = 0;
1197 uint32_t tmp;
1106 1198
1107 GNUNET_assert (me != NULL); 1199 tmp = pkt4->ip_hdr.sadr;
1108 pkt4->tcp_hdr.crc = 0; 1200 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1109 uint32_t sum = 0; 1201 tmp = pkt4->ip_hdr.dadr;
1110 uint32_t tmp; 1202 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1111 tmp = pkt4->ip_hdr.sadr;
1112 sum =
1113 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1114 tmp = pkt4->ip_hdr.dadr;
1115 sum =
1116 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1117 1203
1118 tmp = (0x06 << 16) | (0xffff & pktlen); 1204 tmp = (0x06 << 16) | (0xffff & pktlen);
1119 1205
1120 tmp = htonl(tmp); 1206 tmp = htonl (tmp);
1121 1207
1122 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 1208 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1123 1209
1124 sum = 1210 sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
1125 calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen); 1211 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1126 pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
1127 1212
1128 pkt4->ip_hdr.chks = 1213 pkt4->ip_hdr.chks =
1129 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4); 1214 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
1130 1215
1131 write_to_helper (pkt4, size); 1216 write_to_helper (pkt4, size);
1132 } 1217 }
1133 1218
1134 return GNUNET_OK; 1219 return GNUNET_OK;
1135} 1220}
@@ -1144,37 +1229,34 @@ receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel*
1144 */ 1229 */
1145static void 1230static void
1146run (void *cls, 1231run (void *cls,
1147 char *const *args __attribute__((unused)), 1232 char *const *args __attribute__ ((unused)),
1148 const char *cfgfilep __attribute__((unused)), 1233 const char *cfgfilep __attribute__ ((unused)),
1149 const struct GNUNET_CONFIGURATION_Handle *cfg_) 1234 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1150{ 1235{
1151 static const struct GNUNET_MESH_MessageHandler handlers[] = { 1236 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1152 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0}, 1237 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
1153 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0}, 1238 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
1154 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0}, 1239 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
1155 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0}, 1240 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
1156 {NULL, 0, 0} 1241 {NULL, 0, 0}
1157 }; 1242 };
1158 1243
1159 static const GNUNET_MESH_ApplicationType types[] = { 1244 static const GNUNET_MESH_ApplicationType types[] = {
1160 GNUNET_APPLICATION_TYPE_END 1245 GNUNET_APPLICATION_TYPE_END
1161 }; 1246 };
1162 1247
1163 mesh_handle = GNUNET_MESH_connect(cfg_, 1248 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, types);
1164 NULL, 1249 cfg = cfg_;
1165 NULL, 1250 restart_hijack = 0;
1166 handlers, 1251 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
1167 types); 1252 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1168 cfg = cfg_; 1253 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
1169 restart_hijack = 0; 1254 &max_mappings);
1170 hashmap = GNUNET_CONTAINER_multihashmap_create(65536); 1255 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1171 heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 1256 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1172 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg", 1257 shs_task =
1173 &max_mappings); 1258 GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1174 udp_connections = GNUNET_CONTAINER_multihashmap_create(65536); 1259 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1175 conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
1176 shs_task = GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
1177 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
1178} 1260}
1179 1261
1180/** 1262/**
@@ -1185,18 +1267,18 @@ run (void *cls,
1185 * @return 0 ok, 1 on error 1267 * @return 0 ok, 1 on error
1186 */ 1268 */
1187int 1269int
1188main (int argc, char *const *argv) { 1270main (int argc, char *const *argv)
1189 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 1271{
1190 GNUNET_GETOPT_OPTION_END 1272 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1191 }; 1273 GNUNET_GETOPT_OPTION_END
1192 1274 };
1193 return (GNUNET_OK == 1275
1194 GNUNET_PROGRAM_run (argc, 1276 return (GNUNET_OK ==
1195 argv, 1277 GNUNET_PROGRAM_run (argc,
1196 "vpn", 1278 argv,
1197 gettext_noop ("help text"), 1279 "vpn",
1198 options, &run, NULL)) ? ret : 1; 1280 gettext_noop ("help text"),
1281 options, &run, NULL)) ? ret : 1;
1199} 1282}
1200 1283
1201/* end of gnunet-daemon-vpn.c */ 1284/* end of gnunet-daemon-vpn.c */
1202