aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-daemon-testbed-underlay.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2014-01-02 23:05:27 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2014-01-02 23:05:27 +0000
commit6af4c1d1b29d08490b936946ab01e377d3b62240 (patch)
tree3cdbcf068a45c3bf650fdcf8f0c8db55cb9e55ec /src/testbed/gnunet-daemon-testbed-underlay.c
parent56fd299ee48c317fbe5731cb1ec8c02ef6fb45f1 (diff)
downloadgnunet-6af4c1d1b29d08490b936946ab01e377d3b62240.tar.gz
gnunet-6af4c1d1b29d08490b936946ab01e377d3b62240.zip
- read whitelist from database
Diffstat (limited to 'src/testbed/gnunet-daemon-testbed-underlay.c')
-rw-r--r--src/testbed/gnunet-daemon-testbed-underlay.c167
1 files changed, 140 insertions, 27 deletions
diff --git a/src/testbed/gnunet-daemon-testbed-underlay.c b/src/testbed/gnunet-daemon-testbed-underlay.c
index 3613664d6..9b77ba0d1 100644
--- a/src/testbed/gnunet-daemon-testbed-underlay.c
+++ b/src/testbed/gnunet-daemon-testbed-underlay.c
@@ -29,6 +29,7 @@
29#include "platform.h" 29#include "platform.h"
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_transport_service.h" 31#include "gnunet_transport_service.h"
32#include "gnunet_ats_service.h"
32#include "gnunet_testing_lib.h" 33#include "gnunet_testing_lib.h"
33#include <sqlite3.h> 34#include <sqlite3.h>
34 35
@@ -97,6 +98,11 @@ static struct GNUNET_DISK_MapHandle *idmap;
97static struct GNUNET_PeerIdentity *hostkeys; 98static struct GNUNET_PeerIdentity *hostkeys;
98 99
99/** 100/**
101 * Handle to the transport service. This is used for setting link metrics
102 */
103static struct GNUNET_TRANSPORT_Handle *transport;
104
105/**
100 * The number of hostkeys in the hostkeys array 106 * The number of hostkeys in the hostkeys array
101 */ 107 */
102static unsigned int num_hostkeys; 108static unsigned int num_hostkeys;
@@ -253,12 +259,12 @@ blacklist_peer (unsigned int offset)
253/** 259/**
254 * Blacklist peer 260 * Blacklist peer
255 */ 261 */
256struct ListRow 262struct BlackListRow
257{ 263{
258 /** 264 /**
259 * Next ptr 265 * Next ptr
260 */ 266 */
261 struct ListRow *next; 267 struct BlackListRow *next;
262 268
263 /** 269 /**
264 * The offset where to find the hostkey for the peer 270 * The offset where to find the hostkey for the peer
@@ -268,32 +274,46 @@ struct ListRow
268 274
269 275
270/** 276/**
271 * Function to add a peer to the blacklist 277 * Whilelist entry
272 *
273 * @param head the head of the list
274 * @param id the id of the peer to add
275 */ 278 */
276static void 279struct WhiteListRow
277listrow_add (struct ListRow *head, unsigned int id)
278{ 280{
279 struct ListRow *bp; 281 /**
282 * Next ptr
283 */
284 struct WhiteListRow *next;
285
286 /**
287 * The offset where to find the hostkey for the peer
288 */
289 unsigned int id;
290
291 /**
292 * Bandwidth to be assigned to the link
293 */
294 int bandwidth;
280 295
281 bp = GNUNET_new (struct ListRow); 296 /**
282 bp->id = id; 297 * Latency to be assigned to the link
283 bp->next = head; 298 */
284 head = bp; 299 int latency;
285} 300
301 /**
302 * Loss to be assigned to the link
303 */
304 int loss;
305};
286 306
287 307
288/** 308/**
289 * Add peers in the blacklist to the blacklist map 309 * Add peers in the blacklist to the blacklist map
290 */ 310 */
291static int 311static int
292map_populate (struct ListRow *head, 312map_populate (struct BlackListRow *head,
293 struct GNUNET_CONTAINER_MultiPeerMap *map, 313 struct GNUNET_CONTAINER_MultiPeerMap *map,
294 const struct GNUNET_PeerIdentity *hostkeys) 314 const struct GNUNET_PeerIdentity *hostkeys)
295{ 315{
296 struct ListRow *row; 316 struct BlackListRow *row;
297 int ret; 317 int ret;
298 318
299 while (NULL != (row = head)) 319 while (NULL != (row = head))
@@ -364,11 +384,20 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
364} 384}
365 385
366 386
387/**
388 * Function to read blacklist rows from the database
389 *
390 * @param db the database connection
391 * @param pid the identity of this peer
392 * @param bl_rows where to store the retrieved blacklist rows
393 * @return GNUNET_SYSERR upon error OR the number of rows retrieved
394 */
367static int 395static int
368db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist_rows) 396db_read_blacklist (struct sqlite3 *db, unsigned int pid, struct BlackListRow **bl_rows)
369{ 397{
370 static const char *query_bl = "SELECT (id, oid) FROM blacklist WHERE (id == ?);"; 398 static const char *query_bl = "SELECT (oid) FROM blacklist WHERE (id == ?);";
371 static struct sqlite3_stmt *stmt_bl; 399 struct sqlite3_stmt *stmt_bl;
400 struct BlackListRow *lr;
372 int nrows; 401 int nrows;
373 int peer_id; 402 int peer_id;
374 int ret; 403 int ret;
@@ -391,7 +420,10 @@ db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist
391 if (SQLITE_ROW != ret) 420 if (SQLITE_ROW != ret)
392 break; 421 break;
393 peer_id = sqlite3_column_int (stmt_bl, 1); 422 peer_id = sqlite3_column_int (stmt_bl, 1);
394 listrow_add (*blacklist_rows, peer_id); 423 lr = GNUNET_new (struct BlackListRow);
424 lr->id = peer_id;
425 lr->next = *bl_rows;
426 *bl_rows = lr;
395 nrows++; 427 nrows++;
396 } while (1); 428 } while (1);
397 sqlite3_finalize (stmt_bl); 429 sqlite3_finalize (stmt_bl);
@@ -401,6 +433,54 @@ db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist
401 433
402 434
403/** 435/**
436 * Function to read whitelist rows from the database
437 *
438 * @param db the database connection
439 * @param pid the identity of this peer
440 * @param wl_rows where to store the retrieved whitelist rows
441 * @return GNUNET_SYSERR upon error OR the number of rows retrieved
442 */
443static int
444db_read_whitelist (struct sqlite3 *db, unsigned int pid, struct WhiteListRow **wl_rows)
445{
446 static const char *query_wl = "SELECT (oid, bandwidth, latency, loss) FROM whitelist WHERE (id == ?);";
447 struct sqlite3_stmt *stmt_wl;
448 struct WhiteListRow *lr;
449 int nrows;
450 int ret;
451
452 if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_wl, -1, &stmt_wl, NULL)))
453 {
454 LOG_SQLITE_ERROR (ret);
455 return GNUNET_SYSERR;
456 }
457 if (SQLITE_OK != (ret = sqlite3_bind_int (stmt_wl, 1, pid)))
458 {
459 LOG_SQLITE_ERROR (ret);
460 sqlite3_finalize (stmt_wl);
461 return GNUNET_SYSERR;
462 }
463 nrows = 0;
464 do
465 {
466 ret = sqlite3_step (stmt_wl);
467 if (SQLITE_ROW != ret)
468 break;
469 nrows++;
470 lr = GNUNET_new (struct WhiteListRow);
471 lr->id = sqlite3_column_int (stmt_wl, 1);
472 lr->bandwidth = sqlite3_column_int (stmt_wl, 2);
473 lr->latency = sqlite3_column_int (stmt_wl, 3);
474 lr->loss = sqlite3_column_int (stmt_wl, 4);
475 lr->next = *wl_rows;
476 *wl_rows = lr;
477 } while (1);
478 sqlite3_finalize (stmt_wl);
479 return nrows;
480}
481
482
483/**
404 * Main function that will be run. 484 * Main function that will be run.
405 * 485 *
406 * @param cls closure 486 * @param cls closure
@@ -413,7 +493,11 @@ run (void *cls, char *const *args, const char *cfgfile,
413 const struct GNUNET_CONFIGURATION_Handle *c) 493 const struct GNUNET_CONFIGURATION_Handle *c)
414{ 494{
415 char *dbfile; 495 char *dbfile;
416 struct ListRow *blacklist_rows; 496 struct BlackListRow *bl_head;
497 struct BlackListRow *bl_entry;
498 struct WhiteListRow *wl_head;
499 struct WhiteListRow *wl_entry;
500 struct GNUNET_ATS_Information triplet[3];
417 unsigned long long pid; 501 unsigned long long pid;
418 unsigned int nrows; 502 unsigned int nrows;
419 int ret; 503 int ret;
@@ -424,6 +508,12 @@ run (void *cls, char *const *args, const char *cfgfile,
424 GNUNET_break (0); 508 GNUNET_break (0);
425 return; 509 return;
426 } 510 }
511 transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
512 if (NULL == transport)
513 {
514 GNUNET_break (0);
515 return;
516 }
427 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED", 517 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED",
428 "UNDERLAY_DB", 518 "UNDERLAY_DB",
429 &dbfile)) 519 &dbfile))
@@ -440,22 +530,45 @@ run (void *cls, char *const *args, const char *cfgfile,
440 DEBUG ("Opened database %s\n", dbfile); 530 DEBUG ("Opened database %s\n", dbfile);
441 GNUNET_free (dbfile); 531 GNUNET_free (dbfile);
442 dbfile = NULL; 532 dbfile = NULL;
443 blacklist_rows = NULL; 533 bl_head = NULL;
444 nrows = db_read_blacklist (db, pid, &blacklist_rows); 534 wl_head = NULL;
445 if (-1 == nrows) 535 nrows = db_read_blacklist (db, pid, &bl_head);
536 if (GNUNET_SYSERR == nrows)
446 goto close_db; 537 goto close_db;
447 if (nrows > 0) 538 if (nrows > 0)
448 { 539 {
449 blacklist_map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_YES); 540 blacklist_map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_YES);
450 if (GNUNET_OK != load_keys (c)) 541 if (GNUNET_OK != load_keys (c))
451 {
452 goto close_db; 542 goto close_db;
453 }
454 } 543 }
455 /* process whitelist */ 544 /* process whitelist */
456 GNUNET_break (0); /* TODO */ 545 nrows = 0;
546 wl_head = NULL;
547 nrows = db_read_whitelist (db, pid, &wl_head);
548 if ((GNUNET_SYSERR == nrows) || (0 == nrows))
549 goto close_db;
550 triplet[0].type = 0; //FIXME: not implemented: GNUNET_ATS_QUALITY_NET_THROUGHPUT
551 triplet[1].type = GNUNET_ATS_QUALITY_NET_DELAY;
552 triplet[2].type = 0; //FIXME: not implemented: GNUNET_ATS_QUALITY_NET_LOSSRATE;
553 while (NULL != (wl_entry = wl_head))
554 {
555 wl_head = wl_entry->next;
556 triplet[0].value = wl_entry->bandwidth; //FIXME: bandwidth != throughput !!
557 triplet[1].value = wl_entry->latency;
558 triplet[2].value = wl_entry->loss;
559 GNUNET_TRANSPORT_set_traffic_metric (transport,
560 &hostkeys[wl_entry->id],
561 GNUNET_YES,
562 GNUNET_YES, /* FIXME: Separate inbound, outboud metrics */
563 triplet, 3);
564 }
457 565
458 close_db: 566 close_db:
567 while (NULL != (bl_entry = bl_head))
568 {
569 bl_head = bl_entry->next;
570 GNUNET_free (bl_entry);
571 }
459 GNUNET_break (GNUNET_OK == sqlite3_close (db)); 572 GNUNET_break (GNUNET_OK == sqlite3_close (db));
460 return; 573 return;
461} 574}