aboutsummaryrefslogtreecommitdiff
path: root/src/vpn
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-11-04 14:00:32 +0000
committerChristian Grothoff <christian@grothoff.org>2011-11-04 14:00:32 +0000
commit83b19539f4d322b43683f5838b72e9ec2c8e6073 (patch)
treed0ab9329fcbefe360d9d14e2ace21a6b3396dfe9 /src/vpn
parent28a2eb43281a1f08a67954f07beb9af3a9bc9a35 (diff)
downloadgnunet-83b19539f4d322b43683f5838b72e9ec2c8e6073.tar.gz
gnunet-83b19539f4d322b43683f5838b72e9ec2c8e6073.zip
curly wars / auto-indentation
Diffstat (limited to 'src/vpn')
-rw-r--r--src/vpn/gnunet-daemon-exit.c178
-rw-r--r--src/vpn/gnunet-daemon-vpn-dns.c41
-rw-r--r--src/vpn/gnunet-daemon-vpn-helper.c315
-rw-r--r--src/vpn/gnunet-daemon-vpn.c111
-rw-r--r--src/vpn/gnunet-dns-parser.c182
-rw-r--r--src/vpn/gnunet-service-dns-p.h3
-rw-r--r--src/vpn/gnunet-service-dns.c676
7 files changed, 797 insertions, 709 deletions
diff --git a/src/vpn/gnunet-daemon-exit.c b/src/vpn/gnunet-daemon-exit.c
index 45076250d..838893882 100644
--- a/src/vpn/gnunet-daemon-exit.c
+++ b/src/vpn/gnunet-daemon-exit.c
@@ -199,24 +199,26 @@ cleanup (void *cls
199} 199}
200 200
201static void * 201static void *
202new_tunnel (void *cls __attribute__((unused)), 202new_tunnel (void *cls
203 struct GNUNET_MESH_Tunnel *tunnel, 203 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
204 const struct GNUNET_PeerIdentity *initiator __attribute__((unused)), 204 const struct GNUNET_PeerIdentity *initiator
205 const struct GNUNET_ATS_Information *ats __attribute__((unused))) 205 __attribute__ ((unused)), const struct GNUNET_ATS_Information *ats
206 __attribute__ ((unused)))
206{ 207{
207 struct tunnel_state *s = GNUNET_malloc(sizeof *s); 208 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
208 s->head = NULL; 209
209 s->tail = NULL; 210 s->head = NULL;
210 s->th = NULL; 211 s->tail = NULL;
211 return s; 212 s->th = NULL;
213 return s;
212} 214}
213 215
214static void 216static void
215clean_tunnel (void *cls __attribute__((unused)), 217clean_tunnel (void *cls
216 const struct GNUNET_MESH_Tunnel *tunnel, 218 __attribute__ ((unused)), const struct GNUNET_MESH_Tunnel *tunnel,
217 void *tunnel_ctx) 219 void *tunnel_ctx)
218{ 220{
219 GNUNET_free(tunnel_ctx); 221 GNUNET_free (tunnel_ctx);
220} 222}
221 223
222static void 224static void
@@ -236,7 +238,8 @@ collect_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */ 238 /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
237 239
238 GNUNET_assert (GNUNET_OK == 240 GNUNET_assert (GNUNET_OK ==
239 GNUNET_CONTAINER_multihashmap_remove (state->hashmap, &state->hash, state)); 241 GNUNET_CONTAINER_multihashmap_remove (state->hashmap,
242 &state->hash, state));
240 243
241 GNUNET_free (state); 244 GNUNET_free (state);
242} 245}
@@ -275,7 +278,7 @@ send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
275 memcpy (buf, hdr, ntohs (hdr->size)); 278 memcpy (buf, hdr, ntohs (hdr->size));
276 size = ntohs (hdr->size); 279 size = ntohs (hdr->size);
277 280
278 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data(*tunnel); 281 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);
279 282
280 if (NULL != s->head) 283 if (NULL != s->head)
281 { 284 {
@@ -284,9 +287,7 @@ send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
284 GNUNET_CONTAINER_DLL_remove (s->head, s->tail, element); 287 GNUNET_CONTAINER_DLL_remove (s->head, s->tail, element);
285 288
286 s->th = 289 s->th =
287 GNUNET_MESH_notify_transmit_ready (*tunnel, 290 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
288 GNUNET_NO,
289 42,
290 GNUNET_TIME_relative_divide 291 GNUNET_TIME_relative_divide
291 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 292 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
292 (const struct GNUNET_PeerIdentity *) 293 (const struct GNUNET_PeerIdentity *)
@@ -394,14 +395,13 @@ udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
394 395
395 memcpy (_udp, udp, ntohs (udp->len)); 396 memcpy (_udp, udp, ntohs (udp->len));
396 397
397 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data(tunnel); 398 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel);
399
398 if (NULL == s->th) 400 if (NULL == s->th)
399 { 401 {
400 /* No notify is pending */ 402 /* No notify is pending */
401 s->th = 403 s->th =
402 GNUNET_MESH_notify_transmit_ready (tunnel, 404 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42,
403 GNUNET_NO,
404 42,
405 GNUNET_TIME_relative_divide 405 GNUNET_TIME_relative_divide
406 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 406 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
407 (const struct GNUNET_PeerIdentity *) 407 (const struct GNUNET_PeerIdentity *)
@@ -502,18 +502,16 @@ tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
502 memcpy (_tcp, tcp, pktlen); 502 memcpy (_tcp, tcp, pktlen);
503 503
504 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel); 504 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (tunnel);
505
505 if (NULL == s->th) 506 if (NULL == s->th)
506 { 507 {
507 /* No notify is pending */ 508 /* No notify is pending */
508 s->th = 509 s->th =
509 GNUNET_MESH_notify_transmit_ready (tunnel, 510 GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 42,
510 GNUNET_NO,
511 42,
512 GNUNET_TIME_relative_divide 511 GNUNET_TIME_relative_divide
513 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 512 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
514 (const struct GNUNET_PeerIdentity *) 513 (const struct GNUNET_PeerIdentity *)
515 NULL, 514 NULL, len,
516 len,
517 send_udp_to_peer_notify_callback, 515 send_udp_to_peer_notify_callback,
518 ctunnel); 516 ctunnel);
519 } 517 }
@@ -684,44 +682,57 @@ read_service_conf (void *cls __attribute__ ((unused)), const char *section)
684 } 682 }
685 else 683 else
686 { 684 {
687 struct addrinfo* res; 685 struct addrinfo *res;
688 686
689 int ret = getaddrinfo(hostname, NULL, NULL, &res); 687 int ret = getaddrinfo (hostname, NULL, NULL, &res);
690 688
691 if (ret != 0) 689 if (ret != 0)
690 {
691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n",
692 hostname);
693 GNUNET_free (serv);
694 continue;
695 }
696 else
697 {
698 char buf[256];
699 struct addrinfo *c = res;
700
701 if (c)
692 { 702 {
693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No addresses found for %s!\n", hostname); 703 if (c->ai_family == AF_INET)
694 GNUNET_free (serv); 704 {
695 continue; 705 serv->version = 4;
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "Found %s as address for %s\n",
708 inet_ntop (c->ai_family,
709 &((struct sockaddr_in *) (c->
710 ai_addr))->sin_addr,
711 (char *) &buf, 256), hostname);
712 memcpy (serv->v4.ip4address,
713 &((struct sockaddr_in *) (c->ai_addr))->sin_addr, 4);
714 }
715 else if (c->ai_family == AF_INET6)
716 {
717 serv->version = 6;
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Found %s as address for %s\n",
720 inet_ntop (c->ai_family,
721 &((struct sockaddr_in6 *) (c->
722 ai_addr))->sin6_addr,
723 (char *) &buf, 256), hostname);
724 memcpy (serv->v6.ip6address,
725 &((struct sockaddr_in6 *) (c->ai_addr))->sin6_addr, 16);
726 }
696 } 727 }
697 else 728 else
698 { 729 {
699 char buf[256]; 730 freeaddrinfo (res);
700 struct addrinfo* c = res; 731 GNUNET_free (serv);
701 732 continue;
702 if(c)
703 {
704 if (c->ai_family == AF_INET)
705 {
706 serv->version = 4;
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %s as address for %s\n", inet_ntop(c->ai_family, &((struct sockaddr_in *)(c->ai_addr))->sin_addr, (char*)&buf, 256), hostname);
708 memcpy(serv->v4.ip4address, &((struct sockaddr_in *)(c->ai_addr))->sin_addr, 4);
709 }
710 else if (c->ai_family == AF_INET6)
711 {
712 serv->version = 6;
713 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %s as address for %s\n", inet_ntop(c->ai_family, &((struct sockaddr_in6*)(c->ai_addr))->sin6_addr, (char*)&buf, 256), hostname);
714 memcpy(serv->v6.ip6address, &((struct sockaddr_in6 *)(c->ai_addr))->sin6_addr, 16);
715 }
716 }
717 else
718 {
719 freeaddrinfo(res);
720 GNUNET_free (serv);
721 continue;
722 }
723 freeaddrinfo(res);
724 } 733 }
734 freeaddrinfo (res);
735 }
725 } 736 }
726 serv->remote_port = atoi (hostport); 737 serv->remote_port = atoi (hostport);
727 if (UDP == proto) 738 if (UDP == proto)
@@ -1095,14 +1106,12 @@ receive_tcp_service (void *cls
1095 switch (serv->version) 1106 switch (serv->version)
1096 { 1107 {
1097 case 4: 1108 case 4:
1098 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, 1109 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v4.ip4address,
1099 &serv->v4.ip4address, tunnel, state, 1110 tunnel, state, (struct ip_pkt *) buf);
1100 (struct ip_pkt *) buf);
1101 break; 1111 break;
1102 case 6: 1112 case 6:
1103 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, 1113 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &serv->v6.ip6address,
1104 &serv->v6.ip6address, tunnel, state, 1114 tunnel, state, (struct ip6_pkt *) buf);
1105 (struct ip6_pkt *) buf);
1106 1115
1107 break; 1116 break;
1108 default: 1117 default:
@@ -1171,12 +1180,12 @@ receive_tcp_remote (void *cls
1171 switch (s->addrlen) 1180 switch (s->addrlen)
1172 { 1181 {
1173 case 4: 1182 case 4:
1174 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, 1183 prepare_ipv4_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel,
1175 &s->addr, tunnel, state, (struct ip_pkt *) buf); 1184 state, (struct ip_pkt *) buf);
1176 break; 1185 break;
1177 case 16: 1186 case 16:
1178 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, 1187 prepare_ipv6_packet (len, pkt_len, pkt, IPPROTO_TCP, &s->addr, tunnel,
1179 &s->addr, tunnel, state, (struct ip6_pkt *) buf); 1188 state, (struct ip6_pkt *) buf);
1180 break; 1189 break;
1181 default: 1190 default:
1182 GNUNET_free (state); 1191 GNUNET_free (state);
@@ -1252,12 +1261,12 @@ receive_udp_remote (void *cls
1252 switch (s->addrlen) 1261 switch (s->addrlen)
1253 { 1262 {
1254 case 4: 1263 case 4:
1255 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, 1264 prepare_ipv4_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr,
1256 &s->addr, tunnel, state, (struct ip_pkt *) buf); 1265 tunnel, state, (struct ip_pkt *) buf);
1257 break; 1266 break;
1258 case 16: 1267 case 16:
1259 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, 1268 prepare_ipv6_packet (len, ntohs (pkt->len), pkt, IPPROTO_UDP, &s->addr,
1260 &s->addr, tunnel, state, (struct ip6_pkt *) buf); 1269 tunnel, state, (struct ip6_pkt *) buf);
1261 break; 1270 break;
1262 default: 1271 default:
1263 GNUNET_assert (0); 1272 GNUNET_assert (0);
@@ -1291,12 +1300,14 @@ receive_udp_remote (void *cls
1291 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt 1300 * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
1292 */ 1301 */
1293static int 1302static int
1294receive_udp_service (void *cls __attribute__ ((unused)), 1303receive_udp_service (void *cls
1295 struct GNUNET_MESH_Tunnel *tunnel, 1304 __attribute__ ((unused)),
1296 void **tunnel_ctx, 1305 struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
1297 const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)), 1306 const struct GNUNET_PeerIdentity *sender
1307 __attribute__ ((unused)),
1298 const struct GNUNET_MessageHeader *message, 1308 const struct GNUNET_MessageHeader *message,
1299 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused))) 1309 const struct GNUNET_ATS_Information *atsi
1310 __attribute__ ((unused)))
1300{ 1311{
1301 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1312 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1302 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1); 1313 struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
@@ -1437,7 +1448,9 @@ connect_to_mesh ()
1437 app_idx++; 1448 app_idx++;
1438 } 1449 }
1439 1450
1440 mesh_handle = GNUNET_MESH_connect (cfg, 42, NULL, new_tunnel, clean_tunnel, handlers, apptypes); 1451 mesh_handle =
1452 GNUNET_MESH_connect (cfg, 42, NULL, new_tunnel, clean_tunnel, handlers,
1453 apptypes);
1441} 1454}
1442 1455
1443 1456
@@ -1468,11 +1481,11 @@ run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1468 1481
1469 if (GNUNET_OK != 1482 if (GNUNET_OK !=
1470 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS", 1483 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
1471 &max_udp_connections)) 1484 &max_udp_connections))
1472 max_udp_connections = 1024; 1485 max_udp_connections = 1024;
1473 if (GNUNET_OK != 1486 if (GNUNET_OK !=
1474 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS", 1487 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
1475 &max_tcp_connections)) 1488 &max_tcp_connections))
1476 max_tcp_connections = 256; 1489 max_tcp_connections = 256;
1477 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL); 1490 GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
1478 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL); 1491 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
@@ -1481,7 +1494,7 @@ run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfile
1481 1494
1482 1495
1483/** 1496/**
1484 * The main function 1497 * The main function
1485 * 1498 *
1486 * @param argc number of arguments from the command line 1499 * @param argc number of arguments from the command line
1487 * @param argv command line arguments 1500 * @param argv command line arguments
@@ -1495,8 +1508,9 @@ main (int argc, char *const *argv)
1495 }; 1508 };
1496 1509
1497 return (GNUNET_OK == 1510 return (GNUNET_OK ==
1498 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit", 1511 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
1499 gettext_noop ("Daemon to run to provide an IP exit node for the VPN"), 1512 gettext_noop
1513 ("Daemon to run to provide an IP exit node for the VPN"),
1500 options, &run, NULL)) ? ret : 1; 1514 options, &run, NULL)) ? ret : 1;
1501} 1515}
1502 1516
diff --git a/src/vpn/gnunet-daemon-vpn-dns.c b/src/vpn/gnunet-daemon-vpn-dns.c
index 7f762238c..77d900046 100644
--- a/src/vpn/gnunet-daemon-vpn-dns.c
+++ b/src/vpn/gnunet-daemon-vpn-dns.c
@@ -57,6 +57,7 @@ size_t
57send_query (void *cls __attribute__ ((unused)), size_t size, void *buf) 57send_query (void *cls __attribute__ ((unused)), size_t size, void *buf)
58{ 58{
59 size_t len; 59 size_t len;
60
60 dns_transmit_handle = NULL; 61 dns_transmit_handle = NULL;
61 62
62 /* 63 /*
@@ -100,17 +101,20 @@ send_query (void *cls __attribute__ ((unused)), size_t size, void *buf)
100 */ 101 */
101 if (head != NULL) 102 if (head != NULL)
102 { 103 {
103 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, 104 dns_transmit_handle =
104 ntohs (head->pkt.hdr.size), 105 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
105 GNUNET_TIME_UNIT_FOREVER_REL, 106 ntohs (head->pkt.hdr.size),
106 GNUNET_YES, &send_query, NULL); 107 GNUNET_TIME_UNIT_FOREVER_REL,
108 GNUNET_YES, &send_query, NULL);
107 } 109 }
108 else if (restart_hijack == 1) 110 else if (restart_hijack == 1)
109 { 111 {
110 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, 112 dns_transmit_handle =
111 sizeof (struct GNUNET_MessageHeader), 113 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
112 GNUNET_TIME_UNIT_FOREVER_REL, 114 sizeof (struct
113 GNUNET_YES, &send_query, NULL); 115 GNUNET_MessageHeader),
116 GNUNET_TIME_UNIT_FOREVER_REL,
117 GNUNET_YES, &send_query, NULL);
114 } 118 }
115 119
116 return len; 120 return len;
@@ -144,16 +148,19 @@ connect_to_service_dns (void *cls
144 148
145 /* If a packet is already in the list, schedule to send it */ 149 /* If a packet is already in the list, schedule to send it */
146 if (dns_transmit_handle == NULL && head != NULL) 150 if (dns_transmit_handle == NULL && head != NULL)
147 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, 151 dns_transmit_handle =
148 ntohs (head->pkt.hdr.size), 152 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
149 GNUNET_TIME_UNIT_FOREVER_REL, 153 ntohs (head->pkt.hdr.size),
150 GNUNET_YES, &send_query, NULL); 154 GNUNET_TIME_UNIT_FOREVER_REL,
155 GNUNET_YES, &send_query, NULL);
151 else if (dns_transmit_handle == NULL && restart_hijack == 1) 156 else if (dns_transmit_handle == NULL && restart_hijack == 1)
152 { 157 {
153 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, 158 dns_transmit_handle =
154 sizeof (struct GNUNET_MessageHeader), 159 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
155 GNUNET_TIME_UNIT_FOREVER_REL, 160 sizeof (struct
156 GNUNET_YES, &send_query, NULL); 161 GNUNET_MessageHeader),
162 GNUNET_TIME_UNIT_FOREVER_REL,
163 GNUNET_YES, &send_query, NULL);
157 } 164 }
158} 165}
159 166
@@ -170,7 +177,7 @@ dns_answer_handler (void *cls
170 if (msg == NULL) 177 if (msg == NULL)
171 { 178 {
172 if (dns_transmit_handle != NULL) 179 if (dns_transmit_handle != NULL)
173 GNUNET_CLIENT_notify_transmit_ready_cancel(dns_transmit_handle); 180 GNUNET_CLIENT_notify_transmit_ready_cancel (dns_transmit_handle);
174 dns_transmit_handle = NULL; 181 dns_transmit_handle = NULL;
175 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO); 182 GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
176 dns_connection = NULL; 183 dns_connection = NULL;
diff --git a/src/vpn/gnunet-daemon-vpn-helper.c b/src/vpn/gnunet-daemon-vpn-helper.c
index 124b53bf1..29227893a 100644
--- a/src/vpn/gnunet-daemon-vpn-helper.c
+++ b/src/vpn/gnunet-daemon-vpn-helper.c
@@ -45,7 +45,7 @@
45 45
46struct GNUNET_VPN_HELPER_Handle *helper_handle; 46struct GNUNET_VPN_HELPER_Handle *helper_handle;
47 47
48extern struct GNUNET_CLIENT_TransmitHandle* dns_transmit_handle; 48extern struct GNUNET_CLIENT_TransmitHandle *dns_transmit_handle;
49 49
50/** 50/**
51 * The tunnels that will be used to send tcp- and udp-packets 51 * The tunnels that will be used to send tcp- and udp-packets
@@ -140,10 +140,12 @@ start_helper_and_schedule (void *cls,
140 */ 140 */
141 restart_hijack = 1; 141 restart_hijack = 1;
142 if (NULL != dns_connection && dns_transmit_handle == NULL) 142 if (NULL != dns_connection && dns_transmit_handle == NULL)
143 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, 143 dns_transmit_handle =
144 sizeof (struct GNUNET_MessageHeader), 144 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
145 GNUNET_TIME_UNIT_FOREVER_REL, 145 sizeof (struct
146 GNUNET_YES, &send_query, NULL); 146 GNUNET_MessageHeader),
147 GNUNET_TIME_UNIT_FOREVER_REL,
148 GNUNET_YES, &send_query, NULL);
147 149
148 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, 150 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
149 helper_handle->fh_to_helper, &helper_write, 151 helper_handle->fh_to_helper, &helper_write,
@@ -152,10 +154,11 @@ start_helper_and_schedule (void *cls,
152 154
153/*}}}*/ 155/*}}}*/
154 156
155static void* 157static void *
156initialize_tunnel_state(int addrlen, struct GNUNET_MESH_TransmitHandle* th) 158initialize_tunnel_state (int addrlen, struct GNUNET_MESH_TransmitHandle *th)
157{ 159{
158 struct tunnel_state* ts = GNUNET_malloc(sizeof *ts); 160 struct tunnel_state *ts = GNUNET_malloc (sizeof *ts);
161
159 ts->addrlen = addrlen; 162 ts->addrlen = addrlen;
160 ts->th = th; 163 ts->th = th;
161 return ts; 164 return ts;
@@ -186,111 +189,115 @@ helper_write (void *cls
186 189
187 size_t data_len = len - sizeof (struct answer_packet) + 1; 190 size_t data_len = len - sizeof (struct answer_packet) + 1;
188 191
189 void* buf; 192 void *buf;
190 size_t pkt_len; 193 size_t pkt_len;
191 194
192 if (ans->pkt.addrlen == 16) 195 if (ans->pkt.addrlen == 16)
193 { 196 {
194 size_t net_len = sizeof (struct ip6_hdr) + sizeof (struct udp_dns) + data_len; 197 size_t net_len =
195 pkt_len = 198 sizeof (struct ip6_hdr) + sizeof (struct udp_dns) + data_len;
196 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len; 199 pkt_len =
200 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
201 net_len;
197 202
198 struct ip6_udp_dns *pkt = alloca (pkt_len); 203 struct ip6_udp_dns *pkt = alloca (pkt_len);
199 204
200 GNUNET_assert (pkt != NULL); 205 GNUNET_assert (pkt != NULL);
201 memset (pkt, 0, pkt_len); 206 memset (pkt, 0, pkt_len);
202 207
203 /* set the gnunet-header */ 208 /* set the gnunet-header */
204 pkt->shdr.size = htons (pkt_len); 209 pkt->shdr.size = htons (pkt_len);
205 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); 210 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
206 211
207 /* set the tun-header (no flags and ethertype of IPv4) */ 212 /* set the tun-header (no flags and ethertype of IPv4) */
208 pkt->tun.flags = 0; 213 pkt->tun.flags = 0;
209 pkt->tun.type = htons (0x86dd); 214 pkt->tun.type = htons (0x86dd);
210 215
211 memcpy(&pkt->ip6_hdr.sadr, ans->pkt.from, 16); 216 memcpy (&pkt->ip6_hdr.sadr, ans->pkt.from, 16);
212 memcpy(&pkt->ip6_hdr.dadr, ans->pkt.to, 16); 217 memcpy (&pkt->ip6_hdr.dadr, ans->pkt.to, 16);
213 218
214 /* set the udp-header */ 219 /* set the udp-header */
215 pkt->udp_dns.udp_hdr.spt = htons (53); 220 pkt->udp_dns.udp_hdr.spt = htons (53);
216 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port; 221 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
217 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip6_hdr)); 222 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip6_hdr));
218 pkt->udp_dns.udp_hdr.crc = 0; 223 pkt->udp_dns.udp_hdr.crc = 0;
219 uint32_t sum = 0; 224 uint32_t sum = 0;
220 225
221 sum = 226 sum = calculate_checksum_update (sum, (uint16_t *) & pkt->ip6_hdr.sadr, 16);
222 calculate_checksum_update (sum, (uint16_t *) & pkt->ip6_hdr.sadr, 16); 227 sum = calculate_checksum_update (sum, (uint16_t *) & pkt->ip6_hdr.dadr, 16);
223 sum = 228 uint32_t tmp = (pkt->udp_dns.udp_hdr.len & 0xffff);
224 calculate_checksum_update (sum, (uint16_t *) & pkt->ip6_hdr.dadr, 16);
225 uint32_t tmp = (pkt->udp_dns.udp_hdr.len & 0xffff);
226 229
227 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 230 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
228 tmp = htons (((pkt->ip6_hdr.nxthdr & 0x00ff))); 231 tmp = htons (((pkt->ip6_hdr.nxthdr & 0x00ff)));
229 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 232 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
230 233
231 sum = 234 sum =
232 calculate_checksum_update (sum, (uint16_t *) & pkt->udp_dns.udp_hdr, 235 calculate_checksum_update (sum, (uint16_t *) & pkt->udp_dns.udp_hdr,
233 ntohs (net_len - sizeof(struct ip6_hdr))); 236 ntohs (net_len - sizeof (struct ip6_hdr)));
234 pkt->udp_dns.udp_hdr.crc = calculate_checksum_end (sum); 237 pkt->udp_dns.udp_hdr.crc = calculate_checksum_end (sum);
235 238
236 pkt->ip6_hdr.version = 6; 239 pkt->ip6_hdr.version = 6;
237 pkt->ip6_hdr.paylgth = net_len - sizeof (struct ip6_hdr); 240 pkt->ip6_hdr.paylgth = net_len - sizeof (struct ip6_hdr);
238 pkt->ip6_hdr.nxthdr = IPPROTO_UDP; 241 pkt->ip6_hdr.nxthdr = IPPROTO_UDP;
239 pkt->ip6_hdr.hoplmt = 0xff; 242 pkt->ip6_hdr.hoplmt = 0xff;
240 243
241 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len); 244 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
242 buf = pkt; 245 buf = pkt;
243 } 246 }
244 else if (ans->pkt.addrlen == 4) 247 else if (ans->pkt.addrlen == 4)
245 { 248 {
246 size_t net_len = sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len; 249 size_t net_len =
247 pkt_len = 250 sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len;
248 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len; 251 pkt_len =
249 252 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
250 struct ip_udp_dns *pkt = alloca (pkt_len); 253 net_len;
251 254
252 GNUNET_assert (pkt != NULL); 255 struct ip_udp_dns *pkt = alloca (pkt_len);
253 memset (pkt, 0, pkt_len); 256
254 257 GNUNET_assert (pkt != NULL);
255 /* set the gnunet-header */ 258 memset (pkt, 0, pkt_len);
256 pkt->shdr.size = htons (pkt_len); 259
257 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER); 260 /* set the gnunet-header */
258 261 pkt->shdr.size = htons (pkt_len);
259 /* set the tun-header (no flags and ethertype of IPv4) */ 262 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
260 pkt->tun.flags = 0; 263
261 pkt->tun.type = htons (0x0800); 264 /* set the tun-header (no flags and ethertype of IPv4) */
262 265 pkt->tun.flags = 0;
263 /* set the ip-header */ 266 pkt->tun.type = htons (0x0800);
264 pkt->ip_hdr.version = 4; 267
265 pkt->ip_hdr.hdr_lngth = 5; 268 /* set the ip-header */
266 pkt->ip_hdr.diff_serv = 0; 269 pkt->ip_hdr.version = 4;
267 pkt->ip_hdr.tot_lngth = htons (net_len); 270 pkt->ip_hdr.hdr_lngth = 5;
268 pkt->ip_hdr.ident = 0; 271 pkt->ip_hdr.diff_serv = 0;
269 pkt->ip_hdr.flags = 0; 272 pkt->ip_hdr.tot_lngth = htons (net_len);
270 pkt->ip_hdr.frag_off = 0; 273 pkt->ip_hdr.ident = 0;
271 pkt->ip_hdr.ttl = 255; 274 pkt->ip_hdr.flags = 0;
272 pkt->ip_hdr.proto = IPPROTO_UDP; 275 pkt->ip_hdr.frag_off = 0;
273 pkt->ip_hdr.chks = 0; /* Will be calculated later */ 276 pkt->ip_hdr.ttl = 255;
274 277 pkt->ip_hdr.proto = IPPROTO_UDP;
275 memcpy(&pkt->ip_hdr.sadr, ans->pkt.from, 4); 278 pkt->ip_hdr.chks = 0; /* Will be calculated later */
276 memcpy(&pkt->ip_hdr.dadr, ans->pkt.to, 4); 279
277 280 memcpy (&pkt->ip_hdr.sadr, ans->pkt.from, 4);
278 pkt->ip_hdr.chks = calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4); 281 memcpy (&pkt->ip_hdr.dadr, ans->pkt.to, 4);
279 282
280 /* set the udp-header */ 283 pkt->ip_hdr.chks =
281 pkt->udp_dns.udp_hdr.spt = htons (53); 284 calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4);
282 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port; 285
283 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr)); 286 /* set the udp-header */
284 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */ 287 pkt->udp_dns.udp_hdr.spt = htons (53);
285 288 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
286 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len); 289 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr));
287 buf = pkt; 290 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
288 } 291
292 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
293 buf = pkt;
294 }
289 else 295 else
290 { 296 {
291 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Wrong addrlen = %d\n", ans->pkt.addrlen); 297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Wrong addrlen = %d\n",
292 GNUNET_assert(0); 298 ans->pkt.addrlen);
293 } 299 GNUNET_assert (0);
300 }
294 301
295 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans); 302 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
296 GNUNET_free (ans); 303 GNUNET_free (ans);
@@ -332,37 +339,43 @@ message_token (void *cls __attribute__ ((unused)), void *client
332 struct ip6_udp *pkt6_udp; 339 struct ip6_udp *pkt6_udp;
333 struct ip6_icmp *pkt6_icmp; 340 struct ip6_icmp *pkt6_icmp;
334 341
335 pkt6_udp = NULL; /* make compiler happy */ 342 pkt6_udp = NULL; /* make compiler happy */
336 switch (pkt6->ip6_hdr.nxthdr) 343 switch (pkt6->ip6_hdr.nxthdr)
337 { 344 {
338 case IPPROTO_UDP: 345 case IPPROTO_UDP:
339 pkt6_udp = (struct ip6_udp *) pkt6; 346 pkt6_udp = (struct ip6_udp *) pkt6;
340 /* Send dns-packets to the service-dns */ 347 /* Send dns-packets to the service-dns */
341 if (ntohs (pkt6_udp->udp_hdr.dpt) == 53) 348 if (ntohs (pkt6_udp->udp_hdr.dpt) == 53)
342 { 349 {
343 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */ 350 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
344 size_t len = sizeof (struct query_packet) + ntohs (pkt6_udp->udp_hdr.len) - 9; 351 size_t len =
345 352 sizeof (struct query_packet) + ntohs (pkt6_udp->udp_hdr.len) - 9;
346 struct query_packet_list *query = 353
347 GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 354 struct query_packet_list *query =
348 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); 355 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
349 query->pkt.hdr.size = htons (len); 356 sizeof (struct answer_packet));
350 memcpy(query->pkt.orig_to, &pkt6->ip6_hdr.dadr, 16); 357 query->pkt.hdr.type =
351 memcpy(query->pkt.orig_from, &pkt6->ip6_hdr.sadr, 16); 358 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
352 query->pkt.addrlen = 16; 359 query->pkt.hdr.size = htons (len);
353 query->pkt.src_port = pkt6_udp->udp_hdr.spt; 360 memcpy (query->pkt.orig_to, &pkt6->ip6_hdr.dadr, 16);
354 memcpy (query->pkt.data, pkt6_udp->data, ntohs (pkt6_udp->udp_hdr.len) - 8); 361 memcpy (query->pkt.orig_from, &pkt6->ip6_hdr.sadr, 16);
355 362 query->pkt.addrlen = 16;
356 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query); 363 query->pkt.src_port = pkt6_udp->udp_hdr.spt;
357 364 memcpy (query->pkt.data, pkt6_udp->data,
358 GNUNET_assert (head != NULL); 365 ntohs (pkt6_udp->udp_hdr.len) - 8);
359 366
360 if (dns_connection != NULL && dns_transmit_handle == NULL) 367 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
361 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, len, 368
362 GNUNET_TIME_UNIT_FOREVER_REL, 369 GNUNET_assert (head != NULL);
363 GNUNET_YES, &send_query, NULL); 370
364 break; 371 if (dns_connection != NULL && dns_transmit_handle == NULL)
365 } 372 dns_transmit_handle =
373 GNUNET_CLIENT_notify_transmit_ready (dns_connection, len,
374 GNUNET_TIME_UNIT_FOREVER_REL,
375 GNUNET_YES, &send_query,
376 NULL);
377 break;
378 }
366 /* fall through */ 379 /* fall through */
367 case IPPROTO_TCP: 380 case IPPROTO_TCP:
368 pkt6_tcp = (struct ip6_tcp *) pkt6; 381 pkt6_tcp = (struct ip6_tcp *) pkt6;
@@ -418,19 +431,20 @@ message_token (void *cls __attribute__ ((unused)), void *client
418 } 431 }
419 else 432 else
420 { 433 {
421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n", port_in_ports(me->desc.ports, pkt6_tcp->tcp_hdr.dpt)); 434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n",
422 GNUNET_assert(0); 435 port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt));
436 GNUNET_assert (0);
423 } 437 }
424 if (me->tunnel == NULL && NULL != cls) 438 if (me->tunnel == NULL && NULL != cls)
425 { 439 {
426 *cls = 440 *cls =
427 GNUNET_MESH_tunnel_create(mesh_handle, initialize_tunnel_state(16, NULL), 441 GNUNET_MESH_tunnel_create (mesh_handle,
428 &send_pkt_to_peer, NULL, cls); 442 initialize_tunnel_state (16, NULL),
443 &send_pkt_to_peer, NULL, cls);
429 444
430 GNUNET_MESH_peer_request_connect_add (*cls, 445 GNUNET_MESH_peer_request_connect_add (*cls,
431 (struct 446 (struct GNUNET_PeerIdentity *)
432 GNUNET_PeerIdentity *) 447 &me->desc.peer);
433 &me->desc.peer);
434 me->tunnel = *cls; 448 me->tunnel = *cls;
435 } 449 }
436 else if (NULL != cls) 450 else if (NULL != cls)
@@ -451,7 +465,7 @@ message_token (void *cls __attribute__ ((unused)), void *client
451 memcpy (s->addr, me->addr, me->addrlen); 465 memcpy (s->addr, me->addr, me->addrlen);
452 s->proto = pkt6->ip6_hdr.nxthdr; 466 s->proto = pkt6->ip6_hdr.nxthdr;
453 if (s->proto == IPPROTO_UDP) 467 if (s->proto == IPPROTO_UDP)
454 { 468 {
455 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP); 469 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
456 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len)); 470 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
457 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY; 471 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
@@ -473,11 +487,11 @@ message_token (void *cls __attribute__ ((unused)), void *client
473 if (me->tunnel == NULL && NULL != cls) 487 if (me->tunnel == NULL && NULL != cls)
474 { 488 {
475 *cls = 489 *cls =
476 GNUNET_MESH_tunnel_create(mesh_handle, initialize_tunnel_state(16, NULL), 490 GNUNET_MESH_tunnel_create (mesh_handle,
477 &send_pkt_to_peer, NULL, cls); 491 initialize_tunnel_state (16, NULL),
492 &send_pkt_to_peer, NULL, cls);
478 493
479 GNUNET_MESH_peer_request_connect_by_type (*cls, 494 GNUNET_MESH_peer_request_connect_by_type (*cls, app_type);
480 app_type);
481 me->tunnel = *cls; 495 me->tunnel = *cls;
482 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type) 496 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
483 udp_tunnel = *cls; 497 udp_tunnel = *cls;
@@ -538,11 +552,12 @@ message_token (void *cls __attribute__ ((unused)), void *client
538 size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9; 552 size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
539 553
540 struct query_packet_list *query = 554 struct query_packet_list *query =
541 GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 555 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
556 sizeof (struct answer_packet));
542 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); 557 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
543 query->pkt.hdr.size = htons (len); 558 query->pkt.hdr.size = htons (len);
544 memcpy(query->pkt.orig_to, &pkt->ip_hdr.dadr, 4); 559 memcpy (query->pkt.orig_to, &pkt->ip_hdr.dadr, 4);
545 memcpy(query->pkt.orig_from, &pkt->ip_hdr.sadr, 4); 560 memcpy (query->pkt.orig_from, &pkt->ip_hdr.sadr, 4);
546 query->pkt.addrlen = 4; 561 query->pkt.addrlen = 4;
547 query->pkt.src_port = udp->udp_hdr.spt; 562 query->pkt.src_port = udp->udp_hdr.spt;
548 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8); 563 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
@@ -552,9 +567,10 @@ message_token (void *cls __attribute__ ((unused)), void *client
552 GNUNET_assert (head != NULL); 567 GNUNET_assert (head != NULL);
553 568
554 if (dns_connection != NULL && dns_transmit_handle == NULL) 569 if (dns_connection != NULL && dns_transmit_handle == NULL)
555 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, len, 570 dns_transmit_handle =
556 GNUNET_TIME_UNIT_FOREVER_REL, 571 GNUNET_CLIENT_notify_transmit_ready (dns_connection, len,
557 GNUNET_YES, &send_query, NULL); 572 GNUNET_TIME_UNIT_FOREVER_REL,
573 GNUNET_YES, &send_query, NULL);
558 } 574 }
559 else 575 else
560 { 576 {
@@ -625,12 +641,13 @@ message_token (void *cls __attribute__ ((unused)), void *client
625 } 641 }
626 if (me->tunnel == NULL && NULL != cls) 642 if (me->tunnel == NULL && NULL != cls)
627 { 643 {
628 *cls = GNUNET_MESH_tunnel_create(mesh_handle, 644 *cls =
629 initialize_tunnel_state(4, NULL), 645 GNUNET_MESH_tunnel_create (mesh_handle,
630 send_pkt_to_peer, NULL, cls); 646 initialize_tunnel_state (4, NULL),
647 send_pkt_to_peer, NULL, cls);
631 GNUNET_MESH_peer_request_connect_add (*cls, 648 GNUNET_MESH_peer_request_connect_add (*cls,
632 (struct GNUNET_PeerIdentity *) 649 (struct GNUNET_PeerIdentity
633 &me->desc.peer); 650 *) &me->desc.peer);
634 me->tunnel = *cls; 651 me->tunnel = *cls;
635 } 652 }
636 else if (NULL != cls) 653 else if (NULL != cls)
@@ -664,13 +681,15 @@ message_token (void *cls __attribute__ ((unused)), void *client
664 ntohs (pkt->ip_hdr.tot_lngth) - 681 ntohs (pkt->ip_hdr.tot_lngth) -
665 4 * pkt->ip_hdr.hdr_lngth); 682 4 * pkt->ip_hdr.hdr_lngth);
666 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY; 683 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
667 } else 684 }
668 GNUNET_assert (0); 685 else
686 GNUNET_assert (0);
669 if (me->tunnel == NULL && NULL != cls) 687 if (me->tunnel == NULL && NULL != cls)
670 { 688 {
671 *cls = 689 *cls =
672 GNUNET_MESH_tunnel_create(mesh_handle, initialize_tunnel_state(4, NULL), 690 GNUNET_MESH_tunnel_create (mesh_handle,
673 send_pkt_to_peer, NULL, cls); 691 initialize_tunnel_state (4, NULL),
692 send_pkt_to_peer, NULL, cls);
674 693
675 GNUNET_MESH_peer_request_connect_by_type (*cls, app_type); 694 GNUNET_MESH_peer_request_connect_by_type (*cls, app_type);
676 me->tunnel = *cls; 695 me->tunnel = *cls;
diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c
index 1191dffe1..cc8001eae 100644
--- a/src/vpn/gnunet-daemon-vpn.c
+++ b/src/vpn/gnunet-daemon-vpn.c
@@ -183,7 +183,7 @@ collect_mappings (void *cls
183 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */ 183 /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
184 184
185 GNUNET_assert (GNUNET_YES == 185 GNUNET_assert (GNUNET_YES ==
186 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me)); 186 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
187 187
188 GNUNET_free (me); 188 GNUNET_free (me);
189} 189}
@@ -282,21 +282,22 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
282{ 282{
283 struct GNUNET_MESH_Tunnel **tunnel = cls; 283 struct GNUNET_MESH_Tunnel **tunnel = cls;
284 284
285 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel); 285 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
286
286 ts->th = NULL; 287 ts->th = NULL;
287 288
288 if (NULL != buf) 289 if (NULL != buf)
289 { 290 {
290 struct GNUNET_MessageHeader *hdr = 291 struct GNUNET_MessageHeader *hdr =
291 (struct GNUNET_MessageHeader *) (tunnel + 1); 292 (struct GNUNET_MessageHeader *) (tunnel + 1);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, 294 "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
294 size); 295 size);
295 GNUNET_assert (size >= ntohs (hdr->size)); 296 GNUNET_assert (size >= ntohs (hdr->size));
296 memcpy (buf, hdr, ntohs (hdr->size)); 297 memcpy (buf, hdr, ntohs (hdr->size));
297 size = ntohs (hdr->size); 298 size = ntohs (hdr->size);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n"); 299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
299 } 300 }
300 else 301 else
301 size = 0; 302 size = 0;
302 303
@@ -307,9 +308,7 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
307 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element); 308 GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
308 309
309 ts->th = 310 ts->th =
310 GNUNET_MESH_notify_transmit_ready (*tunnel, 311 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
311 GNUNET_NO,
312 42,
313 GNUNET_TIME_relative_divide 312 GNUNET_TIME_relative_divide
314 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 313 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
315 (const struct GNUNET_PeerIdentity *) 314 (const struct GNUNET_PeerIdentity *)
@@ -330,12 +329,14 @@ port_in_ports (uint64_t ports, uint16_t port)
330{ 329{
331 uint16_t *ps = (uint16_t *) & ports; 330 uint16_t *ps = (uint16_t *) & ports;
332 331
333 return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port; 332 return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port ||
333 ps[3] == port;
334} 334}
335 335
336void 336void
337send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer, 337send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
338 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused))) 338 const struct GNUNET_ATS_Information *atsi
339 __attribute__ ((unused)))
339{ 340{
340 /* peer == NULL means that all peers in this request are connected */ 341 /* peer == NULL means that all peers in this request are connected */
341 if (peer == NULL) 342 if (peer == NULL)
@@ -347,18 +348,16 @@ send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
347 GNUNET_assert (NULL != tunnel); 348 GNUNET_assert (NULL != tunnel);
348 GNUNET_assert (NULL != *tunnel); 349 GNUNET_assert (NULL != *tunnel);
349 350
350 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel); 351 struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
352
351 if (NULL == ts->th) 353 if (NULL == ts->th)
352 { 354 {
353 ts->th = 355 ts->th =
354 GNUNET_MESH_notify_transmit_ready (*tunnel, 356 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
355 GNUNET_NO,
356 42,
357 GNUNET_TIME_relative_divide 357 GNUNET_TIME_relative_divide
358 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 358 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
359 (const struct GNUNET_PeerIdentity *) 359 (const struct GNUNET_PeerIdentity *)
360 NULL, 360 NULL, ntohs (hdr->size),
361 ntohs (hdr->size),
362 send_pkt_to_peer_notify_callback, 361 send_pkt_to_peer_notify_callback,
363 cls); 362 cls);
364 } 363 }
@@ -574,7 +573,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
574 573
575 list = 574 list =
576 GNUNET_malloc (htons (pkt->hdr.size) + 575 GNUNET_malloc (htons (pkt->hdr.size) +
577 sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 576 sizeof (struct answer_packet_list) -
577 sizeof (struct answer_packet));
578 578
579 memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); 579 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
580 580
@@ -622,8 +622,9 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
622 char *name = (char *) (map_entry + 1); 622 char *name = (char *) (map_entry + 1);
623 623
624 list = 624 list =
625 GNUNET_malloc (sizeof(struct answer_packet_list) - sizeof(struct answer_packet) + offset + 2 + 625 GNUNET_malloc (sizeof (struct answer_packet_list) -
626 ntohs (namelen)); 626 sizeof (struct answer_packet) + offset + 2 +
627 ntohs (namelen));
627 628
628 struct answer_packet *rpkt = &list->pkt; 629 struct answer_packet *rpkt = &list->pkt;
629 630
@@ -641,7 +642,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
641 { 642 {
642 list = 643 list =
643 GNUNET_malloc (htons (pkt->hdr.size) + 644 GNUNET_malloc (htons (pkt->hdr.size) +
644 sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 645 sizeof (struct answer_packet_list) -
646 sizeof (struct answer_packet));
645 memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); 647 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
646 } 648 }
647 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA) 649 else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
@@ -701,7 +703,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
701 703
702 list = 704 list =
703 GNUNET_malloc (htons (pkt->hdr.size) + 705 GNUNET_malloc (htons (pkt->hdr.size) +
704 sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 706 sizeof (struct answer_packet_list) -
707 sizeof (struct answer_packet));
705 708
706 memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); 709 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
707 } 710 }
@@ -763,7 +766,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
763 766
764 list = 767 list =
765 GNUNET_malloc (htons (pkt->hdr.size) + 768 GNUNET_malloc (htons (pkt->hdr.size) +
766 sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 769 sizeof (struct answer_packet_list) -
770 sizeof (struct answer_packet));
767 771
768 memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); 772 memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
769 } 773 }
@@ -852,12 +856,12 @@ add_additional_port (struct map_entry *me, uint16_t port)
852} 856}
853 857
854static int 858static int
855receive_udp_back (void *cls __attribute__ ((unused)), 859receive_udp_back (void *cls
856 struct GNUNET_MESH_Tunnel *tunnel, 860 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
857 void **tunnel_ctx, 861 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
858 const struct GNUNET_PeerIdentity *sender,
859 const struct GNUNET_MessageHeader *message, 862 const struct GNUNET_MessageHeader *message,
860 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused))) 863 const struct GNUNET_ATS_Information *atsi
864 __attribute__ ((unused)))
861{ 865{
862 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 866 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
863 struct remote_addr *s = (struct remote_addr *) desc; 867 struct remote_addr *s = (struct remote_addr *) desc;
@@ -1029,12 +1033,13 @@ receive_udp_back (void *cls __attribute__ ((unused)),
1029} 1033}
1030 1034
1031static int 1035static int
1032receive_tcp_back (void *cls __attribute__ ((unused)), 1036receive_tcp_back (void *cls
1033 struct GNUNET_MESH_Tunnel *tunnel, 1037 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
1034 void **tunnel_ctx, 1038 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender
1035 const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)), 1039 __attribute__ ((unused)),
1036 const struct GNUNET_MessageHeader *message, 1040 const struct GNUNET_MessageHeader *message,
1037 const struct GNUNET_ATS_Information *atsi __attribute__ ((unused))) 1041 const struct GNUNET_ATS_Information *atsi
1042 __attribute__ ((unused)))
1038{ 1043{
1039 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1); 1044 GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
1040 struct remote_addr *s = (struct remote_addr *) desc; 1045 struct remote_addr *s = (struct remote_addr *) desc;
@@ -1046,9 +1051,10 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
1046 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1051 ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
1047 sizeof (GNUNET_HashCode); 1052 sizeof (GNUNET_HashCode);
1048 1053
1049 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet back, addrlen = %d\n", s->addrlen); 1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
1050 1056
1051 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK || 1057 if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
1052 ts->addrlen == 16) 1058 ts->addrlen == 16)
1053 { 1059 {
1054 size_t size = pktlen + sizeof (struct ip6_tcp) - 1; 1060 size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
@@ -1195,7 +1201,7 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
1195 tmp = pkt4->ip_hdr.dadr; 1201 tmp = pkt4->ip_hdr.dadr;
1196 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4); 1202 sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
1197 1203
1198 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP? 1204 tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
1199 1205
1200 tmp = htonl (tmp); 1206 tmp = htonl (tmp);
1201 1207
@@ -1213,22 +1219,21 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
1213 return GNUNET_OK; 1219 return GNUNET_OK;
1214} 1220}
1215 1221
1216static void* new_tunnel(void* cls, 1222static void *
1217 struct GNUNET_MESH_Tunnel *tunnel, 1223new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1218 const struct GNUNET_PeerIdentity *initiator, 1224 const struct GNUNET_PeerIdentity *initiator,
1219 const struct GNUNET_ATS_Information *atsi) 1225 const struct GNUNET_ATS_Information *atsi)
1220{ 1226{
1221 /* Why should anyone open an inbound tunnel to vpn? */ 1227 /* Why should anyone open an inbound tunnel to vpn? */
1222 GNUNET_break(0); 1228 GNUNET_break (0);
1223 return NULL; 1229 return NULL;
1224} 1230}
1225 1231
1226static void cleaner(void *cls, 1232static void
1227 const struct GNUNET_MESH_Tunnel *tunnel, 1233cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
1228 void *tunnel_ctx)
1229{ 1234{
1230 /* Why should anyone open an inbound tunnel to vpn? */ 1235 /* Why should anyone open an inbound tunnel to vpn? */
1231 GNUNET_break(0); 1236 GNUNET_break (0);
1232} 1237}
1233 1238
1234/** 1239/**
@@ -1255,7 +1260,9 @@ run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
1255 GNUNET_APPLICATION_TYPE_END 1260 GNUNET_APPLICATION_TYPE_END
1256 }; 1261 };
1257 1262
1258 mesh_handle = GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers, types); 1263 mesh_handle =
1264 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
1265 types);
1259 cfg = cfg_; 1266 cfg = cfg_;
1260 restart_hijack = 0; 1267 restart_hijack = 0;
1261 hashmap = GNUNET_CONTAINER_multihashmap_create (65536); 1268 hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
diff --git a/src/vpn/gnunet-dns-parser.c b/src/vpn/gnunet-dns-parser.c
index e3b03fefd..e87109e0c 100644
--- a/src/vpn/gnunet-dns-parser.c
+++ b/src/vpn/gnunet-dns-parser.c
@@ -136,114 +136,126 @@ parse_dns_packet (struct dns_pkt *pkt)
136} /*}}} */ 136} /*}}} */
137 137
138static void 138static void
139unparse_dns_name(char* dest, char* src, size_t len) 139unparse_dns_name (char *dest, char *src, size_t len)
140{ 140{
141 char *b = dest; 141 char *b = dest;
142 char cnt = 0; 142 char cnt = 0;
143
143 dest++; 144 dest++;
144 while (*src != 0) 145 while (*src != 0)
146 {
147 while (*src != '.' && *src != 0)
145 { 148 {
146 while (*src != '.' && *src != 0) 149 *dest = *src;
147 {
148 *dest = *src;
149 src++;
150 dest++;
151 cnt++;
152 }
153 *b = cnt;
154 cnt = 0;
155 b = dest;
156 dest++;
157 src++; 150 src++;
151 dest++;
152 cnt++;
158 } 153 }
154 *b = cnt;
155 cnt = 0;
156 b = dest;
157 dest++;
158 src++;
159 }
159 *b = 0; 160 *b = 0;
160} 161}
161 162
162struct dns_pkt * 163struct dns_pkt *
163unparse_dns_packet (struct dns_pkt_parsed* ppkt) 164unparse_dns_packet (struct dns_pkt_parsed *ppkt)
164{ 165{
165 size_t size = sizeof(struct dns_pkt) - 1; 166 size_t size = sizeof (struct dns_pkt) - 1;
166 int i; 167 int i;
167 for (i = 0; i < ntohs(ppkt->s.qdcount); i++) 168
169 for (i = 0; i < ntohs (ppkt->s.qdcount); i++)
168 size += ppkt->queries[i]->namelen + 1; 170 size += ppkt->queries[i]->namelen + 1;
169 171
170 for (i = 0; i < ntohs(ppkt->s.ancount); i++) 172 for (i = 0; i < ntohs (ppkt->s.ancount); i++)
171 { 173 {
172 size += ppkt->answers[i]->namelen + 1; 174 size += ppkt->answers[i]->namelen + 1;
173 size += ppkt->answers[i]->data_len; 175 size += ppkt->answers[i]->data_len;
174 } 176 }
175 for (i = 0; i < ntohs(ppkt->s.nscount); i++) 177 for (i = 0; i < ntohs (ppkt->s.nscount); i++)
176 { 178 {
177 size += ppkt->nameservers[i]->namelen + 1; 179 size += ppkt->nameservers[i]->namelen + 1;
178 size += ppkt->nameservers[i]->data_len; 180 size += ppkt->nameservers[i]->data_len;
179 } 181 }
180 for (i = 0; i < ntohs(ppkt->s.arcount); i++) 182 for (i = 0; i < ntohs (ppkt->s.arcount); i++)
181 { 183 {
182 size += ppkt->additional[i]->namelen + 1; 184 size += ppkt->additional[i]->namelen + 1;
183 size += ppkt->additional[i]->data_len; 185 size += ppkt->additional[i]->data_len;
184 } 186 }
187
188 size +=
189 4 * ntohs (ppkt->s.qdcount) + 10 * (ntohs (ppkt->s.ancount) +
190 ntohs (ppkt->s.arcount) +
191 ntohs (ppkt->s.nscount));
185 192
186 size += 4*ntohs(ppkt->s.qdcount) + 10*( 193 struct dns_pkt *pkt = GNUNET_malloc (size);
187 ntohs(ppkt->s.ancount)+ 194 char *pkt_c = (char *) pkt;
188 ntohs(ppkt->s.arcount)+
189 ntohs(ppkt->s.nscount));
190 195
191 struct dns_pkt *pkt = GNUNET_malloc(size); 196 memcpy (&pkt->s, &ppkt->s, sizeof ppkt->s);
192 char *pkt_c = (char*)pkt;
193 memcpy(&pkt->s, &ppkt->s, sizeof ppkt->s);
194 size_t idx = sizeof ppkt->s; 197 size_t idx = sizeof ppkt->s;
195 198
196 for (i = 0; i < ntohs(ppkt->s.qdcount); i++) 199 for (i = 0; i < ntohs (ppkt->s.qdcount); i++)
197 { 200 {
198 unparse_dns_name(&pkt_c[idx], ppkt->queries[i]->name, ppkt->queries[i]->namelen); 201 unparse_dns_name (&pkt_c[idx], ppkt->queries[i]->name,
199 idx += ppkt->queries[i]->namelen; 202 ppkt->queries[i]->namelen);
200 struct dns_query_line *d = (struct dns_query_line*)&pkt_c[idx]; 203 idx += ppkt->queries[i]->namelen;
201 d->class = ppkt->queries[i]->qclass; 204 struct dns_query_line *d = (struct dns_query_line *) &pkt_c[idx];
202 d->type = ppkt->queries[i]->qtype; 205
203 idx += sizeof(struct dns_query_line); 206 d->class = ppkt->queries[i]->qclass;
204 } 207 d->type = ppkt->queries[i]->qtype;
208 idx += sizeof (struct dns_query_line);
209 }
205 210
206 for (i = 0; i < ntohs(ppkt->s.ancount); i++) 211 for (i = 0; i < ntohs (ppkt->s.ancount); i++)
207 { 212 {
208 unparse_dns_name(&pkt_c[idx], ppkt->answers[i]->name, ppkt->answers[i]->namelen); 213 unparse_dns_name (&pkt_c[idx], ppkt->answers[i]->name,
209 idx += ppkt->answers[i]->namelen; 214 ppkt->answers[i]->namelen);
210 struct dns_record_line *r = (struct dns_record_line*)&pkt_c[idx]; 215 idx += ppkt->answers[i]->namelen;
211 r->type = ppkt->answers[i]->type; 216 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
212 r->class = ppkt->answers[i]->class; 217
213 r->ttl = ppkt->answers[i]->ttl; 218 r->type = ppkt->answers[i]->type;
214 r->data_len = ppkt->answers[i]->data_len; 219 r->class = ppkt->answers[i]->class;
215 idx += sizeof(struct dns_record_line); 220 r->ttl = ppkt->answers[i]->ttl;
216 memcpy(&r->data, ppkt->answers[i]->data, ppkt->answers[i]->data_len); 221 r->data_len = ppkt->answers[i]->data_len;
217 idx += ppkt->answers[i]->data_len; 222 idx += sizeof (struct dns_record_line);
218 } 223 memcpy (&r->data, ppkt->answers[i]->data, ppkt->answers[i]->data_len);
224 idx += ppkt->answers[i]->data_len;
225 }
219 226
220 for (i = 0; i < ntohs(ppkt->s.nscount); i++) 227 for (i = 0; i < ntohs (ppkt->s.nscount); i++)
221 { 228 {
222 unparse_dns_name(&pkt_c[idx], ppkt->nameservers[i]->name, ppkt->nameservers[i]->namelen); 229 unparse_dns_name (&pkt_c[idx], ppkt->nameservers[i]->name,
223 idx += ppkt->nameservers[i]->namelen; 230 ppkt->nameservers[i]->namelen);
224 struct dns_record_line *r = (struct dns_record_line*)&pkt_c[idx]; 231 idx += ppkt->nameservers[i]->namelen;
225 r->type = ppkt->nameservers[i]->type; 232 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
226 r->class = ppkt->nameservers[i]->class; 233
227 r->ttl = ppkt->nameservers[i]->ttl; 234 r->type = ppkt->nameservers[i]->type;
228 r->data_len = ppkt->nameservers[i]->data_len; 235 r->class = ppkt->nameservers[i]->class;
229 idx += sizeof(struct dns_record_line); 236 r->ttl = ppkt->nameservers[i]->ttl;
230 memcpy(&r->data, ppkt->nameservers[i]->data, ppkt->nameservers[i]->data_len); 237 r->data_len = ppkt->nameservers[i]->data_len;
231 idx += ppkt->nameservers[i]->data_len; 238 idx += sizeof (struct dns_record_line);
232 } 239 memcpy (&r->data, ppkt->nameservers[i]->data,
240 ppkt->nameservers[i]->data_len);
241 idx += ppkt->nameservers[i]->data_len;
242 }
233 243
234 for (i = 0; i < ntohs(ppkt->s.arcount); i++) 244 for (i = 0; i < ntohs (ppkt->s.arcount); i++)
235 { 245 {
236 unparse_dns_name(&pkt_c[idx], ppkt->additional[i]->name, ppkt->additional[i]->namelen); 246 unparse_dns_name (&pkt_c[idx], ppkt->additional[i]->name,
237 idx += ppkt->additional[i]->namelen; 247 ppkt->additional[i]->namelen);
238 struct dns_record_line *r = (struct dns_record_line*)&pkt_c[idx]; 248 idx += ppkt->additional[i]->namelen;
239 r->type = ppkt->additional[i]->type; 249 struct dns_record_line *r = (struct dns_record_line *) &pkt_c[idx];
240 r->class = ppkt->additional[i]->class; 250
241 r->ttl = ppkt->additional[i]->ttl; 251 r->type = ppkt->additional[i]->type;
242 r->data_len = ppkt->additional[i]->data_len; 252 r->class = ppkt->additional[i]->class;
243 idx += sizeof(struct dns_record_line); 253 r->ttl = ppkt->additional[i]->ttl;
244 memcpy(&r->data, ppkt->additional[i]->data, ppkt->additional[i]->data_len); 254 r->data_len = ppkt->additional[i]->data_len;
245 idx += ppkt->additional[i]->data_len; 255 idx += sizeof (struct dns_record_line);
246 } 256 memcpy (&r->data, ppkt->additional[i]->data, ppkt->additional[i]->data_len);
257 idx += ppkt->additional[i]->data_len;
258 }
247 259
248 return pkt; 260 return pkt;
249} 261}
diff --git a/src/vpn/gnunet-service-dns-p.h b/src/vpn/gnunet-service-dns-p.h
index 13b410e2f..0519b1f84 100644
--- a/src/vpn/gnunet-service-dns-p.h
+++ b/src/vpn/gnunet-service-dns-p.h
@@ -102,7 +102,8 @@ struct answer_packet
102 unsigned char data[1]; 102 unsigned char data[1];
103}; 103};
104 104
105struct answer_packet_list { 105struct answer_packet_list
106{
106 struct answer_packet_list *next GNUNET_PACKED; 107 struct answer_packet_list *next GNUNET_PACKED;
107 struct answer_packet_list *prev GNUNET_PACKED; 108 struct answer_packet_list *prev GNUNET_PACKED;
108 struct GNUNET_SERVER_Client *client; 109 struct GNUNET_SERVER_Client *client;
diff --git a/src/vpn/gnunet-service-dns.c b/src/vpn/gnunet-service-dns.c
index 489235ed4..c8d790b56 100644
--- a/src/vpn/gnunet-service-dns.c
+++ b/src/vpn/gnunet-service-dns.c
@@ -99,7 +99,7 @@ static struct
99 char *name; 99 char *name;
100 uint8_t namelen; 100 uint8_t namelen;
101 uint16_t qtype; 101 uint16_t qtype;
102} query_states[UINT16_MAX+1]; 102} query_states[UINT16_MAX + 1];
103 103
104/** 104/**
105 * A struct used to give more than one value as 105 * A struct used to give more than one value as
@@ -122,39 +122,45 @@ struct tunnel_notify_queue
122 122
123struct tunnel_state 123struct tunnel_state
124{ 124{
125 struct tunnel_notify_queue *head,*tail; 125 struct tunnel_notify_queue *head, *tail;
126 struct GNUNET_MESH_TransmitHandle *th; 126 struct GNUNET_MESH_TransmitHandle *th;
127}; 127};
128 128
129static size_t send_answer (void *cls, size_t size, void *buf); 129static size_t
130send_answer (void *cls, size_t size, void *buf);
130 131
131static void 132static void
132client_disconnect(void* cls, struct GNUNET_SERVER_Client *client) 133client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
133{ 134{
134 if (NULL == head) return; 135 if (NULL == head)
136 return;
135 137
136 if (head->client == client) 138 if (head->client == client)
137 { 139 {
138 GNUNET_CONNECTION_notify_transmit_ready_cancel(server_notify); 140 GNUNET_CONNECTION_notify_transmit_ready_cancel (server_notify);
139 server_notify = GNUNET_SERVER_notify_transmit_ready (head->next->client, ntohs (head->next->pkt.hdr.size), 141 server_notify =
140 GNUNET_TIME_UNIT_FOREVER_REL, 142 GNUNET_SERVER_notify_transmit_ready (head->next->client,
141 &send_answer, NULL); 143 ntohs (head->next->pkt.hdr.size),
142 } 144 GNUNET_TIME_UNIT_FOREVER_REL,
145 &send_answer, NULL);
146 }
143 147
144 struct answer_packet_list *element = head; 148 struct answer_packet_list *element = head;
149
145 while (element != NULL) 150 while (element != NULL)
151 {
152 if (element->client == client)
146 { 153 {
147 if (element->client == client) 154 GNUNET_SERVER_client_drop (client);
148 { 155 GNUNET_CONTAINER_DLL_remove (head, tail, element);
149 GNUNET_SERVER_client_drop(client); 156 struct answer_packet_list *t = element;
150 GNUNET_CONTAINER_DLL_remove(head, tail, element); 157
151 struct answer_packet_list* t = element; 158 element = element->next;
152 element = element->next; 159 GNUNET_free (t);
153 GNUNET_free(t);
154 }
155 else
156 element = element->next;
157 } 160 }
161 else
162 element = element->next;
163 }
158} 164}
159 165
160/** 166/**
@@ -201,24 +207,26 @@ hijack (void *cls
201} 207}
202 208
203static void * 209static void *
204new_tunnel (void *cls __attribute__((unused)), 210new_tunnel (void *cls
205 struct GNUNET_MESH_Tunnel *tunnel, 211 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
206 const struct GNUNET_PeerIdentity *initiator __attribute__((unused)), 212 const struct GNUNET_PeerIdentity *initiator
207 const struct GNUNET_ATS_Information *ats __attribute__((unused))) 213 __attribute__ ((unused)), const struct GNUNET_ATS_Information *ats
214 __attribute__ ((unused)))
208{ 215{
209 struct tunnel_state *s = GNUNET_malloc(sizeof *s); 216 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
210 s->head = NULL; 217
211 s->tail = NULL; 218 s->head = NULL;
212 s->th = NULL; 219 s->tail = NULL;
213 return s; 220 s->th = NULL;
221 return s;
214} 222}
215 223
216static void 224static void
217clean_tunnel (void *cls __attribute__((unused)), 225clean_tunnel (void *cls
218 const struct GNUNET_MESH_Tunnel *tunnel, 226 __attribute__ ((unused)), const struct GNUNET_MESH_Tunnel *tunnel,
219 void *tunnel_ctx) 227 void *tunnel_ctx)
220{ 228{
221 GNUNET_free(tunnel_ctx); 229 GNUNET_free (tunnel_ctx);
222} 230}
223 231
224/** 232/**
@@ -246,10 +254,10 @@ unhijack (unsigned short port)
246 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns", 254 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
247 "gnunet-hijack-dns", "-d", port_s, virt_dns, 255 "gnunet-hijack-dns", "-d", port_s, virt_dns,
248 NULL))) 256 NULL)))
249 { 257 {
250 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc)); 258 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
251 GNUNET_OS_process_close (proc); 259 GNUNET_OS_process_close (proc);
252 } 260 }
253 GNUNET_free (virt_dns); 261 GNUNET_free (virt_dns);
254} 262}
255 263
@@ -272,11 +280,13 @@ send_answer (void *cls, size_t size, void *buf)
272 280
273 /* When more data is to be sent, reschedule */ 281 /* When more data is to be sent, reschedule */
274 if (head != NULL) 282 if (head != NULL)
275 server_notify = GNUNET_SERVER_notify_transmit_ready (head->client, ntohs (head->pkt.hdr.size), 283 server_notify =
276 GNUNET_TIME_UNIT_FOREVER_REL, 284 GNUNET_SERVER_notify_transmit_ready (head->client,
277 &send_answer, NULL); 285 ntohs (head->pkt.hdr.size),
286 GNUNET_TIME_UNIT_FOREVER_REL,
287 &send_answer, NULL);
278 288
279 GNUNET_SERVER_client_drop(query->client); 289 GNUNET_SERVER_client_drop (query->client);
280 GNUNET_free (query); 290 GNUNET_free (query);
281 return len; 291 return len;
282} 292}
@@ -311,7 +321,8 @@ mesh_send_response (void *cls, size_t size, void *buf)
311 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader))); 321 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
312 322
313 memcpy (hdr + 1, dns, *sz); 323 memcpy (hdr + 1, dns, *sz);
314 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data(*tunnel); 324 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);
325
315 if (NULL != s->head) 326 if (NULL != s->head)
316 { 327 {
317 struct tunnel_notify_queue *element = s->head; 328 struct tunnel_notify_queue *element = s->head;
@@ -321,14 +332,12 @@ mesh_send_response (void *cls, size_t size, void *buf)
321 GNUNET_CONTAINER_DLL_remove (head, tail, element); 332 GNUNET_CONTAINER_DLL_remove (head, tail, element);
322 333
323 s->th = 334 s->th =
324 GNUNET_MESH_notify_transmit_ready (*tunnel, 335 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
325 GNUNET_NO,
326 42,
327 GNUNET_TIME_relative_divide 336 GNUNET_TIME_relative_divide
328 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 337 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
329 (const struct GNUNET_PeerIdentity *) 338 (const struct GNUNET_PeerIdentity *)
330 NULL, element->len, 339 NULL, element->len, element->cb,
331 element->cb, element->cls); 340 element->cls);
332 } 341 }
333 342
334 GNUNET_free (cls); 343 GNUNET_free (cls);
@@ -350,27 +359,23 @@ mesh_send (void *cls, size_t size, void *buf)
350 359
351 memcpy (buf, &cls_->hdr, size); 360 memcpy (buf, &cls_->hdr, size);
352 361
353 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data(cls_->tunnel); 362 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
363
354 if (NULL != s->head) 364 if (NULL != s->head)
355 { 365 {
356 struct tunnel_notify_queue *element = 366 struct tunnel_notify_queue *element = s->head;
357 s->head; 367 struct tunnel_notify_queue *head = s->head;
358 struct tunnel_notify_queue *head = 368 struct tunnel_notify_queue *tail = s->tail;;
359 s->head;
360 struct tunnel_notify_queue *tail =
361 s->tail;;
362 369
363 GNUNET_CONTAINER_DLL_remove (head, tail, element); 370 GNUNET_CONTAINER_DLL_remove (head, tail, element);
364 371
365 s->th = 372 s->th =
366 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, 373 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_NO, 42,
367 GNUNET_NO,
368 42,
369 GNUNET_TIME_relative_divide 374 GNUNET_TIME_relative_divide
370 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 375 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
371 (const struct GNUNET_PeerIdentity *) 376 (const struct GNUNET_PeerIdentity *)
372 NULL, element->len, 377 NULL, element->len, element->cb,
373 element->cb, element->cls); 378 element->cls);
374 379
375 GNUNET_free (element); 380 GNUNET_free (element);
376 } 381 }
@@ -392,26 +397,20 @@ mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
392 "Connected to peer %s, %x, sending query with id %d\n", 397 "Connected to peer %s, %x, sending query with id %d\n",
393 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id)); 398 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
394 399
395 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data(cls_->tunnel); 400 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
401
396 if (NULL == s->head) 402 if (NULL == s->head)
397 { 403 {
398 s->th = 404 s->th =
399 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, 405 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_YES, 42,
400 GNUNET_YES, 406 GNUNET_TIME_UNIT_MINUTES, NULL,
401 42, 407 cls_->hdr.size, mesh_send, cls);
402 GNUNET_TIME_UNIT_MINUTES,
403 NULL,
404 cls_->hdr.size,
405 mesh_send,
406 cls);
407 408
408 } 409 }
409 else 410 else
410 { 411 {
411 struct tunnel_notify_queue *head = 412 struct tunnel_notify_queue *head = s->head;
412 s->head; 413 struct tunnel_notify_queue *tail = s->tail;
413 struct tunnel_notify_queue *tail =
414 s->tail;
415 414
416 struct tunnel_notify_queue *element = 415 struct tunnel_notify_queue *element =
417 GNUNET_malloc (sizeof (struct tunnel_notify_queue)); 416 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
@@ -432,13 +431,14 @@ send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
432 431
433 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls; 432 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
434 433
435 struct tunnel_state *s = GNUNET_malloc(sizeof *s); 434 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
435
436 s->head = NULL; 436 s->head = NULL;
437 s->tail = NULL; 437 s->tail = NULL;
438 s->th = NULL; 438 s->th = NULL;
439 439
440 cls_->tunnel = 440 cls_->tunnel =
441 GNUNET_MESH_tunnel_create(mesh_handle, s, mesh_connect, NULL, cls_); 441 GNUNET_MESH_tunnel_create (mesh_handle, s, mesh_connect, NULL, cls_);
442 442
443 GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel, 443 GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel,
444 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER); 444 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER);
@@ -461,12 +461,15 @@ receive_mesh_query (void *cls
461 461
462 struct sockaddr_in dest; 462 struct sockaddr_in dest;
463 463
464 struct dns_pkt_parsed *pdns = parse_dns_packet(dns); 464 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
465 465
466 memset (&dest, 0, sizeof dest); 466 memset (&dest, 0, sizeof dest);
467 dest.sin_port = htons (53); 467 dest.sin_port = htons (53);
468 char *dns_resolver; 468 char *dns_resolver;
469 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "dns", "EXTERNAL_DNS", &dns_resolver) || 469
470 if (GNUNET_OK !=
471 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "EXTERNAL_DNS",
472 &dns_resolver) ||
470 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr)) 473 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr))
471 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr); 474 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
472 475
@@ -476,16 +479,17 @@ receive_mesh_query (void *cls
476 query_states[dns->s.id].valid = GNUNET_YES; 479 query_states[dns->s.id].valid = GNUNET_YES;
477 480
478 int i; 481 int i;
479 for (i= 0; i < ntohs(pdns->s.qdcount); i++) 482
483 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
484 {
485 if (pdns->queries[i]->qtype == htons (28) ||
486 pdns->queries[i]->qtype == htons (1))
480 { 487 {
481 if (pdns->queries[i]->qtype == htons(28) || 488 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
482 pdns->queries[i]->qtype == htons(1)) 489 break;
483 {
484 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
485 break;
486 }
487 } 490 }
488 free_parsed_dns_packet(pdns); 491 }
492 free_parsed_dns_packet (pdns);
489 493
490 GNUNET_NETWORK_socket_sendto (dnsout, dns, 494 GNUNET_NETWORK_socket_sendto (dnsout, dns,
491 ntohs (message->size) - 495 ntohs (message->size) -
@@ -528,7 +532,8 @@ receive_mesh_answer (void *cls
528 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ 532 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
529 533
530 struct answer_packet_list *answer = 534 struct answer_packet_list *answer =
531 GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 535 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
536 sizeof (struct answer_packet));
532 537
533 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 538 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
534 answer->pkt.hdr.size = htons (len); 539 answer->pkt.hdr.size = htons (len);
@@ -546,17 +551,18 @@ receive_mesh_answer (void *cls
546 551
547 int i = 0; 552 int i = 0;
548 553
549 while (i < ntohs(pdns->s.ancount) && 554 while (i < ntohs (pdns->s.ancount) && ntohs (pdns->answers[i]->type) != 28 &&
550 ntohs(pdns->answers[i]->type) != 28 && 555 ntohs (pdns->answers[i]->type) != 1)
551 ntohs(pdns->answers[i]->type) != 1) 556 {
552{ 557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n",
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n", ntohs(pdns->answers[i]->type)); 558 ntohs (pdns->answers[i]->type));
554 i++; 559 i++;
555} 560 }
556 561
557 if (i >= ntohs (pdns->s.ancount)) 562 if (i >= ntohs (pdns->s.ancount))
558 { 563 {
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer does not contain any usable answers.\n"); 564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Answer does not contain any usable answers.\n");
560 free_parsed_dns_packet (pdns); 566 free_parsed_dns_packet (pdns);
561 GNUNET_free (answer); 567 GNUNET_free (answer);
562 return GNUNET_OK; 568 return GNUNET_OK;
@@ -568,8 +574,10 @@ receive_mesh_answer (void *cls
568 memcpy (answer->pkt.addr, pdns->answers[i]->data, 574 memcpy (answer->pkt.addr, pdns->answers[i]->data,
569 ntohs (pdns->answers[i]->data_len)); 575 ntohs (pdns->answers[i]->data_len));
570 576
571 memcpy(answer->pkt.from, query_states[dns->s.id].remote_ip, query_states[dns->s.id].addrlen); 577 memcpy (answer->pkt.from, query_states[dns->s.id].remote_ip,
572 memcpy(answer->pkt.to, query_states[dns->s.id].local_ip, query_states[dns->s.id].addrlen); 578 query_states[dns->s.id].addrlen);
579 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip,
580 query_states[dns->s.id].addrlen);
573 answer->pkt.addrlen = query_states[dns->s.id].addrlen; 581 answer->pkt.addrlen = query_states[dns->s.id].addrlen;
574 answer->pkt.dst_port = query_states[dns->s.id].local_port; 582 answer->pkt.dst_port = query_states[dns->s.id].local_port;
575 583
@@ -595,14 +603,14 @@ receive_mesh_answer (void *cls
595 (struct dns_record_line *) (dpkt->data + 603 (struct dns_record_line *) (dpkt->data +
596 (query_states[dns->s.id].namelen) + 604 (query_states[dns->s.id].namelen) +
597 sizeof (struct dns_query_line) + 2); 605 sizeof (struct dns_query_line) + 2);
598 if (htons(28) == query_states[dns->s.id].qtype) 606 if (htons (28) == query_states[dns->s.id].qtype)
599 { 607 {
600 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA; 608 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
601 dque->type = htons (28); /* AAAA */ 609 dque->type = htons (28); /* AAAA */
602 drec_data->type = htons (28); /* AAAA */ 610 drec_data->type = htons (28); /* AAAA */
603 drec_data->data_len = htons (16); 611 drec_data->data_len = htons (16);
604 } 612 }
605 else if (htons(1) == query_states[dns->s.id].qtype) 613 else if (htons (1) == query_states[dns->s.id].qtype)
606 { 614 {
607 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A; 615 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
608 dque->type = htons (1); /* A */ 616 dque->type = htons (1); /* A */
@@ -610,10 +618,11 @@ receive_mesh_answer (void *cls
610 drec_data->data_len = htons (4); 618 drec_data->data_len = htons (4);
611 } 619 }
612 else 620 else
613 { 621 {
614 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n", query_states[dns->s.id].qtype); 622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n",
615 GNUNET_assert(0); 623 query_states[dns->s.id].qtype);
616 } 624 GNUNET_assert (0);
625 }
617 dque->class = htons (1); /* IN */ 626 dque->class = htons (1); /* IN */
618 627
619 char *anname = 628 char *anname =
@@ -634,10 +643,10 @@ receive_mesh_answer (void *cls
634 answer->client = query_states[dns->s.id].client; 643 answer->client = query_states[dns->s.id].client;
635 644
636 if (server_notify == NULL) 645 if (server_notify == NULL)
637 server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, len, 646 server_notify =
638 GNUNET_TIME_UNIT_FOREVER_REL, 647 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
639 &send_answer, 648 len, GNUNET_TIME_UNIT_FOREVER_REL,
640 NULL); 649 &send_answer, NULL);
641 650
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Sent answer of length %d on to client, addroffset = %d\n", len, 652 "Sent answer of length %d on to client, addroffset = %d\n", len,
@@ -672,14 +681,16 @@ send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
672 2 /* We do not know the lenght of the answer yet */ ; 681 2 /* We do not know the lenght of the answer yet */ ;
673 682
674 struct answer_packet_list *answer = 683 struct answer_packet_list *answer =
675 GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 684 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
685 sizeof (struct answer_packet));
676 686
677 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 687 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
678 answer->pkt.hdr.size = htons (len); 688 answer->pkt.hdr.size = htons (len);
679 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV; 689 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
680 690
681 memcpy(answer->pkt.from, query_states[id].remote_ip, query_states[id].addrlen); 691 memcpy (answer->pkt.from, query_states[id].remote_ip,
682 memcpy(answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen); 692 query_states[id].addrlen);
693 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
683 694
684 answer->pkt.dst_port = query_states[id].local_port; 695 answer->pkt.dst_port = query_states[id].local_port;
685 696
@@ -728,9 +739,10 @@ send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
728 answer->client = query_states[id].client; 739 answer->client = query_states[id].client;
729 740
730 if (server_notify == NULL) 741 if (server_notify == NULL)
731 server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len, 742 server_notify =
732 GNUNET_TIME_UNIT_FOREVER_REL, 743 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
733 &send_answer, NULL); 744 GNUNET_TIME_UNIT_FOREVER_REL,
745 &send_answer, NULL);
734} 746}
735 747
736/** 748/**
@@ -740,11 +752,12 @@ static void
740receive_dht (void *cls, struct GNUNET_TIME_Absolute exp 752receive_dht (void *cls, struct GNUNET_TIME_Absolute exp
741 __attribute__ ((unused)), const GNUNET_HashCode * key 753 __attribute__ ((unused)), const GNUNET_HashCode * key
742 __attribute__ ((unused)), 754 __attribute__ ((unused)),
743 const struct GNUNET_PeerIdentity *get_path __attribute__ ((unused)), 755 const struct GNUNET_PeerIdentity *get_path
744 unsigned int get_path_length __attribute__ ((unused)), 756 __attribute__ ((unused)), unsigned int get_path_length
745 const struct GNUNET_PeerIdentity *put_path __attribute__ ((unused)), 757 __attribute__ ((unused)),
746 unsigned int put_path_length __attribute__ ((unused)), 758 const struct GNUNET_PeerIdentity *put_path
747 enum GNUNET_BLOCK_Type type, size_t size, 759 __attribute__ ((unused)), unsigned int put_path_length
760 __attribute__ ((unused)), enum GNUNET_BLOCK_Type type, size_t size,
748 const void *data) 761 const void *data)
749{ 762{
750 763
@@ -773,8 +786,8 @@ receive_dht (void *cls, struct GNUNET_TIME_Absolute exp
773 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ 786 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
774 787
775 struct answer_packet_list *answer = 788 struct answer_packet_list *answer =
776 GNUNET_malloc (len + 789 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
777 sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); 790 sizeof (struct answer_packet));
778 791
779 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 792 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
780 answer->pkt.hdr.size = htons (len); 793 answer->pkt.hdr.size = htons (len);
@@ -792,8 +805,9 @@ receive_dht (void *cls, struct GNUNET_TIME_Absolute exp
792 memcpy (&answer->pkt.service_descr.ports, &rec->ports, 805 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
793 sizeof (answer->pkt.service_descr.ports)); 806 sizeof (answer->pkt.service_descr.ports));
794 807
795 memcpy(answer->pkt.from, query_states[id].remote_ip, query_states[id].addrlen); 808 memcpy (answer->pkt.from, query_states[id].remote_ip,
796 memcpy(answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen); 809 query_states[id].addrlen);
810 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
797 answer->pkt.addrlen = query_states[id].addrlen; 811 answer->pkt.addrlen = query_states[id].addrlen;
798 812
799 answer->pkt.dst_port = query_states[id].local_port; 813 answer->pkt.dst_port = query_states[id].local_port;
@@ -844,9 +858,10 @@ receive_dht (void *cls, struct GNUNET_TIME_Absolute exp
844 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); 858 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
845 859
846 if (server_notify == NULL) 860 if (server_notify == NULL)
847 server_notify = GNUNET_SERVER_notify_transmit_ready (answer->client, len, 861 server_notify =
848 GNUNET_TIME_UNIT_FOREVER_REL, 862 GNUNET_SERVER_notify_transmit_ready (answer->client, len,
849 &send_answer, NULL); 863 GNUNET_TIME_UNIT_FOREVER_REL,
864 &send_answer, NULL);
850} 865}
851 866
852/** 867/**
@@ -877,11 +892,11 @@ receive_query (void *cls
877 892
878 query_states[dns->s.id].valid = GNUNET_YES; 893 query_states[dns->s.id].valid = GNUNET_YES;
879 query_states[dns->s.id].client = client; 894 query_states[dns->s.id].client = client;
880 GNUNET_SERVER_client_keep(client); 895 GNUNET_SERVER_client_keep (client);
881 memcpy(query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen); 896 memcpy (query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen);
882 query_states[dns->s.id].addrlen = pkt->addrlen; 897 query_states[dns->s.id].addrlen = pkt->addrlen;
883 query_states[dns->s.id].local_port = pkt->src_port; 898 query_states[dns->s.id].local_port = pkt->src_port;
884 memcpy(query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen); 899 memcpy (query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen);
885 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1; 900 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
886 if (query_states[dns->s.id].name != NULL) 901 if (query_states[dns->s.id].name != NULL)
887 GNUNET_free (query_states[dns->s.id].name); 902 GNUNET_free (query_states[dns->s.id].name);
@@ -891,15 +906,16 @@ receive_query (void *cls
891 query_states[dns->s.id].namelen); 906 query_states[dns->s.id].namelen);
892 907
893 int i; 908 int i;
894 for (i= 0; i < ntohs(pdns->s.qdcount); i++) 909
910 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
911 {
912 if (pdns->queries[i]->qtype == htons (28) ||
913 pdns->queries[i]->qtype == htons (1))
895 { 914 {
896 if (pdns->queries[i]->qtype == htons(28) || 915 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
897 pdns->queries[i]->qtype == htons(1)) 916 break;
898 {
899 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
900 break;
901 }
902 } 917 }
918 }
903 919
904 /* The query is for a .gnunet-address */ 920 /* The query is for a .gnunet-address */
905 if (pdns->queries[0]->namelen > 9 && 921 if (pdns->queries[0]->namelen > 9 &&
@@ -922,8 +938,8 @@ receive_query (void *cls
922 cls->handle = 938 cls->handle =
923 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES, 939 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
924 GNUNET_BLOCK_TYPE_DNS, &key, 940 GNUNET_BLOCK_TYPE_DNS, &key,
925 5 /* DEFAULT_GET_REPLICATION */, GNUNET_DHT_RO_NONE, NULL, 941 5 /* DEFAULT_GET_REPLICATION */ ,
926 0, &receive_dht, cls); 942 GNUNET_DHT_RO_NONE, NULL, 0, &receive_dht, cls);
927 943
928 goto outfree; 944 goto outfree;
929 } 945 }
@@ -992,53 +1008,55 @@ receive_query (void *cls
992 unsigned char virt_dns_bytes[16]; 1008 unsigned char virt_dns_bytes[16];
993 1009
994 if (pkt->addrlen == 4) 1010 if (pkt->addrlen == 4)
995 { 1011 {
996 char *virt_dns; 1012 char *virt_dns;
997
998 if (GNUNET_SYSERR ==
999 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
1000 {
1001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1002 "No entry 'VIRTDNS' in configuration!\n");
1003 exit (1);
1004 }
1005
1006 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
1007 {
1008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
1009 virt_dns);
1010 exit (1);
1011 }
1012 1013
1013 GNUNET_free (virt_dns); 1014 if (GNUNET_SYSERR ==
1014 } 1015 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
1015 else if (pkt->addrlen == 16) 1016 &virt_dns))
1016 { 1017 {
1017 char *virt_dns; 1018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1019 "No entry 'VIRTDNS' in configuration!\n");
1020 exit (1);
1021 }
1018 1022
1019 if (GNUNET_SYSERR == 1023 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
1020 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6", &virt_dns)) 1024 {
1021 { 1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
1022 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1026 virt_dns);
1023 "No entry 'VIRTDNS6' in configuration!\n"); 1027 exit (1);
1024 exit (1); 1028 }
1025 }
1026 1029
1027 if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes)) 1030 GNUNET_free (virt_dns);
1028 { 1031 }
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS6': %s; %m!\n", 1032 else if (pkt->addrlen == 16)
1030 virt_dns); 1033 {
1031 exit (1); 1034 char *virt_dns;
1032 }
1033 1035
1034 GNUNET_free (virt_dns); 1036 if (GNUNET_SYSERR ==
1037 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6",
1038 &virt_dns))
1039 {
1040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1041 "No entry 'VIRTDNS6' in configuration!\n");
1042 exit (1);
1035 } 1043 }
1036 else 1044
1045 if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes))
1037 { 1046 {
1038 GNUNET_assert(0); 1047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1048 "Error parsing 'VIRTDNS6': %s; %m!\n", virt_dns);
1049 exit (1);
1039 } 1050 }
1040 1051
1041 if (memcmp(virt_dns_bytes,pkt->orig_to, pkt->addrlen) == 0) 1052 GNUNET_free (virt_dns);
1053 }
1054 else
1055 {
1056 GNUNET_assert (0);
1057 }
1058
1059 if (memcmp (virt_dns_bytes, pkt->orig_to, pkt->addrlen) == 0)
1042 { 1060 {
1043 /* This is a packet that was sent directly to the virtual dns-server 1061 /* This is a packet that was sent directly to the virtual dns-server
1044 * 1062 *
@@ -1061,36 +1079,36 @@ receive_query (void *cls
1061 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader)); 1079 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1062 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_); 1080 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1063 1081
1064 if (ntohs(pdns->s.qdcount) == 1) 1082 if (ntohs (pdns->s.qdcount) == 1)
1083 {
1084 if (ntohs (pdns->queries[0]->qtype) == 1)
1085 pdns->queries[0]->qtype = htons (28);
1086 else if (ntohs (pdns->queries[0]->qtype) == 28)
1087 pdns->queries[0]->qtype = htons (1);
1088 else
1065 { 1089 {
1066 if (ntohs(pdns->queries[0]->qtype) == 1) 1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n");
1067 pdns->queries[0]->qtype = htons(28); 1091 goto outfree;
1068 else if (ntohs(pdns->queries[0]->qtype) == 28) 1092 }
1069 pdns->queries[0]->qtype = htons(1); 1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n");
1070 else 1094 struct dns_pkt *rdns = unparse_dns_packet (pdns);
1071{ 1095 size_t size =
1072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n");
1073 goto outfree;
1074}
1075 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n");
1076 struct dns_pkt *rdns = unparse_dns_packet (pdns);
1077 size_t size =
1078 sizeof (struct GNUNET_MESH_Tunnel *) + 1096 sizeof (struct GNUNET_MESH_Tunnel *) +
1079 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) - 1097 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1080 sizeof (struct query_packet) + 1098 sizeof (struct query_packet) +
1081 1); 1099 1);
1082 struct tunnel_cls *cls_ = GNUNET_malloc (size); 1100 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1083 1101
1084 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *); 1102 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1085 1103
1086 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS); 1104 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size); 1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1088 1106
1089 memcpy (&cls_->dns, rdns, 1107 memcpy (&cls_->dns, rdns,
1090 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader)); 1108 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1091 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_); 1109 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1092 GNUNET_free (rdns); 1110 GNUNET_free (rdns);
1093 } 1111 }
1094 1112
1095 goto outfree; 1113 goto outfree;
1096 } 1114 }
@@ -1098,31 +1116,31 @@ receive_query (void *cls
1098 1116
1099 /* The query should be sent to the network */ 1117 /* The query should be sent to the network */
1100 if (pkt->addrlen == 4) 1118 if (pkt->addrlen == 4)
1101 { 1119 {
1102 struct sockaddr_in dest; 1120 struct sockaddr_in dest;
1103 1121
1104 memset (&dest, 0, sizeof dest); 1122 memset (&dest, 0, sizeof dest);
1105 dest.sin_port = htons (53); 1123 dest.sin_port = htons (53);
1106 memcpy(&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen); 1124 memcpy (&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen);
1107 1125
1108 GNUNET_NETWORK_socket_sendto (dnsout, dns, 1126 GNUNET_NETWORK_socket_sendto (dnsout, dns,
1109 ntohs (pkt->hdr.size) - 1127 ntohs (pkt->hdr.size) -
1110 sizeof (struct query_packet) + 1, 1128 sizeof (struct query_packet) + 1,
1111 (struct sockaddr *) &dest, sizeof dest); 1129 (struct sockaddr *) &dest, sizeof dest);
1112 } 1130 }
1113 else if (pkt->addrlen == 16) 1131 else if (pkt->addrlen == 16)
1114 { 1132 {
1115 struct sockaddr_in6 dest; 1133 struct sockaddr_in6 dest;
1116 1134
1117 memset (&dest, 0, sizeof dest); 1135 memset (&dest, 0, sizeof dest);
1118 dest.sin6_port = htons (53); 1136 dest.sin6_port = htons (53);
1119 memcpy(&dest.sin6_addr, pkt->orig_to, pkt->addrlen); 1137 memcpy (&dest.sin6_addr, pkt->orig_to, pkt->addrlen);
1120 1138
1121 GNUNET_NETWORK_socket_sendto (dnsout6, dns, 1139 GNUNET_NETWORK_socket_sendto (dnsout6, dns,
1122 ntohs (pkt->hdr.size) - 1140 ntohs (pkt->hdr.size) -
1123 sizeof (struct query_packet) + 1, 1141 sizeof (struct query_packet) + 1,
1124 (struct sockaddr *) &dest, sizeof dest); 1142 (struct sockaddr *) &dest, sizeof dest);
1125 } 1143 }
1126 1144
1127outfree: 1145outfree:
1128 free_parsed_dns_packet (pdns); 1146 free_parsed_dns_packet (pdns);
@@ -1144,10 +1162,10 @@ open_port6 ()
1144 1162
1145 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0); 1163 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0);
1146 if (dnsout6 == NULL) 1164 if (dnsout6 == NULL)
1147 { 1165 {
1148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n"); 1166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n");
1149 return GNUNET_SYSERR; 1167 return GNUNET_SYSERR;
1150 } 1168 }
1151 memset (&addr, 0, sizeof (struct sockaddr_in6)); 1169 memset (&addr, 0, sizeof (struct sockaddr_in6));
1152 1170
1153 addr.sin6_family = AF_INET6; 1171 addr.sin6_family = AF_INET6;
@@ -1205,7 +1223,9 @@ open_port ()
1205 return GNUNET_YES; 1223 return GNUNET_YES;
1206} 1224}
1207 1225
1208void handle_response(struct dns_pkt* dns, struct sockaddr *addr, socklen_t addrlen, int r); 1226void
1227handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1228 int r);
1209 1229
1210/** 1230/**
1211 * Read a response-packet of the UDP-Socket 1231 * Read a response-packet of the UDP-Socket
@@ -1228,7 +1248,7 @@ read_response6 (void *cls
1228#ifndef MINGW 1248#ifndef MINGW
1229 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len)) 1249 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len))
1230 { 1250 {
1231 (void)open_port6 (); 1251 (void) open_port6 ();
1232 return; 1252 return;
1233 } 1253 }
1234#else 1254#else
@@ -1243,14 +1263,15 @@ read_response6 (void *cls
1243 (struct sockaddr *) &addr, &addrlen); 1263 (struct sockaddr *) &addr, &addrlen);
1244 1264
1245 if (r < 0) 1265 if (r < 0)
1246 { 1266 {
1247 (void)open_port6 (); 1267 (void) open_port6 ();
1248 return; 1268 return;
1249 } 1269 }
1270
1271 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1250 1272
1251 struct sockaddr *addr_ = GNUNET_malloc(sizeof addr);
1252 memcpy (addr_, &addr, sizeof addr); 1273 memcpy (addr_, &addr, sizeof addr);
1253 handle_response(dns, addr_, 4, r); 1274 handle_response (dns, addr_, 4, r);
1254 1275
1255 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6, 1276 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1256 &read_response6, NULL); 1277 &read_response6, NULL);
@@ -1277,8 +1298,7 @@ read_response (void *cls
1277#ifndef MINGW 1298#ifndef MINGW
1278 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len)) 1299 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
1279 { 1300 {
1280 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 1301 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
1281 "ioctl");
1282 unhijack (dnsoutport); 1302 unhijack (dnsoutport);
1283 if (GNUNET_YES == open_port ()) 1303 if (GNUNET_YES == open_port ())
1284 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); 1304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
@@ -1296,114 +1316,118 @@ read_response (void *cls
1296 (struct sockaddr *) &addr, &addrlen); 1316 (struct sockaddr *) &addr, &addrlen);
1297 1317
1298 if (r < 0) 1318 if (r < 0)
1299 { 1319 {
1300 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 1320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1301 "recvfrom"); 1321 unhijack (dnsoutport);
1302 unhijack (dnsoutport); 1322 if (GNUNET_YES == open_port ())
1303 if (GNUNET_YES == open_port ()) 1323 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL); 1324 return;
1305 return; 1325 }
1306 } 1326
1327 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1307 1328
1308 struct sockaddr *addr_ = GNUNET_malloc(sizeof addr);
1309 memcpy (addr_, &addr, sizeof addr); 1329 memcpy (addr_, &addr, sizeof addr);
1310 handle_response(dns, addr_, 4, r); 1330 handle_response (dns, addr_, 4, r);
1311 1331
1312 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout, 1332 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1313 &read_response, NULL); 1333 &read_response, NULL);
1314} 1334}
1315 1335
1316void 1336void
1317handle_response(struct dns_pkt* dns, struct sockaddr *addr, socklen_t addrlen, int r) 1337handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1338 int r)
1318{ 1339{
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n", 1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1320 ntohs (dns->s.id)); 1341 ntohs (dns->s.id));
1321 1342
1322 1343
1323 if (query_states[dns->s.id].valid == GNUNET_YES) 1344 if (query_states[dns->s.id].valid == GNUNET_YES)
1345 {
1346 if (query_states[dns->s.id].tunnel != NULL)
1347 {
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Answer to query %d for a remote peer!\n", ntohs (dns->s.id));
1350 /* This response should go through a tunnel */
1351 uint32_t *c =
1352 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1353 *c = r;
1354 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1355
1356 *t = query_states[dns->s.id].tunnel;
1357 memcpy (t + 1, dns, r);
1358 struct tunnel_state *s =
1359 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel);
1360 if (NULL == s->th)
1361 {
1362 s->th =
1363 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1364 GNUNET_YES, 32,
1365 GNUNET_TIME_UNIT_MINUTES, NULL,
1366 r +
1367 sizeof (struct
1368 GNUNET_MessageHeader),
1369 mesh_send_response, c);
1370 }
1371 else
1372 {
1373 struct tunnel_notify_queue *element =
1374 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1375 element->cls = c;
1376 element->len = r + sizeof (struct GNUNET_MessageHeader);
1377 element->cb = mesh_send_response;
1378
1379 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
1380 }
1381 }
1382 else
1324 { 1383 {
1325 if (query_states[dns->s.id].tunnel != NULL) 1384 query_states[dns->s.id].valid = GNUNET_NO;
1326 { 1385
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1386 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1328 "Answer to query %d for a remote peer!\n", 1387 struct answer_packet_list *answer =
1329 ntohs (dns->s.id)); 1388 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
1330 /* This response should go through a tunnel */ 1389 (sizeof (struct answer_packet)));
1331 uint32_t *c = 1390 answer->pkt.hdr.type =
1332 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r); 1391 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1333 *c = r; 1392 answer->pkt.hdr.size = htons (len);
1334 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1); 1393 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1335 1394 answer->pkt.addrlen = addrlen;
1336 *t = query_states[dns->s.id].tunnel; 1395 if (addrlen == 16)
1337 memcpy (t + 1, dns, r); 1396 {
1338 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel); 1397 struct sockaddr_in6 *addr_ = (struct sockaddr_in6 *) addr;
1339 if (NULL == s->th) 1398
1340 { 1399 memcpy (answer->pkt.from, &addr_->sin6_addr, addrlen);
1341 s->th = 1400 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1342 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel, 1401 }
1343 GNUNET_YES, 1402 else if (addrlen == 4)
1344 32, 1403 {
1345 GNUNET_TIME_UNIT_MINUTES, 1404 struct sockaddr_in *addr_ = (struct sockaddr_in *) addr;
1346 NULL, 1405
1347 r + 1406 memcpy (answer->pkt.from, &addr_->sin_addr.s_addr, addrlen);
1348 sizeof (struct 1407 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1349 GNUNET_MessageHeader), 1408 }
1350 mesh_send_response, c);
1351 }
1352 else
1353 {
1354 struct tunnel_notify_queue *element =
1355 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1356 element->cls = c;
1357 element->len = r + sizeof (struct GNUNET_MessageHeader);
1358 element->cb = mesh_send_response;
1359
1360 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
1361 }
1362 }
1363 else 1409 else
1364 { 1410 {
1365 query_states[dns->s.id].valid = GNUNET_NO; 1411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen);
1366 1412 GNUNET_assert (0);
1367 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */ 1413 }
1368 struct answer_packet_list *answer = 1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n",
1369 GNUNET_malloc (len + sizeof(struct answer_packet_list) - (sizeof(struct answer_packet))); 1415 addrlen);
1370 answer->pkt.hdr.type = 1416 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1371 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); 1417 memcpy (answer->pkt.data, dns, r);
1372 answer->pkt.hdr.size = htons (len); 1418 answer->client = query_states[dns->s.id].client;
1373 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; 1419
1374 answer->pkt.addrlen = addrlen; 1420 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1375 if (addrlen == 16) 1421
1376 { 1422 if (server_notify == NULL)
1377 struct sockaddr_in6 *addr_ = (struct sockaddr_in6*)addr; 1423 server_notify =
1378 memcpy(answer->pkt.from, &addr_->sin6_addr, addrlen); 1424 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1379 memcpy(answer->pkt.to, query_states[dns->s.id].local_ip, addrlen); 1425 len,
1380 } 1426 GNUNET_TIME_UNIT_FOREVER_REL,
1381 else if (addrlen == 4) 1427 &send_answer, NULL);
1382 {
1383 struct sockaddr_in *addr_ = (struct sockaddr_in*)addr;
1384 memcpy(answer->pkt.from, &addr_->sin_addr.s_addr, addrlen);
1385 memcpy(answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1386 }
1387 else
1388 {
1389 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen);
1390 GNUNET_assert(0);
1391 }
1392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n", addrlen);
1393 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1394 memcpy (answer->pkt.data, dns, r);
1395 answer->client = query_states[dns->s.id].client;
1396
1397 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1398
1399 if (server_notify == NULL)
1400 server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1401 len, GNUNET_TIME_UNIT_FOREVER_REL,
1402 &send_answer,
1403 NULL);
1404 }
1405 } 1428 }
1406 GNUNET_free(addr); 1429 }
1430 GNUNET_free (addr);
1407} 1431}
1408 1432
1409 1433
@@ -1535,8 +1559,9 @@ publish_name (const char *name, uint64_t ports, uint32_t service_type,
1535 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer); 1559 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1536 1560
1537 data.expiration_time = 1561 data.expiration_time =
1538 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply 1562 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute
1539 (GNUNET_TIME_UNIT_HOURS, 2))); 1563 (GNUNET_TIME_relative_multiply
1564 (GNUNET_TIME_UNIT_HOURS, 2)));
1540 1565
1541 /* Sign the block */ 1566 /* Sign the block */
1542 if (GNUNET_OK != 1567 if (GNUNET_OK !=
@@ -1549,7 +1574,8 @@ publish_name (const char *name, uint64_t ports, uint32_t service_type,
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n", 1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1550 *((unsigned int *) &data.service_descriptor), size); 1575 *((unsigned int *) &data.service_descriptor), size);
1551 1576
1552 GNUNET_DHT_put (dht, &data.service_descriptor, 5 /* DEFAULT_PUT_REPLICATION */, 1577 GNUNET_DHT_put (dht, &data.service_descriptor,
1578 5 /* DEFAULT_PUT_REPLICATION */ ,
1553 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size, 1579 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1554 (char *) &data, 1580 (char *) &data,
1555 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), 1581 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
@@ -1694,13 +1720,15 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
1694 if (GNUNET_YES == 1720 if (GNUNET_YES ==
1695 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) 1721 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1696 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER; 1722 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1697 mesh_handle = GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel, mesh_handlers, apptypes); 1723 mesh_handle =
1724 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel,
1725 mesh_handlers, apptypes);
1698 1726
1699 cfg = cfg_; 1727 cfg = cfg_;
1700 dht = GNUNET_DHT_connect (cfg, 1024); 1728 dht = GNUNET_DHT_connect (cfg, 1024);
1701 GNUNET_SCHEDULER_add_now (publish_names, NULL); 1729 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1702 GNUNET_SERVER_add_handlers (server, handlers); 1730 GNUNET_SERVER_add_handlers (server, handlers);
1703 GNUNET_SERVER_disconnect_notify(server, &client_disconnect, NULL); 1731 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1704 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, 1732 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1705 cls); 1733 cls);
1706} 1734}