2 This file is part of GNUnet
3 (C) 2008--2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/generate-underlay-topology.c
23 * @brief Program to generate a database file containing given underlay topology
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_topology.h"
33 #define LOG(type, ...) \
34 GNUNET_log (type, __VA_ARGS__)
37 #define LOG_ERROR(...) \
38 LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
41 * Log an error message at log-level 'level' that indicates
42 * a failure of the command 'cmd' on file 'filename'
43 * with the message given by strerror(errno).
45 #define LOG_SQLITE(db, msg, level, cmd) \
47 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
50 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
51 __FILE__, __LINE__, sqlite3_errmsg(db)); \
56 * Handle to the sqlite3 database
58 static struct sqlite3 *db;
61 * Prepared statement for inserting link values into db
63 struct sqlite3_stmt *stmt_insert;
66 * The topology to generate
68 enum GNUNET_TESTBED_TopologyOption topology;
71 * The number of peers to include in the topology
78 static int exit_result;
82 * Functions of this type are called to process underlay link
85 * @param A offset of first peer
86 * @param B offset of second peer
87 * @param bandwidth the bandwidth of the link in bytes per second
88 * @param latency the latency of link in milliseconds
89 * @param loss the percentage of messages dropped on the link
90 * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
93 link_processor (void *cls,
96 unsigned int bandwidth,
100 if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
101 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
102 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
103 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
104 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)) )
106 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
107 return GNUNET_SYSERR;
109 if (SQLITE_DONE != sqlite3_step (stmt_insert))
111 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
112 return GNUNET_SYSERR;
114 FPRINTF (stdout, "%u -> %u\n", A, B);
115 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
116 //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
117 if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) ||
118 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)) )
120 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
121 return GNUNET_SYSERR;
123 if (SQLITE_DONE != sqlite3_step (stmt_insert))
125 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
126 return GNUNET_SYSERR;
128 FPRINTF (stdout, "%u -> %u\n", B, A);
129 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
135 * Open the database file, creating a new database if not existing and setup the
138 * @param dbfile the database filename
139 * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
143 setup_db (const char *dbfile)
145 const char *query_create =
146 "CREATE TABLE whitelist ("
149 "bandwidth INTEGER DEFAULT NULL,"
150 "latency INTEGER DEFAULT NULL,"
151 "loss INTEGER DEFAULT NULL,"
155 " ) ON CONFLICT IGNORE"
157 const char *query_insert =
158 "INSERT INTO whitelist("
173 if (SQLITE_OK != sqlite3_open (dbfile, &db))
175 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
178 if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
180 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
181 FPRINTF (stderr, "Error: %d. Perhaps the database `%s' already exits.\n",
182 sqlite3_errcode (db),
186 GNUNET_break (0 == sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
187 if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1,
190 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
204 * @param args arguments passed to GNUNET_PROGRAM_run
205 * @param cfgfile the path to configuration file
206 * @param cfg the configuration file handle
209 run (void *cls, char *const *args, const char *cfgfile,
210 const struct GNUNET_CONFIGURATION_Handle *config)
213 const char *topology_string;
214 unsigned int arg_uint1;
215 unsigned int arg_uint2;
216 const char *arg_str1;
223 LOG_ERROR (_("Need atleast 2 arguments\n"));
226 if (NULL == (dbfile = args[argc++]))
228 LOG_ERROR (_("Database filename missing\n"));
231 if (GNUNET_OK != setup_db (dbfile))
233 if (NULL == (topology_string = args[argc++]))
235 LOG_ERROR (_("Topology string missing\n"));
238 if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string))
240 LOG_ERROR (_("Invalid topology: %s\n"), topology_string);
243 /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
246 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
247 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
248 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
249 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
250 if (NULL == (value = args[argc++]))
252 LOG_ERROR (_("An argument is missing for given topology `%s'\n"),
256 if (-1 == SSCANF (value, "%u", &arg_uint1))
258 LOG_ERROR (_("Invalid argument `%s' given as topology argument\n"),
263 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
264 if (NULL == (arg_str1 = args[argc++]))
266 LOG_ERROR (_("Filename argument missing for topology `%s'\n"),
274 /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
277 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
278 if (NULL == (value = args[argc++]))
280 LOG_ERROR (_("Second argument for topology `%s' is missing\n"),
284 if (-1 == SSCANF (value, "%u", &arg_uint2))
286 LOG_ERROR (_("Invalid argument `%s'; expecting unsigned int\n"), value);
293 /* contruct topologies */
296 case GNUNET_TESTBED_TOPOLOGY_LINE:
297 case GNUNET_TESTBED_TOPOLOGY_RING:
298 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
299 case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
300 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
303 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
304 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
305 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
306 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
310 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
311 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
315 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
316 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
331 main (int argc, char *const argv[])
333 struct GNUNET_GETOPT_CommandLineOption option[] = {
334 {'p', "num-peers", "COUNT",
335 gettext_noop ("create COUNT number of peers"),
336 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
337 GNUNET_GETOPT_OPTION_END
341 exit_result = GNUNET_SYSERR;
343 GNUNET_PROGRAM_run (argc, argv, "gnunet-underlay-topology",
344 _("Generates SQLite3 database representing a given underlay topology.\n"
345 "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
346 "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
349 "\t RANDOM <num_rnd_links>\n"
350 "\t SMALL_WORLD <num_rnd_links>\n"
351 "\t SMALL_WORLD_RING <num_rnd_links>\n"
354 "\t SCALE_FREE <cap> <m>\n"
355 "\t FROM_FILE <filename>\n"
357 "\t num_rnd_links: The number of random links\n"
358 "\t cap: the maximum number of links a node can have\n"
359 "\t m: the number of links a node should have while joining the network\n"
360 "\t filename: the path of the file which contains topology information\n"
361 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
363 if (NULL != stmt_insert)
364 sqlite3_finalize (stmt_insert);
367 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))