aboutsummaryrefslogtreecommitdiff
path: root/src/vpn
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/vpn
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/vpn')
-rw-r--r--src/vpn/gnunet-daemon-exit.c1271
-rw-r--r--src/vpn/gnunet-daemon-vpn-dns.c222
-rw-r--r--src/vpn/gnunet-daemon-vpn-dns.h6
-rw-r--r--src/vpn/gnunet-daemon-vpn-helper.c1002
-rw-r--r--src/vpn/gnunet-daemon-vpn-helper.h22
-rw-r--r--src/vpn/gnunet-daemon-vpn.c1572
-rw-r--r--src/vpn/gnunet-daemon-vpn.h40
-rw-r--r--src/vpn/gnunet-dns-parser.c271
-rw-r--r--src/vpn/gnunet-dns-parser.h4
-rw-r--r--src/vpn/gnunet-helper-hijack-dns.c185
-rw-r--r--src/vpn/gnunet-helper-vpn-api.c66
-rw-r--r--src/vpn/gnunet-helper-vpn.c596
-rw-r--r--src/vpn/gnunet-service-dns-p.h116
-rw-r--r--src/vpn/gnunet-service-dns.c1434
-rw-r--r--src/vpn/gnunet-vpn-checksum.c32
-rw-r--r--src/vpn/gnunet-vpn-checksum.h6
-rw-r--r--src/vpn/gnunet-vpn-packet.h411
17 files changed, 3806 insertions, 3450 deletions
diff --git a/src/vpn/gnunet-daemon-exit.c b/src/vpn/gnunet-daemon-exit.c
index ac07dd4d5..de906af3b 100644
--- a/src/vpn/gnunet-daemon-exit.c
+++ b/src/vpn/gnunet-daemon-exit.c
@@ -132,11 +132,12 @@ struct redirect_state
132 struct redirect_service *serv; 132 struct redirect_service *serv;
133 struct remote_addr remote; 133 struct remote_addr remote;
134 134
135 struct GNUNET_CONTAINER_HeapNode* heap_node; 135 struct GNUNET_CONTAINER_HeapNode *heap_node;
136 struct GNUNET_CONTAINER_MultiHashMap *hashmap; 136 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
137 GNUNET_HashCode hash; 137 GNUNET_HashCode hash;
138 138
139 enum { SERVICE, REMOTE } type; 139 enum
140 { SERVICE, REMOTE } type;
140 141
141 /** 142 /**
142 * The source-address and -port of this connection 143 * The source-address and -port of this connection
@@ -152,9 +153,9 @@ static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
152 153
153struct tunnel_notify_queue 154struct tunnel_notify_queue
154{ 155{
155 struct tunnel_notify_queue* next; 156 struct tunnel_notify_queue *next;
156 struct tunnel_notify_queue* prev; 157 struct tunnel_notify_queue *prev;
157 void* cls; 158 void *cls;
158 size_t len; 159 size_t len;
159}; 160};
160 161
@@ -162,9 +163,10 @@ struct tunnel_notify_queue
162 * Function that frees everything from a hashmap 163 * Function that frees everything from a hashmap
163 */ 164 */
164static int 165static int
165free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __attribute__((unused)), void* value) 166free_iterate (void *cls __attribute__ ((unused)), const GNUNET_HashCode * hash
167 __attribute__ ((unused)), void *value)
166{ 168{
167 GNUNET_free(value); 169 GNUNET_free (value);
168 return GNUNET_YES; 170 return GNUNET_YES;
169} 171}
170 172
@@ -172,46 +174,47 @@ free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __at
172 * Function scheduled as very last function, cleans up after us 174 * Function scheduled as very last function, cleans up after us
173 */ 175 */
174static void 176static void
175cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) { 177cleanup (void *cls
176 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); 178 __attribute__ ((unused)),
179 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
180{
181 GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
177 182
178 GNUNET_CONTAINER_multihashmap_iterate(udp_connections, 183 GNUNET_CONTAINER_multihashmap_iterate (udp_connections, free_iterate, NULL);
179 free_iterate,
180 NULL);
181 184
182 GNUNET_CONTAINER_multihashmap_iterate(tcp_connections, 185 GNUNET_CONTAINER_multihashmap_iterate (tcp_connections, free_iterate, NULL);
183 free_iterate,
184 NULL);
185 186
186 if (mesh_handle != NULL) 187 if (mesh_handle != NULL)
187 { 188 {
188 GNUNET_MESH_disconnect(mesh_handle); 189 GNUNET_MESH_disconnect (mesh_handle);
189 mesh_handle = NULL; 190 mesh_handle = NULL;
190 } 191 }
191} 192}
192 193
193static void 194static void
194collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) { 195collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) 196{
196 return; 197 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
198 return;
197 199
198 200
199 struct GNUNET_CONTAINER_Heap *heap = cls; 201 struct GNUNET_CONTAINER_Heap *heap = cls;
200 202
201 struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap); 203 struct redirect_state *state = GNUNET_CONTAINER_heap_remove_root (heap);
202 204
203 /* This is free()ed memory! */ 205 /* This is free()ed memory! */
204 state->heap_node = NULL; 206 state->heap_node = NULL;
205 207
206 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */ 208 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
207 209
208 GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state); 210 GNUNET_CONTAINER_multihashmap_remove (state->hashmap, &state->hash, state);
209 211
210 GNUNET_free(state); 212 GNUNET_free (state);
211} 213}
212 214
213static void 215static void
214hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen) 216hash_redirect_info (GNUNET_HashCode * hash, struct redirect_info *u_i,
217 size_t addrlen)
215{ 218{
216 219
217 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash 220 /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
@@ -219,9 +222,12 @@ hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addr
219 * build the hash out of the last bytes of the address and the 2 bytes of 222 * build the hash out of the last bytes of the address and the 2 bytes of
220 * the port 223 * the port
221 */ 224 */
222 memcpy(hash, &u_i->pt, sizeof(u_i->pt)); 225 memcpy (hash, &u_i->pt, sizeof (u_i->pt));
223 memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2)); 226 memcpy (((unsigned char *) hash) + 2,
224 memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int)); 227 u_i->addr + (addrlen - (sizeof (unsigned int) - 2)),
228 (sizeof (unsigned int) - 2));
229 memset (((unsigned char *) hash) + sizeof (unsigned int), 0,
230 sizeof (GNUNET_HashCode) - sizeof (unsigned int));
225} 231}
226 232
227/** 233/**
@@ -231,36 +237,41 @@ hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addr
231static size_t 237static size_t
232send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf) 238send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
233{ 239{
234 struct GNUNET_MESH_Tunnel** tunnel = cls; 240 struct GNUNET_MESH_Tunnel **tunnel = cls;
235 GNUNET_MESH_tunnel_set_data(*tunnel, NULL); 241
236 struct GNUNET_MessageHeader *hdr = (struct GNUNET_MessageHeader*)(tunnel + 1); 242 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
243 struct GNUNET_MessageHeader *hdr =
244 (struct GNUNET_MessageHeader *) (tunnel + 1);
237 GNUNET_assert (size >= ntohs (hdr->size)); 245 GNUNET_assert (size >= ntohs (hdr->size));
238 memcpy (buf, hdr, ntohs (hdr->size)); 246 memcpy (buf, hdr, ntohs (hdr->size));
239 size = ntohs(hdr->size); 247 size = ntohs (hdr->size);
240 248
241 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel)) 249 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
242 { 250 {
243 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel); 251 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
244 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel); 252 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
245 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel); 253 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
246 254
247 GNUNET_CONTAINER_DLL_remove(head, tail, element); 255 GNUNET_CONTAINER_DLL_remove (head, tail, element);
248 256
249 GNUNET_MESH_tunnel_set_head(*tunnel, head); 257 GNUNET_MESH_tunnel_set_head (*tunnel, head);
250 GNUNET_MESH_tunnel_set_tail(*tunnel, tail); 258 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
251 259
252 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel, 260 struct GNUNET_MESH_TransmitHandle *th =
253 GNUNET_NO, 261 GNUNET_MESH_notify_transmit_ready (*tunnel,
254 42, 262 GNUNET_NO,
255 GNUNET_TIME_relative_divide 263 42,
256 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 264 GNUNET_TIME_relative_divide
257 (const struct GNUNET_PeerIdentity *) 265 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
258 NULL, element->len, 266 (const struct GNUNET_PeerIdentity *)
259 send_udp_to_peer_notify_callback, element->cls); 267 NULL, element->len,
260 /* save the handle */ 268 send_udp_to_peer_notify_callback,
261 GNUNET_MESH_tunnel_set_data(*tunnel, th); 269 element->cls);
262 GNUNET_free(element); 270
263 } 271 /* save the handle */
272 GNUNET_MESH_tunnel_set_data (*tunnel, th);
273 GNUNET_free (element);
274 }
264 275
265 GNUNET_free (cls); 276 GNUNET_free (cls);
266 277
@@ -291,10 +302,11 @@ udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
291 302
292 /* get tunnel and service-descriptor from this */ 303 /* get tunnel and service-descriptor from this */
293 GNUNET_HashCode hash; 304 GNUNET_HashCode hash;
294 hash_redirect_info(&hash, &u_i, addrlen); 305
306 hash_redirect_info (&hash, &u_i, addrlen);
295 307
296 struct redirect_state *state = 308 struct redirect_state *state =
297 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash); 309 GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
298 310
299 /* Mark this connection as freshly used */ 311 /* Mark this connection as freshly used */
300 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node, 312 GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
@@ -303,80 +315,92 @@ udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
303 tunnel = state->tunnel; 315 tunnel = state->tunnel;
304 316
305 if (state->type == SERVICE) 317 if (state->type == SERVICE)
318 {
319 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
320 if (ntohs (udp->spt) == state->serv->remote_port)
306 { 321 {
307 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */ 322 udp->spt = htons (state->serv->my_port);
308 if (ntohs (udp->spt) == state->serv->remote_port) 323 }
309 { 324 else
310 udp->spt = htons (state->serv->my_port); 325 {
311 } 326 /* otherwise the answer came from a different port (tftp does this)
312 else 327 * add this new port to the list of all services, so that the packets
313 { 328 * coming back from the client to this new port will be routed correctly
314 /* otherwise the answer came from a different port (tftp does this) 329 */
315 * add this new port to the list of all services, so that the packets 330 struct redirect_service *serv =
316 * coming back from the client to this new port will be routed correctly 331 GNUNET_malloc (sizeof (struct redirect_service));
317 */ 332 memcpy (serv, state->serv, sizeof (struct redirect_service));
318 struct redirect_service *serv = 333 serv->my_port = ntohs (udp->spt);
319 GNUNET_malloc (sizeof (struct redirect_service)); 334 serv->remote_port = ntohs (udp->spt);
320 memcpy (serv, state->serv, sizeof (struct redirect_service)); 335 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
321 serv->my_port = ntohs (udp->spt); 336
322 serv->remote_port = ntohs (udp->spt); 337 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
323 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2); 338 sizeof (GNUNET_HashCode));
324 memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc, 339 *desc = ntohs (udp->spt);
325 sizeof (GNUNET_HashCode)); 340 GNUNET_assert (GNUNET_OK ==
326 *desc = ntohs (udp->spt); 341 GNUNET_CONTAINER_multihashmap_put (udp_services,
327 GNUNET_assert (GNUNET_OK == 342 (GNUNET_HashCode *)
328 GNUNET_CONTAINER_multihashmap_put (udp_services, 343 desc, serv,
329 (GNUNET_HashCode*)desc, serv, 344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 345
331 346 state->serv = serv;
332 state->serv = serv;
333 }
334 } 347 }
348 }
335 349
336 /* send udp-packet back */ 350 /* send udp-packet back */
337 len = 351 len =
338 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + 352 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
339 ntohs (udp->len); 353 ntohs (udp->len);
340 struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len); 354 struct GNUNET_MESH_Tunnel **ctunnel =
355 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
341 *ctunnel = tunnel; 356 *ctunnel = tunnel;
342 msg = (struct GNUNET_MessageHeader*)(ctunnel + 1); 357 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
343 msg->size = htons (len); 358 msg->size = htons (len);
344 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK); 359 msg->type =
360 htons (state->type ==
361 SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK :
362 GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
345 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1); 363 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
364
346 if (state->type == SERVICE) 365 if (state->type == SERVICE)
347 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode)); 366 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
348 else 367 else
349 memcpy (desc, &state->remote, sizeof (struct remote_addr)); 368 memcpy (desc, &state->remote, sizeof (struct remote_addr));
350 void *_udp = desc + 1; 369 void *_udp = desc + 1;
370
351 memcpy (_udp, udp, ntohs (udp->len)); 371 memcpy (_udp, udp, ntohs (udp->len));
352 372
353 if (NULL == GNUNET_MESH_tunnel_get_data(tunnel)) 373 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
354 { 374 {
355 /* No notify is pending */ 375 /* No notify is pending */
356 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel, 376 struct GNUNET_MESH_TransmitHandle *th =
357 GNUNET_NO, 377 GNUNET_MESH_notify_transmit_ready (tunnel,
358 42, 378 GNUNET_NO,
359 GNUNET_TIME_relative_divide 379 42,
360 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 380 GNUNET_TIME_relative_divide
361 (const struct GNUNET_PeerIdentity *) 381 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
362 NULL, len, 382 (const struct GNUNET_PeerIdentity *)
363 send_udp_to_peer_notify_callback, ctunnel); 383 NULL, len,
364 /* save the handle */ 384 send_udp_to_peer_notify_callback,
365 GNUNET_MESH_tunnel_set_data(tunnel, th); 385 ctunnel);
366 } 386
387 /* save the handle */
388 GNUNET_MESH_tunnel_set_data (tunnel, th);
389 }
367 else 390 else
368 { 391 {
369 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel); 392 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
370 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel); 393 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
371 394
372 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue)); 395 struct tunnel_notify_queue *element =
373 element->cls = ctunnel; 396 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
374 element->len = len; 397 element->cls = ctunnel;
375 398 element->len = len;
376 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element); 399
377 GNUNET_MESH_tunnel_set_head(tunnel, head); 400 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
378 GNUNET_MESH_tunnel_set_tail(tunnel, tail); 401 GNUNET_MESH_tunnel_set_head (tunnel, head);
379 } 402 GNUNET_MESH_tunnel_set_tail (tunnel, tail);
403 }
380} 404}
381 405
382/** 406/**
@@ -404,16 +428,19 @@ tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
404 428
405 /* get tunnel and service-descriptor from this */ 429 /* get tunnel and service-descriptor from this */
406 GNUNET_HashCode hash; 430 GNUNET_HashCode hash;
407 hash_redirect_info(&hash, &u_i, addrlen); 431
432 hash_redirect_info (&hash, &u_i, addrlen);
408 433
409 struct redirect_state *state = 434 struct redirect_state *state =
410 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash); 435 GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
411 436
412 if (state == NULL) 437 if (state == NULL)
413 { 438 {
414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No mapping for this connection; hash is %x\n", *((uint32_t*)&hash)); 439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 return; 440 "No mapping for this connection; hash is %x\n",
416 } 441 *((uint32_t *) & hash));
442 return;
443 }
417 444
418 /* Mark this connection as freshly used */ 445 /* Mark this connection as freshly used */
419 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node, 446 GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
@@ -422,61 +449,72 @@ tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
422 tunnel = state->tunnel; 449 tunnel = state->tunnel;
423 450
424 if (state->type == SERVICE) 451 if (state->type == SERVICE)
452 {
453 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
454 if (ntohs (tcp->spt) == state->serv->remote_port)
425 { 455 {
426 /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */ 456 tcp->spt = htons (state->serv->my_port);
427 if (ntohs (tcp->spt) == state->serv->remote_port) 457 }
428 { 458 else
429 tcp->spt = htons (state->serv->my_port); 459 {
430 } 460 // This is an illegal packet.
431 else 461 return;
432 {
433 // This is an illegal packet.
434 return;
435 }
436 } 462 }
463 }
437 464
438 /* send tcp-packet back */ 465 /* send tcp-packet back */
439 len = 466 len =
440 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen; 467 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen); 468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
442 struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len); 469 struct GNUNET_MESH_Tunnel **ctunnel =
470 GNUNET_malloc (sizeof (struct GNUNET_MESH_TUNNEL *) + len);
443 *ctunnel = tunnel; 471 *ctunnel = tunnel;
444 msg = (struct GNUNET_MessageHeader*)(ctunnel + 1); 472 msg = (struct GNUNET_MessageHeader *) (ctunnel + 1);
445 msg->size = htons (len); 473 msg->size = htons (len);
446 msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK); 474 msg->type =
475 htons (state->type ==
476 SERVICE ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK :
477 GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
447 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1); 478 GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
479
448 if (state->type == SERVICE) 480 if (state->type == SERVICE)
449 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode)); 481 memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
450 else 482 else
451 memcpy (desc, &state->remote, sizeof (struct remote_addr)); 483 memcpy (desc, &state->remote, sizeof (struct remote_addr));
452 void *_tcp = desc + 1; 484 void *_tcp = desc + 1;
485
453 memcpy (_tcp, tcp, pktlen); 486 memcpy (_tcp, tcp, pktlen);
454 487
455 if (NULL == GNUNET_MESH_tunnel_get_data(tunnel)) 488 if (NULL == GNUNET_MESH_tunnel_get_data (tunnel))
456 { 489 {
457 /* No notify is pending */ 490 /* No notify is pending */
458 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel, 491 struct GNUNET_MESH_TransmitHandle *th =
459 GNUNET_NO, 492 GNUNET_MESH_notify_transmit_ready (tunnel,
460 42, 493 GNUNET_NO,
461 GNUNET_TIME_relative_divide 494 42,
462 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 495 GNUNET_TIME_relative_divide
463 (const struct GNUNET_PeerIdentity *)NULL, 496 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
464 len, send_udp_to_peer_notify_callback, 497 (const struct GNUNET_PeerIdentity *)
465 ctunnel); 498 NULL,
466 /* save the handle */ 499 len,
467 GNUNET_MESH_tunnel_set_data(tunnel, th); 500 send_udp_to_peer_notify_callback,
468 } 501 ctunnel);
502
503 /* save the handle */
504 GNUNET_MESH_tunnel_set_data (tunnel, th);
505 }
469 else 506 else
470 { 507 {
471 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel); 508 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (tunnel);
472 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel); 509 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (tunnel);
473 510
474 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue)); 511 struct tunnel_notify_queue *element =
475 element->cls = ctunnel; 512 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
476 element->len = len; 513 element->cls = ctunnel;
514 element->len = len;
477 515
478 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element); 516 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
479 } 517 }
480} 518}
481 519
482 520
@@ -484,8 +522,10 @@ tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
484 * Receive packets from the helper-process 522 * Receive packets from the helper-process
485 */ 523 */
486static void 524static void
487message_token (void *cls __attribute__((unused)), 525message_token (void *cls __attribute__ ((unused)),
488 void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message) 526 void *client
527 __attribute__ ((unused)),
528 const struct GNUNET_MessageHeader *message)
489{ 529{
490 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER); 530 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
491 531
@@ -493,37 +533,40 @@ message_token (void *cls __attribute__((unused)),
493 533
494 /* ethertype is ipv6 */ 534 /* ethertype is ipv6 */
495 if (ntohs (pkt_tun->tun.type) == 0x86dd) 535 if (ntohs (pkt_tun->tun.type) == 0x86dd)
496 { 536 {
497 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun; 537 struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
498 if (0x11 == pkt6->ip6_hdr.nxthdr) 538
499 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr, 539 if (0x11 == pkt6->ip6_hdr.nxthdr)
500 (unsigned char *) &pkt6->ip6_hdr.dadr, 16); 540 udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
501 else if (0x06 == pkt6->ip6_hdr.nxthdr) 541 (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
502 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr, 542 else if (0x06 == pkt6->ip6_hdr.nxthdr)
503 (unsigned char *) &pkt6->ip6_hdr.dadr, 16, 543 tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
504 ntohs (pkt6->ip6_hdr.paylgth)); 544 (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
505 } 545 ntohs (pkt6->ip6_hdr.paylgth));
546 }
506 else if (ntohs (pkt_tun->tun.type) == 0x0800) 547 else if (ntohs (pkt_tun->tun.type) == 0x0800)
548 {
549 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
550 uint32_t tmp = pkt4->ip_hdr.dadr;
551
552 if (0x11 == pkt4->ip_hdr.proto)
553 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
554 (unsigned char *) &tmp, 4);
555 else if (0x06 == pkt4->ip_hdr.proto)
507 { 556 {
508 struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun; 557 size_t pktlen = ntohs (pkt4->ip_hdr.tot_lngth);
509 uint32_t tmp = pkt4->ip_hdr.dadr; 558
510 if (0x11 == pkt4->ip_hdr.proto) 559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
511 udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr, 560 pktlen -= 4 * pkt4->ip_hdr.hdr_lngth;
512 (unsigned char *) &tmp, 4); 561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
513 else if (0x06 == pkt4->ip_hdr.proto) 562 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
514 { 563 (unsigned char *) &tmp, 4, pktlen);
515 size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
517 pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
519 tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
520 (unsigned char *) &tmp, 4, pktlen);
521 }
522 } 564 }
565 }
523 else 566 else
524 { 567 {
525 return; 568 return;
526 } 569 }
527} 570}
528 571
529/** 572/**
@@ -533,18 +576,21 @@ message_token (void *cls __attribute__((unused)),
533 * @param section name of section in config, equal to hostname 576 * @param section name of section in config, equal to hostname
534 */ 577 */
535static void 578static void
536read_service_conf (void *cls __attribute__((unused)), const char *section) 579read_service_conf (void *cls __attribute__ ((unused)), const char *section)
537{ 580{
538 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8)))) 581 if ((strlen (section) < 8) ||
582 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
539 return; 583 return;
540 584
541 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", strlen(section), section, section + (strlen(section) - 8)); 585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n",
586 strlen (section), section, section + (strlen (section) - 8));
542 587
543 char *cpy; 588 char *cpy;
544 char *redirect; 589 char *redirect;
545 char *hostname; 590 char *hostname;
546 char *hostport; 591 char *hostport;
547 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2); 592 uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
593
548 GNUNET_CRYPTO_hash (section, strlen (section) + 1, 594 GNUNET_CRYPTO_hash (section, strlen (section) + 1,
549 (GNUNET_HashCode *) (desc + 1)); 595 (GNUNET_HashCode *) (desc + 1));
550 596
@@ -554,96 +600,105 @@ read_service_conf (void *cls __attribute__((unused)), const char *section)
554 int proto = UDP; 600 int proto = UDP;
555 601
556 do 602 do
603 {
604 if (proto == UDP &&
605 (GNUNET_OK !=
606 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
607 &cpy)))
608 goto next;
609 else if (proto == TCP &&
610 (GNUNET_OK !=
611 GNUNET_CONFIGURATION_get_value_string (cfg, section,
612 "TCP_REDIRECTS", &cpy)))
613 goto next;
614
615 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
616 (NULL, " "))
557 { 617 {
558 if (proto == UDP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "UDP_REDIRECTS", &cpy))) 618 if (NULL == (hostname = strstr (redirect, ":")))
559 goto next; 619 {
560 else if (proto == TCP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "TCP_REDIRECTS", &cpy))) 620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
561 goto next; 621 "Warning: option %s is not formatted correctly!\n",
562 622 redirect);
563 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok 623 continue;
564 (NULL, " ")) 624 }
565 { 625 hostname[0] = '\0';
566 if (NULL == (hostname = strstr (redirect, ":"))) 626 hostname++;
567 { 627 if (NULL == (hostport = strstr (hostname, ":")))
568 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", 628 {
569 redirect); 629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
570 continue; 630 "Warning: option %s is not formatted correctly!\n",
571 } 631 redirect);
572 hostname[0] = '\0'; 632 continue;
573 hostname++; 633 }
574 if (NULL == (hostport = strstr (hostname, ":"))) 634 hostport[0] = '\0';
575 { 635 hostport++;
576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", 636
577 redirect); 637 int local_port = atoi (redirect);
578 continue; 638
579 } 639 if (!((local_port > 0) && (local_port < 65536)))
580 hostport[0] = '\0'; 640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
581 hostport++; 641 "Warning: %s is not a correct port.", redirect);
582 642
583 int local_port = atoi (redirect); 643 *desc = local_port;
584 if (!((local_port > 0) && (local_port < 65536))) 644
585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", 645 struct redirect_service *serv =
586 redirect); 646 GNUNET_malloc (sizeof (struct redirect_service));
587 647 memset (serv, 0, sizeof (struct redirect_service));
588 *desc = local_port; 648 serv->my_port = local_port;
589 649
590 struct redirect_service *serv = 650 if (0 == strcmp ("localhost4", hostname))
591 GNUNET_malloc (sizeof (struct redirect_service)); 651 {
592 memset (serv, 0, sizeof (struct redirect_service)); 652 serv->version = 4;
593 serv->my_port = local_port; 653
594 654 char *ip4addr;
595 if (0 == strcmp ("localhost4", hostname)) 655
596 { 656 GNUNET_assert (GNUNET_OK ==
597 serv->version = 4; 657 GNUNET_CONFIGURATION_get_value_string (cfg,
598 658 "exit",
599 char *ip4addr; 659 "IPV4ADDR",
600 GNUNET_assert (GNUNET_OK == 660 &ip4addr));
601 GNUNET_CONFIGURATION_get_value_string (cfg, 661 GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, serv->v4.ip4address));
602 "exit", 662 GNUNET_free (ip4addr);
603 "IPV4ADDR", 663 }
604 &ip4addr)); 664 else if (0 == strcmp ("localhost6", hostname))
605 GNUNET_assert (1 == 665 {
606 inet_pton (AF_INET, ip4addr, 666 serv->version = 6;
607 serv->v4.ip4address)); 667
608 GNUNET_free (ip4addr); 668 char *ip6addr;
609 } 669
610 else if (0 == strcmp ("localhost6", hostname)) 670 GNUNET_assert (GNUNET_OK ==
611 { 671 GNUNET_CONFIGURATION_get_value_string (cfg,
612 serv->version = 6; 672 "exit",
613 673 "IPV6ADDR",
614 char *ip6addr; 674 &ip6addr));
615 GNUNET_assert (GNUNET_OK == 675 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, serv->v6.ip6address));
616 GNUNET_CONFIGURATION_get_value_string (cfg, 676 GNUNET_free (ip6addr);
617 "exit", 677 }
618 "IPV6ADDR", 678 else
619 &ip6addr)); 679 {
620 GNUNET_assert (1 == 680 // TODO Lookup, yadayadayada
621 inet_pton (AF_INET6, ip6addr, 681 GNUNET_assert (0);
622 serv->v6.ip6address)); 682 }
623 GNUNET_free (ip6addr); 683 serv->remote_port = atoi (hostport);
624 } 684 if (UDP == proto)
625 else 685 GNUNET_assert (GNUNET_OK ==
626 { 686 GNUNET_CONTAINER_multihashmap_put (udp_services,
627 // TODO Lookup, yadayadayada 687 (GNUNET_HashCode *)
628 GNUNET_assert (0); 688 desc, serv,
629 } 689 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
630 serv->remote_port = atoi (hostport); 690 else
631 if (UDP == proto) 691 GNUNET_assert (GNUNET_OK ==
632 GNUNET_assert (GNUNET_OK == 692 GNUNET_CONTAINER_multihashmap_put (tcp_services,
633 GNUNET_CONTAINER_multihashmap_put (udp_services, 693 (GNUNET_HashCode *)
634 (GNUNET_HashCode*)desc, serv, 694 desc, serv,
635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
636 else 696
637 GNUNET_assert (GNUNET_OK ==
638 GNUNET_CONTAINER_multihashmap_put (tcp_services,
639 (GNUNET_HashCode*)desc, serv,
640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
641
642 }
643 GNUNET_free (cpy);
644next:
645 proto = (proto == UDP) ? TCP : UDP;
646 } 697 }
698 GNUNET_free (cpy);
699next:
700 proto = (proto == UDP) ? TCP : UDP;
701 }
647 while (proto != UDP); 702 while (proto != UDP);
648} 703}
649 704
@@ -654,71 +709,84 @@ next:
654 * helper. cls is then taken as handle to the old helper and is cleaned up. 709 * helper. cls is then taken as handle to the old helper and is cleaned up.
655 */ 710 */
656static void 711static void
657start_helper_and_schedule(void *cls, 712start_helper_and_schedule (void *cls,
658 const struct GNUNET_SCHEDULER_TaskContext *tc) { 713 const struct GNUNET_SCHEDULER_TaskContext *tc)
659 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 714{
660 return; 715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
661 716 return;
662 if (cls != NULL)
663 cleanup_helper(cls);
664 cls = NULL;
665
666 char* ifname;
667 char* ipv6addr;
668 char* ipv6prefix;
669 char* ipv4addr;
670 char* ipv4mask;
671
672 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
673 {
674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
675 exit(1);
676 }
677
678 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
679 {
680 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
681 exit(1);
682 }
683 717
684 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix)) 718 if (cls != NULL)
685 { 719 cleanup_helper (cls);
686 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n"); 720 cls = NULL;
687 exit(1);
688 }
689 721
690 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr)) 722 char *ifname;
691 { 723 char *ipv6addr;
692 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n"); 724 char *ipv6prefix;
693 exit(1); 725 char *ipv4addr;
694 } 726 char *ipv4mask;
695 727
696 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask)) 728 if (GNUNET_SYSERR ==
697 { 729 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IFNAME", &ifname))
698 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n"); 730 {
699 exit(1); 731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
700 } 732 "No entry 'IFNAME' in configuration!\n");
733 exit (1);
734 }
735
736 if (GNUNET_SYSERR ==
737 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
738 &ipv6addr))
739 {
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 "No entry 'IPV6ADDR' in configuration!\n");
742 exit (1);
743 }
744
745 if (GNUNET_SYSERR ==
746 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
747 &ipv6prefix))
748 {
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
750 "No entry 'IPV6PREFIX' in configuration!\n");
751 exit (1);
752 }
753
754 if (GNUNET_SYSERR ==
755 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
756 &ipv4addr))
757 {
758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
759 "No entry 'IPV4ADDR' in configuration!\n");
760 exit (1);
761 }
762
763 if (GNUNET_SYSERR ==
764 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
765 &ipv4mask))
766 {
767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768 "No entry 'IPV4MASK' in configuration!\n");
769 exit (1);
770 }
771
772 /* Start the helper
773 * Messages get passed to the function message_token
774 * When the helper dies, this function will be called again with the
775 * helper_handle as cls.
776 */
777 helper_handle = start_helper (ifname,
778 ipv6addr,
779 ipv6prefix,
780 ipv4addr,
781 ipv4mask,
782 "exit-gnunet",
783 start_helper_and_schedule, message_token, NULL);
701 784
702 /* Start the helper 785 GNUNET_free (ipv6addr);
703 * Messages get passed to the function message_token 786 GNUNET_free (ipv6prefix);
704 * When the helper dies, this function will be called again with the 787 GNUNET_free (ipv4addr);
705 * helper_handle as cls. 788 GNUNET_free (ipv4mask);
706 */ 789 GNUNET_free (ifname);
707 helper_handle = start_helper(ifname,
708 ipv6addr,
709 ipv6prefix,
710 ipv4addr,
711 ipv4mask,
712 "exit-gnunet",
713 start_helper_and_schedule,
714 message_token,
715 NULL);
716
717 GNUNET_free(ipv6addr);
718 GNUNET_free(ipv6prefix);
719 GNUNET_free(ipv4addr);
720 GNUNET_free(ipv4mask);
721 GNUNET_free(ifname);
722} 790}
723 791
724static void 792static void
@@ -752,14 +820,13 @@ prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
752 /* Generate a new src-address */ 820 /* Generate a new src-address */
753 char *ipv4addr; 821 char *ipv4addr;
754 char *ipv4mask; 822 char *ipv4mask;
823
755 GNUNET_assert (GNUNET_OK == 824 GNUNET_assert (GNUNET_OK ==
756 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", 825 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
757 "IPV4ADDR", 826 "IPV4ADDR", &ipv4addr));
758 &ipv4addr));
759 GNUNET_assert (GNUNET_OK == 827 GNUNET_assert (GNUNET_OK ==
760 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", 828 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
761 "IPV4MASK", 829 "IPV4MASK", &ipv4mask));
762 &ipv4mask));
763 inet_pton (AF_INET, ipv4addr, &tmp); 830 inet_pton (AF_INET, ipv4addr, &tmp);
764 inet_pton (AF_INET, ipv4mask, &tmp2); 831 inet_pton (AF_INET, ipv4mask, &tmp2);
765 GNUNET_free (ipv4addr); 832 GNUNET_free (ipv4addr);
@@ -774,41 +841,44 @@ prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
774 841
775 memcpy (&state->redirect_info.addr, &tmp, 4); 842 memcpy (&state->redirect_info.addr, &tmp, 4);
776 if (0x11 == protocol) 843 if (0x11 == protocol)
777 { 844 {
778 struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4; 845 struct ip_udp *pkt4_udp = (struct ip_udp *) pkt4;
779 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
780 846
781 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */ 847 state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
782 } 848
849 pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
850 }
783 else if (0x06 == protocol) 851 else if (0x06 == protocol)
784 { 852 {
785 struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4; 853 struct ip_tcp *pkt4_tcp = (struct ip_tcp *) pkt4;
786 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
787 854
788 pkt4_tcp->tcp_hdr.crc = 0; 855 state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
789 uint32_t sum = 0;
790 tmp = pkt4->ip_hdr.sadr;
791 sum =
792 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
793 tmp = pkt4->ip_hdr.dadr;
794 sum =
795 calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
796 856
797 tmp = (protocol << 16) | (0xffff & pktlen); 857 pkt4_tcp->tcp_hdr.crc = 0;
858 uint32_t sum = 0;
798 859
799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen)); 860 tmp = pkt4->ip_hdr.sadr;
861 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
862 tmp = pkt4->ip_hdr.dadr;
863 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
800 864
801 tmp = htonl(tmp); 865 tmp = (protocol << 16) | (0xffff & pktlen);
802 866
803 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp,
868 (0xffff & pktlen));
804 869
805 sum = 870 tmp = htonl (tmp);
806 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen); 871
807 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum); 872 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
808 } 873
874 sum =
875 calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr,
876 pktlen);
877 pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
878 }
809 879
810 pkt4->ip_hdr.chks = 880 pkt4->ip_hdr.chks =
811 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4); 881 calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
812} 882}
813 883
814static void 884static void
@@ -838,10 +908,10 @@ prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
838 * the host-mask*/ 908 * the host-mask*/
839 char *ipv6addr; 909 char *ipv6addr;
840 unsigned long long ipv6prefix; 910 unsigned long long ipv6prefix;
911
841 GNUNET_assert (GNUNET_OK == 912 GNUNET_assert (GNUNET_OK ==
842 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", 913 GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
843 "IPV6ADDR", 914 "IPV6ADDR", &ipv6addr));
844 &ipv6addr));
845 GNUNET_assert (GNUNET_OK == 915 GNUNET_assert (GNUNET_OK ==
846 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", 916 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
847 "IPV6PREFIX", 917 "IPV6PREFIX",
@@ -856,6 +926,7 @@ prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
856 ipv6prefix = 16 - sizeof (void *); 926 ipv6prefix = 16 - sizeof (void *);
857 927
858 unsigned int offset = ipv6prefix - (16 - sizeof (void *)); 928 unsigned int offset = ipv6prefix - (16 - sizeof (void *));
929
859 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix, 930 memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
860 ((char *) &tunnel) + offset, 16 - ipv6prefix); 931 ((char *) &tunnel) + offset, 16 - ipv6prefix);
861 932
@@ -863,66 +934,74 @@ prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
863 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16); 934 memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
864 935
865 if (0x11 == protocol) 936 if (0x11 == protocol)
866 { 937 {
867 struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6; 938 struct ip6_udp *pkt6_udp = (struct ip6_udp *) pkt6;
868 state->redirect_info.pt = pkt6_udp->udp_hdr.spt; 939
869 940 state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
870 pkt6_udp->udp_hdr.crc = 0; 941
871 uint32_t sum = 0; 942 pkt6_udp->udp_hdr.crc = 0;
872 sum = 943 uint32_t sum = 0;
873 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16); 944
874 sum = 945 sum =
875 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16); 946 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr,
876 tmp = (htons (pktlen) & 0xffff); 947 16);
877 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 948 sum =
878 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff))); 949 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr,
879 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 950 16);
880 951 tmp = (htons (pktlen) & 0xffff);
881 sum = 952 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
953 tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
954 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
955
956 sum =
882 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr, 957 calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
883 ntohs (pkt6_udp->udp_hdr.len)); 958 ntohs (pkt6_udp->udp_hdr.len));
884 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum); 959 pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
885 } 960 }
886 else if (0x06 == protocol) 961 else if (0x06 == protocol)
887 { 962 {
888 struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6; 963 struct ip6_tcp *pkt6_tcp = (struct ip6_tcp *) pkt6;
889 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt; 964
965 state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
966
967 pkt6_tcp->tcp_hdr.crc = 0;
968 uint32_t sum = 0;
890 969
891 pkt6_tcp->tcp_hdr.crc = 0; 970 sum =
892 uint32_t sum = 0;
893 sum =
894 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16); 971 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
895 sum = 972 sum =
896 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16); 973 calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
897 tmp = htonl(pktlen); 974 tmp = htonl (pktlen);
898 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 975 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
899 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff))); 976 tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
900 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 977 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
901 978
902 sum = 979 sum =
903 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr, 980 calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
904 ntohs (pkt6->ip6_hdr.paylgth)); 981 ntohs (pkt6->ip6_hdr.paylgth));
905 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum); 982 pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
906 } 983 }
907} 984}
908 985
909/** 986/**
910 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt 987 * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
911 */ 988 */
912static int 989static int
913receive_tcp_service (void *cls __attribute__((unused)), 990receive_tcp_service (void *cls __attribute__ ((unused)),
914 struct GNUNET_MESH_Tunnel *tunnel, 991 struct GNUNET_MESH_Tunnel *tunnel,
915 void **tunnel_ctx __attribute__((unused)), 992 void **tunnel_ctx __attribute__ ((unused)),
916 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 993 const struct GNUNET_PeerIdentity *sender
994 __attribute__ ((unused)),
917 const struct GNUNET_MessageHeader *message, 995 const struct GNUNET_MessageHeader *message,
918 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 996 const struct GNUNET_TRANSPORT_ATS_Information *atsi
997 __attribute__ ((unused)))
919{ 998{
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n"); 999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
921 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1000 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
922 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1); 1001 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
923 unsigned int pkt_len = ntohs (message->size) - sizeof (struct 1002 unsigned int pkt_len = ntohs (message->size) - sizeof (struct
924 GNUNET_MessageHeader) 1003 GNUNET_MessageHeader)
925 - sizeof (GNUNET_HashCode); 1004 - sizeof (GNUNET_HashCode);
926 1005
927 /** Get the configuration from the services-hashmap. 1006 /** Get the configuration from the services-hashmap.
928 * 1007 *
@@ -934,13 +1013,15 @@ receive_tcp_service (void *cls __attribute__((unused)),
934 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode)); 1013 memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
935 *tcp_desc = ntohs (pkt->dpt); 1014 *tcp_desc = ntohs (pkt->dpt);
936 struct redirect_service *serv = 1015 struct redirect_service *serv =
937 GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc); 1016 GNUNET_CONTAINER_multihashmap_get (tcp_services,
1017 (GNUNET_HashCode *) tcp_desc);
1018
938 if (NULL == serv) 1019 if (NULL == serv)
939 { 1020 {
940 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
941 "No service found for TCP dpt %d!\n", *tcp_desc); 1022 "No service found for TCP dpt %d!\n", *tcp_desc);
942 return GNUNET_YES; 1023 return GNUNET_YES;
943 } 1024 }
944 1025
945 pkt->dpt = htons (serv->remote_port); 1026 pkt->dpt = htons (serv->remote_port);
946 1027
@@ -955,8 +1036,8 @@ receive_tcp_service (void *cls __attribute__((unused)),
955 * This will be saved in the hashmap, so that the receiving procedure knows 1036 * This will be saved in the hashmap, so that the receiving procedure knows
956 * through which tunnel this connection has to be routed. 1037 * through which tunnel this connection has to be routed.
957 */ 1038 */
958 struct redirect_state *state = 1039 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
959 GNUNET_malloc (sizeof (struct redirect_state)); 1040
960 memset (state, 0, sizeof (struct redirect_state)); 1041 memset (state, 0, sizeof (struct redirect_state));
961 state->tunnel = tunnel; 1042 state->tunnel = tunnel;
962 state->serv = serv; 1043 state->serv = serv;
@@ -965,44 +1046,46 @@ receive_tcp_service (void *cls __attribute__((unused)),
965 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode)); 1046 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
966 1047
967 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + 1048 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
968 sizeof (struct ip6_hdr) + pkt_len; 1049 sizeof (struct ip6_hdr) + pkt_len;
969 buf = alloca (len); 1050 buf = alloca (len);
970 1051
971 memset (buf, 0, len); 1052 memset (buf, 0, len);
972 1053
973 switch (serv->version) 1054 switch (serv->version)
974 { 1055 {
975 case 4: 1056 case 4:
976 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */ 1057 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
977 &serv->v4.ip4address, 1058 &serv->v4.ip4address,
978 tunnel, state, (struct ip_pkt *) buf); 1059 tunnel, state, (struct ip_pkt *) buf);
979 break; 1060 break;
980 case 6: 1061 case 6:
981 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */ 1062 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
982 &serv->v6.ip6address, 1063 &serv->v6.ip6address,
983 tunnel, state, (struct ip6_pkt *) buf); 1064 tunnel, state, (struct ip6_pkt *) buf);
984 1065
985 break; 1066 break;
986 default: 1067 default:
987 GNUNET_assert (0); 1068 GNUNET_assert (0);
988 break; 1069 break;
989 } 1070 }
990 1071
991 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16); 1072 hash_redirect_info (&state->hash, &state->redirect_info,
1073 serv->version == 4 ? 4 : 16);
992 1074
993 if (GNUNET_NO == 1075 if (GNUNET_NO ==
994 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash)) 1076 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
995 { 1077 {
996 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state, 1078 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
997 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
998 1080
999 state->heap_node = 1081 state->heap_node =
1000 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state, 1082 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1001 GNUNET_TIME_absolute_get ().abs_value); 1083 GNUNET_TIME_absolute_get ().abs_value);
1002 1084
1003 if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections) 1085 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1004 GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap); 1086 max_tcp_connections)
1005 } 1087 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1088 }
1006 else 1089 else
1007 GNUNET_free (state); 1090 GNUNET_free (state);
1008 1091
@@ -1011,22 +1094,26 @@ receive_tcp_service (void *cls __attribute__((unused)),
1011} 1094}
1012 1095
1013static int 1096static int
1014receive_tcp_remote (void *cls __attribute__((unused)), 1097receive_tcp_remote (void *cls __attribute__ ((unused)),
1015 struct GNUNET_MESH_Tunnel *tunnel, 1098 struct GNUNET_MESH_Tunnel *tunnel,
1016 void **tunnel_ctx __attribute__((unused)), 1099 void **tunnel_ctx __attribute__ ((unused)),
1017 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 1100 const struct GNUNET_PeerIdentity *sender
1018 const struct GNUNET_MessageHeader *message, 1101 __attribute__ ((unused)),
1019 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 1102 const struct GNUNET_MessageHeader *message,
1103 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1104 __attribute__ ((unused)))
1020{ 1105{
1021 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1106 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1022 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1); 1107 struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
1023 struct remote_addr *s = (struct remote_addr *) desc; 1108 struct remote_addr *s = (struct remote_addr *) desc;
1024 char *buf; 1109 char *buf;
1025 size_t len; 1110 size_t len;
1026 unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode); 1111 unsigned int pkt_len =
1112 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1113 sizeof (GNUNET_HashCode);
1114
1115 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1027 1116
1028 struct redirect_state *state =
1029 GNUNET_malloc (sizeof (struct redirect_state));
1030 memset (state, 0, sizeof (struct redirect_state)); 1117 memset (state, 0, sizeof (struct redirect_state));
1031 state->tunnel = tunnel; 1118 state->tunnel = tunnel;
1032 state->type = REMOTE; 1119 state->type = REMOTE;
@@ -1034,44 +1121,45 @@ receive_tcp_remote (void *cls __attribute__((unused)),
1034 memcpy (&state->remote, s, sizeof (struct remote_addr)); 1121 memcpy (&state->remote, s, sizeof (struct remote_addr));
1035 1122
1036 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + 1123 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1037 sizeof (struct ip6_hdr) + pkt_len; 1124 sizeof (struct ip6_hdr) + pkt_len;
1038 buf = alloca (len); 1125 buf = alloca (len);
1039 1126
1040 memset (buf, 0, len); 1127 memset (buf, 0, len);
1041 1128
1042 switch (s->addrlen) 1129 switch (s->addrlen)
1043 { 1130 {
1044 case 4: 1131 case 4:
1045 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */ 1132 prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
1046 &s->addr, tunnel, state, (struct ip_pkt *) buf); 1133 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1047 break; 1134 break;
1048 case 16: 1135 case 16:
1049 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */ 1136 prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
1050 &s->addr, tunnel, state, (struct ip6_pkt *) buf); 1137 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1051 break; 1138 break;
1052 default: 1139 default:
1053 return GNUNET_SYSERR; 1140 return GNUNET_SYSERR;
1054 break; 1141 break;
1055 } 1142 }
1056 1143
1057 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen); 1144 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1058 1145
1059 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n", *((uint32_t*)&state->hash)); 1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n",
1147 *((uint32_t *) & state->hash));
1060 1148
1061 if (GNUNET_NO == 1149 if (GNUNET_NO ==
1062 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash)) 1150 GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
1063 { 1151 {
1064 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state, 1152 GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
1065 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1153 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1066 1154
1067 state->heap_node = 1155 state->heap_node =
1068 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state, 1156 GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
1069 GNUNET_TIME_absolute_get ().abs_value); 1157 GNUNET_TIME_absolute_get ().abs_value);
1070 1158
1071 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) > 1159 if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
1072 max_tcp_connections) 1160 max_tcp_connections)
1073 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap); 1161 GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
1074 } 1162 }
1075 else 1163 else
1076 GNUNET_free (state); 1164 GNUNET_free (state);
1077 1165
@@ -1081,12 +1169,14 @@ receive_tcp_remote (void *cls __attribute__((unused)),
1081} 1169}
1082 1170
1083static int 1171static int
1084receive_udp_remote (void *cls __attribute__((unused)), 1172receive_udp_remote (void *cls __attribute__ ((unused)),
1085 struct GNUNET_MESH_Tunnel *tunnel, 1173 struct GNUNET_MESH_Tunnel *tunnel,
1086 void **tunnel_ctx __attribute__((unused)), 1174 void **tunnel_ctx __attribute__ ((unused)),
1087 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 1175 const struct GNUNET_PeerIdentity *sender
1176 __attribute__ ((unused)),
1088 const struct GNUNET_MessageHeader *message, 1177 const struct GNUNET_MessageHeader *message,
1089 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 1178 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1179 __attribute__ ((unused)))
1090{ 1180{
1091 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1181 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1092 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); 1182 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
@@ -1103,8 +1193,8 @@ receive_udp_remote (void *cls __attribute__((unused)),
1103 * This will be saved in the hashmap, so that the receiving procedure knows 1193 * This will be saved in the hashmap, so that the receiving procedure knows
1104 * through which tunnel this connection has to be routed. 1194 * through which tunnel this connection has to be routed.
1105 */ 1195 */
1106 struct redirect_state *state = 1196 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1107 GNUNET_malloc (sizeof (struct redirect_state)); 1197
1108 memset (state, 0, sizeof (struct redirect_state)); 1198 memset (state, 0, sizeof (struct redirect_state));
1109 state->tunnel = tunnel; 1199 state->tunnel = tunnel;
1110 state->hashmap = udp_connections; 1200 state->hashmap = udp_connections;
@@ -1112,42 +1202,42 @@ receive_udp_remote (void *cls __attribute__((unused)),
1112 memcpy (&state->remote, s, sizeof (struct remote_addr)); 1202 memcpy (&state->remote, s, sizeof (struct remote_addr));
1113 1203
1114 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + 1204 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1115 sizeof (struct ip6_hdr) + ntohs (pkt->len); 1205 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1116 buf = alloca (len); 1206 buf = alloca (len);
1117 1207
1118 memset (buf, 0, len); 1208 memset (buf, 0, len);
1119 1209
1120 switch (s->addrlen) 1210 switch (s->addrlen)
1121 { 1211 {
1122 case 4: 1212 case 4:
1123 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */ 1213 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1124 &s->addr, tunnel, state, (struct ip_pkt *) buf); 1214 &s->addr, tunnel, state, (struct ip_pkt *) buf);
1125 break; 1215 break;
1126 case 16: 1216 case 16:
1127 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */ 1217 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1128 &s->addr, tunnel, state, (struct ip6_pkt *) buf); 1218 &s->addr, tunnel, state, (struct ip6_pkt *) buf);
1129 break; 1219 break;
1130 default: 1220 default:
1131 GNUNET_assert (0); 1221 GNUNET_assert (0);
1132 break; 1222 break;
1133 } 1223 }
1134 1224
1135 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen); 1225 hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
1136 1226
1137 if (GNUNET_NO == 1227 if (GNUNET_NO ==
1138 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash)) 1228 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1139 { 1229 {
1140 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state, 1230 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1231 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1142 1232
1143 state->heap_node = 1233 state->heap_node =
1144 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state, 1234 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1145 GNUNET_TIME_absolute_get ().abs_value); 1235 GNUNET_TIME_absolute_get ().abs_value);
1146 1236
1147 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) > 1237 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1148 max_udp_connections) 1238 max_udp_connections)
1149 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap); 1239 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1150 } 1240 }
1151 else 1241 else
1152 GNUNET_free (state); 1242 GNUNET_free (state);
1153 1243
@@ -1159,12 +1249,14 @@ receive_udp_remote (void *cls __attribute__((unused)),
1159 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt 1249 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1160 */ 1250 */
1161static int 1251static int
1162receive_udp_service (void *cls __attribute__((unused)), 1252receive_udp_service (void *cls __attribute__ ((unused)),
1163 struct GNUNET_MESH_Tunnel *tunnel, 1253 struct GNUNET_MESH_Tunnel *tunnel,
1164 void **tunnel_ctx __attribute__((unused)), 1254 void **tunnel_ctx __attribute__ ((unused)),
1165 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 1255 const struct GNUNET_PeerIdentity *sender
1256 __attribute__ ((unused)),
1166 const struct GNUNET_MessageHeader *message, 1257 const struct GNUNET_MessageHeader *message,
1167 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 1258 const struct GNUNET_TRANSPORT_ATS_Information *atsi
1259 __attribute__ ((unused)))
1168{ 1260{
1169 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1261 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1170 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); 1262 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
@@ -1176,16 +1268,19 @@ receive_udp_service (void *cls __attribute__((unused)),
1176 1268
1177 /* Get the configuration from the hashmap */ 1269 /* Get the configuration from the hashmap */
1178 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2); 1270 uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
1271
1179 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode)); 1272 memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
1180 *udp_desc = ntohs (pkt->dpt); 1273 *udp_desc = ntohs (pkt->dpt);
1181 struct redirect_service *serv = 1274 struct redirect_service *serv =
1182 GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc); 1275 GNUNET_CONTAINER_multihashmap_get (udp_services,
1276 (GNUNET_HashCode *) udp_desc);
1277
1183 if (NULL == serv) 1278 if (NULL == serv)
1184 { 1279 {
1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 "No service found for UDP dpt %d!\n", *udp_desc); 1281 "No service found for UDP dpt %d!\n", *udp_desc);
1187 return GNUNET_YES; 1282 return GNUNET_YES;
1188 } 1283 }
1189 1284
1190 pkt->dpt = htons (serv->remote_port); 1285 pkt->dpt = htons (serv->remote_port);
1191 1286
@@ -1200,8 +1295,8 @@ receive_udp_service (void *cls __attribute__((unused)),
1200 * This will be saved in the hashmap, so that the receiving procedure knows 1295 * This will be saved in the hashmap, so that the receiving procedure knows
1201 * through which tunnel this connection has to be routed. 1296 * through which tunnel this connection has to be routed.
1202 */ 1297 */
1203 struct redirect_state *state = 1298 struct redirect_state *state = GNUNET_malloc (sizeof (struct redirect_state));
1204 GNUNET_malloc (sizeof (struct redirect_state)); 1299
1205 memset (state, 0, sizeof (struct redirect_state)); 1300 memset (state, 0, sizeof (struct redirect_state));
1206 state->tunnel = tunnel; 1301 state->tunnel = tunnel;
1207 state->serv = serv; 1302 state->serv = serv;
@@ -1210,44 +1305,46 @@ receive_udp_service (void *cls __attribute__((unused)),
1210 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode)); 1305 memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
1211 1306
1212 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + 1307 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
1213 sizeof (struct ip6_hdr) + ntohs (pkt->len); 1308 sizeof (struct ip6_hdr) + ntohs (pkt->len);
1214 buf = alloca (len); 1309 buf = alloca (len);
1215 1310
1216 memset (buf, 0, len); 1311 memset (buf, 0, len);
1217 1312
1218 switch (serv->version) 1313 switch (serv->version)
1219 { 1314 {
1220 case 4: 1315 case 4:
1221 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */ 1316 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1222 &serv->v4.ip4address, 1317 &serv->v4.ip4address,
1223 tunnel, state, (struct ip_pkt *) buf); 1318 tunnel, state, (struct ip_pkt *) buf);
1224 break; 1319 break;
1225 case 6: 1320 case 6:
1226 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */ 1321 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
1227 &serv->v6.ip6address, 1322 &serv->v6.ip6address,
1228 tunnel, state, (struct ip6_pkt *) buf); 1323 tunnel, state, (struct ip6_pkt *) buf);
1229 1324
1230 break; 1325 break;
1231 default: 1326 default:
1232 GNUNET_assert (0); 1327 GNUNET_assert (0);
1233 break; 1328 break;
1234 } 1329 }
1235 1330
1236 hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16); 1331 hash_redirect_info (&state->hash, &state->redirect_info,
1332 serv->version == 4 ? 4 : 16);
1237 1333
1238 if (GNUNET_NO == 1334 if (GNUNET_NO ==
1239 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash)) 1335 GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
1240 { 1336 {
1241 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state, 1337 GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
1242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1243 1339
1244 state->heap_node = 1340 state->heap_node =
1245 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state, 1341 GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
1246 GNUNET_TIME_absolute_get ().abs_value); 1342 GNUNET_TIME_absolute_get ().abs_value);
1247 1343
1248 if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections) 1344 if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
1249 GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap); 1345 max_udp_connections)
1250 } 1346 GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
1347 }
1251 else 1348 else
1252 GNUNET_free (state); 1349 GNUNET_free (state);
1253 1350
@@ -1256,13 +1353,13 @@ receive_udp_service (void *cls __attribute__((unused)),
1256} 1353}
1257 1354
1258static void 1355static void
1259connect_to_mesh() 1356connect_to_mesh ()
1260{ 1357{
1261 int udp, tcp; 1358 int udp, tcp;
1262 int handler_idx, app_idx; 1359 int handler_idx, app_idx;
1263 1360
1264 udp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_UDP"); 1361 udp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_UDP");
1265 tcp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_TCP"); 1362 tcp = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_TCP");
1266 1363
1267 static struct GNUNET_MESH_MessageHandler handlers[] = { 1364 static struct GNUNET_MESH_MessageHandler handlers[] = {
1268 {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0}, 1365 {receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
@@ -1272,35 +1369,34 @@ connect_to_mesh()
1272 {NULL, 0, 0} 1369 {NULL, 0, 0}
1273 }; 1370 };
1274 1371
1275 static GNUNET_MESH_ApplicationType apptypes[] = 1372 static GNUNET_MESH_ApplicationType apptypes[] = {
1276 { 1373 GNUNET_APPLICATION_TYPE_END,
1277 GNUNET_APPLICATION_TYPE_END, 1374 GNUNET_APPLICATION_TYPE_END,
1278 GNUNET_APPLICATION_TYPE_END, 1375 GNUNET_APPLICATION_TYPE_END
1279 GNUNET_APPLICATION_TYPE_END 1376 };
1280 };
1281 1377
1282 app_idx = 0; 1378 app_idx = 0;
1283 handler_idx = 2; 1379 handler_idx = 2;
1284 1380
1285 if (GNUNET_YES == udp) 1381 if (GNUNET_YES == udp)
1286 { 1382 {
1287 handlers[handler_idx].callback = receive_udp_remote; 1383 handlers[handler_idx].callback = receive_udp_remote;
1288 handlers[handler_idx].expected_size = 0; 1384 handlers[handler_idx].expected_size = 0;
1289 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP; 1385 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP;
1290 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY; 1386 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
1291 handler_idx++; 1387 handler_idx++;
1292 app_idx++; 1388 app_idx++;
1293 } 1389 }
1294 1390
1295 if (GNUNET_YES == tcp) 1391 if (GNUNET_YES == tcp)
1296 { 1392 {
1297 handlers[handler_idx].callback = receive_tcp_remote; 1393 handlers[handler_idx].callback = receive_tcp_remote;
1298 handlers[handler_idx].expected_size = 0; 1394 handlers[handler_idx].expected_size = 0;
1299 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP; 1395 handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP;
1300 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY; 1396 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
1301 handler_idx++; 1397 handler_idx++;
1302 app_idx++; 1398 app_idx++;
1303 } 1399 }
1304 1400
1305 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes); 1401 mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
1306} 1402}
@@ -1315,19 +1411,20 @@ connect_to_mesh()
1315 */ 1411 */
1316static void 1412static void
1317run (void *cls, 1413run (void *cls,
1318 char *const *args __attribute__((unused)), 1414 char *const *args __attribute__ ((unused)),
1319 const char *cfgfile __attribute__((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_) 1415 const char *cfgfile
1416 __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
1320{ 1417{
1321 cfg = cfg_; 1418 cfg = cfg_;
1322 1419
1323 connect_to_mesh(); 1420 connect_to_mesh ();
1324 1421
1325 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536); 1422 udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1326 udp_connections_heap = 1423 udp_connections_heap =
1327 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 1424 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1328 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536); 1425 tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
1329 tcp_connections_heap = 1426 tcp_connections_heap =
1330 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 1427 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1331 udp_services = GNUNET_CONTAINER_multihashmap_create (65536); 1428 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1332 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536); 1429 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
1333 1430
@@ -1350,18 +1447,18 @@ run (void *cls,
1350 * @return 0 ok, 1 on error 1447 * @return 0 ok, 1 on error
1351 */ 1448 */
1352int 1449int
1353main (int argc, char *const *argv) { 1450main (int argc, char *const *argv)
1354 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 1451{
1355 GNUNET_GETOPT_OPTION_END 1452 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1356 }; 1453 GNUNET_GETOPT_OPTION_END
1357 1454 };
1358 return (GNUNET_OK == 1455
1359 GNUNET_PROGRAM_run (argc, 1456 return (GNUNET_OK ==
1360 argv, 1457 GNUNET_PROGRAM_run (argc,
1361 "exit", 1458 argv,
1362 gettext_noop ("help text"), 1459 "exit",
1363 options, &run, NULL)) ? ret : 1; 1460 gettext_noop ("help text"),
1461 options, &run, NULL)) ? ret : 1;
1364} 1462}
1365 1463
1366/* end of gnunet-daemon-exit.c */ 1464/* end of gnunet-daemon-exit.c */
1367
diff --git a/src/vpn/gnunet-daemon-vpn-dns.c b/src/vpn/gnunet-daemon-vpn-dns.c
index 6ccef5869..d6f842e34 100644
--- a/src/vpn/gnunet-daemon-vpn-dns.c
+++ b/src/vpn/gnunet-daemon-vpn-dns.c
@@ -52,56 +52,67 @@ struct answer_packet_list *answer_proc_tail;
52 * {{{ 52 * {{{
53 */ 53 */
54size_t 54size_t
55send_query(void* cls __attribute__((unused)), size_t size, void* buf) { 55send_query (void *cls __attribute__ ((unused)), size_t size, void *buf)
56 size_t len; 56{
57 size_t len;
58
59 /*
60 * Send the rehijack-message
61 */
62 if (restart_hijack == 1)
63 {
64 restart_hijack = 0;
57 /* 65 /*
58 * Send the rehijack-message 66 * The message is just a header
59 */ 67 */
60 if (restart_hijack == 1) 68 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= size);
61 { 69 struct GNUNET_MessageHeader *hdr = buf;
62 restart_hijack = 0; 70
63 /* 71 len = sizeof (struct GNUNET_MessageHeader);
64 * The message is just a header 72 hdr->size = htons (len);
65 */ 73 hdr->type = htons (GNUNET_MESSAGE_TYPE_REHIJACK);
66 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) <= size); 74 }
67 struct GNUNET_MessageHeader* hdr = buf; 75 else if (head != NULL)
68 len = sizeof(struct GNUNET_MessageHeader); 76 {
69 hdr->size = htons(len); 77 struct query_packet_list *query = head;
70 hdr->type = htons(GNUNET_MESSAGE_TYPE_REHIJACK); 78
71 } 79 len = ntohs (query->pkt.hdr.size);
72 else if (head != NULL) 80
73 { 81 GNUNET_assert (len <= size);
74 struct query_packet_list* query = head; 82
75 len = ntohs(query->pkt.hdr.size); 83 memcpy (buf, &query->pkt.hdr, len);
76 84
77 GNUNET_assert(len <= size); 85 GNUNET_CONTAINER_DLL_remove (head, tail, query);
78 86
79 memcpy(buf, &query->pkt.hdr, len); 87 GNUNET_free (query);
80 88 }
81 GNUNET_CONTAINER_DLL_remove (head, tail, query); 89 else
82 90 {
83 GNUNET_free(query); 91 GNUNET_break (0);
84 } 92 len = 0;
85 else 93 }
86 { 94
87 GNUNET_break(0); 95 /*
88 len = 0; 96 * Check whether more data is to be sent
89 } 97 */
90 98 if (head != NULL)
91 /* 99 {
92 * Check whether more data is to be sent 100 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
93 */ 101 ntohs (head->pkt.hdr.size),
94 if (head != NULL) 102 GNUNET_TIME_UNIT_FOREVER_REL,
95 { 103 GNUNET_YES, &send_query, NULL);
96 GNUNET_CLIENT_notify_transmit_ready(dns_connection, ntohs(head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); 104 }
97 } 105 else if (restart_hijack == 1)
98 else if (restart_hijack == 1) 106 {
99 { 107 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
100 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); 108 sizeof (struct GNUNET_MessageHeader),
101 } 109 GNUNET_TIME_UNIT_FOREVER_REL,
102 110 GNUNET_YES, &send_query, NULL);
103 return len; 111 }
112
113 return len;
104} 114}
115
105/* }}} */ 116/* }}} */
106 117
107 118
@@ -109,33 +120,37 @@ send_query(void* cls __attribute__((unused)), size_t size, void* buf) {
109 * Connect to the service-dns 120 * Connect to the service-dns
110 */ 121 */
111void 122void
112connect_to_service_dns (void *cls __attribute__((unused)), 123connect_to_service_dns (void *cls __attribute__ ((unused)),
113 const struct GNUNET_SCHEDULER_TaskContext *tc) { 124 const struct GNUNET_SCHEDULER_TaskContext *tc)
114 conn_task = GNUNET_SCHEDULER_NO_TASK; 125{
115 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 126 conn_task = GNUNET_SCHEDULER_NO_TASK;
116 return; 127 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n"); 128 return;
118 GNUNET_assert (dns_connection == NULL); 129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
119 dns_connection = GNUNET_CLIENT_connect ("dns", cfg); 130 GNUNET_assert (dns_connection == NULL);
120 /* This would most likely be a misconfiguration */ 131 dns_connection = GNUNET_CLIENT_connect ("dns", cfg);
121 GNUNET_assert(NULL != dns_connection); 132 /* This would most likely be a misconfiguration */
122 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL); 133 GNUNET_assert (NULL != dns_connection);
123 134 GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
124 /* We might not yet be connected. Yay, mps. */ 135 GNUNET_TIME_UNIT_FOREVER_REL);
125 if (NULL == dns_connection) return; 136
126 137 /* We might not yet be connected. Yay, mps. */
127 /* If a packet is already in the list, schedule to send it */ 138 if (NULL == dns_connection)
128 if (head != NULL) 139 return;
129 GNUNET_CLIENT_notify_transmit_ready(dns_connection, 140
130 ntohs(head->pkt.hdr.size), 141 /* If a packet is already in the list, schedule to send it */
131 GNUNET_TIME_UNIT_FOREVER_REL, 142 if (head != NULL)
132 GNUNET_YES, 143 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
133 &send_query, 144 ntohs (head->pkt.hdr.size),
134 NULL); 145 GNUNET_TIME_UNIT_FOREVER_REL,
135 else if (restart_hijack == 1) 146 GNUNET_YES, &send_query, NULL);
136 { 147 else if (restart_hijack == 1)
137 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); 148 {
138 } 149 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
150 sizeof (struct GNUNET_MessageHeader),
151 GNUNET_TIME_UNIT_FOREVER_REL,
152 GNUNET_YES, &send_query, NULL);
153 }
139} 154}
140 155
141/** 156/**
@@ -143,33 +158,34 @@ connect_to_service_dns (void *cls __attribute__((unused)),
143 * handle it 158 * handle it
144 */ 159 */
145void 160void
146dns_answer_handler(void* cls __attribute__((unused)), const struct GNUNET_MessageHeader *msg) { 161dns_answer_handler (void *cls
147 /* the service disconnected, reconnect after short wait */ 162 __attribute__ ((unused)),
148 if (msg == NULL) 163 const struct GNUNET_MessageHeader *msg)
149 { 164{
150 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO); 165 /* the service disconnected, reconnect after short wait */
151 dns_connection = NULL; 166 if (msg == NULL)
152 conn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 167 {
153 &connect_to_service_dns, 168 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
154 NULL); 169 dns_connection = NULL;
155 return; 170 conn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
156 } 171 &connect_to_service_dns, NULL);
157 172 return;
158 /* the service did something strange, reconnect immediately */ 173 }
159 if (msg->type != htons(GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS)) 174
160 { 175 /* the service did something strange, reconnect immediately */
161 GNUNET_break (0); 176 if (msg->type != htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS))
162 GNUNET_CLIENT_disconnect(dns_connection, GNUNET_NO); 177 {
163 dns_connection = NULL; 178 GNUNET_break (0);
164 conn_task = GNUNET_SCHEDULER_add_now (&connect_to_service_dns, 179 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
165 NULL); 180 dns_connection = NULL;
166 return; 181 conn_task = GNUNET_SCHEDULER_add_now (&connect_to_service_dns, NULL);
167 } 182 return;
168 void *pkt = GNUNET_malloc(ntohs(msg->size)); 183 }
169 184 void *pkt = GNUNET_malloc (ntohs (msg->size));
170 memcpy(pkt, msg, ntohs(msg->size)); 185
171 186 memcpy (pkt, msg, ntohs (msg->size));
172 GNUNET_SCHEDULER_add_now(process_answer, pkt); 187
173 GNUNET_CLIENT_receive(dns_connection, &dns_answer_handler, NULL, GNUNET_TIME_UNIT_FOREVER_REL); 188 GNUNET_SCHEDULER_add_now (process_answer, pkt);
189 GNUNET_CLIENT_receive (dns_connection, &dns_answer_handler, NULL,
190 GNUNET_TIME_UNIT_FOREVER_REL);
174} 191}
175
diff --git a/src/vpn/gnunet-daemon-vpn-dns.h b/src/vpn/gnunet-daemon-vpn-dns.h
index ed980e65f..2882969e5 100644
--- a/src/vpn/gnunet-daemon-vpn-dns.h
+++ b/src/vpn/gnunet-daemon-vpn-dns.h
@@ -40,19 +40,19 @@ extern struct query_packet_list *tail;
40 * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages 40 * Callback called by notify_transmit_ready; sends dns-queries or rehijack-messages
41 * to the service-dns 41 * to the service-dns
42 */ 42 */
43size_t send_query(void* cls, size_t size, void* buf); 43size_t send_query (void *cls, size_t size, void *buf);
44 44
45/** 45/**
46 * Connect to the service-dns 46 * Connect to the service-dns
47 */ 47 */
48void connect_to_service_dns (void *cls, 48void connect_to_service_dns (void *cls,
49 const struct GNUNET_SCHEDULER_TaskContext *tc); 49 const struct GNUNET_SCHEDULER_TaskContext *tc);
50 50
51/** 51/**
52 * This receives packets from the service-dns and schedules process_answer to 52 * This receives packets from the service-dns and schedules process_answer to
53 * handle it 53 * handle it
54 */ 54 */
55void dns_answer_handler(void* cls, const struct GNUNET_MessageHeader *msg); 55void dns_answer_handler (void *cls, const struct GNUNET_MessageHeader *msg);
56 56
57/** 57/**
58 * The connection to the service-dns 58 * The connection to the service-dns
diff --git a/src/vpn/gnunet-daemon-vpn-helper.c b/src/vpn/gnunet-daemon-vpn-helper.c
index 05e2bd081..65e92b8f9 100644
--- a/src/vpn/gnunet-daemon-vpn-helper.c
+++ b/src/vpn/gnunet-daemon-vpn-helper.c
@@ -48,8 +48,8 @@ struct GNUNET_VPN_HELPER_Handle *helper_handle;
48/** 48/**
49 * The tunnels that will be used to send tcp- and udp-packets 49 * The tunnels that will be used to send tcp- and udp-packets
50 */ 50 */
51static struct GNUNET_MESH_Tunnel* tcp_tunnel; 51static struct GNUNET_MESH_Tunnel *tcp_tunnel;
52static struct GNUNET_MESH_Tunnel* udp_tunnel; 52static struct GNUNET_MESH_Tunnel *udp_tunnel;
53 53
54/** 54/**
55 * Start the helper-process 55 * Start the helper-process
@@ -59,542 +59,558 @@ static struct GNUNET_MESH_Tunnel* udp_tunnel;
59 * {{{ 59 * {{{
60 */ 60 */
61void 61void
62start_helper_and_schedule(void *cls, 62start_helper_and_schedule (void *cls,
63 const struct GNUNET_SCHEDULER_TaskContext *tc) { 63 const struct GNUNET_SCHEDULER_TaskContext *tc)
64{
64 shs_task = GNUNET_SCHEDULER_NO_TASK; 65 shs_task = GNUNET_SCHEDULER_NO_TASK;
65 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 66 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
66 return; 67 return;
67 68
68 if (cls != NULL) 69 if (cls != NULL)
69 cleanup_helper(cls); 70 cleanup_helper (cls);
70 cls = NULL; 71 cls = NULL;
71 72
72 char* ifname; 73 char *ifname;
73 char* ipv6addr; 74 char *ipv6addr;
74 char* ipv6prefix; 75 char *ipv6prefix;
75 char* ipv4addr; 76 char *ipv4addr;
76 char* ipv4mask; 77 char *ipv4mask;
77 78
78 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname)) 79 if (GNUNET_SYSERR ==
79 { 80 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
80 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n"); 81 {
81 exit(1); 82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82 } 83 "No entry 'IFNAME' in configuration!\n");
83 84 exit (1);
84 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr)) 85 }
85 { 86
86 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n"); 87 if (GNUNET_SYSERR ==
87 exit(1); 88 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr))
88 } 89 {
89 90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
90 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6PREFIX", &ipv6prefix)) 91 "No entry 'IPV6ADDR' in configuration!\n");
91 { 92 exit (1);
92 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n"); 93 }
93 exit(1); 94
94 } 95 if (GNUNET_SYSERR ==
95 96 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
96 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4ADDR", &ipv4addr)) 97 &ipv6prefix))
97 { 98 {
98 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n"); 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 exit(1); 100 "No entry 'IPV6PREFIX' in configuration!\n");
100 } 101 exit (1);
101 102 }
102 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4MASK", &ipv4mask)) 103
103 { 104 if (GNUNET_SYSERR ==
104 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n"); 105 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr))
105 exit(1); 106 {
106 } 107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 108 "No entry 'IPV4ADDR' in configuration!\n");
108 /* Start the helper 109 exit (1);
109 * Messages get passed to the function message_token 110 }
110 * When the helper dies, this function will be called again with the 111
111 * helper_handle as cls. 112 if (GNUNET_SYSERR ==
112 */ 113 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask))
113 helper_handle = start_helper(ifname, 114 {
114 ipv6addr, 115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 ipv6prefix, 116 "No entry 'IPV4MASK' in configuration!\n");
116 ipv4addr, 117 exit (1);
117 ipv4mask, 118 }
118 "vpn-gnunet", 119
119 start_helper_and_schedule, 120 /* Start the helper
120 message_token, 121 * Messages get passed to the function message_token
121 NULL); 122 * When the helper dies, this function will be called again with the
122 123 * helper_handle as cls.
123 GNUNET_free(ipv6addr); 124 */
124 GNUNET_free(ipv6prefix); 125 helper_handle = start_helper (ifname,
125 GNUNET_free(ipv4addr); 126 ipv6addr,
126 GNUNET_free(ipv4mask); 127 ipv6prefix,
127 GNUNET_free(ifname); 128 ipv4addr,
128 129 ipv4mask,
129 /* Tell the dns-service to rehijack the dns-port 130 "vpn-gnunet",
130 * The routing-table gets flushed if an interface disappears. 131 start_helper_and_schedule, message_token, NULL);
131 */ 132
132 restart_hijack = 1; 133 GNUNET_free (ipv6addr);
133 if (NULL != dns_connection) 134 GNUNET_free (ipv6prefix);
134 GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); 135 GNUNET_free (ipv4addr);
135 136 GNUNET_free (ipv4mask);
136 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, helper_handle->fh_to_helper, &helper_write, NULL); 137 GNUNET_free (ifname);
138
139 /* Tell the dns-service to rehijack the dns-port
140 * The routing-table gets flushed if an interface disappears.
141 */
142 restart_hijack = 1;
143 if (NULL != dns_connection)
144 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
145 sizeof (struct GNUNET_MessageHeader),
146 GNUNET_TIME_UNIT_FOREVER_REL,
147 GNUNET_YES, &send_query, NULL);
148
149 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
150 helper_handle->fh_to_helper, &helper_write,
151 NULL);
137} 152}
153
138/*}}}*/ 154/*}}}*/
139 155
140/** 156/**
141 * Send an dns-answer-packet to the helper 157 * Send an dns-answer-packet to the helper
142 */ 158 */
143void 159void
144helper_write(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) { 160helper_write (void *cls
145 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) 161 __attribute__ ((unused)),
146 return; 162 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
147 163{
148 struct answer_packet_list* ans = answer_proc_head; 164 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
149 165 return;
150 if (NULL == ans) return; 166
167 struct answer_packet_list *ans = answer_proc_head;
168
169 if (NULL == ans)
170 return;
171
172 size_t len = ntohs (ans->pkt.hdr.size);
173
174 GNUNET_assert (ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
175
176 GNUNET_assert (20 == sizeof (struct ip_hdr));
177 GNUNET_assert (8 == sizeof (struct udp_pkt));
178 size_t data_len = len - sizeof (struct answer_packet) + 1;
179 size_t net_len = sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len;
180 size_t pkt_len =
181 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len;
182
183 struct ip_udp_dns *pkt = alloca (pkt_len);
184
185 GNUNET_assert (pkt != NULL);
186 memset (pkt, 0, pkt_len);
187
188 /* set the gnunet-header */
189 pkt->shdr.size = htons (pkt_len);
190 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
191
192 /* set the tun-header (no flags and ethertype of IPv4) */
193 pkt->tun.flags = 0;
194 pkt->tun.type = htons (0x0800);
195
196 /* set the ip-header */
197 pkt->ip_hdr.version = 4;
198 pkt->ip_hdr.hdr_lngth = 5;
199 pkt->ip_hdr.diff_serv = 0;
200 pkt->ip_hdr.tot_lngth = htons (net_len);
201 pkt->ip_hdr.ident = 0;
202 pkt->ip_hdr.flags = 0;
203 pkt->ip_hdr.frag_off = 0;
204 pkt->ip_hdr.ttl = 255;
205 pkt->ip_hdr.proto = 0x11; /* UDP */
206 pkt->ip_hdr.chks = 0; /* Will be calculated later */
207 pkt->ip_hdr.sadr = ans->pkt.from;
208 pkt->ip_hdr.dadr = ans->pkt.to;
209
210 pkt->ip_hdr.chks = calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4);
211
212 /* set the udp-header */
213 pkt->udp_dns.udp_hdr.spt = htons (53);
214 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
215 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr));
216 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
217
218 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
219
220 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
221 GNUNET_free (ans);
222
223 if (GNUNET_DISK_file_write (helper_handle->fh_to_helper, pkt, pkt_len) < 0)
224 {
225 cleanup_helper (helper_handle);
226 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
227 return;
228 }
229
230 /* if more packets are available, reschedule */
231 if (answer_proc_head != NULL)
232 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
233 helper_handle->fh_to_helper,
234 &helper_write, NULL);
235}
151 236
152 size_t len = ntohs(ans->pkt.hdr.size); 237/**
238 * Receive packets from the helper-process
239 */
240void
241message_token (void *cls __attribute__ ((unused)),
242 void *client
243 __attribute__ ((unused)),
244 const struct GNUNET_MessageHeader *message)
245{
246 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
153 247
154 GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP); 248 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
249 GNUNET_HashCode *key;
155 250
156 GNUNET_assert (20 == sizeof (struct ip_hdr)); 251 /* ethertype is ipv6 */
157 GNUNET_assert (8 == sizeof (struct udp_pkt)); 252 if (ntohs (pkt_tun->tun.type) == 0x86dd)
158 size_t data_len = len - sizeof(struct answer_packet) + 1; 253 {
159 size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len; 254 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
160 size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
161 255
162 struct ip_udp_dns* pkt = alloca(pkt_len); 256 GNUNET_assert (pkt6->ip6_hdr.version == 6);
163 GNUNET_assert(pkt != NULL); 257 struct ip6_tcp *pkt6_tcp;
164 memset(pkt, 0, pkt_len); 258 struct ip6_udp *pkt6_udp;
259 struct ip6_icmp *pkt6_icmp;
165 260
166 /* set the gnunet-header */ 261 switch (pkt6->ip6_hdr.nxthdr)
167 pkt->shdr.size = htons(pkt_len); 262 {
168 pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER); 263 case 0x06: /* TCP */
264 case 0x11: /* UDP */
265 pkt6_tcp = (struct ip6_tcp *) pkt6;
266 pkt6_udp = (struct ip6_udp *) pkt6;
169 267
170 /* set the tun-header (no flags and ethertype of IPv4) */ 268 if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
171 pkt->tun.flags = 0; 269 {
172 pkt->tun.type = htons(0x0800); 270 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
173 271
174 /* set the ip-header */ 272 GNUNET_assert (me != NULL);
175 pkt->ip_hdr.version = 4; 273 GNUNET_free (key);
176 pkt->ip_hdr.hdr_lngth = 5;
177 pkt->ip_hdr.diff_serv = 0;
178 pkt->ip_hdr.tot_lngth = htons(net_len);
179 pkt->ip_hdr.ident = 0;
180 pkt->ip_hdr.flags = 0;
181 pkt->ip_hdr.frag_off = 0;
182 pkt->ip_hdr.ttl = 255;
183 pkt->ip_hdr.proto = 0x11; /* UDP */
184 pkt->ip_hdr.chks = 0; /* Will be calculated later*/
185 pkt->ip_hdr.sadr = ans->pkt.from;
186 pkt->ip_hdr.dadr = ans->pkt.to;
187 274
188 pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4); 275 size_t size =
276 sizeof (struct GNUNET_MESH_Tunnel *) +
277 sizeof (struct GNUNET_MessageHeader) +
278 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth);
189 279
190 /* set the udp-header */ 280 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
191 pkt->udp_dns.udp_hdr.spt = htons(53); 281 struct GNUNET_MessageHeader *hdr =
192 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port; 282 (struct GNUNET_MessageHeader *) (cls + 1);
193 pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr)); 283 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
194 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
195 284
196 memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len); 285 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
286 sizeof (GNUNET_HashCode) +
287 ntohs (pkt6->ip6_hdr.paylgth));
197 288
198 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans); 289 GNUNET_MESH_ApplicationType app_type;
199 GNUNET_free(ans);
200 290
201 if (GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len) < 0) 291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
292 me->addrlen);
293 if (me->addrlen == 0)
294 {
295 /* This is a mapping to a gnunet-service */
296 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
297
298 if (0x11 == pkt6->ip6_hdr.nxthdr
299 && (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
300 && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
301 || testBit (me->additional_ports,
302 ntohs (pkt6_udp->udp_hdr.dpt))))
303 {
304 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
305
306 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
307
308 }
309 else if (0x06 == pkt6->ip6_hdr.nxthdr
310 && (me->desc.
311 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP)) &&
312 (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
313 {
314 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
315
316 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
317
318 }
319 if (me->tunnel == NULL && NULL != cls)
320 {
321 *cls =
322 GNUNET_MESH_peer_request_connect_all (mesh_handle,
323 GNUNET_TIME_UNIT_FOREVER_REL,
324 1,
325 (struct
326 GNUNET_PeerIdentity
327 *) &me->desc.peer,
328 send_pkt_to_peer,
329 NULL, cls);
330 me->tunnel = *cls;
331 }
332 else if (NULL != cls)
333 {
334 *cls = me->tunnel;
335 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Queued to send to peer %x, type %d\n",
338 *((unsigned int *) &me->desc.peer), ntohs (hdr->type));
339 }
340 }
341 else
342 {
343 /* This is a mapping to a "real" address */
344 struct remote_addr *s = (struct remote_addr *) hc;
345
346 s->addrlen = me->addrlen;
347 memcpy (s->addr, me->addr, me->addrlen);
348 s->proto = pkt6->ip6_hdr.nxthdr;
349 if (s->proto == 0x11)
350 {
351 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
352 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
353 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
354 if (NULL != udp_tunnel)
355 me->tunnel = udp_tunnel;
356 }
357 else if (s->proto == 0x06)
358 {
359 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
360 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
361 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
362 if (NULL != tcp_tunnel)
363 me->tunnel = tcp_tunnel;
364 }
365 if (me->tunnel == NULL && NULL != cls)
366 {
367 *cls = GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
368 GNUNET_TIME_UNIT_FOREVER_REL,
369 app_type,
370 send_pkt_to_peer,
371 NULL, cls);
372 me->tunnel = *cls;
373 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
374 udp_tunnel = *cls;
375 else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
376 tcp_tunnel = *cls;
377 }
378 else if (NULL != cls)
379 {
380 *cls = me->tunnel;
381 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
382 }
383 }
384 }
385 else
202 { 386 {
203 cleanup_helper(helper_handle); 387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 GNUNET_SCHEDULER_add_now(start_helper_and_schedule, NULL); 388 "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n",
205 return; 389 pkt6->ip6_hdr.dadr[0], pkt6->ip6_hdr.dadr[1],
390 pkt6->ip6_hdr.dadr[2], pkt6->ip6_hdr.dadr[3],
391 pkt6->ip6_hdr.dadr[4], pkt6->ip6_hdr.dadr[5],
392 pkt6->ip6_hdr.dadr[6], pkt6->ip6_hdr.dadr[7],
393 pkt6->ip6_hdr.dadr[8], pkt6->ip6_hdr.dadr[9],
394 pkt6->ip6_hdr.dadr[10], pkt6->ip6_hdr.dadr[11],
395 pkt6->ip6_hdr.dadr[12], pkt6->ip6_hdr.dadr[13],
396 pkt6->ip6_hdr.dadr[14], pkt6->ip6_hdr.dadr[15]);
206 } 397 }
398 break;
399 case 0x3a:
400 /* ICMPv6 */
401 pkt6_icmp = (struct ip6_icmp *) pkt6;
402 /* If this packet is an icmp-echo-request and a mapping exists, answer */
403 if (pkt6_icmp->icmp_hdr.type == 0x80
404 && (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
405 {
406 GNUNET_free (key);
407 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
408 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
409 GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
410 }
411 break;
412 }
413 }
414 /* ethertype is ipv4 */
415 else if (ntohs (pkt_tun->tun.type) == 0x0800)
416 {
417 struct ip_pkt *pkt = (struct ip_pkt *) message;
418 struct ip_udp *udp = (struct ip_udp *) message;
419 struct ip_tcp *pkt_tcp;
420 struct ip_udp *pkt_udp;
421 struct ip_icmp *pkt_icmp;
207 422
208 /* if more packets are available, reschedule */ 423 GNUNET_assert (pkt->ip_hdr.version == 4);
209 if (answer_proc_head != NULL)
210 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
211 helper_handle->fh_to_helper,
212 &helper_write,
213 NULL);
214}
215
216/**
217 * Receive packets from the helper-process
218 */
219void
220message_token (void *cls __attribute__((unused)),
221 void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
222{
223 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
224
225 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
226 GNUNET_HashCode *key;
227 424
228 /* ethertype is ipv6 */ 425 /* Send dns-packets to the service-dns */
229 if (ntohs (pkt_tun->tun.type) == 0x86dd) 426 if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
230 { 427 {
231 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message; 428 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
232 GNUNET_assert (pkt6->ip6_hdr.version == 6); 429 size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
233 struct ip6_tcp *pkt6_tcp; 430
234 struct ip6_udp *pkt6_udp; 431 struct query_packet_list *query =
235 struct ip6_icmp *pkt6_icmp; 432 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
433 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
434 query->pkt.hdr.size = htons (len);
435 query->pkt.orig_to = pkt->ip_hdr.dadr;
436 query->pkt.orig_from = pkt->ip_hdr.sadr;
437 query->pkt.src_port = udp->udp_hdr.spt;
438 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
439
440 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
441
442 GNUNET_assert (head != NULL);
443
444 if (dns_connection != NULL)
445 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
446 len,
447 GNUNET_TIME_UNIT_FOREVER_REL,
448 GNUNET_YES, &send_query, NULL);
449 }
450 else
451 {
452 uint32_t dadr = pkt->ip_hdr.dadr;
453 unsigned char *c = (unsigned char *) &dadr;
454
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
456 c[0], c[1], c[2], c[3], pkt->ip_hdr.proto);
457 switch (pkt->ip_hdr.proto)
458 {
459 case 0x06: /* TCP */
460 case 0x11: /* UDP */
461 pkt_tcp = (struct ip_tcp *) pkt;
462 pkt_udp = (struct ip_udp *) pkt;
236 463
237 switch (pkt6->ip6_hdr.nxthdr) 464 if ((key = address4_mapping_exists (dadr)) != NULL)
238 { 465 {
239 case 0x06: /* TCP */ 466 struct map_entry *me =
240 case 0x11: /* UDP */ 467 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
241 pkt6_tcp = (struct ip6_tcp *) pkt6; 468 GNUNET_assert (me != NULL);
242 pkt6_udp = (struct ip6_udp *) pkt6; 469 GNUNET_free (key);
470
471 size_t size =
472 sizeof (struct GNUNET_MESH_Tunnel *) +
473 sizeof (struct GNUNET_MessageHeader) +
474 sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
475 4 * pkt->ip_hdr.hdr_lngth;
476
477 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
478 struct GNUNET_MessageHeader *hdr =
479 (struct GNUNET_MessageHeader *) (cls + 1);
480 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
481
482 hdr->size =
483 htons (sizeof (struct GNUNET_MessageHeader) +
484 sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
485 4 * pkt->ip_hdr.hdr_lngth);
486
487 GNUNET_MESH_ApplicationType app_type;
488
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
490 me->addrlen);
491 if (me->addrlen == 0)
492 {
493 /* This is a mapping to a gnunet-service */
494 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
495
496 if (0x11 == pkt->ip_hdr.proto
497 && (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
498 && (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt)
499 || testBit (me->additional_ports,
500 ntohs (pkt_udp->udp_hdr.dpt))))
501 {
502 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
503
504 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
243 505
244 if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL) 506 }
507 else if (0x06 == pkt->ip_hdr.proto
508 && (me->desc.service_type &
509 htonl (GNUNET_DNS_SERVICE_TYPE_TCP)) &&
510 (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
511 {
512 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
513
514 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
515 ntohs (pkt->ip_hdr.tot_lngth) -
516 4 * pkt->ip_hdr.hdr_lngth);
517
518 }
519 if (me->tunnel == NULL && NULL != cls)
245 { 520 {
246 struct map_entry *me = 521 *cls =
247 GNUNET_CONTAINER_multihashmap_get (hashmap, key); 522 GNUNET_MESH_peer_request_connect_all (mesh_handle,
248 GNUNET_assert (me != NULL); 523 GNUNET_TIME_UNIT_FOREVER_REL,
249 GNUNET_free (key); 524 1,
250 525 (struct
251 size_t size = 526 GNUNET_PeerIdentity
252 sizeof (struct GNUNET_MESH_Tunnel *) + 527 *) &me->desc.peer,
253 sizeof (struct GNUNET_MessageHeader) + 528 send_pkt_to_peer,
254 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth); 529 NULL, cls);
255 530 me->tunnel = *cls;
256 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
257 struct GNUNET_MessageHeader *hdr =
258 (struct GNUNET_MessageHeader *) (cls + 1);
259 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
260
261 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
262 sizeof (GNUNET_HashCode) +
263 ntohs (pkt6->ip6_hdr.paylgth));
264
265 GNUNET_MESH_ApplicationType app_type;
266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", me->addrlen);
267 if (me->addrlen == 0)
268 {
269 /* This is a mapping to a gnunet-service */
270 memcpy (hc, &me->desc.service_descriptor,
271 sizeof (GNUNET_HashCode));
272
273 if (0x11 == pkt6->ip6_hdr.nxthdr
274 && (me->desc.
275 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
276 && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
277 || testBit (me->additional_ports,
278 ntohs (pkt6_udp->udp_hdr.dpt))))
279 {
280 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
281
282 memcpy (hc + 1, &pkt6_udp->udp_hdr,
283 ntohs (pkt6_udp->udp_hdr.len));
284
285 }
286 else if (0x06 == pkt6->ip6_hdr.nxthdr
287 && (me->desc.
288 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
289 &&
290 (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
291 {
292 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
293
294 memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
295 ntohs (pkt6->ip6_hdr.paylgth));
296
297 }
298 if (me->tunnel == NULL && NULL != cls)
299 {
300 *cls =
301 GNUNET_MESH_peer_request_connect_all (mesh_handle,
302 GNUNET_TIME_UNIT_FOREVER_REL,
303 1,
304 (struct
305 GNUNET_PeerIdentity
306 *) &me->desc.peer,
307 send_pkt_to_peer,
308 NULL, cls);
309 me->tunnel = *cls;
310 }
311 else if (NULL != cls)
312 {
313 *cls = me->tunnel;
314 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
315 NULL);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Queued to send to peer %x, type %d\n",
318 *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
319 }
320 }
321 else
322 {
323 /* This is a mapping to a "real" address */
324 struct remote_addr *s = (struct remote_addr*) hc;
325 s->addrlen = me->addrlen;
326 memcpy(s->addr, me->addr, me->addrlen);
327 s->proto= pkt6->ip6_hdr.nxthdr;
328 if (s->proto == 0x11)
329 {
330 hdr->type = htons(GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
331 memcpy (hc + 1, &pkt6_udp->udp_hdr,
332 ntohs (pkt6_udp->udp_hdr.len));
333 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
334 if (NULL != udp_tunnel)
335 me->tunnel = udp_tunnel;
336 }
337 else if (s->proto == 0x06)
338 {
339 hdr->type = htons(GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
340 memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
341 ntohs (pkt6->ip6_hdr.paylgth));
342 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
343 if (NULL != tcp_tunnel)
344 me->tunnel = tcp_tunnel;
345 }
346 if (me->tunnel == NULL && NULL != cls)
347 {
348 *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
349 GNUNET_TIME_UNIT_FOREVER_REL,
350 app_type,
351 send_pkt_to_peer,
352 NULL,
353 cls);
354 me->tunnel = *cls;
355 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
356 udp_tunnel = *cls;
357 else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
358 tcp_tunnel = *cls;
359 }
360 else if (NULL != cls)
361 {
362 *cls = me->tunnel;
363 send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
364 }
365 }
366 } 531 }
532 else if (NULL != cls)
533 {
534 *cls = me->tunnel;
535 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Queued to send to peer %x, type %d\n",
538 *((unsigned int *) &me->desc.peer),
539 ntohs (hdr->type));
540 }
541 }
367 else 542 else
543 {
544 /* This is a mapping to a "real" address */
545 struct remote_addr *s = (struct remote_addr *) hc;
546
547 s->addrlen = me->addrlen;
548 memcpy (s->addr, me->addr, me->addrlen);
549 s->proto = pkt->ip_hdr.proto;
550 if (s->proto == 0x11)
551 {
552 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
553 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
554 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
555 }
556 else if (s->proto == 0x06)
557 {
558 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
559 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
560 ntohs (pkt->ip_hdr.tot_lngth) -
561 4 * pkt->ip_hdr.hdr_lngth);
562 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
563 }
564 if (me->tunnel == NULL && NULL != cls)
368 { 565 {
369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n", pkt6->ip6_hdr.dadr[0], 566 *cls = GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
370 pkt6->ip6_hdr.dadr[1], 567 GNUNET_TIME_UNIT_FOREVER_REL,
371 pkt6->ip6_hdr.dadr[2], 568 app_type,
372 pkt6->ip6_hdr.dadr[3], 569 send_pkt_to_peer,
373 pkt6->ip6_hdr.dadr[4], 570 NULL, cls);
374 pkt6->ip6_hdr.dadr[5], 571 me->tunnel = *cls;
375 pkt6->ip6_hdr.dadr[6],
376 pkt6->ip6_hdr.dadr[7],
377 pkt6->ip6_hdr.dadr[8],
378 pkt6->ip6_hdr.dadr[9],
379 pkt6->ip6_hdr.dadr[10],
380 pkt6->ip6_hdr.dadr[11],
381 pkt6->ip6_hdr.dadr[12],
382 pkt6->ip6_hdr.dadr[13],
383 pkt6->ip6_hdr.dadr[14],
384 pkt6->ip6_hdr.dadr[15]);
385 } 572 }
386 break; 573 else if (NULL != cls)
387 case 0x3a:
388 /* ICMPv6 */
389 pkt6_icmp = (struct ip6_icmp *) pkt6;
390 /* If this packet is an icmp-echo-request and a mapping exists, answer */
391 if (pkt6_icmp->icmp_hdr.type == 0x80
392 && (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
393 { 574 {
394 GNUNET_free (key); 575 *cls = me->tunnel;
395 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size)); 576 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
396 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
397 GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
398 } 577 }
399 break; 578 }
400 } 579 }
401 } 580 else
402 /* ethertype is ipv4 */
403 else if (ntohs (pkt_tun->tun.type) == 0x0800)
404 {
405 struct ip_pkt *pkt = (struct ip_pkt *) message;
406 struct ip_udp *udp = (struct ip_udp *) message;
407 struct ip_tcp *pkt_tcp;
408 struct ip_udp *pkt_udp;
409 struct ip_icmp *pkt_icmp;
410 GNUNET_assert (pkt->ip_hdr.version == 4);
411
412 /* Send dns-packets to the service-dns */
413 if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
414 { 581 {
415 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */ 582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 size_t len = 583 "Packet to %x which has no mapping\n", dadr);
417 sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
418
419 struct query_packet_list *query =
420 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
421 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
422 query->pkt.hdr.size = htons (len);
423 query->pkt.orig_to = pkt->ip_hdr.dadr;
424 query->pkt.orig_from = pkt->ip_hdr.sadr;
425 query->pkt.src_port = udp->udp_hdr.spt;
426 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
427
428 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
429
430 GNUNET_assert (head != NULL);
431
432 if (dns_connection != NULL)
433 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
434 len,
435 GNUNET_TIME_UNIT_FOREVER_REL,
436 GNUNET_YES,
437 &send_query, NULL);
438 } 584 }
439 else 585 break;
586 case 0x01:
587 /* ICMP */
588 pkt_icmp = (struct ip_icmp *) pkt;
589 if (pkt_icmp->icmp_hdr.type == 0x8 &&
590 (key = address4_mapping_exists (dadr)) != NULL)
440 { 591 {
441 uint32_t dadr = pkt->ip_hdr.dadr; 592 GNUNET_free (key);
442 unsigned char *c = (unsigned char*)&dadr; 593 pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size));
443 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n", 594 memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size));
444 c[0], 595 GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
445 c[1],
446 c[2],
447 c[3],
448 pkt->ip_hdr.proto);
449 switch (pkt->ip_hdr.proto)
450 {
451 case 0x06: /* TCP */
452 case 0x11: /* UDP */
453 pkt_tcp = (struct ip_tcp*) pkt;
454 pkt_udp = (struct ip_udp*) pkt;
455
456 if ((key = address4_mapping_exists (dadr)) != NULL)
457 {
458 struct map_entry *me =
459 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
460 GNUNET_assert (me != NULL);
461 GNUNET_free (key);
462
463 size_t size =
464 sizeof (struct GNUNET_MESH_Tunnel *) +
465 sizeof (struct GNUNET_MessageHeader) +
466 sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth;
467
468 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
469 struct GNUNET_MessageHeader *hdr =
470 (struct GNUNET_MessageHeader *) (cls + 1);
471 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
472
473 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
474
475 GNUNET_MESH_ApplicationType app_type;
476 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", me->addrlen);
477 if (me->addrlen == 0)
478 {
479 /* This is a mapping to a gnunet-service */
480 memcpy (hc, &me->desc.service_descriptor,
481 sizeof (GNUNET_HashCode));
482
483 if (0x11 == pkt->ip_hdr.proto
484 && (me->desc.
485 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
486 && (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt)
487 || testBit (me->additional_ports,
488 ntohs (pkt_udp->udp_hdr.dpt))))
489 {
490 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
491
492 memcpy (hc + 1, &pkt_udp->udp_hdr,
493 ntohs (pkt_udp->udp_hdr.len));
494
495 }
496 else if (0x06 == pkt->ip_hdr.proto
497 && (me->desc.
498 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
499 &&
500 (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
501 {
502 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
503
504 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
505 ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
506
507 }
508 if (me->tunnel == NULL && NULL != cls)
509 {
510 *cls =
511 GNUNET_MESH_peer_request_connect_all (mesh_handle,
512 GNUNET_TIME_UNIT_FOREVER_REL,
513 1,
514 (struct
515 GNUNET_PeerIdentity
516 *) &me->desc.peer,
517 send_pkt_to_peer,
518 NULL, cls);
519 me->tunnel = *cls;
520 }
521 else if (NULL != cls)
522 {
523 *cls = me->tunnel;
524 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
525 NULL);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Queued to send to peer %x, type %d\n",
528 *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
529 }
530 }
531 else
532 {
533 /* This is a mapping to a "real" address */
534 struct remote_addr *s = (struct remote_addr*) hc;
535 s->addrlen = me->addrlen;
536 memcpy(s->addr, me->addr, me->addrlen);
537 s->proto= pkt->ip_hdr.proto;
538 if (s->proto == 0x11)
539 {
540 hdr->type = htons(GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
541 memcpy (hc + 1, &pkt_udp->udp_hdr,
542 ntohs (pkt_udp->udp_hdr.len));
543 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
544 }
545 else if (s->proto == 0x06)
546 {
547 hdr->type = htons(GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
548 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
549 ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
550 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
551 }
552 if (me->tunnel == NULL && NULL != cls)
553 {
554 *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
555 GNUNET_TIME_UNIT_FOREVER_REL,
556 app_type,
557 send_pkt_to_peer,
558 NULL,
559 cls);
560 me->tunnel = *cls;
561 }
562 else if (NULL != cls)
563 {
564 *cls = me->tunnel;
565 send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
566 }
567 }
568 }
569 else
570 {
571 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %x which has no mapping\n", dadr);
572 }
573 break;
574 case 0x01:
575 /* ICMP */
576 pkt_icmp = (struct ip_icmp*)pkt;
577 if (pkt_icmp->icmp_hdr.type == 0x8 &&
578 (key = address4_mapping_exists (dadr)) != NULL)
579 {
580 GNUNET_free(key);
581 pkt_icmp = GNUNET_malloc(ntohs(pkt->shdr.size));
582 memcpy(pkt_icmp, pkt, ntohs(pkt->shdr.size));
583 GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
584 }
585 break;
586 }
587 } 596 }
597 break;
598 }
588 } 599 }
600 }
589} 601}
590 602
591void write_to_helper(void* buf, size_t len) 603void
604write_to_helper (void *buf, size_t len)
592{ 605{
593 (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len); 606 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
594} 607}
595 608
596void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls) 609void
610schedule_helper_write (struct GNUNET_TIME_Relative time, void *cls)
597{ 611{
598 if (GNUNET_SCHEDULER_NO_TASK != shs_task) return; 612 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
599 GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls); 613 return;
614 GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper,
615 &helper_write, cls);
600} 616}
diff --git a/src/vpn/gnunet-daemon-vpn-helper.h b/src/vpn/gnunet-daemon-vpn-helper.h
index 85436e2cd..963fafd09 100644
--- a/src/vpn/gnunet-daemon-vpn-helper.h
+++ b/src/vpn/gnunet-daemon-vpn-helper.h
@@ -38,33 +38,35 @@ extern GNUNET_SCHEDULER_TaskIdentifier shs_task;
38/** 38/**
39 * Start the helper-process 39 * Start the helper-process
40 */ 40 */
41void start_helper_and_schedule(void *cls, 41void start_helper_and_schedule (void *cls,
42 const struct GNUNET_SCHEDULER_TaskContext *tc); 42 const struct GNUNET_SCHEDULER_TaskContext *tc);
43 43
44/** 44/**
45 * Restart the helper-process 45 * Restart the helper-process
46 */ 46 */
47void restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx); 47void restart_helper (void *cls,
48 const struct GNUNET_SCHEDULER_TaskContext *tskctx);
48 49
49/** 50/**
50 * Read from the helper-process 51 * Read from the helper-process
51 */ 52 */
52void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx); 53void helper_read (void *cls,
54 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx);
53 55
54/** 56/**
55 * Send an dns-answer-packet to the helper 57 * Send an dns-answer-packet to the helper
56 */ 58 */
57void helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx); 59void helper_write (void *cls,
60 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx);
58 61
59/** 62/**
60 * Receive packets from the helper-process 63 * Receive packets from the helper-process
61 */ 64 */
62void message_token(void *cls, 65void message_token (void *cls,
63 void *client, 66 void *client, const struct GNUNET_MessageHeader *message);
64 const struct GNUNET_MessageHeader *message);
65 67
66void write_to_helper(void* buf, size_t len); 68void write_to_helper (void *buf, size_t len);
67 69
68void schedule_helper_write(struct GNUNET_TIME_Relative, void* cls); 70void schedule_helper_write (struct GNUNET_TIME_Relative, void *cls);
69 71
70#endif /* end of include guard: GNUNET-DAEMON-VPN-HELPER_H */ 72#endif /* end of include guard: GNUNET-DAEMON-VPN-HELPER_H */
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
diff --git a/src/vpn/gnunet-daemon-vpn.h b/src/vpn/gnunet-daemon-vpn.h
index 7643a6e4d..ac6e50c72 100644
--- a/src/vpn/gnunet-daemon-vpn.h
+++ b/src/vpn/gnunet-daemon-vpn.h
@@ -35,24 +35,25 @@
35 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and 35 * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
36 * doing nothing for "real" services. 36 * doing nothing for "real" services.
37 */ 37 */
38void 38void process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
39process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc);
40 39
41void send_icmp6_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 40void send_icmp6_response (void *cls,
42void send_icmp4_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 41 const struct GNUNET_SCHEDULER_TaskContext *tc);
42void send_icmp4_response (void *cls,
43 const struct GNUNET_SCHEDULER_TaskContext *tc);
43 44
44size_t 45size_t
45send_udp_service (void *cls, size_t size, void *buf); 46 send_udp_service (void *cls, size_t size, void *buf);
46 47
47GNUNET_HashCode* address6_mapping_exists(unsigned char addr[]); 48GNUNET_HashCode *address6_mapping_exists (unsigned char addr[]);
48GNUNET_HashCode* address4_mapping_exists(uint32_t addr); 49GNUNET_HashCode *address4_mapping_exists (uint32_t addr);
49 50
50unsigned int port_in_ports (uint64_t ports, uint16_t port); 51unsigned int port_in_ports (uint64_t ports, uint16_t port);
51 52
52void 53void
53send_pkt_to_peer (void *cls, 54send_pkt_to_peer (void *cls,
54 const struct GNUNET_PeerIdentity *peer, 55 const struct GNUNET_PeerIdentity *peer,
55 const struct GNUNET_TRANSPORT_ATS_Information *atsi); 56 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
56 57
57/** 58/**
58 * The configuration to use 59 * The configuration to use
@@ -67,22 +68,23 @@ extern struct GNUNET_MESH_Handle *mesh_handle;
67/** 68/**
68 * The hashmap containing the mappings from ipv6-addresses to gnunet-descriptors 69 * The hashmap containing the mappings from ipv6-addresses to gnunet-descriptors
69 */ 70 */
70extern struct GNUNET_CONTAINER_MultiHashMap* hashmap; 71extern struct GNUNET_CONTAINER_MultiHashMap *hashmap;
71 72
72struct map_entry { 73struct map_entry
74{
73 /** The description of the service (used for service) */ 75 /** The description of the service (used for service) */
74 struct GNUNET_vpn_service_descriptor desc; 76 struct GNUNET_vpn_service_descriptor desc;
75 77
76 /** The real address of the service (used for remote) */ 78 /** The real address of the service (used for remote) */
77 char addrlen; 79 char addrlen;
78 char addr[16]; 80 char addr[16];
79 81
80 struct GNUNET_MESH_Tunnel *tunnel; 82 struct GNUNET_MESH_Tunnel *tunnel;
81 uint16_t namelen; 83 uint16_t namelen;
82 char additional_ports[8192]; 84 char additional_ports[8192];
83 85
84 struct GNUNET_CONTAINER_HeapNode* heap_node; 86 struct GNUNET_CONTAINER_HeapNode *heap_node;
85 GNUNET_HashCode hash; 87 GNUNET_HashCode hash;
86 /** 88 /**
87 * After this struct the name is located in DNS-Format! 89 * After this struct the name is located in DNS-Format!
88 */ 90 */
diff --git a/src/vpn/gnunet-dns-parser.c b/src/vpn/gnunet-dns-parser.c
index e26638141..70551af52 100644
--- a/src/vpn/gnunet-dns-parser.c
+++ b/src/vpn/gnunet-dns-parser.c
@@ -11,29 +11,33 @@
11 * @returns The offset of the first unparsed byte (the byte right behind the name) 11 * @returns The offset of the first unparsed byte (the byte right behind the name)
12 */ 12 */
13static unsigned int 13static unsigned int
14parse_dns_name(char* d, const unsigned char* src, unsigned short idx) {/*{{{*/ 14parse_dns_name (char *d, const unsigned char *src, unsigned short idx)
15 char* dest = d; 15{ /*{{{ */
16 16 char *dest = d;
17 int len = src[idx++]; 17
18 while (len != 0) 18 int len = src[idx++];
19 { 19
20 if (len & 0xC0) 20 while (len != 0)
21 { /* Compressed name, offset in this and the next octet */ 21 {
22 unsigned short offset = ((len & 0x3F) << 8) | src[idx++]; 22 if (len & 0xC0)
23 parse_dns_name(dest, src, offset - 12); /* 12 for the Header of the DNS-Packet, idx starts at 0 which is 12 bytes from the start of the packet */ 23 { /* Compressed name, offset in this and the next octet */
24 return idx; 24 unsigned short offset = ((len & 0x3F) << 8) | src[idx++];
25 } 25
26 memcpy(dest, src+idx, len); 26 parse_dns_name (dest, src, offset - 12); /* 12 for the Header of the DNS-Packet, idx starts at 0 which is 12 bytes from the start of the packet */
27 idx += len; 27 return idx;
28 dest += len; 28 }
29 *dest = '.'; 29 memcpy (dest, src + idx, len);
30 dest++; 30 idx += len;
31 len = src[idx++]; 31 dest += len;
32 }; 32 *dest = '.';
33 *dest = 0; 33 dest++;
34 34 len = src[idx++];
35 return idx; 35 };
36 *dest = 0;
37
38 return idx;
36} 39}
40
37/*}}}*/ 41/*}}}*/
38 42
39/** 43/**
@@ -46,116 +50,127 @@ parse_dns_name(char* d, const unsigned char* src, unsigned short idx) {/*{{{*/
46 * @returns The offset of the first unparsed byte (the byte right behind the last record) 50 * @returns The offset of the first unparsed byte (the byte right behind the last record)
47 */ 51 */
48static unsigned short 52static unsigned short
49parse_dns_record(unsigned char* data, /*{{{*/ 53parse_dns_record (unsigned char *data, /*{{{ */
50 struct dns_record** dst, 54 struct dns_record **dst,
51 unsigned short count, 55 unsigned short count, unsigned short idx)
52 unsigned short idx) { 56{
53 int i; 57 int i;
54 unsigned short _idx; 58 unsigned short _idx;
55 for (i = 0; i < count; i++) { 59
56 dst[i] = GNUNET_malloc(sizeof(struct dns_record)); 60 for (i = 0; i < count; i++)
57 dst[i]->name = alloca(255); // see RFC1035, no name can be longer than this. 61 {
58 char* name = dst[i]->name; 62 dst[i] = GNUNET_malloc (sizeof (struct dns_record));
59 63 dst[i]->name = alloca (255); // see RFC1035, no name can be longer than this.
60 _idx = parse_dns_name(name, data, idx); 64 char *name = dst[i]->name;
61 dst[i]->namelen = _idx - idx; 65
62 66 _idx = parse_dns_name (name, data, idx);
63 dst[i]->name = GNUNET_malloc(dst[i]->namelen); 67 dst[i]->namelen = _idx - idx;
64 memcpy(dst[i]->name, name, dst[i]->namelen); 68
65 69 dst[i]->name = GNUNET_malloc (dst[i]->namelen);
66 idx = _idx; 70 memcpy (dst[i]->name, name, dst[i]->namelen);
67 71
68 dst[i]->type = *((unsigned short*)(data+idx)); 72 idx = _idx;
69 idx += 2; 73
70 dst[i]->class = *((unsigned short*)(data+idx)); 74 dst[i]->type = *((unsigned short *) (data + idx));
71 idx += 2; 75 idx += 2;
72 dst[i]->ttl = *((unsigned int*)(data+idx)); 76 dst[i]->class = *((unsigned short *) (data + idx));
73 idx += 4; 77 idx += 2;
74 dst[i]->data_len = *((unsigned short*)(data+idx)); 78 dst[i]->ttl = *((unsigned int *) (data + idx));
75 idx += 2; 79 idx += 4;
76 dst[i]->data = GNUNET_malloc(ntohs(dst[i]->data_len)); 80 dst[i]->data_len = *((unsigned short *) (data + idx));
77 memcpy(dst[i]->data, data+idx, ntohs(dst[i]->data_len)); 81 idx += 2;
78 idx += ntohs(dst[i]->data_len); 82 dst[i]->data = GNUNET_malloc (ntohs (dst[i]->data_len));
79 } 83 memcpy (dst[i]->data, data + idx, ntohs (dst[i]->data_len));
80 return idx; 84 idx += ntohs (dst[i]->data_len);
81}/*}}}*/ 85 }
86 return idx;
87} /*}}} */
82 88
83/** 89/**
84 * Parse a raw DNS-Packet into an usable struct 90 * Parse a raw DNS-Packet into an usable struct
85 */ 91 */
86struct dns_pkt_parsed* 92struct dns_pkt_parsed *
87parse_dns_packet(struct dns_pkt* pkt) {/*{{{*/ 93parse_dns_packet (struct dns_pkt *pkt)
88 struct dns_pkt_parsed* ppkt = GNUNET_malloc(sizeof(struct dns_pkt_parsed)); 94{ /*{{{ */
89 memcpy(&ppkt->s, &pkt->s, sizeof pkt->s); 95 struct dns_pkt_parsed *ppkt = GNUNET_malloc (sizeof (struct dns_pkt_parsed));
90 96
91 unsigned short qdcount = ntohs(ppkt->s.qdcount); 97 memcpy (&ppkt->s, &pkt->s, sizeof pkt->s);
92 unsigned short ancount = ntohs(ppkt->s.ancount); 98
93 unsigned short nscount = ntohs(ppkt->s.nscount); 99 unsigned short qdcount = ntohs (ppkt->s.qdcount);
94 unsigned short arcount = ntohs(ppkt->s.arcount); 100 unsigned short ancount = ntohs (ppkt->s.ancount);
95 101 unsigned short nscount = ntohs (ppkt->s.nscount);
96 ppkt->queries = GNUNET_malloc(qdcount*sizeof(struct dns_query*)); 102 unsigned short arcount = ntohs (ppkt->s.arcount);
97 ppkt->answers = GNUNET_malloc(ancount*sizeof(struct dns_record*)); 103
98 ppkt->nameservers = GNUNET_malloc(nscount*sizeof(struct dns_record*)); 104 ppkt->queries = GNUNET_malloc (qdcount * sizeof (struct dns_query *));
99 ppkt->additional = GNUNET_malloc(arcount*sizeof(struct dns_record*)); 105 ppkt->answers = GNUNET_malloc (ancount * sizeof (struct dns_record *));
100 106 ppkt->nameservers = GNUNET_malloc (nscount * sizeof (struct dns_record *));
101 unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */ 107 ppkt->additional = GNUNET_malloc (arcount * sizeof (struct dns_record *));
102 108
103 /* Parse the Query */ 109 unsigned short idx = 0, _idx; /* This keeps track how far we have parsed the data */
104 int i; 110
105 for (i = 0; i < qdcount; i++) 111 /* Parse the Query */
106 { /*{{{*/ 112 int i;
107 ppkt->queries[i] = GNUNET_malloc(sizeof(struct dns_query)); 113
108 char* name = alloca(255); /* see RFC1035, it can't be more than this. */ 114 for (i = 0; i < qdcount; i++)
109 115 { /*{{{ */
110 _idx = parse_dns_name(name, pkt->data, idx); 116 ppkt->queries[i] = GNUNET_malloc (sizeof (struct dns_query));
111 ppkt->queries[i]->namelen = _idx - idx; 117 char *name = alloca (255); /* see RFC1035, it can't be more than this. */
112 idx = _idx; 118
113 119 _idx = parse_dns_name (name, pkt->data, idx);
114 ppkt->queries[i]->name = GNUNET_malloc(ppkt->queries[i]->namelen); 120 ppkt->queries[i]->namelen = _idx - idx;
115 memcpy(ppkt->queries[i]->name, name, ppkt->queries[i]->namelen); 121 idx = _idx;
116 122
117 ppkt->queries[i]->qtype = *((unsigned short*)(pkt->data+idx)); 123 ppkt->queries[i]->name = GNUNET_malloc (ppkt->queries[i]->namelen);
118 idx += 2; 124 memcpy (ppkt->queries[i]->name, name, ppkt->queries[i]->namelen);
119 ppkt->queries[i]->qclass = *((unsigned short*)(pkt->data+idx)); 125
120 idx += 2; 126 ppkt->queries[i]->qtype = *((unsigned short *) (pkt->data + idx));
121 } 127 idx += 2;
122 /*}}}*/ 128 ppkt->queries[i]->qclass = *((unsigned short *) (pkt->data + idx));
123 idx = parse_dns_record(pkt->data, ppkt->answers, ancount, idx); 129 idx += 2;
124 idx = parse_dns_record(pkt->data, ppkt->nameservers, nscount, idx); 130 }
125 idx = parse_dns_record(pkt->data, ppkt->additional, arcount, idx); 131 /*}}} */
126 return ppkt; 132 idx = parse_dns_record (pkt->data, ppkt->answers, ancount, idx);
127}/*}}}*/ 133 idx = parse_dns_record (pkt->data, ppkt->nameservers, nscount, idx);
134 idx = parse_dns_record (pkt->data, ppkt->additional, arcount, idx);
135 return ppkt;
136} /*}}} */
128 137
129void 138void
130free_parsed_dns_packet(struct dns_pkt_parsed* ppkt) { 139free_parsed_dns_packet (struct dns_pkt_parsed *ppkt)
131 unsigned short qdcount = ntohs(ppkt->s.qdcount); 140{
132 unsigned short ancount = ntohs(ppkt->s.ancount); 141 unsigned short qdcount = ntohs (ppkt->s.qdcount);
133 unsigned short nscount = ntohs(ppkt->s.nscount); 142 unsigned short ancount = ntohs (ppkt->s.ancount);
134 unsigned short arcount = ntohs(ppkt->s.arcount); 143 unsigned short nscount = ntohs (ppkt->s.nscount);
135 144 unsigned short arcount = ntohs (ppkt->s.arcount);
136 int i; 145
137 for (i = 0; i < qdcount; i++) { 146 int i;
138 GNUNET_free(ppkt->queries[i]->name); 147
139 GNUNET_free(ppkt->queries[i]); 148 for (i = 0; i < qdcount; i++)
140 } 149 {
141 GNUNET_free(ppkt->queries); 150 GNUNET_free (ppkt->queries[i]->name);
142 for (i = 0; i < ancount; i++) { 151 GNUNET_free (ppkt->queries[i]);
143 GNUNET_free(ppkt->answers[i]->name); 152 }
144 GNUNET_free(ppkt->answers[i]->data); 153 GNUNET_free (ppkt->queries);
145 GNUNET_free(ppkt->answers[i]); 154 for (i = 0; i < ancount; i++)
146 } 155 {
147 GNUNET_free(ppkt->answers); 156 GNUNET_free (ppkt->answers[i]->name);
148 for (i = 0; i < nscount; i++) { 157 GNUNET_free (ppkt->answers[i]->data);
149 GNUNET_free(ppkt->nameservers[i]->name); 158 GNUNET_free (ppkt->answers[i]);
150 GNUNET_free(ppkt->nameservers[i]->data); 159 }
151 GNUNET_free(ppkt->nameservers[i]); 160 GNUNET_free (ppkt->answers);
152 } 161 for (i = 0; i < nscount; i++)
153 GNUNET_free(ppkt->nameservers); 162 {
154 for (i = 0; i < arcount; i++) { 163 GNUNET_free (ppkt->nameservers[i]->name);
155 GNUNET_free(ppkt->additional[i]->name); 164 GNUNET_free (ppkt->nameservers[i]->data);
156 GNUNET_free(ppkt->additional[i]->data); 165 GNUNET_free (ppkt->nameservers[i]);
157 GNUNET_free(ppkt->additional[i]); 166 }
158 } 167 GNUNET_free (ppkt->nameservers);
159 GNUNET_free(ppkt->additional); 168 for (i = 0; i < arcount; i++)
160 GNUNET_free(ppkt); 169 {
170 GNUNET_free (ppkt->additional[i]->name);
171 GNUNET_free (ppkt->additional[i]->data);
172 GNUNET_free (ppkt->additional[i]);
173 }
174 GNUNET_free (ppkt->additional);
175 GNUNET_free (ppkt);
161} 176}
diff --git a/src/vpn/gnunet-dns-parser.h b/src/vpn/gnunet-dns-parser.h
index ea1a5765c..cf9d55e8a 100644
--- a/src/vpn/gnunet-dns-parser.h
+++ b/src/vpn/gnunet-dns-parser.h
@@ -4,8 +4,8 @@
4#include "platform.h" 4#include "platform.h"
5#include "gnunet-vpn-packet.h" 5#include "gnunet-vpn-packet.h"
6 6
7struct dns_pkt_parsed* parse_dns_packet(struct dns_pkt* pkt); 7struct dns_pkt_parsed *parse_dns_packet (struct dns_pkt *pkt);
8 8
9void free_parsed_dns_packet(struct dns_pkt_parsed* ppkt); 9void free_parsed_dns_packet (struct dns_pkt_parsed *ppkt);
10 10
11#endif 11#endif
diff --git a/src/vpn/gnunet-helper-hijack-dns.c b/src/vpn/gnunet-helper-hijack-dns.c
index ee7ae1873..e6912323b 100644
--- a/src/vpn/gnunet-helper-hijack-dns.c
+++ b/src/vpn/gnunet-helper-hijack-dns.c
@@ -27,75 +27,130 @@
27 27
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29 29
30int fork_and_exec(char* file, char* cmd[]) { 30int
31 pid_t pid = fork(); 31fork_and_exec (char *file, char *cmd[])
32 if (pid < 0) { 32{
33 fprintf(stderr, "could not fork: %s\n", strerror(errno)); 33 pid_t pid = fork ();
34 return GNUNET_SYSERR; 34
35 } 35 if (pid < 0)
36 36 {
37 int st = 0; 37 fprintf (stderr, "could not fork: %s\n", strerror (errno));
38 38 return GNUNET_SYSERR;
39 if (pid == 0) { 39 }
40 execv(file, cmd); 40
41 } else { 41 int st = 0;
42 waitpid(pid, &st, 0); 42
43 } 43 if (pid == 0)
44 return WIFEXITED(st) && (WEXITSTATUS(st) == 0); 44 {
45 execv (file, cmd);
46 }
47 else
48 {
49 waitpid (pid, &st, 0);
50 }
51 return WIFEXITED (st) && (WEXITSTATUS (st) == 0);
45} 52}
46 53
47int main(int argc, char** argv) { 54int
48 int delete = 0; 55main (int argc, char **argv)
49 int port = 0; 56{
50 char* virt_dns; 57 int delete = 0;
51 if (argc < 3) return GNUNET_SYSERR; 58 int port = 0;
52 59 char *virt_dns;
53 if (strncmp(argv[1], "-d", 2) == 0) { 60
54 if (argc < 3) return GNUNET_SYSERR; 61 if (argc < 3)
55 delete = 1; 62 return GNUNET_SYSERR;
56 port = atoi(argv[2]); 63
57 virt_dns = argv[3]; 64 if (strncmp (argv[1], "-d", 2) == 0)
58 } else { 65 {
59 port = atoi(argv[1]); 66 if (argc < 3)
60 virt_dns = argv[2]; 67 return GNUNET_SYSERR;
61 } 68 delete = 1;
62 69 port = atoi (argv[2]);
63 if (port == 0) return GNUNET_SYSERR; 70 virt_dns = argv[3];
64 71 }
65 struct stat s; 72 else
66 if (stat("/sbin/iptables", &s) < 0) { 73 {
67 fprintf(stderr, "stat on /sbin/iptables failed: %s\n", strerror(errno)); 74 port = atoi (argv[1]);
68 return GNUNET_SYSERR; 75 virt_dns = argv[2];
69 } 76 }
70 if (stat("/sbin/ip", &s) < 0) { 77
71 fprintf(stderr, "stat on /sbin/ip failed: %s\n", strerror(errno)); 78 if (port == 0)
72 return GNUNET_SYSERR; 79 return GNUNET_SYSERR;
73 } 80
74 81 struct stat s;
75 char localport[7]; 82
76 snprintf(localport, 7, "%d", port); 83 if (stat ("/sbin/iptables", &s) < 0)
77 84 {
78 int r; 85 fprintf (stderr, "stat on /sbin/iptables failed: %s\n", strerror (errno));
79 if (delete) { 86 return GNUNET_SYSERR;
87 }
88 if (stat ("/sbin/ip", &s) < 0)
89 {
90 fprintf (stderr, "stat on /sbin/ip failed: %s\n", strerror (errno));
91 return GNUNET_SYSERR;
92 }
93
94 char localport[7];
95
96 snprintf (localport, 7, "%d", port);
97
98 int r;
99
100 if (delete)
101 {
80e4: 102e4:
81 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "route", "del", "default", "via", virt_dns,"table","2", NULL}); 103 r = fork_and_exec ("/sbin/ip", (char *[])
104 {
105 "ip", "route", "del", "default", "via", virt_dns,
106 "table", "2", NULL});
82e3: 107e3:
83 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "rule", "del", "fwmark", "3", "table","2", NULL}); 108 r = fork_and_exec ("/sbin/ip", (char *[])
109 {
110 "ip", "rule", "del", "fwmark", "3", "table", "2", NULL});
84e2: 111e2:
85 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL}); 112 r = fork_and_exec ("/sbin/iptables", (char *[])
113 {
114 "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
115 "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL});
86e1: 116e1:
87 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", "--sport", localport, "--dport", "53", "-j", "ACCEPT", NULL}); 117 r = fork_and_exec ("/sbin/iptables", (char *[])
88 if (!delete) r = 0; 118 {
89 } else { 119 "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
90 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p", "udp", "--sport", localport, "--dport", "53", "-j", "ACCEPT", NULL}); 120 "--sport", localport, "--dport", "53", "-j", "ACCEPT",
91 if (!r) goto e1; 121 NULL});
92 r = fork_and_exec("/sbin/iptables", (char*[]){"iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p", "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL}); 122 if (!delete)
93 if (!r) goto e2; 123 r = 0;
94 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "rule", "add", "fwmark", "3", "table","2", NULL}); 124 }
95 if (!r) goto e3; 125 else
96 r = fork_and_exec("/sbin/ip", (char*[]){"ip", "route", "add", "default", "via", virt_dns, "table","2", NULL}); 126 {
97 if (!r) goto e4; 127 r = fork_and_exec ("/sbin/iptables", (char *[])
98 } 128 {
99 if (r) return GNUNET_YES; 129 "iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
100 return GNUNET_SYSERR; 130 "udp", "--sport", localport, "--dport", "53", "-j",
131 "ACCEPT", NULL});
132 if (!r)
133 goto e1;
134 r = fork_and_exec ("/sbin/iptables", (char *[])
135 {
136 "iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
137 "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3",
138 NULL});
139 if (!r)
140 goto e2;
141 r = fork_and_exec ("/sbin/ip", (char *[])
142 {
143 "ip", "rule", "add", "fwmark", "3", "table", "2", NULL});
144 if (!r)
145 goto e3;
146 r = fork_and_exec ("/sbin/ip", (char *[])
147 {
148 "ip", "route", "add", "default", "via", virt_dns,
149 "table", "2", NULL});
150 if (!r)
151 goto e4;
152 }
153 if (r)
154 return GNUNET_YES;
155 return GNUNET_SYSERR;
101} 156}
diff --git a/src/vpn/gnunet-helper-vpn-api.c b/src/vpn/gnunet-helper-vpn-api.c
index 1022d54e9..16bfcbd2e 100644
--- a/src/vpn/gnunet-helper-vpn-api.c
+++ b/src/vpn/gnunet-helper-vpn-api.c
@@ -45,7 +45,7 @@ stop_helper (struct GNUNET_VPN_HELPER_Handle *handle)
45 GNUNET_DISK_pipe_close (handle->helper_in); 45 GNUNET_DISK_pipe_close (handle->helper_in);
46 GNUNET_DISK_pipe_close (handle->helper_out); 46 GNUNET_DISK_pipe_close (handle->helper_out);
47 47
48 GNUNET_SERVER_mst_destroy(handle->mst); 48 GNUNET_SERVER_mst_destroy (handle->mst);
49} 49}
50 50
51extern GNUNET_SCHEDULER_TaskIdentifier shs_task; 51extern GNUNET_SCHEDULER_TaskIdentifier shs_task;
@@ -57,6 +57,7 @@ static void
57helper_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tsdkctx) 57helper_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
58{ 58{
59 struct GNUNET_VPN_HELPER_Handle *handle = cls; 59 struct GNUNET_VPN_HELPER_Handle *handle = cls;
60
60 /* no message can be bigger then 64k */ 61 /* no message can be bigger then 64k */
61 char buf[65535]; 62 char buf[65535];
62 63
@@ -67,34 +68,32 @@ helper_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
67 68
68 /* On read-error, restart the helper */ 69 /* On read-error, restart the helper */
69 if (t <= 0) 70 if (t <= 0)
70 { 71 {
71 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 72 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
72 "Read error for header from vpn-helper: %m\n"); 73 "Read error for header from vpn-helper: %m\n");
73 stop_helper (handle); 74 stop_helper (handle);
74 75
75 /* Restart the helper */ 76 /* Restart the helper */
76 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 77 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
77 handle->restart_task, handle); 78 handle->restart_task, handle);
78 return; 79 return;
79 } 80 }
80 81
81 if (GNUNET_SYSERR == 82 if (GNUNET_SYSERR ==
82 GNUNET_SERVER_mst_receive (handle->mst, handle->client, buf, t, 0, 0)) 83 GNUNET_SERVER_mst_receive (handle->mst, handle->client, buf, t, 0, 0))
83 { 84 {
84 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 85 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "SYSERR from mst\n");
85 "SYSERR from mst\n"); 86 stop_helper (handle);
86 stop_helper (handle);
87 87
88 /* Restart the helper */ 88 /* Restart the helper */
89 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 89 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
90 handle->restart_task, handle); 90 handle->restart_task, handle);
91 return; 91 return;
92 92
93 } 93 }
94 94
95 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 95 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
96 handle->fh_from_helper, &helper_read, 96 handle->fh_from_helper, &helper_read, handle);
97 handle);
98} 97}
99 98
100void 99void
@@ -114,7 +113,7 @@ start_helper (const char *ifname,
114 GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls) 113 GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
115{ 114{
116 struct GNUNET_VPN_HELPER_Handle *handle = 115 struct GNUNET_VPN_HELPER_Handle *handle =
117 GNUNET_malloc (sizeof (struct GNUNET_VPN_HELPER_Handle)); 116 GNUNET_malloc (sizeof (struct GNUNET_VPN_HELPER_Handle));
118 117
119 handle->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO); 118 handle->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
120 handle->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); 119 handle->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
@@ -122,20 +121,20 @@ start_helper (const char *ifname,
122 handle->restart_task = restart_task; 121 handle->restart_task = restart_task;
123 122
124 if (handle->helper_in == NULL || handle->helper_out == NULL) 123 if (handle->helper_in == NULL || handle->helper_out == NULL)
125 { 124 {
126 GNUNET_free (handle); 125 GNUNET_free (handle);
127 return NULL; 126 return NULL;
128 } 127 }
129 128
130 handle->helper_proc = 129 handle->helper_proc =
131 GNUNET_OS_start_process (handle->helper_in, handle->helper_out, 130 GNUNET_OS_start_process (handle->helper_in, handle->helper_out,
132 "gnunet-helper-vpn", process_name, ifname, 131 "gnunet-helper-vpn", process_name, ifname,
133 ipv6addr, ipv6prefix, ipv4addr, ipv4mask, NULL); 132 ipv6addr, ipv6prefix, ipv4addr, ipv4mask, NULL);
134 133
135 handle->fh_from_helper = 134 handle->fh_from_helper =
136 GNUNET_DISK_pipe_handle (handle->helper_out, GNUNET_DISK_PIPE_END_READ); 135 GNUNET_DISK_pipe_handle (handle->helper_out, GNUNET_DISK_PIPE_END_READ);
137 handle->fh_to_helper = 136 handle->fh_to_helper =
138 GNUNET_DISK_pipe_handle (handle->helper_in, GNUNET_DISK_PIPE_END_WRITE); 137 GNUNET_DISK_pipe_handle (handle->helper_in, GNUNET_DISK_PIPE_END_WRITE);
139 138
140 GNUNET_DISK_pipe_close_end (handle->helper_out, GNUNET_DISK_PIPE_END_WRITE); 139 GNUNET_DISK_pipe_close_end (handle->helper_out, GNUNET_DISK_PIPE_END_WRITE);
141 GNUNET_DISK_pipe_close_end (handle->helper_in, GNUNET_DISK_PIPE_END_READ); 140 GNUNET_DISK_pipe_close_end (handle->helper_in, GNUNET_DISK_PIPE_END_READ);
@@ -143,8 +142,7 @@ start_helper (const char *ifname,
143 handle->mst = GNUNET_SERVER_mst_create (cb, cb_cls); 142 handle->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
144 143
145 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 144 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
146 handle->fh_from_helper, &helper_read, 145 handle->fh_from_helper, &helper_read, handle);
147 handle);
148 146
149 return handle; 147 return handle;
150} 148}
diff --git a/src/vpn/gnunet-helper-vpn.c b/src/vpn/gnunet-helper-vpn.c
index 554019d7c..698c46f1d 100644
--- a/src/vpn/gnunet-helper-vpn.c
+++ b/src/vpn/gnunet-helper-vpn.c
@@ -74,23 +74,23 @@ init_tun (char *dev)
74 int fd; 74 int fd;
75 75
76 if (NULL == dev) 76 if (NULL == dev)
77 { 77 {
78 errno = EINVAL; 78 errno = EINVAL;
79 return -1; 79 return -1;
80 } 80 }
81 81
82 if (-1 == (fd = open ("/dev/net/tun", O_RDWR))) 82 if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
83 { 83 {
84 fprintf (stderr, 84 fprintf (stderr,
85 "Error opening `%s': %s\n", "/dev/net/tun", strerror (errno)); 85 "Error opening `%s': %s\n", "/dev/net/tun", strerror (errno));
86 return -1; 86 return -1;
87 } 87 }
88 88
89 if (fd >= FD_SETSIZE) 89 if (fd >= FD_SETSIZE)
90 { 90 {
91 fprintf (stderr, "File descriptor to large: %d", fd); 91 fprintf (stderr, "File descriptor to large: %d", fd);
92 return -1; 92 return -1;
93 } 93 }
94 94
95 memset (&ifr, 0, sizeof (ifr)); 95 memset (&ifr, 0, sizeof (ifr));
96 ifr.ifr_flags = IFF_TUN; 96 ifr.ifr_flags = IFF_TUN;
@@ -99,13 +99,13 @@ init_tun (char *dev)
99 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 99 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
100 100
101 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr)) 101 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
102 { 102 {
103 fprintf (stderr, 103 fprintf (stderr,
104 "Error with ioctl on `%s': %s\n", 104 "Error with ioctl on `%s': %s\n",
105 "/dev/net/tun", strerror (errno)); 105 "/dev/net/tun", strerror (errno));
106 close (fd); 106 close (fd);
107 return -1; 107 return -1;
108 } 108 }
109 strcpy (dev, ifr.ifr_name); 109 strcpy (dev, ifr.ifr_name);
110 return fd; 110 return fd;
111} 111}
@@ -119,9 +119,7 @@ init_tun (char *dev)
119 * @param prefix_len the length of the network-prefix 119 * @param prefix_len the length of the network-prefix
120 */ 120 */
121static void 121static void
122set_address6 (const char *dev, 122set_address6 (const char *dev, const char *address, unsigned long prefix_len)
123 const char *address,
124 unsigned long prefix_len)
125{ 123{
126 struct ifreq ifr; 124 struct ifreq ifr;
127 struct in6_ifreq ifr6; 125 struct in6_ifreq ifr6;
@@ -133,25 +131,20 @@ set_address6 (const char *dev,
133 */ 131 */
134 memset (&sa6, 0, sizeof (struct sockaddr_in6)); 132 memset (&sa6, 0, sizeof (struct sockaddr_in6));
135 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr)) 133 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
136 { 134 {
137 fprintf (stderr, 135 fprintf (stderr,
138 "Failed to parse address `%s': %s\n", 136 "Failed to parse address `%s': %s\n", address, strerror (errno));
139 address, strerror (errno)); 137 exit (1);
140 exit (1); 138 }
141 }
142 139
143 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0))) 140 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
144 { 141 {
145 fprintf (stderr, 142 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
146 "Error creating socket: %s\n", 143 exit (1);
147 strerror (errno)); 144 }
148 exit (1);
149 }
150 145
151 sa6.sin6_family = AF_INET6; 146 sa6.sin6_family = AF_INET6;
152 memcpy (&ifr6.ifr6_addr, 147 memcpy (&ifr6.ifr6_addr, &sa6.sin6_addr, sizeof (struct in6_addr));
153 &sa6.sin6_addr,
154 sizeof (struct in6_addr));
155 148
156 149
157 /* 150 /*
@@ -159,13 +152,10 @@ set_address6 (const char *dev,
159 */ 152 */
160 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 153 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
161 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr)) 154 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
162 { 155 {
163 fprintf (stderr, 156 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
164 "ioctl failed at %d: %s\n", 157 exit (1);
165 __LINE__, 158 }
166 strerror (errno));
167 exit (1);
168 }
169 ifr6.ifr6_ifindex = ifr.ifr_ifindex; 159 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
170 160
171 ifr6.ifr6_prefixlen = prefix_len; 161 ifr6.ifr6_prefixlen = prefix_len;
@@ -174,38 +164,38 @@ set_address6 (const char *dev,
174 * Set the address 164 * Set the address
175 */ 165 */
176 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6)) 166 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
177 { 167 {
178 fprintf (stderr, 168 fprintf (stderr,
179 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 169 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
180 exit (1); 170 exit (1);
181 } 171 }
182 172
183 /* 173 /*
184 * Get the flags 174 * Get the flags
185 */ 175 */
186 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 176 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
187 { 177 {
188 fprintf (stderr, 178 fprintf (stderr,
189 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 179 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
190 exit (1); 180 exit (1);
191 } 181 }
192 182
193 /* 183 /*
194 * Add the UP and RUNNING flags 184 * Add the UP and RUNNING flags
195 */ 185 */
196 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 186 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
197 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 187 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
198 { 188 {
199 fprintf (stderr, 189 fprintf (stderr,
200 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 190 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
201 exit (1); 191 exit (1);
202 } 192 }
203 193
204 if (0 != close (fd)) 194 if (0 != close (fd))
205 { 195 {
206 fprintf (stderr, "close failed: %s\n", strerror (errno)); 196 fprintf (stderr, "close failed: %s\n", strerror (errno));
207 exit (1); 197 exit (1);
208 } 198 }
209} 199}
210 200
211 201
@@ -217,9 +207,7 @@ set_address6 (const char *dev,
217 * @param mask the netmask 207 * @param mask the netmask
218 */ 208 */
219static void 209static void
220set_address4 (const char *dev, 210set_address4 (const char *dev, const char *address, const char *mask)
221 const char *address,
222 const char *mask)
223{ 211{
224 int fd; 212 int fd;
225 struct sockaddr_in *addr; 213 struct sockaddr_in *addr;
@@ -235,21 +223,18 @@ set_address4 (const char *dev,
235 * Parse the address 223 * Parse the address
236 */ 224 */
237 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr)) 225 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
238 { 226 {
239 fprintf (stderr, 227 fprintf (stderr,
240 "Failed to parse address `%s': %s\n", 228 "Failed to parse address `%s': %s\n", address, strerror (errno));
241 address, strerror (errno)); 229 exit (1);
242 exit (1); 230 }
243 } 231
244 232
245
246 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0))) 233 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
247 { 234 {
248 fprintf (stderr, 235 fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
249 "Error creating socket: %s\n", 236 exit (1);
250 strerror (errno)); 237 }
251 exit (1);
252 }
253 238
254 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 239 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
255 240
@@ -257,63 +242,58 @@ set_address4 (const char *dev,
257 * Set the address 242 * Set the address
258 */ 243 */
259 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr)) 244 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
260 { 245 {
261 fprintf (stderr, 246 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
262 "ioctl failed at %d: %s\n", 247 exit (1);
263 __LINE__, 248 }
264 strerror (errno));
265 exit (1);
266 }
267 249
268 /* 250 /*
269 * Parse the netmask 251 * Parse the netmask
270 */ 252 */
271 addr = (struct sockaddr_in *) &(ifr.ifr_netmask); 253 addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
272 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr)) 254 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
273 { 255 {
274 fprintf (stderr, 256 fprintf (stderr,
275 "Failed to parse address `%s': %s\n", 257 "Failed to parse address `%s': %s\n", mask, strerror (errno));
276 mask, 258 exit (1);
277 strerror (errno)); 259 }
278 exit (1);
279 }
280 260
281 /* 261 /*
282 * Set the netmask 262 * Set the netmask
283 */ 263 */
284 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr)) 264 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
285 { 265 {
286 fprintf (stderr, 266 fprintf (stderr,
287 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 267 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
288 exit (1); 268 exit (1);
289 } 269 }
290 270
291 /* 271 /*
292 * Get the flags 272 * Get the flags
293 */ 273 */
294 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 274 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
295 { 275 {
296 fprintf (stderr, 276 fprintf (stderr,
297 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 277 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
298 exit (1); 278 exit (1);
299 } 279 }
300 280
301 /* 281 /*
302 * Add the UP and RUNNING flags 282 * Add the UP and RUNNING flags
303 */ 283 */
304 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 284 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
305 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 285 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
306 { 286 {
307 fprintf (stderr, 287 fprintf (stderr,
308 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno)); 288 "ioctl failed at line %d: %s\n", __LINE__, strerror (errno));
309 exit (1); 289 exit (1);
310 } 290 }
311 291
312 if (0 != close (fd)) 292 if (0 != close (fd))
313 { 293 {
314 fprintf (stderr, "close failed: %s\n", strerror (errno)); 294 fprintf (stderr, "close failed: %s\n", strerror (errno));
315 exit (1); 295 exit (1);
316 } 296 }
317} 297}
318 298
319 299
@@ -340,188 +320,182 @@ run (int fd_tun)
340 320
341 /* read refers to reading from fd_tun, writing to stdout */ 321 /* read refers to reading from fd_tun, writing to stdout */
342 int read_open = 1; 322 int read_open = 1;
323
343 /* write refers to reading from stdin, writing to fd_tun */ 324 /* write refers to reading from stdin, writing to fd_tun */
344 int write_open = 1; 325 int write_open = 1;
345 326
346 while ((1 == read_open) || (1 == write_open)) 327 while ((1 == read_open) || (1 == write_open))
328 {
329 FD_ZERO (&fds_w);
330 FD_ZERO (&fds_r);
331
332 /*
333 * We are supposed to read and the buffer is empty
334 * -> select on read from tun
335 */
336 if (read_open && (0 == buftun_size))
337 FD_SET (fd_tun, &fds_r);
338
339 /*
340 * We are supposed to read and the buffer is not empty
341 * -> select on write to stdout
342 */
343 if (read_open && (0 != buftun_size))
344 FD_SET (1, &fds_w);
345
346 /*
347 * We are supposed to write and the buffer is empty
348 * -> select on read from stdin
349 */
350 if (write_open && (NULL == bufin_read))
351 FD_SET (0, &fds_r);
352
353 /*
354 * We are supposed to write and the buffer is not empty
355 * -> select on write to tun
356 */
357 if (write_open && (NULL != bufin_read))
358 FD_SET (fd_tun, &fds_w);
359
360 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
361
362 if (-1 == r)
347 { 363 {
348 FD_ZERO (&fds_w); 364 if (EINTR == errno)
349 FD_ZERO (&fds_r); 365 continue;
350 366 fprintf (stderr, "select failed: %s\n", strerror (errno));
351 /* 367 exit (1);
352 * We are supposed to read and the buffer is empty 368 }
353 * -> select on read from tun 369
354 */ 370 if (r > 0)
355 if (read_open && (0 == buftun_size)) 371 {
356 FD_SET (fd_tun, &fds_r); 372 if (FD_ISSET (fd_tun, &fds_r))
357 373 {
358 /* 374 buftun_size =
359 * We are supposed to read and the buffer is not empty 375 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
360 * -> select on write to stdout 376 MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
361 */ 377 if (-1 == buftun_size)
362 if (read_open && (0 != buftun_size)) 378 {
363 FD_SET (1, &fds_w); 379 fprintf (stderr, "read-error: %s\n", strerror (errno));
364 380 shutdown (fd_tun, SHUT_RD);
365 /* 381 shutdown (1, SHUT_WR);
366 * We are supposed to write and the buffer is empty 382 read_open = 0;
367 * -> select on read from stdin 383 buftun_size = 0;
368 */ 384 }
369 if (write_open && (NULL == bufin_read)) 385 else if (0 == buftun_size)
370 FD_SET (0, &fds_r); 386 {
371 387 fprintf (stderr, "EOF on tun\n");
372 /* 388 shutdown (fd_tun, SHUT_RD);
373 * We are supposed to write and the buffer is not empty 389 shutdown (1, SHUT_WR);
374 * -> select on write to tun 390 read_open = 0;
375 */ 391 buftun_size = 0;
376 if (write_open && (NULL != bufin_read)) 392 }
377 FD_SET (fd_tun, &fds_w); 393 else
378 394 {
379 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL); 395 buftun_read = buftun;
380 if (-1 == r) 396 struct GNUNET_MessageHeader *hdr =
381 { 397 (struct GNUNET_MessageHeader *) buftun;
382 if (EINTR == errno) 398 buftun_size += sizeof (struct GNUNET_MessageHeader);
383 continue; 399 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
384 fprintf (stderr, "select failed: %s\n", strerror (errno)); 400 hdr->size = htons (buftun_size);
385 exit (1); 401 }
386 } 402 }
387 403 else if (FD_ISSET (1, &fds_w))
388 if (r > 0) 404 {
389 { 405 ssize_t written = write (1, buftun_read, buftun_size);
390 if (FD_ISSET (fd_tun, &fds_r)) 406
391 { 407 if (-1 == written)
392 buftun_size = 408 {
393 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader), 409 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
394 MAX_SIZE - sizeof (struct GNUNET_MessageHeader)); 410 shutdown (fd_tun, SHUT_RD);
395 if (-1 == buftun_size) 411 shutdown (1, SHUT_WR);
396 { 412 read_open = 0;
397 fprintf (stderr, "read-error: %s\n", strerror (errno)); 413 buftun_size = 0;
398 shutdown (fd_tun, SHUT_RD); 414 }
399 shutdown (1, SHUT_WR); 415 else if (0 == written)
400 read_open = 0; 416 {
401 buftun_size = 0; 417 fprintf (stderr, "write returned 0!?\n");
402 } 418 exit (1);
403 else if (0 == buftun_size) 419 }
404 { 420 else
405 fprintf (stderr, "EOF on tun\n"); 421 {
406 shutdown (fd_tun, SHUT_RD); 422 buftun_size -= written;
407 shutdown (1, SHUT_WR); 423 buftun_read += written;
408 read_open = 0; 424 }
409 buftun_size = 0; 425 }
410 } 426
411 else 427 if (FD_ISSET (0, &fds_r))
412 { 428 {
413 buftun_read = buftun; 429 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
414 struct GNUNET_MessageHeader *hdr = 430 if (-1 == bufin_size)
415 (struct GNUNET_MessageHeader *) buftun; 431 {
416 buftun_size += sizeof (struct GNUNET_MessageHeader); 432 fprintf (stderr, "read-error: %s\n", strerror (errno));
417 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); 433 shutdown (0, SHUT_RD);
418 hdr->size = htons (buftun_size); 434 shutdown (fd_tun, SHUT_WR);
419 } 435 write_open = 0;
420 } 436 bufin_size = 0;
421 else if (FD_ISSET (1, &fds_w)) 437 }
422 { 438 else if (0 == bufin_size)
423 ssize_t written = write (1, buftun_read, buftun_size); 439 {
424 if (-1 == written) 440 fprintf (stderr, "EOF on stdin\n");
425 { 441 shutdown (0, SHUT_RD);
426 fprintf (stderr, 442 shutdown (fd_tun, SHUT_WR);
427 "write-error to stdout: %s\n", 443 write_open = 0;
428 strerror (errno)); 444 bufin_size = 0;
429 shutdown (fd_tun, SHUT_RD); 445 }
430 shutdown (1, SHUT_WR); 446 else
431 read_open = 0; 447 {
432 buftun_size = 0; 448 struct GNUNET_MessageHeader *hdr;
433 } 449
434 else if (0 == written) 450PROCESS_BUFFER:
435 { 451 bufin_rpos += bufin_size;
436 fprintf (stderr, 452 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
437 "write returned 0!?\n"); 453 continue;
438 exit (1); 454 hdr = (struct GNUNET_MessageHeader *) bufin;
439 } 455 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
440 else 456 {
441 { 457 fprintf (stderr, "protocol violation!\n");
442 buftun_size -= written; 458 exit (1);
443 buftun_read += written; 459 }
444 } 460 if (ntohs (hdr->size) > bufin_rpos)
445 } 461 continue;
446 462 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
447 if (FD_ISSET (0, &fds_r)) 463 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
448 { 464 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
449 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos); 465 }
450 if (-1 == bufin_size) 466 }
451 { 467 else if (FD_ISSET (fd_tun, &fds_w))
452 fprintf (stderr, 468 {
453 "read-error: %s\n", 469 ssize_t written = write (fd_tun, bufin_read, bufin_size);
454 strerror (errno)); 470
455 shutdown (0, SHUT_RD); 471 if (-1 == written)
456 shutdown (fd_tun, SHUT_WR); 472 {
457 write_open = 0; 473 fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
458 bufin_size = 0; 474 shutdown (0, SHUT_RD);
459 } 475 shutdown (fd_tun, SHUT_WR);
460 else if (0 == bufin_size) 476 write_open = 0;
461 { 477 bufin_size = 0;
462 fprintf (stderr, 478 }
463 "EOF on stdin\n"); 479 else if (0 == written)
464 shutdown (0, SHUT_RD); 480 {
465 shutdown (fd_tun, SHUT_WR); 481 fprintf (stderr, "write returned 0!?\n");
466 write_open = 0; 482 exit (1);
467 bufin_size = 0; 483 }
468 } 484 else
469 else 485 {
470 { 486 bufin_size -= written;
471 struct GNUNET_MessageHeader *hdr; 487 bufin_read += written;
472 488 if (0 == bufin_size)
473 PROCESS_BUFFER: 489 {
474 bufin_rpos += bufin_size; 490 memmove (bufin, bufin_read, bufin_rpos);
475 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader)) 491 bufin_read = NULL; /* start reading again */
476 continue; 492 bufin_size = 0;
477 hdr = (struct GNUNET_MessageHeader *) bufin; 493 goto PROCESS_BUFFER;
478 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) 494 }
479 { 495 }
480 fprintf (stderr, "protocol violation!\n"); 496 }
481 exit (1);
482 }
483 if (ntohs (hdr->size) > bufin_rpos)
484 continue;
485 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
486 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
487 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
488 }
489 }
490 else if (FD_ISSET (fd_tun, &fds_w))
491 {
492 ssize_t written = write (fd_tun, bufin_read, bufin_size);
493 if (-1 == written)
494 {
495 fprintf (stderr, "write-error to tun: %s\n",
496 strerror (errno));
497 shutdown (0, SHUT_RD);
498 shutdown (fd_tun, SHUT_WR);
499 write_open = 0;
500 bufin_size = 0;
501 }
502 else if (0 == written)
503 {
504 fprintf (stderr,
505 "write returned 0!?\n");
506 exit (1);
507 }
508 else
509 {
510 bufin_size -= written;
511 bufin_read += written;
512 if (0 == bufin_size)
513 {
514 memmove (bufin,
515 bufin_read,
516 bufin_rpos);
517 bufin_read = NULL; /* start reading again */
518 bufin_size = 0;
519 goto PROCESS_BUFFER;
520 }
521 }
522 }
523 }
524 } 497 }
498 }
525} 499}
526 500
527 501
@@ -532,35 +506,33 @@ main (int argc, char **argv)
532 int fd_tun; 506 int fd_tun;
533 507
534 if (6 != argc) 508 if (6 != argc)
535 { 509 {
536 fprintf (stderr, 510 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
537 "Fatal: must supply 5 arguments!\n"); 511 return 1;
538 return 1; 512 }
539 }
540 513
541 strncpy(dev, argv[1], IFNAMSIZ); 514 strncpy (dev, argv[1], IFNAMSIZ);
542 dev[IFNAMSIZ - 1] = '\0'; 515 dev[IFNAMSIZ - 1] = '\0';
543 516
544 if (-1 == (fd_tun = init_tun (dev))) 517 if (-1 == (fd_tun = init_tun (dev)))
545 { 518 {
546 fprintf (stderr, 519 fprintf (stderr, "Fatal: could not initialize tun-interface\n");
547 "Fatal: could not initialize tun-interface\n"); 520 return 1;
548 return 1; 521 }
549 }
550 522
551 { 523 {
552 const char *address = argv[2]; 524 const char *address = argv[2];
553 long prefix_len = atol(argv[3]); 525 long prefix_len = atol (argv[3]);
554 526
555 if ( (prefix_len < 1) || (prefix_len > 127) ) 527 if ((prefix_len < 1) || (prefix_len > 127))
556 { 528 {
557 fprintf(stderr, "Fatal: prefix_len out of range\n"); 529 fprintf (stderr, "Fatal: prefix_len out of range\n");
558 return 1; 530 return 1;
559 } 531 }
560 532
561 set_address6 (dev, address, prefix_len); 533 set_address6 (dev, address, prefix_len);
562 } 534 }
563 535
564 { 536 {
565 const char *address = argv[4]; 537 const char *address = argv[4];
566 const char *mask = argv[5]; 538 const char *mask = argv[5];
@@ -569,14 +541,12 @@ main (int argc, char **argv)
569 } 541 }
570 542
571 uid_t uid = getuid (); 543 uid_t uid = getuid ();
544
572 if (0 != setresuid (uid, uid, uid)) 545 if (0 != setresuid (uid, uid, uid))
573 fprintf (stderr, 546 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
574 "Failed to setresuid: %s\n",
575 strerror (errno));
576 if (SIG_ERR == signal (SIGPIPE, SIG_IGN)) 547 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
577 fprintf (stderr, 548 fprintf (stderr,
578 "Failed to protect against SIGPIPE: %s\n", 549 "Failed to protect against SIGPIPE: %s\n", strerror (errno));
579 strerror (errno));
580 run (fd_tun); 550 run (fd_tun);
581 close (fd_tun); 551 close (fd_tun);
582 return 0; 552 return 0;
diff --git a/src/vpn/gnunet-service-dns-p.h b/src/vpn/gnunet-service-dns-p.h
index 6ffc2e58b..c48786d66 100644
--- a/src/vpn/gnunet-service-dns-p.h
+++ b/src/vpn/gnunet-service-dns-p.h
@@ -3,102 +3,108 @@
3 3
4#include "gnunet_common.h" 4#include "gnunet_common.h"
5 5
6struct query_packet { 6struct query_packet
7 struct GNUNET_MessageHeader hdr; 7{
8 struct GNUNET_MessageHeader hdr;
8 9
9 /** 10 /**
10 * The IP-Address this query was originally sent to 11 * The IP-Address this query was originally sent to
11 */ 12 */
12 unsigned orig_to:32 GNUNET_PACKED; 13 unsigned orig_to:32 GNUNET_PACKED;
13 /** 14 /**
14 * The IP-Address this query was originally sent from 15 * The IP-Address this query was originally sent from
15 */ 16 */
16 unsigned orig_from:32 GNUNET_PACKED; 17 unsigned orig_from:32 GNUNET_PACKED;
17 /** 18 /**
18 * The UDP-Portthis query was originally sent from 19 * The UDP-Portthis query was originally sent from
19 */ 20 */
20 unsigned src_port:16 GNUNET_PACKED; 21 unsigned src_port:16 GNUNET_PACKED;
21 22
22 unsigned char data[1]; /* The DNS-Packet */ 23 unsigned char data[1]; /* The DNS-Packet */
23}; 24};
24 25
25struct query_packet_list { 26struct query_packet_list
26 struct query_packet_list* next GNUNET_PACKED; 27{
27 struct query_packet_list* prev GNUNET_PACKED; 28 struct query_packet_list *next GNUNET_PACKED;
28 struct query_packet pkt; 29 struct query_packet_list *prev GNUNET_PACKED;
30 struct query_packet pkt;
29}; 31};
30 32
31enum GNUNET_DNS_ANSWER_Subtype { 33enum GNUNET_DNS_ANSWER_Subtype
34{
32 /** 35 /**
33 * Answers of this type contain a dns-packet that just has to be transmitted 36 * Answers of this type contain a dns-packet that just has to be transmitted
34 */ 37 */
35 GNUNET_DNS_ANSWER_TYPE_IP, 38 GNUNET_DNS_ANSWER_TYPE_IP,
36 39
37 /** 40 /**
38 * Answers of this type contain an incomplete dns-packet. The IP-Address 41 * Answers of this type contain an incomplete dns-packet. The IP-Address
39 * is all 0s. The addroffset points to it. 42 * is all 0s. The addroffset points to it.
40 */ 43 */
41 GNUNET_DNS_ANSWER_TYPE_SERVICE, 44 GNUNET_DNS_ANSWER_TYPE_SERVICE,
42 45
43 /** 46 /**
44 * Answers of this type contain an incomplete dns-packet as answer to a 47 * Answers of this type contain an incomplete dns-packet as answer to a
45 * PTR-Query. The resolved name is not allocated. The addroffset points to it. 48 * PTR-Query. The resolved name is not allocated. The addroffset points to it.
46 */ 49 */
47 GNUNET_DNS_ANSWER_TYPE_REV, 50 GNUNET_DNS_ANSWER_TYPE_REV,
48 51
49 /** 52 /**
50 * Answers of this type contains an IP6-Address but traffic to this IP should 53 * Answers of this type contains an IP6-Address but traffic to this IP should
51 * be routed through the GNUNet. 54 * be routed through the GNUNet.
52 */ 55 */
53 GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA, 56 GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA,
54 57
55 /** 58 /**
56 * Answers of this type contains an IP4-Address but traffic to this IP should 59 * Answers of this type contains an IP4-Address but traffic to this IP should
57 * be routed through the GNUNet. 60 * be routed through the GNUNet.
58 */ 61 */
59 GNUNET_DNS_ANSWER_TYPE_REMOTE_A 62 GNUNET_DNS_ANSWER_TYPE_REMOTE_A
60}; 63};
61 64
62struct GNUNET_vpn_service_descriptor { 65struct GNUNET_vpn_service_descriptor
63 GNUNET_HashCode peer GNUNET_PACKED; 66{
64 GNUNET_HashCode service_descriptor GNUNET_PACKED; 67 GNUNET_HashCode peer GNUNET_PACKED;
65 uint64_t ports GNUNET_PACKED; 68 GNUNET_HashCode service_descriptor GNUNET_PACKED;
66 uint32_t service_type GNUNET_PACKED; 69 uint64_t ports GNUNET_PACKED;
70 uint32_t service_type GNUNET_PACKED;
67}; 71};
68 72
69struct answer_packet { 73struct answer_packet
70 /* General data */ 74{
71 struct GNUNET_MessageHeader hdr; 75 /* General data */
72 enum GNUNET_DNS_ANSWER_Subtype subtype GNUNET_PACKED; 76 struct GNUNET_MessageHeader hdr;
73 77 enum GNUNET_DNS_ANSWER_Subtype subtype GNUNET_PACKED;
74 unsigned from:32 GNUNET_PACKED; 78
75 unsigned to:32 GNUNET_PACKED; 79 unsigned from:32 GNUNET_PACKED;
76 unsigned dst_port:16 GNUNET_PACKED; 80 unsigned to:32 GNUNET_PACKED;
77 /* -- */ 81 unsigned dst_port:16 GNUNET_PACKED;
78 82 /* -- */
79 /* Data for GNUNET_DNS_ANSWER_TYPE_SERVICE */ 83
80 struct GNUNET_vpn_service_descriptor service_descr; 84 /* Data for GNUNET_DNS_ANSWER_TYPE_SERVICE */
81 /* -- */ 85 struct GNUNET_vpn_service_descriptor service_descr;
82 86 /* -- */
83 /* Data for GNUNET_DNS_ANSWER_TYPE_REV */ 87
84 /* The offsett in octets from the beginning of the struct to the field 88 /* Data for GNUNET_DNS_ANSWER_TYPE_REV */
85 * in data where the IP-Address has to go. */ 89 /* The offsett in octets from the beginning of the struct to the field
86 uint16_t addroffset GNUNET_PACKED; 90 * in data where the IP-Address has to go. */
87 /* -- */ 91 uint16_t addroffset GNUNET_PACKED;
88 92 /* -- */
89 /* Data for GNUNET_DNS_ANSWER_TYPE_REMOTE */ 93
90 /* either 4 or 16 */ 94 /* Data for GNUNET_DNS_ANSWER_TYPE_REMOTE */
91 char addrsize; 95 /* either 4 or 16 */
92 unsigned char addr[16]; 96 char addrsize;
93 /* -- */ 97 unsigned char addr[16];
94 98 /* -- */
95 unsigned char data[1]; 99
100 unsigned char data[1];
96}; 101};
97 102
98struct answer_packet_list { 103struct answer_packet_list
99 struct answer_packet_list* next GNUNET_PACKED; 104{
100 struct answer_packet_list* prev GNUNET_PACKED; 105 struct answer_packet_list *next GNUNET_PACKED;
101 struct answer_packet pkt; 106 struct answer_packet_list *prev GNUNET_PACKED;
107 struct answer_packet pkt;
102}; 108};
103 109
104#endif 110#endif
diff --git a/src/vpn/gnunet-service-dns.c b/src/vpn/gnunet-service-dns.c
index fca21ee4d..9959e9564 100644
--- a/src/vpn/gnunet-service-dns.c
+++ b/src/vpn/gnunet-service-dns.c
@@ -83,31 +83,33 @@ static struct answer_packet_list *tail;
83 * = 17 MiB on 64 bit. 83 * = 17 MiB on 64 bit.
84 * = 11 MiB on 32 bit. 84 * = 11 MiB on 32 bit.
85 */ 85 */
86static struct { 86static struct
87 unsigned valid:1; 87{
88 struct GNUNET_SERVER_Client* client; 88 unsigned valid:1;
89 struct GNUNET_MESH_Tunnel *tunnel; 89 struct GNUNET_SERVER_Client *client;
90 uint32_t local_ip; 90 struct GNUNET_MESH_Tunnel *tunnel;
91 uint32_t remote_ip; 91 uint32_t local_ip;
92 uint16_t local_port; 92 uint32_t remote_ip;
93 char* name; 93 uint16_t local_port;
94 uint8_t namelen; 94 char *name;
95 uint8_t namelen;
95} query_states[UINT16_MAX]; 96} query_states[UINT16_MAX];
96 97
97/** 98/**
98 * A struct used to give more than one value as 99 * A struct used to give more than one value as
99 * closure to receive_dht 100 * closure to receive_dht
100 */ 101 */
101struct receive_dht_cls { 102struct receive_dht_cls
102 uint16_t id; 103{
103 struct GNUNET_DHT_GetHandle* handle; 104 uint16_t id;
105 struct GNUNET_DHT_GetHandle *handle;
104}; 106};
105 107
106struct tunnel_notify_queue 108struct tunnel_notify_queue
107{ 109{
108 struct tunnel_notify_queue* next; 110 struct tunnel_notify_queue *next;
109 struct tunnel_notify_queue* prev; 111 struct tunnel_notify_queue *prev;
110 void* cls; 112 void *cls;
111 size_t len; 113 size_t len;
112 GNUNET_CONNECTION_TransmitReadyNotify cb; 114 GNUNET_CONNECTION_TransmitReadyNotify cb;
113}; 115};
@@ -116,30 +118,31 @@ struct tunnel_notify_queue
116 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port. 118 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
117 */ 119 */
118static void 120static void
119hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc) 121hijack (void *cls
122 __attribute__ ((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
120{ 123{
121 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 124 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
122 return; 125 return;
123 126
124 if (0 == dnsoutport) 127 if (0 == dnsoutport)
125 { 128 {
126 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Delaying the hijacking, port is still %d!\n", dnsoutport); 129 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL); 130 "Delaying the hijacking, port is still %d!\n", dnsoutport);
128 return; 131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
129 } 132 return;
133 }
130 134
131 char port_s[6]; 135 char port_s[6];
132 char *virt_dns; 136 char *virt_dns;
133 struct GNUNET_OS_Process *proc; 137 struct GNUNET_OS_Process *proc;
134 138
135 if (GNUNET_SYSERR == 139 if (GNUNET_SYSERR ==
136 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", 140 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
137 &virt_dns)) 141 {
138 { 142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 143 "No entry 'VIRTDNS' in configuration!\n");
140 "No entry 'VIRTDNS' in configuration!\n"); 144 exit (1);
141 exit (1); 145 }
142 }
143 146
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport); 147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
145 snprintf (port_s, 6, "%d", dnsoutport); 148 snprintf (port_s, 6, "%d", dnsoutport);
@@ -163,13 +166,12 @@ unhijack (unsigned short port)
163 struct GNUNET_OS_Process *proc; 166 struct GNUNET_OS_Process *proc;
164 167
165 if (GNUNET_SYSERR == 168 if (GNUNET_SYSERR ==
166 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", 169 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
167 &virt_dns)) 170 {
168 { 171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 172 "No entry 'VIRTDNS' in configuration!\n");
170 "No entry 'VIRTDNS' in configuration!\n"); 173 exit (1);
171 exit (1); 174 }
172 }
173 175
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port); 176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
175 snprintf (port_s, 6, "%d", port); 177 snprintf (port_s, 6, "%d", port);
@@ -187,33 +189,34 @@ unhijack (unsigned short port)
187 * system. 189 * system.
188 */ 190 */
189static size_t 191static size_t
190send_answer(void* cls, size_t size, void* buf) { 192send_answer (void *cls, size_t size, void *buf)
191 struct answer_packet_list* query = head; 193{
192 size_t len = ntohs(query->pkt.hdr.size); 194 struct answer_packet_list *query = head;
195 size_t len = ntohs (query->pkt.hdr.size);
193 196
194 GNUNET_assert(len <= size); 197 GNUNET_assert (len <= size);
195 198
196 memcpy(buf, &query->pkt.hdr, len); 199 memcpy (buf, &query->pkt.hdr, len);
197 200
198 GNUNET_CONTAINER_DLL_remove (head, tail, query); 201 GNUNET_CONTAINER_DLL_remove (head, tail, query);
199 202
200 GNUNET_free(query); 203 GNUNET_free (query);
201 204
202 /* When more data is to be sent, reschedule */ 205 /* When more data is to be sent, reschedule */
203 if (head != NULL) 206 if (head != NULL)
204 GNUNET_SERVER_notify_transmit_ready(cls, 207 GNUNET_SERVER_notify_transmit_ready (cls,
205 ntohs(head->pkt.hdr.size), 208 ntohs (head->pkt.hdr.size),
206 GNUNET_TIME_UNIT_FOREVER_REL, 209 GNUNET_TIME_UNIT_FOREVER_REL,
207 &send_answer, 210 &send_answer, cls);
208 cls);
209 211
210 return len; 212 return len;
211} 213}
212 214
213struct tunnel_cls { 215struct tunnel_cls
214 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED; 216{
215 struct GNUNET_MessageHeader hdr; 217 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
216 struct dns_pkt dns; 218 struct GNUNET_MessageHeader hdr;
219 struct dns_pkt dns;
217}; 220};
218 221
219struct tunnel_cls *remote_pending[UINT16_MAX]; 222struct tunnel_cls *remote_pending[UINT16_MAX];
@@ -224,8 +227,9 @@ mesh_send_response (void *cls, size_t size, void *buf)
224 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 227 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
225 struct GNUNET_MessageHeader *hdr = buf; 228 struct GNUNET_MessageHeader *hdr = buf;
226 uint32_t *sz = cls; 229 uint32_t *sz = cls;
227 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel**)(sz+1); 230 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
228 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1); 231 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
232
229 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS); 233 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
230 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader)); 234 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
231 235
@@ -237,27 +241,29 @@ mesh_send_response (void *cls, size_t size, void *buf)
237 241
238 memcpy (hdr + 1, dns, *sz); 242 memcpy (hdr + 1, dns, *sz);
239 243
240 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel)) 244 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
241 { 245 {
242 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel); 246 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
243 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel); 247 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
244 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel); 248 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
245 249
246 GNUNET_CONTAINER_DLL_remove(head, tail, element); 250 GNUNET_CONTAINER_DLL_remove (head, tail, element);
247 251
248 GNUNET_MESH_tunnel_set_head(*tunnel, head); 252 GNUNET_MESH_tunnel_set_head (*tunnel, head);
249 GNUNET_MESH_tunnel_set_tail(*tunnel, tail); 253 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
250 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel, 254 struct GNUNET_MESH_TransmitHandle *th =
251 GNUNET_NO, 255 GNUNET_MESH_notify_transmit_ready (*tunnel,
252 42, 256 GNUNET_NO,
253 GNUNET_TIME_relative_divide 257 42,
254 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 258 GNUNET_TIME_relative_divide
255 (const struct GNUNET_PeerIdentity *) 259 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
256 NULL, element->len, 260 (const struct GNUNET_PeerIdentity *)
257 element->cb, element->cls); 261 NULL, element->len,
258 /* save the handle */ 262 element->cb, element->cls);
259 GNUNET_MESH_tunnel_set_data(*tunnel, th); 263
260 } 264 /* save the handle */
265 GNUNET_MESH_tunnel_set_data (*tunnel, th);
266 }
261 267
262 GNUNET_free (cls); 268 GNUNET_free (cls);
263 269
@@ -268,37 +274,43 @@ static size_t
268mesh_send (void *cls, size_t size, void *buf) 274mesh_send (void *cls, size_t size, void *buf)
269{ 275{
270 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; 276 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
271 GNUNET_MESH_tunnel_set_data(cls_->tunnel, NULL);
272 277
273 GNUNET_assert(cls_->hdr.size <= size); 278 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
279
280 GNUNET_assert (cls_->hdr.size <= size);
274 281
275 size = cls_->hdr.size; 282 size = cls_->hdr.size;
276 cls_->hdr.size = htons(cls_->hdr.size); 283 cls_->hdr.size = htons (cls_->hdr.size);
277 284
278 memcpy(buf, &cls_->hdr, size); 285 memcpy (buf, &cls_->hdr, size);
279 286
280 if (NULL != GNUNET_MESH_tunnel_get_head(cls_->tunnel)) 287 if (NULL != GNUNET_MESH_tunnel_get_head (cls_->tunnel))
281 { 288 {
282 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(cls_->tunnel); 289 struct tunnel_notify_queue *element =
283 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(cls_->tunnel); 290 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
284 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(cls_->tunnel); 291 struct tunnel_notify_queue *head =
285 292 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
286 GNUNET_CONTAINER_DLL_remove(head, tail, element); 293 struct tunnel_notify_queue *tail =
287 294 GNUNET_MESH_tunnel_get_tail (cls_->tunnel);
288 GNUNET_MESH_tunnel_set_head(cls_->tunnel, head); 295
289 GNUNET_MESH_tunnel_set_tail(cls_->tunnel, tail); 296 GNUNET_CONTAINER_DLL_remove (head, tail, element);
290 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (cls_->tunnel, 297
291 GNUNET_NO, 298 GNUNET_MESH_tunnel_set_head (cls_->tunnel, head);
292 42, 299 GNUNET_MESH_tunnel_set_tail (cls_->tunnel, tail);
293 GNUNET_TIME_relative_divide 300 struct GNUNET_MESH_TransmitHandle *th =
294 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 301 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
295 (const struct GNUNET_PeerIdentity *) 302 GNUNET_NO,
296 NULL, element->len, 303 42,
297 element->cb, element->cls); 304 GNUNET_TIME_relative_divide
298 /* save the handle */ 305 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
299 GNUNET_MESH_tunnel_set_data(cls_->tunnel, th); 306 (const struct GNUNET_PeerIdentity *)
300 GNUNET_free(element); 307 NULL, element->len,
301 } 308 element->cb, element->cls);
309
310 /* save the handle */
311 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
312 GNUNET_free (element);
313 }
302 314
303 return size; 315 return size;
304} 316}
@@ -312,13 +324,14 @@ mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
312 if (NULL == peer) 324 if (NULL == peer)
313 return; 325 return;
314 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; 326 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
327
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Connected to peer %s, %x, sending query with id %d\n", 329 "Connected to peer %s, %x, sending query with id %d\n",
317 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id)); 330 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
318 331
319 if (NULL == GNUNET_MESH_tunnel_get_data (cls_->tunnel)) 332 if (NULL == GNUNET_MESH_tunnel_get_data (cls_->tunnel))
320 { 333 {
321 struct GNUNET_MESH_TransmitHandle *th = 334 struct GNUNET_MESH_TransmitHandle *th =
322 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, 335 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
323 GNUNET_YES, 336 GNUNET_YES,
324 42, 337 42,
@@ -328,22 +341,25 @@ mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
328 mesh_send, 341 mesh_send,
329 cls); 342 cls);
330 343
331 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th); 344 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
332 } 345 }
333 else 346 else
334 { 347 {
335 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(cls_->tunnel); 348 struct tunnel_notify_queue *head =
336 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(cls_->tunnel); 349 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
337 350 struct tunnel_notify_queue *tail =
338 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue)); 351 GNUNET_MESH_tunnel_get_tail (cls_->tunnel);
339 element->cls = cls; 352
340 element->len = cls_->hdr.size; 353 struct tunnel_notify_queue *element =
341 element->cb = mesh_send; 354 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
342 355 element->cls = cls;
343 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element); 356 element->len = cls_->hdr.size;
344 GNUNET_MESH_tunnel_set_head(cls_->tunnel, head); 357 element->cb = mesh_send;
345 GNUNET_MESH_tunnel_set_tail(cls_->tunnel, tail); 358
346 } 359 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
360 GNUNET_MESH_tunnel_set_head (cls_->tunnel, head);
361 GNUNET_MESH_tunnel_set_tail (cls_->tunnel, tail);
362 }
347} 363}
348 364
349 365
@@ -353,54 +369,58 @@ send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 369 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
354 return; 370 return;
355 371
356 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls; 372 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
357 373
358 cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle, 374 cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
359 GNUNET_TIME_UNIT_HOURS, 375 GNUNET_TIME_UNIT_HOURS,
360 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER, 376 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
361 mesh_connect, 377 mesh_connect,
362 NULL, 378 NULL, cls_);
363 cls_);
364 379
365 remote_pending[cls_->dns.s.id] = cls_; 380 remote_pending[cls_->dns.s.id] = cls_;
366} 381}
367 382
368static int 383static int
369receive_mesh_query (void *cls __attribute__((unused)), 384receive_mesh_query (void *cls __attribute__ ((unused)),
370 struct GNUNET_MESH_Tunnel *tunnel, 385 struct GNUNET_MESH_Tunnel *tunnel,
371 void **ctx __attribute__((unused)), 386 void **ctx __attribute__ ((unused)),
372 const struct GNUNET_PeerIdentity *sender __attribute__((unused)), 387 const struct GNUNET_PeerIdentity *sender
388 __attribute__ ((unused)),
373 const struct GNUNET_MessageHeader *message, 389 const struct GNUNET_MessageHeader *message,
374 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 390 const struct GNUNET_TRANSPORT_ATS_Information *atsi
391 __attribute__ ((unused)))
375{ 392{
376 struct dns_pkt *dns = (struct dns_pkt*)(message + 1); 393 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
377 394
378 struct sockaddr_in dest; 395 struct sockaddr_in dest;
379 memset(&dest, 0, sizeof dest); 396
380 dest.sin_port = htons(53); 397 memset (&dest, 0, sizeof dest);
398 dest.sin_port = htons (53);
381 /* TODO: read from config */ 399 /* TODO: read from config */
382 inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr); 400 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
383 401
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n", ntohs(dns->s.id)); 402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
403 ntohs (dns->s.id));
385 query_states[dns->s.id].tunnel = tunnel; 404 query_states[dns->s.id].tunnel = tunnel;
386 query_states[dns->s.id].valid = GNUNET_YES; 405 query_states[dns->s.id].valid = GNUNET_YES;
387 406
388 GNUNET_NETWORK_socket_sendto(dnsout, 407 GNUNET_NETWORK_socket_sendto (dnsout,
389 dns, 408 dns,
390 ntohs(message->size) - sizeof(struct GNUNET_MessageHeader), 409 ntohs (message->size) -
391 (struct sockaddr*) &dest, 410 sizeof (struct GNUNET_MessageHeader),
392 sizeof dest); 411 (struct sockaddr *) &dest, sizeof dest);
393 412
394 return GNUNET_SYSERR; 413 return GNUNET_SYSERR;
395} 414}
396 415
397static int 416static int
398receive_mesh_answer (void *cls __attribute__((unused)), 417receive_mesh_answer (void *cls __attribute__ ((unused)),
399 struct GNUNET_MESH_Tunnel *tunnel, 418 struct GNUNET_MESH_Tunnel *tunnel,
400 void **ctx __attribute__((unused)), 419 void **ctx __attribute__ ((unused)),
401 const struct GNUNET_PeerIdentity *sender, 420 const struct GNUNET_PeerIdentity *sender,
402 const struct GNUNET_MessageHeader *message, 421 const struct GNUNET_MessageHeader *message,
403 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) 422 const struct GNUNET_TRANSPORT_ATS_Information *atsi
423 __attribute__ ((unused)))
404{ 424{
405 /* TODo: size check */ 425 /* TODo: size check */
406 struct dns_pkt *dns = (struct dns_pkt *) (message + 1); 426 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
@@ -410,37 +430,42 @@ receive_mesh_answer (void *cls __attribute__((unused)),
410 || remote_pending[dns->s.id]->tunnel != tunnel) 430 || remote_pending[dns->s.id]->tunnel != tunnel)
411 return GNUNET_OK; 431 return GNUNET_OK;
412 432
413 GNUNET_free(remote_pending[dns->s.id]); 433 GNUNET_free (remote_pending[dns->s.id]);
414 remote_pending[dns->s.id] = NULL; 434 remote_pending[dns->s.id] = NULL;
415 435
416 if (query_states[dns->s.id].valid != GNUNET_YES) 436 if (query_states[dns->s.id].valid != GNUNET_YES)
417 return GNUNET_SYSERR; 437 return GNUNET_SYSERR;
418 query_states[dns->s.id].valid = GNUNET_NO; 438 query_states[dns->s.id].valid = GNUNET_NO;
419 439
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id)); 440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
442 ntohs (dns->s.id));
421 443
422 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */ 444 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
423 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ 445 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
424 446
425 struct answer_packet_list *answer = 447 struct answer_packet_list *answer =
426 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); 448 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
427 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *)); 449 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
428 450
429 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 451 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
430 answer->pkt.hdr.size = htons (len); 452 answer->pkt.hdr.size = htons (len);
431 453
432 struct dns_pkt_parsed* pdns = parse_dns_packet(dns); 454 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
433 455
434 if (ntohs(pdns->s.ancount) < 1) 456 if (ntohs (pdns->s.ancount) < 1)
435 { 457 {
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount)); 458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n",
437 free_parsed_dns_packet(pdns); 459 ntohs (pdns->s.ancount));
438 return GNUNET_OK; 460 free_parsed_dns_packet (pdns);
439 } 461 return GNUNET_OK;
462 }
440 463
441 answer->pkt.addrsize = ntohs(pdns->answers[0]->data_len); 464 answer->pkt.addrsize = ntohs (pdns->answers[0]->data_len);
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n", answer->pkt.addrsize); 465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n",
443 memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len)); 466 answer->pkt.addrsize);
467 memcpy (answer->pkt.addr, pdns->answers[0]->data,
468 ntohs (pdns->answers[0]->data_len));
444 469
445 answer->pkt.from = query_states[dns->s.id].remote_ip; 470 answer->pkt.from = query_states[dns->s.id].remote_ip;
446 471
@@ -462,32 +487,32 @@ receive_mesh_answer (void *cls __attribute__((unused)),
462 query_states[dns->s.id].name = NULL; 487 query_states[dns->s.id].name = NULL;
463 488
464 struct dns_query_line *dque = 489 struct dns_query_line *dque =
465 (struct dns_query_line *) (dpkt->data + 490 (struct dns_query_line *) (dpkt->data +
466 (query_states[dns->s.id].namelen)); 491 (query_states[dns->s.id].namelen));
467 492
468 struct dns_record_line *drec_data = 493 struct dns_record_line *drec_data =
469 (struct dns_record_line *) (dpkt->data + 494 (struct dns_record_line *) (dpkt->data +
470 (query_states[dns->s.id].namelen) + 495 (query_states[dns->s.id].namelen) +
471 sizeof (struct dns_query_line) + 2); 496 sizeof (struct dns_query_line) + 2);
472 if (16 == answer->pkt.addrsize) 497 if (16 == answer->pkt.addrsize)
473 { 498 {
474 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA; 499 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
475 dque->type = htons (28); /* AAAA */ 500 dque->type = htons (28); /* AAAA */
476 drec_data->type = htons (28); /* AAAA */ 501 drec_data->type = htons (28); /* AAAA */
477 drec_data->data_len = htons (16); 502 drec_data->data_len = htons (16);
478 } 503 }
479 else 504 else
480 { 505 {
481 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A; 506 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
482 dque->type = htons (1); /* A */ 507 dque->type = htons (1); /* A */
483 drec_data->type = htons (1); /* A*/ 508 drec_data->type = htons (1); /* A */
484 drec_data->data_len = htons (4); 509 drec_data->data_len = htons (4);
485 } 510 }
486 dque->class = htons (1); /* IN */ 511 dque->class = htons (1); /* IN */
487 512
488 char *anname = 513 char *anname =
489 (char *) (dpkt->data + (query_states[dns->s.id].namelen) + 514 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
490 sizeof (struct dns_query_line)); 515 sizeof (struct dns_query_line));
491 memcpy (anname, "\xc0\x0c", 2); 516 memcpy (anname, "\xc0\x0c", 2);
492 drec_data->class = htons (1); /* IN */ 517 drec_data->class = htons (1); /* IN */
493 518
@@ -496,8 +521,8 @@ receive_mesh_answer (void *cls __attribute__((unused)),
496 /* Calculate at which offset in the packet the IPv6-Address belongs, it is 521 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
497 * filled in by the daemon-vpn */ 522 * filled in by the daemon-vpn */
498 answer->pkt.addroffset = 523 answer->pkt.addroffset =
499 htons ((unsigned short) ((unsigned long) (&drec_data->data) - 524 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
500 (unsigned long) (&answer->pkt))); 525 (unsigned long) (&answer->pkt)));
501 526
502 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); 527 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
503 528
@@ -507,378 +532,408 @@ receive_mesh_answer (void *cls __attribute__((unused)),
507 &send_answer, 532 &send_answer,
508 query_states[dns->s.id].client); 533 query_states[dns->s.id].client);
509 534
510 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer of length %d on to client, addroffset = %d\n", len, answer->pkt.addroffset); 535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Sent answer of length %d on to client, addroffset = %d\n", len,
537 answer->pkt.addroffset);
511 538
512 free_parsed_dns_packet(pdns); 539 free_parsed_dns_packet (pdns);
513 return GNUNET_OK; 540 return GNUNET_OK;
514} 541}
515 542
516 543
517static void 544static void
518send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { 545send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 546{
520 return; 547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
521 548 return;
522 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
523 549
524 unsigned short id = pdns->s.id; 550 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
525 551
526 free_parsed_dns_packet(pdns); 552 unsigned short id = pdns->s.id;
527 553
528 if (query_states[id].valid != GNUNET_YES) return; 554 free_parsed_dns_packet (pdns);
529 query_states[id].valid = GNUNET_NO;
530 555
531 GNUNET_assert(query_states[id].namelen == 74); 556 if (query_states[id].valid != GNUNET_YES)
557 return;
558 query_states[id].valid = GNUNET_NO;
532 559
533 size_t len = sizeof(struct answer_packet) - 1 \ 560 GNUNET_assert (query_states[id].namelen == 74);
534 + sizeof(struct dns_static) \
535 + 74 /* this is the length of a reverse ipv6-lookup */ \
536 + sizeof(struct dns_query_line) \
537 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
538 + sizeof(struct dns_record_line) - 1 \
539 - 2 /* We do not know the lenght of the answer yet*/;
540 561
541 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*)); 562 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */
542 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*)); 563 + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
564 + sizeof (struct dns_record_line) - 1
565 - 2 /* We do not know the lenght of the answer yet */ ;
543 566
544 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 567 struct answer_packet_list *answer =
545 answer->pkt.hdr.size = htons(len); 568 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
546 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV; 569 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
547 570
548 answer->pkt.from = query_states[id].remote_ip; 571 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
572 answer->pkt.hdr.size = htons (len);
573 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
549 574
550 answer->pkt.to = query_states[id].local_ip; 575 answer->pkt.from = query_states[id].remote_ip;
551 answer->pkt.dst_port = query_states[id].local_port;
552 576
553 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data; 577 answer->pkt.to = query_states[id].local_ip;
578 answer->pkt.dst_port = query_states[id].local_port;
554 579
555 dpkt->s.id = id; 580 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
556 dpkt->s.aa = 1;
557 dpkt->s.qr = 1;
558 dpkt->s.ra = 1;
559 dpkt->s.qdcount = htons(1);
560 dpkt->s.ancount = htons(1);
561 581
562 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen); 582 dpkt->s.id = id;
563 GNUNET_free(query_states[id].name); 583 dpkt->s.aa = 1;
584 dpkt->s.qr = 1;
585 dpkt->s.ra = 1;
586 dpkt->s.qdcount = htons (1);
587 dpkt->s.ancount = htons (1);
564 588
565 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen)); 589 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
566 dque->type = htons(12); /* PTR */ 590 GNUNET_free (query_states[id].name);
567 dque->class = htons(1); /* IN */
568 591
569 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)); 592 struct dns_query_line *dque =
570 memcpy(anname, "\xc0\x0c", 2); 593 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
594 dque->type = htons (12); /* PTR */
595 dque->class = htons (1); /* IN */
571 596
572 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2); 597 char *anname =
573 drec_data->type = htons(12); /* AAAA */ 598 (char *) (dpkt->data + (query_states[id].namelen) +
574 drec_data->class = htons(1); /* IN */ 599 sizeof (struct dns_query_line));
575 /* FIXME: read the TTL from block: 600 memcpy (anname, "\xc0\x0c", 2);
576 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
577 *
578 * But how to get the seconds out of this?
579 */
580 drec_data->ttl = htonl(3600);
581 601
582 /* Calculate at which offset in the packet the length of the name and the 602 struct dns_record_line *drec_data =
583 * name, it is filled in by the daemon-vpn */ 603 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
584 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt))); 604 sizeof (struct dns_query_line) + 2);
605 drec_data->type = htons (12); /* AAAA */
606 drec_data->class = htons (1); /* IN */
607 /* FIXME: read the TTL from block:
608 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
609 *
610 * But how to get the seconds out of this?
611 */
612 drec_data->ttl = htonl (3600);
613
614 /* Calculate at which offset in the packet the length of the name and the
615 * name, it is filled in by the daemon-vpn */
616 answer->pkt.addroffset =
617 htons ((unsigned short) ((unsigned long) (&drec_data->data_len) -
618 (unsigned long) (&answer->pkt)));
585 619
586 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer); 620 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
587 621
588 GNUNET_SERVER_notify_transmit_ready(query_states[id].client, 622 GNUNET_SERVER_notify_transmit_ready (query_states[id].client,
589 len, 623 len,
590 GNUNET_TIME_UNIT_FOREVER_REL, 624 GNUNET_TIME_UNIT_FOREVER_REL,
591 &send_answer, 625 &send_answer, query_states[id].client);
592 query_states[id].client);
593} 626}
594 627
595/** 628/**
596 * Receive a block from the dht. 629 * Receive a block from the dht.
597 */ 630 */
598static void 631static void
599receive_dht(void *cls, 632receive_dht (void *cls,
600 struct GNUNET_TIME_Absolute exp __attribute__((unused)), 633 struct GNUNET_TIME_Absolute exp __attribute__ ((unused)),
601 const GNUNET_HashCode *key __attribute__((unused)), 634 const GNUNET_HashCode * key __attribute__ ((unused)),
602 const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)), 635 const struct GNUNET_PeerIdentity *const *get_path
603 const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)), 636 __attribute__ ((unused)),
604 enum GNUNET_BLOCK_Type type, 637 const struct GNUNET_PeerIdentity *const *put_path
605 size_t size, 638 __attribute__ ((unused)), enum GNUNET_BLOCK_Type type, size_t size,
606 const void *data) { 639 const void *data)
607 640{
608 unsigned short id = ((struct receive_dht_cls*)cls)->id; 641
609 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle; 642 unsigned short id = ((struct receive_dht_cls *) cls)->id;
610 GNUNET_free(cls); 643 struct GNUNET_DHT_GetHandle *handle =
611 644 ((struct receive_dht_cls *) cls)->handle;
612 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS); 645 GNUNET_free (cls);
613 646
614 /* If no query with this id is pending, ignore the block */ 647 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
615 if (query_states[id].valid != GNUNET_YES) return; 648
616 query_states[id].valid = GNUNET_NO; 649 /* If no query with this id is pending, ignore the block */
617 650 if (query_states[id].valid != GNUNET_YES)
618 const struct GNUNET_DNS_Record* rec = data; 651 return;
619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 652 query_states[id].valid = GNUNET_NO;
620 "Got block of size %d, peer: %08x, desc: %08x\n", 653
621 size, 654 const struct GNUNET_DNS_Record *rec = data;
622 *((unsigned int*)&rec->peer), 655
623 *((unsigned int*)&rec->service_descriptor)); 656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 657 "Got block of size %d, peer: %08x, desc: %08x\n",
625 size_t len = sizeof(struct answer_packet) - 1 \ 658 size,
626 + sizeof(struct dns_static) \ 659 *((unsigned int *) &rec->peer),
627 + query_states[id].namelen \ 660 *((unsigned int *) &rec->service_descriptor));
628 + sizeof(struct dns_query_line) \ 661
629 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \ 662 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
630 + sizeof(struct dns_record_line) - 1 \ 663 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
631 + 16; /* To hold the IPv6-Address */
632
633 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
634 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
635
636 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
637 answer->pkt.hdr.size = htons(len);
638 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
639
640 GNUNET_CRYPTO_hash(&rec->peer,
641 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
642 &answer->pkt.service_descr.peer);
643
644 memcpy(&answer->pkt.service_descr.service_descriptor,
645 &rec->service_descriptor,
646 sizeof(GNUNET_HashCode));
647 memcpy(&answer->pkt.service_descr.service_type,
648 &rec->service_type,
649 sizeof(answer->pkt.service_descr.service_type));
650 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
651
652 answer->pkt.from = query_states[id].remote_ip;
653
654 answer->pkt.to = query_states[id].local_ip;
655 answer->pkt.dst_port = query_states[id].local_port;
656
657 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
658
659 dpkt->s.id = id;
660 dpkt->s.aa = 1;
661 dpkt->s.qr = 1;
662 dpkt->s.ra = 1;
663 dpkt->s.qdcount = htons(1);
664 dpkt->s.ancount = htons(1);
665
666 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
667 GNUNET_free(query_states[id].name);
668
669 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
670 dque->type = htons(28); /* AAAA */
671 dque->class = htons(1); /* IN */
672
673 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
674 memcpy(anname, "\xc0\x0c", 2);
675
676 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
677 drec_data->type = htons(28); /* AAAA */
678 drec_data->class = htons(1); /* IN */
679
680 /* FIXME: read the TTL from block:
681 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
682 *
683 * But how to get the seconds out of this?
684 */
685 drec_data->ttl = htonl(3600);
686 drec_data->data_len = htons(16);
687 664
688 /* Calculate at which offset in the packet the IPv6-Address belongs, it is 665 struct answer_packet_list *answer =
689 * filled in by the daemon-vpn */ 666 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
690 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt))); 667 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
668
669 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
670 answer->pkt.hdr.size = htons (len);
671 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
672
673 GNUNET_CRYPTO_hash (&rec->peer,
674 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
675 &answer->pkt.service_descr.peer);
676
677 memcpy (&answer->pkt.service_descr.service_descriptor,
678 &rec->service_descriptor, sizeof (GNUNET_HashCode));
679 memcpy (&answer->pkt.service_descr.service_type,
680 &rec->service_type, sizeof (answer->pkt.service_descr.service_type));
681 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
682 sizeof (answer->pkt.service_descr.ports));
683
684 answer->pkt.from = query_states[id].remote_ip;
685
686 answer->pkt.to = query_states[id].local_ip;
687 answer->pkt.dst_port = query_states[id].local_port;
688
689 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
690
691 dpkt->s.id = id;
692 dpkt->s.aa = 1;
693 dpkt->s.qr = 1;
694 dpkt->s.ra = 1;
695 dpkt->s.qdcount = htons (1);
696 dpkt->s.ancount = htons (1);
691 697
692 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer); 698 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
699 GNUNET_free (query_states[id].name);
693 700
694 GNUNET_SERVER_notify_transmit_ready(query_states[id].client, 701 struct dns_query_line *dque =
695 len, 702 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
696 GNUNET_TIME_UNIT_FOREVER_REL, 703 dque->type = htons (28); /* AAAA */
697 &send_answer, 704 dque->class = htons (1); /* IN */
698 query_states[id].client); 705
706 char *anname =
707 (char *) (dpkt->data + (query_states[id].namelen) +
708 sizeof (struct dns_query_line));
709 memcpy (anname, "\xc0\x0c", 2);
710
711 struct dns_record_line *drec_data =
712 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
713 sizeof (struct dns_query_line) + 2);
714 drec_data->type = htons (28); /* AAAA */
715 drec_data->class = htons (1); /* IN */
716
717 /* FIXME: read the TTL from block:
718 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
719 *
720 * But how to get the seconds out of this?
721 */
722 drec_data->ttl = htonl (3600);
723 drec_data->data_len = htons (16);
724
725 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
726 * filled in by the daemon-vpn */
727 answer->pkt.addroffset =
728 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
729 (unsigned long) (&answer->pkt)));
730
731 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
732
733 GNUNET_SERVER_notify_transmit_ready (query_states[id].client,
734 len,
735 GNUNET_TIME_UNIT_FOREVER_REL,
736 &send_answer, query_states[id].client);
699 737
700 GNUNET_DHT_get_stop(handle); 738 GNUNET_DHT_get_stop (handle);
701} 739}
702 740
703/** 741/**
704 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS 742 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
705 */ 743 */
706static void 744static void
707rehijack(void *cls __attribute__((unused)), 745rehijack (void *cls __attribute__ ((unused)),
708 struct GNUNET_SERVER_Client *client, 746 struct GNUNET_SERVER_Client *client,
709 const struct GNUNET_MessageHeader *message __attribute__((unused))) { 747 const struct GNUNET_MessageHeader *message __attribute__ ((unused)))
710 unhijack(dnsoutport); 748{
711 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL); 749 unhijack (dnsoutport);
750 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
712 751
713 GNUNET_SERVER_receive_done(client, GNUNET_OK); 752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
714} 753}
715 754
716/** 755/**
717 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket 756 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
718 */ 757 */
719static void 758static void
720receive_query(void *cls __attribute__((unused)), 759receive_query (void *cls __attribute__ ((unused)),
721 struct GNUNET_SERVER_Client *client, 760 struct GNUNET_SERVER_Client *client,
722 const struct GNUNET_MessageHeader *message) { 761 const struct GNUNET_MessageHeader *message)
723 struct query_packet* pkt = (struct query_packet*)message; 762{
724 struct dns_pkt* dns = (struct dns_pkt*)pkt->data; 763 struct query_packet *pkt = (struct query_packet *) message;
725 struct dns_pkt_parsed* pdns = parse_dns_packet(dns); 764 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
726 765 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
727 query_states[dns->s.id].valid = GNUNET_YES;
728 query_states[dns->s.id].client = client;
729 query_states[dns->s.id].local_ip = pkt->orig_from;
730 query_states[dns->s.id].local_port = pkt->src_port;
731 query_states[dns->s.id].remote_ip = pkt->orig_to;
732 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
733 if (query_states[dns->s.id].name != NULL)
734 GNUNET_free(query_states[dns->s.id].name);
735 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
736 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
737
738 /* The query is for a .gnunet-address */
739 if (pdns->queries[0]->namelen > 9 &&
740 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
741 {
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
743 GNUNET_HashCode key;
744 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
745
746 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
747 "Getting with key %08x, len is %d\n",
748 *((unsigned int*)&key),
749 pdns->queries[0]->namelen);
750
751 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
752 cls->id = dns->s.id;
753
754 cls->handle = GNUNET_DHT_get_start(dht,
755 GNUNET_TIME_UNIT_MINUTES,
756 GNUNET_BLOCK_TYPE_DNS,
757 &key,
758 DEFAULT_GET_REPLICATION,
759 GNUNET_DHT_RO_NONE,
760 NULL,
761 0,
762 NULL,
763 0,
764 receive_dht,
765 cls);
766
767 goto outfree;
768 }
769 766
770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen); 767 query_states[dns->s.id].valid = GNUNET_YES;
768 query_states[dns->s.id].client = client;
769 query_states[dns->s.id].local_ip = pkt->orig_from;
770 query_states[dns->s.id].local_port = pkt->src_port;
771 query_states[dns->s.id].remote_ip = pkt->orig_to;
772 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
773 if (query_states[dns->s.id].name != NULL)
774 GNUNET_free (query_states[dns->s.id].name);
775 query_states[dns->s.id].name =
776 GNUNET_malloc (query_states[dns->s.id].namelen);
777 memcpy (query_states[dns->s.id].name, dns->data,
778 query_states[dns->s.id].namelen);
771 779
772 /* This is a PTR-Query. Check if it is for "our" network */ 780 /* The query is for a .gnunet-address */
773 if (htons(pdns->queries[0]->qtype) == 12 && 781 if (pdns->queries[0]->namelen > 9 &&
774 74 == pdns->queries[0]->namelen) 782 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
775 { 783 ".gnunet.", 9))
776 char* ipv6addr; 784 {
777 char ipv6[16]; 785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
778 char ipv6rev[74] = "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa."; 786 GNUNET_HashCode key;
779 unsigned int i;
780 unsigned long long ipv6prefix;
781 unsigned int comparelen;
782
783 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
784 inet_pton (AF_INET6, ipv6addr, ipv6);
785 GNUNET_free(ipv6addr);
786
787 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
788 GNUNET_assert(ipv6prefix < 127);
789 ipv6prefix = (ipv6prefix + 7)/8;
790
791 for (i = ipv6prefix; i < 16; i++)
792 ipv6[i] = 0;
793
794 for (i = 0; i < 16; i++)
795 {
796 unsigned char c1 = ipv6[i] >> 4;
797 unsigned char c2 = ipv6[i] & 0xf;
798
799 if (c1 <= 9)
800 ipv6rev[62-(4*i)] = c1 + '0';
801 else
802 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
803
804 if (c2 <= 9)
805 ipv6rev[62-((4*i)+2)] = c2 + '0';
806 else
807 ipv6rev[62-((4*i)+2)] = c2 + 87;
808 }
809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
810 comparelen = 10 + 4*ipv6prefix;
811 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
812 ipv6rev + (74 - comparelen),
813 comparelen))
814 {
815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
816
817 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
818
819 goto out;
820 }
821 }
822 787
823 char* virt_dns; 788 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
824 unsigned int virt_dns_bytes; 789 &key);
825 if (GNUNET_SYSERR ==
826 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
827 &virt_dns))
828 {
829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
830 "No entry 'VIRTDNS' in configuration!\n");
831 exit (1);
832 }
833 790
834 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes)) 791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 { 792 "Getting with key %08x, len is %d\n",
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 793 *((unsigned int *) &key), pdns->queries[0]->namelen);
837 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
838 exit(1);
839 }
840 794
841 GNUNET_free(virt_dns); 795 struct receive_dht_cls *cls =
796 GNUNET_malloc (sizeof (struct receive_dht_cls));
797 cls->id = dns->s.id;
842 798
843 if (virt_dns_bytes == pkt->orig_to) 799 cls->handle = GNUNET_DHT_get_start (dht,
844 { 800 GNUNET_TIME_UNIT_MINUTES,
845 /* This is a packet that was sent directly to the virtual dns-server 801 GNUNET_BLOCK_TYPE_DNS,
846 * 802 &key,
847 * This means we have to send this query over gnunet 803 DEFAULT_GET_REPLICATION,
848 */ 804 GNUNET_DHT_RO_NONE,
805 NULL, 0, NULL, 0, receive_dht, cls);
849 806
850 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1); 807 goto outfree;
851 struct tunnel_cls *cls_ = GNUNET_malloc(size); 808 }
852 cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
853 809
854 cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS); 810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
855 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size); 811 pdns->queries[0]->name, pdns->queries[0]->namelen);
856 812
857 memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader)); 813 /* This is a PTR-Query. Check if it is for "our" network */
858 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_); 814 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
815 {
816 char *ipv6addr;
817 char ipv6[16];
818 char ipv6rev[74] =
819 "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa.";
820 unsigned int i;
821 unsigned long long ipv6prefix;
822 unsigned int comparelen;
823
824 GNUNET_assert (GNUNET_OK ==
825 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
826 "IPV6ADDR",
827 &ipv6addr));
828 inet_pton (AF_INET6, ipv6addr, ipv6);
829 GNUNET_free (ipv6addr);
830
831 GNUNET_assert (GNUNET_OK ==
832 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
833 "IPV6PREFIX",
834 &ipv6prefix));
835 GNUNET_assert (ipv6prefix < 127);
836 ipv6prefix = (ipv6prefix + 7) / 8;
837
838 for (i = ipv6prefix; i < 16; i++)
839 ipv6[i] = 0;
840
841 for (i = 0; i < 16; i++)
842 {
843 unsigned char c1 = ipv6[i] >> 4;
844 unsigned char c2 = ipv6[i] & 0xf;
845
846 if (c1 <= 9)
847 ipv6rev[62 - (4 * i)] = c1 + '0';
848 else
849 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
850
851 if (c2 <= 9)
852 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
853 else
854 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
855 }
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
857 comparelen = 10 + 4 * ipv6prefix;
858 if (0 ==
859 strncmp (pdns->queries[0]->name +
860 (pdns->queries[0]->namelen - comparelen),
861 ipv6rev + (74 - comparelen), comparelen))
862 {
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
859 864
860 goto outfree; 865 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
861 }
862 866
867 goto out;
868 }
869 }
863 870
864 /* The query should be sent to the network */ 871 char *virt_dns;
872 unsigned int virt_dns_bytes;
865 873
866 struct sockaddr_in dest; 874 if (GNUNET_SYSERR ==
867 memset(&dest, 0, sizeof dest); 875 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
868 dest.sin_port = htons(53); 876 {
869 dest.sin_addr.s_addr = pkt->orig_to; 877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878 "No entry 'VIRTDNS' in configuration!\n");
879 exit (1);
880 }
870 881
871 GNUNET_NETWORK_socket_sendto(dnsout, 882 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
872 dns, 883 {
873 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, 884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
874 (struct sockaddr*) &dest, 885 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
875 sizeof dest); 886 exit (1);
887 }
888
889 GNUNET_free (virt_dns);
890
891 if (virt_dns_bytes == pkt->orig_to)
892 {
893 /* This is a packet that was sent directly to the virtual dns-server
894 *
895 * This means we have to send this query over gnunet
896 */
897
898 size_t size =
899 sizeof (struct GNUNET_MESH_Tunnel *) +
900 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
901 sizeof (struct query_packet) +
902 1);
903 struct tunnel_cls *cls_ = GNUNET_malloc (size);
904
905 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
906
907 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
909
910 memcpy (&cls_->dns, dns,
911 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
912 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
913
914 goto outfree;
915 }
916
917
918 /* The query should be sent to the network */
919
920 struct sockaddr_in dest;
921
922 memset (&dest, 0, sizeof dest);
923 dest.sin_port = htons (53);
924 dest.sin_addr.s_addr = pkt->orig_to;
925
926 GNUNET_NETWORK_socket_sendto (dnsout,
927 dns,
928 ntohs (pkt->hdr.size) -
929 sizeof (struct query_packet) + 1,
930 (struct sockaddr *) &dest, sizeof dest);
876 931
877outfree: 932outfree:
878 free_parsed_dns_packet(pdns); 933 free_parsed_dns_packet (pdns);
879 pdns = NULL; 934 pdns = NULL;
880out: 935out:
881 GNUNET_SERVER_receive_done(client, GNUNET_OK); 936 GNUNET_SERVER_receive_done (client, GNUNET_OK);
882} 937}
883 938
884static void read_response (void *cls, 939static void read_response (void *cls,
@@ -900,14 +955,14 @@ open_port ()
900 sizeof (struct sockaddr_in)); 955 sizeof (struct sockaddr_in));
901 956
902 if (err != GNUNET_OK) 957 if (err != GNUNET_OK)
903 { 958 {
904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
905 "Could not bind a port: %m\n"); 960 return GNUNET_SYSERR;
906 return GNUNET_SYSERR; 961 }
907 }
908 962
909 /* Read the port we bound to */ 963 /* Read the port we bound to */
910 socklen_t addrlen = sizeof (struct sockaddr_in); 964 socklen_t addrlen = sizeof (struct sockaddr_in);
965
911 err = getsockname (GNUNET_NETWORK_get_fd (dnsout), 966 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
912 (struct sockaddr *) &addr, &addrlen); 967 (struct sockaddr *) &addr, &addrlen);
913 968
@@ -941,12 +996,12 @@ read_response (void *cls
941 996
942#ifndef MINGW 997#ifndef MINGW
943 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len)) 998 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
944 { 999 {
945 unhijack (dnsoutport); 1000 unhijack (dnsoutport);
946 if (GNUNET_YES == open_port ()) 1001 if (GNUNET_YES == open_port ())
947 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL); 1002 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
948 return; 1003 return;
949 } 1004 }
950#else 1005#else
951 /* port the code above? */ 1006 /* port the code above? */
952 len = 65536; 1007 len = 65536;
@@ -961,83 +1016,90 @@ read_response (void *cls
961 (struct sockaddr *) &addr, &addrlen); 1016 (struct sockaddr *) &addr, &addrlen);
962 1017
963 if (r < 0) 1018 if (r < 0)
964 { 1019 {
965 unhijack (dnsoutport); 1020 unhijack (dnsoutport);
966 if (GNUNET_YES == open_port ()) 1021 if (GNUNET_YES == open_port ())
967 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL); 1022 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
968 return; 1023 return;
969 } 1024 }
970 1025
971 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n", ntohs(dns->s.id)); 1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1027 ntohs (dns->s.id));
972 1028
973 if (query_states[dns->s.id].valid == GNUNET_YES) 1029 if (query_states[dns->s.id].valid == GNUNET_YES)
1030 {
1031 if (query_states[dns->s.id].tunnel != NULL)
974 { 1032 {
975 if (query_states[dns->s.id].tunnel != NULL) 1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 { 1034 "Answer to query %d for a remote peer!\n",
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d for a remote peer!\n", ntohs(dns->s.id)); 1035 ntohs (dns->s.id));
978 /* This response should go through a tunnel */ 1036 /* This response should go through a tunnel */
979 uint32_t *c = GNUNET_malloc (4 + sizeof(struct GNUNET_MESH_Tunnel*) + r); 1037 uint32_t *c =
980 *c = r; 1038 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
981 struct GNUNET_MESH_Tunnel** t = (struct GNUNET_MESH_Tunnel**)(c + 1); 1039 *c = r;
982 *t = query_states[dns->s.id].tunnel; 1040 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
983 memcpy (t + 1, dns, r); 1041
984 if (NULL == 1042 *t = query_states[dns->s.id].tunnel;
985 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel)) 1043 memcpy (t + 1, dns, r);
986 { 1044 if (NULL ==
987 struct GNUNET_MESH_TransmitHandle *th = 1045 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel))
988 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel, 1046 {
989 GNUNET_YES, 1047 struct GNUNET_MESH_TransmitHandle *th =
990 32, 1048 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
991 GNUNET_TIME_UNIT_MINUTES, 1049 GNUNET_YES,
992 NULL, 1050 32,
993 r + 1051 GNUNET_TIME_UNIT_MINUTES,
994 sizeof (struct 1052 NULL,
995 GNUNET_MessageHeader), 1053 r +
996 mesh_send_response, c); 1054 sizeof (struct
997 GNUNET_MESH_tunnel_set_data (query_states[dns->s.id].tunnel, 1055 GNUNET_MessageHeader),
998 th); 1056 mesh_send_response, c);
999 } 1057
1000 else 1058 GNUNET_MESH_tunnel_set_data (query_states[dns->s.id].tunnel, th);
1001 { 1059 }
1002 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(query_states[dns->s.id].tunnel);
1003 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(query_states[dns->s.id].tunnel);
1004
1005 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
1006 element->cls = c;
1007 element->len = r+sizeof(struct GNUNET_MessageHeader);
1008 element->cb = mesh_send_response;
1009
1010 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
1011 GNUNET_MESH_tunnel_set_head(query_states[dns->s.id].tunnel, head);
1012 GNUNET_MESH_tunnel_set_tail(query_states[dns->s.id].tunnel, tail);
1013 }
1014 }
1015 else 1060 else
1016 { 1061 {
1017 query_states[dns->s.id].valid = GNUNET_NO; 1062 struct tunnel_notify_queue *head =
1018 1063 GNUNET_MESH_tunnel_get_head (query_states[dns->s.id].tunnel);
1019 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */ 1064 struct tunnel_notify_queue *tail =
1020 struct answer_packet_list *answer = 1065 GNUNET_MESH_tunnel_get_tail (query_states[dns->s.id].tunnel);
1021 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); 1066
1022 answer->pkt.hdr.type = 1067 struct tunnel_notify_queue *element =
1023 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 1068 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1024 answer->pkt.hdr.size = htons (len); 1069 element->cls = c;
1025 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; 1070 element->len = r + sizeof (struct GNUNET_MessageHeader);
1026 answer->pkt.from = addr.sin_addr.s_addr; 1071 element->cb = mesh_send_response;
1027 answer->pkt.to = query_states[dns->s.id].local_ip; 1072
1028 answer->pkt.dst_port = query_states[dns->s.id].local_port; 1073 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
1029 memcpy (answer->pkt.data, buf, r); 1074 GNUNET_MESH_tunnel_set_head (query_states[dns->s.id].tunnel, head);
1030 1075 GNUNET_MESH_tunnel_set_tail (query_states[dns->s.id].tunnel, tail);
1031 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); 1076 }
1032 1077 }
1033 GNUNET_SERVER_notify_transmit_ready (query_states 1078 else
1034 [dns->s.id].client, len, 1079 {
1035 GNUNET_TIME_UNIT_FOREVER_REL, 1080 query_states[dns->s.id].valid = GNUNET_NO;
1036 &send_answer, 1081
1037 query_states[dns->s. 1082 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1038 id].client); 1083 struct answer_packet_list *answer =
1039 } 1084 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
1085 answer->pkt.hdr.type =
1086 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1087 answer->pkt.hdr.size = htons (len);
1088 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1089 answer->pkt.from = addr.sin_addr.s_addr;
1090 answer->pkt.to = query_states[dns->s.id].local_ip;
1091 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1092 memcpy (answer->pkt.data, buf, r);
1093
1094 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1095
1096 GNUNET_SERVER_notify_transmit_ready (query_states
1097 [dns->s.id].client, len,
1098 GNUNET_TIME_UNIT_FOREVER_REL,
1099 &send_answer,
1100 query_states[dns->s.id].client);
1040 } 1101 }
1102 }
1041 } 1103 }
1042 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 1104 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1043 dnsout, &read_response, NULL); 1105 dnsout, &read_response, NULL);
@@ -1051,14 +1113,14 @@ read_response (void *cls
1051 * @param tc unused 1113 * @param tc unused
1052 */ 1114 */
1053static void 1115static void
1054cleanup_task (void *cls __attribute__((unused)), 1116cleanup_task (void *cls __attribute__ ((unused)),
1055 const struct GNUNET_SCHEDULER_TaskContext *tc) 1117 const struct GNUNET_SCHEDULER_TaskContext *tc)
1056{ 1118{
1057 GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)); 1119 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1058 1120
1059 unhijack(dnsoutport); 1121 unhijack (dnsoutport);
1060 GNUNET_DHT_disconnect(dht); 1122 GNUNET_DHT_disconnect (dht);
1061 GNUNET_MESH_disconnect(mesh_handle); 1123 GNUNET_MESH_disconnect (mesh_handle);
1062} 1124}
1063 1125
1064/** 1126/**
@@ -1073,70 +1135,78 @@ uint64_t
1073get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects) 1135get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1074{ 1136{
1075 uint64_t ret = 0; 1137 uint64_t ret = 0;
1076 char* cpy, *hostname, *redirect; 1138 char *cpy, *hostname, *redirect;
1077 int local_port, count = 0; 1139 int local_port, count = 0;
1078 1140
1079 if (NULL != udp_redirects) 1141 if (NULL != udp_redirects)
1142 {
1143 cpy = GNUNET_strdup (udp_redirects);
1144 for (redirect = strtok (cpy, " "); redirect != NULL;
1145 redirect = strtok (NULL, " "))
1080 { 1146 {
1081 cpy = GNUNET_strdup (udp_redirects); 1147 if (NULL == (hostname = strstr (redirect, ":")))
1082 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " ")) 1148 {
1083 { 1149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1084 if (NULL == (hostname = strstr (redirect, ":"))) 1150 "Warning: option %s is not formatted correctly!\n",
1085 { 1151 redirect);
1086 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect); 1152 continue;
1087 continue; 1153 }
1088 } 1154 hostname[0] = '\0';
1089 hostname[0] = '\0'; 1155 local_port = atoi (redirect);
1090 local_port = atoi (redirect); 1156 if (!((local_port > 0) && (local_port < 65536)))
1091 if (!((local_port > 0) && (local_port < 65536))) 1157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1092 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect); 1158 "Warning: %s is not a correct port.", redirect);
1093 1159
1094 ret |= (0xFFFF & htons(local_port)); 1160 ret |= (0xFFFF & htons (local_port));
1095 ret <<= 16; 1161 ret <<= 16;
1096 count ++; 1162 count++;
1097 1163
1098 if(count > 4) 1164 if (count > 4)
1099 { 1165 {
1100 ret = 0; 1166 ret = 0;
1101 goto out; 1167 goto out;
1102 } 1168 }
1103 }
1104 GNUNET_free(cpy);
1105 cpy = NULL;
1106 } 1169 }
1170 GNUNET_free (cpy);
1171 cpy = NULL;
1172 }
1107 1173
1108 if (NULL != tcp_redirects) 1174 if (NULL != tcp_redirects)
1175 {
1176 cpy = GNUNET_strdup (tcp_redirects);
1177 for (redirect = strtok (cpy, " "); redirect != NULL;
1178 redirect = strtok (NULL, " "))
1109 { 1179 {
1110 cpy = GNUNET_strdup (tcp_redirects); 1180 if (NULL == (hostname = strstr (redirect, ":")))
1111 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " ")) 1181 {
1112 { 1182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1113 if (NULL == (hostname = strstr (redirect, ":"))) 1183 "Warning: option %s is not formatted correctly!\n",
1114 { 1184 redirect);
1115 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect); 1185 continue;
1116 continue; 1186 }
1117 } 1187 hostname[0] = '\0';
1118 hostname[0] = '\0'; 1188 local_port = atoi (redirect);
1119 local_port = atoi (redirect); 1189 if (!((local_port > 0) && (local_port < 65536)))
1120 if (!((local_port > 0) && (local_port < 65536))) 1190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1121 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect); 1191 "Warning: %s is not a correct port.", redirect);
1122 1192
1123 ret |= (0xFFFF & htons(local_port)); 1193 ret |= (0xFFFF & htons (local_port));
1124 ret <<= 16; 1194 ret <<= 16;
1125 count ++; 1195 count++;
1126 1196
1127 if(count > 4) 1197 if (count > 4)
1128 { 1198 {
1129 ret = 0; 1199 ret = 0;
1130 goto out; 1200 goto out;
1131 } 1201 }
1132 }
1133 GNUNET_free(cpy);
1134 cpy = NULL;
1135 } 1202 }
1203 GNUNET_free (cpy);
1204 cpy = NULL;
1205 }
1136 1206
1137out: 1207out:
1138 if (NULL != cpy) 1208 if (NULL != cpy)
1139 GNUNET_free(cpy); 1209 GNUNET_free (cpy);
1140 return ret; 1210 return ret;
1141} 1211}
1142 1212
@@ -1146,10 +1216,10 @@ publish_name (const char *name, uint64_t ports, uint32_t service_type,
1146{ 1216{
1147 size_t size = sizeof (struct GNUNET_DNS_Record); 1217 size_t size = sizeof (struct GNUNET_DNS_Record);
1148 struct GNUNET_DNS_Record data; 1218 struct GNUNET_DNS_Record data;
1219
1149 memset (&data, 0, size); 1220 memset (&data, 0, size);
1150 1221
1151 data.purpose.size = 1222 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1152 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1153 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD; 1223 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1154 1224
1155 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor); 1225 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
@@ -1162,15 +1232,16 @@ publish_name (const char *name, uint64_t ports, uint32_t service_type,
1162 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer); 1232 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1163 1233
1164 data.expiration_time = 1234 data.expiration_time =
1165 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2)); 1235 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
1236 (GNUNET_TIME_UNIT_HOURS, 2));
1166 1237
1167 /* Sign the block */ 1238 /* Sign the block */
1168 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key, 1239 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1169 &data.purpose, &data.signature)) 1240 &data.purpose, &data.signature))
1170 { 1241 {
1171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n"); 1242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1172 return; 1243 return;
1173 } 1244 }
1174 1245
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Putting with key %08x, size = %d\n", 1247 "Putting with key %08x, size = %d\n",
@@ -1194,15 +1265,16 @@ publish_name (const char *name, uint64_t ports, uint32_t service_type,
1194 * @param section the current section 1265 * @param section the current section
1195 */ 1266 */
1196void 1267void
1197publish_iterate (void *cls __attribute__((unused)), const char *section) 1268publish_iterate (void *cls __attribute__ ((unused)), const char *section)
1198{ 1269{
1199 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8)))) 1270 if ((strlen (section) < 8) ||
1271 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1200 return; 1272 return;
1201 1273
1202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section); 1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1203 1275
1204 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name, 1276 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1205 *keyfile; 1277 *keyfile;
1206 1278
1207 GNUNET_CONFIGURATION_get_value_string (cfg, section, 1279 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1208 "UDP_REDIRECTS", &udp_redirects); 1280 "UDP_REDIRECTS", &udp_redirects);
@@ -1212,15 +1284,15 @@ publish_iterate (void *cls __attribute__((unused)), const char *section)
1212 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", 1284 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1213 "HOSTKEY", 1285 "HOSTKEY",
1214 &keyfile)) 1286 &keyfile))
1215 { 1287 {
1216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n"); 1288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1217 if (keyfile != NULL) 1289 if (keyfile != NULL)
1218 GNUNET_free (keyfile); 1290 GNUNET_free (keyfile);
1219 return; 1291 return;
1220 } 1292 }
1221 1293
1222 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = 1294 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1223 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile); 1295 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1224 GNUNET_free (keyfile); 1296 GNUNET_free (keyfile);
1225 GNUNET_assert (my_private_key != NULL); 1297 GNUNET_assert (my_private_key != NULL);
1226 1298
@@ -1243,17 +1315,17 @@ publish_iterate (void *cls __attribute__((unused)), const char *section)
1243 &alternative_names); 1315 &alternative_names);
1244 for (alternative_name = strtok (alternative_names, " "); 1316 for (alternative_name = strtok (alternative_names, " ");
1245 alternative_name != NULL; alternative_name = strtok (NULL, " ")) 1317 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1246 { 1318 {
1247 char *altname = 1319 char *altname =
1248 alloca (strlen (alternative_name) + strlen (section) + 1 + 1); 1320 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1249 strcpy (altname, alternative_name); 1321 strcpy (altname, alternative_name);
1250 strcpy (altname + strlen (alternative_name) + 1, section); 1322 strcpy (altname + strlen (alternative_name) + 1, section);
1251 altname[strlen (alternative_name)] = '.'; 1323 altname[strlen (alternative_name)] = '.';
1252 1324
1253 publish_name (altname, ports, service_type, my_private_key); 1325 publish_name (altname, ports, service_type, my_private_key);
1254 } 1326 }
1255 1327
1256 GNUNET_free_non_null(alternative_names); 1328 GNUNET_free_non_null (alternative_names);
1257 GNUNET_CRYPTO_rsa_key_free (my_private_key); 1329 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1258 GNUNET_free_non_null (udp_redirects); 1330 GNUNET_free_non_null (udp_redirects);
1259 GNUNET_free_non_null (tcp_redirects); 1331 GNUNET_free_non_null (tcp_redirects);
@@ -1263,16 +1335,15 @@ publish_iterate (void *cls __attribute__((unused)), const char *section)
1263 * Publish a DNS-record in the DHT. 1335 * Publish a DNS-record in the DHT.
1264 */ 1336 */
1265static void 1337static void
1266publish_names (void *cls __attribute__((unused)), 1338publish_names (void *cls __attribute__ ((unused)),
1267 const struct GNUNET_SCHEDULER_TaskContext *tc) { 1339 const struct GNUNET_SCHEDULER_TaskContext *tc)
1268 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1340{
1269 return; 1341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1342 return;
1270 1343
1271 GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL); 1344 GNUNET_CONFIGURATION_iterate_sections (cfg, publish_iterate, NULL);
1272 1345
1273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, 1346 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, publish_names, NULL);
1274 publish_names,
1275 NULL);
1276} 1347}
1277 1348
1278/** 1349/**
@@ -1299,35 +1370,31 @@ run (void *cls,
1299 {NULL, 0, 0} 1370 {NULL, 0, 0}
1300 }; 1371 };
1301 1372
1302 static GNUNET_MESH_ApplicationType *apptypes; 1373 static GNUNET_MESH_ApplicationType apptypes[] = {
1374 GNUNET_APPLICATION_TYPE_END,
1375 GNUNET_APPLICATION_TYPE_END
1376 };
1377
1303 1378
1304 if (GNUNET_YES != open_port ()) 1379 if (GNUNET_YES != open_port ())
1305 { 1380 {
1306 GNUNET_SCHEDULER_shutdown(); 1381 GNUNET_SCHEDULER_shutdown ();
1307 return; 1382 return;
1308 } 1383 }
1309 1384
1310 if (GNUNET_YES == 1385 if (GNUNET_YES ==
1311 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) 1386 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1312 apptypes = (GNUNET_MESH_ApplicationType[]) 1387 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1313 { 1388 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1314 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1315 GNUNET_APPLICATION_TYPE_END};
1316 else
1317 apptypes = (GNUNET_MESH_ApplicationType[])
1318 {
1319 GNUNET_APPLICATION_TYPE_END};
1320
1321 mesh_handle =
1322 GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1323 1389
1324 cfg = cfg_; 1390 cfg = cfg_;
1325 1391
1326 unsigned int i; 1392 unsigned int i;
1393
1327 for (i = 0; i < 65536; i++) 1394 for (i = 0; i < 65536; i++)
1328 { 1395 {
1329 query_states[i].valid = GNUNET_NO; 1396 query_states[i].valid = GNUNET_NO;
1330 } 1397 }
1331 1398
1332 dht = GNUNET_DHT_connect (cfg, 1024); 1399 dht = GNUNET_DHT_connect (cfg, 1024);
1333 1400
@@ -1349,9 +1416,8 @@ int
1349main (int argc, char *const *argv) 1416main (int argc, char *const *argv)
1350{ 1417{
1351 return (GNUNET_OK == 1418 return (GNUNET_OK ==
1352 GNUNET_SERVICE_run (argc, 1419 GNUNET_SERVICE_run (argc,
1353 argv, 1420 argv,
1354 "dns", 1421 "dns",
1355 GNUNET_SERVICE_OPTION_NONE, 1422 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1356 &run, NULL)) ? 0 : 1;
1357} 1423}
diff --git a/src/vpn/gnunet-vpn-checksum.c b/src/vpn/gnunet-vpn-checksum.c
index 81ec044dc..0e0e24cdc 100644
--- a/src/vpn/gnunet-vpn-checksum.c
+++ b/src/vpn/gnunet-vpn-checksum.c
@@ -26,26 +26,32 @@
26 26
27#include "gnunet-vpn-checksum.h" 27#include "gnunet-vpn-checksum.h"
28 28
29uint32_t calculate_checksum_update(uint32_t sum, uint16_t *hdr, short len) { 29uint32_t
30 for(; len >= 2; len -= 2) 30calculate_checksum_update (uint32_t sum, uint16_t * hdr, short len)
31 sum += *(hdr++); 31{
32 if (len == 1) 32 for (; len >= 2; len -= 2)
33 sum += *((unsigned char*)hdr); 33 sum += *(hdr++);
34 return sum; 34 if (len == 1)
35 sum += *((unsigned char *) hdr);
36 return sum;
35} 37}
36 38
37uint16_t calculate_checksum_end(uint32_t sum) { 39uint16_t
38 while (sum >> 16) 40calculate_checksum_end (uint32_t sum)
39 sum = (sum >> 16) + (sum & 0xFFFF); 41{
42 while (sum >> 16)
43 sum = (sum >> 16) + (sum & 0xFFFF);
40 44
41 return ~sum; 45 return ~sum;
42} 46}
43 47
44/** 48/**
45 * Calculate the checksum of an IPv4-Header 49 * Calculate the checksum of an IPv4-Header
46 */ 50 */
47uint16_t 51uint16_t
48calculate_ip_checksum(uint16_t* hdr, short len) { 52calculate_ip_checksum (uint16_t * hdr, short len)
49 uint32_t sum = calculate_checksum_update(0, hdr, len); 53{
50 return calculate_checksum_end(sum); 54 uint32_t sum = calculate_checksum_update (0, hdr, len);
55
56 return calculate_checksum_end (sum);
51} 57}
diff --git a/src/vpn/gnunet-vpn-checksum.h b/src/vpn/gnunet-vpn-checksum.h
index 0c0605650..82f4954ca 100644
--- a/src/vpn/gnunet-vpn-checksum.h
+++ b/src/vpn/gnunet-vpn-checksum.h
@@ -4,13 +4,13 @@
4 4
5#include <platform.h> 5#include <platform.h>
6 6
7uint32_t calculate_checksum_update(uint32_t sum, uint16_t *hdr, short len); 7uint32_t calculate_checksum_update (uint32_t sum, uint16_t * hdr, short len);
8 8
9uint16_t calculate_checksum_end(uint32_t sum); 9uint16_t calculate_checksum_end (uint32_t sum);
10 10
11/** 11/**
12 * Calculate the checksum of an IPv4-Header 12 * Calculate the checksum of an IPv4-Header
13 */ 13 */
14uint16_t calculate_ip_checksum(uint16_t* hdr, short len); 14uint16_t calculate_ip_checksum (uint16_t * hdr, short len);
15 15
16#endif /* end of include guard: GNUNET-VPN-CHECKSUM_H */ 16#endif /* end of include guard: GNUNET-VPN-CHECKSUM_H */
diff --git a/src/vpn/gnunet-vpn-packet.h b/src/vpn/gnunet-vpn-packet.h
index 91c6eb9c1..47fa69847 100644
--- a/src/vpn/gnunet-vpn-packet.h
+++ b/src/vpn/gnunet-vpn-packet.h
@@ -5,221 +5,246 @@
5#include "gnunet_common.h" 5#include "gnunet_common.h"
6 6
7// Headers 7// Headers
8struct pkt_tun { 8struct pkt_tun
9 unsigned flags:16 GNUNET_PACKED; 9{
10 unsigned type:16 GNUNET_PACKED; 10 unsigned flags:16 GNUNET_PACKED;
11 unsigned type:16 GNUNET_PACKED;
11}; 12};
12 13
13struct ip6_hdr { 14struct ip6_hdr
14 unsigned tclass_h:4 GNUNET_PACKED; 15{
15 unsigned version:4 GNUNET_PACKED; 16 unsigned tclass_h:4 GNUNET_PACKED;
16 unsigned tclass_l:4 GNUNET_PACKED; 17 unsigned version:4 GNUNET_PACKED;
17 unsigned flowlbl:20 GNUNET_PACKED; 18 unsigned tclass_l:4 GNUNET_PACKED;
18 unsigned paylgth:16 GNUNET_PACKED; 19 unsigned flowlbl:20 GNUNET_PACKED;
19 unsigned nxthdr:8 GNUNET_PACKED; 20 unsigned paylgth:16 GNUNET_PACKED;
20 unsigned hoplmt:8 GNUNET_PACKED; 21 unsigned nxthdr:8 GNUNET_PACKED;
21 unsigned char sadr[16]; 22 unsigned hoplmt:8 GNUNET_PACKED;
22 unsigned char dadr[16]; 23 unsigned char sadr[16];
24 unsigned char dadr[16];
23}; 25};
24 26
25struct ip_hdr { 27struct ip_hdr
26 unsigned hdr_lngth:4 GNUNET_PACKED; 28{
27 unsigned version:4 GNUNET_PACKED; 29 unsigned hdr_lngth:4 GNUNET_PACKED;
30 unsigned version:4 GNUNET_PACKED;
28 31
29 unsigned diff_serv:8 GNUNET_PACKED; 32 unsigned diff_serv:8 GNUNET_PACKED;
30 unsigned tot_lngth:16 GNUNET_PACKED; 33 unsigned tot_lngth:16 GNUNET_PACKED;
31 34
32 unsigned ident:16 GNUNET_PACKED; 35 unsigned ident:16 GNUNET_PACKED;
33 unsigned flags:3 GNUNET_PACKED; 36 unsigned flags:3 GNUNET_PACKED;
34 unsigned frag_off:13 GNUNET_PACKED; 37 unsigned frag_off:13 GNUNET_PACKED;
35 38
36 unsigned ttl:8 GNUNET_PACKED; 39 unsigned ttl:8 GNUNET_PACKED;
37 unsigned proto:8 GNUNET_PACKED; 40 unsigned proto:8 GNUNET_PACKED;
38 unsigned chks:16 GNUNET_PACKED; 41 unsigned chks:16 GNUNET_PACKED;
39 42
40 unsigned sadr:32 GNUNET_PACKED; 43 unsigned sadr:32 GNUNET_PACKED;
41 unsigned dadr:32 GNUNET_PACKED; 44 unsigned dadr:32 GNUNET_PACKED;
42}; 45};
43 46
44#define TCP_FLAG_SYN 2 47#define TCP_FLAG_SYN 2
45 48
46struct tcp_pkt { 49struct tcp_pkt
47 unsigned spt:16 GNUNET_PACKED; 50{
48 unsigned dpt:16 GNUNET_PACKED; 51 unsigned spt:16 GNUNET_PACKED;
49 unsigned seq:32 GNUNET_PACKED; 52 unsigned dpt:16 GNUNET_PACKED;
50 unsigned ack:32 GNUNET_PACKED; 53 unsigned seq:32 GNUNET_PACKED;
51 unsigned off:4 GNUNET_PACKED; 54 unsigned ack:32 GNUNET_PACKED;
52 unsigned rsv:4 GNUNET_PACKED; 55 unsigned off:4 GNUNET_PACKED;
53 unsigned flg:8 GNUNET_PACKED; 56 unsigned rsv:4 GNUNET_PACKED;
54 unsigned wsz:16 GNUNET_PACKED; 57 unsigned flg:8 GNUNET_PACKED;
55 unsigned crc:16 GNUNET_PACKED; 58 unsigned wsz:16 GNUNET_PACKED;
56 unsigned urg:16 GNUNET_PACKED; 59 unsigned crc:16 GNUNET_PACKED;
60 unsigned urg:16 GNUNET_PACKED;
57}; 61};
58 62
59struct udp_pkt { 63struct udp_pkt
60 unsigned spt:16 GNUNET_PACKED; 64{
61 unsigned dpt:16 GNUNET_PACKED; 65 unsigned spt:16 GNUNET_PACKED;
62 unsigned len:16 GNUNET_PACKED; 66 unsigned dpt:16 GNUNET_PACKED;
63 unsigned crc:16 GNUNET_PACKED; 67 unsigned len:16 GNUNET_PACKED;
68 unsigned crc:16 GNUNET_PACKED;
64}; 69};
65 70
66struct icmp_hdr { 71struct icmp_hdr
67 unsigned type:8 GNUNET_PACKED; 72{
68 unsigned code:8 GNUNET_PACKED; 73 unsigned type:8 GNUNET_PACKED;
69 unsigned chks:16 GNUNET_PACKED; 74 unsigned code:8 GNUNET_PACKED;
75 unsigned chks:16 GNUNET_PACKED;
70}; 76};
71 77
72// DNS-Stuff 78// DNS-Stuff
73struct dns_static { 79struct dns_static
74 uint16_t id GNUNET_PACKED; 80{
75 81 uint16_t id GNUNET_PACKED;
76 unsigned rd:1 GNUNET_PACKED; // recursion desired (client -> server) 82
77 unsigned tc:1 GNUNET_PACKED; // message is truncated 83 unsigned rd:1 GNUNET_PACKED; // recursion desired (client -> server)
78 unsigned aa:1 GNUNET_PACKED; // authoritative answer 84 unsigned tc:1 GNUNET_PACKED; // message is truncated
79 unsigned op:4 GNUNET_PACKED; // query:0, inverse q.:1, status: 2 85 unsigned aa:1 GNUNET_PACKED; // authoritative answer
80 unsigned qr:1 GNUNET_PACKED; // query:0, response:1 86 unsigned op:4 GNUNET_PACKED; // query:0, inverse q.:1, status: 2
81 87 unsigned qr:1 GNUNET_PACKED; // query:0, response:1
82 unsigned rcode:4 GNUNET_PACKED; // 0 No error 88
83 // 1 Format error 89 unsigned rcode:4 GNUNET_PACKED; // 0 No error
84 // 2 Server failure 90 // 1 Format error
85 // 3 Name Error 91 // 2 Server failure
86 // 4 Not Implemented 92 // 3 Name Error
87 // 5 Refused 93 // 4 Not Implemented
88 unsigned z:3 GNUNET_PACKED; // reserved 94 // 5 Refused
89 unsigned ra:1 GNUNET_PACKED; // recursion available (server -> client) 95 unsigned z:3 GNUNET_PACKED; // reserved
90 96 unsigned ra:1 GNUNET_PACKED; // recursion available (server -> client)
91 uint16_t qdcount GNUNET_PACKED; // number of questions 97
92 uint16_t ancount GNUNET_PACKED; // number of answers 98 uint16_t qdcount GNUNET_PACKED; // number of questions
93 uint16_t nscount GNUNET_PACKED; // number of authority-records 99 uint16_t ancount GNUNET_PACKED; // number of answers
94 uint16_t arcount GNUNET_PACKED; // number of additional records 100 uint16_t nscount GNUNET_PACKED; // number of authority-records
95}; 101 uint16_t arcount GNUNET_PACKED; // number of additional records
96 102};
97struct dns_pkt { 103
98 struct dns_static s; 104struct dns_pkt
99 unsigned char data[1]; 105{
100}; 106 struct dns_static s;
101 107 unsigned char data[1];
102struct dns_pkt_parsed { 108};
103 struct dns_static s; 109
104 struct dns_query** queries; 110struct dns_pkt_parsed
105 struct dns_record** answers; 111{
106 struct dns_record** nameservers; 112 struct dns_static s;
107 struct dns_record** additional; 113 struct dns_query **queries;
108}; 114 struct dns_record **answers;
109 115 struct dns_record **nameservers;
110struct dns_query_line { 116 struct dns_record **additional;
111 unsigned short type; 117};
112 unsigned short class; 118
113}; 119struct dns_query_line
114 120{
115struct dns_query { 121 unsigned short type;
116 char* name; 122 unsigned short class;
117 unsigned char namelen; 123};
118 unsigned short qtype; 124
119 unsigned short qclass; 125struct dns_query
120}; 126{
121 127 char *name;
122struct dns_record_line { 128 unsigned char namelen;
123 unsigned short type; 129 unsigned short qtype;
124 unsigned short class; 130 unsigned short qclass;
125 unsigned int ttl; 131};
126 unsigned short data_len; 132
127 unsigned char data; 133struct dns_record_line
128}; 134{
129 135 unsigned short type;
130struct dns_record { 136 unsigned short class;
131 char* name; 137 unsigned int ttl;
132 unsigned char namelen; 138 unsigned short data_len;
133 unsigned short type; 139 unsigned char data;
134 unsigned short class; 140};
135 unsigned int ttl; 141
136 unsigned short data_len; 142struct dns_record
137 unsigned char* data; 143{
138}; 144 char *name;
139 145 unsigned char namelen;
140struct udp_dns { 146 unsigned short type;
141 struct udp_pkt udp_hdr; 147 unsigned short class;
142 struct dns_pkt data; 148 unsigned int ttl;
149 unsigned short data_len;
150 unsigned char *data;
151};
152
153struct udp_dns
154{
155 struct udp_pkt udp_hdr;
156 struct dns_pkt data;
143}; 157};
144 158
145// Complete Packets 159// Complete Packets
146struct tun_pkt { 160struct tun_pkt
147 struct GNUNET_MessageHeader shdr; 161{
148 struct pkt_tun tun; 162 struct GNUNET_MessageHeader shdr;
149}; 163 struct pkt_tun tun;
150 164};
151struct ip6_pkt { 165
152 struct GNUNET_MessageHeader shdr; 166struct ip6_pkt
153 struct pkt_tun tun; 167{
154 struct ip6_hdr ip6_hdr; 168 struct GNUNET_MessageHeader shdr;
155 unsigned char data[1]; 169 struct pkt_tun tun;
156}; 170 struct ip6_hdr ip6_hdr;
157 171 unsigned char data[1];
158struct ip6_tcp { 172};
159 struct GNUNET_MessageHeader shdr; 173
160 struct pkt_tun tun; 174struct ip6_tcp
161 struct ip6_hdr ip6_hdr; 175{
162 struct tcp_pkt tcp_hdr; 176 struct GNUNET_MessageHeader shdr;
163 unsigned char data[1]; 177 struct pkt_tun tun;
164}; 178 struct ip6_hdr ip6_hdr;
165 179 struct tcp_pkt tcp_hdr;
166struct ip6_icmp { 180 unsigned char data[1];
167 struct GNUNET_MessageHeader shdr; 181};
168 struct pkt_tun tun; 182
169 struct ip6_hdr ip6_hdr; 183struct ip6_icmp
170 struct icmp_hdr icmp_hdr; 184{
171}; 185 struct GNUNET_MessageHeader shdr;
172 186 struct pkt_tun tun;
173struct ip6_udp { 187 struct ip6_hdr ip6_hdr;
174 struct GNUNET_MessageHeader shdr; 188 struct icmp_hdr icmp_hdr;
175 struct pkt_tun tun; 189};
176 struct ip6_hdr ip6_hdr; 190
177 struct udp_pkt udp_hdr; 191struct ip6_udp
178 unsigned char data[1]; 192{
179}; 193 struct GNUNET_MessageHeader shdr;
180 194 struct pkt_tun tun;
181struct ip6_udp_dns { 195 struct ip6_hdr ip6_hdr;
182 struct GNUNET_MessageHeader shdr; 196 struct udp_pkt udp_hdr;
183 struct pkt_tun tun; 197 unsigned char data[1];
184 struct ip6_hdr ip6_hdr; 198};
185 struct udp_dns udp_dns; 199
186}; 200struct ip6_udp_dns
187 201{
188struct ip_pkt { 202 struct GNUNET_MessageHeader shdr;
189 struct GNUNET_MessageHeader shdr; 203 struct pkt_tun tun;
190 struct pkt_tun tun; 204 struct ip6_hdr ip6_hdr;
191 struct ip_hdr ip_hdr; 205 struct udp_dns udp_dns;
192 unsigned char data[1]; 206};
193}; 207
194 208struct ip_pkt
195struct ip_udp { 209{
196 struct GNUNET_MessageHeader shdr; 210 struct GNUNET_MessageHeader shdr;
197 struct pkt_tun tun; 211 struct pkt_tun tun;
198 struct ip_hdr ip_hdr; 212 struct ip_hdr ip_hdr;
199 struct udp_pkt udp_hdr; 213 unsigned char data[1];
200 unsigned char data[1]; 214};
201}; 215
202 216struct ip_udp
203struct ip_udp_dns { 217{
204 struct GNUNET_MessageHeader shdr; 218 struct GNUNET_MessageHeader shdr;
205 struct pkt_tun tun; 219 struct pkt_tun tun;
206 struct ip_hdr ip_hdr; 220 struct ip_hdr ip_hdr;
207 struct udp_dns udp_dns; 221 struct udp_pkt udp_hdr;
208}; 222 unsigned char data[1];
209 223};
210struct ip_tcp { 224
211 struct GNUNET_MessageHeader shdr; 225struct ip_udp_dns
212 struct pkt_tun tun; 226{
213 struct ip_hdr ip_hdr; 227 struct GNUNET_MessageHeader shdr;
214 struct tcp_pkt tcp_hdr; 228 struct pkt_tun tun;
215 unsigned char data[1]; 229 struct ip_hdr ip_hdr;
216}; 230 struct udp_dns udp_dns;
217 231};
218struct ip_icmp { 232
219 struct GNUNET_MessageHeader shdr; 233struct ip_tcp
220 struct pkt_tun tun; 234{
221 struct ip_hdr ip_hdr; 235 struct GNUNET_MessageHeader shdr;
222 struct icmp_hdr icmp_hdr; 236 struct pkt_tun tun;
237 struct ip_hdr ip_hdr;
238 struct tcp_pkt tcp_hdr;
239 unsigned char data[1];
240};
241
242struct ip_icmp
243{
244 struct GNUNET_MessageHeader shdr;
245 struct pkt_tun tun;
246 struct ip_hdr ip_hdr;
247 struct icmp_hdr icmp_hdr;
223}; 248};
224 249
225#endif 250#endif