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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, ...) GNUNET_log (type, __VA_ARGS__)
36 #define LOG_ERROR(...) 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) \
46 GNUNET_log_from (level, \
48 _ ("`%s' failed at %s:%d with error: %s\n"), \
52 sqlite3_errmsg (db)); \
54 GNUNET_asprintf (msg, \
55 _ ("`%s' failed at %s:%u with error: %s"), \
59 sqlite3_errmsg (db)); \
64 * Handle to the sqlite3 database
66 static struct sqlite3 *db;
69 * Prepared statement for inserting link values into db
71 struct sqlite3_stmt *stmt_insert;
74 * The topology to generate
76 enum GNUNET_TESTBED_TopologyOption topology;
79 * The number of peers to include in the topology
81 static unsigned int num_peers;
86 static int exit_result;
90 * Functions of this type are called to process underlay link
93 * @param A offset of first peer
94 * @param B offset of second peer
95 * @param bandwidth the bandwidth of the link in bytes per second
96 * @param latency the latency of link in milliseconds
97 * @param loss the percentage of messages dropped on the link
98 * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
101 link_processor (void *cls,
104 unsigned int bandwidth,
105 unsigned int latency,
108 if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) ||
109 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) ||
110 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) ||
111 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) ||
112 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)))
114 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
115 return GNUNET_SYSERR;
117 if (SQLITE_DONE != sqlite3_step (stmt_insert))
119 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
120 return GNUNET_SYSERR;
122 fprintf (stdout, "%u -> %u\n", A, B);
123 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
124 // GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert));
125 if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) ||
126 (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)))
128 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
129 return GNUNET_SYSERR;
131 if (SQLITE_DONE != sqlite3_step (stmt_insert))
133 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
134 return GNUNET_SYSERR;
136 fprintf (stdout, "%u -> %u\n", B, A);
137 GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert));
143 * Open the database file, creating a new database if not existing and setup the
146 * @param dbfile the database filename
147 * @return GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has
151 setup_db (const char *dbfile)
153 const char *query_create = "CREATE TABLE whitelist ("
156 "bandwidth INTEGER DEFAULT NULL,"
157 "latency INTEGER DEFAULT NULL,"
158 "loss INTEGER DEFAULT NULL,"
162 " ) ON CONFLICT IGNORE"
164 const char *query_insert = "INSERT INTO whitelist("
179 if (SQLITE_OK != sqlite3_open (dbfile, &db))
181 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
184 if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
186 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
188 "Error: %d. Perhaps the database `%s' already exits.\n",
189 sqlite3_errcode (db),
194 sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
196 sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert, NULL))
198 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
212 * @param args arguments passed to GNUNET_PROGRAM_run
213 * @param cfgfile the path to configuration file
214 * @param cfg the configuration file handle
220 const struct GNUNET_CONFIGURATION_Handle *config)
223 const char *topology_string;
224 unsigned int arg_uint1;
225 unsigned int arg_uint2;
226 const char *arg_str1;
231 arg_uint1 = 0; /* make compilers happy */
232 arg_uint2 = 0; /* make compilers happy */
235 LOG_ERROR (_ ("Need at least 2 arguments\n"));
238 if (NULL == (dbfile = args[argc++]))
240 LOG_ERROR (_ ("Database filename missing\n"));
243 if (GNUNET_OK != setup_db (dbfile))
245 if (NULL == (topology_string = args[argc++]))
247 LOG_ERROR (_ ("Topology string missing\n"));
250 if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string))
252 LOG_ERROR (_ ("Invalid topology: %s\n"), topology_string);
256 /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
259 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
260 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
261 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
262 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
263 if (NULL == (value = args[argc++]))
265 LOG_ERROR (_ ("An argument is missing for given topology `%s'\n"),
269 if (-1 == sscanf (value, "%u", &arg_uint1))
271 LOG_ERROR (_ ("Invalid argument `%s' given as topology argument\n"),
277 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
278 if (NULL == (arg_str1 = args[argc++]))
280 LOG_ERROR (_ ("Filename argument missing for topology `%s'\n"),
289 /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
292 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
293 if (NULL == (value = args[argc++]))
295 LOG_ERROR (_ ("Second argument for topology `%s' is missing\n"),
299 if (-1 == sscanf (value, "%u", &arg_uint2))
301 LOG_ERROR (_ ("Invalid argument `%s'; expecting unsigned int\n"), value);
309 /* contruct topologies */
312 case GNUNET_TESTBED_TOPOLOGY_LINE:
313 case GNUNET_TESTBED_TOPOLOGY_RING:
314 case GNUNET_TESTBED_TOPOLOGY_STAR:
315 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
316 case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
317 GNUNET_TESTBED_underlay_construct_ (num_peers,
323 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
324 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
325 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
326 GNUNET_TESTBED_underlay_construct_ (num_peers,
333 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
334 GNUNET_TESTBED_underlay_construct_ (num_peers,
341 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
342 GNUNET_TESTBED_underlay_construct_ (num_peers,
360 main (int argc, char *const argv[])
362 struct GNUNET_GETOPT_CommandLineOption option[] = {
363 GNUNET_GETOPT_option_uint ('p',
366 gettext_noop ("create COUNT number of peers"),
368 GNUNET_GETOPT_OPTION_END
373 exit_result = GNUNET_SYSERR;
374 ret = GNUNET_PROGRAM_run (
377 "gnunet-underlay-topology",
379 "Generates SQLite3 database representing a given underlay topology.\n"
380 "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
381 "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
384 "\t RANDOM <num_rnd_links>\n"
385 "\t SMALL_WORLD <num_rnd_links>\n"
386 "\t SMALL_WORLD_RING <num_rnd_links>\n"
389 "\t SCALE_FREE <cap> <m>\n"
390 "\t FROM_FILE <filename>\n"
392 "\t num_rnd_links: The number of random links\n"
393 "\t cap: the maximum number of links a node can have\n"
394 "\t m: the number of links a node should have while joining the network\n"
395 "\t filename: the path of the file which contains topology information\n"
396 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
400 if (NULL != stmt_insert)
401 sqlite3_finalize (stmt_insert);
403 GNUNET_break (SQLITE_OK == sqlite3_close (db));
404 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))