gnunet-service-fs_cp.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 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 fs/gnunet-service-fs_cp.c
  19. * @brief API to handle 'connected peers'
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_load_lib.h"
  25. #include "gnunet-service-fs.h"
  26. #include "gnunet-service-fs_cp.h"
  27. #include "gnunet-service-fs_pe.h"
  28. #include "gnunet-service-fs_pr.h"
  29. #include "gnunet-service-fs_push.h"
  30. #include "gnunet_peerstore_service.h"
  31. /**
  32. * Ratio for moving average delay calculation. The previous
  33. * average goes in with a factor of (n-1) into the calculation.
  34. * Must be > 0.
  35. */
  36. #define RUNAVG_DELAY_N 16
  37. /**
  38. * How often do we flush respect values to disk?
  39. */
  40. #define RESPECT_FLUSH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
  41. /**
  42. * After how long do we discard a reply?
  43. */
  44. #define REPLY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
  45. /**
  46. * Collect an instane number of statistics? May cause excessive IPC.
  47. */
  48. #define INSANE_STATISTICS GNUNET_NO
  49. /**
  50. * Handle to cancel a transmission request.
  51. */
  52. struct GSF_PeerTransmitHandle
  53. {
  54. /**
  55. * Kept in a doubly-linked list.
  56. */
  57. struct GSF_PeerTransmitHandle *next;
  58. /**
  59. * Kept in a doubly-linked list.
  60. */
  61. struct GSF_PeerTransmitHandle *prev;
  62. /**
  63. * Time when this transmission request was issued.
  64. */
  65. struct GNUNET_TIME_Absolute transmission_request_start_time;
  66. /**
  67. * Timeout for this request.
  68. */
  69. struct GNUNET_TIME_Absolute timeout;
  70. /**
  71. * Task called on timeout, or 0 for none.
  72. */
  73. struct GNUNET_SCHEDULER_Task * timeout_task;
  74. /**
  75. * Function to call to get the actual message.
  76. */
  77. GSF_GetMessageCallback gmc;
  78. /**
  79. * Peer this request targets.
  80. */
  81. struct GSF_ConnectedPeer *cp;
  82. /**
  83. * Closure for @e gmc.
  84. */
  85. void *gmc_cls;
  86. /**
  87. * Size of the message to be transmitted.
  88. */
  89. size_t size;
  90. /**
  91. * #GNUNET_YES if this is a query, #GNUNET_NO for content.
  92. */
  93. int is_query;
  94. /**
  95. * Did we get a reservation already?
  96. */
  97. int was_reserved;
  98. /**
  99. * Priority of this request.
  100. */
  101. uint32_t priority;
  102. };
  103. /**
  104. * Handle for an entry in our delay list.
  105. */
  106. struct GSF_DelayedHandle
  107. {
  108. /**
  109. * Kept in a doubly-linked list.
  110. */
  111. struct GSF_DelayedHandle *next;
  112. /**
  113. * Kept in a doubly-linked list.
  114. */
  115. struct GSF_DelayedHandle *prev;
  116. /**
  117. * Peer this transmission belongs to.
  118. */
  119. struct GSF_ConnectedPeer *cp;
  120. /**
  121. * The PUT that was delayed.
  122. */
  123. struct PutMessage *pm;
  124. /**
  125. * Task for the delay.
  126. */
  127. struct GNUNET_SCHEDULER_Task * delay_task;
  128. /**
  129. * Size of the message.
  130. */
  131. size_t msize;
  132. };
  133. /**
  134. * Information per peer and request.
  135. */
  136. struct PeerRequest
  137. {
  138. /**
  139. * Handle to generic request.
  140. */
  141. struct GSF_PendingRequest *pr;
  142. /**
  143. * Handle to specific peer.
  144. */
  145. struct GSF_ConnectedPeer *cp;
  146. /**
  147. * Task for asynchronous stopping of this request.
  148. */
  149. struct GNUNET_SCHEDULER_Task * kill_task;
  150. };
  151. /**
  152. * A connected peer.
  153. */
  154. struct GSF_ConnectedPeer
  155. {
  156. /**
  157. * Performance data for this peer.
  158. */
  159. struct GSF_PeerPerformanceData ppd;
  160. /**
  161. * Time until when we blocked this peer from migrating
  162. * data to us.
  163. */
  164. struct GNUNET_TIME_Absolute last_migration_block;
  165. /**
  166. * Task scheduled to revive migration to this peer.
  167. */
  168. struct GNUNET_SCHEDULER_Task * mig_revive_task;
  169. /**
  170. * Messages (replies, queries, content migration) we would like to
  171. * send to this peer in the near future. Sorted by priority, head.
  172. */
  173. struct GSF_PeerTransmitHandle *pth_head;
  174. /**
  175. * Messages (replies, queries, content migration) we would like to
  176. * send to this peer in the near future. Sorted by priority, tail.
  177. */
  178. struct GSF_PeerTransmitHandle *pth_tail;
  179. /**
  180. * Messages (replies, queries, content migration) we would like to
  181. * send to this peer in the near future. Sorted by priority, head.
  182. */
  183. struct GSF_DelayedHandle *delayed_head;
  184. /**
  185. * Messages (replies, queries, content migration) we would like to
  186. * send to this peer in the near future. Sorted by priority, tail.
  187. */
  188. struct GSF_DelayedHandle *delayed_tail;
  189. /**
  190. * Migration stop message in our queue, or NULL if we have none pending.
  191. */
  192. struct GSF_PeerTransmitHandle *migration_pth;
  193. /**
  194. * Context of our GNUNET_ATS_reserve_bandwidth call (or NULL).
  195. */
  196. struct GNUNET_ATS_ReservationContext *rc;
  197. /**
  198. * Task scheduled if we need to retry bandwidth reservation later.
  199. */
  200. struct GNUNET_SCHEDULER_Task * rc_delay_task;
  201. /**
  202. * Active requests from this neighbour, map of query to 'struct PeerRequest'.
  203. */
  204. struct GNUNET_CONTAINER_MultiHashMap *request_map;
  205. /**
  206. * Handle for an active request for transmission to this
  207. * peer, or NULL (if core queue was full).
  208. */
  209. struct GNUNET_CORE_TransmitHandle *cth;
  210. /**
  211. * Increase in traffic preference still to be submitted
  212. * to the core service for this peer.
  213. */
  214. uint64_t inc_preference;
  215. /**
  216. * Set to 1 if we're currently in the process of calling
  217. * #GNUNET_CORE_notify_transmit_ready() (so while @e cth is
  218. * NULL, we should not call notify_transmit_ready for this
  219. * handle right now).
  220. */
  221. unsigned int cth_in_progress;
  222. /**
  223. * Respect rating for this peer on disk.
  224. */
  225. uint32_t disk_respect;
  226. /**
  227. * Which offset in "last_p2p_replies" will be updated next?
  228. * (we go round-robin).
  229. */
  230. unsigned int last_p2p_replies_woff;
  231. /**
  232. * Which offset in "last_client_replies" will be updated next?
  233. * (we go round-robin).
  234. */
  235. unsigned int last_client_replies_woff;
  236. /**
  237. * Current offset into 'last_request_times' ring buffer.
  238. */
  239. unsigned int last_request_times_off;
  240. /**
  241. * GNUNET_YES if we did successfully reserve 32k bandwidth,
  242. * GNUNET_NO if not.
  243. */
  244. int did_reserve;
  245. /**
  246. * Function called when the creation of this record is complete.
  247. */
  248. GSF_ConnectedPeerCreationCallback creation_cb;
  249. /**
  250. * Closure for @e creation_cb
  251. */
  252. void *creation_cb_cls;
  253. /**
  254. * Handle to the PEERSTORE iterate request for peer respect value
  255. */
  256. struct GNUNET_PEERSTORE_IterateContext *respect_iterate_req;
  257. };
  258. /**
  259. * Map from peer identities to 'struct GSF_ConnectPeer' entries.
  260. */
  261. static struct GNUNET_CONTAINER_MultiPeerMap *cp_map;
  262. /**
  263. * Handle to peerstore service.
  264. */
  265. static struct GNUNET_PEERSTORE_Handle *peerstore;
  266. /**
  267. * Update the latency information kept for the given peer.
  268. *
  269. * @param id peer record to update
  270. * @param latency current latency value
  271. */
  272. void
  273. GSF_update_peer_latency_ (const struct GNUNET_PeerIdentity *id,
  274. struct GNUNET_TIME_Relative latency)
  275. {
  276. struct GSF_ConnectedPeer *cp;
  277. cp = GSF_peer_get_ (id);
  278. if (NULL == cp)
  279. return; /* we're not yet connected at the core level, ignore */
  280. GNUNET_LOAD_value_set_decline (cp->ppd.transmission_delay, latency);
  281. }
  282. /**
  283. * Return the performance data record for the given peer
  284. *
  285. * @param cp peer to query
  286. * @return performance data record for the peer
  287. */
  288. struct GSF_PeerPerformanceData *
  289. GSF_get_peer_performance_data_ (struct GSF_ConnectedPeer *cp)
  290. {
  291. return &cp->ppd;
  292. }
  293. /**
  294. * Core is ready to transmit to a peer, get the message.
  295. *
  296. * @param cls the `struct GSF_PeerTransmitHandle` of the message
  297. * @param size number of bytes core is willing to take
  298. * @param buf where to copy the message
  299. * @return number of bytes copied to @a buf
  300. */
  301. static size_t
  302. peer_transmit_ready_cb (void *cls,
  303. size_t size,
  304. void *buf);
  305. /**
  306. * Function called by core upon success or failure of our bandwidth reservation request.
  307. *
  308. * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
  309. * @param peer identifies the peer
  310. * @param amount set to the amount that was actually reserved or unreserved;
  311. * either the full requested amount or zero (no partial reservations)
  312. * @param res_delay if the reservation could not be satisfied (amount was 0), how
  313. * long should the client wait until re-trying?
  314. */
  315. static void
  316. ats_reserve_callback (void *cls,
  317. const struct GNUNET_PeerIdentity *peer,
  318. int32_t amount,
  319. struct GNUNET_TIME_Relative res_delay);
  320. /**
  321. * If ready (bandwidth reserved), try to schedule transmission via
  322. * core for the given handle.
  323. *
  324. * @param pth transmission handle to schedule
  325. */
  326. static void
  327. schedule_transmission (struct GSF_PeerTransmitHandle *pth)
  328. {
  329. struct GSF_ConnectedPeer *cp;
  330. struct GNUNET_PeerIdentity target;
  331. cp = pth->cp;
  332. if ((NULL != cp->cth) || (0 != cp->cth_in_progress))
  333. return; /* already done */
  334. GNUNET_assert (0 != cp->ppd.pid);
  335. GNUNET_PEER_resolve (cp->ppd.pid, &target);
  336. if (0 != cp->inc_preference)
  337. {
  338. GNUNET_ATS_performance_change_preference (GSF_ats,
  339. &target,
  340. GNUNET_ATS_PREFERENCE_BANDWIDTH,
  341. (double) cp->inc_preference,
  342. GNUNET_ATS_PREFERENCE_END);
  343. cp->inc_preference = 0;
  344. }
  345. if ((GNUNET_YES == pth->is_query) && (GNUNET_YES != pth->was_reserved))
  346. {
  347. /* query, need reservation */
  348. if (GNUNET_YES != cp->did_reserve)
  349. return; /* not ready */
  350. cp->did_reserve = GNUNET_NO;
  351. /* reservation already done! */
  352. pth->was_reserved = GNUNET_YES;
  353. cp->rc =
  354. GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
  355. &ats_reserve_callback, cp);
  356. return;
  357. }
  358. GNUNET_assert (NULL == cp->cth);
  359. cp->cth_in_progress++;
  360. cp->cth =
  361. GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES,
  362. GNUNET_CORE_PRIO_BACKGROUND,
  363. GNUNET_TIME_absolute_get_remaining
  364. (pth->timeout), &target, pth->size,
  365. &peer_transmit_ready_cb, cp);
  366. GNUNET_assert (NULL != cp->cth);
  367. GNUNET_assert (0 < cp->cth_in_progress--);
  368. }
  369. /**
  370. * Core is ready to transmit to a peer, get the message.
  371. *
  372. * @param cls the `struct GSF_PeerTransmitHandle` of the message
  373. * @param size number of bytes core is willing to take
  374. * @param buf where to copy the message
  375. * @return number of bytes copied to @a buf
  376. */
  377. static size_t
  378. peer_transmit_ready_cb (void *cls, size_t size, void *buf)
  379. {
  380. struct GSF_ConnectedPeer *cp = cls;
  381. struct GSF_PeerTransmitHandle *pth = cp->pth_head;
  382. struct GSF_PeerTransmitHandle *pos;
  383. size_t ret;
  384. cp->cth = NULL;
  385. if (NULL == pth)
  386. return 0;
  387. if (pth->size > size)
  388. {
  389. schedule_transmission (pth);
  390. return 0;
  391. }
  392. if (NULL != pth->timeout_task)
  393. {
  394. GNUNET_SCHEDULER_cancel (pth->timeout_task);
  395. pth->timeout_task = NULL;
  396. }
  397. GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
  398. if (GNUNET_YES == pth->is_query)
  399. {
  400. cp->ppd.last_request_times[(cp->last_request_times_off++) %
  401. MAX_QUEUE_PER_PEER] =
  402. GNUNET_TIME_absolute_get ();
  403. GNUNET_assert (0 < cp->ppd.pending_queries--);
  404. }
  405. else if (GNUNET_NO == pth->is_query)
  406. {
  407. GNUNET_assert (0 < cp->ppd.pending_replies--);
  408. }
  409. GNUNET_LOAD_update (cp->ppd.transmission_delay,
  410. GNUNET_TIME_absolute_get_duration
  411. (pth->transmission_request_start_time).rel_value_us);
  412. ret = pth->gmc (pth->gmc_cls, size, buf);
  413. if (NULL != (pos = cp->pth_head))
  414. {
  415. GNUNET_assert (pos != pth);
  416. schedule_transmission (pos);
  417. }
  418. GNUNET_free (pth);
  419. return ret;
  420. }
  421. /**
  422. * (re)try to reserve bandwidth from the given peer.
  423. *
  424. * @param cls the `struct GSF_ConnectedPeer` to reserve from
  425. * @param tc scheduler context
  426. */
  427. static void
  428. retry_reservation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  429. {
  430. struct GSF_ConnectedPeer *cp = cls;
  431. struct GNUNET_PeerIdentity target;
  432. GNUNET_PEER_resolve (cp->ppd.pid, &target);
  433. cp->rc_delay_task = NULL;
  434. cp->rc =
  435. GNUNET_ATS_reserve_bandwidth (GSF_ats, &target, DBLOCK_SIZE,
  436. &ats_reserve_callback, cp);
  437. }
  438. /**
  439. * Function called by core upon success or failure of our bandwidth reservation request.
  440. *
  441. * @param cls the `struct GSF_ConnectedPeer` of the peer for which we made the request
  442. * @param peer identifies the peer
  443. * @param amount set to the amount that was actually reserved or unreserved;
  444. * either the full requested amount or zero (no partial reservations)
  445. * @param res_delay if the reservation could not be satisfied (amount was 0), how
  446. * long should the client wait until re-trying?
  447. */
  448. static void
  449. ats_reserve_callback (void *cls,
  450. const struct GNUNET_PeerIdentity *peer,
  451. int32_t amount,
  452. struct GNUNET_TIME_Relative res_delay)
  453. {
  454. struct GSF_ConnectedPeer *cp = cls;
  455. struct GSF_PeerTransmitHandle *pth;
  456. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  457. "Reserved %d bytes / need to wait %s for reservation\n",
  458. (int) amount,
  459. GNUNET_STRINGS_relative_time_to_string (res_delay, GNUNET_YES));
  460. cp->rc = NULL;
  461. if (0 == amount)
  462. {
  463. cp->rc_delay_task =
  464. GNUNET_SCHEDULER_add_delayed (res_delay, &retry_reservation, cp);
  465. return;
  466. }
  467. cp->did_reserve = GNUNET_YES;
  468. pth = cp->pth_head;
  469. if ((NULL != pth) && (NULL == cp->cth) && (0 == cp->cth_in_progress))
  470. {
  471. /* reservation success, try transmission now! */
  472. cp->cth_in_progress++;
  473. cp->cth =
  474. GNUNET_CORE_notify_transmit_ready (GSF_core, GNUNET_YES,
  475. GNUNET_CORE_PRIO_BACKGROUND,
  476. GNUNET_TIME_absolute_get_remaining
  477. (pth->timeout), peer, pth->size,
  478. &peer_transmit_ready_cb, cp);
  479. GNUNET_assert (NULL != cp->cth);
  480. GNUNET_assert (0 < cp->cth_in_progress--);
  481. }
  482. }
  483. /**
  484. * Function called by PEERSTORE with peer respect record
  485. *
  486. * @param cls handle to connected peer entry
  487. * @param record peerstore record information
  488. * @param emsg error message, or NULL if no errors
  489. * @return #GNUNET_NO to stop iterating since we only expect 0 or 1 records
  490. */
  491. static int
  492. peer_respect_cb (void *cls,
  493. const struct GNUNET_PEERSTORE_Record *record,
  494. const char *emsg)
  495. {
  496. struct GSF_ConnectedPeer *cp = cls;
  497. cp->respect_iterate_req = NULL;
  498. if ((NULL != record) && (sizeof (cp->disk_respect) == record->value_size))
  499. cp->disk_respect = cp->ppd.respect = *((uint32_t *)record->value);
  500. GSF_push_start_ (cp);
  501. if (NULL != cp->creation_cb)
  502. cp->creation_cb (cp->creation_cb_cls, cp);
  503. return GNUNET_NO;
  504. }
  505. /**
  506. * A peer connected to us. Setup the connected peer
  507. * records.
  508. *
  509. * @param peer identity of peer that connected
  510. * @param creation_cb callback function when the record is created.
  511. * @param creation_cb_cls closure for @creation_cb
  512. */
  513. void
  514. GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer,
  515. GSF_ConnectedPeerCreationCallback creation_cb,
  516. void *creation_cb_cls)
  517. {
  518. struct GSF_ConnectedPeer *cp;
  519. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  520. "Connected to peer %s\n",
  521. GNUNET_i2s (peer));
  522. cp = GNUNET_new (struct GSF_ConnectedPeer);
  523. cp->ppd.pid = GNUNET_PEER_intern (peer);
  524. cp->ppd.transmission_delay = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_ZERO);
  525. cp->rc =
  526. GNUNET_ATS_reserve_bandwidth (GSF_ats, peer, DBLOCK_SIZE,
  527. &ats_reserve_callback, cp);
  528. cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
  529. GNUNET_break (GNUNET_OK ==
  530. GNUNET_CONTAINER_multipeermap_put (cp_map,
  531. GSF_connected_peer_get_identity2_ (cp),
  532. cp,
  533. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  534. GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
  535. GNUNET_CONTAINER_multipeermap_size (cp_map),
  536. GNUNET_NO);
  537. cp->creation_cb = creation_cb;
  538. cp->creation_cb_cls = creation_cb_cls;
  539. cp->respect_iterate_req =
  540. GNUNET_PEERSTORE_iterate (peerstore, "fs", peer, "respect",
  541. GNUNET_TIME_UNIT_FOREVER_REL, &peer_respect_cb,
  542. cp);
  543. }
  544. /**
  545. * It may be time to re-start migrating content to this
  546. * peer. Check, and if so, restart migration.
  547. *
  548. * @param cls the `struct GSF_ConnectedPeer`
  549. * @param tc scheduler context
  550. */
  551. static void
  552. revive_migration (void *cls,
  553. const struct GNUNET_SCHEDULER_TaskContext *tc)
  554. {
  555. struct GSF_ConnectedPeer *cp = cls;
  556. struct GNUNET_TIME_Relative bt;
  557. cp->mig_revive_task = NULL;
  558. bt = GNUNET_TIME_absolute_get_remaining (cp->ppd.migration_blocked_until);
  559. if (0 != bt.rel_value_us)
  560. {
  561. /* still time left... */
  562. cp->mig_revive_task =
  563. GNUNET_SCHEDULER_add_delayed (bt, &revive_migration, cp);
  564. return;
  565. }
  566. GSF_push_start_ (cp);
  567. }
  568. /**
  569. * Get a handle for a connected peer.
  570. *
  571. * @param peer peer's identity
  572. * @return NULL if the peer is not currently connected
  573. */
  574. struct GSF_ConnectedPeer *
  575. GSF_peer_get_ (const struct GNUNET_PeerIdentity *peer)
  576. {
  577. if (NULL == cp_map)
  578. return NULL;
  579. return GNUNET_CONTAINER_multipeermap_get (cp_map, peer);
  580. }
  581. /**
  582. * Handle P2P "MIGRATION_STOP" message.
  583. *
  584. * @param cls closure, always NULL
  585. * @param other the other peer involved (sender or receiver, NULL
  586. * for loopback messages where we are both sender and receiver)
  587. * @param message the actual message
  588. * @return #GNUNET_OK to keep the connection open,
  589. * #GNUNET_SYSERR to close it (signal serious error)
  590. */
  591. int
  592. GSF_handle_p2p_migration_stop_ (void *cls,
  593. const struct GNUNET_PeerIdentity *other,
  594. const struct GNUNET_MessageHeader *message)
  595. {
  596. struct GSF_ConnectedPeer *cp;
  597. const struct MigrationStopMessage *msm;
  598. struct GNUNET_TIME_Relative bt;
  599. msm = (const struct MigrationStopMessage *) message;
  600. cp = GSF_peer_get_ (other);
  601. if (NULL == cp)
  602. {
  603. GNUNET_break (0);
  604. return GNUNET_OK;
  605. }
  606. GNUNET_STATISTICS_update (GSF_stats,
  607. gettext_noop ("# migration stop messages received"),
  608. 1, GNUNET_NO);
  609. bt = GNUNET_TIME_relative_ntoh (msm->duration);
  610. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  611. _("Migration of content to peer `%s' blocked for %s\n"),
  612. GNUNET_i2s (other),
  613. GNUNET_STRINGS_relative_time_to_string (bt, GNUNET_YES));
  614. cp->ppd.migration_blocked_until = GNUNET_TIME_relative_to_absolute (bt);
  615. if (NULL == cp->mig_revive_task)
  616. {
  617. GSF_push_stop_ (cp);
  618. cp->mig_revive_task =
  619. GNUNET_SCHEDULER_add_delayed (bt,
  620. &revive_migration, cp);
  621. }
  622. return GNUNET_OK;
  623. }
  624. /**
  625. * Copy reply and free put message.
  626. *
  627. * @param cls the `struct PutMessage`
  628. * @param buf_size number of bytes available in @a buf
  629. * @param buf where to copy the message, NULL on error (peer disconnect)
  630. * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
  631. */
  632. static size_t
  633. copy_reply (void *cls, size_t buf_size, void *buf)
  634. {
  635. struct PutMessage *pm = cls;
  636. size_t size;
  637. if (NULL != buf)
  638. {
  639. GNUNET_assert (buf_size >= ntohs (pm->header.size));
  640. size = ntohs (pm->header.size);
  641. memcpy (buf, pm, size);
  642. GNUNET_STATISTICS_update (GSF_stats,
  643. gettext_noop
  644. ("# replies transmitted to other peers"), 1,
  645. GNUNET_NO);
  646. }
  647. else
  648. {
  649. size = 0;
  650. GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# replies dropped"), 1,
  651. GNUNET_NO);
  652. }
  653. GNUNET_free (pm);
  654. return size;
  655. }
  656. /**
  657. * Free resources associated with the given peer request.
  658. *
  659. * @param peerreq request to free
  660. * @param query associated key for the request
  661. */
  662. static void
  663. free_pending_request (struct PeerRequest *peerreq,
  664. const struct GNUNET_HashCode *query)
  665. {
  666. struct GSF_ConnectedPeer *cp = peerreq->cp;
  667. if (NULL != peerreq->kill_task)
  668. {
  669. GNUNET_SCHEDULER_cancel (peerreq->kill_task);
  670. peerreq->kill_task = NULL;
  671. }
  672. GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
  673. -1, GNUNET_NO);
  674. GNUNET_break (GNUNET_YES ==
  675. GNUNET_CONTAINER_multihashmap_remove (cp->request_map,
  676. query, peerreq));
  677. GNUNET_free (peerreq);
  678. }
  679. /**
  680. * Cancel all requests associated with the peer.
  681. *
  682. * @param cls unused
  683. * @param query hash code of the request
  684. * @param value the `struct GSF_PendingRequest`
  685. * @return #GNUNET_YES (continue to iterate)
  686. */
  687. static int
  688. cancel_pending_request (void *cls,
  689. const struct GNUNET_HashCode *query,
  690. void *value)
  691. {
  692. struct PeerRequest *peerreq = value;
  693. struct GSF_PendingRequest *pr = peerreq->pr;
  694. struct GSF_PendingRequestData *prd;
  695. prd = GSF_pending_request_get_data_ (pr);
  696. GSF_pending_request_cancel_ (pr, GNUNET_NO);
  697. free_pending_request (peerreq, &prd->query);
  698. return GNUNET_OK;
  699. }
  700. /**
  701. * Free the given request.
  702. *
  703. * @param cls the request to free
  704. * @param tc task context
  705. */
  706. static void
  707. peer_request_destroy (void *cls,
  708. const struct GNUNET_SCHEDULER_TaskContext *tc)
  709. {
  710. struct PeerRequest *peerreq = cls;
  711. struct GSF_PendingRequest *pr = peerreq->pr;
  712. struct GSF_PendingRequestData *prd;
  713. peerreq->kill_task = NULL;
  714. prd = GSF_pending_request_get_data_ (pr);
  715. cancel_pending_request (NULL, &prd->query, peerreq);
  716. }
  717. /**
  718. * The artificial delay is over, transmit the message now.
  719. *
  720. * @param cls the `struct GSF_DelayedHandle` with the message
  721. * @param tc scheduler context
  722. */
  723. static void
  724. transmit_delayed_now (void *cls,
  725. const struct GNUNET_SCHEDULER_TaskContext *tc)
  726. {
  727. struct GSF_DelayedHandle *dh = cls;
  728. struct GSF_ConnectedPeer *cp = dh->cp;
  729. GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
  730. if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
  731. {
  732. GNUNET_free (dh->pm);
  733. GNUNET_free (dh);
  734. return;
  735. }
  736. (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT,
  737. dh->msize, &copy_reply, dh->pm);
  738. GNUNET_free (dh);
  739. }
  740. /**
  741. * Get the randomized delay a response should be subjected to.
  742. *
  743. * @return desired delay
  744. */
  745. static struct GNUNET_TIME_Relative
  746. get_randomized_delay ()
  747. {
  748. struct GNUNET_TIME_Relative ret;
  749. ret =
  750. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  751. GNUNET_CRYPTO_random_u32
  752. (GNUNET_CRYPTO_QUALITY_WEAK,
  753. 2 * GSF_avg_latency.rel_value_us + 1));
  754. #if INSANE_STATISTICS
  755. GNUNET_STATISTICS_update (GSF_stats,
  756. gettext_noop
  757. ("# artificial delays introduced (ms)"),
  758. ret.rel_value_us / 1000LL, GNUNET_NO);
  759. #endif
  760. return ret;
  761. }
  762. /**
  763. * Handle a reply to a pending request. Also called if a request
  764. * expires (then with data == NULL). The handler may be called
  765. * many times (depending on the request type), but will not be
  766. * called during or after a call to GSF_pending_request_cancel
  767. * and will also not be called anymore after a call signalling
  768. * expiration.
  769. *
  770. * @param cls `struct PeerRequest` this is an answer for
  771. * @param eval evaluation of the result
  772. * @param pr handle to the original pending request
  773. * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
  774. * @param expiration when does @a data expire?
  775. * @param last_transmission when did we last transmit a request for this block
  776. * @param type type of the block
  777. * @param data response data, NULL on request expiration
  778. * @param data_len number of bytes in @a data
  779. */
  780. static void
  781. handle_p2p_reply (void *cls,
  782. enum GNUNET_BLOCK_EvaluationResult eval,
  783. struct GSF_PendingRequest *pr,
  784. uint32_t reply_anonymity_level,
  785. struct GNUNET_TIME_Absolute expiration,
  786. struct GNUNET_TIME_Absolute last_transmission,
  787. enum GNUNET_BLOCK_Type type,
  788. const void *data,
  789. size_t data_len)
  790. {
  791. struct PeerRequest *peerreq = cls;
  792. struct GSF_ConnectedPeer *cp = peerreq->cp;
  793. struct GSF_PendingRequestData *prd;
  794. struct PutMessage *pm;
  795. size_t msize;
  796. GNUNET_assert (data_len + sizeof (struct PutMessage) <
  797. GNUNET_SERVER_MAX_MESSAGE_SIZE);
  798. GNUNET_assert (peerreq->pr == pr);
  799. prd = GSF_pending_request_get_data_ (pr);
  800. if (NULL == data)
  801. {
  802. free_pending_request (peerreq, &prd->query);
  803. return;
  804. }
  805. GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
  806. if ((prd->type != type) && (GNUNET_BLOCK_TYPE_ANY != prd->type))
  807. {
  808. GNUNET_STATISTICS_update (GSF_stats,
  809. gettext_noop
  810. ("# replies dropped due to type mismatch"),
  811. 1, GNUNET_NO);
  812. return;
  813. }
  814. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  815. "Transmitting result for query `%s' to peer\n",
  816. GNUNET_h2s (&prd->query));
  817. GNUNET_STATISTICS_update (GSF_stats,
  818. gettext_noop ("# replies received for other peers"),
  819. 1, GNUNET_NO);
  820. msize = sizeof (struct PutMessage) + data_len;
  821. if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  822. {
  823. GNUNET_break (0);
  824. return;
  825. }
  826. if ((UINT32_MAX != reply_anonymity_level) && (reply_anonymity_level > 1))
  827. {
  828. if (reply_anonymity_level - 1 > GSF_cover_content_count)
  829. {
  830. GNUNET_STATISTICS_update (GSF_stats,
  831. gettext_noop
  832. ("# replies dropped due to insufficient cover traffic"),
  833. 1, GNUNET_NO);
  834. return;
  835. }
  836. GSF_cover_content_count -= (reply_anonymity_level - 1);
  837. }
  838. pm = GNUNET_malloc (msize);
  839. pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
  840. pm->header.size = htons (msize);
  841. pm->type = htonl (type);
  842. pm->expiration = GNUNET_TIME_absolute_hton (expiration);
  843. memcpy (&pm[1], data, data_len);
  844. if ((UINT32_MAX != reply_anonymity_level) && (0 != reply_anonymity_level) &&
  845. (GNUNET_YES == GSF_enable_randomized_delays))
  846. {
  847. struct GSF_DelayedHandle *dh;
  848. dh = GNUNET_new (struct GSF_DelayedHandle);
  849. dh->cp = cp;
  850. dh->pm = pm;
  851. dh->msize = msize;
  852. GNUNET_CONTAINER_DLL_insert (cp->delayed_head, cp->delayed_tail, dh);
  853. dh->delay_task =
  854. GNUNET_SCHEDULER_add_delayed (get_randomized_delay (),
  855. &transmit_delayed_now, dh);
  856. }
  857. else
  858. {
  859. (void) GSF_peer_transmit_ (cp, GNUNET_NO, UINT32_MAX, REPLY_TIMEOUT, msize,
  860. &copy_reply, pm);
  861. }
  862. if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
  863. return;
  864. if (NULL == peerreq->kill_task)
  865. {
  866. GNUNET_STATISTICS_update (GSF_stats,
  867. gettext_noop
  868. ("# P2P searches destroyed due to ultimate reply"),
  869. 1, GNUNET_NO);
  870. peerreq->kill_task =
  871. GNUNET_SCHEDULER_add_now (&peer_request_destroy, peerreq);
  872. }
  873. }
  874. /**
  875. * Increase the peer's respect by a value.
  876. *
  877. * @param cp which peer to change the respect value on
  878. * @param value is the int value by which the
  879. * peer's credit is to be increased or decreased
  880. * @returns the actual change in respect (positive or negative)
  881. */
  882. static int
  883. change_peer_respect (struct GSF_ConnectedPeer *cp, int value)
  884. {
  885. if (0 == value)
  886. return 0;
  887. GNUNET_assert (NULL != cp);
  888. if (value > 0)
  889. {
  890. if (cp->ppd.respect + value < cp->ppd.respect)
  891. {
  892. value = UINT32_MAX - cp->ppd.respect;
  893. cp->ppd.respect = UINT32_MAX;
  894. }
  895. else
  896. cp->ppd.respect += value;
  897. }
  898. else
  899. {
  900. if (cp->ppd.respect < -value)
  901. {
  902. value = -cp->ppd.respect;
  903. cp->ppd.respect = 0;
  904. }
  905. else
  906. cp->ppd.respect += value;
  907. }
  908. return value;
  909. }
  910. /**
  911. * We've received a request with the specified priority. Bound it
  912. * according to how much we respect the given peer.
  913. *
  914. * @param prio_in requested priority
  915. * @param cp the peer making the request
  916. * @return effective priority
  917. */
  918. static int32_t
  919. bound_priority (uint32_t prio_in, struct GSF_ConnectedPeer *cp)
  920. {
  921. #define N ((double)128.0)
  922. uint32_t ret;
  923. double rret;
  924. int ld;
  925. ld = GSF_test_get_load_too_high_ (0);
  926. if (GNUNET_SYSERR == ld)
  927. {
  928. #if INSANE_STATISTICS
  929. GNUNET_STATISTICS_update (GSF_stats,
  930. gettext_noop
  931. ("# requests done for free (low load)"), 1,
  932. GNUNET_NO);
  933. #endif
  934. return 0; /* excess resources */
  935. }
  936. if (prio_in > INT32_MAX)
  937. prio_in = INT32_MAX;
  938. ret = -change_peer_respect (cp, -(int) prio_in);
  939. if (ret > 0)
  940. {
  941. if (ret > GSF_current_priorities + N)
  942. rret = GSF_current_priorities + N;
  943. else
  944. rret = ret;
  945. GSF_current_priorities = (GSF_current_priorities * (N - 1) + rret) / N;
  946. }
  947. if ((GNUNET_YES == ld) && (ret > 0))
  948. {
  949. /* try with charging */
  950. ld = GSF_test_get_load_too_high_ (ret);
  951. }
  952. if (GNUNET_YES == ld)
  953. {
  954. GNUNET_STATISTICS_update (GSF_stats,
  955. gettext_noop
  956. ("# request dropped, priority insufficient"), 1,
  957. GNUNET_NO);
  958. /* undo charge */
  959. change_peer_respect (cp, (int) ret);
  960. return -1; /* not enough resources */
  961. }
  962. else
  963. {
  964. GNUNET_STATISTICS_update (GSF_stats,
  965. gettext_noop
  966. ("# requests done for a price (normal load)"), 1,
  967. GNUNET_NO);
  968. }
  969. #undef N
  970. return ret;
  971. }
  972. /**
  973. * The priority level imposes a bound on the maximum
  974. * value for the ttl that can be requested.
  975. *
  976. * @param ttl_in requested ttl
  977. * @param prio given priority
  978. * @return ttl_in if ttl_in is below the limit,
  979. * otherwise the ttl-limit for the given priority
  980. */
  981. static int32_t
  982. bound_ttl (int32_t ttl_in, uint32_t prio)
  983. {
  984. unsigned long long allowed;
  985. if (ttl_in <= 0)
  986. return ttl_in;
  987. allowed = ((unsigned long long) prio) * TTL_DECREMENT / 1000;
  988. if (ttl_in > allowed)
  989. {
  990. if (allowed >= (1 << 30))
  991. return 1 << 30;
  992. return allowed;
  993. }
  994. return ttl_in;
  995. }
  996. /**
  997. * Handle P2P "QUERY" message. Creates the pending request entry
  998. * and sets up all of the data structures to that we will
  999. * process replies properly. Does not initiate forwarding or
  1000. * local database lookups.
  1001. *
  1002. * @param other the other peer involved (sender or receiver, NULL
  1003. * for loopback messages where we are both sender and receiver)
  1004. * @param message the actual message
  1005. * @return pending request handle, NULL on error
  1006. */
  1007. struct GSF_PendingRequest *
  1008. GSF_handle_p2p_query_ (const struct GNUNET_PeerIdentity *other,
  1009. const struct GNUNET_MessageHeader *message)
  1010. {
  1011. struct PeerRequest *peerreq;
  1012. struct GSF_PendingRequest *pr;
  1013. struct GSF_PendingRequestData *prd;
  1014. struct GSF_ConnectedPeer *cp;
  1015. struct GSF_ConnectedPeer *cps;
  1016. const struct GNUNET_PeerIdentity *target;
  1017. enum GSF_PendingRequestOptions options;
  1018. uint16_t msize;
  1019. const struct GetMessage *gm;
  1020. unsigned int bits;
  1021. const struct GNUNET_PeerIdentity *opt;
  1022. uint32_t bm;
  1023. size_t bfsize;
  1024. uint32_t ttl_decrement;
  1025. int32_t priority;
  1026. int32_t ttl;
  1027. enum GNUNET_BLOCK_Type type;
  1028. GNUNET_PEER_Id spid;
  1029. GNUNET_assert (other != NULL);
  1030. msize = ntohs (message->size);
  1031. if (msize < sizeof (struct GetMessage))
  1032. {
  1033. GNUNET_break_op (0);
  1034. return NULL;
  1035. }
  1036. GNUNET_STATISTICS_update (GSF_stats,
  1037. gettext_noop
  1038. ("# GET requests received (from other peers)"), 1,
  1039. GNUNET_NO);
  1040. gm = (const struct GetMessage *) message;
  1041. type = ntohl (gm->type);
  1042. bm = ntohl (gm->hash_bitmap);
  1043. bits = 0;
  1044. while (bm > 0)
  1045. {
  1046. if (1 == (bm & 1))
  1047. bits++;
  1048. bm >>= 1;
  1049. }
  1050. if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_PeerIdentity))
  1051. {
  1052. GNUNET_break_op (0);
  1053. return NULL;
  1054. }
  1055. opt = (const struct GNUNET_PeerIdentity *) &gm[1];
  1056. bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
  1057. /* bfsize must be power of 2, check! */
  1058. if (0 != ((bfsize - 1) & bfsize))
  1059. {
  1060. GNUNET_break_op (0);
  1061. return NULL;
  1062. }
  1063. GSF_cover_query_count++;
  1064. bm = ntohl (gm->hash_bitmap);
  1065. bits = 0;
  1066. cps = GSF_peer_get_ (other);
  1067. if (NULL == cps)
  1068. {
  1069. /* peer must have just disconnected */
  1070. GNUNET_STATISTICS_update (GSF_stats,
  1071. gettext_noop
  1072. ("# requests dropped due to initiator not being connected"),
  1073. 1, GNUNET_NO);
  1074. return NULL;
  1075. }
  1076. if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
  1077. cp = GSF_peer_get_ (&opt[bits++]);
  1078. else
  1079. cp = cps;
  1080. if (NULL == cp)
  1081. {
  1082. if (0 != (bm & GET_MESSAGE_BIT_RETURN_TO))
  1083. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1084. "Failed to find RETURN-TO peer `%4s' in connection set. Dropping query.\n",
  1085. GNUNET_i2s (&opt[bits - 1]));
  1086. else
  1087. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1088. "Failed to find peer `%4s' in connection set. Dropping query.\n",
  1089. GNUNET_i2s (other));
  1090. #if INSANE_STATISTICS
  1091. GNUNET_STATISTICS_update (GSF_stats,
  1092. gettext_noop
  1093. ("# requests dropped due to missing reverse route"),
  1094. 1, GNUNET_NO);
  1095. #endif
  1096. return NULL;
  1097. }
  1098. /* note that we can really only check load here since otherwise
  1099. * peers could find out that we are overloaded by not being
  1100. * disconnected after sending us a malformed query... */
  1101. priority = bound_priority (ntohl (gm->priority), cps);
  1102. if (priority < 0)
  1103. {
  1104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1105. "Dropping query from `%s', this peer is too busy.\n",
  1106. GNUNET_i2s (other));
  1107. return NULL;
  1108. }
  1109. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1110. "Received request for `%s' of type %u from peer `%4s' with flags %u\n",
  1111. GNUNET_h2s (&gm->query),
  1112. (unsigned int) type,
  1113. GNUNET_i2s (other),
  1114. (unsigned int) bm);
  1115. target =
  1116. (0 !=
  1117. (bm & GET_MESSAGE_BIT_TRANSMIT_TO)) ? (&opt[bits++]) : NULL;
  1118. options = GSF_PRO_DEFAULTS;
  1119. spid = 0;
  1120. if ((GNUNET_LOAD_get_load (cp->ppd.transmission_delay) > 3 * (1 + priority))
  1121. || (GNUNET_LOAD_get_average (cp->ppd.transmission_delay) >
  1122. GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value_us * 2 +
  1123. GNUNET_LOAD_get_average (GSF_rt_entry_lifetime)))
  1124. {
  1125. /* don't have BW to send to peer, or would likely take longer than we have for it,
  1126. * so at best indirect the query */
  1127. priority = 0;
  1128. options |= GSF_PRO_FORWARD_ONLY;
  1129. spid = GNUNET_PEER_intern (other);
  1130. GNUNET_assert (0 != spid);
  1131. }
  1132. ttl = bound_ttl (ntohl (gm->ttl), priority);
  1133. /* decrement ttl (always) */
  1134. ttl_decrement =
  1135. 2 * TTL_DECREMENT + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1136. TTL_DECREMENT);
  1137. if ((ttl < 0) && (((int32_t) (ttl - ttl_decrement)) > 0))
  1138. {
  1139. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1140. "Dropping query from `%s' due to TTL underflow (%d - %u).\n",
  1141. GNUNET_i2s (other), ttl, ttl_decrement);
  1142. GNUNET_STATISTICS_update (GSF_stats,
  1143. gettext_noop
  1144. ("# requests dropped due TTL underflow"), 1,
  1145. GNUNET_NO);
  1146. /* integer underflow => drop (should be very rare)! */
  1147. return NULL;
  1148. }
  1149. ttl -= ttl_decrement;
  1150. /* test if the request already exists */
  1151. peerreq = GNUNET_CONTAINER_multihashmap_get (cp->request_map, &gm->query);
  1152. if (peerreq != NULL)
  1153. {
  1154. pr = peerreq->pr;
  1155. prd = GSF_pending_request_get_data_ (pr);
  1156. if (prd->type == type)
  1157. {
  1158. if (prd->ttl.abs_value_us >= GNUNET_TIME_absolute_get ().abs_value_us + ttl * 1000LL)
  1159. {
  1160. /* existing request has higher TTL, drop new one! */
  1161. prd->priority += priority;
  1162. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1163. "Have existing request with higher TTL, dropping new request.\n",
  1164. GNUNET_i2s (other));
  1165. GNUNET_STATISTICS_update (GSF_stats,
  1166. gettext_noop
  1167. ("# requests dropped due to higher-TTL request"),
  1168. 1, GNUNET_NO);
  1169. return NULL;
  1170. }
  1171. /* existing request has lower TTL, drop old one! */
  1172. priority += prd->priority;
  1173. GSF_pending_request_cancel_ (pr, GNUNET_YES);
  1174. free_pending_request (peerreq, &gm->query);
  1175. }
  1176. }
  1177. peerreq = GNUNET_new (struct PeerRequest);
  1178. peerreq->cp = cp;
  1179. pr = GSF_pending_request_create_ (options, type, &gm->query,
  1180. target,
  1181. (bfsize >
  1182. 0) ? (const char *) &opt[bits] : NULL,
  1183. bfsize, ntohl (gm->filter_mutator),
  1184. 1 /* anonymity */ ,
  1185. (uint32_t) priority, ttl, spid, GNUNET_PEER_intern (other), NULL, 0, /* replies_seen */
  1186. &handle_p2p_reply, peerreq);
  1187. GNUNET_assert (NULL != pr);
  1188. peerreq->pr = pr;
  1189. GNUNET_break (GNUNET_OK ==
  1190. GNUNET_CONTAINER_multihashmap_put (cp->request_map, &gm->query,
  1191. peerreq,
  1192. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  1193. GNUNET_STATISTICS_update (GSF_stats,
  1194. gettext_noop
  1195. ("# P2P query messages received and processed"), 1,
  1196. GNUNET_NO);
  1197. GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# P2P searches active"),
  1198. 1, GNUNET_NO);
  1199. return pr;
  1200. }
  1201. /**
  1202. * Function called if there has been a timeout trying to satisfy
  1203. * a transmission request.
  1204. *
  1205. * @param cls the `struct GSF_PeerTransmitHandle` of the request
  1206. * @param tc scheduler context
  1207. */
  1208. static void
  1209. peer_transmit_timeout (void *cls,
  1210. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1211. {
  1212. struct GSF_PeerTransmitHandle *pth = cls;
  1213. struct GSF_ConnectedPeer *cp;
  1214. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1215. "Timeout trying to transmit to other peer\n");
  1216. pth->timeout_task = NULL;
  1217. cp = pth->cp;
  1218. GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
  1219. if (GNUNET_YES == pth->is_query)
  1220. GNUNET_assert (0 < cp->ppd.pending_queries--);
  1221. else if (GNUNET_NO == pth->is_query)
  1222. GNUNET_assert (0 < cp->ppd.pending_replies--);
  1223. GNUNET_LOAD_update (cp->ppd.transmission_delay, UINT64_MAX);
  1224. if (NULL != cp->cth)
  1225. {
  1226. GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
  1227. cp->cth = NULL;
  1228. }
  1229. pth->gmc (pth->gmc_cls, 0, NULL);
  1230. GNUNET_assert (0 == cp->cth_in_progress);
  1231. GNUNET_free (pth);
  1232. }
  1233. /**
  1234. * Transmit a message to the given peer as soon as possible.
  1235. * If the peer disconnects before the transmission can happen,
  1236. * the callback is invoked with a `NULL` @a buffer.
  1237. *
  1238. * @param cp target peer
  1239. * @param is_query is this a query (#GNUNET_YES) or content (#GNUNET_NO) or neither (#GNUNET_SYSERR)
  1240. * @param priority how important is this request?
  1241. * @param timeout when does this request timeout (call gmc with error)
  1242. * @param size number of bytes we would like to send to the peer
  1243. * @param gmc function to call to get the message
  1244. * @param gmc_cls closure for @a gmc
  1245. * @return handle to cancel request
  1246. */
  1247. struct GSF_PeerTransmitHandle *
  1248. GSF_peer_transmit_ (struct GSF_ConnectedPeer *cp,
  1249. int is_query,
  1250. uint32_t priority,
  1251. struct GNUNET_TIME_Relative timeout,
  1252. size_t size,
  1253. GSF_GetMessageCallback gmc, void *gmc_cls)
  1254. {
  1255. struct GSF_PeerTransmitHandle *pth;
  1256. struct GSF_PeerTransmitHandle *pos;
  1257. struct GSF_PeerTransmitHandle *prev;
  1258. pth = GNUNET_new (struct GSF_PeerTransmitHandle);
  1259. pth->transmission_request_start_time = GNUNET_TIME_absolute_get ();
  1260. pth->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  1261. pth->gmc = gmc;
  1262. pth->gmc_cls = gmc_cls;
  1263. pth->size = size;
  1264. pth->is_query = is_query;
  1265. pth->priority = priority;
  1266. pth->cp = cp;
  1267. /* insertion sort (by priority, descending) */
  1268. prev = NULL;
  1269. pos = cp->pth_head;
  1270. while ((NULL != pos) && (pos->priority > priority))
  1271. {
  1272. prev = pos;
  1273. pos = pos->next;
  1274. }
  1275. GNUNET_CONTAINER_DLL_insert_after (cp->pth_head, cp->pth_tail, prev, pth);
  1276. if (GNUNET_YES == is_query)
  1277. cp->ppd.pending_queries++;
  1278. else if (GNUNET_NO == is_query)
  1279. cp->ppd.pending_replies++;
  1280. pth->timeout_task =
  1281. GNUNET_SCHEDULER_add_delayed (timeout, &peer_transmit_timeout, pth);
  1282. schedule_transmission (pth);
  1283. return pth;
  1284. }
  1285. /**
  1286. * Cancel an earlier request for transmission.
  1287. *
  1288. * @param pth request to cancel
  1289. */
  1290. void
  1291. GSF_peer_transmit_cancel_ (struct GSF_PeerTransmitHandle *pth)
  1292. {
  1293. struct GSF_ConnectedPeer *cp;
  1294. if (NULL != pth->timeout_task)
  1295. {
  1296. GNUNET_SCHEDULER_cancel (pth->timeout_task);
  1297. pth->timeout_task = NULL;
  1298. }
  1299. cp = pth->cp;
  1300. GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
  1301. if (GNUNET_YES == pth->is_query)
  1302. GNUNET_assert (0 < cp->ppd.pending_queries--);
  1303. else if (GNUNET_NO == pth->is_query)
  1304. GNUNET_assert (0 < cp->ppd.pending_replies--);
  1305. GNUNET_free (pth);
  1306. }
  1307. /**
  1308. * Report on receiving a reply; update the performance record of the given peer.
  1309. *
  1310. * @param cp responding peer (will be updated)
  1311. * @param request_time time at which the original query was transmitted
  1312. * @param request_priority priority of the original request
  1313. */
  1314. void
  1315. GSF_peer_update_performance_ (struct GSF_ConnectedPeer *cp,
  1316. struct GNUNET_TIME_Absolute request_time,
  1317. uint32_t request_priority)
  1318. {
  1319. struct GNUNET_TIME_Relative delay;
  1320. delay = GNUNET_TIME_absolute_get_duration (request_time);
  1321. cp->ppd.avg_reply_delay.rel_value_us =
  1322. (cp->ppd.avg_reply_delay.rel_value_us * (RUNAVG_DELAY_N - 1) +
  1323. delay.rel_value_us) / RUNAVG_DELAY_N;
  1324. cp->ppd.avg_priority =
  1325. (cp->ppd.avg_priority * (RUNAVG_DELAY_N - 1) +
  1326. request_priority) / RUNAVG_DELAY_N;
  1327. }
  1328. /**
  1329. * Report on receiving a reply in response to an initiating client.
  1330. * Remember that this peer is good for this client.
  1331. *
  1332. * @param cp responding peer (will be updated)
  1333. * @param initiator_client local client on responsible for query
  1334. */
  1335. void
  1336. GSF_peer_update_responder_client_ (struct GSF_ConnectedPeer *cp,
  1337. struct GSF_LocalClient *initiator_client)
  1338. {
  1339. cp->ppd.last_client_replies[cp->last_client_replies_woff++ %
  1340. CS2P_SUCCESS_LIST_SIZE] = initiator_client;
  1341. }
  1342. /**
  1343. * Report on receiving a reply in response to an initiating peer.
  1344. * Remember that this peer is good for this initiating peer.
  1345. *
  1346. * @param cp responding peer (will be updated)
  1347. * @param initiator_peer other peer responsible for query
  1348. */
  1349. void
  1350. GSF_peer_update_responder_peer_ (struct GSF_ConnectedPeer *cp,
  1351. const struct GSF_ConnectedPeer *initiator_peer)
  1352. {
  1353. unsigned int woff;
  1354. woff = cp->last_p2p_replies_woff % P2P_SUCCESS_LIST_SIZE;
  1355. GNUNET_PEER_change_rc (cp->ppd.last_p2p_replies[woff], -1);
  1356. cp->ppd.last_p2p_replies[woff] = initiator_peer->ppd.pid;
  1357. GNUNET_PEER_change_rc (initiator_peer->ppd.pid, 1);
  1358. cp->last_p2p_replies_woff = (woff + 1) % P2P_SUCCESS_LIST_SIZE;
  1359. }
  1360. /**
  1361. * Write peer-respect information to a file - flush the buffer entry!
  1362. *
  1363. * @param cls unused
  1364. * @param key peer identity
  1365. * @param value the `struct GSF_ConnectedPeer` to flush
  1366. * @return #GNUNET_OK to continue iteration
  1367. */
  1368. static int
  1369. flush_respect (void *cls,
  1370. const struct GNUNET_PeerIdentity *key,
  1371. void *value)
  1372. {
  1373. struct GSF_ConnectedPeer *cp = value;
  1374. struct GNUNET_PeerIdentity pid;
  1375. if (cp->ppd.respect == cp->disk_respect)
  1376. return GNUNET_OK; /* unchanged */
  1377. GNUNET_assert (0 != cp->ppd.pid);
  1378. GNUNET_PEER_resolve (cp->ppd.pid, &pid);
  1379. GNUNET_PEERSTORE_store (peerstore, "fs", &pid, "respect", &cp->ppd.respect,
  1380. sizeof (cp->ppd.respect),
  1381. GNUNET_TIME_UNIT_FOREVER_ABS,
  1382. GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
  1383. return GNUNET_OK;
  1384. }
  1385. /**
  1386. * A peer disconnected from us. Tear down the connected peer
  1387. * record.
  1388. *
  1389. * @param cls unused
  1390. * @param peer identity of peer that connected
  1391. */
  1392. void
  1393. GSF_peer_disconnect_handler_ (void *cls,
  1394. const struct GNUNET_PeerIdentity *peer)
  1395. {
  1396. struct GSF_ConnectedPeer *cp;
  1397. struct GSF_PeerTransmitHandle *pth;
  1398. struct GSF_DelayedHandle *dh;
  1399. cp = GSF_peer_get_ (peer);
  1400. if (NULL == cp)
  1401. return; /* must have been disconnect from core with
  1402. * 'peer' == my_id, ignore */
  1403. flush_respect (NULL, peer, cp);
  1404. GNUNET_assert (GNUNET_YES ==
  1405. GNUNET_CONTAINER_multipeermap_remove (cp_map,
  1406. peer,
  1407. cp));
  1408. GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"),
  1409. GNUNET_CONTAINER_multipeermap_size (cp_map),
  1410. GNUNET_NO);
  1411. if (NULL != cp->respect_iterate_req)
  1412. {
  1413. GNUNET_PEERSTORE_iterate_cancel (cp->respect_iterate_req);
  1414. cp->respect_iterate_req = NULL;
  1415. }
  1416. if (NULL != cp->migration_pth)
  1417. {
  1418. GSF_peer_transmit_cancel_ (cp->migration_pth);
  1419. cp->migration_pth = NULL;
  1420. }
  1421. if (NULL != cp->rc)
  1422. {
  1423. GNUNET_ATS_reserve_bandwidth_cancel (cp->rc);
  1424. cp->rc = NULL;
  1425. }
  1426. if (NULL != cp->rc_delay_task)
  1427. {
  1428. GNUNET_SCHEDULER_cancel (cp->rc_delay_task);
  1429. cp->rc_delay_task = NULL;
  1430. }
  1431. GNUNET_CONTAINER_multihashmap_iterate (cp->request_map,
  1432. &cancel_pending_request, cp);
  1433. GNUNET_CONTAINER_multihashmap_destroy (cp->request_map);
  1434. cp->request_map = NULL;
  1435. GSF_plan_notify_peer_disconnect_ (cp);
  1436. GNUNET_LOAD_value_free (cp->ppd.transmission_delay);
  1437. GNUNET_PEER_decrement_rcs (cp->ppd.last_p2p_replies, P2P_SUCCESS_LIST_SIZE);
  1438. memset (cp->ppd.last_p2p_replies, 0, sizeof (cp->ppd.last_p2p_replies));
  1439. GSF_push_stop_ (cp);
  1440. if (NULL != cp->cth)
  1441. {
  1442. GNUNET_CORE_notify_transmit_ready_cancel (cp->cth);
  1443. cp->cth = NULL;
  1444. }
  1445. GNUNET_assert (0 == cp->cth_in_progress);
  1446. while (NULL != (pth = cp->pth_head))
  1447. {
  1448. if (pth->timeout_task != NULL)
  1449. {
  1450. GNUNET_SCHEDULER_cancel (pth->timeout_task);
  1451. pth->timeout_task = NULL;
  1452. }
  1453. GNUNET_CONTAINER_DLL_remove (cp->pth_head, cp->pth_tail, pth);
  1454. pth->gmc (pth->gmc_cls, 0, NULL);
  1455. GNUNET_free (pth);
  1456. }
  1457. while (NULL != (dh = cp->delayed_head))
  1458. {
  1459. GNUNET_CONTAINER_DLL_remove (cp->delayed_head, cp->delayed_tail, dh);
  1460. GNUNET_SCHEDULER_cancel (dh->delay_task);
  1461. GNUNET_free (dh->pm);
  1462. GNUNET_free (dh);
  1463. }
  1464. GNUNET_PEER_change_rc (cp->ppd.pid, -1);
  1465. if (NULL != cp->mig_revive_task)
  1466. {
  1467. GNUNET_SCHEDULER_cancel (cp->mig_revive_task);
  1468. cp->mig_revive_task = NULL;
  1469. }
  1470. GNUNET_free (cp);
  1471. }
  1472. /**
  1473. * Closure for #call_iterator().
  1474. */
  1475. struct IterationContext
  1476. {
  1477. /**
  1478. * Function to call on each entry.
  1479. */
  1480. GSF_ConnectedPeerIterator it;
  1481. /**
  1482. * Closure for @e it.
  1483. */
  1484. void *it_cls;
  1485. };
  1486. /**
  1487. * Function that calls the callback for each peer.
  1488. *
  1489. * @param cls the `struct IterationContext *`
  1490. * @param key identity of the peer
  1491. * @param value the `struct GSF_ConnectedPeer *`
  1492. * @return #GNUNET_YES to continue iteration
  1493. */
  1494. static int
  1495. call_iterator (void *cls,
  1496. const struct GNUNET_PeerIdentity *key,
  1497. void *value)
  1498. {
  1499. struct IterationContext *ic = cls;
  1500. struct GSF_ConnectedPeer *cp = value;
  1501. ic->it (ic->it_cls,
  1502. key, cp,
  1503. &cp->ppd);
  1504. return GNUNET_YES;
  1505. }
  1506. /**
  1507. * Iterate over all connected peers.
  1508. *
  1509. * @param it function to call for each peer
  1510. * @param it_cls closure for @a it
  1511. */
  1512. void
  1513. GSF_iterate_connected_peers_ (GSF_ConnectedPeerIterator it,
  1514. void *it_cls)
  1515. {
  1516. struct IterationContext ic;
  1517. ic.it = it;
  1518. ic.it_cls = it_cls;
  1519. GNUNET_CONTAINER_multipeermap_iterate (cp_map,
  1520. &call_iterator,
  1521. &ic);
  1522. }
  1523. /**
  1524. * Obtain the identity of a connected peer.
  1525. *
  1526. * @param cp peer to get identity of
  1527. * @param id identity to set (written to)
  1528. */
  1529. void
  1530. GSF_connected_peer_get_identity_ (const struct GSF_ConnectedPeer *cp,
  1531. struct GNUNET_PeerIdentity *id)
  1532. {
  1533. GNUNET_assert (0 != cp->ppd.pid);
  1534. GNUNET_PEER_resolve (cp->ppd.pid, id);
  1535. }
  1536. /**
  1537. * Obtain the identity of a connected peer.
  1538. *
  1539. * @param cp peer to get identity of
  1540. * @return reference to peer identity, valid until peer disconnects (!)
  1541. */
  1542. const struct GNUNET_PeerIdentity *
  1543. GSF_connected_peer_get_identity2_ (const struct GSF_ConnectedPeer *cp)
  1544. {
  1545. GNUNET_assert (0 != cp->ppd.pid);
  1546. return GNUNET_PEER_resolve2 (cp->ppd.pid);
  1547. }
  1548. /**
  1549. * Assemble a migration stop message for transmission.
  1550. *
  1551. * @param cls the `struct GSF_ConnectedPeer` to use
  1552. * @param size number of bytes we're allowed to write to @a buf
  1553. * @param buf where to copy the message
  1554. * @return number of bytes copied to @a buf
  1555. */
  1556. static size_t
  1557. create_migration_stop_message (void *cls,
  1558. size_t size,
  1559. void *buf)
  1560. {
  1561. struct GSF_ConnectedPeer *cp = cls;
  1562. struct MigrationStopMessage msm;
  1563. cp->migration_pth = NULL;
  1564. if (NULL == buf)
  1565. return 0;
  1566. GNUNET_assert (size >= sizeof (struct MigrationStopMessage));
  1567. msm.header.size = htons (sizeof (struct MigrationStopMessage));
  1568. msm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP);
  1569. msm.reserved = htonl (0);
  1570. msm.duration =
  1571. GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
  1572. (cp->last_migration_block));
  1573. memcpy (buf, &msm, sizeof (struct MigrationStopMessage));
  1574. GNUNET_STATISTICS_update (GSF_stats,
  1575. gettext_noop ("# migration stop messages sent"),
  1576. 1, GNUNET_NO);
  1577. return sizeof (struct MigrationStopMessage);
  1578. }
  1579. /**
  1580. * Ask a peer to stop migrating data to us until the given point
  1581. * in time.
  1582. *
  1583. * @param cp peer to ask
  1584. * @param block_time until when to block
  1585. */
  1586. void
  1587. GSF_block_peer_migration_ (struct GSF_ConnectedPeer *cp,
  1588. struct GNUNET_TIME_Absolute block_time)
  1589. {
  1590. if (cp->last_migration_block.abs_value_us > block_time.abs_value_us)
  1591. {
  1592. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1593. "Migration already blocked for another %s\n",
  1594. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining
  1595. (cp->last_migration_block), GNUNET_YES));
  1596. return; /* already blocked */
  1597. }
  1598. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking to stop migration for %s\n",
  1599. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (block_time),
  1600. GNUNET_YES));
  1601. cp->last_migration_block = block_time;
  1602. if (NULL != cp->migration_pth)
  1603. GSF_peer_transmit_cancel_ (cp->migration_pth);
  1604. cp->migration_pth =
  1605. GSF_peer_transmit_ (cp, GNUNET_SYSERR, UINT32_MAX,
  1606. GNUNET_TIME_UNIT_FOREVER_REL,
  1607. sizeof (struct MigrationStopMessage),
  1608. &create_migration_stop_message, cp);
  1609. }
  1610. /**
  1611. * Notify core about a preference we have for the given peer
  1612. * (to allocate more resources towards it). The change will
  1613. * be communicated the next time we reserve bandwidth with
  1614. * core (not instantly).
  1615. *
  1616. * @param cp peer to reserve bandwidth from
  1617. * @param pref preference change
  1618. */
  1619. void
  1620. GSF_connected_peer_change_preference_ (struct GSF_ConnectedPeer *cp,
  1621. uint64_t pref)
  1622. {
  1623. cp->inc_preference += pref;
  1624. }
  1625. /**
  1626. * Call this method periodically to flush respect information to disk.
  1627. *
  1628. * @param cls closure, not used
  1629. * @param tc task context, not used
  1630. */
  1631. static void
  1632. cron_flush_respect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  1633. {
  1634. if (NULL == cp_map)
  1635. return;
  1636. GNUNET_CONTAINER_multipeermap_iterate (cp_map, &flush_respect, NULL);
  1637. if (NULL == tc)
  1638. return;
  1639. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1640. return;
  1641. GNUNET_SCHEDULER_add_delayed_with_priority (RESPECT_FLUSH_FREQ,
  1642. GNUNET_SCHEDULER_PRIORITY_HIGH,
  1643. &cron_flush_respect, NULL);
  1644. }
  1645. /**
  1646. * Initialize peer management subsystem.
  1647. */
  1648. void
  1649. GSF_connected_peer_init_ ()
  1650. {
  1651. cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
  1652. peerstore = GNUNET_PEERSTORE_connect (GSF_cfg);
  1653. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH,
  1654. &cron_flush_respect, NULL);
  1655. }
  1656. /**
  1657. * Iterator to free peer entries.
  1658. *
  1659. * @param cls closure, unused
  1660. * @param key current key code
  1661. * @param value value in the hash map (peer entry)
  1662. * @return #GNUNET_YES (we should continue to iterate)
  1663. */
  1664. static int
  1665. clean_peer (void *cls,
  1666. const struct GNUNET_PeerIdentity *key,
  1667. void *value)
  1668. {
  1669. GSF_peer_disconnect_handler_ (NULL, key);
  1670. return GNUNET_YES;
  1671. }
  1672. /**
  1673. * Shutdown peer management subsystem.
  1674. */
  1675. void
  1676. GSF_connected_peer_done_ ()
  1677. {
  1678. cron_flush_respect (NULL, NULL);
  1679. GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_peer, NULL);
  1680. GNUNET_CONTAINER_multipeermap_destroy (cp_map);
  1681. cp_map = NULL;
  1682. GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
  1683. }
  1684. /**
  1685. * Iterator to remove references to LC entry.
  1686. *
  1687. * @param cls the `struct GSF_LocalClient *` to look for
  1688. * @param key current key code
  1689. * @param value value in the hash map (peer entry)
  1690. * @return #GNUNET_YES (we should continue to iterate)
  1691. */
  1692. static int
  1693. clean_local_client (void *cls,
  1694. const struct GNUNET_PeerIdentity *key,
  1695. void *value)
  1696. {
  1697. const struct GSF_LocalClient *lc = cls;
  1698. struct GSF_ConnectedPeer *cp = value;
  1699. unsigned int i;
  1700. for (i = 0; i < CS2P_SUCCESS_LIST_SIZE; i++)
  1701. if (cp->ppd.last_client_replies[i] == lc)
  1702. cp->ppd.last_client_replies[i] = NULL;
  1703. return GNUNET_YES;
  1704. }
  1705. /**
  1706. * Notification that a local client disconnected. Clean up all of our
  1707. * references to the given handle.
  1708. *
  1709. * @param lc handle to the local client (henceforth invalid)
  1710. */
  1711. void
  1712. GSF_handle_local_client_disconnect_ (const struct GSF_LocalClient *lc)
  1713. {
  1714. if (NULL == cp_map)
  1715. return; /* already cleaned up */
  1716. GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_local_client,
  1717. (void *) lc);
  1718. }
  1719. /* end of gnunet-service-fs_cp.c */