gnunet-service-dv.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 Christian Grothoff (and other contributing authors)
  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 dv/gnunet-service-dv.c
  19. * @brief the distance vector service, primarily handles gossip of nearby
  20. * peers and sending/receiving DV messages from core and decapsulating
  21. * them
  22. *
  23. * @author Christian Grothoff
  24. * @author Nathan Evans
  25. */
  26. #include "platform.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_protocols.h"
  29. #include "gnunet_core_service.h"
  30. #include "gnunet_hello_lib.h"
  31. #include "gnunet_peerinfo_service.h"
  32. #include "gnunet_statistics_service.h"
  33. #include "gnunet_set_service.h"
  34. #include "gnunet_ats_service.h"
  35. #include "dv.h"
  36. #include <gcrypt.h>
  37. /**
  38. * How often do we establish the consensu?
  39. */
  40. #define GNUNET_DV_CONSENSUS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
  41. /**
  42. * Maximum number of messages we queue per peer.
  43. */
  44. #define MAX_QUEUE_SIZE 16
  45. /**
  46. * Maximum number of messages we queue towards the clients/plugin.
  47. */
  48. #define MAX_QUEUE_SIZE_PLUGIN 1024
  49. /**
  50. * The default fisheye depth, from how many hops away will
  51. * we keep peers?
  52. */
  53. #define DEFAULT_FISHEYE_DEPTH 3
  54. /**
  55. * How many hops is a direct neighbor away?
  56. */
  57. #define DIRECT_NEIGHBOR_COST 1
  58. GNUNET_NETWORK_STRUCT_BEGIN
  59. /**
  60. * Information about a peer DV can route to. These entries are what
  61. * we use as the binary format to establish consensus to create our
  62. * routing table and as the address format in the HELLOs.
  63. */
  64. struct Target
  65. {
  66. /**
  67. * Identity of the peer we can reach.
  68. */
  69. struct GNUNET_PeerIdentity peer;
  70. /**
  71. * How many hops (1-3) is this peer away? in network byte order
  72. */
  73. uint32_t distance GNUNET_PACKED;
  74. };
  75. /**
  76. * Message exchanged between DV services (via core), requesting a
  77. * message to be routed.
  78. */
  79. struct RouteMessage
  80. {
  81. /**
  82. * Type: #GNUNET_MESSAGE_TYPE_DV_ROUTE
  83. */
  84. struct GNUNET_MessageHeader header;
  85. /**
  86. * Expected (remaining) distance. Must be always smaller than
  87. * #DEFAULT_FISHEYE_DEPTH, should be zero at the target. Must
  88. * be decremented by one at each hop. Peers must not forward
  89. * these messages further once the counter has reached zero.
  90. */
  91. uint32_t distance GNUNET_PACKED;
  92. /**
  93. * The (actual) target of the message (this peer, if distance is zero).
  94. */
  95. struct GNUNET_PeerIdentity target;
  96. /**
  97. * The (actual) sender of the message.
  98. */
  99. struct GNUNET_PeerIdentity sender;
  100. };
  101. GNUNET_NETWORK_STRUCT_END
  102. /**
  103. * Linked list of messages to send to clients.
  104. */
  105. struct PendingMessage
  106. {
  107. /**
  108. * Pointer to next item in the list
  109. */
  110. struct PendingMessage *next;
  111. /**
  112. * Pointer to previous item in the list
  113. */
  114. struct PendingMessage *prev;
  115. /**
  116. * Actual message to be sent, allocated after this struct.
  117. */
  118. const struct GNUNET_MessageHeader *msg;
  119. /**
  120. * Next target for the message (a neighbour of ours).
  121. */
  122. struct GNUNET_PeerIdentity next_target;
  123. /**
  124. * Unique ID of the message.
  125. */
  126. uint32_t uid;
  127. };
  128. /**
  129. * Information about a direct neighbor (core-level, excluding
  130. * DV-links, only DV-enabled peers).
  131. */
  132. struct DirectNeighbor
  133. {
  134. /**
  135. * Identity of the peer.
  136. */
  137. struct GNUNET_PeerIdentity peer;
  138. /**
  139. * Session ID we use whenever we create a set union with
  140. * this neighbor; constructed from the XOR of our peer
  141. * IDs and then salted with "DV-SALT" to avoid conflicts
  142. * with other applications.
  143. */
  144. struct GNUNET_HashCode real_session_id;
  145. /**
  146. * Head of linked list of messages to send to this peer.
  147. */
  148. struct PendingMessage *pm_head;
  149. /**
  150. * Tail of linked list of messages to send to this peer.
  151. */
  152. struct PendingMessage *pm_tail;
  153. /**
  154. * Transmit handle to core service.
  155. */
  156. struct GNUNET_CORE_TransmitHandle *cth;
  157. /**
  158. * Routing table of the neighbor, NULL if not yet established.
  159. * Keys are peer identities, values are 'struct Target' entries.
  160. * Note that the distances in the targets are from the point-of-view
  161. * of the peer, not from us!
  162. */
  163. struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table;
  164. /**
  165. * Updated routing table of the neighbor, under construction,
  166. * NULL if we are not currently building it.
  167. * Keys are peer identities, values are 'struct Target' entries.
  168. * Note that the distances in the targets are from the point-of-view
  169. * of the other peer, not from us!
  170. */
  171. struct GNUNET_CONTAINER_MultiPeerMap *neighbor_table_consensus;
  172. /**
  173. * Our current (exposed) routing table as a set.
  174. */
  175. struct GNUNET_SET_Handle *my_set;
  176. /**
  177. * Handle for our current active set union operation.
  178. */
  179. struct GNUNET_SET_OperationHandle *set_op;
  180. /**
  181. * Handle used if we are listening for this peer, waiting for the
  182. * other peer to initiate construction of the set union. NULL if
  183. * we ar the initiating peer.
  184. */
  185. struct GNUNET_SET_ListenHandle *listen_handle;
  186. /**
  187. * ID of the task we use to (periodically) update our consensus
  188. * with this peer. Used if we are the initiating peer.
  189. */
  190. struct GNUNET_SCHEDULER_Task * initiate_task;
  191. /**
  192. * At what offset are we, with respect to inserting our own routes
  193. * into the consensus?
  194. */
  195. unsigned int consensus_insertion_offset;
  196. /**
  197. * At what distance are we, with respect to inserting our own routes
  198. * into the consensus?
  199. */
  200. unsigned int consensus_insertion_distance;
  201. /**
  202. * Number of messages currently in the 'pm_XXXX'-DLL.
  203. */
  204. unsigned int pm_queue_size;
  205. /**
  206. * Elements in consensus
  207. */
  208. unsigned int consensus_elements;
  209. /**
  210. * Direct one hop route
  211. */
  212. struct Route *direct_route;
  213. /**
  214. * Flag set within 'check_target_removed' to trigger full global route refresh.
  215. */
  216. int target_removed;
  217. /**
  218. * Our distance to this peer, 0 for unknown.
  219. */
  220. uint32_t distance;
  221. /**
  222. * The network this peer is in
  223. */
  224. enum GNUNET_ATS_Network_Type network;
  225. /**
  226. * Is this neighbor connected at the core level?
  227. */
  228. int connected;
  229. };
  230. /**
  231. * A route includes information about the next hop,
  232. * the target, and the ultimate distance to the
  233. * target.
  234. */
  235. struct Route
  236. {
  237. /**
  238. * Which peer do we need to forward the message to?
  239. */
  240. struct DirectNeighbor *next_hop;
  241. /**
  242. * What would be the target, and how far is it away?
  243. */
  244. struct Target target;
  245. /**
  246. * Offset of this target in the respective consensus set.
  247. */
  248. unsigned int set_offset;
  249. };
  250. /**
  251. * Set of targets we bring to a consensus; all targets in a set have a
  252. * distance equal to the sets distance (which is implied by the array
  253. * index of the set).
  254. */
  255. struct ConsensusSet
  256. {
  257. /**
  258. * Array of targets in the set, may include NULL entries if a
  259. * neighbor has disconnected; the targets are allocated with the
  260. * respective container (all_routes), not here.
  261. */
  262. struct Route **targets;
  263. /**
  264. * Size of the @e targets array.
  265. */
  266. unsigned int array_length;
  267. };
  268. /**
  269. * Peermap of all of our neighbors; processing these usually requires
  270. * first checking to see if the peer is core-connected and if the
  271. * distance is 1, in which case they are direct neighbors.
  272. */
  273. static struct GNUNET_CONTAINER_MultiPeerMap *direct_neighbors;
  274. /**
  275. * Hashmap with all routes that we currently support; contains
  276. * routing information for all peers from distance 2
  277. * up to distance #DEFAULT_FISHEYE_DEPTH.
  278. */
  279. static struct GNUNET_CONTAINER_MultiPeerMap *all_routes;
  280. /**
  281. * Array of consensus sets we expose to the outside world. Sets
  282. * are structured by the distance to the target.
  283. */
  284. static struct ConsensusSet consensi[DEFAULT_FISHEYE_DEPTH];
  285. /**
  286. * Handle to the core service api.
  287. */
  288. static struct GNUNET_CORE_Handle *core_api;
  289. /**
  290. * The identity of our peer.
  291. */
  292. static struct GNUNET_PeerIdentity my_identity;
  293. /**
  294. * The configuration for this service.
  295. */
  296. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  297. /**
  298. * The client, the DV plugin connected to us (or an event monitor).
  299. * Hopefully this client will never change, although if the plugin
  300. * dies and returns for some reason it may happen.
  301. */
  302. static struct GNUNET_SERVER_NotificationContext *nc;
  303. /**
  304. * Handle for the statistics service.
  305. */
  306. static struct GNUNET_STATISTICS_Handle *stats;
  307. /**
  308. * Handle to ATS service.
  309. */
  310. static struct GNUNET_ATS_PerformanceHandle *ats;
  311. /**
  312. * Task scheduled to refresh routes based on direct neighbours.
  313. */
  314. static struct GNUNET_SCHEDULER_Task * rr_task;
  315. /**
  316. * #GNUNET_YES if we are shutting down.
  317. */
  318. static int in_shutdown;
  319. /**
  320. * Start creating a new DV set union by initiating the connection.
  321. *
  322. * @param cls the 'struct DirectNeighbor' of the peer we're building
  323. * a routing consensus with
  324. * @param tc scheduler context
  325. */
  326. static void
  327. initiate_set_union (void *cls,
  328. const struct GNUNET_SCHEDULER_TaskContext *tc);
  329. /**
  330. * Start creating a new DV set union construction, our neighbour has
  331. * asked for it (callback for listening peer).
  332. *
  333. * @param cls the 'struct DirectNeighbor' of the peer we're building
  334. * a routing consensus with
  335. * @param other_peer the other peer
  336. * @param context_msg message with application specific information from
  337. * the other peer
  338. * @param request request from the other peer, use GNUNET_SET_accept
  339. * to accept it, otherwise the request will be refused
  340. * Note that we don't use a return value here, as it is also
  341. * necessary to specify the set we want to do the operation with,
  342. * whith sometimes can be derived from the context message.
  343. * Also necessary to specify the timeout.
  344. */
  345. static void
  346. listen_set_union (void *cls,
  347. const struct GNUNET_PeerIdentity *other_peer,
  348. const struct GNUNET_MessageHeader *context_msg,
  349. struct GNUNET_SET_Request *request);
  350. /**
  351. * Forward a message from another peer to the plugin.
  352. *
  353. * @param message the message to send to the plugin
  354. * @param origin the original sender of the message
  355. * @param distance distance to the original sender of the message
  356. */
  357. static void
  358. send_data_to_plugin (const struct GNUNET_MessageHeader *message,
  359. const struct GNUNET_PeerIdentity *origin,
  360. uint32_t distance)
  361. {
  362. struct GNUNET_DV_ReceivedMessage *received_msg;
  363. size_t size;
  364. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  365. "Delivering message from peer `%s' at distance %u\n",
  366. GNUNET_i2s (origin),
  367. (unsigned int) distance);
  368. size = sizeof (struct GNUNET_DV_ReceivedMessage) +
  369. ntohs (message->size);
  370. if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  371. {
  372. GNUNET_break (0); /* too big */
  373. return;
  374. }
  375. received_msg = GNUNET_malloc (size);
  376. received_msg->header.size = htons (size);
  377. received_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DV_RECV);
  378. received_msg->distance = htonl (distance);
  379. received_msg->sender = *origin;
  380. memcpy (&received_msg[1], message, ntohs (message->size));
  381. GNUNET_SERVER_notification_context_broadcast (nc,
  382. &received_msg->header,
  383. GNUNET_YES);
  384. GNUNET_free (received_msg);
  385. }
  386. /**
  387. * Forward a control message to the plugin.
  388. *
  389. * @param message the message to send to the plugin
  390. */
  391. static void
  392. send_control_to_plugin (const struct GNUNET_MessageHeader *message)
  393. {
  394. GNUNET_SERVER_notification_context_broadcast (nc,
  395. message,
  396. GNUNET_NO);
  397. }
  398. /**
  399. * Give an (N)ACK message to the plugin, we transmitted a message for it.
  400. *
  401. * @param target peer that received the message
  402. * @param uid plugin-chosen UID for the message
  403. * @param nack #GNUNET_NO to send ACK, #GNUNET_YES to send NACK
  404. */
  405. static void
  406. send_ack_to_plugin (const struct GNUNET_PeerIdentity *target,
  407. uint32_t uid,
  408. int nack)
  409. {
  410. struct GNUNET_DV_AckMessage ack_msg;
  411. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  412. "Delivering ACK for message to peer `%s'\n",
  413. GNUNET_i2s (target));
  414. ack_msg.header.size = htons (sizeof (ack_msg));
  415. ack_msg.header.type = htons ((GNUNET_YES == nack)
  416. ? GNUNET_MESSAGE_TYPE_DV_SEND_NACK
  417. : GNUNET_MESSAGE_TYPE_DV_SEND_ACK);
  418. ack_msg.uid = htonl (uid);
  419. ack_msg.target = *target;
  420. send_control_to_plugin (&ack_msg.header);
  421. }
  422. /**
  423. * Send a DISTANCE_CHANGED message to the plugin.
  424. *
  425. * @param peer peer with a changed distance
  426. * @param distance new distance to the peer
  427. * @param network network used by the neighbor
  428. */
  429. static void
  430. send_distance_change_to_plugin (const struct GNUNET_PeerIdentity *peer,
  431. uint32_t distance,
  432. enum GNUNET_ATS_Network_Type network)
  433. {
  434. struct GNUNET_DV_DistanceUpdateMessage du_msg;
  435. GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
  436. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  437. "Delivering DISTANCE_CHANGED for message about peer `%s'\n",
  438. GNUNET_i2s (peer));
  439. du_msg.header.size = htons (sizeof (du_msg));
  440. du_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED);
  441. du_msg.distance = htonl (distance);
  442. du_msg.peer = *peer;
  443. du_msg.network = htonl ((uint32_t) network);
  444. send_control_to_plugin (&du_msg.header);
  445. }
  446. /**
  447. * Give a CONNECT message to the plugin.
  448. *
  449. * @param target peer that connected
  450. * @param distance distance to the target
  451. * @param network the network the next hop is located in
  452. */
  453. static void
  454. send_connect_to_plugin (const struct GNUNET_PeerIdentity *target,
  455. uint32_t distance,
  456. enum GNUNET_ATS_Network_Type network)
  457. {
  458. struct GNUNET_DV_ConnectMessage cm;
  459. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  460. "Delivering CONNECT about peer %s with distance %u\n",
  461. GNUNET_i2s (target), distance);
  462. cm.header.size = htons (sizeof (cm));
  463. cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
  464. cm.distance = htonl (distance);
  465. cm.network = htonl ((uint32_t) network);
  466. cm.peer = *target;
  467. send_control_to_plugin (&cm.header);
  468. }
  469. /**
  470. * Give a DISCONNECT message to the plugin.
  471. *
  472. * @param target peer that disconnected
  473. */
  474. static void
  475. send_disconnect_to_plugin (const struct GNUNET_PeerIdentity *target)
  476. {
  477. struct GNUNET_DV_DisconnectMessage dm;
  478. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  479. "Delivering DISCONNECT about peer `%s'\n",
  480. GNUNET_i2s (target));
  481. dm.header.size = htons (sizeof (dm));
  482. dm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_DISCONNECT);
  483. dm.reserved = htonl (0);
  484. dm.peer = *target;
  485. send_control_to_plugin (&dm.header);
  486. }
  487. /**
  488. * Function called to transfer a message to another peer
  489. * via core.
  490. *
  491. * @param cls closure with the direct neighbor
  492. * @param size number of bytes available in buf
  493. * @param buf where the callee should write the message
  494. * @return number of bytes written to buf
  495. */
  496. static size_t
  497. core_transmit_notify (void *cls, size_t size, void *buf)
  498. {
  499. struct DirectNeighbor *dn = cls;
  500. char *cbuf = buf;
  501. struct PendingMessage *pending;
  502. size_t off;
  503. size_t msize;
  504. dn->cth = NULL;
  505. if (NULL == buf)
  506. {
  507. /* client disconnected */
  508. return 0;
  509. }
  510. off = 0;
  511. while ( (NULL != (pending = dn->pm_head)) &&
  512. (size >= off + (msize = ntohs (pending->msg->size))))
  513. {
  514. dn->pm_queue_size--;
  515. GNUNET_CONTAINER_DLL_remove (dn->pm_head,
  516. dn->pm_tail,
  517. pending);
  518. memcpy (&cbuf[off], pending->msg, msize);
  519. if (0 != pending->uid)
  520. {
  521. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  522. "Acking transmission of %u bytes to %s with plugin\n",
  523. msize,
  524. GNUNET_i2s (&pending->next_target));
  525. send_ack_to_plugin (&pending->next_target,
  526. pending->uid,
  527. GNUNET_NO);
  528. }
  529. GNUNET_free (pending);
  530. off += msize;
  531. }
  532. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  533. "Transmitting total of %u bytes to %s\n",
  534. off,
  535. GNUNET_i2s (&dn->peer));
  536. GNUNET_assert (NULL != core_api);
  537. if (NULL != pending)
  538. dn->cth =
  539. GNUNET_CORE_notify_transmit_ready (core_api,
  540. GNUNET_YES /* cork */,
  541. GNUNET_CORE_PRIO_BEST_EFFORT,
  542. GNUNET_TIME_UNIT_FOREVER_REL,
  543. &dn->peer,
  544. msize,
  545. &core_transmit_notify, dn);
  546. return off;
  547. }
  548. /**
  549. * Forward the given payload to the given target.
  550. *
  551. * @param target where to send the message
  552. * @param distance distance to the @a sender
  553. * @param uid unique ID for the message
  554. * @param sender original sender of the message
  555. * @param actual_target ultimate recipient for the message
  556. * @param payload payload of the message
  557. */
  558. static void
  559. forward_payload (struct DirectNeighbor *target,
  560. uint32_t distance,
  561. uint32_t uid,
  562. const struct GNUNET_PeerIdentity *sender,
  563. const struct GNUNET_PeerIdentity *actual_target,
  564. const struct GNUNET_MessageHeader *payload)
  565. {
  566. struct PendingMessage *pm;
  567. struct RouteMessage *rm;
  568. size_t msize;
  569. if ( (target->pm_queue_size >= MAX_QUEUE_SIZE) &&
  570. (0 == uid) &&
  571. (0 != memcmp (sender,
  572. &my_identity,
  573. sizeof (struct GNUNET_PeerIdentity))) )
  574. {
  575. /* not _our_ client and queue is full, drop */
  576. GNUNET_STATISTICS_update (stats,
  577. "# messages dropped",
  578. 1, GNUNET_NO);
  579. return;
  580. }
  581. msize = sizeof (struct RouteMessage) + ntohs (payload->size);
  582. if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  583. {
  584. GNUNET_break (0);
  585. return;
  586. }
  587. pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
  588. pm->next_target = target->peer;
  589. pm->uid = uid;
  590. pm->msg = (const struct GNUNET_MessageHeader *) &pm[1];
  591. rm = (struct RouteMessage *) &pm[1];
  592. rm->header.size = htons ((uint16_t) msize);
  593. rm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_ROUTE);
  594. rm->distance = htonl (distance);
  595. rm->target = *actual_target;
  596. rm->sender = *sender;
  597. memcpy (&rm[1], payload, ntohs (payload->size));
  598. GNUNET_CONTAINER_DLL_insert_tail (target->pm_head,
  599. target->pm_tail,
  600. pm);
  601. target->pm_queue_size++;
  602. GNUNET_assert (NULL != core_api);
  603. if (NULL == target->cth)
  604. target->cth = GNUNET_CORE_notify_transmit_ready (core_api,
  605. GNUNET_YES /* cork */,
  606. GNUNET_CORE_PRIO_BEST_EFFORT,
  607. GNUNET_TIME_UNIT_FOREVER_REL,
  608. &target->peer,
  609. msize,
  610. &core_transmit_notify, target);
  611. }
  612. /**
  613. * Find a free slot for storing a 'route' in the 'consensi'
  614. * set at the given distance.
  615. *
  616. * @param distance distance to use for the set slot
  617. */
  618. static unsigned int
  619. get_consensus_slot (uint32_t distance)
  620. {
  621. struct ConsensusSet *cs;
  622. unsigned int i;
  623. GNUNET_assert (distance < DEFAULT_FISHEYE_DEPTH);
  624. cs = &consensi[distance];
  625. i = 0;
  626. while ( (i < cs->array_length) &&
  627. (NULL != cs->targets[i]) ) i++;
  628. if (i == cs->array_length)
  629. {
  630. GNUNET_array_grow (cs->targets,
  631. cs->array_length,
  632. cs->array_length * 2 + 2);
  633. }
  634. return i;
  635. }
  636. /**
  637. * Allocate a slot in the consensus set for a route.
  638. *
  639. * @param route route to initialize
  640. * @param distance which consensus set to use
  641. */
  642. static void
  643. allocate_route (struct Route *route,
  644. uint32_t distance)
  645. {
  646. unsigned int i;
  647. if (distance >= DEFAULT_FISHEYE_DEPTH)
  648. {
  649. route->target.distance = htonl (distance);
  650. route->set_offset = UINT_MAX; /* invalid slot */
  651. return;
  652. }
  653. i = get_consensus_slot (distance);
  654. route->set_offset = i;
  655. consensi[distance].targets[i] = route;
  656. route->target.distance = htonl (distance);
  657. }
  658. /**
  659. * Release a slot in the consensus set for a route.
  660. *
  661. * @param route route to release the slot from
  662. */
  663. static void
  664. release_route (struct Route *route)
  665. {
  666. if (UINT_MAX == route->set_offset)
  667. return;
  668. GNUNET_assert (ntohl (route->target.distance) < DEFAULT_FISHEYE_DEPTH);
  669. consensi[ntohl (route->target.distance)].targets[route->set_offset] = NULL;
  670. route->set_offset = UINT_MAX; /* indicate invalid slot */
  671. }
  672. /**
  673. * Move a route from one consensus set to another.
  674. *
  675. * @param route route to move
  676. * @param new_distance new distance for the route (destination set)
  677. */
  678. static void
  679. move_route (struct Route *route,
  680. uint32_t new_distance)
  681. {
  682. release_route (route);
  683. allocate_route (route, new_distance);
  684. }
  685. /**
  686. * Initialize this neighbors 'my_set' and when done give
  687. * it to the pending set operation for execution.
  688. *
  689. * Add a single element to the set per call:
  690. *
  691. * If we reached the last element of a consensus element: increase distance
  692. *
  693. *
  694. * @param cls the neighbor for which we are building the set
  695. */
  696. static void
  697. build_set (void *cls)
  698. {
  699. struct DirectNeighbor *neighbor = cls;
  700. struct GNUNET_SET_Element element;
  701. struct Target *target;
  702. struct Route *route;
  703. target = NULL;
  704. /* skip over NULL entries */
  705. while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
  706. (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
  707. (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
  708. neighbor->consensus_insertion_offset++;
  709. while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
  710. (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
  711. {
  712. /* If we reached the last element of a consensus array element: increase distance and start with next array */
  713. neighbor->consensus_insertion_offset = 0;
  714. neighbor->consensus_insertion_distance++;
  715. /* skip over NULL entries */
  716. while ( (DEFAULT_FISHEYE_DEPTH > neighbor->consensus_insertion_distance) &&
  717. (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
  718. (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
  719. neighbor->consensus_insertion_offset++;
  720. }
  721. if (DEFAULT_FISHEYE_DEPTH == neighbor->consensus_insertion_distance)
  722. {
  723. /* we have added all elements to the set, run the operation */
  724. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  725. "Finished building my SET for peer `%s' with %u elements, committing\n",
  726. GNUNET_i2s (&neighbor->peer),
  727. neighbor->consensus_elements);
  728. GNUNET_SET_commit (neighbor->set_op,
  729. neighbor->my_set);
  730. GNUNET_SET_destroy (neighbor->my_set);
  731. neighbor->my_set = NULL;
  732. return;
  733. }
  734. route = consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset];
  735. GNUNET_assert (NULL != route);
  736. target = &route->target;
  737. GNUNET_assert (ntohl (target->distance) < DEFAULT_FISHEYE_DEPTH);
  738. element.size = sizeof (struct Target);
  739. element.element_type = htons (0);
  740. element.data = target;
  741. /* Find next non-NULL entry */
  742. neighbor->consensus_insertion_offset++;
  743. if ( (0 != memcmp (&target->peer, &my_identity, sizeof (my_identity))) &&
  744. (0 != memcmp (&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
  745. {
  746. /* Add target if it is not the neighbor or this peer */
  747. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  748. "Adding peer `%s' with distance %u to SET\n",
  749. GNUNET_i2s (&target->peer),
  750. ntohl (target->distance) + 1);
  751. GNUNET_SET_add_element (neighbor->my_set,
  752. &element,
  753. &build_set, neighbor);
  754. neighbor->consensus_elements++;
  755. }
  756. else
  757. build_set (neighbor);
  758. }
  759. /**
  760. * A peer is now connected to us at distance 1. Initiate DV exchange.
  761. *
  762. * @param neighbor entry for the neighbor at distance 1
  763. */
  764. static void
  765. handle_direct_connect (struct DirectNeighbor *neighbor)
  766. {
  767. struct Route *route;
  768. struct GNUNET_HashCode h1;
  769. struct GNUNET_HashCode h2;
  770. struct GNUNET_HashCode session_id;
  771. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  772. "Direct connection to %s established, routing table exchange begins.\n",
  773. GNUNET_i2s (&neighbor->peer));
  774. GNUNET_STATISTICS_update (stats,
  775. "# peers connected (1-hop)",
  776. 1, GNUNET_NO);
  777. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  778. &neighbor->peer);
  779. if (NULL != route)
  780. {
  781. GNUNET_assert (GNUNET_YES ==
  782. GNUNET_CONTAINER_multipeermap_remove (all_routes,
  783. &neighbor->peer,
  784. route));
  785. send_disconnect_to_plugin (&neighbor->peer);
  786. release_route (route);
  787. GNUNET_free (route);
  788. }
  789. neighbor->direct_route = GNUNET_new (struct Route);
  790. neighbor->direct_route->next_hop = neighbor;
  791. neighbor->direct_route->target.peer = neighbor->peer;
  792. allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
  793. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  794. "Adding direct route to %s\n",
  795. GNUNET_i2s (&neighbor->direct_route->target.peer));
  796. /* construct session ID seed as XOR of both peer's identities */
  797. GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &h1);
  798. GNUNET_CRYPTO_hash (&neighbor->peer, sizeof (struct GNUNET_PeerIdentity), &h2);
  799. GNUNET_CRYPTO_hash_xor (&h1,
  800. &h2,
  801. &session_id);
  802. /* make sure session ID is unique across applications by salting it with 'DV' */
  803. GNUNET_CRYPTO_hkdf (&neighbor->real_session_id, sizeof (struct GNUNET_HashCode),
  804. GCRY_MD_SHA512, GCRY_MD_SHA256,
  805. "DV-SALT", 2,
  806. &session_id, sizeof (session_id),
  807. NULL, 0);
  808. if (0 < memcmp (&neighbor->peer,
  809. &my_identity,
  810. sizeof (struct GNUNET_PeerIdentity)))
  811. {
  812. if (NULL != neighbor->listen_handle)
  813. {
  814. GNUNET_break (0);
  815. }
  816. else
  817. neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
  818. neighbor);
  819. }
  820. else
  821. {
  822. if (NULL != neighbor->listen_handle)
  823. {
  824. GNUNET_break (0);
  825. }
  826. else
  827. {
  828. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  829. "Starting SET listen operation with peer `%s'\n",
  830. GNUNET_i2s(&neighbor->peer));
  831. neighbor->listen_handle = GNUNET_SET_listen (cfg,
  832. GNUNET_SET_OPERATION_UNION,
  833. &neighbor->real_session_id,
  834. &listen_set_union,
  835. neighbor);
  836. }
  837. }
  838. }
  839. /**
  840. * Method called whenever a peer connects.
  841. *
  842. * @param cls closure
  843. * @param peer peer identity this notification is about
  844. */
  845. static void
  846. handle_core_connect (void *cls,
  847. const struct GNUNET_PeerIdentity *peer)
  848. {
  849. struct DirectNeighbor *neighbor;
  850. /* Check for connect to self message */
  851. if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
  852. return;
  853. /* check if entry exists */
  854. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  855. peer);
  856. if (NULL != neighbor)
  857. {
  858. GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != neighbor->network);
  859. GNUNET_break (GNUNET_YES != neighbor->connected);
  860. neighbor->connected = GNUNET_YES;
  861. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  862. "Core connected to %s (distance %u)\n",
  863. GNUNET_i2s (peer),
  864. (unsigned int) neighbor->distance);
  865. if (DIRECT_NEIGHBOR_COST != neighbor->distance)
  866. return;
  867. handle_direct_connect (neighbor);
  868. return;
  869. }
  870. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  871. "Core connected to %s (distance unknown)\n",
  872. GNUNET_i2s (peer));
  873. neighbor = GNUNET_new (struct DirectNeighbor);
  874. neighbor->peer = *peer;
  875. GNUNET_assert (GNUNET_YES ==
  876. GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
  877. peer,
  878. neighbor,
  879. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  880. neighbor->connected = GNUNET_YES;
  881. neighbor->distance = 0; /* unknown */
  882. neighbor->network = GNUNET_ATS_NET_UNSPECIFIED;
  883. }
  884. /**
  885. * Called for each 'target' in a neighbor table to free the associated memory.
  886. *
  887. * @param cls NULL
  888. * @param key key of the value
  889. * @param value value to free
  890. * @return #GNUNET_OK to continue to iterate
  891. */
  892. static int
  893. free_targets (void *cls,
  894. const struct GNUNET_PeerIdentity *key,
  895. void *value)
  896. {
  897. GNUNET_free (value);
  898. return GNUNET_OK;
  899. }
  900. /**
  901. * Add a new route to the given @a target via the given @a neighbor.
  902. *
  903. * @param target the target of the route
  904. * @param neighbor the next hop for communicating with the @a target
  905. */
  906. static void
  907. add_new_route (struct Target *target,
  908. struct DirectNeighbor *neighbor)
  909. {
  910. struct Route *route;
  911. route = GNUNET_new (struct Route);
  912. route->next_hop = neighbor;
  913. route->target.peer = target->peer;
  914. allocate_route (route, ntohl (target->distance) + 1);
  915. GNUNET_assert (GNUNET_YES ==
  916. GNUNET_CONTAINER_multipeermap_put (all_routes,
  917. &route->target.peer,
  918. route,
  919. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  920. send_connect_to_plugin (&route->target.peer,
  921. ntohl (route->target.distance),
  922. neighbor->network);
  923. }
  924. /**
  925. * Multipeerhmap iterator for checking if a given route is
  926. * (now) useful to this peer.
  927. *
  928. * @param cls the direct neighbor for the given route
  929. * @param key key value stored under
  930. * @param value a 'struct Target' that may or may not be useful; not that
  931. * the distance in 'target' does not include the first hop yet
  932. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  933. */
  934. static int
  935. check_possible_route (void *cls,
  936. const struct GNUNET_PeerIdentity *key,
  937. void *value)
  938. {
  939. struct DirectNeighbor *neighbor = cls;
  940. struct Target *target = value;
  941. struct Route *route;
  942. if (GNUNET_YES ==
  943. GNUNET_CONTAINER_multipeermap_contains (direct_neighbors,
  944. key))
  945. return GNUNET_YES; /* direct route, do not care about alternatives */
  946. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  947. key);
  948. if (NULL != route)
  949. {
  950. /* we have an existing route, check how it compares with going via 'target' */
  951. if (ntohl (route->target.distance) > ntohl (target->distance) + 1)
  952. {
  953. /* via 'target' is cheaper than the existing route; switch to alternative route! */
  954. move_route (route, ntohl (target->distance) + 1);
  955. route->next_hop = neighbor;
  956. send_distance_change_to_plugin (&target->peer,
  957. ntohl (target->distance) + 1,
  958. neighbor->network);
  959. }
  960. return GNUNET_YES; /* got a route to this target already */
  961. }
  962. if (ntohl (target->distance) >= DEFAULT_FISHEYE_DEPTH)
  963. return GNUNET_YES; /* distance is too large to be interesting */
  964. add_new_route (target, neighbor);
  965. return GNUNET_YES;
  966. }
  967. /**
  968. * Multipeermap iterator for finding routes that were previously
  969. * "hidden" due to a better route (called after a disconnect event).
  970. *
  971. * @param cls NULL
  972. * @param key peer identity of the given direct neighbor
  973. * @param value a `struct DirectNeighbor` to check for additional routes
  974. * @return #GNUNET_YES to continue iteration
  975. */
  976. static int
  977. refresh_routes (void *cls,
  978. const struct GNUNET_PeerIdentity *key,
  979. void *value)
  980. {
  981. struct DirectNeighbor *neighbor = value;
  982. if ( (GNUNET_YES != neighbor->connected) ||
  983. (DIRECT_NEIGHBOR_COST != neighbor->distance) )
  984. return GNUNET_YES;
  985. if (NULL != neighbor->neighbor_table)
  986. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  987. &check_possible_route,
  988. neighbor);
  989. return GNUNET_YES;
  990. }
  991. /**
  992. * Task to run #refresh_routes() on all direct neighbours.
  993. *
  994. * @param cls NULL
  995. * @param tc unused
  996. */
  997. static void
  998. refresh_routes_task (void *cls,
  999. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1000. {
  1001. rr_task = NULL;
  1002. GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
  1003. &refresh_routes,
  1004. NULL);
  1005. }
  1006. /**
  1007. * Asynchronously run #refresh_routes() at the next opportunity
  1008. * on all direct neighbours.
  1009. */
  1010. static void
  1011. schedule_refresh_routes ()
  1012. {
  1013. if (NULL == rr_task)
  1014. rr_task = GNUNET_SCHEDULER_add_now (&refresh_routes_task,
  1015. NULL);
  1016. }
  1017. /**
  1018. * Multipeermap iterator for freeing routes that go via a particular
  1019. * neighbor that disconnected and is thus no longer available.
  1020. *
  1021. * @param cls the direct neighbor that is now unavailable
  1022. * @param key key value stored under
  1023. * @param value a `struct Route` that may or may not go via neighbor
  1024. *
  1025. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1026. */
  1027. static int
  1028. cull_routes (void *cls,
  1029. const struct GNUNET_PeerIdentity *key,
  1030. void *value)
  1031. {
  1032. struct DirectNeighbor *neighbor = cls;
  1033. struct Route *route = value;
  1034. if (route->next_hop != neighbor)
  1035. return GNUNET_YES; /* not affected */
  1036. GNUNET_assert (GNUNET_YES ==
  1037. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
  1038. release_route (route);
  1039. send_disconnect_to_plugin (&route->target.peer);
  1040. GNUNET_free (route);
  1041. return GNUNET_YES;
  1042. }
  1043. /**
  1044. * Handle the case that a direct connection to a peer is
  1045. * disrupted. Remove all routes via that peer and
  1046. * stop the consensus with it.
  1047. *
  1048. * @param neighbor peer that was disconnected (or at least is no
  1049. * longer at distance 1)
  1050. */
  1051. static void
  1052. handle_direct_disconnect (struct DirectNeighbor *neighbor)
  1053. {
  1054. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1055. "Culling routes via %s due to direct disconnect\n",
  1056. GNUNET_i2s (&neighbor->peer));
  1057. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  1058. &cull_routes,
  1059. neighbor);
  1060. if (NULL != neighbor->cth)
  1061. {
  1062. GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
  1063. neighbor->cth = NULL;
  1064. }
  1065. if (NULL != neighbor->direct_route)
  1066. {
  1067. release_route (neighbor->direct_route);
  1068. GNUNET_free (neighbor->direct_route);
  1069. neighbor->direct_route = NULL;
  1070. }
  1071. if (NULL != neighbor->neighbor_table_consensus)
  1072. {
  1073. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1074. &free_targets,
  1075. NULL);
  1076. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
  1077. neighbor->neighbor_table_consensus = NULL;
  1078. }
  1079. if (NULL != neighbor->neighbor_table)
  1080. {
  1081. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1082. &free_targets,
  1083. NULL);
  1084. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
  1085. neighbor->neighbor_table = NULL;
  1086. }
  1087. if (NULL != neighbor->set_op)
  1088. {
  1089. GNUNET_SET_operation_cancel (neighbor->set_op);
  1090. neighbor->set_op = NULL;
  1091. }
  1092. if (NULL != neighbor->my_set)
  1093. {
  1094. GNUNET_SET_destroy (neighbor->my_set);
  1095. neighbor->my_set = NULL;
  1096. }
  1097. if (NULL != neighbor->listen_handle)
  1098. {
  1099. GNUNET_SET_listen_cancel (neighbor->listen_handle);
  1100. neighbor->listen_handle = NULL;
  1101. }
  1102. if (NULL != neighbor->initiate_task)
  1103. {
  1104. GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
  1105. neighbor->initiate_task = NULL;
  1106. }
  1107. }
  1108. /**
  1109. * Function that is called with QoS information about an address; used
  1110. * to update our current distance to another peer.
  1111. *
  1112. * @param cls closure
  1113. * @param address the address
  1114. * @param active #GNUNET_YES if this address is actively used
  1115. * to maintain a connection to a peer;
  1116. * #GNUNET_NO if the address is not actively used;
  1117. * #GNUNET_SYSERR if this address is no longer available for ATS
  1118. * @param bandwidth_out assigned outbound bandwidth for the connection
  1119. * @param bandwidth_in assigned inbound bandwidth for the connection
  1120. * @param prop performance data for the address (as far as known)
  1121. */
  1122. static void
  1123. handle_ats_update (void *cls,
  1124. const struct GNUNET_HELLO_Address *address,
  1125. int active,
  1126. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  1127. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1128. const struct GNUNET_ATS_Properties *prop)
  1129. {
  1130. struct DirectNeighbor *neighbor;
  1131. uint32_t distance;
  1132. enum GNUNET_ATS_Network_Type network;
  1133. if (NULL == address)
  1134. {
  1135. /* ATS service temporarily disconnected */
  1136. return;
  1137. }
  1138. if (GNUNET_YES != active)
  1139. {
  1140. // FIXME: handle disconnect/inactive case too!
  1141. return;
  1142. }
  1143. distance = prop->distance;
  1144. network = prop->scope;
  1145. GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
  1146. /* check if entry exists */
  1147. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1148. &address->peer);
  1149. if (NULL != neighbor)
  1150. {
  1151. neighbor->network = network;
  1152. if (neighbor->distance == distance)
  1153. return; /* nothing new to see here, move along */
  1154. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1155. "ATS says distance to %s is now %u\n",
  1156. GNUNET_i2s (&address->peer),
  1157. (unsigned int) distance);
  1158. if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
  1159. (DIRECT_NEIGHBOR_COST == distance) )
  1160. return; /* no change */
  1161. if (DIRECT_NEIGHBOR_COST == neighbor->distance)
  1162. {
  1163. neighbor->distance = distance;
  1164. GNUNET_STATISTICS_update (stats,
  1165. "# peers connected (1-hop)",
  1166. -1, GNUNET_NO);
  1167. handle_direct_disconnect (neighbor);
  1168. schedule_refresh_routes ();
  1169. return;
  1170. }
  1171. neighbor->distance = distance;
  1172. if (DIRECT_NEIGHBOR_COST != neighbor->distance)
  1173. return;
  1174. if (GNUNET_YES != neighbor->connected)
  1175. return;
  1176. handle_direct_connect (neighbor);
  1177. return;
  1178. }
  1179. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1180. "ATS says distance to %s is now %u\n",
  1181. GNUNET_i2s (&address->peer),
  1182. (unsigned int) distance);
  1183. neighbor = GNUNET_new (struct DirectNeighbor);
  1184. neighbor->peer = address->peer;
  1185. GNUNET_assert (GNUNET_YES ==
  1186. GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
  1187. &address->peer,
  1188. neighbor,
  1189. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1190. neighbor->connected = GNUNET_NO; /* not yet */
  1191. neighbor->distance = distance;
  1192. neighbor->network = network;
  1193. }
  1194. /**
  1195. * Check if a target was removed from the set of the other peer; if so,
  1196. * if we also used it for our route, we need to remove it from our
  1197. * 'all_routes' set (and later check if an alternative path now exists).
  1198. *
  1199. * @param cls the `struct DirectNeighbor`
  1200. * @param key peer identity for the target
  1201. * @param value a `struct Target` previously reachable via the given neighbor
  1202. */
  1203. static int
  1204. check_target_removed (void *cls,
  1205. const struct GNUNET_PeerIdentity *key,
  1206. void *value)
  1207. {
  1208. struct DirectNeighbor *neighbor = cls;
  1209. struct Target *new_target;
  1210. struct Route *current_route;
  1211. new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
  1212. key);
  1213. current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1214. key);
  1215. if (NULL != new_target)
  1216. {
  1217. /* target was in old set, is in new set */
  1218. if ( (NULL != current_route) &&
  1219. (current_route->next_hop == neighbor) &&
  1220. (current_route->target.distance != new_target->distance) )
  1221. {
  1222. /* need to recalculate routes due to distance change */
  1223. neighbor->target_removed = GNUNET_YES;
  1224. }
  1225. return GNUNET_OK;
  1226. }
  1227. /* target was revoked, check if it was used */
  1228. if ( (NULL == current_route) ||
  1229. (current_route->next_hop != neighbor) )
  1230. {
  1231. /* didn't matter, wasn't used */
  1232. return GNUNET_OK;
  1233. }
  1234. /* remove existing route */
  1235. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1236. "Lost route to %s\n",
  1237. GNUNET_i2s (&current_route->target.peer));
  1238. GNUNET_assert (GNUNET_YES ==
  1239. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
  1240. send_disconnect_to_plugin (&current_route->target.peer);
  1241. release_route (current_route);
  1242. GNUNET_free (current_route);
  1243. neighbor->target_removed = GNUNET_YES;
  1244. return GNUNET_OK;
  1245. }
  1246. /**
  1247. * Check if a target was added to the set of the other peer; if it
  1248. * was added or impoves the existing route, do the needed updates.
  1249. *
  1250. * @param cls the `struct DirectNeighbor`
  1251. * @param key peer identity for the target
  1252. * @param value a `struct Target` now reachable via the given neighbor
  1253. */
  1254. static int
  1255. check_target_added (void *cls,
  1256. const struct GNUNET_PeerIdentity *key,
  1257. void *value)
  1258. {
  1259. struct DirectNeighbor *neighbor = cls;
  1260. struct Target *target = value;
  1261. struct Route *current_route;
  1262. /* target was revoked, check if it was used */
  1263. current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1264. key);
  1265. if (NULL != current_route)
  1266. {
  1267. /* route exists */
  1268. if (current_route->next_hop == neighbor)
  1269. {
  1270. /* we had the same route before, no change in target */
  1271. if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
  1272. {
  1273. /* but distance changed! */
  1274. if (ntohl (target->distance) + 1 > DEFAULT_FISHEYE_DEPTH)
  1275. {
  1276. /* distance increased beyond what is allowed, kill route */
  1277. GNUNET_assert (GNUNET_YES ==
  1278. GNUNET_CONTAINER_multipeermap_remove (all_routes,
  1279. key,
  1280. current_route));
  1281. send_disconnect_to_plugin (key);
  1282. release_route (current_route);
  1283. GNUNET_free (current_route);
  1284. }
  1285. else
  1286. {
  1287. /* distance decreased, update route */
  1288. move_route (current_route,
  1289. ntohl (target->distance) + 1);
  1290. send_distance_change_to_plugin (&target->peer,
  1291. ntohl (target->distance) + 1,
  1292. neighbor->network);
  1293. }
  1294. }
  1295. return GNUNET_OK;
  1296. }
  1297. if (ntohl (current_route->target.distance) <= ntohl (target->distance) + 1)
  1298. {
  1299. /* alternative, shorter route exists, ignore */
  1300. return GNUNET_OK;
  1301. }
  1302. /* new route is better than the existing one, take over! */
  1303. /* NOTE: minor security issue: malicious peers may advertise
  1304. very short routes to take over longer paths; as we don't
  1305. check that the shorter routes actually work, a malicious
  1306. direct neighbor can use this to DoS our long routes */
  1307. move_route (current_route, ntohl (target->distance) + 1);
  1308. current_route->next_hop = neighbor;
  1309. send_distance_change_to_plugin (&target->peer,
  1310. ntohl (target->distance) + 1,
  1311. neighbor->network);
  1312. return GNUNET_OK;
  1313. }
  1314. /* new route */
  1315. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1316. "Discovered new route to %s using %u hops\n",
  1317. GNUNET_i2s (&target->peer),
  1318. (unsigned int) (ntohl (target->distance) + 1));
  1319. current_route = GNUNET_new (struct Route);
  1320. current_route->next_hop = neighbor;
  1321. current_route->target.peer = target->peer;
  1322. allocate_route (current_route, ntohl (target->distance) + 1);
  1323. GNUNET_assert (GNUNET_YES ==
  1324. GNUNET_CONTAINER_multipeermap_put (all_routes,
  1325. &current_route->target.peer,
  1326. current_route,
  1327. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1328. send_connect_to_plugin (&current_route->target.peer,
  1329. ntohl (current_route->target.distance),
  1330. neighbor->network);
  1331. return GNUNET_OK;
  1332. }
  1333. /**
  1334. * Callback for set operation results. Called for each element
  1335. * in the result set.
  1336. * We have learned a new route from the other peer. Add it to the
  1337. * route set we're building.
  1338. *
  1339. * @param cls the `struct DirectNeighbor` we're building the consensus with
  1340. * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
  1341. * @param status see `enum GNUNET_SET_Status`
  1342. */
  1343. static void
  1344. handle_set_union_result (void *cls,
  1345. const struct GNUNET_SET_Element *element,
  1346. enum GNUNET_SET_Status status)
  1347. {
  1348. struct DirectNeighbor *neighbor = cls;
  1349. struct DirectNeighbor *dn;
  1350. struct Target *target;
  1351. char *status_str;
  1352. switch (status)
  1353. {
  1354. case GNUNET_SET_STATUS_OK:
  1355. status_str = "GNUNET_SET_STATUS_OK";
  1356. break;
  1357. case GNUNET_SET_STATUS_FAILURE:
  1358. status_str = "GNUNET_SET_STATUS_FAILURE";
  1359. break;
  1360. case GNUNET_SET_STATUS_HALF_DONE:
  1361. status_str = "GNUNET_SET_STATUS_HALF_DONE";
  1362. break;
  1363. case GNUNET_SET_STATUS_DONE:
  1364. status_str = "GNUNET_SET_STATUS_DONE";
  1365. break;
  1366. default:
  1367. status_str = "UNDEFINED";
  1368. break;
  1369. }
  1370. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1371. "Got SET union result: %s\n",
  1372. status_str);
  1373. switch (status)
  1374. {
  1375. case GNUNET_SET_STATUS_OK:
  1376. if (sizeof (struct Target) != element->size)
  1377. {
  1378. GNUNET_break_op (0);
  1379. return;
  1380. }
  1381. if ( (NULL != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors, &((struct Target *) element->data)->peer))) && (DIRECT_NEIGHBOR_COST == dn->distance) )
  1382. {
  1383. /* this is a direct neighbor of ours, we do not care about routes
  1384. to this peer */
  1385. return;
  1386. }
  1387. target = GNUNET_new (struct Target);
  1388. memcpy (target, element->data, sizeof (struct Target));
  1389. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1390. "Received information about peer `%s' with distance %u from SET\n",
  1391. GNUNET_i2s (&target->peer),
  1392. ntohl (target->distance) + 1);
  1393. if (NULL == neighbor->neighbor_table_consensus)
  1394. neighbor->neighbor_table_consensus
  1395. = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1396. if (GNUNET_YES !=
  1397. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
  1398. &target->peer,
  1399. target,
  1400. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1401. {
  1402. GNUNET_break_op (0);
  1403. GNUNET_free (target);
  1404. }
  1405. break;
  1406. case GNUNET_SET_STATUS_FAILURE:
  1407. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1408. "Failed to establish DV union, will try again later\n");
  1409. neighbor->set_op = NULL;
  1410. if (NULL != neighbor->neighbor_table_consensus)
  1411. {
  1412. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1413. &free_targets,
  1414. NULL);
  1415. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
  1416. neighbor->neighbor_table_consensus = NULL;
  1417. }
  1418. if (0 < memcmp (&neighbor->peer,
  1419. &my_identity,
  1420. sizeof (struct GNUNET_PeerIdentity)))
  1421. neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
  1422. &initiate_set_union,
  1423. neighbor);
  1424. break;
  1425. case GNUNET_SET_STATUS_HALF_DONE:
  1426. break;
  1427. case GNUNET_SET_STATUS_DONE:
  1428. /* we got all of our updates; integrate routing table! */
  1429. neighbor->target_removed = GNUNET_NO;
  1430. if (NULL == neighbor->neighbor_table_consensus)
  1431. neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1432. if (NULL != neighbor->neighbor_table)
  1433. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1434. &check_target_removed,
  1435. neighbor);
  1436. if (GNUNET_YES == neighbor->target_removed)
  1437. {
  1438. /* check if we got an alternative for the removed routes */
  1439. schedule_refresh_routes ();
  1440. }
  1441. /* add targets that appeared (and check for improved routes) */
  1442. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1443. &check_target_added,
  1444. neighbor);
  1445. if (NULL != neighbor->neighbor_table)
  1446. {
  1447. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1448. &free_targets,
  1449. NULL);
  1450. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
  1451. neighbor->neighbor_table = NULL;
  1452. }
  1453. neighbor->neighbor_table = neighbor->neighbor_table_consensus;
  1454. neighbor->neighbor_table_consensus = NULL;
  1455. /* operation done, schedule next run! */
  1456. neighbor->set_op = NULL;
  1457. if (0 < memcmp (&neighbor->peer,
  1458. &my_identity,
  1459. sizeof (struct GNUNET_PeerIdentity)))
  1460. neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
  1461. &initiate_set_union,
  1462. neighbor);
  1463. break;
  1464. default:
  1465. GNUNET_break (0);
  1466. return;
  1467. }
  1468. }
  1469. /**
  1470. * Start creating a new DV set union construction, our neighbour has
  1471. * asked for it (callback for listening peer).
  1472. *
  1473. * @param cls the 'struct DirectNeighbor' of the peer we're building
  1474. * a routing consensus with
  1475. * @param other_peer the other peer
  1476. * @param context_msg message with application specific information from
  1477. * the other peer
  1478. * @param request request from the other peer, use GNUNET_SET_accept
  1479. * to accept it, otherwise the request will be refused
  1480. * Note that we don't use a return value here, as it is also
  1481. * necessary to specify the set we want to do the operation with,
  1482. * whith sometimes can be derived from the context message.
  1483. * Also necessary to specify the timeout.
  1484. */
  1485. static void
  1486. listen_set_union (void *cls,
  1487. const struct GNUNET_PeerIdentity *other_peer,
  1488. const struct GNUNET_MessageHeader *context_msg,
  1489. struct GNUNET_SET_Request *request)
  1490. {
  1491. struct DirectNeighbor *neighbor = cls;
  1492. if (NULL == request)
  1493. return; /* why??? */
  1494. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1495. "Starting to create consensus with %s\n",
  1496. GNUNET_i2s (&neighbor->peer));
  1497. if (NULL != neighbor->set_op)
  1498. {
  1499. GNUNET_SET_operation_cancel (neighbor->set_op);
  1500. neighbor->set_op = NULL;
  1501. }
  1502. if (NULL != neighbor->my_set)
  1503. {
  1504. GNUNET_SET_destroy (neighbor->my_set);
  1505. neighbor->my_set = NULL;
  1506. }
  1507. neighbor->my_set = GNUNET_SET_create (cfg,
  1508. GNUNET_SET_OPERATION_UNION);
  1509. neighbor->set_op = GNUNET_SET_accept (request,
  1510. GNUNET_SET_RESULT_ADDED,
  1511. &handle_set_union_result,
  1512. neighbor);
  1513. neighbor->consensus_insertion_offset = 0;
  1514. neighbor->consensus_insertion_distance = 0;
  1515. neighbor->consensus_elements = 0;
  1516. build_set (neighbor);
  1517. }
  1518. /**
  1519. * Start creating a new DV set union by initiating the connection.
  1520. *
  1521. * @param cls the `struct DirectNeighbor *` of the peer we're building
  1522. * a routing consensus with
  1523. * @param tc scheduler context
  1524. */
  1525. static void
  1526. initiate_set_union (void *cls,
  1527. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1528. {
  1529. struct DirectNeighbor *neighbor = cls;
  1530. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1531. "Initiating SET union with peer `%s'\n",
  1532. GNUNET_i2s (&neighbor->peer));
  1533. neighbor->initiate_task = NULL;
  1534. neighbor->my_set = GNUNET_SET_create (cfg,
  1535. GNUNET_SET_OPERATION_UNION);
  1536. neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
  1537. &neighbor->real_session_id,
  1538. NULL,
  1539. GNUNET_SET_RESULT_ADDED,
  1540. &handle_set_union_result,
  1541. neighbor);
  1542. neighbor->consensus_insertion_offset = 0;
  1543. neighbor->consensus_insertion_distance = 0;
  1544. neighbor->consensus_elements = 0;
  1545. build_set (neighbor);
  1546. }
  1547. /**
  1548. * Core handler for DV data messages. Whatever this message
  1549. * contains all we really have to do is rip it out of its
  1550. * DV layering and give it to our pal the DV plugin to report
  1551. * in with.
  1552. *
  1553. * @param cls closure
  1554. * @param peer peer which sent the message (immediate sender)
  1555. * @param message the message
  1556. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the other peer violated the protocol
  1557. */
  1558. static int
  1559. handle_dv_route_message (void *cls,
  1560. const struct GNUNET_PeerIdentity *peer,
  1561. const struct GNUNET_MessageHeader *message)
  1562. {
  1563. const struct RouteMessage *rm;
  1564. const struct GNUNET_MessageHeader *payload;
  1565. struct Route *route;
  1566. struct DirectNeighbor *neighbor;
  1567. struct DirectNeighbor *dn;
  1568. struct Target *target;
  1569. uint32_t distance;
  1570. char me[5];
  1571. char src[5];
  1572. char prev[5];
  1573. char dst[5];
  1574. if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
  1575. {
  1576. GNUNET_break_op (0);
  1577. return GNUNET_SYSERR;
  1578. }
  1579. rm = (const struct RouteMessage *) message;
  1580. distance = ntohl (rm->distance);
  1581. payload = (const struct GNUNET_MessageHeader *) &rm[1];
  1582. if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
  1583. {
  1584. GNUNET_break_op (0);
  1585. return GNUNET_SYSERR;
  1586. }
  1587. strncpy (prev, GNUNET_i2s (peer), 4);
  1588. strncpy (me, GNUNET_i2s (&my_identity), 4);
  1589. strncpy (src, GNUNET_i2s (&rm->sender), 4);
  1590. strncpy (dst, GNUNET_i2s (&rm->target), 4);
  1591. prev[4] = me[4] = src[4] = dst[4] = '\0';
  1592. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1593. "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
  1594. ntohs (message->size) - sizeof (struct RouteMessage),
  1595. ntohs (payload->type),
  1596. src, dst,
  1597. prev, me,
  1598. (unsigned int) distance + 1);
  1599. if (0 == memcmp (&rm->target,
  1600. &my_identity,
  1601. sizeof (struct GNUNET_PeerIdentity)))
  1602. {
  1603. if ((NULL
  1604. != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1605. &rm->sender))) && (DIRECT_NEIGHBOR_COST == dn->distance))
  1606. {
  1607. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1608. "Discarding DV message, as %s is a direct neighbor\n",
  1609. GNUNET_i2s (&rm->sender));
  1610. GNUNET_STATISTICS_update (stats,
  1611. "# messages discarded (direct neighbor)",
  1612. 1, GNUNET_NO);
  1613. return GNUNET_OK;
  1614. }
  1615. /* message is for me, check reverse route! */
  1616. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1617. &rm->sender);
  1618. if ( (NULL == route) &&
  1619. (distance < DEFAULT_FISHEYE_DEPTH) )
  1620. {
  1621. /* don't have reverse route yet, learn it! */
  1622. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1623. peer);
  1624. if (NULL == neighbor)
  1625. {
  1626. GNUNET_break (0);
  1627. return GNUNET_OK;
  1628. }
  1629. target = GNUNET_new (struct Target);
  1630. target->peer = rm->sender;
  1631. target->distance = htonl (distance);
  1632. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1633. "Learning sender %s at distance %u from delivery!\n",
  1634. GNUNET_i2s (&rm->sender),
  1635. (unsigned int) distance + 1);
  1636. if (NULL == neighbor->neighbor_table)
  1637. neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1638. if (GNUNET_YES !=
  1639. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
  1640. &target->peer,
  1641. target,
  1642. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1643. {
  1644. GNUNET_break_op (0);
  1645. GNUNET_free (target);
  1646. return GNUNET_OK;
  1647. }
  1648. add_new_route (target, neighbor);
  1649. }
  1650. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1651. "Delivering %u bytes from %s to myself!\n",
  1652. ntohs (payload->size),
  1653. GNUNET_i2s (&rm->sender));
  1654. send_data_to_plugin (payload,
  1655. &rm->sender,
  1656. 1 + distance);
  1657. return GNUNET_OK;
  1658. }
  1659. if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1660. &rm->sender)) &&
  1661. (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
  1662. &rm->sender)) )
  1663. {
  1664. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1665. "Learning sender %s at distance %u from forwarding!\n",
  1666. GNUNET_i2s (&rm->sender),
  1667. 1 + distance);
  1668. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1669. peer);
  1670. if (NULL == neighbor)
  1671. {
  1672. GNUNET_break (0);
  1673. return GNUNET_OK;
  1674. }
  1675. target = GNUNET_new (struct Target);
  1676. target->peer = rm->sender;
  1677. target->distance = htonl (distance);
  1678. if (NULL == neighbor->neighbor_table)
  1679. neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1680. if (GNUNET_YES !=
  1681. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
  1682. &target->peer,
  1683. target,
  1684. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1685. {
  1686. GNUNET_break_op (0);
  1687. GNUNET_free (target);
  1688. return GNUNET_OK;
  1689. }
  1690. add_new_route (target, neighbor);
  1691. }
  1692. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1693. &rm->target);
  1694. if (NULL == route)
  1695. {
  1696. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1697. &rm->target);
  1698. if (NULL == neighbor)
  1699. {
  1700. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1701. "No route to %s, not routing %u bytes!\n",
  1702. GNUNET_i2s (&rm->target),
  1703. ntohs (payload->size));
  1704. GNUNET_STATISTICS_update (stats,
  1705. "# messages discarded (no route)",
  1706. 1, GNUNET_NO);
  1707. return GNUNET_OK;
  1708. }
  1709. }
  1710. else
  1711. {
  1712. neighbor = route->next_hop;
  1713. }
  1714. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1715. "Forwarding message to %s\n",
  1716. GNUNET_i2s (&neighbor->peer));
  1717. forward_payload (neighbor,
  1718. distance + 1,
  1719. 0,
  1720. &rm->sender,
  1721. &rm->target,
  1722. payload);
  1723. return GNUNET_OK;
  1724. }
  1725. /**
  1726. * Service server's handler for message send requests (which come
  1727. * bubbling up to us through the DV plugin).
  1728. *
  1729. * @param cls closure
  1730. * @param client identification of the client
  1731. * @param message the actual message
  1732. */
  1733. static void
  1734. handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
  1735. const struct GNUNET_MessageHeader *message)
  1736. {
  1737. struct Route *route;
  1738. const struct GNUNET_DV_SendMessage *msg;
  1739. const struct GNUNET_MessageHeader *payload;
  1740. if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
  1741. {
  1742. GNUNET_break (0);
  1743. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1744. return;
  1745. }
  1746. msg = (const struct GNUNET_DV_SendMessage *) message;
  1747. GNUNET_break (0 != ntohl (msg->uid));
  1748. payload = (const struct GNUNET_MessageHeader *) &msg[1];
  1749. if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
  1750. {
  1751. GNUNET_break (0);
  1752. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1753. return;
  1754. }
  1755. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1756. &msg->target);
  1757. if (NULL == route)
  1758. {
  1759. /* got disconnected */
  1760. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1761. "No route to %s, dropping local message of type %u\n",
  1762. GNUNET_i2s (&msg->target),
  1763. ntohs (payload->type));
  1764. GNUNET_STATISTICS_update (stats,
  1765. "# local messages discarded (no route)",
  1766. 1, GNUNET_NO);
  1767. send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
  1768. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1769. return;
  1770. }
  1771. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1772. "Forwarding %u bytes of type %u to %s\n",
  1773. ntohs (payload->size),
  1774. ntohs (payload->type),
  1775. GNUNET_i2s (&msg->target));
  1776. forward_payload (route->next_hop,
  1777. 0 /* first hop, distance is zero */,
  1778. htonl (msg->uid),
  1779. &my_identity,
  1780. &msg->target,
  1781. payload);
  1782. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1783. }
  1784. /**
  1785. * Cleanup all of the data structures associated with a given neighbor.
  1786. *
  1787. * @param neighbor neighbor to clean up
  1788. */
  1789. static void
  1790. cleanup_neighbor (struct DirectNeighbor *neighbor)
  1791. {
  1792. struct PendingMessage *pending;
  1793. while (NULL != (pending = neighbor->pm_head))
  1794. {
  1795. neighbor->pm_queue_size--;
  1796. GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
  1797. neighbor->pm_tail,
  1798. pending);
  1799. GNUNET_free (pending);
  1800. }
  1801. handle_direct_disconnect (neighbor);
  1802. GNUNET_assert (GNUNET_YES ==
  1803. GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
  1804. &neighbor->peer,
  1805. neighbor));
  1806. GNUNET_free (neighbor);
  1807. }
  1808. /**
  1809. * Method called whenever a given peer disconnects.
  1810. *
  1811. * @param cls closure
  1812. * @param peer peer identity this notification is about
  1813. */
  1814. static void
  1815. handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
  1816. {
  1817. struct DirectNeighbor *neighbor;
  1818. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1819. "Received core peer disconnect message for peer `%s'!\n",
  1820. GNUNET_i2s (peer));
  1821. /* Check for disconnect from self message */
  1822. if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
  1823. return;
  1824. neighbor =
  1825. GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
  1826. if (NULL == neighbor)
  1827. {
  1828. GNUNET_break (0);
  1829. return;
  1830. }
  1831. GNUNET_break (GNUNET_YES == neighbor->connected);
  1832. neighbor->connected = GNUNET_NO;
  1833. if (DIRECT_NEIGHBOR_COST == neighbor->distance)
  1834. {
  1835. GNUNET_STATISTICS_update (stats,
  1836. "# peers connected (1-hop)",
  1837. -1, GNUNET_NO);
  1838. }
  1839. cleanup_neighbor (neighbor);
  1840. if (GNUNET_YES == in_shutdown)
  1841. return;
  1842. schedule_refresh_routes ();
  1843. }
  1844. /**
  1845. * Multipeermap iterator for freeing routes. Should never be called.
  1846. *
  1847. * @param cls NULL
  1848. * @param key key value stored under
  1849. * @param value the route to be freed
  1850. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1851. */
  1852. static int
  1853. free_route (void *cls,
  1854. const struct GNUNET_PeerIdentity *key,
  1855. void *value)
  1856. {
  1857. struct Route *route = value;
  1858. GNUNET_break (0);
  1859. GNUNET_assert (GNUNET_YES ==
  1860. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
  1861. release_route (route);
  1862. send_disconnect_to_plugin (&route->target.peer);
  1863. GNUNET_free (route);
  1864. return GNUNET_YES;
  1865. }
  1866. /**
  1867. * Multipeermap iterator for freeing direct neighbors. Should never be called.
  1868. *
  1869. * @param cls NULL
  1870. * @param key key value stored under
  1871. * @param value the direct neighbor to be freed
  1872. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1873. */
  1874. static int
  1875. free_direct_neighbors (void *cls,
  1876. const struct GNUNET_PeerIdentity *key,
  1877. void *value)
  1878. {
  1879. struct DirectNeighbor *neighbor = value;
  1880. cleanup_neighbor (neighbor);
  1881. return GNUNET_YES;
  1882. }
  1883. /**
  1884. * Task run during shutdown.
  1885. *
  1886. * @param cls unused
  1887. * @param tc unused
  1888. */
  1889. static void
  1890. shutdown_task (void *cls,
  1891. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1892. {
  1893. unsigned int i;
  1894. in_shutdown = GNUNET_YES;
  1895. GNUNET_assert (NULL != core_api);
  1896. GNUNET_CORE_disconnect (core_api);
  1897. core_api = NULL;
  1898. GNUNET_ATS_performance_done (ats);
  1899. ats = NULL;
  1900. GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
  1901. &free_direct_neighbors, NULL);
  1902. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  1903. &free_route, NULL);
  1904. GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
  1905. GNUNET_CONTAINER_multipeermap_destroy (all_routes);
  1906. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  1907. stats = NULL;
  1908. GNUNET_SERVER_notification_context_destroy (nc);
  1909. nc = NULL;
  1910. for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
  1911. {
  1912. GNUNET_array_grow (consensi[i].targets,
  1913. consensi[i].array_length,
  1914. 0);
  1915. }
  1916. if (NULL != rr_task)
  1917. {
  1918. GNUNET_SCHEDULER_cancel (rr_task);
  1919. rr_task = NULL;
  1920. }
  1921. }
  1922. /**
  1923. * Notify newly connected client about an existing route.
  1924. *
  1925. * @param cls the `struct GNUNET_SERVER_Client *`
  1926. * @param key peer identity
  1927. * @param value the `struct Route *`
  1928. * @return #GNUNET_OK (continue to iterate)
  1929. */
  1930. static int
  1931. notify_client_about_route (void *cls,
  1932. const struct GNUNET_PeerIdentity *key,
  1933. void *value)
  1934. {
  1935. struct GNUNET_SERVER_Client *client = cls;
  1936. struct Route *route = value;
  1937. struct GNUNET_DV_ConnectMessage cm;
  1938. memset (&cm, 0, sizeof (cm));
  1939. cm.header.size = htons (sizeof (cm));
  1940. cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
  1941. cm.distance = htonl (route->target.distance);
  1942. cm.peer = route->target.peer;
  1943. GNUNET_SERVER_notification_context_unicast (nc,
  1944. client,
  1945. &cm.header,
  1946. GNUNET_NO);
  1947. return GNUNET_OK;
  1948. }
  1949. /**
  1950. * Handle START-message. This is the first message sent to us
  1951. * by the client (can only be one!).
  1952. *
  1953. * @param cls closure (always NULL)
  1954. * @param client identification of the client
  1955. * @param message the actual message
  1956. */
  1957. static void
  1958. handle_start (void *cls, struct GNUNET_SERVER_Client *client,
  1959. const struct GNUNET_MessageHeader *message)
  1960. {
  1961. GNUNET_SERVER_notification_context_add (nc, client);
  1962. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1963. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  1964. &notify_client_about_route,
  1965. client);
  1966. }
  1967. /**
  1968. * Called on core init.
  1969. *
  1970. * @param cls unused
  1971. * @param identity this peer's identity
  1972. */
  1973. static void
  1974. core_init (void *cls,
  1975. const struct GNUNET_PeerIdentity *identity)
  1976. {
  1977. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1978. "I am peer: %s\n",
  1979. GNUNET_i2s (identity));
  1980. my_identity = *identity;
  1981. }
  1982. /**
  1983. * Process dv requests.
  1984. *
  1985. * @param cls closure
  1986. * @param server the initialized server
  1987. * @param c configuration to use
  1988. */
  1989. static void
  1990. run (void *cls, struct GNUNET_SERVER_Handle *server,
  1991. const struct GNUNET_CONFIGURATION_Handle *c)
  1992. {
  1993. static struct GNUNET_CORE_MessageHandler core_handlers[] = {
  1994. {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
  1995. {NULL, 0, 0}
  1996. };
  1997. static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
  1998. {&handle_start, NULL,
  1999. GNUNET_MESSAGE_TYPE_DV_START,
  2000. sizeof (struct GNUNET_MessageHeader) },
  2001. { &handle_dv_send_message, NULL,
  2002. GNUNET_MESSAGE_TYPE_DV_SEND,
  2003. 0},
  2004. {NULL, NULL, 0, 0}
  2005. };
  2006. in_shutdown = GNUNET_NO;
  2007. cfg = c;
  2008. direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
  2009. all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
  2010. core_api = GNUNET_CORE_connect (cfg, NULL,
  2011. &core_init,
  2012. &handle_core_connect,
  2013. &handle_core_disconnect,
  2014. NULL, GNUNET_NO,
  2015. NULL, GNUNET_NO,
  2016. core_handlers);
  2017. if (NULL == core_api)
  2018. return;
  2019. ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
  2020. if (NULL == ats)
  2021. {
  2022. GNUNET_CORE_disconnect (core_api);
  2023. core_api = NULL;
  2024. return;
  2025. }
  2026. nc = GNUNET_SERVER_notification_context_create (server,
  2027. MAX_QUEUE_SIZE_PLUGIN);
  2028. stats = GNUNET_STATISTICS_create ("dv", cfg);
  2029. GNUNET_SERVER_add_handlers (server, plugin_handlers);
  2030. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  2031. &shutdown_task, NULL);
  2032. }
  2033. /**
  2034. * The main function for the dv service.
  2035. *
  2036. * @param argc number of arguments from the command line
  2037. * @param argv command line arguments
  2038. * @return 0 ok, 1 on error
  2039. */
  2040. int
  2041. main (int argc, char *const *argv)
  2042. {
  2043. return (GNUNET_OK ==
  2044. GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
  2045. &run, NULL)) ? 0 : 1;
  2046. }
  2047. /* end of gnunet-service-dv.c */