GNUnet  0.10.x
Macros | Functions | Variables
generate-underlay-topology.c File Reference

Program to generate a database file containing given underlay topology. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_testbed_service.h"
#include "testbed_api_topology.h"
#include "sqlite3.h"
Include dependency graph for generate-underlay-topology.c:

Go to the source code of this file.

Macros

#define LOG(type, ...)   GNUNET_log(type, __VA_ARGS__)
 
#define LOG_ERROR(...)   LOG(GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
 
#define LOG_SQLITE(db, msg, level, cmd)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno). More...
 

Functions

static int link_processor (void *cls, unsigned int A, unsigned int B, unsigned int bandwidth, unsigned int latency, unsigned int loss)
 Functions of this type are called to process underlay link. More...
 
static int setup_db (const char *dbfile)
 Open the database file, creating a new database if not existing and setup the whitelist table. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config)
 Main run function. More...
 
int main (int argc, char *const argv[])
 Main. More...
 

Variables

static struct sqlite3 * db
 Handle to the sqlite3 database. More...
 
struct sqlite3_stmt * stmt_insert
 Prepared statement for inserting link values into db. More...
 
enum GNUNET_TESTBED_TopologyOption topology
 The topology to generate. More...
 
static unsigned int num_peers
 The number of peers to include in the topology. More...
 
static int exit_result
 program result More...
 

Detailed Description

Program to generate a database file containing given underlay topology.

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in

Definition in file generate-underlay-topology.c.

Macro Definition Documentation

◆ LOG

#define LOG (   type,
  ... 
)    GNUNET_log(type, __VA_ARGS__)

Definition at line 33 of file generate-underlay-topology.c.

◆ LOG_ERROR

#define LOG_ERROR (   ...)    LOG(GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)

Definition at line 36 of file generate-underlay-topology.c.

Referenced by run().

◆ LOG_SQLITE

