2 This file is part of GNUnet
3 Copyright (C) 2008--2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed/generate-underlay-topology.c
21 * @brief Program to generate a database file containing given underlay topology
22 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api_topology.h"
31 #define LOG(type, ...) \
32 GNUNET_log (type, __VA_ARGS__)
35 #define LOG_ERROR(...) \
36 LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
39 * Log an error message at log-level 'level' that indicates
40 * a failure of the command 'cmd' on file 'filename'
41 * with the message given by strerror(errno).
43 #define LOG_SQLITE(db, msg, level, cmd) \
45 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
46 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
48 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
49 __FILE__, __LINE__, sqlite3_errmsg(db)); \
54 * Handle to the sqlite3 database
56 static struct sqlite3 *db;
59 * Prepared statement for inserting link values into db
61 struct sqlite3_stmt *stmt_insert;
64 * The topology to generate
66 enum GNUNET_TESTBED_TopologyOption topology;
69 * The number of peers to include in the topology
71 static unsigned int num_peers;
76 static int exit_result;
80 * Functions of this type are called to process underlay link
83 * @param A offset of first peer
84 * @param B offset of second peer
85 * @param bandwidth the bandwidth of the link in bytes per second
86 * @param latency the latency of link in milliseconds
87 * @param loss the percentage of messages dropped on the link
88 * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
91 link_processor (void *cls,
94 unsigned int bandwidth,
98 if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
99 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
100 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
101 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
102 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)) )
104 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
105 return GNUNET_SYSERR;
107 if (SQLITE_DONE != sqlite3_step (stmt_insert))
109 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
110 return GNUNET_SYSERR;
112 FPRINTF (stdout, "%u -> %u\n", A, B);
113 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
114 //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
115 if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) ||
116 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)) )
118 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
119 return GNUNET_SYSERR;
121 if (SQLITE_DONE != sqlite3_step (stmt_insert))
123 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
124 return GNUNET_SYSERR;
126 FPRINTF (stdout, "%u -> %u\n", B, A);
127 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
133 * Open the database file, creating a new database if not existing and setup the
136 * @param dbfile the database filename
137 * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
141 setup_db (const char *dbfile)
143 const char *query_create =
144 "CREATE TABLE whitelist ("
147 "bandwidth INTEGER DEFAULT NULL,"
148 "latency INTEGER DEFAULT NULL,"
149 "loss INTEGER DEFAULT NULL,"
153 " ) ON CONFLICT IGNORE"
155 const char *query_insert =
156 "INSERT INTO whitelist("
171 if (SQLITE_OK != sqlite3_open (dbfile, &db))
173 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
176 if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
178 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
179 FPRINTF (stderr, "Error: %d. Perhaps the database `%s' already exits.\n",
180 sqlite3_errcode (db),
184 GNUNET_break (0 == sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
185 if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1,
188 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
202 * @param args arguments passed to GNUNET_PROGRAM_run
203 * @param cfgfile the path to configuration file
204 * @param cfg the configuration file handle
207 run (void *cls, char *const *args, const char *cfgfile,
208 const struct GNUNET_CONFIGURATION_Handle *config)
211 const char *topology_string;
212 unsigned int arg_uint1;
213 unsigned int arg_uint2;
214 const char *arg_str1;
219 arg_uint1 = 0; /* make compilers happy */
220 arg_uint2 = 0; /* make compilers happy */
223 LOG_ERROR (_("Need at least 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);
244 /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
247 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
248 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
249 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
250 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
251 if (NULL == (value = args[argc++]))
253 LOG_ERROR (_("An argument is missing for given topology `%s'\n"),
257 if (-1 == SSCANF (value, "%u", &arg_uint1))
259 LOG_ERROR (_("Invalid argument `%s' given as topology argument\n"),
264 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
265 if (NULL == (arg_str1 = args[argc++]))
267 LOG_ERROR (_("Filename argument missing for topology `%s'\n"),
275 /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
278 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
279 if (NULL == (value = args[argc++]))
281 LOG_ERROR (_("Second argument for topology `%s' is missing\n"),
285 if (-1 == SSCANF (value, "%u", &arg_uint2))
287 LOG_ERROR (_("Invalid argument `%s'; expecting unsigned int\n"), value);
294 /* contruct topologies */
297 case GNUNET_TESTBED_TOPOLOGY_LINE:
298 case GNUNET_TESTBED_TOPOLOGY_RING:
299 case GNUNET_TESTBED_TOPOLOGY_STAR:
300 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
301 case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
302 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
305 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
306 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
307 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
308 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
312 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
313 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
317 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
318 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
333 main (int argc, char *const argv[])
335 struct GNUNET_GETOPT_CommandLineOption option[] = {
337 GNUNET_GETOPT_option_uint ('p',
340 gettext_noop ("create COUNT number of peers"),
342 GNUNET_GETOPT_OPTION_END
347 exit_result = GNUNET_SYSERR;
349 GNUNET_PROGRAM_run (argc, argv, "gnunet-underlay-topology",
350 _("Generates SQLite3 database representing a given underlay topology.\n"
351 "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
352 "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
355 "\t RANDOM <num_rnd_links>\n"
356 "\t SMALL_WORLD <num_rnd_links>\n"
357 "\t SMALL_WORLD_RING <num_rnd_links>\n"
360 "\t SCALE_FREE <cap> <m>\n"
361 "\t FROM_FILE <filename>\n"
363 "\t num_rnd_links: The number of random links\n"
364 "\t cap: the maximum number of links a node can have\n"
365 "\t m: the number of links a node should have while joining the network\n"
366 "\t filename: the path of the file which contains topology information\n"
367 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
369 if (NULL != stmt_insert)
370 sqlite3_finalize (stmt_insert);
372 GNUNET_break (SQLITE_OK == sqlite3_close (db));
373 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))