gnunet-service-dv.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302
  1. /*
  2. This file is part of GNUnet.
  3. (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. GNUNET_SCHEDULER_TaskIdentifier 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 GNUNET_SCHEDULER_TaskIdentifier 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 != dn->pm_head)
  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 = GNUNET_SCHEDULER_NO_TASK;
  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 (GNUNET_SCHEDULER_NO_TASK == rr_task)
  1014. rr_task = GNUNET_SCHEDULER_add_now (&refresh_routes_task,
  1015. NULL);
  1016. }
  1017. /**
  1018. * Get distance information from 'atsi'.
  1019. *
  1020. * @param atsi performance data
  1021. * @param atsi_count number of entries in atsi
  1022. * @return connected transport distance
  1023. */
  1024. static uint32_t
  1025. get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
  1026. uint32_t atsi_count)
  1027. {
  1028. uint32_t i;
  1029. for (i = 0; i < atsi_count; i++)
  1030. if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
  1031. return (0 == ntohl (atsi[i].value)) ? DIRECT_NEIGHBOR_COST : ntohl (atsi[i].value); // FIXME: 0 check should not be required once ATS is fixed!
  1032. /* If we do not have explicit distance data, assume direct neighbor. */
  1033. return DIRECT_NEIGHBOR_COST;
  1034. }
  1035. /**
  1036. * Get network information from 'atsi'.
  1037. *
  1038. * @param atsi performance data
  1039. * @param atsi_count number of entries in atsi
  1040. * @return connected transport network
  1041. */
  1042. static enum GNUNET_ATS_Network_Type
  1043. get_atsi_network (const struct GNUNET_ATS_Information *atsi,
  1044. uint32_t atsi_count)
  1045. {
  1046. uint32_t i;
  1047. for (i = 0; i < atsi_count; i++)
  1048. if (ntohl (atsi[i].type) == GNUNET_ATS_NETWORK_TYPE)
  1049. return (enum GNUNET_ATS_Network_Type) ntohl (atsi[i].value);
  1050. return GNUNET_ATS_NET_UNSPECIFIED;
  1051. }
  1052. /**
  1053. * Multipeermap iterator for freeing routes that go via a particular
  1054. * neighbor that disconnected and is thus no longer available.
  1055. *
  1056. * @param cls the direct neighbor that is now unavailable
  1057. * @param key key value stored under
  1058. * @param value a `struct Route` that may or may not go via neighbor
  1059. *
  1060. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1061. */
  1062. static int
  1063. cull_routes (void *cls,
  1064. const struct GNUNET_PeerIdentity *key,
  1065. void *value)
  1066. {
  1067. struct DirectNeighbor *neighbor = cls;
  1068. struct Route *route = value;
  1069. if (route->next_hop != neighbor)
  1070. return GNUNET_YES; /* not affected */
  1071. GNUNET_assert (GNUNET_YES ==
  1072. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
  1073. release_route (route);
  1074. send_disconnect_to_plugin (&route->target.peer);
  1075. GNUNET_free (route);
  1076. return GNUNET_YES;
  1077. }
  1078. /**
  1079. * Handle the case that a direct connection to a peer is
  1080. * disrupted. Remove all routes via that peer and
  1081. * stop the consensus with it.
  1082. *
  1083. * @param neighbor peer that was disconnected (or at least is no
  1084. * longer at distance 1)
  1085. */
  1086. static void
  1087. handle_direct_disconnect (struct DirectNeighbor *neighbor)
  1088. {
  1089. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1090. "Culling routes via %s due to direct disconnect\n",
  1091. GNUNET_i2s (&neighbor->peer));
  1092. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  1093. &cull_routes,
  1094. neighbor);
  1095. if (NULL != neighbor->cth)
  1096. {
  1097. GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
  1098. neighbor->cth = NULL;
  1099. }
  1100. if (NULL != neighbor->direct_route)
  1101. {
  1102. release_route (neighbor->direct_route);
  1103. GNUNET_free (neighbor->direct_route);
  1104. neighbor->direct_route = NULL;
  1105. }
  1106. if (NULL != neighbor->neighbor_table_consensus)
  1107. {
  1108. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1109. &free_targets,
  1110. NULL);
  1111. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
  1112. neighbor->neighbor_table_consensus = NULL;
  1113. }
  1114. if (NULL != neighbor->neighbor_table)
  1115. {
  1116. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1117. &free_targets,
  1118. NULL);
  1119. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
  1120. neighbor->neighbor_table = NULL;
  1121. }
  1122. if (NULL != neighbor->set_op)
  1123. {
  1124. GNUNET_SET_operation_cancel (neighbor->set_op);
  1125. neighbor->set_op = NULL;
  1126. }
  1127. if (NULL != neighbor->my_set)
  1128. {
  1129. GNUNET_SET_destroy (neighbor->my_set);
  1130. neighbor->my_set = NULL;
  1131. }
  1132. if (NULL != neighbor->listen_handle)
  1133. {
  1134. GNUNET_SET_listen_cancel (neighbor->listen_handle);
  1135. neighbor->listen_handle = NULL;
  1136. }
  1137. if (GNUNET_SCHEDULER_NO_TASK != neighbor->initiate_task)
  1138. {
  1139. GNUNET_SCHEDULER_cancel (neighbor->initiate_task);
  1140. neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
  1141. }
  1142. }
  1143. /**
  1144. * Function that is called with QoS information about an address; used
  1145. * to update our current distance to another peer.
  1146. *
  1147. * @param cls closure
  1148. * @param address the address
  1149. * @param active #GNUNET_YES if this address is actively used
  1150. * to maintain a connection to a peer;
  1151. * #GNUNET_NO if the address is not actively used;
  1152. * #GNUNET_SYSERR if this address is no longer available for ATS
  1153. * @param bandwidth_out assigned outbound bandwidth for the connection
  1154. * @param bandwidth_in assigned inbound bandwidth for the connection
  1155. * @param ats performance data for the address (as far as known)
  1156. * @param ats_count number of performance records in @a ats
  1157. */
  1158. static void
  1159. handle_ats_update (void *cls,
  1160. const struct GNUNET_HELLO_Address *address,
  1161. int active,
  1162. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  1163. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1164. const struct GNUNET_ATS_Information *ats,
  1165. uint32_t ats_count)
  1166. {
  1167. struct DirectNeighbor *neighbor;
  1168. uint32_t distance;
  1169. enum GNUNET_ATS_Network_Type network = GNUNET_ATS_NET_UNSPECIFIED;
  1170. if (NULL == address)
  1171. {
  1172. /* ATS service temporarily disconnected */
  1173. return;
  1174. }
  1175. if (GNUNET_YES != active)
  1176. {
  1177. // FIXME: handle disconnect/inactive case too!
  1178. return;
  1179. }
  1180. distance = get_atsi_distance (ats, ats_count);
  1181. network = get_atsi_network (ats, ats_count);
  1182. GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
  1183. /* check if entry exists */
  1184. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1185. &address->peer);
  1186. if (NULL != neighbor)
  1187. {
  1188. neighbor->network = network;
  1189. if (neighbor->distance == distance)
  1190. return; /* nothing new to see here, move along */
  1191. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1192. "ATS says distance to %s is now %u\n",
  1193. GNUNET_i2s (&address->peer),
  1194. (unsigned int) distance);
  1195. if ( (DIRECT_NEIGHBOR_COST == neighbor->distance) &&
  1196. (DIRECT_NEIGHBOR_COST == distance) )
  1197. return; /* no change */
  1198. if (DIRECT_NEIGHBOR_COST == neighbor->distance)
  1199. {
  1200. neighbor->distance = distance;
  1201. GNUNET_STATISTICS_update (stats,
  1202. "# peers connected (1-hop)",
  1203. -1, GNUNET_NO);
  1204. handle_direct_disconnect (neighbor);
  1205. schedule_refresh_routes ();
  1206. return;
  1207. }
  1208. neighbor->distance = distance;
  1209. if (DIRECT_NEIGHBOR_COST != neighbor->distance)
  1210. return;
  1211. if (GNUNET_YES != neighbor->connected)
  1212. return;
  1213. handle_direct_connect (neighbor);
  1214. return;
  1215. }
  1216. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1217. "ATS says distance to %s is now %u\n",
  1218. GNUNET_i2s (&address->peer),
  1219. (unsigned int) distance);
  1220. neighbor = GNUNET_new (struct DirectNeighbor);
  1221. neighbor->peer = address->peer;
  1222. GNUNET_assert (GNUNET_YES ==
  1223. GNUNET_CONTAINER_multipeermap_put (direct_neighbors,
  1224. &address->peer,
  1225. neighbor,
  1226. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1227. neighbor->connected = GNUNET_NO; /* not yet */
  1228. neighbor->distance = distance;
  1229. neighbor->network = network;
  1230. }
  1231. /**
  1232. * Check if a target was removed from the set of the other peer; if so,
  1233. * if we also used it for our route, we need to remove it from our
  1234. * 'all_routes' set (and later check if an alternative path now exists).
  1235. *
  1236. * @param cls the `struct DirectNeighbor`
  1237. * @param key peer identity for the target
  1238. * @param value a `struct Target` previously reachable via the given neighbor
  1239. */
  1240. static int
  1241. check_target_removed (void *cls,
  1242. const struct GNUNET_PeerIdentity *key,
  1243. void *value)
  1244. {
  1245. struct DirectNeighbor *neighbor = cls;
  1246. struct Target *new_target;
  1247. struct Route *current_route;
  1248. new_target = GNUNET_CONTAINER_multipeermap_get (neighbor->neighbor_table_consensus,
  1249. key);
  1250. current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1251. key);
  1252. if (NULL != new_target)
  1253. {
  1254. /* target was in old set, is in new set */
  1255. if ( (NULL != current_route) &&
  1256. (current_route->next_hop == neighbor) &&
  1257. (current_route->target.distance != new_target->distance) )
  1258. {
  1259. /* need to recalculate routes due to distance change */
  1260. neighbor->target_removed = GNUNET_YES;
  1261. }
  1262. return GNUNET_OK;
  1263. }
  1264. /* target was revoked, check if it was used */
  1265. if ( (NULL == current_route) ||
  1266. (current_route->next_hop != neighbor) )
  1267. {
  1268. /* didn't matter, wasn't used */
  1269. return GNUNET_OK;
  1270. }
  1271. /* remove existing route */
  1272. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1273. "Lost route to %s\n",
  1274. GNUNET_i2s (&current_route->target.peer));
  1275. GNUNET_assert (GNUNET_YES ==
  1276. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, current_route));
  1277. send_disconnect_to_plugin (&current_route->target.peer);
  1278. release_route (current_route);
  1279. GNUNET_free (current_route);
  1280. neighbor->target_removed = GNUNET_YES;
  1281. return GNUNET_OK;
  1282. }
  1283. /**
  1284. * Check if a target was added to the set of the other peer; if it
  1285. * was added or impoves the existing route, do the needed updates.
  1286. *
  1287. * @param cls the `struct DirectNeighbor`
  1288. * @param key peer identity for the target
  1289. * @param value a `struct Target` now reachable via the given neighbor
  1290. */
  1291. static int
  1292. check_target_added (void *cls,
  1293. const struct GNUNET_PeerIdentity *key,
  1294. void *value)
  1295. {
  1296. struct DirectNeighbor *neighbor = cls;
  1297. struct Target *target = value;
  1298. struct Route *current_route;
  1299. /* target was revoked, check if it was used */
  1300. current_route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1301. key);
  1302. if (NULL != current_route)
  1303. {
  1304. /* route exists */
  1305. if (current_route->next_hop == neighbor)
  1306. {
  1307. /* we had the same route before, no change in target */
  1308. if (ntohl (target->distance) + 1 != ntohl (current_route->target.distance))
  1309. {
  1310. /* but distance changed! */
  1311. if (ntohl (target->distance) + 1 > DEFAULT_FISHEYE_DEPTH)
  1312. {
  1313. /* distance increased beyond what is allowed, kill route */
  1314. GNUNET_assert (GNUNET_YES ==
  1315. GNUNET_CONTAINER_multipeermap_remove (all_routes,
  1316. key,
  1317. current_route));
  1318. send_disconnect_to_plugin (key);
  1319. release_route (current_route);
  1320. GNUNET_free (current_route);
  1321. }
  1322. else
  1323. {
  1324. /* distance decreased, update route */
  1325. move_route (current_route,
  1326. ntohl (target->distance) + 1);
  1327. send_distance_change_to_plugin (&target->peer,
  1328. ntohl (target->distance) + 1,
  1329. neighbor->network);
  1330. }
  1331. }
  1332. return GNUNET_OK;
  1333. }
  1334. if (ntohl (current_route->target.distance) <= ntohl (target->distance) + 1)
  1335. {
  1336. /* alternative, shorter route exists, ignore */
  1337. return GNUNET_OK;
  1338. }
  1339. /* new route is better than the existing one, take over! */
  1340. /* NOTE: minor security issue: malicious peers may advertise
  1341. very short routes to take over longer paths; as we don't
  1342. check that the shorter routes actually work, a malicious
  1343. direct neighbor can use this to DoS our long routes */
  1344. move_route (current_route, ntohl (target->distance) + 1);
  1345. current_route->next_hop = neighbor;
  1346. send_distance_change_to_plugin (&target->peer,
  1347. ntohl (target->distance) + 1,
  1348. neighbor->network);
  1349. return GNUNET_OK;
  1350. }
  1351. /* new route */
  1352. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1353. "Discovered new route to %s using %u hops\n",
  1354. GNUNET_i2s (&target->peer),
  1355. (unsigned int) (ntohl (target->distance) + 1));
  1356. current_route = GNUNET_new (struct Route);
  1357. current_route->next_hop = neighbor;
  1358. current_route->target.peer = target->peer;
  1359. allocate_route (current_route, ntohl (target->distance) + 1);
  1360. GNUNET_assert (GNUNET_YES ==
  1361. GNUNET_CONTAINER_multipeermap_put (all_routes,
  1362. &current_route->target.peer,
  1363. current_route,
  1364. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1365. send_connect_to_plugin (&current_route->target.peer,
  1366. ntohl (current_route->target.distance),
  1367. neighbor->network);
  1368. return GNUNET_OK;
  1369. }
  1370. /**
  1371. * Callback for set operation results. Called for each element
  1372. * in the result set.
  1373. * We have learned a new route from the other peer. Add it to the
  1374. * route set we're building.
  1375. *
  1376. * @param cls the `struct DirectNeighbor` we're building the consensus with
  1377. * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
  1378. * @param status see `enum GNUNET_SET_Status`
  1379. */
  1380. static void
  1381. handle_set_union_result (void *cls,
  1382. const struct GNUNET_SET_Element *element,
  1383. enum GNUNET_SET_Status status)
  1384. {
  1385. struct DirectNeighbor *neighbor = cls;
  1386. struct DirectNeighbor *dn;
  1387. struct Target *target;
  1388. char *status_str;
  1389. switch (status)
  1390. {
  1391. case GNUNET_SET_STATUS_OK:
  1392. status_str = "GNUNET_SET_STATUS_OK";
  1393. break;
  1394. case GNUNET_SET_STATUS_FAILURE:
  1395. status_str = "GNUNET_SET_STATUS_FAILURE";
  1396. break;
  1397. case GNUNET_SET_STATUS_HALF_DONE:
  1398. status_str = "GNUNET_SET_STATUS_HALF_DONE";
  1399. break;
  1400. case GNUNET_SET_STATUS_DONE:
  1401. status_str = "GNUNET_SET_STATUS_DONE";
  1402. break;
  1403. default:
  1404. status_str = "UNDEFINED";
  1405. break;
  1406. }
  1407. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1408. "Got SET union result: %s\n",
  1409. status_str);
  1410. switch (status)
  1411. {
  1412. case GNUNET_SET_STATUS_OK:
  1413. if (sizeof (struct Target) != element->size)
  1414. {
  1415. GNUNET_break_op (0);
  1416. return;
  1417. }
  1418. if ( (NULL != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors, &((struct Target *) element->data)->peer))) && (DIRECT_NEIGHBOR_COST == dn->distance) )
  1419. {
  1420. /* this is a direct neighbor of ours, we do not care about routes
  1421. to this peer */
  1422. return;
  1423. }
  1424. target = GNUNET_new (struct Target);
  1425. memcpy (target, element->data, sizeof (struct Target));
  1426. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1427. "Received information about peer `%s' with distance %u from SET\n",
  1428. GNUNET_i2s (&target->peer),
  1429. ntohl (target->distance) + 1);
  1430. if (NULL == neighbor->neighbor_table_consensus)
  1431. neighbor->neighbor_table_consensus
  1432. = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1433. if (GNUNET_YES !=
  1434. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
  1435. &target->peer,
  1436. target,
  1437. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1438. {
  1439. GNUNET_break_op (0);
  1440. GNUNET_free (target);
  1441. }
  1442. break;
  1443. case GNUNET_SET_STATUS_FAILURE:
  1444. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1445. "Failed to establish DV union, will try again later\n");
  1446. neighbor->set_op = NULL;
  1447. if (NULL != neighbor->neighbor_table_consensus)
  1448. {
  1449. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1450. &free_targets,
  1451. NULL);
  1452. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table_consensus);
  1453. neighbor->neighbor_table_consensus = NULL;
  1454. }
  1455. if (0 < memcmp (&neighbor->peer,
  1456. &my_identity,
  1457. sizeof (struct GNUNET_PeerIdentity)))
  1458. neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
  1459. &initiate_set_union,
  1460. neighbor);
  1461. break;
  1462. case GNUNET_SET_STATUS_HALF_DONE:
  1463. break;
  1464. case GNUNET_SET_STATUS_DONE:
  1465. /* we got all of our updates; integrate routing table! */
  1466. neighbor->target_removed = GNUNET_NO;
  1467. if (NULL == neighbor->neighbor_table_consensus)
  1468. neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1469. if (NULL != neighbor->neighbor_table)
  1470. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1471. &check_target_removed,
  1472. neighbor);
  1473. if (GNUNET_YES == neighbor->target_removed)
  1474. {
  1475. /* check if we got an alternative for the removed routes */
  1476. schedule_refresh_routes ();
  1477. }
  1478. /* add targets that appeared (and check for improved routes) */
  1479. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
  1480. &check_target_added,
  1481. neighbor);
  1482. if (NULL != neighbor->neighbor_table)
  1483. {
  1484. GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
  1485. &free_targets,
  1486. NULL);
  1487. GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
  1488. neighbor->neighbor_table = NULL;
  1489. }
  1490. neighbor->neighbor_table = neighbor->neighbor_table_consensus;
  1491. neighbor->neighbor_table_consensus = NULL;
  1492. /* operation done, schedule next run! */
  1493. neighbor->set_op = NULL;
  1494. if (0 < memcmp (&neighbor->peer,
  1495. &my_identity,
  1496. sizeof (struct GNUNET_PeerIdentity)))
  1497. neighbor->initiate_task = GNUNET_SCHEDULER_add_delayed (GNUNET_DV_CONSENSUS_FREQUENCY,
  1498. &initiate_set_union,
  1499. neighbor);
  1500. break;
  1501. default:
  1502. GNUNET_break (0);
  1503. return;
  1504. }
  1505. }
  1506. /**
  1507. * Start creating a new DV set union construction, our neighbour has
  1508. * asked for it (callback for listening peer).
  1509. *
  1510. * @param cls the 'struct DirectNeighbor' of the peer we're building
  1511. * a routing consensus with
  1512. * @param other_peer the other peer
  1513. * @param context_msg message with application specific information from
  1514. * the other peer
  1515. * @param request request from the other peer, use GNUNET_SET_accept
  1516. * to accept it, otherwise the request will be refused
  1517. * Note that we don't use a return value here, as it is also
  1518. * necessary to specify the set we want to do the operation with,
  1519. * whith sometimes can be derived from the context message.
  1520. * Also necessary to specify the timeout.
  1521. */
  1522. static void
  1523. listen_set_union (void *cls,
  1524. const struct GNUNET_PeerIdentity *other_peer,
  1525. const struct GNUNET_MessageHeader *context_msg,
  1526. struct GNUNET_SET_Request *request)
  1527. {
  1528. struct DirectNeighbor *neighbor = cls;
  1529. if (NULL == request)
  1530. return; /* why??? */
  1531. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1532. "Starting to create consensus with %s\n",
  1533. GNUNET_i2s (&neighbor->peer));
  1534. if (NULL != neighbor->set_op)
  1535. {
  1536. GNUNET_SET_operation_cancel (neighbor->set_op);
  1537. neighbor->set_op = NULL;
  1538. }
  1539. if (NULL != neighbor->my_set)
  1540. {
  1541. GNUNET_SET_destroy (neighbor->my_set);
  1542. neighbor->my_set = NULL;
  1543. }
  1544. neighbor->my_set = GNUNET_SET_create (cfg,
  1545. GNUNET_SET_OPERATION_UNION);
  1546. neighbor->set_op = GNUNET_SET_accept (request,
  1547. GNUNET_SET_RESULT_ADDED,
  1548. &handle_set_union_result,
  1549. neighbor);
  1550. neighbor->consensus_insertion_offset = 0;
  1551. neighbor->consensus_insertion_distance = 0;
  1552. neighbor->consensus_elements = 0;
  1553. build_set (neighbor);
  1554. }
  1555. /**
  1556. * Start creating a new DV set union by initiating the connection.
  1557. *
  1558. * @param cls the `struct DirectNeighbor *` of the peer we're building
  1559. * a routing consensus with
  1560. * @param tc scheduler context
  1561. */
  1562. static void
  1563. initiate_set_union (void *cls,
  1564. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1565. {
  1566. struct DirectNeighbor *neighbor = cls;
  1567. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1568. "Initiating SET union with peer `%s'\n",
  1569. GNUNET_i2s (&neighbor->peer));
  1570. neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
  1571. neighbor->my_set = GNUNET_SET_create (cfg,
  1572. GNUNET_SET_OPERATION_UNION);
  1573. neighbor->set_op = GNUNET_SET_prepare (&neighbor->peer,
  1574. &neighbor->real_session_id,
  1575. NULL,
  1576. GNUNET_SET_RESULT_ADDED,
  1577. &handle_set_union_result,
  1578. neighbor);
  1579. neighbor->consensus_insertion_offset = 0;
  1580. neighbor->consensus_insertion_distance = 0;
  1581. neighbor->consensus_elements = 0;
  1582. build_set (neighbor);
  1583. }
  1584. /**
  1585. * Core handler for DV data messages. Whatever this message
  1586. * contains all we really have to do is rip it out of its
  1587. * DV layering and give it to our pal the DV plugin to report
  1588. * in with.
  1589. *
  1590. * @param cls closure
  1591. * @param peer peer which sent the message (immediate sender)
  1592. * @param message the message
  1593. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the other peer violated the protocol
  1594. */
  1595. static int
  1596. handle_dv_route_message (void *cls,
  1597. const struct GNUNET_PeerIdentity *peer,
  1598. const struct GNUNET_MessageHeader *message)
  1599. {
  1600. const struct RouteMessage *rm;
  1601. const struct GNUNET_MessageHeader *payload;
  1602. struct Route *route;
  1603. struct DirectNeighbor *neighbor;
  1604. struct DirectNeighbor *dn;
  1605. struct Target *target;
  1606. uint32_t distance;
  1607. char me[5];
  1608. char src[5];
  1609. char prev[5];
  1610. char dst[5];
  1611. if (ntohs (message->size) < sizeof (struct RouteMessage) + sizeof (struct GNUNET_MessageHeader))
  1612. {
  1613. GNUNET_break_op (0);
  1614. return GNUNET_SYSERR;
  1615. }
  1616. rm = (const struct RouteMessage *) message;
  1617. distance = ntohl (rm->distance);
  1618. payload = (const struct GNUNET_MessageHeader *) &rm[1];
  1619. if (ntohs (message->size) != sizeof (struct RouteMessage) + ntohs (payload->size))
  1620. {
  1621. GNUNET_break_op (0);
  1622. return GNUNET_SYSERR;
  1623. }
  1624. strncpy (prev, GNUNET_i2s (peer), 4);
  1625. strncpy (me, GNUNET_i2s (&my_identity), 4);
  1626. strncpy (src, GNUNET_i2s (&rm->sender), 4);
  1627. strncpy (dst, GNUNET_i2s (&rm->target), 4);
  1628. prev[4] = me[4] = src[4] = dst[4] = '\0';
  1629. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1630. "Handling DV message with %u bytes payload of type %u from %s to %s routed by %s to me (%s @ hop %u)\n",
  1631. ntohs (message->size) - sizeof (struct RouteMessage),
  1632. ntohs (payload->type),
  1633. src, dst,
  1634. prev, me,
  1635. (unsigned int) distance + 1);
  1636. if (0 == memcmp (&rm->target,
  1637. &my_identity,
  1638. sizeof (struct GNUNET_PeerIdentity)))
  1639. {
  1640. if ((NULL
  1641. != (dn = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1642. &rm->sender))) && (DIRECT_NEIGHBOR_COST == dn->distance))
  1643. {
  1644. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1645. "Discarding DV message, as %s is a direct neighbor\n",
  1646. GNUNET_i2s (&rm->sender));
  1647. GNUNET_STATISTICS_update (stats,
  1648. "# messages discarded (direct neighbor)",
  1649. 1, GNUNET_NO);
  1650. return GNUNET_OK;
  1651. }
  1652. /* message is for me, check reverse route! */
  1653. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1654. &rm->sender);
  1655. if ( (NULL == route) &&
  1656. (distance < DEFAULT_FISHEYE_DEPTH) )
  1657. {
  1658. /* don't have reverse route yet, learn it! */
  1659. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1660. peer);
  1661. if (NULL == neighbor)
  1662. {
  1663. GNUNET_break (0);
  1664. return GNUNET_OK;
  1665. }
  1666. target = GNUNET_new (struct Target);
  1667. target->peer = rm->sender;
  1668. target->distance = htonl (distance);
  1669. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1670. "Learning sender %s at distance %u from delivery!\n",
  1671. GNUNET_i2s (&rm->sender),
  1672. (unsigned int) distance + 1);
  1673. if (NULL == neighbor->neighbor_table)
  1674. neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1675. if (GNUNET_YES !=
  1676. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
  1677. &target->peer,
  1678. target,
  1679. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1680. {
  1681. GNUNET_break_op (0);
  1682. GNUNET_free (target);
  1683. return GNUNET_OK;
  1684. }
  1685. add_new_route (target, neighbor);
  1686. }
  1687. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1688. "Delivering %u bytes from %s to myself!\n",
  1689. ntohs (payload->size),
  1690. GNUNET_i2s (&rm->sender));
  1691. send_data_to_plugin (payload,
  1692. &rm->sender,
  1693. 1 + distance);
  1694. return GNUNET_OK;
  1695. }
  1696. if ( (NULL == GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1697. &rm->sender)) &&
  1698. (NULL == GNUNET_CONTAINER_multipeermap_get (all_routes,
  1699. &rm->sender)) )
  1700. {
  1701. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1702. "Learning sender %s at distance %u from forwarding!\n",
  1703. GNUNET_i2s (&rm->sender),
  1704. 1 + distance);
  1705. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1706. peer);
  1707. if (NULL == neighbor)
  1708. {
  1709. GNUNET_break (0);
  1710. return GNUNET_OK;
  1711. }
  1712. target = GNUNET_new (struct Target);
  1713. target->peer = rm->sender;
  1714. target->distance = htonl (distance);
  1715. if (NULL == neighbor->neighbor_table)
  1716. neighbor->neighbor_table = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1717. if (GNUNET_YES !=
  1718. GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table,
  1719. &target->peer,
  1720. target,
  1721. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  1722. {
  1723. GNUNET_break_op (0);
  1724. GNUNET_free (target);
  1725. return GNUNET_OK;
  1726. }
  1727. add_new_route (target, neighbor);
  1728. }
  1729. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1730. &rm->target);
  1731. if (NULL == route)
  1732. {
  1733. neighbor = GNUNET_CONTAINER_multipeermap_get (direct_neighbors,
  1734. &rm->target);
  1735. if (NULL == neighbor)
  1736. {
  1737. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1738. "No route to %s, not routing %u bytes!\n",
  1739. GNUNET_i2s (&rm->target),
  1740. ntohs (payload->size));
  1741. GNUNET_STATISTICS_update (stats,
  1742. "# messages discarded (no route)",
  1743. 1, GNUNET_NO);
  1744. return GNUNET_OK;
  1745. }
  1746. }
  1747. else
  1748. {
  1749. neighbor = route->next_hop;
  1750. }
  1751. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1752. "Forwarding message to %s\n",
  1753. GNUNET_i2s (&neighbor->peer));
  1754. forward_payload (neighbor,
  1755. distance + 1,
  1756. 0,
  1757. &rm->sender,
  1758. &rm->target,
  1759. payload);
  1760. return GNUNET_OK;
  1761. }
  1762. /**
  1763. * Service server's handler for message send requests (which come
  1764. * bubbling up to us through the DV plugin).
  1765. *
  1766. * @param cls closure
  1767. * @param client identification of the client
  1768. * @param message the actual message
  1769. */
  1770. static void
  1771. handle_dv_send_message (void *cls, struct GNUNET_SERVER_Client *client,
  1772. const struct GNUNET_MessageHeader *message)
  1773. {
  1774. struct Route *route;
  1775. const struct GNUNET_DV_SendMessage *msg;
  1776. const struct GNUNET_MessageHeader *payload;
  1777. if (ntohs (message->size) < sizeof (struct GNUNET_DV_SendMessage) + sizeof (struct GNUNET_MessageHeader))
  1778. {
  1779. GNUNET_break (0);
  1780. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1781. return;
  1782. }
  1783. msg = (const struct GNUNET_DV_SendMessage *) message;
  1784. GNUNET_break (0 != ntohl (msg->uid));
  1785. payload = (const struct GNUNET_MessageHeader *) &msg[1];
  1786. if (ntohs (message->size) != sizeof (struct GNUNET_DV_SendMessage) + ntohs (payload->size))
  1787. {
  1788. GNUNET_break (0);
  1789. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  1790. return;
  1791. }
  1792. route = GNUNET_CONTAINER_multipeermap_get (all_routes,
  1793. &msg->target);
  1794. if (NULL == route)
  1795. {
  1796. /* got disconnected */
  1797. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1798. "No route to %s, dropping local message of type %u\n",
  1799. GNUNET_i2s (&msg->target),
  1800. ntohs (payload->type));
  1801. GNUNET_STATISTICS_update (stats,
  1802. "# local messages discarded (no route)",
  1803. 1, GNUNET_NO);
  1804. send_ack_to_plugin (&msg->target, ntohl (msg->uid), GNUNET_YES);
  1805. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1806. return;
  1807. }
  1808. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1809. "Forwarding %u bytes of type %u to %s\n",
  1810. ntohs (payload->size),
  1811. ntohs (payload->type),
  1812. GNUNET_i2s (&msg->target));
  1813. forward_payload (route->next_hop,
  1814. 0 /* first hop, distance is zero */,
  1815. htonl (msg->uid),
  1816. &my_identity,
  1817. &msg->target,
  1818. payload);
  1819. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1820. }
  1821. /**
  1822. * Cleanup all of the data structures associated with a given neighbor.
  1823. *
  1824. * @param neighbor neighbor to clean up
  1825. */
  1826. static void
  1827. cleanup_neighbor (struct DirectNeighbor *neighbor)
  1828. {
  1829. struct PendingMessage *pending;
  1830. while (NULL != (pending = neighbor->pm_head))
  1831. {
  1832. neighbor->pm_queue_size--;
  1833. GNUNET_CONTAINER_DLL_remove (neighbor->pm_head,
  1834. neighbor->pm_tail,
  1835. pending);
  1836. GNUNET_free (pending);
  1837. }
  1838. handle_direct_disconnect (neighbor);
  1839. GNUNET_assert (GNUNET_YES ==
  1840. GNUNET_CONTAINER_multipeermap_remove (direct_neighbors,
  1841. &neighbor->peer,
  1842. neighbor));
  1843. GNUNET_free (neighbor);
  1844. }
  1845. /**
  1846. * Method called whenever a given peer disconnects.
  1847. *
  1848. * @param cls closure
  1849. * @param peer peer identity this notification is about
  1850. */
  1851. static void
  1852. handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
  1853. {
  1854. struct DirectNeighbor *neighbor;
  1855. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1856. "Received core peer disconnect message for peer `%s'!\n",
  1857. GNUNET_i2s (peer));
  1858. /* Check for disconnect from self message */
  1859. if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
  1860. return;
  1861. neighbor =
  1862. GNUNET_CONTAINER_multipeermap_get (direct_neighbors, peer);
  1863. if (NULL == neighbor)
  1864. {
  1865. GNUNET_break (0);
  1866. return;
  1867. }
  1868. GNUNET_break (GNUNET_YES == neighbor->connected);
  1869. neighbor->connected = GNUNET_NO;
  1870. if (DIRECT_NEIGHBOR_COST == neighbor->distance)
  1871. {
  1872. GNUNET_STATISTICS_update (stats,
  1873. "# peers connected (1-hop)",
  1874. -1, GNUNET_NO);
  1875. }
  1876. cleanup_neighbor (neighbor);
  1877. if (GNUNET_YES == in_shutdown)
  1878. return;
  1879. schedule_refresh_routes ();
  1880. }
  1881. /**
  1882. * Multipeermap iterator for freeing routes. Should never be called.
  1883. *
  1884. * @param cls NULL
  1885. * @param key key value stored under
  1886. * @param value the route to be freed
  1887. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1888. */
  1889. static int
  1890. free_route (void *cls,
  1891. const struct GNUNET_PeerIdentity *key,
  1892. void *value)
  1893. {
  1894. struct Route *route = value;
  1895. GNUNET_break (0);
  1896. GNUNET_assert (GNUNET_YES ==
  1897. GNUNET_CONTAINER_multipeermap_remove (all_routes, key, value));
  1898. release_route (route);
  1899. send_disconnect_to_plugin (&route->target.peer);
  1900. GNUNET_free (route);
  1901. return GNUNET_YES;
  1902. }
  1903. /**
  1904. * Multipeermap iterator for freeing direct neighbors. Should never be called.
  1905. *
  1906. * @param cls NULL
  1907. * @param key key value stored under
  1908. * @param value the direct neighbor to be freed
  1909. * @return #GNUNET_YES to continue iteration, #GNUNET_NO to stop
  1910. */
  1911. static int
  1912. free_direct_neighbors (void *cls,
  1913. const struct GNUNET_PeerIdentity *key,
  1914. void *value)
  1915. {
  1916. struct DirectNeighbor *neighbor = value;
  1917. cleanup_neighbor (neighbor);
  1918. return GNUNET_YES;
  1919. }
  1920. /**
  1921. * Task run during shutdown.
  1922. *
  1923. * @param cls unused
  1924. * @param tc unused
  1925. */
  1926. static void
  1927. shutdown_task (void *cls,
  1928. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1929. {
  1930. unsigned int i;
  1931. in_shutdown = GNUNET_YES;
  1932. GNUNET_assert (NULL != core_api);
  1933. GNUNET_CORE_disconnect (core_api);
  1934. core_api = NULL;
  1935. GNUNET_ATS_performance_done (ats);
  1936. ats = NULL;
  1937. GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
  1938. &free_direct_neighbors, NULL);
  1939. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  1940. &free_route, NULL);
  1941. GNUNET_CONTAINER_multipeermap_destroy (direct_neighbors);
  1942. GNUNET_CONTAINER_multipeermap_destroy (all_routes);
  1943. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  1944. stats = NULL;
  1945. GNUNET_SERVER_notification_context_destroy (nc);
  1946. nc = NULL;
  1947. for (i=0;i<DEFAULT_FISHEYE_DEPTH;i++)
  1948. {
  1949. GNUNET_array_grow (consensi[i].targets,
  1950. consensi[i].array_length,
  1951. 0);
  1952. }
  1953. if (GNUNET_SCHEDULER_NO_TASK != rr_task)
  1954. {
  1955. GNUNET_SCHEDULER_cancel (rr_task);
  1956. rr_task = GNUNET_SCHEDULER_NO_TASK;
  1957. }
  1958. }
  1959. /**
  1960. * Notify newly connected client about an existing route.
  1961. *
  1962. * @param cls the `struct GNUNET_SERVER_Client *`
  1963. * @param key peer identity
  1964. * @param value the `struct Route *`
  1965. * @return #GNUNET_OK (continue to iterate)
  1966. */
  1967. static int
  1968. notify_client_about_route (void *cls,
  1969. const struct GNUNET_PeerIdentity *key,
  1970. void *value)
  1971. {
  1972. struct GNUNET_SERVER_Client *client = cls;
  1973. struct Route *route = value;
  1974. struct GNUNET_DV_ConnectMessage cm;
  1975. memset (&cm, 0, sizeof (cm));
  1976. cm.header.size = htons (sizeof (cm));
  1977. cm.header.type = htons (GNUNET_MESSAGE_TYPE_DV_CONNECT);
  1978. cm.distance = htonl (route->target.distance);
  1979. cm.peer = route->target.peer;
  1980. GNUNET_SERVER_notification_context_unicast (nc,
  1981. client,
  1982. &cm.header,
  1983. GNUNET_NO);
  1984. return GNUNET_OK;
  1985. }
  1986. /**
  1987. * Handle START-message. This is the first message sent to us
  1988. * by the client (can only be one!).
  1989. *
  1990. * @param cls closure (always NULL)
  1991. * @param client identification of the client
  1992. * @param message the actual message
  1993. */
  1994. static void
  1995. handle_start (void *cls, struct GNUNET_SERVER_Client *client,
  1996. const struct GNUNET_MessageHeader *message)
  1997. {
  1998. GNUNET_SERVER_notification_context_add (nc, client);
  1999. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  2000. GNUNET_CONTAINER_multipeermap_iterate (all_routes,
  2001. &notify_client_about_route,
  2002. client);
  2003. }
  2004. /**
  2005. * Called on core init.
  2006. *
  2007. * @param cls unused
  2008. * @param identity this peer's identity
  2009. */
  2010. static void
  2011. core_init (void *cls,
  2012. const struct GNUNET_PeerIdentity *identity)
  2013. {
  2014. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2015. "I am peer: %s\n",
  2016. GNUNET_i2s (identity));
  2017. my_identity = *identity;
  2018. }
  2019. /**
  2020. * Process dv requests.
  2021. *
  2022. * @param cls closure
  2023. * @param server the initialized server
  2024. * @param c configuration to use
  2025. */
  2026. static void
  2027. run (void *cls, struct GNUNET_SERVER_Handle *server,
  2028. const struct GNUNET_CONFIGURATION_Handle *c)
  2029. {
  2030. static struct GNUNET_CORE_MessageHandler core_handlers[] = {
  2031. {&handle_dv_route_message, GNUNET_MESSAGE_TYPE_DV_ROUTE, 0},
  2032. {NULL, 0, 0}
  2033. };
  2034. static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
  2035. {&handle_start, NULL,
  2036. GNUNET_MESSAGE_TYPE_DV_START,
  2037. sizeof (struct GNUNET_MessageHeader) },
  2038. { &handle_dv_send_message, NULL,
  2039. GNUNET_MESSAGE_TYPE_DV_SEND,
  2040. 0},
  2041. {NULL, NULL, 0, 0}
  2042. };
  2043. in_shutdown = GNUNET_NO;
  2044. cfg = c;
  2045. direct_neighbors = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
  2046. all_routes = GNUNET_CONTAINER_multipeermap_create (65536, GNUNET_NO);
  2047. core_api = GNUNET_CORE_connect (cfg, NULL,
  2048. &core_init,
  2049. &handle_core_connect,
  2050. &handle_core_disconnect,
  2051. NULL, GNUNET_NO,
  2052. NULL, GNUNET_NO,
  2053. core_handlers);
  2054. if (NULL == core_api)
  2055. return;
  2056. ats = GNUNET_ATS_performance_init (cfg, &handle_ats_update, NULL);
  2057. if (NULL == ats)
  2058. {
  2059. GNUNET_CORE_disconnect (core_api);
  2060. core_api = NULL;
  2061. return;
  2062. }
  2063. nc = GNUNET_SERVER_notification_context_create (server,
  2064. MAX_QUEUE_SIZE_PLUGIN);
  2065. stats = GNUNET_STATISTICS_create ("dv", cfg);
  2066. GNUNET_SERVER_add_handlers (server, plugin_handlers);
  2067. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  2068. &shutdown_task, NULL);
  2069. }
  2070. /**
  2071. * The main function for the dv service.
  2072. *
  2073. * @param argc number of arguments from the command line
  2074. * @param argv command line arguments
  2075. * @return 0 ok, 1 on error
  2076. */
  2077. int
  2078. main (int argc, char *const *argv)
  2079. {
  2080. return (GNUNET_OK ==
  2081. GNUNET_SERVICE_run (argc, argv, "dv", GNUNET_SERVICE_OPTION_NONE,
  2082. &run, NULL)) ? 0 : 1;
  2083. }
  2084. /* end of gnunet-service-dv.c */