2 This file is part of GNUnet
3 (C) 2008--2013 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.
23 * @file testbed/gnunet-daemon-testbed-blacklist.c
24 * @brief daemon to restrict incoming connections from other peers at the
25 * transport layer of a peer
26 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
30 #include "gnunet_util_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_ats_service.h"
33 #include "gnunet_testing_lib.h"
39 #define LOG(type,...) \
40 GNUNET_log (type, __VA_ARGS__)
43 * Debug logging shorthand
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49 * Log an error message at log-level 'level' that indicates
50 * a failure of the command 'cmd' on file 'filename'
51 * with the message given by strerror(errno).
53 #define LOG_SQLITE(db, msg, level, cmd) \
55 GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
56 cmd, __FILE__,__LINE__, sqlite3_errmsg(db)); \
58 GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
59 __FILE__, __LINE__, sqlite3_errmsg(db)); \
64 * The map to store the peer identities to allow/deny
66 static struct GNUNET_CONTAINER_MultiPeerMap *map;
69 * The database connection
71 static struct sqlite3 *db;
74 * The blacklist handle we obtain from transport when we register ourselves for
77 struct GNUNET_TRANSPORT_Blacklist *bh;
82 struct GNUNET_DISK_FileHandle *hostkeys_fd;
87 static struct GNUNET_DISK_MapHandle *hostkeys_map;
92 static char *hostkeys_data;
95 * Handle to the transport service. This is used for setting link metrics
97 static struct GNUNET_TRANSPORT_Handle *transport;
100 * The number of hostkeys in the hostkeys array
102 static unsigned int num_hostkeys;
107 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
112 * Iterator over hash map entries.
115 * @param key current key code
116 * @param value value in the hash map
117 * @return #GNUNET_YES if we should continue to
122 iterator (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
124 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (map, key,
131 * Cleaup and destroy the map
138 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_iterate (map,
141 GNUNET_CONTAINER_multipeermap_destroy (map);
148 * Function that decides if a connection is acceptable or not.
151 * @param pid peer to approve or disapproave
152 * @return GNUNET_OK if the connection is allowed, GNUNET_SYSERR if not
155 check_access (void *cls, const struct GNUNET_PeerIdentity * pid)
159 GNUNET_assert (NULL != map);
160 contains = GNUNET_CONTAINER_multipeermap_contains (map, pid);
161 return (contains) ? GNUNET_OK : GNUNET_SYSERR;
166 get_identity (unsigned int offset, struct GNUNET_PeerIdentity *id)
168 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
170 if (offset >= num_hostkeys)
171 return GNUNET_SYSERR;
172 (void) memcpy (&private_key,
173 hostkeys_data + (offset * GNUNET_TESTING_HOSTKEYFILESIZE),
174 GNUNET_TESTING_HOSTKEYFILESIZE);
175 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &id->public_key);
188 struct WhiteListRow *next;
191 * The offset where to find the hostkey for the peer
196 * Bandwidth to be assigned to the link
201 * Latency to be assigned to the link
206 * Loss to be assigned to the link
213 * Function to load keys
216 load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
225 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
226 GNUNET_asprintf (&idfile, "%s/testing_hostkeys.ecc", data_dir);
227 GNUNET_free (data_dir);
230 GNUNET_DISK_file_size (idfile, &fsize, GNUNET_YES, GNUNET_YES))
232 GNUNET_free (idfile);
233 return GNUNET_SYSERR;
235 if (0 != (fsize % GNUNET_TESTING_HOSTKEYFILESIZE))
237 LOG (GNUNET_ERROR_TYPE_ERROR,
238 _("Incorrect hostkey file format: %s\n"), idfile);
239 GNUNET_free (idfile);
240 return GNUNET_SYSERR;
242 hostkeys_fd = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ,
243 GNUNET_DISK_PERM_NONE);
244 if (NULL == hostkeys_fd)
246 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", idfile);
247 GNUNET_free (idfile);
248 return GNUNET_SYSERR;
250 GNUNET_free (idfile);
252 hostkeys_data = GNUNET_DISK_file_map (hostkeys_fd,
254 GNUNET_DISK_MAP_TYPE_READ,
256 if (NULL == hostkeys_data)
259 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mmap");
260 return GNUNET_SYSERR;
262 num_hostkeys = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
268 * Function to unload keys
273 if (NULL != hostkeys_map)
275 GNUNET_assert (NULL != hostkeys_data);
276 GNUNET_DISK_file_unmap (hostkeys_map);
278 hostkeys_data = NULL;
280 if (NULL != hostkeys_fd)
282 GNUNET_DISK_file_close (hostkeys_fd);
289 * Shutdown task to cleanup our resources and exit.
292 * @param tc scheduler task context
295 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
300 GNUNET_TRANSPORT_blacklist_cancel (bh);
305 * Function to read whitelist rows from the database
307 * @param db the database connection
308 * @param pid the identity of this peer
309 * @param wl_rows where to store the retrieved whitelist rows
310 * @return GNUNET_SYSERR upon error OR the number of rows retrieved
313 db_read_whitelist (struct sqlite3 *db, unsigned int pid, struct WhiteListRow **wl_rows)
315 static const char *query_wl = "SELECT (oid, bandwidth, latency, loss) FROM whitelist WHERE (id == ?);";
316 struct sqlite3_stmt *stmt_wl;
317 struct WhiteListRow *lr;
321 if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_wl, -1, &stmt_wl, NULL)))
323 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
324 return GNUNET_SYSERR;
326 if (SQLITE_OK != (ret = sqlite3_bind_int (stmt_wl, 1, pid)))
328 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
329 sqlite3_finalize (stmt_wl);
330 return GNUNET_SYSERR;
335 ret = sqlite3_step (stmt_wl);
336 if (SQLITE_ROW != ret)
339 lr = GNUNET_new (struct WhiteListRow);
340 lr->id = sqlite3_column_int (stmt_wl, 1);
341 lr->bandwidth = sqlite3_column_int (stmt_wl, 2);
342 lr->latency = sqlite3_column_int (stmt_wl, 3);
343 lr->loss = sqlite3_column_int (stmt_wl, 4);
347 sqlite3_finalize (stmt_wl);
353 * Main function that will be run.
356 * @param args remaining command-line arguments
357 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
358 * @param c configuration
361 run (void *cls, char *const *args, const char *cfgfile,
362 const struct GNUNET_CONFIGURATION_Handle *c)
365 struct WhiteListRow *wl_head;
366 struct WhiteListRow *wl_entry;
367 struct GNUNET_PeerIdentity identity;
368 struct GNUNET_ATS_Information params[1];
369 unsigned long long pid;
373 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "TESTBED",
379 transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
380 if (NULL == transport)
385 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED-UNDERLAY",
392 if (SQLITE_OK != (ret = sqlite3_open_v2 (dbfile, &db, SQLITE_OPEN_READONLY, NULL)))
396 LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
400 LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
401 GNUNET_free (dbfile);
404 DEBUG ("Opened database %s\n", dbfile);
405 GNUNET_free (dbfile);
408 if (GNUNET_OK != load_keys (c))
410 /* read and process whitelist */
413 nrows = db_read_whitelist (db, pid, &wl_head);
414 if ((GNUNET_SYSERR == nrows) || (0 == nrows))
416 map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_YES);
417 params[0].type = GNUNET_ATS_QUALITY_NET_DELAY;
418 while (NULL != (wl_entry = wl_head))
420 wl_head = wl_entry->next;
421 params[0].value = wl_entry->latency;
422 GNUNET_assert (GNUNET_OK == get_identity (wl_entry->id, &identity));
423 GNUNET_break (GNUNET_OK ==
424 GNUNET_CONTAINER_multipeermap_put (map, &identity, &identity,
425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
426 GNUNET_TRANSPORT_set_traffic_metric (transport,
429 GNUNET_YES, /* FIXME: Separate inbound, outboud metrics */
431 GNUNET_free (wl_entry);
433 bh = GNUNET_TRANSPORT_blacklist (c, &check_access, NULL);
434 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
438 GNUNET_break (GNUNET_OK == sqlite3_close (db));
446 * @param argc number of arguments from the command line
447 * @param argv command line arguments
448 * @return 0 ok, 1 on error
451 main (int argc, char *const *argv)
453 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
454 GNUNET_GETOPT_OPTION_END
458 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
460 #ifdef SQLITE_CONFIG_MMAP_SIZE
461 (void) sqlite3_config (SQLITE_CONFIG_MMAP_SIZE, 512000, 256000000);
465 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-testbed-underlay",
467 ("Daemon to restrict underlay network in testbed deployments"),
468 options, &run, NULL)) ? 0 : 1;
469 GNUNET_free ((void*) argv);