diff options
author | ng0 <ng0@n0.is> | 2019-09-06 22:46:29 +0000 |
---|---|---|
committer | ng0 <ng0@n0.is> | 2019-09-06 22:46:29 +0000 |
commit | 6e599264ad13e8fc105493d74d7c11d46f8739ed (patch) | |
tree | 169bef1ecbade5a659831fb169f3ae6943af127f /src/testbed/generate-underlay-topology.c | |
parent | 4f13bc15113021ebf71d5d81e99bc29f8a07fc9c (diff) | |
download | gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.tar.gz gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.zip |
first step to remove plibc
Diffstat (limited to 'src/testbed/generate-underlay-topology.c')
-rw-r--r-- | src/testbed/generate-underlay-topology.c | 210 |
1 files changed, 116 insertions, 94 deletions
diff --git a/src/testbed/generate-underlay-topology.c b/src/testbed/generate-underlay-topology.c index 1bfbfda50..7857652e0 100644 --- a/src/testbed/generate-underlay-topology.c +++ b/src/testbed/generate-underlay-topology.c | |||
@@ -30,26 +30,34 @@ | |||
30 | #include "testbed_api_topology.h" | 30 | #include "testbed_api_topology.h" |
31 | #include "sqlite3.h" | 31 | #include "sqlite3.h" |
32 | 32 | ||
33 | #define LOG(type, ...) \ | 33 | #define LOG(type, ...) GNUNET_log (type, __VA_ARGS__) |
34 | GNUNET_log (type, __VA_ARGS__) | ||
35 | 34 | ||
36 | 35 | ||
37 | #define LOG_ERROR(...) \ | 36 | #define LOG_ERROR(...) LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) |
38 | LOG (GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__) | ||
39 | 37 | ||
40 | /** | 38 | /** |
41 | * Log an error message at log-level 'level' that indicates | 39 | * Log an error message at log-level 'level' that indicates |
42 | * a failure of the command 'cmd' on file 'filename' | 40 | * a failure of the command 'cmd' on file 'filename' |
43 | * with the message given by strerror(errno). | 41 | * with the message given by strerror(errno). |
44 | */ | 42 | */ |
45 | #define LOG_SQLITE(db, msg, level, cmd) \ | 43 | #define LOG_SQLITE(db, msg, level, cmd) \ |
46 | do { \ | 44 | do \ |
47 | GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \ | 45 | { \ |
48 | cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \ | 46 | GNUNET_log_from (level, \ |
49 | if (msg != NULL) \ | 47 | "sqlite", \ |
50 | GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \ | 48 | _ ("`%s' failed at %s:%d with error: %s\n"), \ |
51 | __FILE__, __LINE__, sqlite3_errmsg(db)); \ | 49 | cmd, \ |
52 | } while(0) | 50 | __FILE__, \ |
51 | __LINE__, \ | ||
52 | sqlite3_errmsg (db)); \ | ||
53 | if (msg != NULL) \ | ||
54 | GNUNET_asprintf (msg, \ | ||
55 | _ ("`%s' failed at %s:%u with error: %s"), \ | ||
56 | cmd, \ | ||
57 | __FILE__, \ | ||
58 | __LINE__, \ | ||
59 | sqlite3_errmsg (db)); \ | ||
60 | } while (0) | ||
53 | 61 | ||
54 | 62 | ||
55 | /** | 63 | /** |
@@ -97,11 +105,11 @@ link_processor (void *cls, | |||
97 | unsigned int latency, | 105 | unsigned int latency, |
98 | unsigned int loss) | 106 | unsigned int loss) |
99 | { | 107 | { |
100 | if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) || | 108 | if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, A)) || |
101 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) || | 109 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, B)) || |
102 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) || | 110 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 3, bandwidth)) || |
103 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) || | 111 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 4, latency)) || |
104 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss)) ) | 112 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 5, loss))) |
105 | { | 113 | { |
106 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int"); | 114 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int"); |
107 | return GNUNET_SYSERR; | 115 | return GNUNET_SYSERR; |
@@ -111,11 +119,11 @@ link_processor (void *cls, | |||
111 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); | 119 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); |
112 | return GNUNET_SYSERR; | 120 | return GNUNET_SYSERR; |
113 | } | 121 | } |
114 | FPRINTF (stdout, "%u -> %u\n", A, B); | 122 | fprintf (stdout, "%u -> %u\n", A, B); |
115 | GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert)); | 123 | GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert)); |
116 | //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert)); | 124 | //GNUNET_break (SQLITE_OK == sqlite3_clear_bindings (stmt_insert)); |
117 | if ( (SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) || | 125 | if ((SQLITE_OK != sqlite3_bind_int (stmt_insert, 1, B)) || |
118 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A)) ) | 126 | (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, A))) |
119 | { | 127 | { |
120 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int"); | 128 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int"); |
121 | return GNUNET_SYSERR; | 129 | return GNUNET_SYSERR; |
@@ -125,7 +133,7 @@ link_processor (void *cls, | |||
125 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); | 133 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step"); |
126 | return GNUNET_SYSERR; | 134 | return GNUNET_SYSERR; |
127 | } | 135 | } |
128 | FPRINTF (stdout, "%u -> %u\n", B, A); | 136 | fprintf (stdout, "%u -> %u\n", B, A); |
129 | GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert)); | 137 | GNUNET_break (SQLITE_OK == sqlite3_reset (stmt_insert)); |
130 | return GNUNET_OK; | 138 | return GNUNET_OK; |
131 | } | 139 | } |
@@ -142,31 +150,29 @@ link_processor (void *cls, | |||
142 | static int | 150 | static int |
143 | setup_db (const char *dbfile) | 151 | setup_db (const char *dbfile) |
144 | { | 152 | { |
145 | const char *query_create = | 153 | const char *query_create = "CREATE TABLE whitelist (" |
146 | "CREATE TABLE whitelist (" | 154 | "id INTEGER," |
147 | "id INTEGER," | 155 | "oid INTEGER," |
148 | "oid INTEGER," | 156 | "bandwidth INTEGER DEFAULT NULL," |
149 | "bandwidth INTEGER DEFAULT NULL," | 157 | "latency INTEGER DEFAULT NULL," |
150 | "latency INTEGER DEFAULT NULL," | 158 | "loss INTEGER DEFAULT NULL," |
151 | "loss INTEGER DEFAULT NULL," | 159 | " UNIQUE (" |
152 | " UNIQUE (" | 160 | " id," |
153 | " id," | 161 | " oid" |
154 | " oid" | 162 | " ) ON CONFLICT IGNORE" |
155 | " ) ON CONFLICT IGNORE" | 163 | ");"; |
156 | ");"; | 164 | const char *query_insert = "INSERT INTO whitelist(" |
157 | const char *query_insert = | 165 | " id," |
158 | "INSERT INTO whitelist(" | 166 | " oid," |
159 | " id," | 167 | " bandwidth," |
160 | " oid," | 168 | " latency," |
161 | " bandwidth," | 169 | " loss" |
162 | " latency," | 170 | ") VALUES (" |
163 | " loss" | 171 | " ?1," |
164 | ") VALUES (" | 172 | " ?2," |
165 | " ?1," | 173 | " ?3," |
166 | " ?2," | 174 | " ?4," |
167 | " ?3," | 175 | " ?5);"; |
168 | " ?4," | ||
169 | " ?5);"; | ||
170 | int ret; | 176 | int ret; |
171 | 177 | ||
172 | ret = GNUNET_SYSERR; | 178 | ret = GNUNET_SYSERR; |
@@ -178,21 +184,23 @@ setup_db (const char *dbfile) | |||
178 | if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL)) | 184 | if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL)) |
179 | { | 185 | { |
180 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec"); | 186 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec"); |
181 | FPRINTF (stderr, "Error: %d. Perhaps the database `%s' already exits.\n", | 187 | fprintf (stderr, |
188 | "Error: %d. Perhaps the database `%s' already exits.\n", | ||
182 | sqlite3_errcode (db), | 189 | sqlite3_errcode (db), |
183 | dbfile); | 190 | dbfile); |
184 | goto err_ret; | 191 | goto err_ret; |
185 | } | 192 | } |
186 | GNUNET_break (0 == sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL)); | 193 | GNUNET_break (0 == |
187 | if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, | 194 | sqlite3_exec (db, "PRAGMA synchronous = 0;", NULL, NULL, NULL)); |
188 | &stmt_insert, NULL)) | 195 | if (SQLITE_OK != |
196 | sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert, NULL)) | ||
189 | { | 197 | { |
190 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2"); | 198 | LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2"); |
191 | goto err_ret; | 199 | goto err_ret; |
192 | } | 200 | } |
193 | ret = GNUNET_OK; | 201 | ret = GNUNET_OK; |
194 | 202 | ||
195 | err_ret: | 203 | err_ret: |
196 | return ret; | 204 | return ret; |
197 | } | 205 | } |
198 | 206 | ||
@@ -206,7 +214,9 @@ setup_db (const char *dbfile) | |||
206 | * @param cfg the configuration file handle | 214 | * @param cfg the configuration file handle |
207 | */ | 215 | */ |
208 | static void | 216 | static void |
209 | run (void *cls, char *const *args, const char *cfgfile, | 217 | run (void *cls, |
218 | char *const *args, | ||
219 | const char *cfgfile, | ||
210 | const struct GNUNET_CONFIGURATION_Handle *config) | 220 | const struct GNUNET_CONFIGURATION_Handle *config) |
211 | { | 221 | { |
212 | const char *dbfile; | 222 | const char *dbfile; |
@@ -222,24 +232,24 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
222 | arg_uint2 = 0; /* make compilers happy */ | 232 | arg_uint2 = 0; /* make compilers happy */ |
223 | if (NULL == args) | 233 | if (NULL == args) |
224 | { | 234 | { |
225 | LOG_ERROR (_("Need at least 2 arguments\n")); | 235 | LOG_ERROR (_ ("Need at least 2 arguments\n")); |
226 | return; | 236 | return; |
227 | } | 237 | } |
228 | if (NULL == (dbfile = args[argc++])) | 238 | if (NULL == (dbfile = args[argc++])) |
229 | { | 239 | { |
230 | LOG_ERROR (_("Database filename missing\n")); | 240 | LOG_ERROR (_ ("Database filename missing\n")); |
231 | return; | 241 | return; |
232 | } | 242 | } |
233 | if (GNUNET_OK != setup_db (dbfile)) | 243 | if (GNUNET_OK != setup_db (dbfile)) |
234 | return; | 244 | return; |
235 | if (NULL == (topology_string = args[argc++])) | 245 | if (NULL == (topology_string = args[argc++])) |
236 | { | 246 | { |
237 | LOG_ERROR (_("Topology string missing\n")); | 247 | LOG_ERROR (_ ("Topology string missing\n")); |
238 | return; | 248 | return; |
239 | } | 249 | } |
240 | if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string)) | 250 | if (GNUNET_YES != GNUNET_TESTBED_topology_get_ (&topology, topology_string)) |
241 | { | 251 | { |
242 | LOG_ERROR (_("Invalid topology: %s\n"), topology_string); | 252 | LOG_ERROR (_ ("Invalid topology: %s\n"), topology_string); |
243 | return; | 253 | return; |
244 | } | 254 | } |
245 | arg_str1 = NULL; | 255 | arg_str1 = NULL; |
@@ -252,13 +262,13 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
252 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: | 262 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: |
253 | if (NULL == (value = args[argc++])) | 263 | if (NULL == (value = args[argc++])) |
254 | { | 264 | { |
255 | LOG_ERROR (_("An argument is missing for given topology `%s'\n"), | 265 | LOG_ERROR (_ ("An argument is missing for given topology `%s'\n"), |
256 | topology_string); | 266 | topology_string); |
257 | return; | 267 | return; |
258 | } | 268 | } |
259 | if (-1 == SSCANF (value, "%u", &arg_uint1)) | 269 | if (-1 == sscanf (value, "%u", &arg_uint1)) |
260 | { | 270 | { |
261 | LOG_ERROR (_("Invalid argument `%s' given as topology argument\n"), | 271 | LOG_ERROR (_ ("Invalid argument `%s' given as topology argument\n"), |
262 | value); | 272 | value); |
263 | return; | 273 | return; |
264 | } | 274 | } |
@@ -266,7 +276,7 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
266 | case GNUNET_TESTBED_TOPOLOGY_FROM_FILE: | 276 | case GNUNET_TESTBED_TOPOLOGY_FROM_FILE: |
267 | if (NULL == (arg_str1 = args[argc++])) | 277 | if (NULL == (arg_str1 = args[argc++])) |
268 | { | 278 | { |
269 | LOG_ERROR (_("Filename argument missing for topology `%s'\n"), | 279 | LOG_ERROR (_ ("Filename argument missing for topology `%s'\n"), |
270 | topology_string); | 280 | topology_string); |
271 | return; | 281 | return; |
272 | } | 282 | } |
@@ -280,13 +290,13 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
280 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: | 290 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: |
281 | if (NULL == (value = args[argc++])) | 291 | if (NULL == (value = args[argc++])) |
282 | { | 292 | { |
283 | LOG_ERROR (_("Second argument for topology `%s' is missing\n"), | 293 | LOG_ERROR (_ ("Second argument for topology `%s' is missing\n"), |
284 | topology_string); | 294 | topology_string); |
285 | return; | 295 | return; |
286 | } | 296 | } |
287 | if (-1 == SSCANF (value, "%u", &arg_uint2)) | 297 | if (-1 == sscanf (value, "%u", &arg_uint2)) |
288 | { | 298 | { |
289 | LOG_ERROR (_("Invalid argument `%s'; expecting unsigned int\n"), value); | 299 | LOG_ERROR (_ ("Invalid argument `%s'; expecting unsigned int\n"), value); |
290 | return; | 300 | return; |
291 | } | 301 | } |
292 | break; | 302 | break; |
@@ -301,23 +311,31 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
301 | case GNUNET_TESTBED_TOPOLOGY_STAR: | 311 | case GNUNET_TESTBED_TOPOLOGY_STAR: |
302 | case GNUNET_TESTBED_TOPOLOGY_CLIQUE: | 312 | case GNUNET_TESTBED_TOPOLOGY_CLIQUE: |
303 | case GNUNET_TESTBED_TOPOLOGY_2D_TORUS: | 313 | case GNUNET_TESTBED_TOPOLOGY_2D_TORUS: |
304 | GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL, | 314 | GNUNET_TESTBED_underlay_construct_ (num_peers, |
315 | link_processor, | ||
316 | NULL, | ||
305 | topology); | 317 | topology); |
306 | break; | 318 | break; |
307 | case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI: | 319 | case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI: |
308 | case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING: | 320 | case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING: |
309 | case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD: | 321 | case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD: |
310 | GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL, | 322 | GNUNET_TESTBED_underlay_construct_ (num_peers, |
323 | link_processor, | ||
324 | NULL, | ||
311 | topology, | 325 | topology, |
312 | arg_uint1); | 326 | arg_uint1); |
313 | break; | 327 | break; |
314 | case GNUNET_TESTBED_TOPOLOGY_FROM_FILE: | 328 | case GNUNET_TESTBED_TOPOLOGY_FROM_FILE: |
315 | GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL, | 329 | GNUNET_TESTBED_underlay_construct_ (num_peers, |
330 | link_processor, | ||
331 | NULL, | ||
316 | topology, | 332 | topology, |
317 | arg_str1); | 333 | arg_str1); |
318 | break; | 334 | break; |
319 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: | 335 | case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE: |
320 | GNUNET_TESTBED_underlay_construct_ (num_peers, link_processor, NULL, | 336 | GNUNET_TESTBED_underlay_construct_ (num_peers, |
337 | link_processor, | ||
338 | NULL, | ||
321 | topology, | 339 | topology, |
322 | arg_uint1, | 340 | arg_uint1, |
323 | arg_uint2); | 341 | arg_uint2); |
@@ -337,37 +355,41 @@ main (int argc, char *const argv[]) | |||
337 | struct GNUNET_GETOPT_CommandLineOption option[] = { | 355 | struct GNUNET_GETOPT_CommandLineOption option[] = { |
338 | 356 | ||
339 | GNUNET_GETOPT_option_uint ('p', | 357 | GNUNET_GETOPT_option_uint ('p', |
340 | "num-peers", | 358 | "num-peers", |
341 | "COUNT", | 359 | "COUNT", |
342 | gettext_noop ("create COUNT number of peers"), | 360 | gettext_noop ("create COUNT number of peers"), |
343 | &num_peers), | 361 | &num_peers), |
344 | GNUNET_GETOPT_OPTION_END | 362 | GNUNET_GETOPT_OPTION_END}; |
345 | }; | ||
346 | 363 | ||
347 | int ret; | 364 | int ret; |
348 | 365 | ||
349 | exit_result = GNUNET_SYSERR; | 366 | exit_result = GNUNET_SYSERR; |
350 | ret = | 367 | ret = GNUNET_PROGRAM_run ( |
351 | GNUNET_PROGRAM_run (argc, argv, "gnunet-underlay-topology", | 368 | argc, |
352 | _("Generates SQLite3 database representing a given underlay topology.\n" | 369 | argv, |
353 | "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n" | 370 | "gnunet-underlay-topology", |
354 | "The following options are available for TOPO followed by TOPOOPTS if applicable:\n" | 371 | _ ( |
355 | "\t LINE\n" | 372 | "Generates SQLite3 database representing a given underlay topology.\n" |
356 | "\t RING\n" | 373 | "Usage: gnunet-underlay-topology [OPTIONS] db-filename TOPO [TOPOOPTS]\n" |
357 | "\t RANDOM <num_rnd_links>\n" | 374 | "The following options are available for TOPO followed by TOPOOPTS if applicable:\n" |
358 | "\t SMALL_WORLD <num_rnd_links>\n" | 375 | "\t LINE\n" |
359 | "\t SMALL_WORLD_RING <num_rnd_links>\n" | 376 | "\t RING\n" |
360 | "\t CLIQUE\n" | 377 | "\t RANDOM <num_rnd_links>\n" |
361 | "\t 2D_TORUS\n" | 378 | "\t SMALL_WORLD <num_rnd_links>\n" |
362 | "\t SCALE_FREE <cap> <m>\n" | 379 | "\t SMALL_WORLD_RING <num_rnd_links>\n" |
363 | "\t FROM_FILE <filename>\n" | 380 | "\t CLIQUE\n" |
364 | "TOPOOPTS:\n" | 381 | "\t 2D_TORUS\n" |
365 | "\t num_rnd_links: The number of random links\n" | 382 | "\t SCALE_FREE <cap> <m>\n" |
366 | "\t cap: the maximum number of links a node can have\n" | 383 | "\t FROM_FILE <filename>\n" |
367 | "\t m: the number of links a node should have while joining the network\n" | 384 | "TOPOOPTS:\n" |
368 | "\t filename: the path of the file which contains topology information\n" | 385 | "\t num_rnd_links: The number of random links\n" |
369 | "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"), | 386 | "\t cap: the maximum number of links a node can have\n" |
370 | option, &run, NULL); | 387 | "\t m: the number of links a node should have while joining the network\n" |
388 | "\t filename: the path of the file which contains topology information\n" | ||
389 | "NOTE: the format of the above file is descibed here: https://www.gnunet.org/content/topology-file-format\n"), | ||
390 | option, | ||
391 | &run, | ||
392 | NULL); | ||
371 | if (NULL != stmt_insert) | 393 | if (NULL != stmt_insert) |
372 | sqlite3_finalize (stmt_insert); | 394 | sqlite3_finalize (stmt_insert); |
373 | if (NULL != db) | 395 | if (NULL != db) |