gnunet-daemon-exit.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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-exit.c
  19. * @brief
  20. * @author Philipp Toelke
  21. */
  22. #include <platform.h>
  23. #include <gnunet_common.h>
  24. #include <gnunet_program_lib.h>
  25. #include <gnunet_protocols.h>
  26. #include <gnunet_applications.h>
  27. #include <gnunet_mesh_service.h>
  28. #include <gnunet_constants.h>
  29. #include <string.h>
  30. #include "gnunet-vpn-packet.h"
  31. #include "gnunet-helper-vpn-api.h"
  32. #include "gnunet-vpn-checksum.h"
  33. GNUNET_SCHEDULER_TaskIdentifier shs_task;
  34. /**
  35. * The handle to the configuration used throughout the process
  36. */
  37. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  38. /**
  39. * The handle to the helper
  40. */
  41. struct GNUNET_VPN_HELPER_Handle *helper_handle;
  42. /**
  43. * Final status code.
  44. */
  45. static int ret;
  46. /**
  47. * The handle to mesh
  48. */
  49. static struct GNUNET_MESH_Handle *mesh_handle;
  50. /**
  51. * This hashmaps contains the mapping from peer, service-descriptor,
  52. * source-port and destination-port to a struct redirect_state
  53. */
  54. static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
  55. static struct GNUNET_CONTAINER_Heap *udp_connections_heap;
  56. static struct GNUNET_CONTAINER_MultiHashMap *tcp_connections;
  57. static struct GNUNET_CONTAINER_Heap *tcp_connections_heap;
  58. /**
  59. * If there are at least this many udp-Connections, old ones will be removed
  60. */
  61. static long long unsigned int max_udp_connections = 200;
  62. /**
  63. * If there are at least this many tcp-Connections, old ones will be removed
  64. */
  65. static long long unsigned int max_tcp_connections = 200;
  66. struct remote_addr
  67. {
  68. char addrlen;
  69. unsigned char addr[16];
  70. char proto;
  71. };
  72. /**
  73. * This struct is saved into the services-hashmap
  74. */
  75. struct redirect_service
  76. {
  77. /**
  78. * One of 4 or 6
  79. */
  80. unsigned int version;
  81. uint16_t my_port;
  82. uint16_t remote_port;
  83. union
  84. {
  85. struct
  86. {
  87. char ip4address[4];
  88. } v4;
  89. struct
  90. {
  91. char ip6address[16];
  92. } v6;
  93. };
  94. };
  95. struct redirect_info
  96. {
  97. /**
  98. * The source-address of this connection. When a packet to this address is
  99. * received, this tunnel is used to forward it. ipv4-addresses will be put
  100. * here left-aligned */
  101. char addr[16];
  102. /**
  103. * The source-port of this connection
  104. */
  105. uint16_t pt;
  106. };
  107. /**
  108. * This struct is saved into {tcp,udp}_connections;
  109. */
  110. struct redirect_state
  111. {
  112. struct GNUNET_MESH_Tunnel *tunnel;
  113. GNUNET_HashCode desc;
  114. struct redirect_service *serv;
  115. struct remote_addr remote;
  116. struct GNUNET_CONTAINER_HeapNode* heap_node;
  117. struct GNUNET_CONTAINER_MultiHashMap *hashmap;
  118. GNUNET_HashCode hash;
  119. enum { SERVICE, REMOTE } type;
  120. /**
  121. * The source-address and -port of this connection
  122. */
  123. struct redirect_info redirect_info;
  124. };
  125. /**
  126. * This hashmaps saves interesting things about the configured services
  127. */
  128. static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
  129. static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
  130. struct tunnel_notify_queue
  131. {
  132. struct tunnel_notify_queue* next;
  133. struct tunnel_notify_queue* prev;
  134. void* cls;
  135. size_t len;
  136. };
  137. /**
  138. * Function that frees everything from a hashmap
  139. */
  140. static int
  141. free_iterate(void* cls __attribute__((unused)), const GNUNET_HashCode* hash __attribute__((unused)), void* value)
  142. {
  143. GNUNET_free(value);
  144. return GNUNET_YES;
  145. }
  146. /**
  147. * Function scheduled as very last function, cleans up after us
  148. */
  149. static void
  150. cleanup(void* cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
  151. GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
  152. GNUNET_CONTAINER_multihashmap_iterate(udp_connections,
  153. free_iterate,
  154. NULL);
  155. GNUNET_CONTAINER_multihashmap_iterate(tcp_connections,
  156. free_iterate,
  157. NULL);
  158. if (mesh_handle != NULL)
  159. {
  160. GNUNET_MESH_disconnect(mesh_handle);
  161. mesh_handle = NULL;
  162. }
  163. }
  164. static void
  165. collect_connections(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
  166. if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  167. return;
  168. struct GNUNET_CONTAINER_Heap *heap = cls;
  169. struct redirect_state* state = GNUNET_CONTAINER_heap_remove_root(heap);
  170. /* This is free()ed memory! */
  171. state->heap_node = NULL;
  172. /* FIXME! GNUNET_MESH_close_tunnel(state->tunnel); */
  173. GNUNET_CONTAINER_multihashmap_remove(state->hashmap, &state->hash, state);
  174. GNUNET_free(state);
  175. }
  176. static void
  177. hash_redirect_info(GNUNET_HashCode* hash, struct redirect_info* u_i, size_t addrlen)
  178. {
  179. /* the gnunet hashmap only uses the first sizeof(unsigned int) of the hash
  180. *
  181. * build the hash out of the last bytes of the address and the 2 bytes of
  182. * the port
  183. */
  184. memcpy(hash, &u_i->pt, sizeof(u_i->pt));
  185. memcpy(((unsigned char*)hash)+2, u_i->addr+(addrlen-(sizeof(unsigned int) - 2)), (sizeof(unsigned int) - 2));
  186. memset(((unsigned char*)hash)+sizeof(unsigned int), 0, sizeof(GNUNET_HashCode) - sizeof(unsigned int));
  187. }
  188. /**
  189. * cls is the pointer to a GNUNET_MessageHeader that is
  190. * followed by the service-descriptor and the udp-packet that should be sent;
  191. */
  192. static size_t
  193. send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf)
  194. {
  195. struct GNUNET_MESH_Tunnel** tunnel = cls;
  196. GNUNET_MESH_tunnel_set_data(*tunnel, NULL);
  197. struct GNUNET_MessageHeader *hdr = (struct GNUNET_MessageHeader*)(tunnel + 1);
  198. GNUNET_assert (size >= ntohs (hdr->size));
  199. memcpy (buf, hdr, ntohs (hdr->size));
  200. size = ntohs(hdr->size);
  201. if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
  202. {
  203. struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
  204. struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
  205. struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
  206. GNUNET_CONTAINER_DLL_remove(head, tail, element);
  207. GNUNET_MESH_tunnel_set_head(*tunnel, head);
  208. GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
  209. struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
  210. GNUNET_NO,
  211. 42,
  212. GNUNET_TIME_relative_divide
  213. (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
  214. (const struct GNUNET_PeerIdentity *)
  215. NULL, element->len,
  216. send_udp_to_peer_notify_callback, element->cls);
  217. /* save the handle */
  218. GNUNET_MESH_tunnel_set_data(*tunnel, th);
  219. GNUNET_free(element);
  220. }
  221. GNUNET_free (cls);
  222. return size;
  223. }
  224. /**
  225. * @brief Handles an UDP-Packet received from the helper.
  226. *
  227. * @param udp A pointer to the Packet
  228. * @param dadr The IP-Destination-address
  229. * @param addrlen The length of the address
  230. * @param version 4 or 6
  231. */
  232. static void
  233. udp_from_helper (struct udp_pkt *udp, unsigned char *dadr, size_t addrlen)
  234. {
  235. struct redirect_info u_i;
  236. struct GNUNET_MESH_Tunnel *tunnel;
  237. uint32_t len;
  238. struct GNUNET_MessageHeader *msg;
  239. memset (&u_i, 0, sizeof (struct redirect_info));
  240. memcpy (&u_i.addr, dadr, addrlen);
  241. u_i.pt = udp->dpt;
  242. /* get tunnel and service-descriptor from this */
  243. GNUNET_HashCode hash;
  244. hash_redirect_info(&hash, &u_i, addrlen);
  245. struct redirect_state *state =
  246. GNUNET_CONTAINER_multihashmap_get (udp_connections, &hash);
  247. /* Mark this connection as freshly used */
  248. GNUNET_CONTAINER_heap_update_cost (udp_connections_heap, state->heap_node,
  249. GNUNET_TIME_absolute_get ().abs_value);
  250. tunnel = state->tunnel;
  251. if (state->type == SERVICE)
  252. {
  253. /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
  254. if (ntohs (udp->spt) == state->serv->remote_port)
  255. {
  256. udp->spt = htons (state->serv->my_port);
  257. }
  258. else
  259. {
  260. /* otherwise the answer came from a different port (tftp does this)
  261. * add this new port to the list of all services, so that the packets
  262. * coming back from the client to this new port will be routed correctly
  263. */
  264. struct redirect_service *serv =
  265. GNUNET_malloc (sizeof (struct redirect_service));
  266. memcpy (serv, state->serv, sizeof (struct redirect_service));
  267. serv->my_port = ntohs (udp->spt);
  268. serv->remote_port = ntohs (udp->spt);
  269. uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
  270. memcpy ((GNUNET_HashCode *) (desc + 1), &state->desc,
  271. sizeof (GNUNET_HashCode));
  272. *desc = ntohs (udp->spt);
  273. GNUNET_assert (GNUNET_OK ==
  274. GNUNET_CONTAINER_multihashmap_put (udp_services,
  275. (GNUNET_HashCode*)desc, serv,
  276. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  277. state->serv = serv;
  278. }
  279. }
  280. /* send udp-packet back */
  281. len =
  282. sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
  283. ntohs (udp->len);
  284. struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len);
  285. *ctunnel = tunnel;
  286. msg = (struct GNUNET_MessageHeader*)(ctunnel + 1);
  287. msg->size = htons (len);
  288. msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_UDP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_UDP_BACK);
  289. GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
  290. if (state->type == SERVICE)
  291. memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
  292. else
  293. memcpy (desc, &state->remote, sizeof (struct remote_addr));
  294. void *_udp = desc + 1;
  295. memcpy (_udp, udp, ntohs (udp->len));
  296. if (NULL == GNUNET_MESH_tunnel_get_data(tunnel))
  297. {
  298. /* No notify is pending */
  299. struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel,
  300. GNUNET_NO,
  301. 42,
  302. GNUNET_TIME_relative_divide
  303. (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
  304. (const struct GNUNET_PeerIdentity *)
  305. NULL, len,
  306. send_udp_to_peer_notify_callback, ctunnel);
  307. /* save the handle */
  308. GNUNET_MESH_tunnel_set_data(tunnel, th);
  309. }
  310. else
  311. {
  312. struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel);
  313. struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel);
  314. struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
  315. element->cls = ctunnel;
  316. element->len = len;
  317. GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
  318. GNUNET_MESH_tunnel_set_head(tunnel, head);
  319. GNUNET_MESH_tunnel_set_tail(tunnel, tail);
  320. }
  321. }
  322. /**
  323. * @brief Handles a TCP-Packet received from the helper.
  324. *
  325. * @param tcp A pointer to the Packet
  326. * @param dadr The IP-Destination-address
  327. * @param addrlen The length of the address
  328. * @param version 4 or 6
  329. * @param pktlen the length of the packet, including its header
  330. */
  331. static void
  332. tcp_from_helper (struct tcp_pkt *tcp, unsigned char *dadr, size_t addrlen,
  333. size_t pktlen)
  334. {
  335. struct redirect_info u_i;
  336. struct GNUNET_MESH_Tunnel *tunnel;
  337. uint32_t len;
  338. struct GNUNET_MessageHeader *msg;
  339. memset (&u_i, 0, sizeof (struct redirect_info));
  340. memcpy (&u_i.addr, dadr, addrlen);
  341. u_i.pt = tcp->dpt;
  342. /* get tunnel and service-descriptor from this */
  343. GNUNET_HashCode hash;
  344. hash_redirect_info(&hash, &u_i, addrlen);
  345. struct redirect_state *state =
  346. GNUNET_CONTAINER_multihashmap_get (tcp_connections, &hash);
  347. if (state == NULL)
  348. {
  349. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No mapping for this connection; hash is %x\n", *((uint32_t*)&hash));
  350. return;
  351. }
  352. /* Mark this connection as freshly used */
  353. GNUNET_CONTAINER_heap_update_cost (tcp_connections_heap, state->heap_node,
  354. GNUNET_TIME_absolute_get ().abs_value);
  355. tunnel = state->tunnel;
  356. if (state->type == SERVICE)
  357. {
  358. /* check if spt == serv.remote if yes: set spt = serv.myport ("nat") */
  359. if (ntohs (tcp->spt) == state->serv->remote_port)
  360. {
  361. tcp->spt = htons (state->serv->my_port);
  362. }
  363. else
  364. {
  365. // This is an illegal packet.
  366. return;
  367. }
  368. }
  369. /* send tcp-packet back */
  370. len =
  371. sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + pktlen;
  372. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "len: %d\n", pktlen);
  373. struct GNUNET_MESH_Tunnel** ctunnel = GNUNET_malloc (sizeof(struct GNUNET_MESH_TUNNEL*) + len);
  374. *ctunnel = tunnel;
  375. msg = (struct GNUNET_MessageHeader*)(ctunnel + 1);
  376. msg->size = htons (len);
  377. msg->type = htons (state->type == SERVICE ? GNUNET_MESSAGE_TYPE_SERVICE_TCP_BACK : GNUNET_MESSAGE_TYPE_REMOTE_TCP_BACK);
  378. GNUNET_HashCode *desc = (GNUNET_HashCode *) (msg + 1);
  379. if (state->type == SERVICE)
  380. memcpy (desc, &state->desc, sizeof (GNUNET_HashCode));
  381. else
  382. memcpy (desc, &state->remote, sizeof (struct remote_addr));
  383. void *_tcp = desc + 1;
  384. memcpy (_tcp, tcp, pktlen);
  385. if (NULL == GNUNET_MESH_tunnel_get_data(tunnel))
  386. {
  387. /* No notify is pending */
  388. struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (tunnel,
  389. GNUNET_NO,
  390. 42,
  391. GNUNET_TIME_relative_divide
  392. (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
  393. (const struct GNUNET_PeerIdentity *)NULL,
  394. len, send_udp_to_peer_notify_callback,
  395. ctunnel);
  396. /* save the handle */
  397. GNUNET_MESH_tunnel_set_data(tunnel, th);
  398. }
  399. else
  400. {
  401. struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(tunnel);
  402. struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(tunnel);
  403. struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
  404. element->cls = ctunnel;
  405. element->len = len;
  406. GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
  407. }
  408. }
  409. /**
  410. * Receive packets from the helper-process
  411. */
  412. static void
  413. message_token (void *cls __attribute__((unused)),
  414. void *client __attribute__((unused)), const struct GNUNET_MessageHeader *message)
  415. {
  416. GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
  417. struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
  418. /* ethertype is ipv6 */
  419. if (ntohs (pkt_tun->tun.type) == 0x86dd)
  420. {
  421. struct ip6_pkt *pkt6 = (struct ip6_pkt *) pkt_tun;
  422. if (0x11 == pkt6->ip6_hdr.nxthdr)
  423. udp_from_helper (&((struct ip6_udp *) pkt6)->udp_hdr,
  424. (unsigned char *) &pkt6->ip6_hdr.dadr, 16);
  425. else if (0x06 == pkt6->ip6_hdr.nxthdr)
  426. tcp_from_helper (&((struct ip6_tcp *) pkt6)->tcp_hdr,
  427. (unsigned char *) &pkt6->ip6_hdr.dadr, 16,
  428. ntohs (pkt6->ip6_hdr.paylgth));
  429. }
  430. else if (ntohs (pkt_tun->tun.type) == 0x0800)
  431. {
  432. struct ip_pkt *pkt4 = (struct ip_pkt *) pkt_tun;
  433. uint32_t tmp = pkt4->ip_hdr.dadr;
  434. if (0x11 == pkt4->ip_hdr.proto)
  435. udp_from_helper (&((struct ip_udp *) pkt4)->udp_hdr,
  436. (unsigned char *) &tmp, 4);
  437. else if (0x06 == pkt4->ip_hdr.proto)
  438. {
  439. size_t pktlen = ntohs(pkt4->ip_hdr.tot_lngth);
  440. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tot: %d\n", pktlen);
  441. pktlen -= 4*pkt4->ip_hdr.hdr_lngth;
  442. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "-hdr: %d\n", pktlen);
  443. tcp_from_helper (&((struct ip_tcp *) pkt4)->tcp_hdr,
  444. (unsigned char *) &tmp, 4, pktlen);
  445. }
  446. }
  447. else
  448. {
  449. return;
  450. }
  451. }
  452. /**
  453. * Reads the configuration servicecfg and populates udp_services
  454. *
  455. * @param cls unused
  456. * @param section name of section in config, equal to hostname
  457. */
  458. static void
  459. read_service_conf (void *cls __attribute__((unused)), const char *section)
  460. {
  461. if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
  462. return;
  463. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %d %s %s\n", strlen(section), section, section + (strlen(section) - 8));
  464. char *cpy;
  465. char *redirect;
  466. char *hostname;
  467. char *hostport;
  468. uint16_t *desc = alloca (sizeof (GNUNET_HashCode) + 2);
  469. GNUNET_CRYPTO_hash (section, strlen (section) + 1,
  470. (GNUNET_HashCode *) (desc + 1));
  471. #define TCP 2
  472. #define UDP 1
  473. int proto = UDP;
  474. do
  475. {
  476. if (proto == UDP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "UDP_REDIRECTS", &cpy)))
  477. goto next;
  478. else if (proto == TCP && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, section, "TCP_REDIRECTS", &cpy)))
  479. goto next;
  480. for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok
  481. (NULL, " "))
  482. {
  483. if (NULL == (hostname = strstr (redirect, ":")))
  484. {
  485. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
  486. redirect);
  487. continue;
  488. }
  489. hostname[0] = '\0';
  490. hostname++;
  491. if (NULL == (hostport = strstr (hostname, ":")))
  492. {
  493. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n",
  494. redirect);
  495. continue;
  496. }
  497. hostport[0] = '\0';
  498. hostport++;
  499. int local_port = atoi (redirect);
  500. if (!((local_port > 0) && (local_port < 65536)))
  501. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.",
  502. redirect);
  503. *desc = local_port;
  504. struct redirect_service *serv =
  505. GNUNET_malloc (sizeof (struct redirect_service));
  506. memset (serv, 0, sizeof (struct redirect_service));
  507. serv->my_port = local_port;
  508. if (0 == strcmp ("localhost4", hostname))
  509. {
  510. serv->version = 4;
  511. char *ip4addr;
  512. GNUNET_assert (GNUNET_OK ==
  513. GNUNET_CONFIGURATION_get_value_string (cfg,
  514. "exit",
  515. "IPV4ADDR",
  516. &ip4addr));
  517. GNUNET_assert (1 ==
  518. inet_pton (AF_INET, ip4addr,
  519. serv->v4.ip4address));
  520. GNUNET_free (ip4addr);
  521. }
  522. else if (0 == strcmp ("localhost6", hostname))
  523. {
  524. serv->version = 6;
  525. char *ip6addr;
  526. GNUNET_assert (GNUNET_OK ==
  527. GNUNET_CONFIGURATION_get_value_string (cfg,
  528. "exit",
  529. "IPV6ADDR",
  530. &ip6addr));
  531. GNUNET_assert (1 ==
  532. inet_pton (AF_INET6, ip6addr,
  533. serv->v6.ip6address));
  534. GNUNET_free (ip6addr);
  535. }
  536. else
  537. {
  538. // TODO Lookup, yadayadayada
  539. GNUNET_assert (0);
  540. }
  541. serv->remote_port = atoi (hostport);
  542. if (UDP == proto)
  543. GNUNET_assert (GNUNET_OK ==
  544. GNUNET_CONTAINER_multihashmap_put (udp_services,
  545. (GNUNET_HashCode*)desc, serv,
  546. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  547. else
  548. GNUNET_assert (GNUNET_OK ==
  549. GNUNET_CONTAINER_multihashmap_put (tcp_services,
  550. (GNUNET_HashCode*)desc, serv,
  551. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  552. }
  553. GNUNET_free (cpy);
  554. next:
  555. proto = (proto == UDP) ? TCP : UDP;
  556. }
  557. while (proto != UDP);
  558. }
  559. /**
  560. * Start the helper-process
  561. *
  562. * If cls != NULL it is assumed that this function is called as a result of a dying
  563. * helper. cls is then taken as handle to the old helper and is cleaned up.
  564. */
  565. static void
  566. start_helper_and_schedule(void *cls,
  567. const struct GNUNET_SCHEDULER_TaskContext *tc) {
  568. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  569. return;
  570. if (cls != NULL)
  571. cleanup_helper(cls);
  572. cls = NULL;
  573. char* ifname;
  574. char* ipv6addr;
  575. char* ipv6prefix;
  576. char* ipv4addr;
  577. char* ipv4mask;
  578. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IFNAME", &ifname))
  579. {
  580. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n");
  581. exit(1);
  582. }
  583. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6ADDR", &ipv6addr))
  584. {
  585. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n");
  586. exit(1);
  587. }
  588. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV6PREFIX", &ipv6prefix))
  589. {
  590. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6PREFIX' in configuration!\n");
  591. exit(1);
  592. }
  593. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4ADDR", &ipv4addr))
  594. {
  595. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4ADDR' in configuration!\n");
  596. exit(1);
  597. }
  598. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "exit", "IPV4MASK", &ipv4mask))
  599. {
  600. GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV4MASK' in configuration!\n");
  601. exit(1);
  602. }
  603. /* Start the helper
  604. * Messages get passed to the function message_token
  605. * When the helper dies, this function will be called again with the
  606. * helper_handle as cls.
  607. */
  608. helper_handle = start_helper(ifname,
  609. ipv6addr,
  610. ipv6prefix,
  611. ipv4addr,
  612. ipv4mask,
  613. "exit-gnunet",
  614. start_helper_and_schedule,
  615. message_token,
  616. NULL);
  617. GNUNET_free(ipv6addr);
  618. GNUNET_free(ipv6prefix);
  619. GNUNET_free(ipv4addr);
  620. GNUNET_free(ipv4mask);
  621. GNUNET_free(ifname);
  622. }
  623. static void
  624. prepare_ipv4_packet (ssize_t len, ssize_t pktlen, void *payload,
  625. uint16_t protocol, void *ipaddress, void *tunnel,
  626. struct redirect_state *state, struct ip_pkt *pkt4)
  627. {
  628. uint32_t tmp, tmp2;
  629. pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  630. pkt4->shdr.size = htons (len);
  631. pkt4->tun.flags = 0;
  632. pkt4->tun.type = htons (0x0800);
  633. memcpy (&pkt4->data, payload, pktlen);
  634. pkt4->ip_hdr.version = 4;
  635. pkt4->ip_hdr.hdr_lngth = 5;
  636. pkt4->ip_hdr.diff_serv = 0;
  637. pkt4->ip_hdr.tot_lngth = htons (20 + pktlen);
  638. pkt4->ip_hdr.ident = 0;
  639. pkt4->ip_hdr.flags = 0;
  640. pkt4->ip_hdr.frag_off = 0;
  641. pkt4->ip_hdr.ttl = 255;
  642. pkt4->ip_hdr.proto = protocol;
  643. pkt4->ip_hdr.chks = 0; /* Will be calculated later */
  644. memcpy (&tmp, ipaddress, 4);
  645. pkt4->ip_hdr.dadr = tmp;
  646. /* Generate a new src-address */
  647. char *ipv4addr;
  648. char *ipv4mask;
  649. GNUNET_assert (GNUNET_OK ==
  650. GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
  651. "IPV4ADDR",
  652. &ipv4addr));
  653. GNUNET_assert (GNUNET_OK ==
  654. GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
  655. "IPV4MASK",
  656. &ipv4mask));
  657. inet_pton (AF_INET, ipv4addr, &tmp);
  658. inet_pton (AF_INET, ipv4mask, &tmp2);
  659. GNUNET_free (ipv4addr);
  660. GNUNET_free (ipv4mask);
  661. /* This should be a noop */
  662. tmp = tmp & tmp2;
  663. tmp |= ntohl (*((uint32_t *) tunnel)) & (~tmp2);
  664. pkt4->ip_hdr.sadr = tmp;
  665. memcpy (&state->redirect_info.addr, &tmp, 4);
  666. if (0x11 == protocol)
  667. {
  668. struct ip_udp* pkt4_udp = (struct ip_udp*)pkt4;
  669. state->redirect_info.pt = pkt4_udp->udp_hdr.spt;
  670. pkt4_udp->udp_hdr.crc = 0; /* Optional for IPv4 */
  671. }
  672. else if (0x06 == protocol)
  673. {
  674. struct ip_tcp* pkt4_tcp = (struct ip_tcp*)pkt4;
  675. state->redirect_info.pt = pkt4_tcp->tcp_hdr.spt;
  676. pkt4_tcp->tcp_hdr.crc = 0;
  677. uint32_t sum = 0;
  678. tmp = pkt4->ip_hdr.sadr;
  679. sum =
  680. calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  681. tmp = pkt4->ip_hdr.dadr;
  682. sum =
  683. calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  684. tmp = (protocol << 16) | (0xffff & pktlen);
  685. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "line: %08x, %x \n", tmp, (0xffff & pktlen));
  686. tmp = htonl(tmp);
  687. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  688. sum =
  689. calculate_checksum_update (sum, (uint16_t *) & pkt4_tcp->tcp_hdr, pktlen);
  690. pkt4_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
  691. }
  692. pkt4->ip_hdr.chks =
  693. calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
  694. }
  695. static void
  696. prepare_ipv6_packet (ssize_t len, ssize_t pktlen, void *payload,
  697. uint16_t protocol, void *ipaddress, void *tunnel,
  698. struct redirect_state *state, struct ip6_pkt *pkt6)
  699. {
  700. uint32_t tmp;
  701. pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  702. pkt6->shdr.size = htons (len);
  703. pkt6->tun.flags = 0;
  704. pkt6->tun.type = htons (0x86dd);
  705. memcpy (&pkt6->data, payload, pktlen);
  706. pkt6->ip6_hdr.version = 6;
  707. pkt6->ip6_hdr.nxthdr = protocol;
  708. pkt6->ip6_hdr.paylgth = htons (pktlen);
  709. pkt6->ip6_hdr.hoplmt = 64;
  710. memcpy (pkt6->ip6_hdr.dadr, ipaddress, 16);
  711. /* Generate a new src-address
  712. * This takes as much from the address of the tunnel as fits into
  713. * the host-mask*/
  714. char *ipv6addr;
  715. unsigned long long ipv6prefix;
  716. GNUNET_assert (GNUNET_OK ==
  717. GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
  718. "IPV6ADDR",
  719. &ipv6addr));
  720. GNUNET_assert (GNUNET_OK ==
  721. GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
  722. "IPV6PREFIX",
  723. &ipv6prefix));
  724. GNUNET_assert (ipv6prefix < 127);
  725. ipv6prefix = (ipv6prefix + 7) / 8;
  726. inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.sadr);
  727. GNUNET_free (ipv6addr);
  728. if (ipv6prefix < (16 - sizeof (void *)))
  729. ipv6prefix = 16 - sizeof (void *);
  730. unsigned int offset = ipv6prefix - (16 - sizeof (void *));
  731. memcpy ((((char *) &pkt6->ip6_hdr.sadr)) + ipv6prefix,
  732. ((char *) &tunnel) + offset, 16 - ipv6prefix);
  733. /* copy the needed information into the state */
  734. memcpy (&state->redirect_info.addr, &pkt6->ip6_hdr.sadr, 16);
  735. if (0x11 == protocol)
  736. {
  737. struct ip6_udp* pkt6_udp = (struct ip6_udp*)pkt6;
  738. state->redirect_info.pt = pkt6_udp->udp_hdr.spt;
  739. pkt6_udp->udp_hdr.crc = 0;
  740. uint32_t sum = 0;
  741. sum =
  742. calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.sadr, 16);
  743. sum =
  744. calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->ip6_hdr.dadr, 16);
  745. tmp = (htons (pktlen) & 0xffff);
  746. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  747. tmp = htons (((pkt6_udp->ip6_hdr.nxthdr & 0x00ff)));
  748. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  749. sum =
  750. calculate_checksum_update (sum, (uint16_t *) & pkt6_udp->udp_hdr,
  751. ntohs (pkt6_udp->udp_hdr.len));
  752. pkt6_udp->udp_hdr.crc = calculate_checksum_end (sum);
  753. }
  754. else if (0x06 == protocol)
  755. {
  756. struct ip6_tcp* pkt6_tcp = (struct ip6_tcp*)pkt6;
  757. state->redirect_info.pt = pkt6_tcp->tcp_hdr.spt;
  758. pkt6_tcp->tcp_hdr.crc = 0;
  759. uint32_t sum = 0;
  760. sum =
  761. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
  762. sum =
  763. calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
  764. tmp = htonl(pktlen);
  765. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  766. tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
  767. sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
  768. sum =
  769. calculate_checksum_update (sum, (uint16_t *) & pkt6_tcp->tcp_hdr,
  770. ntohs (pkt6->ip6_hdr.paylgth));
  771. pkt6_tcp->tcp_hdr.crc = calculate_checksum_end (sum);
  772. }
  773. }
  774. /**
  775. * The messages are one GNUNET_HashCode for the service followed by a struct tcp_pkt
  776. */
  777. static int
  778. receive_tcp_service (void *cls __attribute__((unused)),
  779. struct GNUNET_MESH_Tunnel *tunnel,
  780. void **tunnel_ctx __attribute__((unused)),
  781. const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
  782. const struct GNUNET_MessageHeader *message,
  783. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  784. {
  785. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet\n");
  786. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  787. struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
  788. unsigned int pkt_len = ntohs (message->size) - sizeof (struct
  789. GNUNET_MessageHeader)
  790. - sizeof (GNUNET_HashCode);
  791. /** Get the configuration from the services-hashmap.
  792. *
  793. * Which service is needed only depends on the service-descriptor and the
  794. * destination-port
  795. */
  796. uint16_t *tcp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
  797. memcpy (tcp_desc + 1, desc, sizeof (GNUNET_HashCode));
  798. *tcp_desc = ntohs (pkt->dpt);
  799. struct redirect_service *serv =
  800. GNUNET_CONTAINER_multihashmap_get (tcp_services, (GNUNET_HashCode*)tcp_desc);
  801. if (NULL == serv)
  802. {
  803. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  804. "No service found for TCP dpt %d!\n", *tcp_desc);
  805. return GNUNET_YES;
  806. }
  807. pkt->dpt = htons (serv->remote_port);
  808. /*
  809. * At this point it would be possible to check against some kind of ACL.
  810. */
  811. char *buf;
  812. size_t len;
  813. /* Prepare the state.
  814. * This will be saved in the hashmap, so that the receiving procedure knows
  815. * through which tunnel this connection has to be routed.
  816. */
  817. struct redirect_state *state =
  818. GNUNET_malloc (sizeof (struct redirect_state));
  819. memset (state, 0, sizeof (struct redirect_state));
  820. state->tunnel = tunnel;
  821. state->serv = serv;
  822. state->type = SERVICE;
  823. state->hashmap = tcp_connections;
  824. memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
  825. len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
  826. sizeof (struct ip6_hdr) + pkt_len;
  827. buf = alloca (len);
  828. memset (buf, 0, len);
  829. switch (serv->version)
  830. {
  831. case 4:
  832. prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
  833. &serv->v4.ip4address,
  834. tunnel, state, (struct ip_pkt *) buf);
  835. break;
  836. case 6:
  837. prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
  838. &serv->v6.ip6address,
  839. tunnel, state, (struct ip6_pkt *) buf);
  840. break;
  841. default:
  842. GNUNET_assert (0);
  843. break;
  844. }
  845. hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
  846. if (GNUNET_NO ==
  847. GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
  848. {
  849. GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
  850. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  851. state->heap_node =
  852. GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
  853. GNUNET_TIME_absolute_get ().abs_value);
  854. if (GNUNET_CONTAINER_heap_get_size(tcp_connections_heap) > max_tcp_connections)
  855. GNUNET_SCHEDULER_add_now(collect_connections, tcp_connections_heap);
  856. }
  857. else
  858. GNUNET_free (state);
  859. (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  860. return GNUNET_YES;
  861. }
  862. static int
  863. receive_tcp_remote (void *cls __attribute__((unused)),
  864. struct GNUNET_MESH_Tunnel *tunnel,
  865. void **tunnel_ctx __attribute__((unused)),
  866. const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
  867. const struct GNUNET_MessageHeader *message,
  868. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  869. {
  870. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  871. struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
  872. struct remote_addr *s = (struct remote_addr *) desc;
  873. char *buf;
  874. size_t len;
  875. unsigned int pkt_len = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - sizeof (GNUNET_HashCode);
  876. struct redirect_state *state =
  877. GNUNET_malloc (sizeof (struct redirect_state));
  878. memset (state, 0, sizeof (struct redirect_state));
  879. state->tunnel = tunnel;
  880. state->type = REMOTE;
  881. state->hashmap = tcp_connections;
  882. memcpy (&state->remote, s, sizeof (struct remote_addr));
  883. len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
  884. sizeof (struct ip6_hdr) + pkt_len;
  885. buf = alloca (len);
  886. memset (buf, 0, len);
  887. switch (s->addrlen)
  888. {
  889. case 4:
  890. prepare_ipv4_packet (len, pkt_len, pkt, 0x06, /* TCP */
  891. &s->addr, tunnel, state, (struct ip_pkt *) buf);
  892. break;
  893. case 16:
  894. prepare_ipv6_packet (len, pkt_len, pkt, 0x06, /* TCP */
  895. &s->addr, tunnel, state, (struct ip6_pkt *) buf);
  896. break;
  897. default:
  898. return GNUNET_SYSERR;
  899. break;
  900. }
  901. hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
  902. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packet from remote; hash is %x\n", *((uint32_t*)&state->hash));
  903. if (GNUNET_NO ==
  904. GNUNET_CONTAINER_multihashmap_contains (tcp_connections, &state->hash))
  905. {
  906. GNUNET_CONTAINER_multihashmap_put (tcp_connections, &state->hash, state,
  907. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  908. state->heap_node =
  909. GNUNET_CONTAINER_heap_insert (tcp_connections_heap, state,
  910. GNUNET_TIME_absolute_get ().abs_value);
  911. if (GNUNET_CONTAINER_heap_get_size (tcp_connections_heap) >
  912. max_tcp_connections)
  913. GNUNET_SCHEDULER_add_now (collect_connections, tcp_connections_heap);
  914. }
  915. else
  916. GNUNET_free (state);
  917. (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  918. return GNUNET_YES;
  919. }
  920. static int
  921. receive_udp_remote (void *cls __attribute__((unused)),
  922. struct GNUNET_MESH_Tunnel *tunnel,
  923. void **tunnel_ctx __attribute__((unused)),
  924. const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
  925. const struct GNUNET_MessageHeader *message,
  926. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  927. {
  928. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  929. struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
  930. struct remote_addr *s = (struct remote_addr *) desc;
  931. char *buf;
  932. size_t len;
  933. GNUNET_assert (ntohs (pkt->len) ==
  934. ntohs (message->size) -
  935. sizeof (struct GNUNET_MessageHeader) -
  936. sizeof (GNUNET_HashCode));
  937. /* Prepare the state.
  938. * This will be saved in the hashmap, so that the receiving procedure knows
  939. * through which tunnel this connection has to be routed.
  940. */
  941. struct redirect_state *state =
  942. GNUNET_malloc (sizeof (struct redirect_state));
  943. memset (state, 0, sizeof (struct redirect_state));
  944. state->tunnel = tunnel;
  945. state->hashmap = udp_connections;
  946. state->type = REMOTE;
  947. memcpy (&state->remote, s, sizeof (struct remote_addr));
  948. len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
  949. sizeof (struct ip6_hdr) + ntohs (pkt->len);
  950. buf = alloca (len);
  951. memset (buf, 0, len);
  952. switch (s->addrlen)
  953. {
  954. case 4:
  955. prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
  956. &s->addr, tunnel, state, (struct ip_pkt *) buf);
  957. break;
  958. case 16:
  959. prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
  960. &s->addr, tunnel, state, (struct ip6_pkt *) buf);
  961. break;
  962. default:
  963. GNUNET_assert (0);
  964. break;
  965. }
  966. hash_redirect_info (&state->hash, &state->redirect_info, s->addrlen);
  967. if (GNUNET_NO ==
  968. GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
  969. {
  970. GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
  971. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  972. state->heap_node =
  973. GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
  974. GNUNET_TIME_absolute_get ().abs_value);
  975. if (GNUNET_CONTAINER_heap_get_size (udp_connections_heap) >
  976. max_udp_connections)
  977. GNUNET_SCHEDULER_add_now (collect_connections, udp_connections_heap);
  978. }
  979. else
  980. GNUNET_free (state);
  981. (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  982. return GNUNET_YES;
  983. }
  984. /**
  985. * The messages are one GNUNET_HashCode for the service, followed by a struct udp_pkt
  986. */
  987. static int
  988. receive_udp_service (void *cls __attribute__((unused)),
  989. struct GNUNET_MESH_Tunnel *tunnel,
  990. void **tunnel_ctx __attribute__((unused)),
  991. const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
  992. const struct GNUNET_MessageHeader *message,
  993. const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
  994. {
  995. GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
  996. struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
  997. GNUNET_assert (ntohs (pkt->len) ==
  998. ntohs (message->size) -
  999. sizeof (struct GNUNET_MessageHeader) -
  1000. sizeof (GNUNET_HashCode));
  1001. /* Get the configuration from the hashmap */
  1002. uint16_t *udp_desc = alloca (sizeof (GNUNET_HashCode) + 2);
  1003. memcpy (udp_desc + 1, desc, sizeof (GNUNET_HashCode));
  1004. *udp_desc = ntohs (pkt->dpt);
  1005. struct redirect_service *serv =
  1006. GNUNET_CONTAINER_multihashmap_get (udp_services, (GNUNET_HashCode*)udp_desc);
  1007. if (NULL == serv)
  1008. {
  1009. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1010. "No service found for UDP dpt %d!\n", *udp_desc);
  1011. return GNUNET_YES;
  1012. }
  1013. pkt->dpt = htons (serv->remote_port);
  1014. /*
  1015. * At this point it would be possible to check against some kind of ACL.
  1016. */
  1017. char *buf;
  1018. size_t len;
  1019. /* Prepare the state.
  1020. * This will be saved in the hashmap, so that the receiving procedure knows
  1021. * through which tunnel this connection has to be routed.
  1022. */
  1023. struct redirect_state *state =
  1024. GNUNET_malloc (sizeof (struct redirect_state));
  1025. memset (state, 0, sizeof (struct redirect_state));
  1026. state->tunnel = tunnel;
  1027. state->serv = serv;
  1028. state->type = SERVICE;
  1029. state->hashmap = udp_connections;
  1030. memcpy (&state->desc, desc, sizeof (GNUNET_HashCode));
  1031. len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) +
  1032. sizeof (struct ip6_hdr) + ntohs (pkt->len);
  1033. buf = alloca (len);
  1034. memset (buf, 0, len);
  1035. switch (serv->version)
  1036. {
  1037. case 4:
  1038. prepare_ipv4_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
  1039. &serv->v4.ip4address,
  1040. tunnel, state, (struct ip_pkt *) buf);
  1041. break;
  1042. case 6:
  1043. prepare_ipv6_packet (len, ntohs (pkt->len), pkt, 0x11, /* UDP */
  1044. &serv->v6.ip6address,
  1045. tunnel, state, (struct ip6_pkt *) buf);
  1046. break;
  1047. default:
  1048. GNUNET_assert (0);
  1049. break;
  1050. }
  1051. hash_redirect_info(&state->hash, &state->redirect_info, serv->version == 4 ? 4 : 16);
  1052. if (GNUNET_NO ==
  1053. GNUNET_CONTAINER_multihashmap_contains (udp_connections, &state->hash))
  1054. {
  1055. GNUNET_CONTAINER_multihashmap_put (udp_connections, &state->hash, state,
  1056. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
  1057. state->heap_node =
  1058. GNUNET_CONTAINER_heap_insert (udp_connections_heap, state,
  1059. GNUNET_TIME_absolute_get ().abs_value);
  1060. if (GNUNET_CONTAINER_heap_get_size(udp_connections_heap) > max_udp_connections)
  1061. GNUNET_SCHEDULER_add_now(collect_connections, udp_connections_heap);
  1062. }
  1063. else
  1064. GNUNET_free (state);
  1065. (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
  1066. return GNUNET_YES;
  1067. }
  1068. static void
  1069. connect_to_mesh()
  1070. {
  1071. int udp, tcp;
  1072. int handler_idx, app_idx;
  1073. udp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_UDP");
  1074. tcp = GNUNET_CONFIGURATION_get_value_yesno(cfg, "exit", "ENABLE_TCP");
  1075. static struct GNUNET_MESH_MessageHandler handlers[] = {
  1076. {receive_udp_service, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
  1077. {receive_tcp_service, GNUNET_MESSAGE_TYPE_SERVICE_TCP, 0},
  1078. {NULL, 0, 0},
  1079. {NULL, 0, 0},
  1080. {NULL, 0, 0}
  1081. };
  1082. static GNUNET_MESH_ApplicationType apptypes[] =
  1083. {
  1084. GNUNET_APPLICATION_TYPE_END,
  1085. GNUNET_APPLICATION_TYPE_END,
  1086. GNUNET_APPLICATION_TYPE_END
  1087. };
  1088. app_idx = 0;
  1089. handler_idx = 2;
  1090. if (GNUNET_YES == udp)
  1091. {
  1092. handlers[handler_idx].callback = receive_udp_remote;
  1093. handlers[handler_idx].expected_size = 0;
  1094. handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_UDP;
  1095. apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
  1096. handler_idx++;
  1097. app_idx++;
  1098. }
  1099. if (GNUNET_YES == tcp)
  1100. {
  1101. handlers[handler_idx].callback = receive_tcp_remote;
  1102. handlers[handler_idx].expected_size = 0;
  1103. handlers[handler_idx].type = GNUNET_MESSAGE_TYPE_REMOTE_TCP;
  1104. apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
  1105. handler_idx++;
  1106. app_idx++;
  1107. }
  1108. mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL, handlers, apptypes);
  1109. }
  1110. /**
  1111. * @brief Main function that will be run by the scheduler.
  1112. *
  1113. * @param cls closure
  1114. * @param args remaining command-line arguments
  1115. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  1116. * @param cfg_ configuration
  1117. */
  1118. static void
  1119. run (void *cls,
  1120. char *const *args __attribute__((unused)),
  1121. const char *cfgfile __attribute__((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
  1122. {
  1123. cfg = cfg_;
  1124. connect_to_mesh();
  1125. udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
  1126. udp_connections_heap =
  1127. GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  1128. tcp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
  1129. tcp_connections_heap =
  1130. GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  1131. udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
  1132. tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
  1133. GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_UDP_CONNECTIONS",
  1134. &max_udp_connections);
  1135. GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_TCP_CONNECTIONS",
  1136. &max_tcp_connections);
  1137. GNUNET_CONFIGURATION_iterate_sections (cfg, read_service_conf, NULL);
  1138. GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
  1139. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
  1140. }
  1141. /**
  1142. * The main function to obtain template from gnunetd.
  1143. *
  1144. * @param argc number of arguments from the command line
  1145. * @param argv command line arguments
  1146. * @return 0 ok, 1 on error
  1147. */
  1148. int
  1149. main (int argc, char *const *argv) {
  1150. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  1151. GNUNET_GETOPT_OPTION_END
  1152. };
  1153. return (GNUNET_OK ==
  1154. GNUNET_PROGRAM_run (argc,
  1155. argv,
  1156. "exit",
  1157. gettext_noop ("help text"),
  1158. options, &run, NULL)) ? ret : 1;
  1159. }
  1160. /* end of gnunet-daemon-exit.c */