2
0

gnunet-daemon-experimentation_nodes.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2012-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 experimentation/gnunet-daemon-experimentation_nodes.c
  19. * @brief experimentation daemon: node management
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_core_service.h"
  26. #include "gnunet_statistics_service.h"
  27. #include "gnunet-daemon-experimentation.h"
  28. #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  29. /**
  30. * Core handle
  31. */
  32. static struct GNUNET_CORE_Handle *ch;
  33. /**
  34. * Peer's own identity
  35. */
  36. static struct GNUNET_PeerIdentity me;
  37. /**
  38. * Nodes with a pending request
  39. */
  40. static struct GNUNET_CONTAINER_MultiPeerMap *nodes_requested;
  41. /**
  42. * Active experimentation nodes
  43. */
  44. static struct GNUNET_CONTAINER_MultiPeerMap *nodes_active;
  45. /**
  46. * Inactive experimentation nodes
  47. * To be excluded from future requests
  48. */
  49. static struct GNUNET_CONTAINER_MultiPeerMap *nodes_inactive;
  50. struct NodeComCtx
  51. {
  52. struct NodeComCtx *prev;
  53. struct NodeComCtx *next;
  54. struct Node *n;
  55. struct Experiment *e;
  56. size_t size;
  57. GNUNET_CONNECTION_TransmitReadyNotify notify;
  58. void *notify_cls;
  59. };
  60. /**
  61. * Update statistics
  62. *
  63. * @param m peermap to update values from
  64. */
  65. static void
  66. update_stats (struct GNUNET_CONTAINER_MultiPeerMap *m)
  67. {
  68. GNUNET_assert (NULL != m);
  69. GNUNET_assert (NULL != GED_stats);
  70. if (m == nodes_active)
  71. {
  72. GNUNET_STATISTICS_set (GED_stats, "# nodes active",
  73. GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  74. }
  75. else if (m == nodes_inactive)
  76. {
  77. GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
  78. GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  79. }
  80. else if (m == nodes_requested)
  81. {
  82. GNUNET_STATISTICS_set (GED_stats, "# nodes requested",
  83. GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  84. }
  85. else
  86. GNUNET_break (0);
  87. }
  88. /**
  89. * Clean up node
  90. *
  91. * @param cls the peermap to clean up
  92. * @param key key of the current node
  93. * @param value related node object
  94. * @return always #GNUNET_OK
  95. */
  96. static int
  97. cleanup_node (void *cls,
  98. const struct GNUNET_PeerIdentity * key,
  99. void *value)
  100. {
  101. struct Node *n;
  102. struct NodeComCtx *e_cur;
  103. struct NodeComCtx *e_next;
  104. struct GNUNET_CONTAINER_MultiPeerMap *cur = cls;
  105. n = value;
  106. if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
  107. {
  108. GNUNET_SCHEDULER_cancel (n->timeout_task);
  109. n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  110. }
  111. if (NULL != n->cth)
  112. {
  113. GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
  114. n->cth = NULL;
  115. }
  116. e_next = n->e_req_head;
  117. while (NULL != (e_cur = e_next))
  118. {
  119. e_next = e_cur->next;
  120. GNUNET_CONTAINER_DLL_remove (n->e_req_head, n->e_req_tail, e_cur);
  121. GNUNET_free (e_cur);
  122. }
  123. GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (cur, key, value));
  124. GNUNET_free (value);
  125. return GNUNET_OK;
  126. }
  127. /**
  128. * Check if id passed is my id
  129. *
  130. * @param id the id to check
  131. * @return GNUNET_YES or GNUNET_NO
  132. */
  133. static int
  134. is_me (const struct GNUNET_PeerIdentity *id)
  135. {
  136. if (0 == memcmp (&me, id, sizeof (me)))
  137. return GNUNET_YES;
  138. else
  139. return GNUNET_NO;
  140. }
  141. /**
  142. * Core startup callback
  143. *
  144. * @param cls unused
  145. * @param my_identity my id
  146. */
  147. static void
  148. core_startup_handler (void *cls,
  149. const struct GNUNET_PeerIdentity *my_identity)
  150. {
  151. me = *my_identity;
  152. }
  153. static void
  154. schedule_transmisson (struct NodeComCtx *e_ctx);
  155. static size_t
  156. transmit_read_wrapper (void *cls, size_t bufsize, void *buf)
  157. {
  158. struct NodeComCtx *e_ctx = cls;
  159. struct NodeComCtx *next;
  160. size_t res = e_ctx->notify (e_ctx->notify_cls, bufsize, buf);
  161. e_ctx->n->cth = NULL;
  162. GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
  163. next = e_ctx->n->e_req_head;
  164. GNUNET_free (e_ctx);
  165. if (NULL != next)
  166. {
  167. /* Schedule next message */
  168. schedule_transmisson (next);
  169. }
  170. return res;
  171. }
  172. static void
  173. schedule_transmisson (struct NodeComCtx *e_ctx)
  174. {
  175. if (NULL != e_ctx->n->cth)
  176. return;
  177. e_ctx->n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO,
  178. GNUNET_CORE_PRIO_BEST_EFFORT,
  179. FAST_TIMEOUT,
  180. &e_ctx->n->id, e_ctx->size,
  181. transmit_read_wrapper, e_ctx);
  182. if (NULL == e_ctx->n->cth)
  183. {
  184. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  185. _("Cannot send message to peer `%s' for experiment `%s'\n"),
  186. GNUNET_i2s(&e_ctx->n->id), e_ctx->e->name);
  187. GNUNET_free (e_ctx);
  188. }
  189. }
  190. /**
  191. * Remove experimentation request due to timeout
  192. *
  193. * @param cls the related node
  194. * @param tc scheduler's task context
  195. */
  196. static void
  197. remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  198. {
  199. struct Node *n = cls;
  200. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  201. "Removing request for peer %s due to timeout\n",
  202. GNUNET_i2s (&n->id));
  203. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_requested, &n->id))
  204. {
  205. GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_requested, &n->id, n));
  206. update_stats (nodes_requested);
  207. GNUNET_CONTAINER_multipeermap_put (nodes_inactive, &n->id, n,
  208. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  209. update_stats (nodes_inactive);
  210. }
  211. n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  212. }
  213. static int
  214. append_public_key (void *cls,
  215. const struct GNUNET_HashCode *key,
  216. void *value)
  217. {
  218. struct GNUNET_CRYPTO_EddsaPublicKey **issuers = cls;
  219. struct Issuer *issuer = value;
  220. *issuers[0] = issuer->pubkey;
  221. *issuers = &((*issuers)[1]);
  222. return GNUNET_OK;
  223. }
  224. /**
  225. * Core's transmit notify callback to send request
  226. *
  227. * @param cls the related node
  228. * @param bufsize buffer size
  229. * @param buf the buffer to copy to
  230. * @return bytes passed
  231. */
  232. static size_t
  233. send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
  234. {
  235. struct Node *n = cls;
  236. struct Experimentation_Request msg;
  237. unsigned int my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  238. size_t msg_size = sizeof (msg);
  239. size_t ri_size = sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) * my_issuer_count;
  240. size_t total_size = msg_size + ri_size;
  241. struct GNUNET_CRYPTO_EddsaPublicKey *issuers;
  242. n->cth = NULL;
  243. if (NULL == buf)
  244. {
  245. /* client disconnected */
  246. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  247. "Client disconnected\n");
  248. if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
  249. GNUNET_SCHEDULER_cancel (n->timeout_task);
  250. GNUNET_SCHEDULER_add_now (&remove_request, n);
  251. return 0;
  252. }
  253. GNUNET_assert (bufsize >= total_size);
  254. msg.msg.size = htons (total_size);
  255. msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
  256. msg.capabilities = htonl (GSE_node_capabilities);
  257. msg.issuer_count = htonl (my_issuer_count);
  258. memcpy (buf, &msg, msg_size);
  259. issuers = (struct GNUNET_CRYPTO_EddsaPublicKey *) buf + msg_size;
  260. GNUNET_CONTAINER_multihashmap_iterate (valid_issuers,
  261. &append_public_key,
  262. &issuers);
  263. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  264. _("Sending experimentation request to peer %s\n"),
  265. GNUNET_i2s (&n->id));
  266. return total_size;
  267. }
  268. /**
  269. * Send request to peer to start add him to to the set of experimentation nodes
  270. *
  271. * @param peer the peer to send to
  272. */
  273. static void
  274. send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
  275. {
  276. struct Node *n;
  277. struct NodeComCtx *e_ctx;
  278. size_t size;
  279. size_t c_issuers;
  280. c_issuers = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  281. size = sizeof (struct Experimentation_Request) +
  282. c_issuers * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  283. n = GNUNET_new (struct Node);
  284. n->id = *peer;
  285. n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
  286. n->capabilities = NONE;
  287. e_ctx = GNUNET_new (struct NodeComCtx);
  288. e_ctx->n = n;
  289. e_ctx->e = NULL;
  290. e_ctx->size = size;
  291. e_ctx->notify = &send_experimentation_request_cb;
  292. e_ctx->notify_cls = n;
  293. GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
  294. schedule_transmisson (e_ctx);
  295. GNUNET_assert (GNUNET_OK ==
  296. GNUNET_CONTAINER_multipeermap_put (nodes_requested,
  297. peer, n,
  298. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
  299. update_stats (nodes_requested);
  300. }
  301. /**
  302. * Core's transmit notify callback to send response
  303. *
  304. * @param cls the related node
  305. * @param bufsize buffer size
  306. * @param buf the buffer to copy to
  307. * @return bytes passed
  308. */
  309. static size_t
  310. send_response_cb (void *cls, size_t bufsize, void *buf)
  311. {
  312. struct Node *n = cls;
  313. struct Experimentation_Response msg;
  314. size_t c_issuers = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  315. size_t ri_size = c_issuers * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  316. size_t msg_size = sizeof (msg);
  317. size_t total_size = msg_size + ri_size;
  318. struct GNUNET_CRYPTO_EddsaPublicKey *issuers;
  319. n->cth = NULL;
  320. if (buf == NULL)
  321. {
  322. /* client disconnected */
  323. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  324. "Client disconnected\n");
  325. return 0;
  326. }
  327. GNUNET_assert (bufsize >= total_size);
  328. msg.msg.size = htons (total_size);
  329. msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
  330. msg.capabilities = htonl (GSE_node_capabilities);
  331. msg.issuer_count = htonl (c_issuers);
  332. memcpy (buf, &msg, msg_size);
  333. issuers = (struct GNUNET_CRYPTO_EddsaPublicKey *) buf + msg_size;
  334. GNUNET_CONTAINER_multihashmap_iterate (valid_issuers,
  335. &append_public_key,
  336. &issuers);
  337. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  338. "Sending response to peer %s\n",
  339. GNUNET_i2s (&n->id));
  340. return total_size;
  341. }
  342. static void
  343. get_experiments_cb (struct Node *n, struct Experiment *e)
  344. {
  345. static int counter = 0;
  346. if (NULL == e)
  347. return; /* Done */
  348. /* Tell the scheduler to add a node with an experiment */
  349. GED_scheduler_add (n, e, GNUNET_YES);
  350. counter ++;
  351. }
  352. struct Node *
  353. get_node (const struct GNUNET_PeerIdentity *id)
  354. {
  355. struct Node * res;
  356. struct Node * tmp;
  357. res = NULL;
  358. tmp = NULL;
  359. tmp = GNUNET_CONTAINER_multipeermap_get (nodes_active, id);
  360. if (res == NULL)
  361. res = tmp;
  362. tmp = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, id);
  363. if (res == NULL)
  364. res = tmp;
  365. else
  366. GNUNET_break (0); /* Multiple instances */
  367. tmp = GNUNET_CONTAINER_multipeermap_get (nodes_requested, id);
  368. if (res == NULL)
  369. res = tmp;
  370. else
  371. GNUNET_break (0); /* Multiple instances */
  372. return res;
  373. }
  374. /**
  375. * Set a specific node as active
  376. *
  377. * @param n the node
  378. */
  379. static void
  380. node_make_active (struct Node *n)
  381. {
  382. int c1;
  383. GNUNET_CONTAINER_multipeermap_put (nodes_active,
  384. &n->id, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  385. update_stats (nodes_active);
  386. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  387. _("Added peer `%s' as active node\n"),
  388. GNUNET_i2s (&n->id));
  389. /* Request experiments for this node to start them */
  390. for (c1 = 0; c1 < n->issuer_count; c1++)
  391. {
  392. GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
  393. }
  394. }
  395. /**
  396. * Handle a request and send a response
  397. *
  398. * @param peer the source
  399. * @param message the message
  400. */
  401. static void
  402. handle_request (const struct GNUNET_PeerIdentity *peer,
  403. const struct GNUNET_MessageHeader *message)
  404. {
  405. struct Node *n;
  406. struct NodeComCtx *e_ctx;
  407. const struct Experimentation_Request *rm = (const struct Experimentation_Request *) message;
  408. const struct GNUNET_CRYPTO_EddsaPublicKey *rmi = (const struct GNUNET_CRYPTO_EddsaPublicKey *) &rm[1];
  409. unsigned int my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  410. int c1;
  411. int c2;
  412. uint32_t ic;
  413. uint32_t ic_accepted;
  414. int make_active;
  415. if (ntohs (message->size) < sizeof (struct Experimentation_Request))
  416. {
  417. GNUNET_break (0);
  418. return;
  419. }
  420. ic = ntohl (rm->issuer_count);
  421. if (ntohs (message->size) !=
  422. sizeof (struct Experimentation_Request) + ic * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))
  423. {
  424. GNUNET_break (0);
  425. return;
  426. }
  427. make_active = GNUNET_NO;
  428. if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
  429. {
  430. /* Nothing to do */
  431. }
  432. else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
  433. {
  434. GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (nodes_requested, peer, n));
  435. if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
  436. {
  437. GNUNET_SCHEDULER_cancel (n->timeout_task);
  438. n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  439. }
  440. update_stats (nodes_requested);
  441. make_active = GNUNET_YES;
  442. }
  443. else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
  444. {
  445. GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_inactive, peer, n));
  446. update_stats (nodes_inactive);
  447. make_active = GNUNET_YES;
  448. }
  449. else
  450. {
  451. /* Create new node */
  452. n = GNUNET_new (struct Node);
  453. n->id = *peer;
  454. n->capabilities = NONE;
  455. make_active = GNUNET_YES;
  456. }
  457. /* Update node */
  458. n->capabilities = ntohl (rm->capabilities);
  459. /* Filter accepted issuer */
  460. ic_accepted = 0;
  461. for (c1 = 0; c1 < ic; c1++)
  462. {
  463. if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
  464. ic_accepted ++;
  465. }
  466. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  467. "Request from peer `%s' with %u issuers, we accepted %u issuer \n",
  468. GNUNET_i2s (peer), ic, ic_accepted);
  469. GNUNET_free_non_null (n->issuer_id);
  470. n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
  471. c2 = 0;
  472. for (c1 = 0; c1 < ic; c1++)
  473. {
  474. if (GNUNET_YES == GED_experiments_issuer_accepted (&rmi[c1]))
  475. {
  476. n->issuer_id[c2] = rmi[c1];
  477. c2 ++;
  478. }
  479. }
  480. n->issuer_count = ic_accepted;
  481. if (GNUNET_YES == make_active)
  482. node_make_active (n);
  483. /* Send response */
  484. e_ctx = GNUNET_new (struct NodeComCtx);
  485. e_ctx->n = n;
  486. e_ctx->e = NULL;
  487. e_ctx->size = sizeof (struct Experimentation_Response) +
  488. my_issuer_count * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  489. e_ctx->notify = &send_response_cb;
  490. e_ctx->notify_cls = n;
  491. GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
  492. schedule_transmisson (e_ctx);
  493. }
  494. /**
  495. * Handle a response
  496. *
  497. * @param peer the source
  498. * @param message the message
  499. */
  500. static void handle_response (const struct GNUNET_PeerIdentity *peer,
  501. const struct GNUNET_MessageHeader *message)
  502. {
  503. struct Node *n;
  504. const struct Experimentation_Response *rm = (const struct Experimentation_Response *) message;
  505. const struct GNUNET_CRYPTO_EddsaPublicKey *rmi = (const struct GNUNET_CRYPTO_EddsaPublicKey *) &rm[1];
  506. uint32_t ic;
  507. uint32_t ic_accepted;
  508. int make_active;
  509. unsigned int c1;
  510. unsigned int c2;
  511. if (ntohs (message->size) < sizeof (struct Experimentation_Response))
  512. {
  513. GNUNET_break (0);
  514. return;
  515. }
  516. ic = ntohl (rm->issuer_count);
  517. if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))
  518. {
  519. GNUNET_break (0);
  520. return;
  521. }
  522. make_active = GNUNET_NO;
  523. if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
  524. {
  525. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  526. "Received %s from %s peer `%s'\n",
  527. "RESPONSE", "active", GNUNET_i2s (peer));
  528. }
  529. else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
  530. {
  531. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
  532. "RESPONSE", "requested", GNUNET_i2s (peer));
  533. GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (nodes_requested, peer, n));
  534. if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
  535. {
  536. GNUNET_SCHEDULER_cancel (n->timeout_task);
  537. n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  538. }
  539. update_stats (nodes_requested);
  540. make_active = GNUNET_YES;
  541. }
  542. else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
  543. {
  544. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  545. "Received %s from peer `%s'\n",
  546. "RESPONSE", "inactive", GNUNET_i2s (peer));
  547. GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_inactive, peer, n));
  548. update_stats (nodes_inactive);
  549. make_active = GNUNET_YES;
  550. }
  551. else
  552. {
  553. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
  554. "RESPONSE", "unknown", GNUNET_i2s (peer));
  555. return;
  556. }
  557. /* Update */
  558. n->capabilities = ntohl (rm->capabilities);
  559. /* Filter accepted issuer */
  560. ic_accepted = 0;
  561. for (c1 = 0; c1 < ic; c1++)
  562. {
  563. if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
  564. ic_accepted ++;
  565. }
  566. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  567. "Response from peer `%s' with %u issuers, we accepted %u issuer \n",
  568. GNUNET_i2s (peer), ic, ic_accepted);
  569. GNUNET_free_non_null (n->issuer_id);
  570. n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
  571. c2 = 0;
  572. for (c1 = 0; c1 < ic; c1++)
  573. {
  574. if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
  575. {
  576. n->issuer_id[c2] = rmi[c1];
  577. c2 ++;
  578. }
  579. }
  580. n->issuer_count = ic_accepted;
  581. if (GNUNET_YES == make_active)
  582. node_make_active (n);
  583. }
  584. /**
  585. * Handle a response
  586. *
  587. * @param peer the source
  588. * @param message the message
  589. */
  590. static void
  591. handle_start (const struct GNUNET_PeerIdentity *peer,
  592. const struct GNUNET_MessageHeader *message)
  593. {
  594. uint16_t size;
  595. uint32_t name_len;
  596. const struct GED_start_message *msg;
  597. const char *name;
  598. struct Node *n;
  599. struct Experiment *e;
  600. if (NULL == peer)
  601. {
  602. GNUNET_break (0);
  603. return;
  604. }
  605. if (NULL == message)
  606. {
  607. GNUNET_break (0);
  608. return;
  609. }
  610. size = ntohs (message->size);
  611. if (size < sizeof (struct GED_start_message))
  612. {
  613. GNUNET_break (0);
  614. return;
  615. }
  616. msg = (const struct GED_start_message *) message;
  617. name_len = ntohl (msg->len_name);
  618. if (size != sizeof (struct GED_start_message) + name_len)
  619. {
  620. GNUNET_break (0);
  621. return;
  622. }
  623. n = get_node (peer);
  624. if (NULL == n)
  625. {
  626. GNUNET_break (0);
  627. return;
  628. }
  629. name = (const char *) &msg[1];
  630. if (name[name_len-1] != '\0')
  631. {
  632. GNUNET_break (0);
  633. return;
  634. }
  635. if (name_len != strlen (name) + 1)
  636. {
  637. GNUNET_break (0);
  638. return;
  639. }
  640. e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
  641. if (NULL == e)
  642. {
  643. GNUNET_break (0);
  644. return;
  645. }
  646. GED_scheduler_handle_start (n, e);
  647. }
  648. /**
  649. * Handle a response
  650. *
  651. * @param peer the source
  652. * @param message the message
  653. */
  654. static void
  655. handle_start_ack (const struct GNUNET_PeerIdentity *peer,
  656. const struct GNUNET_MessageHeader *message)
  657. {
  658. uint16_t size;
  659. uint32_t name_len;
  660. const struct GED_start_ack_message *msg;
  661. const char *name;
  662. struct Node *n;
  663. struct Experiment *e;
  664. if (NULL == peer)
  665. {
  666. GNUNET_break (0);
  667. return;
  668. }
  669. if (NULL == message)
  670. {
  671. GNUNET_break (0);
  672. return;
  673. }
  674. size = ntohs (message->size);
  675. if (size < sizeof (struct GED_start_ack_message))
  676. {
  677. GNUNET_break (0);
  678. return;
  679. }
  680. msg = (const struct GED_start_ack_message *) message;
  681. name_len = ntohl (msg->len_name);
  682. if (size != sizeof (struct GED_start_message) + name_len)
  683. {
  684. GNUNET_break (0);
  685. return;
  686. }
  687. n = get_node (peer);
  688. if (NULL == n)
  689. {
  690. GNUNET_break (0);
  691. return;
  692. }
  693. name = (const char *) &msg[1];
  694. if (name[name_len-1] != '\0')
  695. {
  696. GNUNET_break (0);
  697. return;
  698. }
  699. if (name_len != strlen (name) + 1)
  700. {
  701. GNUNET_break (0);
  702. return;
  703. }
  704. e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
  705. if (NULL == e)
  706. {
  707. GNUNET_break (0);
  708. return;
  709. }
  710. GED_scheduler_handle_start_ack (n, e);
  711. }
  712. /**
  713. * Handle a response
  714. *
  715. * @param peer the source
  716. * @param message the message
  717. */
  718. static void
  719. handle_stop (const struct GNUNET_PeerIdentity *peer,
  720. const struct GNUNET_MessageHeader *message)
  721. {
  722. uint16_t size;
  723. uint32_t name_len;
  724. const struct GED_stop_message *msg;
  725. const char *name;
  726. struct Node *n;
  727. struct Experiment *e;
  728. if (NULL == peer)
  729. {
  730. GNUNET_break (0);
  731. return;
  732. }
  733. if (NULL == message)
  734. {
  735. GNUNET_break (0);
  736. return;
  737. }
  738. size = ntohs (message->size);
  739. if (size < sizeof (struct GED_stop_message))
  740. {
  741. GNUNET_break (0);
  742. return;
  743. }
  744. msg = (const struct GED_stop_message *) message;
  745. name_len = ntohl (msg->len_name);
  746. if (size != sizeof (struct GED_start_message) + name_len)
  747. {
  748. GNUNET_break (0);
  749. return;
  750. }
  751. n = get_node (peer);
  752. if (NULL == n)
  753. {
  754. GNUNET_break (0);
  755. return;
  756. }
  757. name = (const char *) &msg[1];
  758. if (name[name_len-1] != '\0')
  759. {
  760. GNUNET_break (0);
  761. return;
  762. }
  763. if (name_len != strlen (name) + 1)
  764. {
  765. GNUNET_break (0);
  766. return;
  767. }
  768. e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
  769. if (NULL == e)
  770. {
  771. GNUNET_break (0);
  772. return;
  773. }
  774. GED_scheduler_handle_stop (n, e);
  775. }
  776. /**
  777. * Method called whenever a given peer connects.
  778. *
  779. * @param cls closure
  780. * @param peer peer identity this notification is about
  781. */
  782. static void
  783. core_connect_handler (void *cls,
  784. const struct GNUNET_PeerIdentity *peer)
  785. {
  786. if (GNUNET_YES == is_me(peer))
  787. return;
  788. GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
  789. GNUNET_i2s (peer));
  790. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_requested, peer))
  791. return; /* We already sent a request */
  792. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_active, peer))
  793. return; /* This peer is known as active */
  794. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_inactive, peer))
  795. return; /* This peer is known as inactive */
  796. send_experimentation_request (peer);
  797. }
  798. /**
  799. * Method called whenever a given peer disconnects.
  800. *
  801. * @param cls closure
  802. * @param peer peer identity this notification is about
  803. */
  804. static void
  805. core_disconnect_handler (void *cls,
  806. const struct GNUNET_PeerIdentity * peer)
  807. {
  808. struct Node *n;
  809. if (GNUNET_YES == is_me(peer))
  810. return;
  811. GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
  812. GNUNET_i2s (peer));
  813. if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
  814. cleanup_node (nodes_requested, peer, n);
  815. if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
  816. cleanup_node (nodes_active, peer, n);
  817. if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
  818. cleanup_node (nodes_inactive, peer, n);
  819. }
  820. /**
  821. * Handle a request and send a response
  822. *
  823. * @param cls unused
  824. * @param other the sender
  825. * @param message the message
  826. * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
  827. */
  828. static int
  829. core_receive_handler (void *cls,
  830. const struct GNUNET_PeerIdentity *other,
  831. const struct GNUNET_MessageHeader *message)
  832. {
  833. if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
  834. {
  835. GNUNET_break (0);
  836. return GNUNET_SYSERR;
  837. }
  838. switch (ntohs (message->type)) {
  839. case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
  840. handle_request (other, message);
  841. break;
  842. case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
  843. handle_response (other, message);
  844. break;
  845. case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
  846. handle_start (other, message);
  847. break;
  848. case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
  849. handle_start_ack (other, message);
  850. break;
  851. case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
  852. handle_stop (other, message);
  853. break;
  854. default:
  855. break;
  856. }
  857. return GNUNET_OK;
  858. }
  859. static size_t
  860. node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
  861. {
  862. struct NodeComCtx *e_ctx = cls;
  863. struct GED_start_message *msg;
  864. size_t name_len;
  865. size_t size;
  866. if (NULL == buf)
  867. return 0;
  868. name_len = strlen(e_ctx->e->name) + 1;
  869. size = sizeof (struct GED_start_message) + name_len;
  870. msg = GNUNET_malloc (size);
  871. msg->header.size = htons (size);
  872. msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
  873. msg->issuer = e_ctx->e->issuer;
  874. msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
  875. msg->len_name = htonl (name_len);
  876. memcpy (&msg[1], e_ctx->e->name, name_len);
  877. memcpy (buf, msg, size);
  878. GNUNET_free (msg);
  879. return size;
  880. }
  881. static size_t
  882. node_experiment_start_ack_cb (void *cls, size_t bufsize, void *buf)
  883. {
  884. struct NodeComCtx *e_ctx = cls;
  885. struct GED_start_ack_message *msg;
  886. size_t name_len;
  887. size_t size;
  888. if (NULL == buf)
  889. return 0;
  890. name_len = strlen(e_ctx->e->name) + 1;
  891. size = sizeof (struct GED_start_ack_message) + name_len;
  892. msg = GNUNET_malloc (size);
  893. msg->header.size = htons (size);
  894. msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK);
  895. msg->issuer = e_ctx->e->issuer;
  896. msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
  897. msg->len_name = htonl (name_len);
  898. memcpy (&msg[1], e_ctx->e->name, name_len);
  899. memcpy (buf, msg, size);
  900. GNUNET_free (msg);
  901. return size;
  902. }
  903. /**
  904. * Confirm a experiment START with a node
  905. *
  906. * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
  907. */
  908. int
  909. GED_nodes_send_start_ack (struct Node *n, struct Experiment *e)
  910. {
  911. struct NodeComCtx *e_ctx;
  912. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  913. "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
  914. "START_ACK" ,GNUNET_i2s(&n->id), e->name);
  915. e_ctx = GNUNET_new (struct NodeComCtx);
  916. e_ctx->n = n;
  917. e_ctx->e = e;
  918. e_ctx->size = sizeof (struct GED_start_ack_message) + strlen (e->name) + 1;
  919. e_ctx->notify = &node_experiment_start_ack_cb;
  920. e_ctx->notify_cls = e_ctx;
  921. GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
  922. schedule_transmisson (e_ctx);
  923. return GNUNET_OK;
  924. }
  925. /**
  926. * Request a experiment to start with a node
  927. *
  928. * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
  929. */
  930. int
  931. GED_nodes_send_start (struct Node *n, struct Experiment *e)
  932. {
  933. struct NodeComCtx *e_ctx;
  934. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  935. "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
  936. "START", GNUNET_i2s(&n->id), e->name);
  937. e_ctx = GNUNET_new (struct NodeComCtx);
  938. e_ctx->n = n;
  939. e_ctx->e = e;
  940. e_ctx->size = sizeof (struct GED_start_message) + strlen (e->name) + 1;
  941. e_ctx->notify = &node_experiment_start_cb;
  942. e_ctx->notify_cls = e_ctx;
  943. GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
  944. schedule_transmisson (e_ctx);
  945. return GNUNET_OK;
  946. }
  947. /**
  948. * Start the nodes management
  949. */
  950. void
  951. GED_nodes_start ()
  952. {
  953. /* Connecting to core service to find partners */
  954. ch = GNUNET_CORE_connect (GED_cfg, NULL,
  955. &core_startup_handler,
  956. &core_connect_handler,
  957. &core_disconnect_handler,
  958. &core_receive_handler,
  959. GNUNET_NO, NULL, GNUNET_NO, NULL);
  960. if (NULL == ch)
  961. {
  962. GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
  963. return;
  964. }
  965. nodes_requested = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  966. nodes_active = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  967. nodes_inactive = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  968. }
  969. /**
  970. * Stop the nodes management
  971. */
  972. void
  973. GED_nodes_stop ()
  974. {
  975. if (NULL != ch)
  976. {
  977. GNUNET_CORE_disconnect (ch);
  978. ch = NULL;
  979. }
  980. if (NULL != nodes_requested)
  981. {
  982. GNUNET_CONTAINER_multipeermap_iterate (nodes_requested,
  983. &cleanup_node,
  984. nodes_requested);
  985. update_stats (nodes_requested);
  986. GNUNET_CONTAINER_multipeermap_destroy (nodes_requested);
  987. nodes_requested = NULL;
  988. }
  989. if (NULL != nodes_active)
  990. {
  991. GNUNET_CONTAINER_multipeermap_iterate (nodes_active,
  992. &cleanup_node,
  993. nodes_active);
  994. update_stats (nodes_active);
  995. GNUNET_CONTAINER_multipeermap_destroy (nodes_active);
  996. nodes_active = NULL;
  997. }
  998. if (NULL != nodes_inactive)
  999. {
  1000. GNUNET_CONTAINER_multipeermap_iterate (nodes_inactive,
  1001. &cleanup_node,
  1002. nodes_inactive);
  1003. update_stats (nodes_inactive);
  1004. GNUNET_CONTAINER_multipeermap_destroy (nodes_inactive);
  1005. nodes_inactive = NULL;
  1006. }
  1007. }
  1008. /* end of gnunet-daemon-experimentation_nodes.c */