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"
32 #define LOG(type, ...) \
33 GNUNET_log (type, __VA_ARGS__)
36 #define LOG_ERROR(...) \
37 LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
41 * The topology to generate
43 enum GNUNET_TESTBED_TopologyOption topology;
46 * The number of peers to include in the topology
53 static int exit_result;
57 * Functions of this type are called to process underlay link
60 * @param A offset of first peer
61 * @param B offset of second peer
62 * @param bandwidth the bandwidth of the link in bytes per second
63 * @param latency the latency of link in milliseconds
64 * @param loss the percentage of messages dropped on the link
65 * @return GNUNET_OK to continue processing; GNUNET_SYSERR to abort
68 link_processor (void *cls,
71 unsigned int bandwidth,
75 FPRINTF (stdout, "%u -> %u\n", A, B);
84 * @param args arguments passed to GNUNET_PROGRAM_run
85 * @param cfgfile the path to configuration file
86 * @param cfg the configuration file handle
89 run (void *cls, char *const *args, const char *cfgfile,
90 const struct GNUNET_CONFIGURATION_Handle *config)
93 const char *topology_string;
94 unsigned int arg_uint1;
95 unsigned int arg_uint2;
103 LOG_ERROR (_("Need atleast 2 arguments\n"));
106 if (NULL == (dbfile = args[argc++]))
108 LOG_ERROR (_("Database filename missing\n"));
111 if (NULL == (topology_string = args[argc++]))
113 LOG_ERROR (_("Topology string missing\n"));
116 if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string))
118 LOG_ERROR (_("Invalid topology: %s\n"), topology_string);
121 /* parse for first TOPOOPT. This can either be arg_uint1 or arg_str1 */
124 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
125 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
126 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
127 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
128 if (NULL == (value = args[argc++]))
130 LOG_ERROR (_("An argument is missing for given topology `%s'\n"),
134 if (-1 == SSCANF (value, "%u", &arg_uint1))
136 LOG_ERROR (_("Invalid argument `%s' given as topology argument\n"),
141 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
142 if (NULL == (arg_str1 = args[argc++]))
144 LOG_ERROR (_("Filename argument missing for topology `%s'\n"),
152 /* parse for second TOPOOPT. Only required for SCALE_FREE topology */
155 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
156 if (NULL == (value = args[argc++]))
158 LOG_ERROR (_("Second argument for topology `%s' is missing\n"),
162 if (-1 == SSCANF (value, "%u", &arg_uint2))
164 LOG_ERROR (_("Invalid argument `%s'; expecting unsigned int\n"), value);
171 /* contruct topologies */
174 case GNUNET_TESTBED_TOPOLOGY_LINE:
175 case GNUNET_TESTBED_TOPOLOGY_RING:
176 case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
177 case GNUNET_TESTBED_TOPOLOGY_2D_TORUS:
178 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
181 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
182 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
183 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
184 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
188 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
189 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
193 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
194 GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL,
209 main (int argc, char *const argv[])
211 struct GNUNET_GETOPT_CommandLineOption option[] = {
212 {'p', "num-peers", "COUNT",
213 gettext_noop ("create COUNT number of peers"),
214 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers},
215 GNUNET_GETOPT_OPTION_END
219 exit_result = GNUNET_SYSERR;
221 GNUNET_PROGRAM_run (argc, argv, "gnunet-underlay-topology",
222 _("Generates SQLite3 database representing a given underlay topology.\n"
223 "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n"
224 "The following options are available for TOPO followed by TOPOOPTS if applicable:\n"
227 "\t RANDOM <num_rnd_links>\n"
228 "\t SMALL_WORLD <num_rnd_links>\n"
229 "\t SMALL_WORLD_RING <num_rnd_links>\n"
232 "\t SCALE_FREE <cap> <m>\n"
233 "\t FROM_FILE <filename>\n"
235 "\t num_rnd_links: The number of random links\n"
236 "\t cap: the maximum number of links a node can have\n"
237 "\t m: the number of links a node should have while joining the network\n"
238 "\t filename: the path of the file which contains topology information\n"
239 "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"),
241 if ((GNUNET_OK != ret) || (GNUNET_OK != exit_result))