gnunet-daemon-vpn-helper.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010 Christian Grothoff
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file vpn/gnunet-daemon-vpn-helper.c
  19. * @brief
  20. * @author Philipp Toelke
  21. */
  22. #include <platform.h>
  23. #include <gnunet_common.h>
  24. #include <gnunet_client_lib.h>
  25. #include <gnunet_os_lib.h>
  26. #include <gnunet_mesh_service.h>
  27. #include <gnunet_protocols.h>
  28. #include <gnunet_server_lib.h>
  29. #include <gnunet_container_lib.h>
  30. #include <block_dns.h>
  31. #include <gnunet_configuration_lib.h>
  32. #include <gnunet_applications.h>
  33. #include "gnunet-daemon-vpn-dns.h"
  34. #include "gnunet-daemon-vpn.h"
  35. #include "gnunet-daemon-vpn-helper.h"
  36. #include "gnunet-service-dns-p.h"
  37. #include "gnunet-vpn-packet.h"
  38. #include "gnunet-vpn-checksum.h"
  39. #include "gnunet-helper-vpn-api.h"
  40. struct GNUNET_VPN_HELPER_Handle *helper_handle;
  41. /**
  42. * The tunnels that will be used to send tcp- and udp-packets
  43. */
  44. static struct GNUNET_MESH_Tunnel* tcp_tunnel;
  45. static struct GNUNET_MESH_Tunnel* udp_tunnel;
  46. /**
  47. * Start the helper-process
  48. *
  49. * If cls != NULL it is assumed that this function is called as a result of a dying
  50. * helper. cls is then taken as handle to the old helper and is cleaned up.
  51. * {{{
  52. */
  53. void
  54. start_helper_and_schedule(void *cls,
  55. const struct GNUNET_SCHEDULER_TaskContext *tc) {
  56. shs_task = GNUNET_SCHEDULER_NO_TASK;
  57. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  58. return;
  59. if (cls != NULL)
  60. cleanup_helper(cls);
  61. cls = NULL;
  62. char* ifname;
  63. char* ipv6addr;
  64. char* ipv6prefix;
  65. char* ipv4addr;
  66. char* ipv4mask;
  67. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname))
  68. {
  69. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
  70. exit(1);
  71. }
  72. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr))
  73. {
  74. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
  75. exit(1);
  76. }
  77. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6PREFIX", &ipv6prefix))
  78. {
  79. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
  80. exit(1);
  81. }
  82. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4ADDR", &ipv4addr))
  83. {
  84. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
  85. exit(1);
  86. }
  87. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV4MASK", &ipv4mask))
  88. {
  89. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
  90. exit(1);
  91. }
  92. /* Start the helper
  93. * Messages get passed to the function message_token
  94. * When the helper dies, this function will be called again with the
  95. * helper_handle as cls.
  96. */
  97. helper_handle = start_helper(ifname,
  98. ipv6addr,
  99. ipv6prefix,
  100. ipv4addr,
  101. ipv4mask,
  102. "vpn-gnunet",
  103. start_helper_and_schedule,
  104. message_token,
  105. NULL);
  106. GNUNET_free(ipv6addr);
  107. GNUNET_free(ipv6prefix);
  108. GNUNET_free(ipv4addr);
  109. GNUNET_free(ipv4mask);
  110. GNUNET_free(ifname);
  111. /* Tell the dns-service to rehijack the dns-port
  112. * The routing-table gets flushed if an interface disappears.
  113. */
  114. restart_hijack = 1;
  115. if (NULL != dns_connection)
  116. GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL);
  117. GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, helper_handle->fh_to_helper, &helper_write, NULL);
  118. }
  119. /*}}}*/
  120. /**
  121. * Send an dns-answer-packet to the helper
  122. */
  123. void
  124. helper_write(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
  125. if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
  126. return;
  127. struct answer_packet_list* ans = answer_proc_head;
  128. if (NULL == ans) return;
  129. size_t len = ntohs(ans->pkt.hdr.size);
  130. GNUNET_assert(ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
  131. GNUNET_assert (20 == sizeof (struct ip_hdr));
  132. GNUNET_assert (8 == sizeof (struct udp_pkt));
  133. size_t data_len = len - sizeof(struct answer_packet) + 1;
  134. size_t net_len = sizeof(struct ip_hdr) + sizeof(struct udp_dns) + data_len;
  135. size_t pkt_len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct pkt_tun) + net_len;
  136. struct ip_udp_dns* pkt = alloca(pkt_len);
  137. GNUNET_assert(pkt != NULL);
  138. memset(pkt, 0, pkt_len);
  139. /* set the gnunet-header */
  140. pkt->shdr.size = htons(pkt_len);
  141. pkt->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
  142. /* set the tun-header (no flags and ethertype of IPv4) */
  143. pkt->tun.flags = 0;
  144. pkt->tun.type = htons(0x0800);
  145. /* set the ip-header */
  146. pkt->ip_hdr.version = 4;
  147. pkt->ip_hdr.hdr_lngth = 5;
  148. pkt->ip_hdr.diff_serv = 0;
  149. pkt->ip_hdr.tot_lngth = htons(net_len);
  150. pkt->ip_hdr.ident = 0;
  151. pkt->ip_hdr.flags = 0;
  152. pkt->ip_hdr.frag_off = 0;
  153. pkt->ip_hdr.ttl = 255;
  154. pkt->ip_hdr.proto = 0x11; /* UDP */
  155. pkt->ip_hdr.chks = 0; /* Will be calculated later*/
  156. pkt->ip_hdr.sadr = ans->pkt.from;
  157. pkt->ip_hdr.dadr = ans->pkt.to;
  158. pkt->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&pkt->ip_hdr, 5*4);
  159. /* set the udp-header */
  160. pkt->udp_dns.udp_hdr.spt = htons(53);
  161. pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
  162. pkt->udp_dns.udp_hdr.len = htons(net_len - sizeof(struct ip_hdr));
  163. pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
  164. memcpy(&pkt->udp_dns.data, ans->pkt.data, data_len);
  165. GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
  166. GNUNET_free(ans);
  167. if (GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len) < 0)
  168. {
  169. cleanup_helper(helper_handle);
  170. GNUNET_SCHEDULER_add_now(start_helper_and_schedule, NULL);
  171. return;
  172. }
  173. /* if more packets are available, reschedule */
  174. if (answer_proc_head != NULL)
  175. GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
  176. helper_handle->fh_to_helper,
  177. &helper_write,
  178. NULL);
  179. }
  180. /**
  181. * Receive packets from the helper-process
  182. */
  183. void
  184. message_token (void *cls __attribute__((unused)),
  185. void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
  186. {
  187. GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
  188. struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
  189. GNUNET_HashCode *key;
  190. /* ethertype is ipv6 */
  191. if (ntohs (pkt_tun->tun.type) == 0x86dd)
  192. {
  193. struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
  194. GNUNET_assert (pkt6->ip6_hdr.version == 6);
  195. struct ip6_tcp *pkt6_tcp;
  196. struct ip6_udp *pkt6_udp;
  197. struct ip6_icmp *pkt6_icmp;
  198. switch (pkt6->ip6_hdr.nxthdr)
  199. {
  200. case 0x06: /* TCP */
  201. case 0x11: /* UDP */
  202. pkt6_tcp = (struct ip6_tcp *) pkt6;
  203. pkt6_udp = (struct ip6_udp *) pkt6;
  204. if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
  205. {
  206. struct map_entry *me =
  207. GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  208. GNUNET_assert (me != NULL);
  209. GNUNET_free (key);
  210. size_t size =
  211. sizeof (struct GNUNET_MESH_Tunnel *) +
  212. sizeof (struct GNUNET_MessageHeader) +
  213. sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth);
  214. struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
  215. struct GNUNET_MessageHeader *hdr =
  216. (struct GNUNET_MessageHeader *) (cls + 1);
  217. GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
  218. hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
  219. sizeof (GNUNET_HashCode) +
  220. ntohs (pkt6->ip6_hdr.paylgth));
  221. GNUNET_MESH_ApplicationType app_type;
  222. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", me->addrlen);
  223. if (me->addrlen == 0)
  224. {
  225. /* This is a mapping to a gnunet-service */
  226. memcpy (hc, &me->desc.service_descriptor,
  227. sizeof (GNUNET_HashCode));
  228. if (0x11 == pkt6->ip6_hdr.nxthdr
  229. && (me->desc.
  230. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
  231. && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
  232. || testBit (me->additional_ports,
  233. ntohs (pkt6_udp->udp_hdr.dpt))))
  234. {
  235. hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
  236. memcpy (hc + 1, &pkt6_udp->udp_hdr,
  237. ntohs (pkt6_udp->udp_hdr.len));
  238. }
  239. else if (0x06 == pkt6->ip6_hdr.nxthdr
  240. && (me->desc.
  241. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
  242. &&
  243. (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
  244. {
  245. hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_TCP);
  246. memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
  247. ntohs (pkt6->ip6_hdr.paylgth));
  248. }
  249. if (me->tunnel == NULL && NULL != cls)
  250. {
  251. *cls =
  252. GNUNET_MESH_peer_request_connect_all (mesh_handle,
  253. GNUNET_TIME_UNIT_FOREVER_REL,
  254. 1,
  255. (struct
  256. GNUNET_PeerIdentity
  257. *) &me->desc.peer,
  258. send_pkt_to_peer,
  259. NULL, cls);
  260. me->tunnel = *cls;
  261. }
  262. else if (NULL != cls)
  263. {
  264. *cls = me->tunnel;
  265. send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
  266. NULL);
  267. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  268. "Queued to send to peer %x, type %d\n",
  269. *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
  270. }
  271. }
  272. else
  273. {
  274. /* This is a mapping to a "real" address */
  275. struct remote_addr *s = (struct remote_addr*) hc;
  276. s->addrlen = me->addrlen;
  277. memcpy(s->addr, me->addr, me->addrlen);
  278. s->proto= pkt6->ip6_hdr.nxthdr;
  279. if (s->proto == 0x11)
  280. {
  281. hdr->type = htons(GNUNET_MESSAGE_TYPE_REMOTE_UDP);
  282. memcpy (hc + 1, &pkt6_udp->udp_hdr,
  283. ntohs (pkt6_udp->udp_hdr.len));
  284. app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
  285. if (NULL != udp_tunnel)
  286. me->tunnel = udp_tunnel;
  287. }
  288. else if (s->proto == 0x06)
  289. {
  290. hdr->type = htons(GNUNET_MESSAGE_TYPE_REMOTE_TCP);
  291. memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
  292. ntohs (pkt6->ip6_hdr.paylgth));
  293. app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
  294. if (NULL != tcp_tunnel)
  295. me->tunnel = tcp_tunnel;
  296. }
  297. if (me->tunnel == NULL && NULL != cls)
  298. {
  299. *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
  300. GNUNET_TIME_UNIT_FOREVER_REL,
  301. app_type,
  302. send_pkt_to_peer,
  303. NULL,
  304. cls);
  305. me->tunnel = *cls;
  306. if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
  307. udp_tunnel = *cls;
  308. else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
  309. tcp_tunnel = *cls;
  310. }
  311. else if (NULL != cls)
  312. {
  313. *cls = me->tunnel;
  314. send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
  315. }
  316. }
  317. }
  318. else
  319. {
  320. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n", pkt6->ip6_hdr.dadr[0],
  321. pkt6->ip6_hdr.dadr[1],
  322. pkt6->ip6_hdr.dadr[2],
  323. pkt6->ip6_hdr.dadr[3],
  324. pkt6->ip6_hdr.dadr[4],
  325. pkt6->ip6_hdr.dadr[5],
  326. pkt6->ip6_hdr.dadr[6],
  327. pkt6->ip6_hdr.dadr[7],
  328. pkt6->ip6_hdr.dadr[8],
  329. pkt6->ip6_hdr.dadr[9],
  330. pkt6->ip6_hdr.dadr[10],
  331. pkt6->ip6_hdr.dadr[11],
  332. pkt6->ip6_hdr.dadr[12],
  333. pkt6->ip6_hdr.dadr[13],
  334. pkt6->ip6_hdr.dadr[14],
  335. pkt6->ip6_hdr.dadr[15]);
  336. }
  337. break;
  338. case 0x3a:
  339. /* ICMPv6 */
  340. pkt6_icmp = (struct ip6_icmp *) pkt6;
  341. /* If this packet is an icmp-echo-request and a mapping exists, answer */
  342. if (pkt6_icmp->icmp_hdr.type == 0x80
  343. && (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
  344. {
  345. GNUNET_free (key);
  346. pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
  347. memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
  348. GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
  349. }
  350. break;
  351. }
  352. }
  353. /* ethertype is ipv4 */
  354. else if (ntohs (pkt_tun->tun.type) == 0x0800)
  355. {
  356. struct ip_pkt *pkt = (struct ip_pkt *) message;
  357. struct ip_udp *udp = (struct ip_udp *) message;
  358. struct ip_tcp *pkt_tcp;
  359. struct ip_udp *pkt_udp;
  360. struct ip_icmp *pkt_icmp;
  361. GNUNET_assert (pkt->ip_hdr.version == 4);
  362. /* Send dns-packets to the service-dns */
  363. if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
  364. {
  365. /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
  366. size_t len =
  367. sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
  368. struct query_packet_list *query =
  369. GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
  370. query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
  371. query->pkt.hdr.size = htons (len);
  372. query->pkt.orig_to = pkt->ip_hdr.dadr;
  373. query->pkt.orig_from = pkt->ip_hdr.sadr;
  374. query->pkt.src_port = udp->udp_hdr.spt;
  375. memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
  376. GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
  377. GNUNET_assert (head != NULL);
  378. if (dns_connection != NULL)
  379. GNUNET_CLIENT_notify_transmit_ready (dns_connection,
  380. len,
  381. GNUNET_TIME_UNIT_FOREVER_REL,
  382. GNUNET_YES,
  383. &send_query, NULL);
  384. }
  385. else
  386. {
  387. uint32_t dadr = pkt->ip_hdr.dadr;
  388. unsigned char *c = (unsigned char*)&dadr;
  389. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
  390. c[0],
  391. c[1],
  392. c[2],
  393. c[3],
  394. pkt->ip_hdr.proto);
  395. switch (pkt->ip_hdr.proto)
  396. {
  397. case 0x06: /* TCP */
  398. case 0x11: /* UDP */
  399. pkt_tcp = (struct ip_tcp*) pkt;
  400. pkt_udp = (struct ip_udp*) pkt;
  401. if ((key = address4_mapping_exists (dadr)) != NULL)
  402. {
  403. struct map_entry *me =
  404. GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  405. GNUNET_assert (me != NULL);
  406. GNUNET_free (key);
  407. size_t size =
  408. sizeof (struct GNUNET_MESH_Tunnel *) +
  409. sizeof (struct GNUNET_MessageHeader) +
  410. sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth;
  411. struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
  412. struct GNUNET_MessageHeader *hdr =
  413. (struct GNUNET_MessageHeader *) (cls + 1);
  414. GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
  415. hdr->size = htons (sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
  416. GNUNET_MESH_ApplicationType app_type;
  417. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n", me->addrlen);
  418. if (me->addrlen == 0)
  419. {
  420. /* This is a mapping to a gnunet-service */
  421. memcpy (hc, &me->desc.service_descriptor,
  422. sizeof (GNUNET_HashCode));
  423. if (0x11 == pkt->ip_hdr.proto
  424. && (me->desc.
  425. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP))
  426. && (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt)
  427. || testBit (me->additional_ports,
  428. ntohs (pkt_udp->udp_hdr.dpt))))
  429. {
  430. hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
  431. memcpy (hc + 1, &pkt_udp->udp_hdr,
  432. ntohs (pkt_udp->udp_hdr.len));
  433. }
  434. else if (0x06 == pkt->ip_hdr.proto
  435. && (me->desc.
  436. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
  437. &&
  438. (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
  439. {
  440. hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_TCP);
  441. memcpy (hc + 1, &pkt_tcp->tcp_hdr,
  442. ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
  443. }
  444. if (me->tunnel == NULL && NULL != cls)
  445. {
  446. *cls =
  447. GNUNET_MESH_peer_request_connect_all (mesh_handle,
  448. GNUNET_TIME_UNIT_FOREVER_REL,
  449. 1,
  450. (struct
  451. GNUNET_PeerIdentity
  452. *) &me->desc.peer,
  453. send_pkt_to_peer,
  454. NULL, cls);
  455. me->tunnel = *cls;
  456. }
  457. else if (NULL != cls)
  458. {
  459. *cls = me->tunnel;
  460. send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
  461. NULL);
  462. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  463. "Queued to send to peer %x, type %d\n",
  464. *((unsigned int *) &me->desc.peer), ntohs(hdr->type));
  465. }
  466. }
  467. else
  468. {
  469. /* This is a mapping to a "real" address */
  470. struct remote_addr *s = (struct remote_addr*) hc;
  471. s->addrlen = me->addrlen;
  472. memcpy(s->addr, me->addr, me->addrlen);
  473. s->proto= pkt->ip_hdr.proto;
  474. if (s->proto == 0x11)
  475. {
  476. hdr->type = htons(GNUNET_MESSAGE_TYPE_REMOTE_UDP);
  477. memcpy (hc + 1, &pkt_udp->udp_hdr,
  478. ntohs (pkt_udp->udp_hdr.len));
  479. app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
  480. }
  481. else if (s->proto == 0x06)
  482. {
  483. hdr->type = htons(GNUNET_MESSAGE_TYPE_REMOTE_TCP);
  484. memcpy (hc + 1, &pkt_tcp->tcp_hdr,
  485. ntohs (pkt->ip_hdr.tot_lngth) - 4*pkt->ip_hdr.hdr_lngth);
  486. app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
  487. }
  488. if (me->tunnel == NULL && NULL != cls)
  489. {
  490. *cls = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
  491. GNUNET_TIME_UNIT_FOREVER_REL,
  492. app_type,
  493. send_pkt_to_peer,
  494. NULL,
  495. cls);
  496. me->tunnel = *cls;
  497. }
  498. else if (NULL != cls)
  499. {
  500. *cls = me->tunnel;
  501. send_pkt_to_peer(cls, (struct GNUNET_PeerIdentity*) 1, NULL);
  502. }
  503. }
  504. }
  505. else
  506. {
  507. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet to %x which has no mapping\n", dadr);
  508. }
  509. break;
  510. case 0x01:
  511. /* ICMP */
  512. pkt_icmp = (struct ip_icmp*)pkt;
  513. if (pkt_icmp->icmp_hdr.type == 0x8 &&
  514. (key = address4_mapping_exists (dadr)) != NULL)
  515. {
  516. GNUNET_free(key);
  517. pkt_icmp = GNUNET_malloc(ntohs(pkt->shdr.size));
  518. memcpy(pkt_icmp, pkt, ntohs(pkt->shdr.size));
  519. GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
  520. }
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. void write_to_helper(void* buf, size_t len)
  527. {
  528. (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len);
  529. }
  530. void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls)
  531. {
  532. if (GNUNET_SCHEDULER_NO_TASK != shs_task) return;
  533. GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls);
  534. }