gnunet-daemon-vpn.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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.c
  19. * @brief
  20. * @author Philipp Toelke
  21. */
  22. #include "platform.h"
  23. #include "gnunet_getopt_lib.h"
  24. #include "gnunet_program_lib.h"
  25. #include "gnunet-vpn-packet.h"
  26. #include "gnunet_common.h"
  27. #include "gnunet_protocols.h"
  28. #include "gnunet_applications.h"
  29. #include <gnunet_mesh_service.h>
  30. #include "gnunet_client_lib.h"
  31. #include "gnunet_container_lib.h"
  32. #include "gnunet_constants.h"
  33. #include <block_dns.h>
  34. #include "gnunet-daemon-vpn-helper.h"
  35. #include "gnunet-daemon-vpn-dns.h"
  36. #include "gnunet-daemon-vpn.h"
  37. #include "gnunet-vpn-checksum.h"
  38. const struct GNUNET_CONFIGURATION_Handle *cfg;
  39. struct GNUNET_MESH_Handle *mesh_handle;
  40. struct GNUNET_CONTAINER_MultiHashMap* hashmap;
  41. static struct GNUNET_CONTAINER_Heap *heap;
  42. struct tunnel_notify_queue
  43. {
  44. struct tunnel_notify_queue* next;
  45. struct tunnel_notify_queue* prev;
  46. size_t len;
  47. void* cls;
  48. };
  49. /**
  50. * If there are at least this many address-mappings, old ones will be removed
  51. */
  52. static long long unsigned int max_mappings = 200;
  53. /**
  54. * Final status code.
  55. */
  56. static int ret;
  57. /**
  58. * This hashmap contains the mapping from peer, service-descriptor,
  59. * source-port and destination-port to a socket
  60. */
  61. static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
  62. GNUNET_SCHEDULER_TaskIdentifier conn_task;
  63. GNUNET_SCHEDULER_TaskIdentifier shs_task;
  64. /**
  65. * Function scheduled as very last function, cleans up after us
  66. *{{{
  67. */
  68. static void
  69. cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
  70. GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
  71. /* stop the helper */
  72. cleanup_helper(helper_handle);
  73. /* close the connection to the service-dns */
  74. if (dns_connection != NULL)
  75. {
  76. GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
  77. dns_connection = NULL;
  78. }
  79. if (mesh_handle != NULL)
  80. {
  81. GNUNET_MESH_disconnect(mesh_handle);
  82. mesh_handle = NULL;
  83. }
  84. if (GNUNET_SCHEDULER_NO_TASK != shs_task)
  85. {
  86. GNUNET_SCHEDULER_cancel (shs_task);
  87. shs_task = GNUNET_SCHEDULER_NO_TASK;
  88. }
  89. if (GNUNET_SCHEDULER_NO_TASK != conn_task)
  90. {
  91. GNUNET_SCHEDULER_cancel (conn_task);
  92. conn_task = GNUNET_SCHEDULER_NO_TASK;
  93. }
  94. }
  95. /*}}}*/
  96. /**
  97. * @return the hash of the IP-Address if a mapping exists, NULL otherwise
  98. */
  99. GNUNET_HashCode*
  100. address6_mapping_exists(unsigned char addr[]) {
  101. GNUNET_HashCode* key = GNUNET_malloc(sizeof(GNUNET_HashCode));
  102. unsigned char* k = (unsigned char*)key;
  103. memset(key, 0, sizeof(GNUNET_HashCode));
  104. unsigned int i;
  105. for (i = 0; i < 16; i++)
  106. k[15-i] = addr[i];
  107. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(hashmap, key))
  108. return key;
  109. else
  110. {
  111. GNUNET_free(key);
  112. return NULL;
  113. }
  114. }
  115. /**
  116. * @return the hash of the IP-Address if a mapping exists, NULL otherwise
  117. */
  118. GNUNET_HashCode *
  119. address4_mapping_exists (uint32_t addr)
  120. {
  121. GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
  122. memset (key, 0, sizeof (GNUNET_HashCode));
  123. unsigned char *c = (unsigned char *) &addr;
  124. unsigned char *k = (unsigned char *) key;
  125. unsigned int i;
  126. for (i = 0; i < 4; i++)
  127. k[3 - i] = c[i];
  128. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  129. "a4_m_e: getting with key %08x, addr is %08x, %d.%d.%d.%d\n",
  130. *((uint32_t *) (key)), addr, c[0], c[1], c[2], c[3]);
  131. if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (hashmap, key))
  132. return key;
  133. else
  134. {
  135. GNUNET_free (key);
  136. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping not found!\n");
  137. return NULL;
  138. }
  139. }
  140. static void
  141. collect_mappings(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tc) {
  142. if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  143. return;
  144. struct map_entry* me = GNUNET_CONTAINER_heap_remove_root(heap);
  145. /* This is free()ed memory! */
  146. me->heap_node = NULL;
  147. /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
  148. GNUNET_CONTAINER_multihashmap_remove(hashmap, &me->hash, me);
  149. GNUNET_free(me);
  150. }
  151. void
  152. send_icmp4_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
  153. if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  154. return;
  155. struct ip_icmp* request = cls;
  156. struct ip_icmp* response = alloca(ntohs(request->shdr.size));
  157. GNUNET_assert(response != NULL);
  158. memset(response, 0, ntohs(request->shdr.size));
  159. response->shdr.size = request->shdr.size;
  160. response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
  161. response->tun.flags = 0;
  162. response->tun.type = htons(0x0800);
  163. response->ip_hdr.hdr_lngth = 5;
  164. response->ip_hdr.version = 4;
  165. response->ip_hdr.proto = 0x01;
  166. response->ip_hdr.dadr = request->ip_hdr.sadr;
  167. response->ip_hdr.sadr = request->ip_hdr.dadr;
  168. response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
  169. response->ip_hdr.chks = calculate_ip_checksum((uint16_t*)&response->ip_hdr, 20);
  170. response->icmp_hdr.code = 0;
  171. response->icmp_hdr.type = 0x0;
  172. /* Magic, more Magic! */
  173. response->icmp_hdr.chks = request->icmp_hdr.chks + 0x8;
  174. /* Copy the rest of the packet */
  175. memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip_icmp));
  176. write_to_helper(response, ntohs(response->shdr.size));
  177. GNUNET_free(request);
  178. }
  179. void
  180. send_icmp6_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
  181. if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  182. return;
  183. struct ip6_icmp* request = cls;
  184. struct ip6_icmp* response = alloca(ntohs(request->shdr.size));
  185. GNUNET_assert(response != NULL);
  186. memset(response, 0, ntohs(request->shdr.size));
  187. response->shdr.size = request->shdr.size;
  188. response->shdr.type = htons(GNUNET_MESSAGE_TYPE_VPN_HELPER);
  189. response->tun.flags = 0;
  190. response->tun.type = htons(0x86dd);
  191. response->ip6_hdr.hoplmt = 255;
  192. response->ip6_hdr.paylgth = request->ip6_hdr.paylgth;
  193. response->ip6_hdr.nxthdr = 0x3a;
  194. response->ip6_hdr.version = 6;
  195. memcpy(&response->ip6_hdr.sadr, &request->ip6_hdr.dadr, 16);
  196. memcpy(&response->ip6_hdr.dadr, &request->ip6_hdr.sadr, 16);
  197. response->icmp_hdr.code = 0;
  198. response->icmp_hdr.type = 0x81;
  199. /* Magic, more Magic! */
  200. response->icmp_hdr.chks = request->icmp_hdr.chks - 0x1;
  201. /* Copy the rest of the packet */
  202. memcpy(response+1, request+1, ntohs(request->shdr.size) - sizeof(struct ip6_icmp));
  203. write_to_helper(response, ntohs(response->shdr.size));
  204. GNUNET_free(request);
  205. }
  206. /**
  207. * cls is the pointer to a GNUNET_MessageHeader that is
  208. * followed by the service-descriptor and the packet that should be sent;
  209. */
  210. static size_t
  211. send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
  212. {
  213. struct GNUNET_MESH_Tunnel **tunnel = cls;
  214. GNUNET_MESH_tunnel_set_data(*tunnel, NULL);
  215. struct GNUNET_MessageHeader *hdr =
  216. (struct GNUNET_MessageHeader *) (tunnel + 1);
  217. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
  218. GNUNET_assert (size >= ntohs (hdr->size));
  219. memcpy (buf, hdr, ntohs (hdr->size));
  220. size = ntohs(hdr->size);
  221. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
  222. if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
  223. {
  224. struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
  225. struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
  226. struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
  227. GNUNET_CONTAINER_DLL_remove(head, tail, element);
  228. GNUNET_MESH_tunnel_set_head(*tunnel, head);
  229. GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
  230. struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
  231. GNUNET_NO,
  232. 42,
  233. GNUNET_TIME_relative_divide
  234. (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
  235. (const struct GNUNET_PeerIdentity *)
  236. NULL, element->len,
  237. send_pkt_to_peer_notify_callback, element->cls);
  238. /* save the handle */
  239. GNUNET_MESH_tunnel_set_data(*tunnel, th);
  240. GNUNET_free(element);
  241. }
  242. GNUNET_free (cls);
  243. return size;
  244. }
  245. unsigned int
  246. port_in_ports (uint64_t ports, uint16_t port)
  247. {
  248. uint16_t *ps = (uint16_t *) & ports;
  249. return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
  250. }
  251. void
  252. send_pkt_to_peer (void *cls,
  253. const struct GNUNET_PeerIdentity *peer,
  254. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  255. {
  256. /* peer == NULL means that all peers in this request are connected */
  257. if (peer == NULL) return;
  258. struct GNUNET_MESH_Tunnel **tunnel = cls;
  259. struct GNUNET_MessageHeader *hdr =
  260. (struct GNUNET_MessageHeader *) (tunnel + 1);
  261. GNUNET_assert(NULL != tunnel);
  262. GNUNET_assert(NULL != *tunnel);
  263. if (NULL == GNUNET_MESH_tunnel_get_data(*tunnel))
  264. {
  265. struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
  266. GNUNET_NO,
  267. 42,
  268. GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
  269. (const struct GNUNET_PeerIdentity *)NULL,
  270. ntohs(hdr->size),
  271. send_pkt_to_peer_notify_callback,
  272. cls);
  273. GNUNET_MESH_tunnel_set_data(*tunnel, th);
  274. }
  275. else
  276. {
  277. struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
  278. struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
  279. struct tunnel_notify_queue* element = GNUNET_malloc(sizeof *element);
  280. element->cls = cls;
  281. element->len = ntohs(hdr->size);
  282. GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
  283. GNUNET_MESH_tunnel_set_head(*tunnel, head);
  284. GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
  285. }
  286. }
  287. /**
  288. * Create a new Address from an answer-packet
  289. */
  290. void
  291. new_ip6addr(unsigned char* buf, const GNUNET_HashCode *peer, const GNUNET_HashCode *service_desc) { /* {{{ */
  292. char* ipv6addr;
  293. unsigned long long ipv6prefix;
  294. GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
  295. GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
  296. GNUNET_assert(ipv6prefix < 127);
  297. ipv6prefix = (ipv6prefix + 7)/8;
  298. inet_pton (AF_INET6, ipv6addr, buf);
  299. GNUNET_free(ipv6addr);
  300. int peer_length = 16 - ipv6prefix - 6;
  301. if (peer_length <= 0)
  302. peer_length = 0;
  303. int service_length = 16 - ipv6prefix - peer_length;
  304. if (service_length <= 0)
  305. service_length = 0;
  306. memcpy(buf+ipv6prefix, service_desc, service_length);
  307. memcpy(buf+ipv6prefix+service_length, peer, peer_length);
  308. }
  309. /*}}}*/
  310. /**
  311. * Create a new Address from an answer-packet
  312. */
  313. void
  314. new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
  315. { /* {{{ */
  316. char *ipv6addr;
  317. unsigned long long ipv6prefix;
  318. GNUNET_assert (GNUNET_OK ==
  319. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  320. "IPV6ADDR",
  321. &ipv6addr));
  322. GNUNET_assert (GNUNET_OK ==
  323. GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
  324. "IPV6PREFIX",
  325. &ipv6prefix));
  326. GNUNET_assert (ipv6prefix < 127);
  327. ipv6prefix = (ipv6prefix + 7) / 8;
  328. inet_pton (AF_INET6, ipv6addr, buf);
  329. GNUNET_free (ipv6addr);
  330. int local_length = 16 - ipv6prefix;
  331. memcpy (buf + ipv6prefix, addr, GNUNET_MIN (addrlen, local_length));
  332. }
  333. /*}}}*/
  334. /**
  335. * Create a new Address from an answer-packet
  336. */
  337. void
  338. new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
  339. { /* {{{ */
  340. char *ipv4addr;
  341. char *ipv4mask;
  342. GNUNET_assert (GNUNET_OK ==
  343. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  344. "IPV4ADDR",
  345. &ipv4addr));
  346. GNUNET_assert (GNUNET_OK ==
  347. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  348. "IPV4MASK",
  349. &ipv4mask));
  350. uint32_t mask;
  351. inet_pton (AF_INET, ipv4addr, buf);
  352. int r = inet_pton (AF_INET, ipv4mask, &mask);
  353. mask = htonl(mask);
  354. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "inet_pton: %d; %m; mask: %08x\n", r, mask);
  355. GNUNET_free (ipv4addr);
  356. int c;
  357. if (mask)
  358. {
  359. mask = (mask ^ (mask - 1)) >> 1;
  360. for (c = 0; mask; c++)
  361. {
  362. mask >>= 1;
  363. }
  364. }
  365. else
  366. {
  367. c = CHAR_BIT * sizeof(mask);
  368. }
  369. c = 32-c;
  370. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The mask %s has %d leading 1s.\n", ipv4mask, c);
  371. GNUNET_free (ipv4mask);
  372. if (c % 8 == 0)
  373. c = c / 8;
  374. else
  375. GNUNET_assert(0);
  376. memcpy (buf + c, addr, GNUNET_MIN (addrlen, 4-c));
  377. }
  378. /*}}}*/
  379. /**
  380. * This gets scheduled with cls pointing to an answer_packet and does everything
  381. * needed in order to send it to the helper.
  382. *
  383. * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
  384. * doing nothing for "real" services.
  385. */
  386. void
  387. process_answer(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
  388. if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  389. return;
  390. struct answer_packet* pkt = cls;
  391. struct answer_packet_list* list;
  392. /* This answer is about a .gnunet-service
  393. *
  394. * It contains an almost complete DNS-Response, we have to fill in the ip
  395. * at the offset pkt->addroffset
  396. */
  397. if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
  398. {
  399. pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
  400. GNUNET_HashCode key;
  401. memset(&key, 0, sizeof(GNUNET_HashCode));
  402. unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
  403. unsigned char* k = (unsigned char*)&key;
  404. new_ip6addr(c, &pkt->service_descr.peer, &pkt->service_descr.service_descriptor);
  405. /*
  406. * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
  407. */
  408. unsigned int i;
  409. for (i = 0; i < 16; i++)
  410. k[15-i] = c[i];
  411. uint16_t namelen = strlen((char*)pkt->data+12)+1;
  412. struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
  413. char* name = (char*)(value +1);
  414. value->namelen = namelen;
  415. memcpy(name, pkt->data+12, namelen);
  416. memcpy(&value->desc, &pkt->service_descr, sizeof(struct GNUNET_vpn_service_descriptor));
  417. memset(value->additional_ports, 0, 8192);
  418. memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
  419. if (GNUNET_NO ==
  420. GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
  421. {
  422. GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
  423. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  424. value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
  425. GNUNET_TIME_absolute_get ().abs_value);
  426. if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
  427. GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
  428. }
  429. else
  430. GNUNET_free(value);
  431. list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
  432. memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
  433. }
  434. else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
  435. {
  436. GNUNET_HashCode key;
  437. memset(&key, 0, sizeof key);
  438. unsigned char* k = (unsigned char*)&key;
  439. unsigned char* s = pkt->data+12;
  440. int i = 0;
  441. /* Whoever designed the reverse IPv6-lookup is batshit insane */
  442. for (i = 0; i < 16; i++)
  443. {
  444. unsigned char c1 = s[(4*i)+1];
  445. unsigned char c2 = s[(4*i)+3];
  446. if (c1 <= '9')
  447. k[i] = c1 - '0';
  448. else
  449. k[i] = c1 - 87; /* 87 is the difference between 'a' and 10 */
  450. if (c2 <= '9')
  451. k[i] += 16*(c2 - '0');
  452. else
  453. k[i] += 16*(c2 - 87);
  454. }
  455. struct map_entry* map_entry = GNUNET_CONTAINER_multihashmap_get(hashmap, &key);
  456. uint16_t offset = ntohs(pkt->addroffset);
  457. if (map_entry == NULL)
  458. {
  459. GNUNET_free(pkt);
  460. return;
  461. }
  462. GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
  463. GNUNET_TIME_absolute_get ().abs_value);
  464. unsigned short namelen = htons(map_entry->namelen);
  465. char* name = (char*)(map_entry + 1);
  466. list = GNUNET_malloc(2*sizeof(struct answer_packet_list*) + offset + 2 + ntohs(namelen));
  467. struct answer_packet* rpkt = &list->pkt;
  468. /* The offset points to the first byte belonging to the address */
  469. memcpy(rpkt, pkt, offset - 1);
  470. rpkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
  471. rpkt->hdr.size = ntohs(offset + 2 + ntohs(namelen));
  472. memcpy(((char*)rpkt)+offset, &namelen, 2);
  473. memcpy(((char*)rpkt)+offset+2, name, ntohs(namelen));
  474. }
  475. else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_IP)
  476. {
  477. list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
  478. memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
  479. }
  480. else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
  481. {
  482. pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
  483. GNUNET_HashCode key;
  484. memset(&key, 0, sizeof(GNUNET_HashCode));
  485. unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
  486. new_ip6addr_remote(c, pkt->addr, pkt->addrsize);
  487. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
  488. c[0],
  489. c[1],
  490. c[2],
  491. c[3],
  492. c[4],
  493. c[5],
  494. c[6],
  495. c[7],
  496. c[8],
  497. c[9],
  498. c[10],
  499. c[11],
  500. c[12],
  501. c[13],
  502. c[14],
  503. c[15]);
  504. unsigned char* k = (unsigned char*)&key;
  505. /*
  506. * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
  507. */
  508. unsigned int i;
  509. for (i = 0; i < 16; i++)
  510. k[15-i] = c[i];
  511. uint16_t namelen = strlen((char*)pkt->data+12)+1;
  512. struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
  513. char* name = (char*)(value +1);
  514. value->namelen = namelen;
  515. memcpy(name, pkt->data+12, namelen);
  516. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
  517. value->addrlen = pkt->addrsize;
  518. memcpy(&value->addr, &pkt->addr, pkt->addrsize);
  519. memset(value->additional_ports, 0, 8192);
  520. memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
  521. if (GNUNET_NO ==
  522. GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
  523. {
  524. GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
  525. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  526. value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
  527. GNUNET_TIME_absolute_get ().abs_value);
  528. if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
  529. GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
  530. }
  531. else
  532. GNUNET_free(value);
  533. list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
  534. memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
  535. }
  536. else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
  537. {
  538. pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
  539. GNUNET_HashCode key;
  540. memset(&key, 0, sizeof(GNUNET_HashCode));
  541. unsigned char* c = ((unsigned char*)pkt)+ntohs(pkt->addroffset);
  542. new_ip4addr_remote(c, pkt->addr, pkt->addrsize);
  543. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
  544. c[0],
  545. c[1],
  546. c[2],
  547. c[3]);
  548. unsigned char* k = (unsigned char*)&key;
  549. /*
  550. * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
  551. */
  552. unsigned int i;
  553. for (i = 0; i < 4; i++)
  554. k[3-i] = c[i];
  555. uint16_t namelen = strlen((char*)pkt->data+12)+1;
  556. struct map_entry* value = GNUNET_malloc(sizeof(struct map_entry) + namelen);
  557. char* name = (char*)(value +1);
  558. value->namelen = namelen;
  559. memcpy(name, pkt->data+12, namelen);
  560. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n", pkt->addrsize);
  561. value->addrlen = pkt->addrsize;
  562. memcpy(&value->addr, &pkt->addr, pkt->addrsize);
  563. memset(value->additional_ports, 0, 8192);
  564. memcpy(&value->hash, &key, sizeof(GNUNET_HashCode));
  565. if (GNUNET_NO ==
  566. GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
  567. {
  568. GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
  569. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  570. value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
  571. GNUNET_TIME_absolute_get ().abs_value);
  572. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping is saved in the hashmap with key %08x.\n",
  573. *((uint32_t*)(&key)));
  574. if (GNUNET_CONTAINER_heap_get_size(heap) > max_mappings)
  575. GNUNET_SCHEDULER_add_now(collect_mappings, NULL);
  576. }
  577. else
  578. GNUNET_free(value);
  579. list = GNUNET_malloc(htons(pkt->hdr.size) + 2*sizeof(struct answer_packet_list*));
  580. memcpy(&list->pkt, pkt, htons(pkt->hdr.size));
  581. }
  582. else
  583. {
  584. GNUNET_break(0);
  585. GNUNET_free(pkt);
  586. return;
  587. }
  588. GNUNET_free(pkt);
  589. GNUNET_CONTAINER_DLL_insert_after(answer_proc_head, answer_proc_tail, answer_proc_tail, list);
  590. schedule_helper_write(GNUNET_TIME_UNIT_FOREVER_REL, NULL);
  591. return;
  592. }
  593. /**
  594. * Sets a bit active in a bitArray.
  595. *
  596. * @param bitArray memory area to set the bit in
  597. * @param bitIdx which bit to set
  598. */
  599. void
  600. setBit (char *bitArray, unsigned int bitIdx)
  601. {
  602. size_t arraySlot;
  603. unsigned int targetBit;
  604. arraySlot = bitIdx / 8;
  605. targetBit = (1L << (bitIdx % 8));
  606. bitArray[arraySlot] |= targetBit;
  607. }
  608. /**
  609. * Clears a bit from bitArray.
  610. *
  611. * @param bitArray memory area to set the bit in
  612. * @param bitIdx which bit to unset
  613. */
  614. void
  615. clearBit (char *bitArray, unsigned int bitIdx)
  616. {
  617. size_t slot;
  618. unsigned int targetBit;
  619. slot = bitIdx / 8;
  620. targetBit = (1L << (bitIdx % 8));
  621. bitArray[slot] = bitArray[slot] & (~targetBit);
  622. }
  623. /**
  624. * Checks if a bit is active in the bitArray
  625. *
  626. * @param bitArray memory area to set the bit in
  627. * @param bitIdx which bit to test
  628. * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
  629. */
  630. int
  631. testBit (char *bitArray, unsigned int bitIdx)
  632. {
  633. size_t slot;
  634. unsigned int targetBit;
  635. slot = bitIdx / 8;
  636. targetBit = (1L << (bitIdx % 8));
  637. if (bitArray[slot] & targetBit)
  638. return GNUNET_YES;
  639. else
  640. return GNUNET_NO;
  641. }
  642. /**
  643. * @brief Add the port to the list of additional ports in the map_entry
  644. *
  645. * @param me the map_entry
  646. * @param port the port in host-byte-order
  647. */
  648. static void
  649. add_additional_port (struct map_entry *me, uint16_t port)
  650. {
  651. setBit(me->additional_ports, port);
  652. }
  653. static int
  654. receive_udp_back (void *cls
  655. __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
  656. void **tunnel_ctx __attribute__ ((unused)),
  657. const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)),
  658. const struct GNUNET_MessageHeader *message,
  659. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__ ((unused)))
  660. {
  661. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  662. struct remote_addr *s = (struct remote_addr *) desc;
  663. struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
  664. const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
  665. if (16 == s->addrlen)
  666. {
  667. size_t size =
  668. sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
  669. struct ip6_udp *pkt6 = alloca (size);
  670. GNUNET_assert (pkt6 != NULL);
  671. if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
  672. new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
  673. else
  674. new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
  675. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  676. "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
  677. ntohs (message->size), ntohs (pkt->len));
  678. pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  679. pkt6->shdr.size = htons (size);
  680. pkt6->tun.flags = 0;
  681. pkt6->tun.type = htons (0x86dd);
  682. pkt6->ip6_hdr.version = 6;
  683. pkt6->ip6_hdr.tclass_h = 0;
  684. pkt6->ip6_hdr.tclass_l = 0;
  685. pkt6->ip6_hdr.flowlbl = 0;
  686. pkt6->ip6_hdr.paylgth = pkt->len;
  687. pkt6->ip6_hdr.nxthdr = 0x11;
  688. pkt6->ip6_hdr.hoplmt = 0xff;
  689. {
  690. char *ipv6addr;
  691. GNUNET_assert (GNUNET_OK ==
  692. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  693. "IPV6ADDR",
  694. &ipv6addr));
  695. inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
  696. GNUNET_free (ipv6addr);
  697. }
  698. memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
  699. GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
  700. GNUNET_assert (key != NULL);
  701. struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  702. GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
  703. GNUNET_TIME_absolute_get ().
  704. abs_value);
  705. GNUNET_free (key);
  706. GNUNET_assert (me != NULL);
  707. if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
  708. {
  709. GNUNET_assert (me->desc.
  710. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP));
  711. if (!port_in_ports (me->desc.ports, pkt6->udp_hdr.spt)
  712. && !testBit (me->additional_ports, ntohs (pkt6->udp_hdr.spt)))
  713. {
  714. add_additional_port (me, ntohs (pkt6->udp_hdr.spt));
  715. }
  716. }
  717. pkt6->udp_hdr.crc = 0;
  718. uint32_t sum = 0;
  719. sum =
  720. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
  721. 16);
  722. sum =
  723. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
  724. 16);
  725. uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
  726. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  727. tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
  728. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  729. sum =
  730. calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
  731. ntohs (pkt->len));
  732. pkt6->udp_hdr.crc = calculate_checksum_end (sum);
  733. write_to_helper (pkt6, size);
  734. }
  735. else
  736. {
  737. size_t size =
  738. sizeof (struct ip_udp) + ntohs (pkt->len) - 1 - sizeof (struct udp_pkt);
  739. struct ip_udp *pkt4 = alloca (size);
  740. GNUNET_assert (pkt4 != NULL);
  741. GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
  742. uint32_t sadr;
  743. new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen);
  744. pkt4->ip_hdr.sadr = sadr;
  745. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  746. "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
  747. ntohs (message->size), ntohs (pkt->len));
  748. pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  749. pkt4->shdr.size = htons (size);
  750. pkt4->tun.flags = 0;
  751. pkt4->tun.type = htons (0x0800);
  752. pkt4->ip_hdr.version = 4;
  753. pkt4->ip_hdr.hdr_lngth = 5;
  754. pkt4->ip_hdr.diff_serv = 0;
  755. pkt4->ip_hdr.tot_lngth = htons (20 + ntohs(pkt->len));
  756. pkt4->ip_hdr.ident = 0;
  757. pkt4->ip_hdr.flags = 0;
  758. pkt4->ip_hdr.frag_off = 0;
  759. pkt4->ip_hdr.ttl = 255;
  760. pkt4->ip_hdr.proto = 0x11;
  761. pkt4->ip_hdr.chks = 0; /* Will be calculated later */
  762. {
  763. char *ipv4addr;
  764. uint32_t dadr;
  765. GNUNET_assert (GNUNET_OK ==
  766. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  767. "IPV4ADDR",
  768. &ipv4addr));
  769. inet_pton (AF_INET, ipv4addr, &dadr);
  770. GNUNET_free (ipv4addr);
  771. pkt4->ip_hdr.dadr = dadr;
  772. }
  773. memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
  774. GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
  775. GNUNET_assert (key != NULL);
  776. struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  777. GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
  778. GNUNET_TIME_absolute_get ().
  779. abs_value);
  780. GNUNET_free (key);
  781. GNUNET_assert (me != NULL);
  782. pkt4->udp_hdr.crc = 0; /* Optional for IPv4 */
  783. pkt4->ip_hdr.chks =
  784. calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
  785. write_to_helper (pkt4, size);
  786. }
  787. return GNUNET_OK;
  788. }
  789. static int
  790. receive_tcp_back (void *cls __attribute__((unused)), struct GNUNET_MESH_Tunnel* tunnel,
  791. void **tunnel_ctx __attribute__((unused)),
  792. const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
  793. const struct GNUNET_MessageHeader *message,
  794. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  795. {
  796. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  797. struct remote_addr *s = (struct remote_addr *) desc;
  798. struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
  799. const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
  800. size_t pktlen =
  801. ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
  802. sizeof (GNUNET_HashCode);
  803. if (s->addrlen == 16)
  804. {
  805. size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
  806. struct ip6_tcp *pkt6 = alloca (size);
  807. memset (pkt6, 0, size);
  808. GNUNET_assert (pkt6 != NULL);
  809. if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK)
  810. new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
  811. else
  812. new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
  813. pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  814. pkt6->shdr.size = htons (size);
  815. pkt6->tun.flags = 0;
  816. pkt6->tun.type = htons (0x86dd);
  817. pkt6->ip6_hdr.version = 6;
  818. pkt6->ip6_hdr.tclass_h = 0;
  819. pkt6->ip6_hdr.tclass_l = 0;
  820. pkt6->ip6_hdr.flowlbl = 0;
  821. pkt6->ip6_hdr.paylgth = htons (pktlen);
  822. pkt6->ip6_hdr.nxthdr = 0x06;
  823. pkt6->ip6_hdr.hoplmt = 0xff;
  824. {
  825. char *ipv6addr;
  826. GNUNET_assert (GNUNET_OK ==
  827. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  828. "IPV6ADDR",
  829. &ipv6addr));
  830. inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
  831. GNUNET_free (ipv6addr);
  832. }
  833. memcpy (&pkt6->tcp_hdr, pkt, pktlen);
  834. GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
  835. GNUNET_assert (key != NULL);
  836. struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  837. GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
  838. GNUNET_TIME_absolute_get ().
  839. abs_value);
  840. GNUNET_free (key);
  841. GNUNET_assert (me != NULL);
  842. if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK)
  843. GNUNET_assert (me->desc.
  844. service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP));
  845. pkt6->tcp_hdr.crc = 0;
  846. uint32_t sum = 0;
  847. uint32_t tmp;
  848. sum =
  849. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr,
  850. 16);
  851. sum =
  852. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr,
  853. 16);
  854. tmp = htonl (pktlen);
  855. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  856. tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
  857. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  858. sum =
  859. calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
  860. ntohs (pkt6->ip6_hdr.paylgth));
  861. pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
  862. write_to_helper (pkt6, size);
  863. }
  864. else
  865. {
  866. size_t size = pktlen + sizeof (struct ip_tcp) - 1;
  867. struct ip_tcp *pkt4 = alloca (size);
  868. GNUNET_assert (pkt4 != NULL);
  869. memset (pkt4, 0, size);
  870. GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
  871. uint32_t sadr;
  872. new_ip4addr_remote ((unsigned char*)&sadr, s->addr, s->addrlen);
  873. pkt4->ip_hdr.sadr = sadr;
  874. pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  875. pkt4->shdr.size = htons (size);
  876. pkt4->tun.flags = 0;
  877. pkt4->tun.type = htons (0x0800);
  878. pkt4->ip_hdr.version = 4;
  879. pkt4->ip_hdr.hdr_lngth = 5;
  880. pkt4->ip_hdr.diff_serv = 0;
  881. pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
  882. pkt4->ip_hdr.ident = 0;
  883. pkt4->ip_hdr.flags = 0;
  884. pkt4->ip_hdr.frag_off = 0;
  885. pkt4->ip_hdr.ttl = 255;
  886. pkt4->ip_hdr.proto = 0x06;
  887. pkt4->ip_hdr.chks = 0; /* Will be calculated later */
  888. {
  889. char *ipv4addr;
  890. uint32_t dadr;
  891. GNUNET_assert (GNUNET_OK ==
  892. GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
  893. "IPV4ADDR",
  894. &ipv4addr));
  895. inet_pton (AF_INET, ipv4addr, &dadr);
  896. GNUNET_free (ipv4addr);
  897. pkt4->ip_hdr.dadr = dadr;
  898. }
  899. memcpy (&pkt4->tcp_hdr, pkt, pktlen);
  900. GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
  901. GNUNET_assert (key != NULL);
  902. struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
  903. GNUNET_CONTAINER_heap_update_cost (heap, me->heap_node,
  904. GNUNET_TIME_absolute_get ().
  905. abs_value);
  906. GNUNET_free (key);
  907. GNUNET_assert (me != NULL);
  908. pkt4->tcp_hdr.crc = 0;
  909. uint32_t sum = 0;
  910. uint32_t tmp;
  911. tmp = pkt4->ip_hdr.sadr;
  912. sum =
  913. calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  914. tmp = pkt4->ip_hdr.dadr;
  915. sum =
  916. calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  917. tmp = (0x06 << 16) | (0xffff & pktlen);
  918. tmp = htonl(tmp);
  919. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  920. sum =
  921. calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
  922. pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
  923. pkt4->ip_hdr.chks =
  924. calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
  925. write_to_helper (pkt4, size);
  926. }
  927. return GNUNET_OK;
  928. }
  929. /**
  930. * Main function that will be run by the scheduler.
  931. *
  932. * @param cls closure
  933. * @param args remaining command-line arguments
  934. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  935. * @param cfg_ configuration
  936. */
  937. static void
  938. run (void *cls,
  939. char *const *args __attribute__((unused)),
  940. const char *cfgfilep __attribute__((unused)),
  941. const struct GNUNET_CONFIGURATION_Handle *cfg_)
  942. {
  943. static const struct GNUNET_MESH_MessageHandler handlers[] = {
  944. {receive_udp_back, GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK, 0},
  945. {receive_tcp_back, GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK, 0},
  946. {receive_udp_back, GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK, 0},
  947. {receive_tcp_back, GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK, 0},
  948. {NULL, 0, 0}
  949. };
  950. static const GNUNET_MESH_ApplicationType types[] = {
  951. GNUNET_APPLICATION_TYPE_END
  952. };
  953. mesh_handle = GNUNET_MESH_connect(cfg_,
  954. NULL,
  955. NULL,
  956. handlers,
  957. types);
  958. cfg = cfg_;
  959. restart_hijack = 0;
  960. hashmap = GNUNET_CONTAINER_multihashmap_create(65536);
  961. heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  962. GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
  963. &max_mappings);
  964. udp_connections = GNUNET_CONTAINER_multihashmap_create(65536);
  965. conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
  966. shs_task = GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
  967. GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
  968. }
  969. /**
  970. * The main function to obtain template from gnunetd.
  971. *
  972. * @param argc number of arguments from the command line
  973. * @param argv command line arguments
  974. * @return 0 ok, 1 on error
  975. */
  976. int
  977. main (int argc, char *const *argv) {
  978. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  979. GNUNET_GETOPT_OPTION_END
  980. };
  981. return (GNUNET_OK ==
  982. GNUNET_PROGRAM_run (argc,
  983. argv,
  984. "vpn",
  985. gettext_noop ("help text"),
  986. options, &run, NULL)) ? ret : 1;
  987. }
  988. /* end of gnunet-daemon-vpn.c */