gnunet-daemon-latency-logger.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2014 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 testbed/gnunet-daemon-latency-logger.c
  18. * @brief log latency values from neighbour connections into an SQLite database
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_ats_service.h"
  24. #include <sqlite3.h>
  25. /**
  26. * Logging shorthand
  27. */
  28. #define LOG(type,...) \
  29. GNUNET_log (type, __VA_ARGS__)
  30. /**
  31. * Debug logging shorthand
  32. */
  33. #define DEBUG(...) \
  34. LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
  35. /**
  36. * Log an error message at log-level 'level' that indicates
  37. * a failure of the command 'cmd' on file 'filename'
  38. * with the message given by strerror(errno).
  39. */
  40. #define LOG_SQLITE(db, msg, level, cmd) \
  41. do { \
  42. GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
  43. cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
  44. if (msg != NULL) \
  45. GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
  46. __FILE__, __LINE__, sqlite3_errmsg(db)); \
  47. } while(0)
  48. /**
  49. * Entry type to be used in the map to store old latency values
  50. */
  51. struct Entry
  52. {
  53. /**
  54. * The peer's identity
  55. */
  56. struct GNUNET_PeerIdentity id;
  57. /**
  58. * The last known value for latency.
  59. * FIXME: type!
  60. */
  61. unsigned int latency;
  62. };
  63. /**
  64. * Handle to the map used to store old latency values for peers
  65. */
  66. static struct GNUNET_CONTAINER_MultiPeerMap *map;
  67. /**
  68. * The SQLite database handle
  69. */
  70. static struct sqlite3 *db;
  71. /**
  72. * Handle to the ATS performance subsystem
  73. */
  74. static struct GNUNET_ATS_PerformanceHandle *ats;
  75. /**
  76. * Prepared statement for inserting values into the database table
  77. */
  78. static struct sqlite3_stmt *stmt_insert;
  79. /**
  80. * @ingroup hashmap
  81. * Iterator over hash map entries.
  82. *
  83. * @param cls closure
  84. * @param key current public key
  85. * @param value value in the hash map
  86. * @return #GNUNET_YES if we should continue to
  87. * iterate,
  88. * #GNUNET_NO if not.
  89. */
  90. static int
  91. free_iterator (void *cls,
  92. const struct GNUNET_PeerIdentity *key,
  93. void *value)
  94. {
  95. struct Entry *e = cls;
  96. GNUNET_assert (GNUNET_YES ==
  97. GNUNET_CONTAINER_multipeermap_remove (map, key, e));
  98. GNUNET_free (e);
  99. return GNUNET_YES;
  100. }
  101. /**
  102. * Shutdown
  103. *
  104. * @param cls NULL
  105. * @return
  106. */
  107. static void
  108. do_shutdown (void *cls)
  109. {
  110. GNUNET_ATS_performance_done (ats);
  111. ats = NULL;
  112. if (NULL != stmt_insert)
  113. {
  114. sqlite3_finalize (stmt_insert);
  115. stmt_insert = NULL;
  116. }
  117. GNUNET_break (SQLITE_OK == sqlite3_close (db));
  118. db = NULL;
  119. if (NULL != map)
  120. {
  121. GNUNET_assert (GNUNET_SYSERR !=
  122. GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator, NULL));
  123. GNUNET_CONTAINER_multipeermap_destroy (map);
  124. map = NULL;
  125. }
  126. }
  127. /**
  128. * Signature of a function that is called with QoS information about an address.
  129. *
  130. * @param cls closure
  131. * @param address the address
  132. * @param address_active #GNUNET_YES if this address is actively used
  133. * to maintain a connection to a peer;
  134. * #GNUNET_NO if the address is not actively used;
  135. * #GNUNET_SYSERR if this address is no longer available for ATS
  136. * @param bandwidth_out assigned outbound bandwidth for the connection
  137. * @param bandwidth_in assigned inbound bandwidth for the connection
  138. * @param prop performance data for the address (as far as known)
  139. */
  140. static void
  141. addr_info_cb (void *cls,
  142. const struct GNUNET_HELLO_Address *address,
  143. int address_active,
  144. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  145. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  146. const struct GNUNET_ATS_Properties *prop)
  147. {
  148. static const char *query_insert =
  149. "INSERT INTO ats_info("
  150. " id,"
  151. " val,"
  152. " timestamp"
  153. ") VALUES ("
  154. " ?1,"
  155. " ?2,"
  156. " datetime('now')"
  157. ");";
  158. struct Entry *entry;
  159. int latency; /* FIXME: type!? */
  160. if (NULL == address)
  161. {
  162. /* ATS service temporarily disconnected */
  163. return;
  164. }
  165. GNUNET_assert (NULL != db);
  166. if (GNUNET_YES != address_active)
  167. return;
  168. latency = (int) prop->delay.rel_value_us;
  169. entry = NULL;
  170. if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map,
  171. &address->peer))
  172. {
  173. entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
  174. GNUNET_assert (NULL != entry);
  175. if (latency == entry->latency)
  176. return;
  177. }
  178. if (NULL == stmt_insert)
  179. {
  180. if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
  181. NULL))
  182. {
  183. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
  184. goto err_shutdown;
  185. }
  186. }
  187. if ( (SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
  188. GNUNET_i2s (&address->peer), -1,
  189. SQLITE_STATIC)) ||
  190. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)) )
  191. {
  192. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
  193. goto err_shutdown;
  194. }
  195. if (SQLITE_DONE != sqlite3_step (stmt_insert))
  196. {
  197. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
  198. goto err_shutdown;
  199. }
  200. if (SQLITE_OK != sqlite3_reset (stmt_insert))
  201. {
  202. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
  203. goto err_shutdown;
  204. }
  205. if (NULL == entry)
  206. {
  207. entry = GNUNET_new (struct Entry);
  208. entry->id = address->peer;
  209. GNUNET_CONTAINER_multipeermap_put (map,
  210. &entry->id, entry,
  211. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  212. }
  213. entry->latency = latency;
  214. return;
  215. err_shutdown:
  216. GNUNET_SCHEDULER_shutdown ();
  217. }
  218. /**
  219. * Main function that will be run.
  220. *
  221. * @param cls closure
  222. * @param args remaining command-line arguments
  223. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  224. * @param c configuration
  225. */
  226. static void
  227. run (void *cls, char *const *args, const char *cfgfile,
  228. const struct GNUNET_CONFIGURATION_Handle *c)
  229. {
  230. const char *query_create =
  231. "CREATE TABLE ats_info ("
  232. "id TEXT,"
  233. "val INTEGER,"
  234. "timestamp NUMERIC"
  235. ");";
  236. char *dbfile;
  237. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
  238. "DBFILE",
  239. &dbfile))
  240. {
  241. GNUNET_break (0);
  242. return;
  243. }
  244. if (SQLITE_OK != sqlite3_open (dbfile, &db))
  245. {
  246. if (NULL != db)
  247. {
  248. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
  249. GNUNET_break (SQLITE_OK == sqlite3_close (db));
  250. }
  251. else
  252. LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
  253. GNUNET_free (dbfile);
  254. return;
  255. }
  256. if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
  257. DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n",
  258. sqlite3_errcode (db), dbfile);
  259. DEBUG ("Opened database %s\n", dbfile);
  260. GNUNET_free (dbfile);
  261. dbfile = NULL;
  262. ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL);
  263. map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES);
  264. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  265. }
  266. /**
  267. * Execution entry point
  268. */
  269. int
  270. main (int argc, char * const *argv)
  271. {
  272. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  273. GNUNET_GETOPT_OPTION_END
  274. };
  275. int ret;
  276. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  277. return 2;
  278. ret =
  279. (GNUNET_OK ==
  280. GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
  281. _("Daemon to log latency values of connections to neighbours"),
  282. options, &run, NULL)) ? 0 : 1;
  283. GNUNET_free ((void*) argv);
  284. return ret;
  285. }