gnunet-daemon-hostlist_client.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2010, 2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file hostlist/gnunet-daemon-hostlist_client.c
  19. * @brief hostlist support. Downloads HELLOs via HTTP.
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet-daemon-hostlist_client.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_statistics_service.h"
  27. #include "gnunet_transport_service.h"
  28. #include "gnunet-daemon-hostlist.h"
  29. #if HAVE_CURL_CURL_H
  30. #include <curl/curl.h>
  31. #elif HAVE_GNURL_CURL_H
  32. #include <gnurl/curl.h>
  33. #endif
  34. /**
  35. * Number of connections that we must have to NOT download
  36. * hostlists anymore.
  37. */
  38. #define MIN_CONNECTIONS 4
  39. /**
  40. * Maximum number of hostlist that are saved
  41. */
  42. #define MAX_NUMBER_HOSTLISTS 30
  43. /**
  44. * Time interval hostlists are saved to disk
  45. */
  46. #define SAVING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
  47. /**
  48. * Time interval between two hostlist tests
  49. */
  50. #define TESTING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  51. /**
  52. * Time interval for download dispatcher before a download is re-scheduled
  53. */
  54. #define WAITING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
  55. /**
  56. * Defines concerning the hostlist quality metric
  57. */
  58. /**
  59. * Initial quality of a new created hostlist
  60. */
  61. #define HOSTLIST_INITIAL 10000
  62. /**
  63. * Value subtracted each time a hostlist download fails
  64. */
  65. #define HOSTLIST_FAILED_DOWNLOAD 100
  66. /**
  67. * Value added each time a hostlist download is successful
  68. */
  69. #define HOSTLIST_SUCCESSFUL_DOWNLOAD 100
  70. /**
  71. * Value added for each valid HELLO recived during a hostlist download
  72. */
  73. #define HOSTLIST_SUCCESSFUL_HELLO 1
  74. /**
  75. * A single hostlist obtained by hostlist advertisements
  76. */
  77. struct Hostlist
  78. {
  79. /**
  80. * previous entry, used to manage entries in a double linked list
  81. */
  82. struct Hostlist *prev;
  83. /**
  84. * next entry, used to manage entries in a double linked list
  85. */
  86. struct Hostlist *next;
  87. /**
  88. * URI where hostlist can be obtained
  89. */
  90. const char *hostlist_uri;
  91. /**
  92. * Value describing the quality of the hostlist, the bigger the better but (should) never < 0
  93. * used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
  94. * intial value = HOSTLIST_INITIAL
  95. * increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
  96. * increased every successful download by number of obtained HELLO messages
  97. * decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
  98. */
  99. uint64_t quality;
  100. /**
  101. * Time the hostlist advertisement was recieved and the entry was created
  102. */
  103. struct GNUNET_TIME_Absolute time_creation;
  104. /**
  105. * Last time the hostlist was obtained
  106. */
  107. struct GNUNET_TIME_Absolute time_last_usage;
  108. /**
  109. * Number of HELLO messages obtained during last download
  110. */
  111. uint32_t hello_count;
  112. /**
  113. * Number of times the hostlist was successfully obtained
  114. */
  115. uint32_t times_used;
  116. };
  117. /**
  118. * Our configuration.
  119. */
  120. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  121. /**
  122. * Statistics handle.
  123. */
  124. static struct GNUNET_STATISTICS_Handle *stats;
  125. /**
  126. * Transport handle.
  127. */
  128. static struct GNUNET_TRANSPORT_Handle *transport;
  129. /**
  130. * Proxy hostname or ip we are using (can be NULL).
  131. */
  132. static char *proxy;
  133. /**
  134. * Proxy username we are using (can be NULL).
  135. */
  136. static char *proxy_username;
  137. /**
  138. * Proxy password we are using (can be NULL).
  139. */
  140. static char *proxy_password;
  141. /**
  142. * Proxy type we are using (can be NULL).
  143. */
  144. static curl_proxytype proxy_type;
  145. /**
  146. * Number of bytes valid in 'download_buffer'.
  147. */
  148. static size_t download_pos;
  149. /**
  150. * Current URL that we are using.
  151. */
  152. static char *current_url;
  153. /**
  154. * Current CURL handle.
  155. */
  156. static CURL *curl;
  157. /**
  158. * Current multi-CURL handle.
  159. */
  160. static CURLM *multi;
  161. /**
  162. * How many bytes did we download from the current hostlist URL?
  163. */
  164. static uint32_t stat_bytes_downloaded;
  165. /**
  166. * Amount of time we wait between hostlist downloads.
  167. */
  168. static struct GNUNET_TIME_Relative hostlist_delay;
  169. /**
  170. * ID of the task, checking if hostlist download should take plate
  171. */
  172. static struct GNUNET_SCHEDULER_Task * ti_check_download;
  173. /**
  174. * ID of the task downloading the hostlist
  175. */
  176. static struct GNUNET_SCHEDULER_Task * ti_download;
  177. /**
  178. * ID of the task saving the hostlsit in a regular intervall
  179. */
  180. static struct GNUNET_SCHEDULER_Task * ti_saving_task;
  181. /**
  182. * ID of the task called to initiate a download
  183. */
  184. static struct GNUNET_SCHEDULER_Task * ti_download_dispatcher_task;
  185. /**
  186. * ID of the task controlling the locking between two hostlist tests
  187. */
  188. static struct GNUNET_SCHEDULER_Task * ti_testing_intervall_task;
  189. /**
  190. * At what time MUST the current hostlist request be done?
  191. */
  192. static struct GNUNET_TIME_Absolute end_time;
  193. /**
  194. * Head of the linked list used to store hostlists
  195. */
  196. static struct Hostlist *linked_list_head;
  197. /**
  198. * Tail of the linked list used to store hostlists
  199. */
  200. static struct Hostlist *linked_list_tail;
  201. /**
  202. * Current hostlist used for downloading
  203. */
  204. static struct Hostlist *current_hostlist;
  205. /**
  206. * Size of the linke list used to store hostlists
  207. */
  208. static unsigned int linked_list_size;
  209. /**
  210. * Head of the linked list used to store hostlists
  211. */
  212. static struct Hostlist *hostlist_to_test;
  213. /**
  214. * Handle for our statistics GET operation.
  215. */
  216. static struct GNUNET_STATISTICS_GetHandle *sget;
  217. /**
  218. * Set to GNUNET_YES if the current URL had some problems.
  219. */
  220. static int stat_bogus_url;
  221. /**
  222. * Value controlling if a hostlist is tested at the moment
  223. */
  224. static int stat_testing_hostlist;
  225. /**
  226. * Value controlling if a hostlist testing is allowed at the moment
  227. */
  228. static int stat_testing_allowed;
  229. /**
  230. * Value controlling if a hostlist download is running at the moment
  231. */
  232. static int stat_download_in_progress;
  233. /**
  234. * Value saying if a preconfigured bootstrap server is used
  235. */
  236. static unsigned int stat_use_bootstrap;
  237. /**
  238. * Set if we are allowed to learn new hostlists and use them
  239. */
  240. static int stat_learning;
  241. /**
  242. * Value saying if hostlist download was successful
  243. */
  244. static unsigned int stat_download_successful;
  245. /**
  246. * Value saying how many valid HELLO messages were obtained during download
  247. */
  248. static unsigned int stat_hellos_obtained;
  249. /**
  250. * Number of active connections (according to core service).
  251. */
  252. static unsigned int stat_connection_count;
  253. /**
  254. * Process downloaded bits by calling callback on each HELLO.
  255. *
  256. * @param ptr buffer with downloaded data
  257. * @param size size of a record
  258. * @param nmemb number of records downloaded
  259. * @param ctx unused
  260. * @return number of bytes that were processed (always size*nmemb)
  261. */
  262. static size_t
  263. callback_download (void *ptr,
  264. size_t size,
  265. size_t nmemb,
  266. void *ctx)
  267. {
  268. static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
  269. const char *cbuf = ptr;
  270. const struct GNUNET_MessageHeader *msg;
  271. size_t total;
  272. size_t cpy;
  273. size_t left;
  274. uint16_t msize;
  275. total = size * nmemb;
  276. stat_bytes_downloaded += total;
  277. if ((total == 0) || (stat_bogus_url))
  278. {
  279. return total; /* ok, no data or bogus data */
  280. }
  281. GNUNET_STATISTICS_update (stats,
  282. gettext_noop
  283. ("# bytes downloaded from hostlist servers"),
  284. (int64_t) total, GNUNET_NO);
  285. left = total;
  286. while ((left > 0) || (download_pos > 0))
  287. {
  288. cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - download_pos);
  289. memcpy (&download_buffer[download_pos], cbuf, cpy);
  290. cbuf += cpy;
  291. download_pos += cpy;
  292. left -= cpy;
  293. if (download_pos < sizeof (struct GNUNET_MessageHeader))
  294. {
  295. GNUNET_assert (0 == left);
  296. break;
  297. }
  298. msg = (const struct GNUNET_MessageHeader *) download_buffer;
  299. msize = ntohs (msg->size);
  300. if (msize < sizeof (struct GNUNET_MessageHeader))
  301. {
  302. GNUNET_STATISTICS_update (stats,
  303. gettext_noop
  304. ("# invalid HELLOs downloaded from hostlist servers"),
  305. 1, GNUNET_NO);
  306. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  307. _("Invalid `%s' message received from hostlist at `%s'\n"),
  308. "HELLO", current_url);
  309. stat_hellos_obtained++;
  310. stat_bogus_url = 1;
  311. return total;
  312. }
  313. if (download_pos < msize)
  314. {
  315. GNUNET_assert (left == 0);
  316. break;
  317. }
  318. if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) msg) == msize)
  319. {
  320. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  321. "Received valid `%s' message from hostlist server.\n",
  322. "HELLO");
  323. GNUNET_STATISTICS_update (stats,
  324. gettext_noop
  325. ("# valid HELLOs downloaded from hostlist servers"),
  326. 1, GNUNET_NO);
  327. stat_hellos_obtained++;
  328. GNUNET_TRANSPORT_offer_hello (transport, msg, NULL, NULL);
  329. }
  330. else
  331. {
  332. GNUNET_STATISTICS_update (stats,
  333. gettext_noop
  334. ("# invalid HELLOs downloaded from hostlist servers"),
  335. 1, GNUNET_NO);
  336. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  337. _("Invalid `%s' message received from hostlist at `%s'\n"),
  338. "HELLO", current_url);
  339. stat_bogus_url = GNUNET_YES;
  340. stat_hellos_obtained++;
  341. return total;
  342. }
  343. memmove (download_buffer, &download_buffer[msize], download_pos - msize);
  344. download_pos -= msize;
  345. }
  346. return total;
  347. }
  348. /**
  349. * Obtain a hostlist URL that we should use.
  350. *
  351. * @return NULL if there is no URL available
  352. */
  353. static char *
  354. get_bootstrap_server ()
  355. {
  356. char *servers;
  357. char *ret;
  358. size_t urls;
  359. size_t pos;
  360. if (GNUNET_OK !=
  361. GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST", "SERVERS",
  362. &servers))
  363. {
  364. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
  365. "hostlist", "SERVERS");
  366. return NULL;
  367. }
  368. urls = 0;
  369. if (strlen (servers) > 0)
  370. {
  371. urls++;
  372. pos = strlen (servers) - 1;
  373. while (pos > 0)
  374. {
  375. if (servers[pos] == ' ')
  376. urls++;
  377. pos--;
  378. }
  379. }
  380. if (urls == 0)
  381. {
  382. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
  383. "hostlist", "SERVERS");
  384. GNUNET_free (servers);
  385. return NULL;
  386. }
  387. urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
  388. pos = strlen (servers) - 1;
  389. while (pos > 0)
  390. {
  391. if (servers[pos] == ' ')
  392. {
  393. urls--;
  394. servers[pos] = '\0';
  395. }
  396. if (urls == 0)
  397. {
  398. pos++;
  399. break;
  400. }
  401. pos--;
  402. }
  403. ret = GNUNET_strdup (&servers[pos]);
  404. GNUNET_free (servers);
  405. return ret;
  406. }
  407. /**
  408. * Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
  409. * @return uri to use, NULL if there is no URL available
  410. */
  411. static char *
  412. download_get_url ()
  413. {
  414. uint32_t index;
  415. unsigned int counter;
  416. struct Hostlist *pos;
  417. if (GNUNET_NO == stat_learning)
  418. {
  419. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  420. "Using preconfigured bootstrap server\n");
  421. current_hostlist = NULL;
  422. return get_bootstrap_server ();
  423. }
  424. if ((GNUNET_YES == stat_testing_hostlist) && (NULL != hostlist_to_test))
  425. {
  426. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  427. "Testing new advertised hostlist if it is obtainable\n");
  428. current_hostlist = hostlist_to_test;
  429. return GNUNET_strdup (hostlist_to_test->hostlist_uri);
  430. }
  431. if ((GNUNET_YES == stat_use_bootstrap) || (linked_list_size == 0))
  432. {
  433. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  434. "Using preconfigured bootstrap server\n");
  435. current_hostlist = NULL;
  436. return get_bootstrap_server ();
  437. }
  438. index =
  439. GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
  440. counter = 0;
  441. pos = linked_list_head;
  442. while (counter < index)
  443. {
  444. pos = pos->next;
  445. counter++;
  446. }
  447. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using learned hostlist `%s'\n",
  448. pos->hostlist_uri);
  449. current_hostlist = pos;
  450. return GNUNET_strdup (pos->hostlist_uri);
  451. }
  452. #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt (c, a, b); if (CURLE_OK != ret) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror (ret)); } while (0)
  453. /**
  454. * Method to save hostlist to a file during hostlist client shutdown
  455. *
  456. * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
  457. */
  458. static void
  459. save_hostlist_file (int shutdown);
  460. /**
  461. * Add val2 to val1 with overflow check
  462. *
  463. * @param val1 value 1
  464. * @param val2 value 2
  465. * @return result
  466. */
  467. static uint64_t
  468. checked_add (uint64_t val1,
  469. uint64_t val2)
  470. {
  471. static uint64_t temp;
  472. static uint64_t maxv;
  473. maxv = 0;
  474. maxv--;
  475. temp = val1 + val2;
  476. if (temp < val1)
  477. return maxv;
  478. return temp;
  479. }
  480. /**
  481. * Subtract val2 from val1 with underflow check
  482. *
  483. * @param val1 value 1
  484. * @param val2 value 2
  485. * @return result
  486. */
  487. static uint64_t
  488. checked_sub (uint64_t val1,
  489. uint64_t val2)
  490. {
  491. if (val1 <= val2)
  492. return 0;
  493. return (val1 - val2);
  494. }
  495. /**
  496. * Method to check if a URI is in hostlist linked list
  497. *
  498. * @param uri uri to check
  499. * @return #GNUNET_YES if existing in linked list, #GNUNET_NO if not
  500. */
  501. static int
  502. linked_list_contains (const char *uri)
  503. {
  504. struct Hostlist *pos;
  505. pos = linked_list_head;
  506. while (pos != NULL)
  507. {
  508. if (0 == strcmp (pos->hostlist_uri, uri))
  509. return GNUNET_YES;
  510. pos = pos->next;
  511. }
  512. return GNUNET_NO;
  513. }
  514. /**
  515. * Method returning the hostlist element with the lowest quality in the datastore
  516. * @return hostlist with lowest quality
  517. */
  518. static struct Hostlist *
  519. linked_list_get_lowest_quality ()
  520. {
  521. struct Hostlist *pos;
  522. struct Hostlist *lowest;
  523. if (linked_list_size == 0)
  524. return NULL;
  525. lowest = linked_list_head;
  526. pos = linked_list_head->next;
  527. while (pos != NULL)
  528. {
  529. if (pos->quality < lowest->quality)
  530. lowest = pos;
  531. pos = pos->next;
  532. }
  533. return lowest;
  534. }
  535. /**
  536. * Method to insert a hostlist into the datastore. If datastore
  537. * contains maximum number of elements, the elements with lowest
  538. * quality is dismissed
  539. */
  540. static void
  541. insert_hostlist ()
  542. {
  543. struct Hostlist *lowest_quality;
  544. if (MAX_NUMBER_HOSTLISTS <= linked_list_size)
  545. {
  546. /* No free entries available, replace existing entry */
  547. lowest_quality = linked_list_get_lowest_quality ();
  548. GNUNET_assert (lowest_quality != NULL);
  549. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  550. "Removing hostlist with URI `%s' which has the worst quality of all (%llu)\n",
  551. lowest_quality->hostlist_uri,
  552. (unsigned long long) lowest_quality->quality);
  553. GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail,
  554. lowest_quality);
  555. linked_list_size--;
  556. GNUNET_free (lowest_quality);
  557. }
  558. GNUNET_CONTAINER_DLL_insert (linked_list_head, linked_list_tail,
  559. hostlist_to_test);
  560. linked_list_size++;
  561. GNUNET_STATISTICS_set (stats, gettext_noop ("# advertised hostlist URIs"),
  562. linked_list_size, GNUNET_NO);
  563. stat_testing_hostlist = GNUNET_NO;
  564. }
  565. /**
  566. * Method updating hostlist statistics
  567. */
  568. static void
  569. update_hostlist ()
  570. {
  571. char *stat;
  572. if (((stat_use_bootstrap == GNUNET_NO) && (NULL != current_hostlist)) ||
  573. ((stat_testing_hostlist == GNUNET_YES) && (NULL != current_hostlist)))
  574. {
  575. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  576. "Updating hostlist statics for URI `%s'\n",
  577. current_hostlist->hostlist_uri);
  578. current_hostlist->hello_count = stat_hellos_obtained;
  579. current_hostlist->time_last_usage = GNUNET_TIME_absolute_get ();
  580. current_hostlist->quality =
  581. checked_add (current_hostlist->quality,
  582. (stat_hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
  583. if (GNUNET_YES == stat_download_successful)
  584. {
  585. current_hostlist->times_used++;
  586. current_hostlist->quality =
  587. checked_add (current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
  588. GNUNET_asprintf (&stat, gettext_noop ("# advertised URI `%s' downloaded"),
  589. current_hostlist->hostlist_uri);
  590. GNUNET_STATISTICS_update (stats, stat, 1, GNUNET_YES);
  591. GNUNET_free (stat);
  592. }
  593. else
  594. current_hostlist->quality =
  595. checked_sub (current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD);
  596. }
  597. current_hostlist = NULL;
  598. /* Alternating the usage of preconfigured and learned hostlists */
  599. if (stat_testing_hostlist == GNUNET_YES)
  600. return;
  601. if (GNUNET_YES == stat_learning)
  602. {
  603. if (stat_use_bootstrap == GNUNET_YES)
  604. stat_use_bootstrap = GNUNET_NO;
  605. else
  606. stat_use_bootstrap = GNUNET_YES;
  607. }
  608. else
  609. stat_use_bootstrap = GNUNET_YES;
  610. }
  611. /**
  612. * Clean up the state from the task that downloaded the
  613. * hostlist and schedule the next task.
  614. */
  615. static void
  616. clean_up ()
  617. {
  618. CURLMcode mret;
  619. if ((stat_testing_hostlist == GNUNET_YES) &&
  620. (GNUNET_NO == stat_download_successful) && (NULL != hostlist_to_test))
  621. {
  622. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  623. _
  624. ("Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),
  625. hostlist_to_test->hostlist_uri);
  626. }
  627. if (stat_testing_hostlist == GNUNET_YES)
  628. {
  629. stat_testing_hostlist = GNUNET_NO;
  630. }
  631. if (NULL != hostlist_to_test)
  632. {
  633. GNUNET_free (hostlist_to_test);
  634. hostlist_to_test = NULL;
  635. }
  636. if (multi != NULL)
  637. {
  638. mret = curl_multi_remove_handle (multi, curl);
  639. if (mret != CURLM_OK)
  640. {
  641. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  642. "curl_multi_remove_handle", __FILE__, __LINE__,
  643. curl_multi_strerror (mret));
  644. }
  645. mret = curl_multi_cleanup (multi);
  646. if (mret != CURLM_OK)
  647. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  648. "curl_multi_cleanup", __FILE__, __LINE__,
  649. curl_multi_strerror (mret));
  650. multi = NULL;
  651. }
  652. if (curl != NULL)
  653. {
  654. curl_easy_cleanup (curl);
  655. curl = NULL;
  656. }
  657. GNUNET_free_non_null (current_url);
  658. current_url = NULL;
  659. stat_bytes_downloaded = 0;
  660. stat_download_in_progress = GNUNET_NO;
  661. }
  662. /**
  663. * Task that is run when we are ready to receive more data from the hostlist
  664. * server.
  665. *
  666. * @param cls closure, unused
  667. * @param tc task context, unused
  668. */
  669. static void
  670. task_download (void *cls,
  671. const struct GNUNET_SCHEDULER_TaskContext *tc);
  672. /**
  673. * Ask CURL for the select set and then schedule the
  674. * receiving task with the scheduler.
  675. */
  676. static void
  677. download_prepare ()
  678. {
  679. CURLMcode mret;
  680. fd_set rs;
  681. fd_set ws;
  682. fd_set es;
  683. int max;
  684. struct GNUNET_NETWORK_FDSet *grs;
  685. struct GNUNET_NETWORK_FDSet *gws;
  686. long timeout;
  687. struct GNUNET_TIME_Relative rtime;
  688. max = -1;
  689. FD_ZERO (&rs);
  690. FD_ZERO (&ws);
  691. FD_ZERO (&es);
  692. mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
  693. if (mret != CURLM_OK)
  694. {
  695. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  696. "curl_multi_fdset", __FILE__, __LINE__,
  697. curl_multi_strerror (mret));
  698. clean_up ();
  699. return;
  700. }
  701. mret = curl_multi_timeout (multi, &timeout);
  702. if (mret != CURLM_OK)
  703. {
  704. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  705. "curl_multi_timeout", __FILE__, __LINE__,
  706. curl_multi_strerror (mret));
  707. clean_up ();
  708. return;
  709. }
  710. rtime =
  711. GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
  712. GNUNET_TIME_relative_multiply
  713. (GNUNET_TIME_UNIT_MILLISECONDS, timeout));
  714. grs = GNUNET_NETWORK_fdset_create ();
  715. gws = GNUNET_NETWORK_fdset_create ();
  716. GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
  717. GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
  718. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  719. "Scheduling task for hostlist download using cURL\n");
  720. ti_download =
  721. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  722. rtime, grs, gws,
  723. &task_download, multi);
  724. GNUNET_NETWORK_fdset_destroy (gws);
  725. GNUNET_NETWORK_fdset_destroy (grs);
  726. }
  727. /**
  728. * Task that is run when we are ready to receive more data from the hostlist
  729. * server.
  730. *
  731. * @param cls closure, unused
  732. * @param tc task context, unused
  733. */
  734. static void
  735. task_download (void *cls,
  736. const struct GNUNET_SCHEDULER_TaskContext *tc)
  737. {
  738. int running;
  739. struct CURLMsg *msg;
  740. CURLMcode mret;
  741. ti_download = NULL;
  742. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  743. {
  744. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  745. "Shutdown requested while trying to download hostlist from `%s'\n",
  746. current_url);
  747. update_hostlist ();
  748. clean_up ();
  749. return;
  750. }
  751. if (0 == GNUNET_TIME_absolute_get_remaining (end_time).rel_value_us)
  752. {
  753. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  754. _("Timeout trying to download hostlist from `%s'\n"),
  755. current_url);
  756. update_hostlist ();
  757. clean_up ();
  758. return;
  759. }
  760. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  761. "Ready for processing hostlist client request\n");
  762. do
  763. {
  764. running = 0;
  765. if (stat_bytes_downloaded > MAX_BYTES_PER_HOSTLISTS)
  766. {
  767. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  768. _("Download limit of %u bytes exceeded, stopping download\n"),
  769. MAX_BYTES_PER_HOSTLISTS);
  770. clean_up ();
  771. return;
  772. }
  773. mret = curl_multi_perform (multi, &running);
  774. if (running == 0)
  775. {
  776. do
  777. {
  778. msg = curl_multi_info_read (multi, &running);
  779. GNUNET_break (msg != NULL);
  780. if (msg == NULL)
  781. break;
  782. switch (msg->msg)
  783. {
  784. case CURLMSG_DONE:
  785. if ((msg->data.result != CURLE_OK) &&
  786. (msg->data.result != CURLE_GOT_NOTHING))
  787. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  788. _("Download of hostlist from `%s' failed: `%s'\n"),
  789. current_url,
  790. curl_easy_strerror (msg->data.result));
  791. else
  792. {
  793. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  794. _("Download of hostlist `%s' completed.\n"),
  795. current_url);
  796. stat_download_successful = GNUNET_YES;
  797. update_hostlist ();
  798. if (GNUNET_YES == stat_testing_hostlist)
  799. {
  800. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  801. _
  802. ("Adding successfully tested hostlist `%s' datastore.\n"),
  803. current_url);
  804. insert_hostlist ();
  805. hostlist_to_test = NULL;
  806. stat_testing_hostlist = GNUNET_NO;
  807. }
  808. }
  809. clean_up ();
  810. return;
  811. default:
  812. break;
  813. }
  814. }
  815. while ((running > 0));
  816. }
  817. }
  818. while (mret == CURLM_CALL_MULTI_PERFORM);
  819. if (mret != CURLM_OK)
  820. {
  821. GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("%s failed at %s:%d: `%s'\n"),
  822. "curl_multi_perform", __FILE__, __LINE__,
  823. curl_multi_strerror (mret));
  824. clean_up ();
  825. }
  826. download_prepare ();
  827. }
  828. /**
  829. * Main function that will download a hostlist and process its
  830. * data.
  831. */
  832. static void
  833. download_hostlist ()
  834. {
  835. CURLcode ret;
  836. CURLMcode mret;
  837. current_url = download_get_url ();
  838. if (current_url == NULL)
  839. return;
  840. curl = curl_easy_init ();
  841. multi = NULL;
  842. if (curl == NULL)
  843. {
  844. GNUNET_break (0);
  845. clean_up ();
  846. return;
  847. }
  848. GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
  849. _("Bootstrapping using hostlist at `%s'.\n"), current_url);
  850. stat_download_in_progress = GNUNET_YES;
  851. stat_download_successful = GNUNET_NO;
  852. stat_hellos_obtained = 0;
  853. stat_bytes_downloaded = 0;
  854. GNUNET_STATISTICS_update (stats,
  855. gettext_noop ("# hostlist downloads initiated"), 1,
  856. GNUNET_NO);
  857. if (NULL != proxy)
  858. {
  859. CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
  860. CURL_EASY_SETOPT (curl, CURLOPT_PROXYTYPE, proxy_type);
  861. if (NULL != proxy_username)
  862. CURL_EASY_SETOPT (curl, CURLOPT_PROXYUSERNAME, proxy_username);
  863. if (NULL != proxy_password)
  864. CURL_EASY_SETOPT (curl, CURLOPT_PROXYPASSWORD, proxy_password);
  865. }
  866. download_pos = 0;
  867. stat_bogus_url = 0;
  868. CURL_EASY_SETOPT (curl, CURLOPT_WRITEFUNCTION, &callback_download);
  869. if (ret != CURLE_OK)
  870. {
  871. clean_up ();
  872. return;
  873. }
  874. CURL_EASY_SETOPT (curl, CURLOPT_WRITEDATA, NULL);
  875. if (ret != CURLE_OK)
  876. {
  877. clean_up ();
  878. return;
  879. }
  880. CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
  881. CURL_EASY_SETOPT (curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  882. CURL_EASY_SETOPT (curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  883. CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
  884. /* no need to abort if the above failed */
  885. CURL_EASY_SETOPT (curl, CURLOPT_URL, current_url);
  886. if (ret != CURLE_OK)
  887. {
  888. clean_up ();
  889. return;
  890. }
  891. CURL_EASY_SETOPT (curl, CURLOPT_FAILONERROR, 1);
  892. #if 0
  893. CURL_EASY_SETOPT (curl, CURLOPT_VERBOSE, 1);
  894. #endif
  895. CURL_EASY_SETOPT (curl, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
  896. if (0 == strncmp (current_url, "http", 4))
  897. CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
  898. CURL_EASY_SETOPT (curl, CURLOPT_CONNECTTIMEOUT, 60L);
  899. CURL_EASY_SETOPT (curl, CURLOPT_TIMEOUT, 60L);
  900. multi = curl_multi_init ();
  901. if (multi == NULL)
  902. {
  903. GNUNET_break (0);
  904. /* clean_up (); */
  905. return;
  906. }
  907. mret = curl_multi_add_handle (multi, curl);
  908. if (mret != CURLM_OK)
  909. {
  910. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  911. "curl_multi_add_handle", __FILE__, __LINE__,
  912. curl_multi_strerror (mret));
  913. mret = curl_multi_cleanup (multi);
  914. if (mret != CURLM_OK)
  915. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
  916. "curl_multi_cleanup", __FILE__, __LINE__,
  917. curl_multi_strerror (mret));
  918. multi = NULL;
  919. clean_up ();
  920. return;
  921. }
  922. end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
  923. download_prepare ();
  924. }
  925. static void
  926. task_download_dispatcher (void *cls,
  927. const struct GNUNET_SCHEDULER_TaskContext *tc)
  928. {
  929. ti_download_dispatcher_task = NULL;
  930. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  931. return;
  932. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download is initiated...\n");
  933. if (GNUNET_NO == stat_download_in_progress)
  934. {
  935. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download can start immediately...\n");
  936. download_hostlist ();
  937. }
  938. else
  939. {
  940. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  941. "Download in progess, have to wait...\n");
  942. ti_download_dispatcher_task =
  943. GNUNET_SCHEDULER_add_delayed (WAITING_INTERVAL,
  944. &task_download_dispatcher, NULL);
  945. }
  946. }
  947. /**
  948. * Task that checks if we should try to download a hostlist.
  949. * If so, we initiate the download, otherwise we schedule
  950. * this task again for a later time.
  951. */
  952. static void
  953. task_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  954. {
  955. static int once;
  956. struct GNUNET_TIME_Relative delay;
  957. ti_check_download = NULL;
  958. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  959. return;
  960. if (stats == NULL)
  961. {
  962. curl_global_cleanup ();
  963. return; /* in shutdown */
  964. }
  965. if ( (stat_connection_count < MIN_CONNECTIONS) &&
  966. (NULL == ti_download_dispatcher_task) )
  967. ti_download_dispatcher_task =
  968. GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
  969. delay = hostlist_delay;
  970. if (0 == hostlist_delay.rel_value_us)
  971. hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
  972. else
  973. hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
  974. if (hostlist_delay.rel_value_us >
  975. GNUNET_TIME_UNIT_HOURS.rel_value_us * (1 + stat_connection_count))
  976. hostlist_delay =
  977. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
  978. (1 + stat_connection_count));
  979. GNUNET_STATISTICS_set (stats,
  980. gettext_noop
  981. ("# milliseconds between hostlist downloads"),
  982. hostlist_delay.rel_value_us / 1000LL,
  983. GNUNET_YES);
  984. if (0 == once)
  985. {
  986. delay = GNUNET_TIME_UNIT_ZERO;
  987. once = 1;
  988. }
  989. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  990. _("Have %u/%u connections. Will consider downloading hostlist in %s\n"),
  991. stat_connection_count, MIN_CONNECTIONS,
  992. GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
  993. ti_check_download = GNUNET_SCHEDULER_add_delayed (delay, &task_check, NULL);
  994. }
  995. /**
  996. * This tasks sets hostlist testing to allowed after intervall between to testings is reached
  997. *
  998. * @param cls closure
  999. * @param tc TaskContext
  1000. */
  1001. static void
  1002. task_testing_intervall_reset (void *cls,
  1003. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1004. {
  1005. ti_testing_intervall_task = NULL;
  1006. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1007. return;
  1008. stat_testing_allowed = GNUNET_OK;
  1009. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1010. "Testing new hostlist advertisements is allowed again\n");
  1011. }
  1012. /**
  1013. * Task that writes hostlist entries to a file on a regular base
  1014. *
  1015. * @param cls closure
  1016. * @param tc TaskContext
  1017. */
  1018. static void
  1019. task_hostlist_saving (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  1020. {
  1021. ti_saving_task = NULL;
  1022. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1023. return;
  1024. save_hostlist_file (GNUNET_NO);
  1025. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1026. "Hostlists will be saved to file again in %s\n",
  1027. GNUNET_STRINGS_relative_time_to_string(SAVING_INTERVAL, GNUNET_YES));
  1028. ti_saving_task =
  1029. GNUNET_SCHEDULER_add_delayed (SAVING_INTERVAL, &task_hostlist_saving,
  1030. NULL);
  1031. }
  1032. /**
  1033. * Method called whenever a given peer connects.
  1034. *
  1035. * @param cls closure
  1036. * @param peer peer identity this notification is about
  1037. */
  1038. static void
  1039. handler_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
  1040. {
  1041. GNUNET_assert (stat_connection_count < UINT_MAX);
  1042. stat_connection_count++;
  1043. GNUNET_STATISTICS_update (stats, gettext_noop ("# active connections"), 1,
  1044. GNUNET_NO);
  1045. }
  1046. /**
  1047. * Method called whenever a given peer disconnects.
  1048. *
  1049. * @param cls closure
  1050. * @param peer peer identity this notification is about
  1051. */
  1052. static void
  1053. handler_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
  1054. {
  1055. GNUNET_assert (stat_connection_count > 0);
  1056. stat_connection_count--;
  1057. GNUNET_STATISTICS_update (stats, gettext_noop ("# active connections"), -1,
  1058. GNUNET_NO);
  1059. }
  1060. /**
  1061. * Method called whenever an advertisement message arrives.
  1062. *
  1063. * @param cls closure (always NULL)
  1064. * @param peer the peer sending the message
  1065. * @param message the actual message
  1066. * @return #GNUNET_OK to keep the connection open,
  1067. * #GNUNET_SYSERR to close it (signal serious error)
  1068. */
  1069. static int
  1070. handler_advertisement (void *cls, const struct GNUNET_PeerIdentity *peer,
  1071. const struct GNUNET_MessageHeader *message)
  1072. {
  1073. size_t size;
  1074. size_t uri_size;
  1075. const struct GNUNET_MessageHeader *incoming;
  1076. const char *uri;
  1077. struct Hostlist *hostlist;
  1078. GNUNET_assert (ntohs (message->type) ==
  1079. GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
  1080. size = ntohs (message->size);
  1081. if (size <= sizeof (struct GNUNET_MessageHeader))
  1082. {
  1083. GNUNET_break_op (0);
  1084. return GNUNET_SYSERR;
  1085. }
  1086. incoming = (const struct GNUNET_MessageHeader *) message;
  1087. uri = (const char *) &incoming[1];
  1088. uri_size = size - sizeof (struct GNUNET_MessageHeader);
  1089. if (uri[uri_size - 1] != '\0')
  1090. {
  1091. GNUNET_break_op (0);
  1092. return GNUNET_SYSERR;
  1093. }
  1094. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1095. "Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
  1096. GNUNET_i2s (peer), uri);
  1097. if (GNUNET_NO != linked_list_contains (uri))
  1098. {
  1099. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URI `%s' is already known\n", uri);
  1100. return GNUNET_OK;
  1101. }
  1102. if (GNUNET_NO == stat_testing_allowed)
  1103. {
  1104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1105. "Currently not accepting new advertisements: interval between to advertisements is not reached\n");
  1106. return GNUNET_SYSERR;
  1107. }
  1108. if (GNUNET_YES == stat_testing_hostlist)
  1109. {
  1110. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1111. "Currently not accepting new advertisements: we are already testing a hostlist\n");
  1112. return GNUNET_SYSERR;
  1113. }
  1114. hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
  1115. hostlist->hostlist_uri = (const char *) &hostlist[1];
  1116. memcpy (&hostlist[1], uri, uri_size);
  1117. hostlist->time_creation = GNUNET_TIME_absolute_get ();
  1118. hostlist->quality = HOSTLIST_INITIAL;
  1119. hostlist_to_test = hostlist;
  1120. stat_testing_hostlist = GNUNET_YES;
  1121. stat_testing_allowed = GNUNET_NO;
  1122. ti_testing_intervall_task =
  1123. GNUNET_SCHEDULER_add_delayed (TESTING_INTERVAL,
  1124. &task_testing_intervall_reset, NULL);
  1125. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1126. "Testing new hostlist advertisements is locked for the next %s\n",
  1127. GNUNET_STRINGS_relative_time_to_string (TESTING_INTERVAL,
  1128. GNUNET_YES));
  1129. ti_download_dispatcher_task =
  1130. GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
  1131. return GNUNET_OK;
  1132. }
  1133. /**
  1134. * Continuation called by the statistics code once
  1135. * we go the stat. Initiates hostlist download scheduling.
  1136. *
  1137. * @param cls closure
  1138. * @param success #GNUNET_OK if statistics were
  1139. * successfully obtained, #GNUNET_SYSERR if not.
  1140. */
  1141. static void
  1142. primary_task (void *cls, int success)
  1143. {
  1144. sget = NULL;
  1145. GNUNET_assert (NULL != stats);
  1146. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1147. "Statistics request done, scheduling hostlist download\n");
  1148. ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
  1149. }
  1150. /**
  1151. * We've received the previous delay value from statistics. Remember it.
  1152. *
  1153. * @param cls NULL, unused
  1154. * @param subsystem should be "hostlist", unused
  1155. * @param name will be "milliseconds between hostlist downloads", unused
  1156. * @param value previous delay value, in milliseconds (!)
  1157. * @param is_persistent unused, will be #GNUNET_YES
  1158. */
  1159. static int
  1160. process_stat (void *cls,
  1161. const char *subsystem,
  1162. const char *name,
  1163. uint64_t value,
  1164. int is_persistent)
  1165. {
  1166. hostlist_delay.rel_value_us = value * 1000LL;
  1167. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1168. "Initial time between hostlist downloads is %s\n",
  1169. GNUNET_STRINGS_relative_time_to_string (hostlist_delay,
  1170. GNUNET_YES));
  1171. return GNUNET_OK;
  1172. }
  1173. /**
  1174. * Method to load persistent hostlist file during hostlist client startup
  1175. */
  1176. static void
  1177. load_hostlist_file ()
  1178. {
  1179. char *filename;
  1180. char *uri;
  1181. char *emsg;
  1182. struct Hostlist *hostlist;
  1183. uint32_t times_used;
  1184. uint32_t hellos_returned;
  1185. uint64_t quality;
  1186. uint64_t last_used;
  1187. uint64_t created;
  1188. uint32_t counter;
  1189. struct GNUNET_BIO_ReadHandle *rh;
  1190. uri = NULL;
  1191. if (GNUNET_OK !=
  1192. GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST", "HOSTLISTFILE",
  1193. &filename))
  1194. {
  1195. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
  1196. "hostlist", "HOSTLISTFILE");
  1197. return;
  1198. }
  1199. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1200. _("Loading saved hostlist entries from file `%s' \n"),
  1201. filename);
  1202. if (GNUNET_NO == GNUNET_DISK_file_test (filename))
  1203. {
  1204. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1205. _("Hostlist file `%s' does not exist\n"), filename);
  1206. GNUNET_free (filename);
  1207. return;
  1208. }
  1209. rh = GNUNET_BIO_read_open (filename);
  1210. if (NULL == rh)
  1211. {
  1212. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1213. _("Could not open file `%s' for reading to load hostlists: %s\n"),
  1214. filename, STRERROR (errno));
  1215. GNUNET_free (filename);
  1216. return;
  1217. }
  1218. counter = 0;
  1219. while ((GNUNET_OK == GNUNET_BIO_read_string (rh, "url", &uri, MAX_URL_LEN)) &&
  1220. (NULL != uri) && (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &times_used))
  1221. && (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
  1222. (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
  1223. (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
  1224. (GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)))
  1225. {
  1226. hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
  1227. hostlist->hello_count = hellos_returned;
  1228. hostlist->hostlist_uri = (const char *) &hostlist[1];
  1229. memcpy (&hostlist[1], uri, strlen (uri) + 1);
  1230. hostlist->quality = quality;
  1231. hostlist->time_creation.abs_value_us = created;
  1232. hostlist->time_last_usage.abs_value_us = last_used;
  1233. GNUNET_CONTAINER_DLL_insert (linked_list_head, linked_list_tail, hostlist);
  1234. linked_list_size++;
  1235. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1236. "Added hostlist entry eith URI `%s' \n",
  1237. hostlist->hostlist_uri);
  1238. GNUNET_free (uri);
  1239. uri = NULL;
  1240. counter++;
  1241. if (counter >= MAX_NUMBER_HOSTLISTS)
  1242. break;
  1243. }
  1244. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1245. _("%u hostlist URIs loaded from file\n"),
  1246. counter);
  1247. GNUNET_STATISTICS_set (stats,
  1248. gettext_noop ("# hostlist URIs read from file"),
  1249. counter, GNUNET_YES);
  1250. GNUNET_STATISTICS_set (stats,
  1251. gettext_noop ("# advertised hostlist URIs"),
  1252. linked_list_size, GNUNET_NO);
  1253. GNUNET_free_non_null (uri);
  1254. emsg = NULL;
  1255. (void) GNUNET_BIO_read_close (rh, &emsg);
  1256. if (emsg != NULL)
  1257. GNUNET_free (emsg);
  1258. GNUNET_free (filename);
  1259. }
  1260. /**
  1261. * Method to save persistent hostlist file during hostlist client shutdown
  1262. *
  1263. * @param shutdown set if called because of shutdown, entries in linked list will be destroyed
  1264. */
  1265. static void
  1266. save_hostlist_file (int shutdown)
  1267. {
  1268. char *filename;
  1269. struct Hostlist *pos;
  1270. struct GNUNET_BIO_WriteHandle *wh;
  1271. int ok;
  1272. uint32_t counter;
  1273. if (GNUNET_OK !=
  1274. GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST", "HOSTLISTFILE",
  1275. &filename))
  1276. {
  1277. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
  1278. "hostlist", "HOSTLISTFILE");
  1279. return;
  1280. }
  1281. if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
  1282. {
  1283. GNUNET_free (filename);
  1284. return;
  1285. }
  1286. wh = GNUNET_BIO_write_open (filename);
  1287. if (NULL == wh)
  1288. {
  1289. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1290. _("Could not open file `%s' for writing to save hostlists: %s\n"),
  1291. filename,
  1292. STRERROR (errno));
  1293. GNUNET_free (filename);
  1294. return;
  1295. }
  1296. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1297. _("Writing %u hostlist URIs to `%s'\n"),
  1298. linked_list_size, filename);
  1299. /* add code to write hostlists to file using bio */
  1300. ok = GNUNET_YES;
  1301. counter = 0;
  1302. while (NULL != (pos = linked_list_head))
  1303. {
  1304. if (GNUNET_YES == shutdown)
  1305. {
  1306. GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
  1307. linked_list_size--;
  1308. }
  1309. if (GNUNET_YES == ok)
  1310. {
  1311. if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
  1312. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
  1313. (GNUNET_OK != GNUNET_BIO_write_int64 (wh, pos->quality)) ||
  1314. (GNUNET_OK !=
  1315. GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value_us)) ||
  1316. (GNUNET_OK !=
  1317. GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value_us)) ||
  1318. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->hello_count)))
  1319. {
  1320. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1321. _("Error writing hostlist URIs to file `%s'\n"), filename);
  1322. ok = GNUNET_NO;
  1323. }
  1324. }
  1325. if (GNUNET_YES == shutdown)
  1326. GNUNET_free (pos);
  1327. counter++;
  1328. if (counter >= MAX_NUMBER_HOSTLISTS)
  1329. break;
  1330. }
  1331. GNUNET_STATISTICS_set (stats,
  1332. gettext_noop ("# hostlist URIs written to file"),
  1333. counter, GNUNET_YES);
  1334. if (GNUNET_OK != GNUNET_BIO_write_close (wh))
  1335. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1336. _("Error writing hostlist URIs to file `%s'\n"), filename);
  1337. GNUNET_free (filename);
  1338. }
  1339. /**
  1340. * Start downloading hostlists from hostlist servers as necessary.
  1341. *
  1342. * @param c configuration to use
  1343. * @param st statistics handle to use
  1344. * @param[out] ch set to handler for CORE connect events
  1345. * @param[out] dh set to handler for CORE disconnect events
  1346. * @param[out] msgh set to handler for CORE advertisement messages
  1347. * @param learn should we learn hostlist URLs from CORE
  1348. * @return #GNUNET_OK on success
  1349. */
  1350. int
  1351. GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
  1352. struct GNUNET_STATISTICS_Handle *st,
  1353. GNUNET_CORE_ConnectEventHandler *ch,
  1354. GNUNET_CORE_DisconnectEventHandler *dh,
  1355. GNUNET_CORE_MessageCallback *msgh,
  1356. int learn)
  1357. {
  1358. char *filename;
  1359. char *proxytype_str;
  1360. int result;
  1361. GNUNET_assert (NULL != st);
  1362. if (0 != curl_global_init (CURL_GLOBAL_WIN32))
  1363. {
  1364. GNUNET_break (0);
  1365. return GNUNET_SYSERR;
  1366. }
  1367. transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
  1368. if (NULL == transport)
  1369. {
  1370. GNUNET_break (0);
  1371. curl_global_cleanup ();
  1372. return GNUNET_SYSERR;
  1373. }
  1374. cfg = c;
  1375. stats = st;
  1376. /* Read proxy configuration */
  1377. if (GNUNET_OK ==
  1378. GNUNET_CONFIGURATION_get_value_string (cfg,
  1379. "HOSTLIST", "PROXY", &proxy))
  1380. {
  1381. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1382. "Found proxy host: `%s'\n",
  1383. proxy);
  1384. /* proxy username */
  1385. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
  1386. "HOSTLIST", "PROXY_USERNAME", &proxy_username))
  1387. {
  1388. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1389. "Found proxy username name: `%s'\n",
  1390. proxy_username);
  1391. }
  1392. /* proxy password */
  1393. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
  1394. "HOSTLIST", "PROXY_PASSWORD", &proxy_password))
  1395. {
  1396. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1397. "Found proxy password name: `%s'\n",
  1398. proxy_password);
  1399. }
  1400. /* proxy type */
  1401. if (GNUNET_OK ==
  1402. GNUNET_CONFIGURATION_get_value_string (cfg,
  1403. "HOSTLIST",
  1404. "PROXY_TYPE",
  1405. &proxytype_str))
  1406. {
  1407. GNUNET_STRINGS_utf8_toupper (proxytype_str,
  1408. proxytype_str);
  1409. proxy_type = CURLPROXY_HTTP;
  1410. if (0 == strcmp (proxytype_str, "HTTP"))
  1411. proxy_type = CURLPROXY_HTTP;
  1412. else if (0 == strcmp (proxytype_str, "HTTP_1_0"))
  1413. proxy_type = CURLPROXY_HTTP_1_0;
  1414. else if (0 == strcmp (proxytype_str, "SOCKS4"))
  1415. proxy_type = CURLPROXY_SOCKS4;
  1416. else if (0 == strcmp (proxytype_str, "SOCKS5"))
  1417. proxy_type = CURLPROXY_SOCKS5;
  1418. else if (0 == strcmp (proxytype_str, "SOCKS4A"))
  1419. proxy_type = CURLPROXY_SOCKS4A;
  1420. else if (0 == strcmp (proxytype_str, "SOCKS5_HOSTNAME"))
  1421. proxy_type = CURLPROXY_SOCKS5_HOSTNAME;
  1422. else
  1423. {
  1424. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1425. _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
  1426. proxytype_str);
  1427. GNUNET_free (proxytype_str);
  1428. GNUNET_free (proxy);
  1429. proxy = NULL;
  1430. GNUNET_free_non_null (proxy_username);
  1431. proxy_username = NULL;
  1432. GNUNET_free_non_null (proxy_password);
  1433. proxy_password = NULL;
  1434. return GNUNET_SYSERR;
  1435. }
  1436. }
  1437. GNUNET_free_non_null (proxytype_str);
  1438. }
  1439. stat_learning = learn;
  1440. *ch = &handler_connect;
  1441. *dh = &handler_disconnect;
  1442. linked_list_head = NULL;
  1443. linked_list_tail = NULL;
  1444. stat_use_bootstrap = GNUNET_YES;
  1445. stat_testing_hostlist = GNUNET_NO;
  1446. stat_testing_allowed = GNUNET_YES;
  1447. if (GNUNET_YES == stat_learning)
  1448. {
  1449. *msgh = &handler_advertisement;
  1450. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1451. _("Learning is enabled on this peer\n"));
  1452. load_hostlist_file ();
  1453. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1454. "Hostlists will be saved to file again in %s\n",
  1455. GNUNET_STRINGS_relative_time_to_string (SAVING_INTERVAL, GNUNET_YES));
  1456. ti_saving_task =
  1457. GNUNET_SCHEDULER_add_delayed (SAVING_INTERVAL,
  1458. &task_hostlist_saving,
  1459. NULL);
  1460. }
  1461. else
  1462. {
  1463. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1464. _("Learning is not enabled on this peer\n"));
  1465. *msgh = NULL;
  1466. if (GNUNET_OK ==
  1467. GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST",
  1468. "HOSTLISTFILE", &filename))
  1469. {
  1470. if (GNUNET_YES == GNUNET_DISK_file_test (filename))
  1471. {
  1472. result = remove (filename);
  1473. if (0 == result)
  1474. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1475. _("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),
  1476. filename);
  1477. else
  1478. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
  1479. "remove",
  1480. filename);
  1481. }
  1482. }
  1483. GNUNET_free (filename);
  1484. }
  1485. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1486. "Loading stats value on hostlist download frequency\n");
  1487. sget = GNUNET_STATISTICS_get (stats, "hostlist",
  1488. gettext_noop
  1489. ("# milliseconds between hostlist downloads"),
  1490. GNUNET_TIME_UNIT_MINUTES,
  1491. &primary_task,
  1492. &process_stat,
  1493. NULL);
  1494. if (NULL == sget)
  1495. {
  1496. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1497. "Statistics request failed, scheduling hostlist download\n");
  1498. ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
  1499. }
  1500. return GNUNET_OK;
  1501. }
  1502. /**
  1503. * Stop downloading hostlists from hostlist servers as necessary.
  1504. */
  1505. void
  1506. GNUNET_HOSTLIST_client_stop ()
  1507. {
  1508. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist client shutdown\n");
  1509. if (NULL != sget)
  1510. {
  1511. GNUNET_STATISTICS_get_cancel (sget);
  1512. sget = NULL;
  1513. }
  1514. stats = NULL;
  1515. if (GNUNET_YES == stat_learning)
  1516. save_hostlist_file (GNUNET_YES);
  1517. if (ti_saving_task != NULL)
  1518. {
  1519. GNUNET_SCHEDULER_cancel (ti_saving_task);
  1520. ti_saving_task = NULL;
  1521. }
  1522. if (ti_download_dispatcher_task != NULL)
  1523. {
  1524. GNUNET_SCHEDULER_cancel (ti_download_dispatcher_task);
  1525. ti_download_dispatcher_task = NULL;
  1526. }
  1527. if (ti_testing_intervall_task != NULL)
  1528. {
  1529. GNUNET_SCHEDULER_cancel (ti_testing_intervall_task);
  1530. ti_testing_intervall_task = NULL;
  1531. }
  1532. if (ti_download != NULL)
  1533. {
  1534. GNUNET_SCHEDULER_cancel (ti_download);
  1535. ti_download = NULL;
  1536. }
  1537. if (ti_check_download != NULL)
  1538. {
  1539. GNUNET_SCHEDULER_cancel (ti_check_download);
  1540. ti_check_download = NULL;
  1541. curl_global_cleanup ();
  1542. }
  1543. if (NULL != transport)
  1544. {
  1545. GNUNET_TRANSPORT_disconnect (transport);
  1546. transport = NULL;
  1547. }
  1548. GNUNET_free_non_null (proxy);
  1549. proxy = NULL;
  1550. GNUNET_free_non_null (proxy_username);
  1551. proxy_username = NULL;
  1552. GNUNET_free_non_null (proxy_password);
  1553. proxy_password = NULL;
  1554. cfg = NULL;
  1555. }
  1556. /* end of gnunet-daemon-hostlist_client.c */