gnunet-service-peerinfo.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file peerinfo/gnunet-service-peerinfo.c
  18. * @brief maintains list of known peers
  19. *
  20. * Code to maintain the list of currently known hosts (in memory
  21. * structure of data/hosts/).
  22. *
  23. * @author Christian Grothoff
  24. */
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_hello_lib.h"
  28. #include "gnunet_protocols.h"
  29. #include "gnunet_statistics_service.h"
  30. #include "peerinfo.h"
  31. /**
  32. * How often do we scan the HOST_DIR for new entries?
  33. */
  34. #define DATA_HOST_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  35. /**
  36. * How often do we discard old entries in data/hosts/?
  37. */
  38. #define DATA_HOST_CLEAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 60)
  39. /**
  40. * In-memory cache of known hosts.
  41. */
  42. struct HostEntry
  43. {
  44. /**
  45. * Identity of the peer.
  46. */
  47. struct GNUNET_PeerIdentity identity;
  48. /**
  49. * Hello for the peer (can be NULL)
  50. */
  51. struct GNUNET_HELLO_Message *hello;
  52. /**
  53. * Friend only hello for the peer (can be NULL)
  54. */
  55. struct GNUNET_HELLO_Message *friend_only_hello;
  56. };
  57. /**
  58. * Result of reading a file
  59. */
  60. struct ReadHostFileContext
  61. {
  62. /**
  63. * Hello for the peer (can be NULL)
  64. */
  65. struct GNUNET_HELLO_Message *hello;
  66. /**
  67. * Friend only hello for the peer (can be NULL)
  68. */
  69. struct GNUNET_HELLO_Message *friend_only_hello;
  70. };
  71. /**
  72. * The in-memory list of known hosts, mapping of
  73. * host IDs to 'struct HostEntry*' values.
  74. */
  75. static struct GNUNET_CONTAINER_MultiPeerMap *hostmap;
  76. /**
  77. * Clients to immediately notify about all changes.
  78. */
  79. static struct GNUNET_NotificationContext *notify_list;
  80. /**
  81. * Clients to immediately notify about all changes,
  82. * even for friend-only HELLOs.
  83. */
  84. static struct GNUNET_NotificationContext *notify_friend_only_list;
  85. /**
  86. * Directory where the hellos are stored in (peerinfo/)
  87. */
  88. static char *networkIdDirectory;
  89. /**
  90. * Handle for reporting statistics.
  91. */
  92. static struct GNUNET_STATISTICS_Handle *stats;
  93. /**
  94. * Handle for task to run #cron_clean_data_hosts()
  95. */
  96. static struct GNUNET_SCHEDULER_Task *cron_clean;
  97. /**
  98. * Handle for task to run #cron_scan_directory_data_hosts()
  99. */
  100. static struct GNUNET_SCHEDULER_Task *cron_scan;
  101. /**
  102. * Notify all clients in the notify list about the
  103. * given host entry changing.
  104. *
  105. * @param he entry of the host for which we generate a notification
  106. * @param include_friend_only create public of friend-only message
  107. * @return generated notification message
  108. */
  109. static struct InfoMessage *
  110. make_info_message (const struct HostEntry *he,
  111. int include_friend_only)
  112. {
  113. struct InfoMessage *im;
  114. struct GNUNET_HELLO_Message *src;
  115. size_t hs;
  116. if (GNUNET_YES == include_friend_only)
  117. src = he->friend_only_hello;
  118. else
  119. src = he->hello;
  120. hs = (NULL == src) ? 0 : GNUNET_HELLO_size (src);
  121. im = GNUNET_malloc (sizeof (struct InfoMessage) + hs);
  122. im->header.size = htons (hs + sizeof (struct InfoMessage));
  123. im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  124. im->peer = he->identity;
  125. GNUNET_memcpy (&im[1],
  126. src,
  127. hs);
  128. return im;
  129. }
  130. /**
  131. * Address iterator that causes expired entries to be discarded.
  132. *
  133. * @param cls pointer to the current time
  134. * @param address the address
  135. * @param expiration expiration time for the address
  136. * @return #GNUNET_NO if expiration smaller than the current time
  137. */
  138. static int
  139. discard_expired (void *cls,
  140. const struct GNUNET_HELLO_Address *address,
  141. struct GNUNET_TIME_Absolute expiration)
  142. {
  143. const struct GNUNET_TIME_Absolute *now = cls;
  144. if (now->abs_value_us > expiration.abs_value_us)
  145. {
  146. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  147. _("Removing expired address of transport `%s'\n"),
  148. address->transport_name);
  149. return GNUNET_NO;
  150. }
  151. return GNUNET_OK;
  152. }
  153. /**
  154. * Address iterator that counts the remaining addresses.
  155. *
  156. * @param cls pointer to the counter
  157. * @param address the address
  158. * @param expiration expiration time for the address
  159. * @return #GNUNET_OK (always)
  160. */
  161. static int
  162. count_addresses (void *cls,
  163. const struct GNUNET_HELLO_Address *address,
  164. struct GNUNET_TIME_Absolute expiration)
  165. {
  166. unsigned int *cnt = cls;
  167. (void) address;
  168. (void) expiration;
  169. (*cnt)++;
  170. return GNUNET_OK;
  171. }
  172. /**
  173. * Get the filename under which we would store the GNUNET_HELLO_Message
  174. * for the given host and protocol.
  175. *
  176. * @param id peer for which we need the filename for the HELLO
  177. * @return filename of the form DIRECTORY/HOSTID
  178. */
  179. static char *
  180. get_host_filename (const struct GNUNET_PeerIdentity *id)
  181. {
  182. char *fn;
  183. if (NULL == networkIdDirectory)
  184. return NULL;
  185. GNUNET_asprintf (&fn,
  186. "%s%s%s",
  187. networkIdDirectory,
  188. DIR_SEPARATOR_STR,
  189. GNUNET_i2s_full (id));
  190. return fn;
  191. }
  192. /**
  193. * Broadcast information about the given entry to all
  194. * clients that care.
  195. *
  196. * @param entry entry to broadcast about
  197. */
  198. static void
  199. notify_all (struct HostEntry *entry)
  200. {
  201. struct InfoMessage *msg_pub;
  202. struct InfoMessage *msg_friend;
  203. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  204. "Notifying all clients about peer `%s'\n",
  205. GNUNET_i2s(&entry->identity));
  206. msg_pub = make_info_message (entry,
  207. GNUNET_NO);
  208. GNUNET_notification_context_broadcast (notify_list,
  209. &msg_pub->header,
  210. GNUNET_NO);
  211. GNUNET_free (msg_pub);
  212. msg_friend = make_info_message (entry,
  213. GNUNET_YES);
  214. GNUNET_notification_context_broadcast (notify_friend_only_list,
  215. &msg_friend->header,
  216. GNUNET_NO);
  217. GNUNET_free (msg_friend);
  218. }
  219. /**
  220. * Bind a host address (hello) to a hostId.
  221. *
  222. * @param peer the peer for which this is a hello
  223. * @param hello the verified (!) hello message
  224. */
  225. static void
  226. update_hello (const struct GNUNET_PeerIdentity *peer,
  227. const struct GNUNET_HELLO_Message *hello);
  228. /**
  229. * Try to read the HELLOs in the given filename and discard expired
  230. * addresses. Removes the file if one the HELLO is malformed. If all
  231. * addresses are expired, the HELLO is also removed (but the HELLO
  232. * with the public key is still returned if it was found and valid).
  233. * The file can contain multiple HELLO messages.
  234. *
  235. * @param fn name of the file
  236. * @param unlink_garbage if #GNUNET_YES, try to remove useless files
  237. * @param r ReadHostFileContext to store the resutl
  238. */
  239. static void
  240. read_host_file (const char *fn,
  241. int unlink_garbage,
  242. struct ReadHostFileContext *r)
  243. {
  244. char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
  245. ssize_t size_total;
  246. struct GNUNET_TIME_Absolute now;
  247. unsigned int left;
  248. const struct GNUNET_HELLO_Message *hello;
  249. struct GNUNET_HELLO_Message *hello_clean;
  250. size_t read_pos;
  251. uint16_t size_hello;
  252. r->friend_only_hello = NULL;
  253. r->hello = NULL;
  254. if (GNUNET_YES != GNUNET_DISK_file_test (fn))
  255. return;
  256. size_total = GNUNET_DISK_fn_read (fn,
  257. buffer,
  258. sizeof (buffer));
  259. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  260. "Read %d bytes from `%s'\n",
  261. (int) size_total,
  262. fn);
  263. if ( (size_total < 0) ||
  264. (((size_t) size_total) < sizeof (struct GNUNET_MessageHeader)) )
  265. {
  266. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  267. _("Failed to parse HELLO in file `%s': %s\n"),
  268. fn,
  269. "File has invalid size");
  270. if ( (GNUNET_YES == unlink_garbage) &&
  271. (0 != UNLINK (fn)) &&
  272. (ENOENT != errno) )
  273. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  274. "unlink",
  275. fn);
  276. return;
  277. }
  278. read_pos = 0;
  279. while (read_pos < (size_t) size_total)
  280. {
  281. hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
  282. size_hello = GNUNET_HELLO_size (hello);
  283. if ( (0 == size_hello) ||
  284. (((size_t) size_total) - read_pos < size_hello) )
  285. {
  286. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  287. _("Failed to parse HELLO in file `%s'\n"),
  288. fn);
  289. if (0 == read_pos)
  290. {
  291. if ((GNUNET_YES == unlink_garbage) &&
  292. (0 != UNLINK (fn)) &&
  293. (ENOENT != errno) )
  294. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  295. "unlink",
  296. fn);
  297. }
  298. else
  299. {
  300. if ( (GNUNET_YES == unlink_garbage) &&
  301. (0 != TRUNCATE (fn, read_pos)) &&
  302. (ENOENT != errno) )
  303. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  304. "truncate",
  305. fn);
  306. }
  307. return;
  308. }
  309. now = GNUNET_TIME_absolute_get ();
  310. hello_clean = GNUNET_HELLO_iterate_addresses (hello,
  311. GNUNET_YES,
  312. &discard_expired,
  313. &now);
  314. if (NULL == hello_clean)
  315. {
  316. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  317. _("Failed to parse HELLO in file `%s'\n"),
  318. fn);
  319. if ((GNUNET_YES == unlink_garbage) &&
  320. (0 != UNLINK (fn)) &&
  321. (ENOENT != errno) )
  322. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  323. "unlink",
  324. fn);
  325. return;
  326. }
  327. left = 0;
  328. (void) GNUNET_HELLO_iterate_addresses (hello_clean,
  329. GNUNET_NO,
  330. &count_addresses,
  331. &left);
  332. if (0 == left)
  333. {
  334. GNUNET_free (hello_clean);
  335. break;
  336. }
  337. if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean))
  338. {
  339. if (NULL == r->hello)
  340. r->hello = hello_clean;
  341. else
  342. {
  343. GNUNET_break (0);
  344. GNUNET_free (r->hello);
  345. r->hello = hello_clean;
  346. }
  347. }
  348. else
  349. {
  350. if (NULL == r->friend_only_hello)
  351. r->friend_only_hello = hello_clean;
  352. else
  353. {
  354. GNUNET_break (0);
  355. GNUNET_free (r->friend_only_hello);
  356. r->friend_only_hello = hello_clean;
  357. }
  358. }
  359. read_pos += size_hello;
  360. }
  361. if (0 == left)
  362. {
  363. /* no addresses left, remove from disk */
  364. if ( (GNUNET_YES == unlink_garbage) &&
  365. (0 != UNLINK (fn)) )
  366. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  367. "unlink",
  368. fn);
  369. }
  370. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  371. "Found `%s' and `%s' HELLO message in file\n",
  372. (NULL != r->hello) ? "public" : "NON-public",
  373. (NULL != r->friend_only_hello) ? "friend only" : "NO friend only");
  374. }
  375. /**
  376. * Add a host to the list and notify clients about this event
  377. *
  378. * @param identity the identity of the host
  379. * @return the HostEntry
  380. */
  381. static struct HostEntry *
  382. add_host_to_known_hosts (const struct GNUNET_PeerIdentity *identity)
  383. {
  384. struct HostEntry *entry;
  385. struct ReadHostFileContext r;
  386. char *fn;
  387. entry = GNUNET_CONTAINER_multipeermap_get (hostmap,
  388. identity);
  389. if (NULL == entry)
  390. {
  391. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  392. "Adding new peer `%s'\n",
  393. GNUNET_i2s (identity));
  394. GNUNET_STATISTICS_update (stats,
  395. gettext_noop ("# peers known"),
  396. 1,
  397. GNUNET_NO);
  398. entry = GNUNET_new (struct HostEntry);
  399. entry->identity = *identity;
  400. GNUNET_assert (GNUNET_OK ==
  401. GNUNET_CONTAINER_multipeermap_put (hostmap,
  402. &entry->identity,
  403. entry,
  404. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  405. notify_all (entry);
  406. fn = get_host_filename (identity);
  407. if (NULL != fn)
  408. {
  409. read_host_file (fn,
  410. GNUNET_YES,
  411. &r);
  412. if (NULL != r.hello)
  413. update_hello (identity,
  414. r.hello);
  415. if (NULL != r.friend_only_hello)
  416. update_hello (identity,
  417. r.friend_only_hello);
  418. GNUNET_free_non_null (r.hello);
  419. GNUNET_free_non_null (r.friend_only_hello);
  420. GNUNET_free (fn);
  421. }
  422. }
  423. return entry;
  424. }
  425. /**
  426. * Remove a file that should not be there. LOG
  427. * success or failure.
  428. *
  429. * @param fullname name of the file to remove
  430. */
  431. static void
  432. remove_garbage (const char *fullname)
  433. {
  434. if (0 == UNLINK (fullname))
  435. GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  436. _("File `%s' in directory `%s' does not match naming convention. Removed.\n"),
  437. fullname,
  438. networkIdDirectory);
  439. else
  440. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  441. "unlink",
  442. fullname);
  443. }
  444. /**
  445. * Closure for #hosts_directory_scan_callback().
  446. */
  447. struct DirScanContext
  448. {
  449. /**
  450. * #GNUNET_YES if we should remove files that are broken,
  451. * #GNUNET_NO if the directory we are iterating over should
  452. * be treated as read-only by us.
  453. */
  454. int remove_files;
  455. /**
  456. * Counter for the number of (valid) entries found, incremented
  457. * by one for each match.
  458. */
  459. unsigned int matched;
  460. };
  461. /**
  462. * Function that is called on each HELLO file in a particular directory.
  463. * Try to parse the file and add the HELLO to our list.
  464. *
  465. * @param cls pointer to 'unsigned int' to increment for each file, or NULL
  466. * if the file is from a read-only, read-once resource directory
  467. * @param fullname name of the file to parse
  468. * @return #GNUNET_OK (continue iteration)
  469. */
  470. static int
  471. hosts_directory_scan_callback (void *cls,
  472. const char *fullname)
  473. {
  474. struct DirScanContext *dsc = cls;
  475. struct GNUNET_PeerIdentity identity;
  476. struct ReadHostFileContext r;
  477. const char *filename;
  478. struct GNUNET_PeerIdentity id_public;
  479. struct GNUNET_PeerIdentity id_friend;
  480. struct GNUNET_PeerIdentity id;
  481. if (GNUNET_YES != GNUNET_DISK_file_test (fullname))
  482. return GNUNET_OK; /* ignore non-files */
  483. filename = strrchr (fullname,
  484. DIR_SEPARATOR);
  485. if ( (NULL == filename) ||
  486. (1 > strlen (filename)) )
  487. filename = fullname;
  488. else
  489. filename ++;
  490. read_host_file (fullname,
  491. dsc->remove_files,
  492. &r);
  493. if ( (NULL == r.hello) &&
  494. (NULL == r.friend_only_hello))
  495. return GNUNET_OK;
  496. if (NULL != r.friend_only_hello)
  497. {
  498. if (GNUNET_OK !=
  499. GNUNET_HELLO_get_id (r.friend_only_hello,
  500. &id_friend))
  501. {
  502. if (GNUNET_YES == dsc->remove_files)
  503. remove_garbage (fullname);
  504. return GNUNET_OK;
  505. }
  506. id = id_friend;
  507. }
  508. if (NULL != r.hello)
  509. {
  510. if (GNUNET_OK !=
  511. GNUNET_HELLO_get_id (r.hello,
  512. &id_public))
  513. {
  514. if (GNUNET_YES == dsc->remove_files)
  515. remove_garbage (fullname);
  516. return GNUNET_OK;
  517. }
  518. id = id_public;
  519. }
  520. if ( (NULL != r.hello) &&
  521. (NULL != r.friend_only_hello) &&
  522. (0 != GNUNET_memcmp (&id_friend,
  523. &id_public)) )
  524. {
  525. /* HELLOs are not for the same peer */
  526. GNUNET_break (0);
  527. if (GNUNET_YES == dsc->remove_files)
  528. remove_garbage (fullname);
  529. return GNUNET_OK;
  530. }
  531. if (GNUNET_OK ==
  532. GNUNET_CRYPTO_eddsa_public_key_from_string (filename,
  533. strlen (filename),
  534. &identity.public_key))
  535. {
  536. if (0 != GNUNET_memcmp (&id, &identity))
  537. {
  538. /* HELLOs are not for the same peer */
  539. GNUNET_break (0);
  540. if (GNUNET_YES == dsc->remove_files)
  541. remove_garbage (fullname);
  542. return GNUNET_OK;
  543. }
  544. }
  545. /* ok, found something valid, remember HELLO */
  546. add_host_to_known_hosts (&id);
  547. if (NULL != r.hello)
  548. {
  549. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  550. "Updating peer `%s' public HELLO \n",
  551. GNUNET_i2s (&id));
  552. update_hello (&id, r.hello);
  553. GNUNET_free (r.hello);
  554. }
  555. if (NULL != r.friend_only_hello)
  556. {
  557. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  558. "Updating peer `%s' friend only HELLO \n",
  559. GNUNET_i2s (&id));
  560. update_hello (&id, r.friend_only_hello);
  561. GNUNET_free (r.friend_only_hello);
  562. }
  563. dsc->matched++;
  564. return GNUNET_OK;
  565. }
  566. /**
  567. * Call this method periodically to scan data/hosts for new hosts.
  568. *
  569. * @param cls unused
  570. */
  571. static void
  572. cron_scan_directory_data_hosts (void *cls)
  573. {
  574. static unsigned int retries;
  575. struct DirScanContext dsc;
  576. (void) cls;
  577. cron_scan = NULL;
  578. if (GNUNET_SYSERR ==
  579. GNUNET_DISK_directory_create (networkIdDirectory))
  580. {
  581. cron_scan = GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
  582. GNUNET_SCHEDULER_PRIORITY_IDLE,
  583. &cron_scan_directory_data_hosts, NULL);
  584. return;
  585. }
  586. dsc.matched = 0;
  587. dsc.remove_files = GNUNET_YES;
  588. GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
  589. _("Scanning directory `%s'\n"),
  590. networkIdDirectory);
  591. GNUNET_DISK_directory_scan (networkIdDirectory,
  592. &hosts_directory_scan_callback,
  593. &dsc);
  594. if ( (0 == dsc.matched) &&
  595. (0 == (++retries & 31)) )
  596. GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  597. _("Still no peers found in `%s'!\n"),
  598. networkIdDirectory);
  599. cron_scan = GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
  600. GNUNET_SCHEDULER_PRIORITY_IDLE,
  601. &cron_scan_directory_data_hosts,
  602. NULL);
  603. }
  604. /**
  605. * Update the HELLO of a friend by merging the addresses.
  606. *
  607. * @param hello original hello
  608. * @param friend_hello hello with additional addresses
  609. * @return merged HELLO
  610. */
  611. static struct GNUNET_HELLO_Message *
  612. update_friend_hello (const struct GNUNET_HELLO_Message *hello,
  613. const struct GNUNET_HELLO_Message *friend_hello)
  614. {
  615. struct GNUNET_HELLO_Message * res;
  616. struct GNUNET_HELLO_Message * tmp;
  617. struct GNUNET_PeerIdentity pid;
  618. if (NULL != friend_hello)
  619. {
  620. res = GNUNET_HELLO_merge (hello,
  621. friend_hello);
  622. GNUNET_assert (GNUNET_YES ==
  623. GNUNET_HELLO_is_friend_only (res));
  624. return res;
  625. }
  626. if (GNUNET_OK !=
  627. GNUNET_HELLO_get_id (hello, &pid))
  628. {
  629. GNUNET_break (0);
  630. return NULL;
  631. }
  632. tmp = GNUNET_HELLO_create (&pid.public_key,
  633. NULL,
  634. NULL,
  635. GNUNET_YES);
  636. res = GNUNET_HELLO_merge (hello, tmp);
  637. GNUNET_free (tmp);
  638. GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
  639. return res;
  640. }
  641. /**
  642. * Bind a host address (hello) to a hostId.
  643. *
  644. * @param peer the peer for which this is a hello
  645. * @param hello the verified (!) hello message
  646. */
  647. static void
  648. update_hello (const struct GNUNET_PeerIdentity *peer,
  649. const struct GNUNET_HELLO_Message *hello)
  650. {
  651. char *fn;
  652. struct HostEntry *host;
  653. struct GNUNET_HELLO_Message *mrg;
  654. struct GNUNET_HELLO_Message **dest;
  655. struct GNUNET_TIME_Absolute delta;
  656. unsigned int cnt;
  657. unsigned int size;
  658. int friend_hello_type;
  659. int store_hello;
  660. int store_friend_hello;
  661. unsigned int pos;
  662. char *buffer;
  663. host = GNUNET_CONTAINER_multipeermap_get (hostmap, peer);
  664. GNUNET_assert (NULL != host);
  665. friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
  666. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  667. "Updating %s HELLO for `%s'\n",
  668. (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
  669. GNUNET_i2s (peer));
  670. dest = NULL;
  671. if (GNUNET_YES == friend_hello_type)
  672. {
  673. dest = &host->friend_only_hello;
  674. }
  675. else
  676. {
  677. dest = &host->hello;
  678. }
  679. if (NULL == (*dest))
  680. {
  681. (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
  682. GNUNET_memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
  683. }
  684. else
  685. {
  686. mrg = GNUNET_HELLO_merge ((*dest),
  687. hello);
  688. delta = GNUNET_HELLO_equals (mrg,
  689. (*dest),
  690. GNUNET_TIME_absolute_get ());
  691. if (delta.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
  692. {
  693. /* no differences, just ignore the update */
  694. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  695. "No change in %s HELLO for `%s'\n",
  696. (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
  697. GNUNET_i2s (peer));
  698. GNUNET_free (mrg);
  699. return;
  700. }
  701. GNUNET_free ((*dest));
  702. (*dest) = mrg;
  703. }
  704. if ( (NULL != (host->hello)) &&
  705. (GNUNET_NO == friend_hello_type) )
  706. {
  707. /* Update friend only hello */
  708. mrg = update_friend_hello (host->hello,
  709. host->friend_only_hello);
  710. if (NULL != host->friend_only_hello)
  711. GNUNET_free (host->friend_only_hello);
  712. host->friend_only_hello = mrg;
  713. }
  714. if (NULL != host->hello)
  715. GNUNET_assert ((GNUNET_NO ==
  716. GNUNET_HELLO_is_friend_only (host->hello)));
  717. if (NULL != host->friend_only_hello)
  718. GNUNET_assert ((GNUNET_YES ==
  719. GNUNET_HELLO_is_friend_only (host->friend_only_hello)));
  720. fn = get_host_filename (peer);
  721. if ( (NULL != fn) &&
  722. (GNUNET_OK ==
  723. GNUNET_DISK_directory_create_for_file (fn)) )
  724. {
  725. store_hello = GNUNET_NO;
  726. size = 0;
  727. cnt = 0;
  728. if (NULL != host->hello)
  729. (void) GNUNET_HELLO_iterate_addresses (host->hello,
  730. GNUNET_NO,
  731. &count_addresses,
  732. &cnt);
  733. if (cnt > 0)
  734. {
  735. store_hello = GNUNET_YES;
  736. size += GNUNET_HELLO_size (host->hello);
  737. }
  738. cnt = 0;
  739. if (NULL != host->friend_only_hello)
  740. (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello,
  741. GNUNET_NO,
  742. &count_addresses,
  743. &cnt);
  744. store_friend_hello = GNUNET_NO;
  745. if (0 < cnt)
  746. {
  747. store_friend_hello = GNUNET_YES;
  748. size += GNUNET_HELLO_size (host->friend_only_hello);
  749. }
  750. if ( (GNUNET_NO == store_hello) &&
  751. (GNUNET_NO == store_friend_hello) )
  752. {
  753. /* no valid addresses, don't put HELLO on disk; in fact,
  754. if one exists on disk, remove it */
  755. (void) UNLINK (fn);
  756. }
  757. else
  758. {
  759. buffer = GNUNET_malloc (size);
  760. pos = 0;
  761. if (GNUNET_YES == store_hello)
  762. {
  763. GNUNET_memcpy (buffer,
  764. host->hello,
  765. GNUNET_HELLO_size (host->hello));
  766. pos += GNUNET_HELLO_size (host->hello);
  767. }
  768. if (GNUNET_YES == store_friend_hello)
  769. {
  770. GNUNET_memcpy (&buffer[pos],
  771. host->friend_only_hello,
  772. GNUNET_HELLO_size (host->friend_only_hello));
  773. pos += GNUNET_HELLO_size (host->friend_only_hello);
  774. }
  775. GNUNET_assert (pos == size);
  776. if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
  777. GNUNET_DISK_PERM_USER_READ |
  778. GNUNET_DISK_PERM_USER_WRITE |
  779. GNUNET_DISK_PERM_GROUP_READ |
  780. GNUNET_DISK_PERM_OTHER_READ))
  781. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
  782. else
  783. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  784. "Stored %s %s HELLO in %s with total size %u\n",
  785. (GNUNET_YES == store_friend_hello) ? "friend-only": "",
  786. (GNUNET_YES == store_hello) ? "public": "",
  787. fn,
  788. size);
  789. GNUNET_free (buffer);
  790. }
  791. }
  792. GNUNET_free_non_null (fn);
  793. notify_all (host);
  794. }
  795. /**
  796. * Closure for #add_to_tc()
  797. */
  798. struct TransmitContext
  799. {
  800. /**
  801. * Client to transmit to
  802. */
  803. struct GNUNET_SERVICE_Client *client;
  804. /**
  805. * Include friend only HELLOs #GNUNET_YES or #GNUNET_NO
  806. */
  807. int friend_only;
  808. };
  809. /**
  810. * Do transmit info about peer to given host.
  811. *
  812. * @param cls NULL to hit all hosts, otherwise specifies a particular target
  813. * @param key hostID
  814. * @param value information to transmit
  815. * @return #GNUNET_YES (continue to iterate)
  816. */
  817. static int
  818. add_to_tc (void *cls,
  819. const struct GNUNET_PeerIdentity *key,
  820. void *value)
  821. {
  822. struct TransmitContext *tc = cls;
  823. struct HostEntry *pos = value;
  824. struct InfoMessage *im;
  825. uint16_t hs;
  826. struct GNUNET_MQ_Envelope *env;
  827. hs = 0;
  828. if ( (NULL != pos->hello) &&
  829. (GNUNET_NO == tc->friend_only) )
  830. {
  831. /* Copy public HELLO */
  832. hs = GNUNET_HELLO_size (pos->hello);
  833. GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE -
  834. sizeof (struct InfoMessage));
  835. env = GNUNET_MQ_msg_extra (im,
  836. hs,
  837. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  838. GNUNET_memcpy (&im[1],
  839. pos->hello,
  840. hs);
  841. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  842. "Sending public HELLO with size %u for peer `%s'\n",
  843. hs,
  844. GNUNET_i2s (key));
  845. }
  846. else if ( (NULL != pos->friend_only_hello) &&
  847. (GNUNET_YES == tc->friend_only) )
  848. {
  849. /* Copy friend only HELLO */
  850. hs = GNUNET_HELLO_size (pos->friend_only_hello);
  851. GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE -
  852. sizeof (struct InfoMessage));
  853. env = GNUNET_MQ_msg_extra (im,
  854. hs,
  855. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  856. GNUNET_memcpy (&im[1],
  857. pos->friend_only_hello,
  858. hs);
  859. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  860. "Sending friend-only HELLO with size %u for peer `%s'\n",
  861. hs,
  862. GNUNET_i2s (key));
  863. }
  864. else
  865. {
  866. env = GNUNET_MQ_msg (im,
  867. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  868. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  869. "Adding no HELLO for peer `%s'\n",
  870. GNUNET_i2s (key));
  871. }
  872. im->peer = pos->identity;
  873. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (tc->client),
  874. env);
  875. return GNUNET_YES;
  876. }
  877. /**
  878. * @brief delete expired HELLO entries in directory
  879. *
  880. * @param cls pointer to current time (`struct GNUNET_TIME_Absolute *`)
  881. * @param fn filename to test to see if the HELLO expired
  882. * @return #GNUNET_OK (continue iteration)
  883. */
  884. static int
  885. discard_hosts_helper (void *cls,
  886. const char *fn)
  887. {
  888. struct GNUNET_TIME_Absolute *now = cls;
  889. char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
  890. const struct GNUNET_HELLO_Message *hello;
  891. struct GNUNET_HELLO_Message *new_hello;
  892. int read_size;
  893. unsigned int cur_hello_size;
  894. unsigned int new_hello_size;
  895. int read_pos;
  896. int write_pos;
  897. unsigned int cnt;
  898. char *writebuffer;
  899. uint64_t fsize;
  900. if (GNUNET_OK !=
  901. GNUNET_DISK_file_size (fn,
  902. &fsize,
  903. GNUNET_YES,
  904. GNUNET_YES))
  905. {
  906. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  907. GNUNET_ERROR_TYPE_BULK,
  908. "fstat",
  909. fn);
  910. return GNUNET_OK;
  911. }
  912. read_size = GNUNET_DISK_fn_read (fn,
  913. buffer,
  914. sizeof (buffer));
  915. if ( (read_size < (int) sizeof (struct GNUNET_MessageHeader)) ||
  916. (fsize > GNUNET_MAX_MESSAGE_SIZE) )
  917. {
  918. if (0 != UNLINK (fn))
  919. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  920. GNUNET_ERROR_TYPE_BULK,
  921. "unlink",
  922. fn);
  923. return GNUNET_OK;
  924. }
  925. writebuffer = GNUNET_malloc (read_size);
  926. read_pos = 0;
  927. write_pos = 0;
  928. while (read_pos < read_size)
  929. {
  930. /* Check each HELLO */
  931. hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
  932. cur_hello_size = GNUNET_HELLO_size (hello);
  933. if (0 == cur_hello_size)
  934. {
  935. /* Invalid data, discard */
  936. if (0 != UNLINK (fn))
  937. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  938. GNUNET_ERROR_TYPE_BULK,
  939. "unlink",
  940. fn);
  941. GNUNET_free (writebuffer);
  942. return GNUNET_OK;
  943. }
  944. new_hello = GNUNET_HELLO_iterate_addresses (hello,
  945. GNUNET_YES,
  946. &discard_expired,
  947. now);
  948. cnt = 0;
  949. if (NULL != new_hello)
  950. (void) GNUNET_HELLO_iterate_addresses (hello,
  951. GNUNET_NO,
  952. &count_addresses,
  953. &cnt);
  954. if ( (NULL != new_hello) && (0 < cnt) )
  955. {
  956. /* Store new HELLO to write it when done */
  957. new_hello_size = GNUNET_HELLO_size (new_hello);
  958. GNUNET_memcpy (&writebuffer[write_pos],
  959. new_hello,
  960. new_hello_size);
  961. write_pos += new_hello_size;
  962. }
  963. read_pos += cur_hello_size;
  964. GNUNET_free_non_null (new_hello);
  965. }
  966. if (0 < write_pos)
  967. {
  968. GNUNET_DISK_fn_write (fn,
  969. writebuffer,
  970. write_pos,
  971. GNUNET_DISK_PERM_USER_READ |
  972. GNUNET_DISK_PERM_USER_WRITE |
  973. GNUNET_DISK_PERM_GROUP_READ |
  974. GNUNET_DISK_PERM_OTHER_READ);
  975. }
  976. else if (0 != UNLINK (fn))
  977. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  978. GNUNET_ERROR_TYPE_BULK,
  979. "unlink", fn);
  980. GNUNET_free (writebuffer);
  981. return GNUNET_OK;
  982. }
  983. /**
  984. * Call this method periodically to scan peerinfo/ for ancient
  985. * HELLOs to expire.
  986. *
  987. * @param cls unused
  988. */
  989. static void
  990. cron_clean_data_hosts (void *cls)
  991. {
  992. struct GNUNET_TIME_Absolute now;
  993. (void) cls;
  994. cron_clean = NULL;
  995. now = GNUNET_TIME_absolute_get ();
  996. GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
  997. _("Cleaning up directory `%s'\n"),
  998. networkIdDirectory);
  999. GNUNET_DISK_directory_scan (networkIdDirectory,
  1000. &discard_hosts_helper,
  1001. &now);
  1002. cron_clean = GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ,
  1003. &cron_clean_data_hosts,
  1004. NULL);
  1005. }
  1006. /**
  1007. * Check HELLO-message.
  1008. *
  1009. * @param cls identification of the client
  1010. * @param hello the actual message
  1011. * @return #GNUNET_OK if @a hello is well-formed
  1012. */
  1013. static int
  1014. check_hello (void *cls,
  1015. const struct GNUNET_HELLO_Message *hello)
  1016. {
  1017. struct GNUNET_PeerIdentity pid;
  1018. (void) cls;
  1019. if (GNUNET_OK !=
  1020. GNUNET_HELLO_get_id (hello,
  1021. &pid))
  1022. {
  1023. GNUNET_break (0);
  1024. return GNUNET_SYSERR;
  1025. }
  1026. return GNUNET_OK;
  1027. }
  1028. /**
  1029. * Handle HELLO-message.
  1030. *
  1031. * @param cls identification of the client
  1032. * @param hello the actual message
  1033. */
  1034. static void
  1035. handle_hello (void *cls,
  1036. const struct GNUNET_HELLO_Message *hello)
  1037. {
  1038. struct GNUNET_SERVICE_Client *client = cls;
  1039. struct GNUNET_PeerIdentity pid;
  1040. GNUNET_assert (GNUNET_OK ==
  1041. GNUNET_HELLO_get_id (hello,
  1042. &pid));
  1043. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1044. "HELLO message received for peer `%s'\n",
  1045. GNUNET_i2s (&pid));
  1046. add_host_to_known_hosts (&pid);
  1047. update_hello (&pid,
  1048. hello);
  1049. GNUNET_SERVICE_client_continue (client);
  1050. }
  1051. /**
  1052. * Handle GET-message.
  1053. *
  1054. * @param cls identification of the client
  1055. * @param lpm the actual message
  1056. */
  1057. static void
  1058. handle_get (void *cls,
  1059. const struct ListPeerMessage *lpm)
  1060. {
  1061. struct GNUNET_SERVICE_Client *client = cls;
  1062. struct TransmitContext tcx;
  1063. struct GNUNET_MessageHeader *msg;
  1064. struct GNUNET_MQ_Envelope *env;
  1065. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1066. "GET message received for peer `%s'\n",
  1067. GNUNET_i2s (&lpm->peer));
  1068. tcx.friend_only = ntohl (lpm->include_friend_only);
  1069. tcx.client = client;
  1070. GNUNET_CONTAINER_multipeermap_get_multiple (hostmap,
  1071. &lpm->peer,
  1072. &add_to_tc,
  1073. &tcx);
  1074. env = GNUNET_MQ_msg (msg,
  1075. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1076. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1077. env);
  1078. GNUNET_SERVICE_client_continue (client);
  1079. }
  1080. /**
  1081. * Handle GET-ALL-message.
  1082. *
  1083. * @param cls identification of the client
  1084. * @param lapm the actual message
  1085. */
  1086. static void
  1087. handle_get_all (void *cls,
  1088. const struct ListAllPeersMessage *lapm)
  1089. {
  1090. struct GNUNET_SERVICE_Client *client = cls;
  1091. struct TransmitContext tcx;
  1092. struct GNUNET_MQ_Envelope *env;
  1093. struct GNUNET_MessageHeader *msg;
  1094. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1095. "GET_ALL message received\n");
  1096. tcx.friend_only = ntohl (lapm->include_friend_only);
  1097. tcx.client = client;
  1098. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1099. &add_to_tc,
  1100. &tcx);
  1101. env = GNUNET_MQ_msg (msg,
  1102. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1103. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1104. env);
  1105. GNUNET_SERVICE_client_continue (client);
  1106. }
  1107. /**
  1108. * Handle NOTIFY-message.
  1109. *
  1110. * @param cls identification of the client
  1111. * @param nm the actual message
  1112. */
  1113. static void
  1114. handle_notify (void *cls,
  1115. const struct NotifyMessage *nm)
  1116. {
  1117. struct GNUNET_SERVICE_Client *client = cls;
  1118. struct GNUNET_MQ_Handle *mq;
  1119. struct TransmitContext tcx;
  1120. struct GNUNET_MQ_Envelope *env;
  1121. struct GNUNET_MessageHeader *msg;
  1122. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1123. "NOTIFY message received\n");
  1124. mq = GNUNET_SERVICE_client_get_mq (client);
  1125. GNUNET_SERVICE_client_mark_monitor (client);
  1126. if (ntohl (nm->include_friend_only))
  1127. GNUNET_notification_context_add (notify_friend_only_list,
  1128. mq);
  1129. else
  1130. GNUNET_notification_context_add (notify_list,
  1131. mq);
  1132. tcx.friend_only = ntohl (nm->include_friend_only);
  1133. tcx.client = client;
  1134. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1135. &add_to_tc,
  1136. &tcx);
  1137. env = GNUNET_MQ_msg (msg,
  1138. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1139. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1140. env);
  1141. GNUNET_SERVICE_client_continue (client);
  1142. }
  1143. /**
  1144. * Client connect callback
  1145. *
  1146. * @param cls unused
  1147. * @param client server client
  1148. * @param mq for @a client
  1149. * @return @a client
  1150. */
  1151. static void *
  1152. client_connect_cb (void *cls,
  1153. struct GNUNET_SERVICE_Client *client,
  1154. struct GNUNET_MQ_Handle *mq)
  1155. {
  1156. (void) cls;
  1157. (void) mq;
  1158. return client;
  1159. }
  1160. /**
  1161. * Client disconnect callback
  1162. *
  1163. * @param cls unused
  1164. * @param client server client
  1165. * @param app_ctx should be @a client
  1166. */
  1167. static void
  1168. client_disconnect_cb (void *cls,
  1169. struct GNUNET_SERVICE_Client *client,
  1170. void *app_ctx)
  1171. {
  1172. (void) cls;
  1173. GNUNET_assert (app_ctx == client);
  1174. }
  1175. /**
  1176. * Release memory taken by a host entry.
  1177. *
  1178. * @param cls NULL
  1179. * @param key key of the host entry
  1180. * @param value the `struct HostEntry` to free
  1181. * @return #GNUNET_YES (continue to iterate)
  1182. */
  1183. static int
  1184. free_host_entry (void *cls,
  1185. const struct GNUNET_PeerIdentity *key,
  1186. void *value)
  1187. {
  1188. struct HostEntry *he = value;
  1189. (void) cls;
  1190. (void) key;
  1191. GNUNET_free_non_null (he->hello);
  1192. GNUNET_free_non_null (he->friend_only_hello);
  1193. GNUNET_free (he);
  1194. return GNUNET_YES;
  1195. }
  1196. /**
  1197. * Clean up our state. Called during shutdown.
  1198. *
  1199. * @param cls unused
  1200. */
  1201. static void
  1202. shutdown_task (void *cls)
  1203. {
  1204. (void) cls;
  1205. GNUNET_notification_context_destroy (notify_list);
  1206. notify_list = NULL;
  1207. GNUNET_notification_context_destroy (notify_friend_only_list);
  1208. notify_friend_only_list = NULL;
  1209. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1210. &free_host_entry,
  1211. NULL);
  1212. GNUNET_CONTAINER_multipeermap_destroy (hostmap);
  1213. if (NULL != stats)
  1214. {
  1215. GNUNET_STATISTICS_destroy (stats,
  1216. GNUNET_NO);
  1217. stats = NULL;
  1218. }
  1219. if (NULL != cron_clean)
  1220. {
  1221. GNUNET_SCHEDULER_cancel (cron_clean);
  1222. cron_clean = NULL;
  1223. }
  1224. if (NULL != cron_scan)
  1225. {
  1226. GNUNET_SCHEDULER_cancel (cron_scan);
  1227. cron_scan = NULL;
  1228. }
  1229. if (NULL != networkIdDirectory)
  1230. {
  1231. GNUNET_free (networkIdDirectory);
  1232. networkIdDirectory = NULL;
  1233. }
  1234. }
  1235. /**
  1236. * Start up peerinfo service.
  1237. *
  1238. * @param cls closure
  1239. * @param cfg configuration to use
  1240. * @param service the initialized service
  1241. */
  1242. static void
  1243. run (void *cls,
  1244. const struct GNUNET_CONFIGURATION_Handle *cfg,
  1245. struct GNUNET_SERVICE_Handle *service)
  1246. {
  1247. char *peerdir;
  1248. char *ip;
  1249. struct DirScanContext dsc;
  1250. int noio;
  1251. int use_included;
  1252. (void) cls;
  1253. (void) service;
  1254. hostmap
  1255. = GNUNET_CONTAINER_multipeermap_create (1024,
  1256. GNUNET_YES);
  1257. stats
  1258. = GNUNET_STATISTICS_create ("peerinfo",
  1259. cfg);
  1260. notify_list
  1261. = GNUNET_notification_context_create (0);
  1262. notify_friend_only_list
  1263. = GNUNET_notification_context_create (0);
  1264. noio = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  1265. "peerinfo",
  1266. "NO_IO");
  1267. use_included
  1268. = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  1269. "peerinfo",
  1270. "USE_INCLUDED_HELLOS");
  1271. if (GNUNET_SYSERR == use_included)
  1272. use_included = GNUNET_NO;
  1273. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  1274. NULL);
  1275. if (GNUNET_YES != noio)
  1276. {
  1277. GNUNET_assert (GNUNET_OK ==
  1278. GNUNET_CONFIGURATION_get_value_filename (cfg,
  1279. "peerinfo",
  1280. "HOSTS",
  1281. &networkIdDirectory));
  1282. if (GNUNET_OK !=
  1283. GNUNET_DISK_directory_create (networkIdDirectory))
  1284. {
  1285. GNUNET_SCHEDULER_shutdown ();
  1286. return;
  1287. }
  1288. cron_scan
  1289. = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
  1290. &cron_scan_directory_data_hosts,
  1291. NULL);
  1292. cron_clean
  1293. = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
  1294. &cron_clean_data_hosts,
  1295. NULL);
  1296. if (GNUNET_YES == use_included)
  1297. {
  1298. ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
  1299. GNUNET_asprintf (&peerdir,
  1300. "%shellos",
  1301. ip);
  1302. GNUNET_free (ip);
  1303. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1304. _("Importing HELLOs from `%s'\n"),
  1305. peerdir);
  1306. dsc.matched = 0;
  1307. dsc.remove_files = GNUNET_NO;
  1308. GNUNET_DISK_directory_scan (peerdir,
  1309. &hosts_directory_scan_callback,
  1310. &dsc);
  1311. GNUNET_free (peerdir);
  1312. }
  1313. else
  1314. {
  1315. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1316. _("Skipping import of included HELLOs\n"));
  1317. }
  1318. }
  1319. }
  1320. /**
  1321. * Define "main" method using service macro.
  1322. */
  1323. GNUNET_SERVICE_MAIN
  1324. ("peerinfo",
  1325. GNUNET_SERVICE_OPTION_NONE,
  1326. &run,
  1327. &client_connect_cb,
  1328. &client_disconnect_cb,
  1329. NULL,
  1330. GNUNET_MQ_hd_var_size (hello,
  1331. GNUNET_MESSAGE_TYPE_HELLO,
  1332. struct GNUNET_HELLO_Message,
  1333. NULL),
  1334. GNUNET_MQ_hd_fixed_size (get,
  1335. GNUNET_MESSAGE_TYPE_PEERINFO_GET,
  1336. struct ListPeerMessage,
  1337. NULL),
  1338. GNUNET_MQ_hd_fixed_size (get_all,
  1339. GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
  1340. struct ListAllPeersMessage,
  1341. NULL),
  1342. GNUNET_MQ_hd_fixed_size (notify,
  1343. GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
  1344. struct NotifyMessage,
  1345. NULL),
  1346. GNUNET_MQ_handler_end ());
  1347. /* end of gnunet-service-peerinfo.c */