gnunet-service-peerinfo.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 != memcmp (&id_friend,
  523. &id_public,
  524. sizeof (id_friend))) )
  525. {
  526. /* HELLOs are not for the same peer */
  527. GNUNET_break (0);
  528. if (GNUNET_YES == dsc->remove_files)
  529. remove_garbage (fullname);
  530. return GNUNET_OK;
  531. }
  532. if (GNUNET_OK ==
  533. GNUNET_CRYPTO_eddsa_public_key_from_string (filename,
  534. strlen (filename),
  535. &identity.public_key))
  536. {
  537. if (0 != memcmp (&id, &identity, sizeof (id_friend)))
  538. {
  539. /* HELLOs are not for the same peer */
  540. GNUNET_break (0);
  541. if (GNUNET_YES == dsc->remove_files)
  542. remove_garbage (fullname);
  543. return GNUNET_OK;
  544. }
  545. }
  546. /* ok, found something valid, remember HELLO */
  547. add_host_to_known_hosts (&id);
  548. if (NULL != r.hello)
  549. {
  550. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  551. "Updating peer `%s' public HELLO \n",
  552. GNUNET_i2s (&id));
  553. update_hello (&id, r.hello);
  554. GNUNET_free (r.hello);
  555. }
  556. if (NULL != r.friend_only_hello)
  557. {
  558. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  559. "Updating peer `%s' friend only HELLO \n",
  560. GNUNET_i2s (&id));
  561. update_hello (&id, r.friend_only_hello);
  562. GNUNET_free (r.friend_only_hello);
  563. }
  564. dsc->matched++;
  565. return GNUNET_OK;
  566. }
  567. /**
  568. * Call this method periodically to scan data/hosts for new hosts.
  569. *
  570. * @param cls unused
  571. */
  572. static void
  573. cron_scan_directory_data_hosts (void *cls)
  574. {
  575. static unsigned int retries;
  576. struct DirScanContext dsc;
  577. (void) cls;
  578. cron_scan = NULL;
  579. if (GNUNET_SYSERR ==
  580. GNUNET_DISK_directory_create (networkIdDirectory))
  581. {
  582. cron_scan = GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
  583. GNUNET_SCHEDULER_PRIORITY_IDLE,
  584. &cron_scan_directory_data_hosts, NULL);
  585. return;
  586. }
  587. dsc.matched = 0;
  588. dsc.remove_files = GNUNET_YES;
  589. GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
  590. _("Scanning directory `%s'\n"),
  591. networkIdDirectory);
  592. GNUNET_DISK_directory_scan (networkIdDirectory,
  593. &hosts_directory_scan_callback,
  594. &dsc);
  595. if ( (0 == dsc.matched) &&
  596. (0 == (++retries & 31)) )
  597. GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  598. _("Still no peers found in `%s'!\n"),
  599. networkIdDirectory);
  600. cron_scan = GNUNET_SCHEDULER_add_delayed_with_priority (DATA_HOST_FREQ,
  601. GNUNET_SCHEDULER_PRIORITY_IDLE,
  602. &cron_scan_directory_data_hosts,
  603. NULL);
  604. }
  605. /**
  606. * Update the HELLO of a friend by merging the addresses.
  607. *
  608. * @param hello original hello
  609. * @param friend_hello hello with additional addresses
  610. * @return merged HELLO
  611. */
  612. static struct GNUNET_HELLO_Message *
  613. update_friend_hello (const struct GNUNET_HELLO_Message *hello,
  614. const struct GNUNET_HELLO_Message *friend_hello)
  615. {
  616. struct GNUNET_HELLO_Message * res;
  617. struct GNUNET_HELLO_Message * tmp;
  618. struct GNUNET_PeerIdentity pid;
  619. if (NULL != friend_hello)
  620. {
  621. res = GNUNET_HELLO_merge (hello,
  622. friend_hello);
  623. GNUNET_assert (GNUNET_YES ==
  624. GNUNET_HELLO_is_friend_only (res));
  625. return res;
  626. }
  627. if (GNUNET_OK !=
  628. GNUNET_HELLO_get_id (hello, &pid))
  629. {
  630. GNUNET_break (0);
  631. return NULL;
  632. }
  633. tmp = GNUNET_HELLO_create (&pid.public_key,
  634. NULL,
  635. NULL,
  636. GNUNET_YES);
  637. res = GNUNET_HELLO_merge (hello, tmp);
  638. GNUNET_free (tmp);
  639. GNUNET_assert (GNUNET_YES == GNUNET_HELLO_is_friend_only (res));
  640. return res;
  641. }
  642. /**
  643. * Bind a host address (hello) to a hostId.
  644. *
  645. * @param peer the peer for which this is a hello
  646. * @param hello the verified (!) hello message
  647. */
  648. static void
  649. update_hello (const struct GNUNET_PeerIdentity *peer,
  650. const struct GNUNET_HELLO_Message *hello)
  651. {
  652. char *fn;
  653. struct HostEntry *host;
  654. struct GNUNET_HELLO_Message *mrg;
  655. struct GNUNET_HELLO_Message **dest;
  656. struct GNUNET_TIME_Absolute delta;
  657. unsigned int cnt;
  658. unsigned int size;
  659. int friend_hello_type;
  660. int store_hello;
  661. int store_friend_hello;
  662. unsigned int pos;
  663. char *buffer;
  664. host = GNUNET_CONTAINER_multipeermap_get (hostmap, peer);
  665. GNUNET_assert (NULL != host);
  666. friend_hello_type = GNUNET_HELLO_is_friend_only (hello);
  667. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  668. "Updating %s HELLO for `%s'\n",
  669. (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
  670. GNUNET_i2s (peer));
  671. dest = NULL;
  672. if (GNUNET_YES == friend_hello_type)
  673. {
  674. dest = &host->friend_only_hello;
  675. }
  676. else
  677. {
  678. dest = &host->hello;
  679. }
  680. if (NULL == (*dest))
  681. {
  682. (*dest) = GNUNET_malloc (GNUNET_HELLO_size (hello));
  683. GNUNET_memcpy ((*dest), hello, GNUNET_HELLO_size (hello));
  684. }
  685. else
  686. {
  687. mrg = GNUNET_HELLO_merge ((*dest),
  688. hello);
  689. delta = GNUNET_HELLO_equals (mrg,
  690. (*dest),
  691. GNUNET_TIME_absolute_get ());
  692. if (delta.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
  693. {
  694. /* no differences, just ignore the update */
  695. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  696. "No change in %s HELLO for `%s'\n",
  697. (GNUNET_YES == friend_hello_type) ? "friend-only" : "public",
  698. GNUNET_i2s (peer));
  699. GNUNET_free (mrg);
  700. return;
  701. }
  702. GNUNET_free ((*dest));
  703. (*dest) = mrg;
  704. }
  705. if ( (NULL != (host->hello)) &&
  706. (GNUNET_NO == friend_hello_type) )
  707. {
  708. /* Update friend only hello */
  709. mrg = update_friend_hello (host->hello,
  710. host->friend_only_hello);
  711. if (NULL != host->friend_only_hello)
  712. GNUNET_free (host->friend_only_hello);
  713. host->friend_only_hello = mrg;
  714. }
  715. if (NULL != host->hello)
  716. GNUNET_assert ((GNUNET_NO ==
  717. GNUNET_HELLO_is_friend_only (host->hello)));
  718. if (NULL != host->friend_only_hello)
  719. GNUNET_assert ((GNUNET_YES ==
  720. GNUNET_HELLO_is_friend_only (host->friend_only_hello)));
  721. fn = get_host_filename (peer);
  722. if ( (NULL != fn) &&
  723. (GNUNET_OK ==
  724. GNUNET_DISK_directory_create_for_file (fn)) )
  725. {
  726. store_hello = GNUNET_NO;
  727. size = 0;
  728. cnt = 0;
  729. if (NULL != host->hello)
  730. (void) GNUNET_HELLO_iterate_addresses (host->hello,
  731. GNUNET_NO,
  732. &count_addresses,
  733. &cnt);
  734. if (cnt > 0)
  735. {
  736. store_hello = GNUNET_YES;
  737. size += GNUNET_HELLO_size (host->hello);
  738. }
  739. cnt = 0;
  740. if (NULL != host->friend_only_hello)
  741. (void) GNUNET_HELLO_iterate_addresses (host->friend_only_hello,
  742. GNUNET_NO,
  743. &count_addresses,
  744. &cnt);
  745. store_friend_hello = GNUNET_NO;
  746. if (0 < cnt)
  747. {
  748. store_friend_hello = GNUNET_YES;
  749. size += GNUNET_HELLO_size (host->friend_only_hello);
  750. }
  751. if ( (GNUNET_NO == store_hello) &&
  752. (GNUNET_NO == store_friend_hello) )
  753. {
  754. /* no valid addresses, don't put HELLO on disk; in fact,
  755. if one exists on disk, remove it */
  756. (void) UNLINK (fn);
  757. }
  758. else
  759. {
  760. buffer = GNUNET_malloc (size);
  761. pos = 0;
  762. if (GNUNET_YES == store_hello)
  763. {
  764. GNUNET_memcpy (buffer,
  765. host->hello,
  766. GNUNET_HELLO_size (host->hello));
  767. pos += GNUNET_HELLO_size (host->hello);
  768. }
  769. if (GNUNET_YES == store_friend_hello)
  770. {
  771. GNUNET_memcpy (&buffer[pos],
  772. host->friend_only_hello,
  773. GNUNET_HELLO_size (host->friend_only_hello));
  774. pos += GNUNET_HELLO_size (host->friend_only_hello);
  775. }
  776. GNUNET_assert (pos == size);
  777. if (GNUNET_SYSERR == GNUNET_DISK_fn_write (fn, buffer, size,
  778. GNUNET_DISK_PERM_USER_READ |
  779. GNUNET_DISK_PERM_USER_WRITE |
  780. GNUNET_DISK_PERM_GROUP_READ |
  781. GNUNET_DISK_PERM_OTHER_READ))
  782. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
  783. else
  784. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  785. "Stored %s %s HELLO in %s with total size %u\n",
  786. (GNUNET_YES == store_friend_hello) ? "friend-only": "",
  787. (GNUNET_YES == store_hello) ? "public": "",
  788. fn,
  789. size);
  790. GNUNET_free (buffer);
  791. }
  792. }
  793. GNUNET_free_non_null (fn);
  794. notify_all (host);
  795. }
  796. /**
  797. * Closure for #add_to_tc()
  798. */
  799. struct TransmitContext
  800. {
  801. /**
  802. * Client to transmit to
  803. */
  804. struct GNUNET_SERVICE_Client *client;
  805. /**
  806. * Include friend only HELLOs #GNUNET_YES or #GNUNET_NO
  807. */
  808. int friend_only;
  809. };
  810. /**
  811. * Do transmit info about peer to given host.
  812. *
  813. * @param cls NULL to hit all hosts, otherwise specifies a particular target
  814. * @param key hostID
  815. * @param value information to transmit
  816. * @return #GNUNET_YES (continue to iterate)
  817. */
  818. static int
  819. add_to_tc (void *cls,
  820. const struct GNUNET_PeerIdentity *key,
  821. void *value)
  822. {
  823. struct TransmitContext *tc = cls;
  824. struct HostEntry *pos = value;
  825. struct InfoMessage *im;
  826. uint16_t hs;
  827. struct GNUNET_MQ_Envelope *env;
  828. hs = 0;
  829. if ( (NULL != pos->hello) &&
  830. (GNUNET_NO == tc->friend_only) )
  831. {
  832. /* Copy public HELLO */
  833. hs = GNUNET_HELLO_size (pos->hello);
  834. GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE -
  835. sizeof (struct InfoMessage));
  836. env = GNUNET_MQ_msg_extra (im,
  837. hs,
  838. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  839. GNUNET_memcpy (&im[1],
  840. pos->hello,
  841. hs);
  842. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  843. "Sending public HELLO with size %u for peer `%s'\n",
  844. hs,
  845. GNUNET_i2s (key));
  846. }
  847. else if ( (NULL != pos->friend_only_hello) &&
  848. (GNUNET_YES == tc->friend_only) )
  849. {
  850. /* Copy friend only HELLO */
  851. hs = GNUNET_HELLO_size (pos->friend_only_hello);
  852. GNUNET_assert (hs < GNUNET_MAX_MESSAGE_SIZE -
  853. sizeof (struct InfoMessage));
  854. env = GNUNET_MQ_msg_extra (im,
  855. hs,
  856. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  857. GNUNET_memcpy (&im[1],
  858. pos->friend_only_hello,
  859. hs);
  860. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  861. "Sending friend-only HELLO with size %u for peer `%s'\n",
  862. hs,
  863. GNUNET_i2s (key));
  864. }
  865. else
  866. {
  867. env = GNUNET_MQ_msg (im,
  868. GNUNET_MESSAGE_TYPE_PEERINFO_INFO);
  869. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  870. "Adding no HELLO for peer `%s'\n",
  871. GNUNET_i2s (key));
  872. }
  873. im->peer = pos->identity;
  874. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (tc->client),
  875. env);
  876. return GNUNET_YES;
  877. }
  878. /**
  879. * @brief delete expired HELLO entries in directory
  880. *
  881. * @param cls pointer to current time (`struct GNUNET_TIME_Absolute *`)
  882. * @param fn filename to test to see if the HELLO expired
  883. * @return #GNUNET_OK (continue iteration)
  884. */
  885. static int
  886. discard_hosts_helper (void *cls,
  887. const char *fn)
  888. {
  889. struct GNUNET_TIME_Absolute *now = cls;
  890. char buffer[GNUNET_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN;
  891. const struct GNUNET_HELLO_Message *hello;
  892. struct GNUNET_HELLO_Message *new_hello;
  893. int read_size;
  894. unsigned int cur_hello_size;
  895. unsigned int new_hello_size;
  896. int read_pos;
  897. int write_pos;
  898. unsigned int cnt;
  899. char *writebuffer;
  900. uint64_t fsize;
  901. if (GNUNET_OK !=
  902. GNUNET_DISK_file_size (fn,
  903. &fsize,
  904. GNUNET_YES,
  905. GNUNET_YES))
  906. {
  907. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  908. GNUNET_ERROR_TYPE_BULK,
  909. "fstat",
  910. fn);
  911. return GNUNET_OK;
  912. }
  913. read_size = GNUNET_DISK_fn_read (fn,
  914. buffer,
  915. sizeof (buffer));
  916. if ( (read_size < (int) sizeof (struct GNUNET_MessageHeader)) ||
  917. (fsize > GNUNET_MAX_MESSAGE_SIZE) )
  918. {
  919. if (0 != UNLINK (fn))
  920. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  921. GNUNET_ERROR_TYPE_BULK,
  922. "unlink",
  923. fn);
  924. return GNUNET_OK;
  925. }
  926. writebuffer = GNUNET_malloc (read_size);
  927. read_pos = 0;
  928. write_pos = 0;
  929. while (read_pos < read_size)
  930. {
  931. /* Check each HELLO */
  932. hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos];
  933. cur_hello_size = GNUNET_HELLO_size (hello);
  934. if (0 == cur_hello_size)
  935. {
  936. /* Invalid data, discard */
  937. if (0 != UNLINK (fn))
  938. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  939. GNUNET_ERROR_TYPE_BULK,
  940. "unlink",
  941. fn);
  942. GNUNET_free (writebuffer);
  943. return GNUNET_OK;
  944. }
  945. new_hello = GNUNET_HELLO_iterate_addresses (hello,
  946. GNUNET_YES,
  947. &discard_expired,
  948. now);
  949. cnt = 0;
  950. if (NULL != new_hello)
  951. (void) GNUNET_HELLO_iterate_addresses (hello,
  952. GNUNET_NO,
  953. &count_addresses,
  954. &cnt);
  955. if ( (NULL != new_hello) && (0 < cnt) )
  956. {
  957. /* Store new HELLO to write it when done */
  958. new_hello_size = GNUNET_HELLO_size (new_hello);
  959. GNUNET_memcpy (&writebuffer[write_pos],
  960. new_hello,
  961. new_hello_size);
  962. write_pos += new_hello_size;
  963. }
  964. read_pos += cur_hello_size;
  965. GNUNET_free_non_null (new_hello);
  966. }
  967. if (0 < write_pos)
  968. {
  969. GNUNET_DISK_fn_write (fn,
  970. writebuffer,
  971. write_pos,
  972. GNUNET_DISK_PERM_USER_READ |
  973. GNUNET_DISK_PERM_USER_WRITE |
  974. GNUNET_DISK_PERM_GROUP_READ |
  975. GNUNET_DISK_PERM_OTHER_READ);
  976. }
  977. else if (0 != UNLINK (fn))
  978. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
  979. GNUNET_ERROR_TYPE_BULK,
  980. "unlink", fn);
  981. GNUNET_free (writebuffer);
  982. return GNUNET_OK;
  983. }
  984. /**
  985. * Call this method periodically to scan peerinfo/ for ancient
  986. * HELLOs to expire.
  987. *
  988. * @param cls unused
  989. */
  990. static void
  991. cron_clean_data_hosts (void *cls)
  992. {
  993. struct GNUNET_TIME_Absolute now;
  994. (void) cls;
  995. cron_clean = NULL;
  996. now = GNUNET_TIME_absolute_get ();
  997. GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
  998. _("Cleaning up directory `%s'\n"),
  999. networkIdDirectory);
  1000. GNUNET_DISK_directory_scan (networkIdDirectory,
  1001. &discard_hosts_helper,
  1002. &now);
  1003. cron_clean = GNUNET_SCHEDULER_add_delayed (DATA_HOST_CLEAN_FREQ,
  1004. &cron_clean_data_hosts,
  1005. NULL);
  1006. }
  1007. /**
  1008. * Check HELLO-message.
  1009. *
  1010. * @param cls identification of the client
  1011. * @param hello the actual message
  1012. * @return #GNUNET_OK if @a hello is well-formed
  1013. */
  1014. static int
  1015. check_hello (void *cls,
  1016. const struct GNUNET_HELLO_Message *hello)
  1017. {
  1018. struct GNUNET_PeerIdentity pid;
  1019. (void) cls;
  1020. if (GNUNET_OK !=
  1021. GNUNET_HELLO_get_id (hello,
  1022. &pid))
  1023. {
  1024. GNUNET_break (0);
  1025. return GNUNET_SYSERR;
  1026. }
  1027. return GNUNET_OK;
  1028. }
  1029. /**
  1030. * Handle HELLO-message.
  1031. *
  1032. * @param cls identification of the client
  1033. * @param hello the actual message
  1034. */
  1035. static void
  1036. handle_hello (void *cls,
  1037. const struct GNUNET_HELLO_Message *hello)
  1038. {
  1039. struct GNUNET_SERVICE_Client *client = cls;
  1040. struct GNUNET_PeerIdentity pid;
  1041. GNUNET_assert (GNUNET_OK ==
  1042. GNUNET_HELLO_get_id (hello,
  1043. &pid));
  1044. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1045. "HELLO message received for peer `%s'\n",
  1046. GNUNET_i2s (&pid));
  1047. add_host_to_known_hosts (&pid);
  1048. update_hello (&pid,
  1049. hello);
  1050. GNUNET_SERVICE_client_continue (client);
  1051. }
  1052. /**
  1053. * Handle GET-message.
  1054. *
  1055. * @param cls identification of the client
  1056. * @param lpm the actual message
  1057. */
  1058. static void
  1059. handle_get (void *cls,
  1060. const struct ListPeerMessage *lpm)
  1061. {
  1062. struct GNUNET_SERVICE_Client *client = cls;
  1063. struct TransmitContext tcx;
  1064. struct GNUNET_MessageHeader *msg;
  1065. struct GNUNET_MQ_Envelope *env;
  1066. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1067. "GET message received for peer `%s'\n",
  1068. GNUNET_i2s (&lpm->peer));
  1069. tcx.friend_only = ntohl (lpm->include_friend_only);
  1070. tcx.client = client;
  1071. GNUNET_CONTAINER_multipeermap_get_multiple (hostmap,
  1072. &lpm->peer,
  1073. &add_to_tc,
  1074. &tcx);
  1075. env = GNUNET_MQ_msg (msg,
  1076. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1077. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1078. env);
  1079. GNUNET_SERVICE_client_continue (client);
  1080. }
  1081. /**
  1082. * Handle GET-ALL-message.
  1083. *
  1084. * @param cls identification of the client
  1085. * @param lapm the actual message
  1086. */
  1087. static void
  1088. handle_get_all (void *cls,
  1089. const struct ListAllPeersMessage *lapm)
  1090. {
  1091. struct GNUNET_SERVICE_Client *client = cls;
  1092. struct TransmitContext tcx;
  1093. struct GNUNET_MQ_Envelope *env;
  1094. struct GNUNET_MessageHeader *msg;
  1095. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1096. "GET_ALL message received\n");
  1097. tcx.friend_only = ntohl (lapm->include_friend_only);
  1098. tcx.client = client;
  1099. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1100. &add_to_tc,
  1101. &tcx);
  1102. env = GNUNET_MQ_msg (msg,
  1103. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1104. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1105. env);
  1106. GNUNET_SERVICE_client_continue (client);
  1107. }
  1108. /**
  1109. * Handle NOTIFY-message.
  1110. *
  1111. * @param cls identification of the client
  1112. * @param nm the actual message
  1113. */
  1114. static void
  1115. handle_notify (void *cls,
  1116. const struct NotifyMessage *nm)
  1117. {
  1118. struct GNUNET_SERVICE_Client *client = cls;
  1119. struct GNUNET_MQ_Handle *mq;
  1120. struct TransmitContext tcx;
  1121. struct GNUNET_MQ_Envelope *env;
  1122. struct GNUNET_MessageHeader *msg;
  1123. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1124. "NOTIFY message received\n");
  1125. mq = GNUNET_SERVICE_client_get_mq (client);
  1126. GNUNET_SERVICE_client_mark_monitor (client);
  1127. if (ntohl (nm->include_friend_only))
  1128. GNUNET_notification_context_add (notify_friend_only_list,
  1129. mq);
  1130. else
  1131. GNUNET_notification_context_add (notify_list,
  1132. mq);
  1133. tcx.friend_only = ntohl (nm->include_friend_only);
  1134. tcx.client = client;
  1135. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1136. &add_to_tc,
  1137. &tcx);
  1138. env = GNUNET_MQ_msg (msg,
  1139. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END);
  1140. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
  1141. env);
  1142. GNUNET_SERVICE_client_continue (client);
  1143. }
  1144. /**
  1145. * Client connect callback
  1146. *
  1147. * @param cls unused
  1148. * @param client server client
  1149. * @param mq for @a client
  1150. * @return @a client
  1151. */
  1152. static void *
  1153. client_connect_cb (void *cls,
  1154. struct GNUNET_SERVICE_Client *client,
  1155. struct GNUNET_MQ_Handle *mq)
  1156. {
  1157. (void) cls;
  1158. (void) mq;
  1159. return client;
  1160. }
  1161. /**
  1162. * Client disconnect callback
  1163. *
  1164. * @param cls unused
  1165. * @param client server client
  1166. * @param app_ctx should be @a client
  1167. */
  1168. static void
  1169. client_disconnect_cb (void *cls,
  1170. struct GNUNET_SERVICE_Client *client,
  1171. void *app_ctx)
  1172. {
  1173. (void) cls;
  1174. GNUNET_assert (app_ctx == client);
  1175. }
  1176. /**
  1177. * Release memory taken by a host entry.
  1178. *
  1179. * @param cls NULL
  1180. * @param key key of the host entry
  1181. * @param value the `struct HostEntry` to free
  1182. * @return #GNUNET_YES (continue to iterate)
  1183. */
  1184. static int
  1185. free_host_entry (void *cls,
  1186. const struct GNUNET_PeerIdentity *key,
  1187. void *value)
  1188. {
  1189. struct HostEntry *he = value;
  1190. (void) cls;
  1191. (void) key;
  1192. GNUNET_free_non_null (he->hello);
  1193. GNUNET_free_non_null (he->friend_only_hello);
  1194. GNUNET_free (he);
  1195. return GNUNET_YES;
  1196. }
  1197. /**
  1198. * Clean up our state. Called during shutdown.
  1199. *
  1200. * @param cls unused
  1201. */
  1202. static void
  1203. shutdown_task (void *cls)
  1204. {
  1205. (void) cls;
  1206. GNUNET_notification_context_destroy (notify_list);
  1207. notify_list = NULL;
  1208. GNUNET_notification_context_destroy (notify_friend_only_list);
  1209. notify_friend_only_list = NULL;
  1210. GNUNET_CONTAINER_multipeermap_iterate (hostmap,
  1211. &free_host_entry,
  1212. NULL);
  1213. GNUNET_CONTAINER_multipeermap_destroy (hostmap);
  1214. if (NULL != stats)
  1215. {
  1216. GNUNET_STATISTICS_destroy (stats,
  1217. GNUNET_NO);
  1218. stats = NULL;
  1219. }
  1220. if (NULL != cron_clean)
  1221. {
  1222. GNUNET_SCHEDULER_cancel (cron_clean);
  1223. cron_clean = NULL;
  1224. }
  1225. if (NULL != cron_scan)
  1226. {
  1227. GNUNET_SCHEDULER_cancel (cron_scan);
  1228. cron_scan = NULL;
  1229. }
  1230. if (NULL != networkIdDirectory)
  1231. {
  1232. GNUNET_free (networkIdDirectory);
  1233. networkIdDirectory = NULL;
  1234. }
  1235. }
  1236. /**
  1237. * Start up peerinfo service.
  1238. *
  1239. * @param cls closure
  1240. * @param cfg configuration to use
  1241. * @param service the initialized service
  1242. */
  1243. static void
  1244. run (void *cls,
  1245. const struct GNUNET_CONFIGURATION_Handle *cfg,
  1246. struct GNUNET_SERVICE_Handle *service)
  1247. {
  1248. char *peerdir;
  1249. char *ip;
  1250. struct DirScanContext dsc;
  1251. int noio;
  1252. int use_included;
  1253. (void) cls;
  1254. (void) service;
  1255. hostmap
  1256. = GNUNET_CONTAINER_multipeermap_create (1024,
  1257. GNUNET_YES);
  1258. stats
  1259. = GNUNET_STATISTICS_create ("peerinfo",
  1260. cfg);
  1261. notify_list
  1262. = GNUNET_notification_context_create (0);
  1263. notify_friend_only_list
  1264. = GNUNET_notification_context_create (0);
  1265. noio = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  1266. "peerinfo",
  1267. "NO_IO");
  1268. use_included
  1269. = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  1270. "peerinfo",
  1271. "USE_INCLUDED_HELLOS");
  1272. if (GNUNET_SYSERR == use_included)
  1273. use_included = GNUNET_NO;
  1274. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  1275. NULL);
  1276. if (GNUNET_YES != noio)
  1277. {
  1278. GNUNET_assert (GNUNET_OK ==
  1279. GNUNET_CONFIGURATION_get_value_filename (cfg,
  1280. "peerinfo",
  1281. "HOSTS",
  1282. &networkIdDirectory));
  1283. if (GNUNET_OK !=
  1284. GNUNET_DISK_directory_create (networkIdDirectory))
  1285. {
  1286. GNUNET_SCHEDULER_shutdown ();
  1287. return;
  1288. }
  1289. cron_scan
  1290. = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
  1291. &cron_scan_directory_data_hosts,
  1292. NULL);
  1293. cron_clean
  1294. = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
  1295. &cron_clean_data_hosts,
  1296. NULL);
  1297. if (GNUNET_YES == use_included)
  1298. {
  1299. ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
  1300. GNUNET_asprintf (&peerdir,
  1301. "%shellos",
  1302. ip);
  1303. GNUNET_free (ip);
  1304. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1305. _("Importing HELLOs from `%s'\n"),
  1306. peerdir);
  1307. dsc.matched = 0;
  1308. dsc.remove_files = GNUNET_NO;
  1309. GNUNET_DISK_directory_scan (peerdir,
  1310. &hosts_directory_scan_callback,
  1311. &dsc);
  1312. GNUNET_free (peerdir);
  1313. }
  1314. else
  1315. {
  1316. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1317. _("Skipping import of included HELLOs\n"));
  1318. }
  1319. }
  1320. }
  1321. /**
  1322. * Define "main" method using service macro.
  1323. */
  1324. GNUNET_SERVICE_MAIN
  1325. ("peerinfo",
  1326. GNUNET_SERVICE_OPTION_NONE,
  1327. &run,
  1328. &client_connect_cb,
  1329. &client_disconnect_cb,
  1330. NULL,
  1331. GNUNET_MQ_hd_var_size (hello,
  1332. GNUNET_MESSAGE_TYPE_HELLO,
  1333. struct GNUNET_HELLO_Message,
  1334. NULL),
  1335. GNUNET_MQ_hd_fixed_size (get,
  1336. GNUNET_MESSAGE_TYPE_PEERINFO_GET,
  1337. struct ListPeerMessage,
  1338. NULL),
  1339. GNUNET_MQ_hd_fixed_size (get_all,
  1340. GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL,
  1341. struct ListAllPeersMessage,
  1342. NULL),
  1343. GNUNET_MQ_hd_fixed_size (notify,
  1344. GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY,
  1345. struct NotifyMessage,
  1346. NULL),
  1347. GNUNET_MQ_handler_end ());
  1348. /* end of gnunet-service-peerinfo.c */