#define LOG_SQLITE (   db,
  msg,
  level,
  cmd 
)
Value:
do \
{ \
GNUNET_log_from(level, \
"sqlite", \
_("`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, \
__LINE__, \
sqlite3_errmsg(db)); \
if (msg != NULL) \
GNUNET_asprintf (msg, \
_("`%s' failed at %s:%u with error: %s"), \
cmd, \
__FILE__, \
__LINE__, \
sqlite3_errmsg(db)); \
} while (0)
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static struct sqlite3 * db
Handle to the sqlite3 database.

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno).

Definition at line 43 of file generate-underlay-topology.c.

Referenced by link_processor(), and setup_db().

Function Documentation

◆ link_processor()

static int link_processor ( void *  cls,
unsigned int  A,
unsigned int  B,
unsigned int  bandwidth,
unsigned int  latency,
unsigned int  loss 
)
static

Functions of this type are called to process underlay link.

Parameters
clsclosure
Aoffset of first peer
Boffset of second peer
bandwidththe bandwidth of the link in bytes per second
latencythe latency of link in milliseconds
lossthe percentage of messages dropped on the link
Returns
GNUNET_OK to continue processing; GNUNET_SYSERR to abort

Definition at line 101 of file generate-underlay-topology.c.

References db, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SYSERR, LOG_SQLITE, and stmt_insert.

Referenced by run().

107 {
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)))
113  {
114  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
115  return GNUNET_SYSERR;
116  }
117  if (SQLITE_DONE != sqlite3_step(stmt_insert))
118  {
119  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
120  return GNUNET_SYSERR;
121  }
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)))
127  {
128  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
129  return GNUNET_SYSERR;
130  }
131  if (SQLITE_DONE != sqlite3_step(stmt_insert))
132  {
133  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
134  return GNUNET_SYSERR;
135  }
136  fprintf(stdout, "%u -> %u\n", B, A);
137  GNUNET_break(SQLITE_OK == sqlite3_reset(stmt_insert));
138  return GNUNET_OK;
139 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct sqlite3 * db
Handle to the sqlite3 database.
struct sqlite3_stmt * stmt_insert
Prepared statement for inserting link values into db.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG_SQLITE(db, msg, level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
Here is the caller graph for this function:

◆ setup_db()

static int setup_db ( const char *  dbfile)
static

Open the database file, creating a new database if not existing and setup the whitelist table.

Parameters
dbfilethe database filename
Returns
GNUNET_OK upon success; GNUNET_SYSERR upon failure (error message has to be printed)

Definition at line 151 of file generate-underlay-topology.c.

References db, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SYSERR, LOG_SQLITE, ret, and stmt_insert.

Referenced by run().

152 {
153  const char *query_create = "CREATE TABLE whitelist ("
154  "id INTEGER,"
155  "oid INTEGER,"
156  "bandwidth INTEGER DEFAULT NULL,"
157  "latency INTEGER DEFAULT NULL,"
158  "loss INTEGER DEFAULT NULL,"
159  " UNIQUE ("
160  " id,"
161  " oid"
162  " ) ON CONFLICT IGNORE"
163  ");";
164  const char *query_insert = "INSERT INTO whitelist("
165  " id,"
166  " oid,"
167  " bandwidth,"
168  " latency,"
169  " loss"
170  ") VALUES ("
171  " ?1,"
172  " ?2,"
173  " ?3,"
174  " ?4,"
175  " ?5);";
176  int ret;
177 
178  ret = GNUNET_SYSERR;
179  if (SQLITE_OK != sqlite3_open(dbfile, &db))
180  {
181  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_open");
182  goto err_ret;
183  }
184  if (0 != sqlite3_exec(db, query_create, NULL, NULL, NULL))
185  {
186  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
187  fprintf(stderr,
188  "Error: %d. Perhaps the database `%s' already exits.\n",
189  sqlite3_errcode(db),
190  dbfile);
191  goto err_ret;
192  }
193  GNUNET_break(0 ==
194  sqlite3_exec(db, "PRAGMA synchronous = 0;", NULL, NULL, NULL));
195  if (SQLITE_OK !=
196  sqlite3_prepare_v2(db, query_insert, -1, &stmt_insert, NULL))
197  {
198  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
199  goto err_ret;
200  }
201  ret = GNUNET_OK;
202 
203 err_ret:
204  return ret;
205 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct sqlite3 * db
Handle to the sqlite3 database.
struct sqlite3_stmt * stmt_insert
Prepared statement for inserting link values into db.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG_SQLITE(db, msg, level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle config 
)
static

Main run function.

Parameters
clsNULL
argsarguments passed to GNUNET_PROGRAM_run
cfgfilethe path to configuration file
cfgthe configuration file handle

Definition at line 217 of file generate-underlay-topology.c.

References _, GNUNET_assert, GNUNET_OK, GNUNET_TESTBED_TOPOLOGY_2D_TORUS, GNUNET_TESTBED_TOPOLOGY_CLIQUE, GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, GNUNET_TESTBED_TOPOLOGY_FROM_FILE, GNUNET_TESTBED_topology_get_(), GNUNET_TESTBED_TOPOLOGY_LINE, GNUNET_TESTBED_TOPOLOGY_RING, GNUNET_TESTBED_TOPOLOGY_SCALE_FREE, GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD, GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING, GNUNET_TESTBED_TOPOLOGY_STAR, GNUNET_TESTBED_underlay_construct_(), GNUNET_YES, link_processor(), LOG_ERROR, num_peers, setup_db(), topology, and value.

Referenced by main().

221 {
222  const char *dbfile;
223  const char *topology_string;
224  unsigned int arg_uint1;
225  unsigned int arg_uint2;
226  const char *arg_str1;
227  const char *value;
228  unsigned int argc;
229 
230  argc = 0;
231  arg_uint1 = 0; /* make compilers happy */
232  arg_uint2 = 0; /* make compilers happy */
233  if (NULL == args)
234  {
235  LOG_ERROR(_("Need at least 2 arguments\n"));
236  return;
237  }
238  if (NULL == (dbfile = args[argc++]))
239  {
240  LOG_ERROR(_("Database filename missing\n"));
241  return;
242  }
243  if (GNUNET_OK != setup_db(dbfile))
244  return;
245  if (NULL == (topology_string = args[argc++]))
246  {
247  LOG_ERROR(_("Topology string missing\n"));
248  return;
249  }
250  if (GNUNET_YES != GNUNET_TESTBED_topology_get_(&topology, topology_string))
251  {
252  LOG_ERROR(_("Invalid topology: %s\n"), topology_string);
253  return;
254  }
255  arg_str1 = NULL;
256  /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
257  switch (topology)
258  {
263  if (NULL == (value = args[argc++]))
264  {
265  LOG_ERROR(_("An argument is missing for given topology `%s'\n"),
266  topology_string);
267  return;
268  }
269  if (-1 == sscanf(value, "%u", &arg_uint1))
270  {
271  LOG_ERROR(_("Invalid argument `%s' given as topology argument\n"),
272  value);
273  return;
274  }
275  break;
276 
278  if (NULL == (arg_str1 = args[argc++]))
279  {
280  LOG_ERROR(_("Filename argument missing for topology `%s'\n"),
281  topology_string);
282  return;
283  }
284  break;
285 
286  default:
287  break;
288  }
289  /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
290  switch (topology)
291  {
293  if (NULL == (value = args[argc++]))
294  {
295  LOG_ERROR(_("Second argument for topology `%s' is missing\n"),
296  topology_string);
297  return;
298  }
299  if (-1 == sscanf(value, "%u", &arg_uint2))
300  {
301  LOG_ERROR(_("Invalid argument `%s'; expecting unsigned int\n"), value);
302  return;
303  }
304  break;
305 
306  default:
307  break;
308  }
309  /* contruct topologies */
310  switch (topology)
311  {
319  NULL,
320  topology);
321  break;
322 
328  NULL,
329  topology,
330  arg_uint1);
331  break;
332 
336  NULL,
337  topology,
338  arg_str1);
339  break;
340 
344  NULL,
345  topology,
346  arg_uint1,
347  arg_uint2);
348  break;
349 
350  default:
351  GNUNET_assert(0);
352  }
353 }
A clique (everyone connected to everyone else).
int GNUNET_TESTBED_underlay_construct_(int num_peers, underlay_link_processor proc, void *cls,...)
Function to construct an underlay topology.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
enum GNUNET_TESTBED_TopologyOption topology
The topology to generate.
static int setup_db(const char *dbfile)
Open the database file, creating a new database if not existing and setup the whitelist table...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
Read a topology from a given file.
static char * value
Value of the record to add/remove.
#define LOG_ERROR(...)
static int link_processor(void *cls, unsigned int A, unsigned int B, unsigned int bandwidth, unsigned int latency, unsigned int loss)
Functions of this type are called to process underlay link.
Small-world network (2d torus plus random links).
static unsigned int num_peers
The number of peers to include in the topology.
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_TESTBED_topology_get_(enum GNUNET_TESTBED_TopologyOption *topology, const char *topology_string)
Get a topology from a string input.
Small-world network (ring plus random links).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const  argv[] 
)

