generate-underlay-topology.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/generate-underlay-topology.c
  18. * @brief Program to generate a database file containing given underlay topology
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. #include "testbed_api_topology.h"
  25. #include "sqlite3.h"
  26. #define LOG(type, ...) GNUNET_log (type, __VA_ARGS__)
  27. #define LOG_ERROR(...) LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
  28. /**
  29. * Log an error message at log-level 'level' that indicates
  30. * a failure of the command 'cmd' on file 'filename'
  31. * with the message given by strerror(errno).
  32. */
  33. #define LOG_SQLITE(db, msg, level, cmd) \
  34. do \
  35. { \
  36. GNUNET_log_from (level, \
  37. "sqlite", \
  38. _ ("`%s' failed at %s:%d with error: %s\n"), \
  39. cmd, \
  40. __FILE__, \
  41. __LINE__, \
  42. sqlite3_errmsg (db)); \
  43. if (msg != NULL) \
  44. GNUNET_asprintf (msg, \
  45. _ ("`%s' failed at %s:%u with error: %s"), \
  46. cmd, \
  47. __FILE__, \
  48. __LINE__, \
  49. sqlite3_errmsg (db)); \
  50. } while (0)
  51. /**
  52. * Handle to the sqlite3 database
  53. */
  54. static struct sqlite3 *db;
  55. /**
  56. * Prepared statement for inserting link values into db
  57. */
  58. struct sqlite3_stmt *stmt_insert;
  59. /**
  60. * The topology to generate
  61. */
  62. enum GNUNET_TESTBED_TopologyOption topology;
  63. /**
  64. * The number of peers to include in the topology
  65. */
  66. static unsigned int num_peers;
  67. /**
  68. * program result
  69. */
  70. static int exit_result;
  71. /**
  72. * Functions of this type are called to process underlay link
  73. *
  74. * @param cls closure
  75. * @param A offset of first peer
  76. * @param B offset of second peer
  77. * @param bandwidth the bandwidth of the link in bytes per second
  78. * @param latency the latency of link in milliseconds
  79. * @param loss the percentage of messages dropped on the link
  80. * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
  81. */
  82. static int
  83. link_processor (void *cls,
  84. unsigned int A,
  85. unsigned int B,
  86. unsigned int bandwidth,
  87. unsigned int latency,
  88. unsigned int loss)
  89. {
  90. if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
  91. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
  92. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
  93. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
  94. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)))
  95. {
  96. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
  97. return GNUNET_SYSERR;
  98. }
  99. if (SQLITE_DONE != sqlite3_step (stmt_insert))
  100. {
  101. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
  102. return GNUNET_SYSERR;
  103. }
  104. fprintf (stdout, "%u -> %u\n", A, B);
  105. GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
  106. // GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
  107. if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) ||
  108. (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)))
  109. {
  110. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
  111. return GNUNET_SYSERR;
  112. }
  113. if (SQLITE_DONE != sqlite3_step (stmt_insert))
  114. {
  115. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
  116. return GNUNET_SYSERR;
  117. }
  118. fprintf (stdout, "%u -> %u\n", B, A);
  119. GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
  120. return GNUNET_OK;
  121. }
  122. /**
  123. * Open the database file, creating a new database if not existing and setup the
  124. * whitelist table
  125. *
  126. * @param dbfile the database filename
  127. * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
  128. * to be printed)
  129. */
  130. static int
  131. setup_db (const char *dbfile)
  132. {
  133. const char *query_create = "CREATE TABLE whitelist ("
  134. "id INTEGER,"
  135. "oid INTEGER,"
  136. "bandwidth INTEGER DEFAULT NULL,"
  137. "latency INTEGER DEFAULT NULL,"
  138. "loss INTEGER DEFAULT NULL,"
  139. " UNIQUE ("
  140. " id,"
  141. " oid"
  142. " ) ON CONFLICT IGNORE"
  143. ");";
  144. const char *query_insert = "INSERT INTO whitelist("
  145. " id,"
  146. " oid,"
  147. " bandwidth,"
  148. " latency,"
  149. " loss"
  150. ") VALUES ("
  151. " ?1,"
  152. " ?2,"
  153. " ?3,"
  154. " ?4,"
  155. " ?5);";
  156. int ret;
  157. ret = GNUNET_SYSERR;
  158. if (SQLITE_OK != sqlite3_open (dbfile, &db))
  159. {
  160. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
  161. goto err_ret;
  162. }
  163. if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
  164. {
  165. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
  166. fprintf (stderr,
  167. "Error: %d. Perhaps the database `%s' already exits.\n",
  168. sqlite3_errcode (db),
  169. dbfile);
  170. goto err_ret;
  171. }
  172. GNUNET_break (0 ==
  173. sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
  174. if (SQLITE_OK !=
  175. sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert, NULL))
  176. {
  177. LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
  178. goto err_ret;
  179. }
  180. ret = GNUNET_OK;
  181. err_ret:
  182. return ret;
  183. }
  184. /**
  185. * Main run function.
  186. *
  187. * @param cls NULL
  188. * @param args arguments passed to GNUNET_PROGRAM_run
  189. * @param cfgfile the path to configuration file
  190. * @param cfg the configuration file handle
  191. */
  192. static void
  193. run (void *cls,
  194. char *const *args,
  195. const char *cfgfile,
  196. const struct GNUNET_CONFIGURATION_Handle *config)
  197. {
  198. const char *dbfile;
  199. const char *topology_string;
  200. unsigned int arg_uint1;
  201. unsigned int arg_uint2;
  202. const char *arg_str1;
  203. const char *value;
  204. unsigned int argc;
  205. argc = 0;
  206. arg_uint1 = 0; /* make compilers happy */
  207. arg_uint2 = 0; /* make compilers happy */
  208. if (NULL == args)
  209. {
  210. LOG_ERROR (_ ("Need at least 2 arguments\n"));
  211. return;
  212. }
  213. if (NULL == (dbfile = args[argc++]))
  214. {
  215. LOG_ERROR (_ ("Database filename missing\n"));
  216. return;
  217. }
  218. if (GNUNET_OK != setup_db (dbfile))
  219. return;
  220. if (NULL == (topology_string = args[argc++]))
  221. {
  222. LOG_ERROR (_ ("Topology string missing\n"));
  223. return;
  224. }
  225. if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string))
  226. {
  227. LOG_ERROR (_ ("Invalid topology: %s\n"), topology_string);
  228. return;
  229. }
  230. arg_str1 = NULL;
  231. /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
  232. switch (topology)
  233. {
  234. case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
  235. case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
  236. case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
  237. case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
  238. if (NULL == (value = args[argc++]))
  239. {
  240. LOG_ERROR (_ ("An argument is missing for given topology `%s'\n"),
  241. topology_string);
  242. return;
  243. }
  244. if (-1 == sscanf (value, "%u", &arg_uint1))
  245. {
  246. LOG_ERROR (_ ("Invalid argument `%s' given as topology argument\n"),
  247. value);
  248. return;
  249. }
  250. break;
  251. case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
  252. if (NULL == (arg_str1 = args[argc++]))
  253. {
  254. LOG_ERROR (_ ("Filename argument missing for topology `%s'\n"),
  255. topology_string);
  256. return;
  257. }
  258. break;
  259. default:
  260. break;
  261. }
  262. /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
  263. switch (topology)
  264. {
  265. case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
  266. if (NULL == (value = args[argc++]))
  267. {
  268. LOG_ERROR (_ ("Second argument for topology `%s' is missing\n"),
  269. topology_string);
  270. return;
  271. }
  272. if (-1 == sscanf (value, "%u", &arg_uint2))
  273. {
  274. LOG_ERROR (_ ("Invalid argument `%s'; expecting unsigned int\n"), value);
  275. return;
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. /* contruct topologies */
  282. switch (topology)
  283. {
  284. case GNUNET_TESTBED_TOPOLOGY_LINE:
  285. case GNUNET_TESTBED_TOPOLOGY_RING:
  286. case GNUNET_TESTBED_TOPOLOGY_STAR:
  287. case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
  288. case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
  289. GNUNET_TESTBED_underlay_construct_ (num_peers,
  290. link_processor,
  291. NULL,
  292. topology);
  293. break;
  294. case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
  295. case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
  296. case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
  297. GNUNET_TESTBED_underlay_construct_ (num_peers,
  298. link_processor,
  299. NULL,
  300. topology,
  301. arg_uint1);
  302. break;
  303. case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
  304. GNUNET_TESTBED_underlay_construct_ (num_peers,
  305. link_processor,
  306. NULL,
  307. topology,
  308. arg_str1);
  309. break;
  310. case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
  311. GNUNET_TESTBED_underlay_construct_ (num_peers,
  312. link_processor,
  313. NULL,
  314. topology,
  315. arg_uint1,
  316. arg_uint2);
  317. break;
  318. default:
  319. GNUNET_assert (0);
  320. }
  321. }
  322. /**
  323. * Main
  324. */
  325. int
  326. main (int argc, char *const argv[])
  327. {
  328. struct GNUNET_GETOPT_CommandLineOption option[] = {
  329. GNUNET_GETOPT_option_uint ('p',
  330. "num-peers",
  331. "COUNT",
  332. gettext_noop ("create COUNT number of peers"),
  333. &num_peers),
  334. GNUNET_GETOPT_OPTION_END
  335. };
  336. int ret;
  337. exit_result = GNUNET_SYSERR;
  338. ret = GNUNET_PROGRAM_run (
  339. argc,
  340. argv,
  341. "gnunet-underlay-topology",
  342. _ (
  343. "Generates SQLite3 database representing a given underlay topology.\n"
  344. "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
  345. "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
  346. "\t LINE\n"
  347. "\t RING\n"
  348. "\t RANDOM <num_rnd_links>\n"
  349. "\t SMALL_WORLD <num_rnd_links>\n"
  350. "\t SMALL_WORLD_RING <num_rnd_links>\n"
  351. "\t CLIQUE\n"
  352. "\t 2D_TORUS\n"
  353. "\t SCALE_FREE <cap> <m>\n"
  354. "\t FROM_FILE <filename>\n"
  355. "TOPOOPTS:\n"
  356. "\t num_rnd_links: The number of random links\n"
  357. "\t cap: the maximum number of links a node can have\n"
  358. "\t m: the number of links a node should have while joining the network\n"
  359. "\t filename: the path of the file which contains topology information\n"
  360. "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
  361. option,
  362. &run,
  363. NULL);
  364. if (NULL != stmt_insert)
  365. sqlite3_finalize (stmt_insert);
  366. if (NULL != db)
  367. GNUNET_break (SQLITE_OK == sqlite3_close (db));
  368. if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))
  369. return 1;
  370. return 0;
  371. }