Main.

Definition at line 360 of file generate-underlay-topology.c.

References _, db, exit_result, gettext_noop, GNUNET_break, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_uint(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_SYSERR, num_peers, ret, run(), and stmt_insert.

361 {
364  "num-peers",
365  "COUNT",
366  gettext_noop("create COUNT number of peers"),
367  &num_peers),
369  };
370 
371  int ret;
372 
374  ret = GNUNET_PROGRAM_run(
375  argc,
376  argv,
377  "gnunet-underlay-topology",
378  _(
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"
382  "\t LINE\n"
383  "\t RING\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"
387  "\t CLIQUE\n"
388  "\t 2D_TORUS\n"
389  "\t SCALE_FREE <cap> <m>\n"
390  "\t FROM_FILE <filename>\n"
391  "TOPOOPTS:\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"),
397  option,
398  &run,
399  NULL);
400  if (NULL != stmt_insert)
401  sqlite3_finalize(stmt_insert);
402  if (NULL != db)
403  GNUNET_break(SQLITE_OK == sqlite3_close(db));
404  if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))
405  return 1;
406  return 0;
407 }
static int exit_result
program result
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static struct sqlite3 * db
Handle to the sqlite3 database.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config)
Main run function.
struct sqlite3_stmt * stmt_insert
Prepared statement for inserting link values into db.
static char * option
Name of the option.
Definition: gnunet-config.c:38
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
static unsigned int num_peers
The number of peers to include in the topology.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ db

struct sqlite3* db
static

Handle to the sqlite3 database.

Definition at line 66 of file generate-underlay-topology.c.

Referenced by link_processor(), main(), and setup_db().

◆ stmt_insert

struct sqlite3_stmt* stmt_insert

Prepared statement for inserting link values into db.

Definition at line 71 of file generate-underlay-topology.c.

Referenced by link_processor(), main(), and setup_db().

◆ topology

The topology to generate.

Definition at line 76 of file generate-underlay-topology.c.

Referenced by GNUNET_TESTBED_run(), GNUNET_TESTBED_underlay_construct_(), and run().

◆ num_peers

unsigned int num_peers
static

The number of peers to include in the topology.

Definition at line 81 of file generate-underlay-topology.c.

Referenced by main(), and run().

◆ exit_result

int exit_result
static

program result

Definition at line 86 of file generate-underlay-topology.c.

Referenced by main().