GNUnet  0.10.x
gnunet-daemon-latency-logger.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2008--2014 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include <sqlite3.h>
31 
32 
36 #define LOG(type, ...) \
37  GNUNET_log(type, __VA_ARGS__)
38 
42 #define DEBUG(...) \
43  LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
44 
50 #define LOG_SQLITE(db, msg, level, cmd) \
51  do { \
52  GNUNET_log_from(level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
53  cmd, __FILE__, __LINE__, sqlite3_errmsg(db)); \
54  if (msg != NULL) \
55  GNUNET_asprintf (msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
56  __FILE__, __LINE__, sqlite3_errmsg(db)); \
57  } while (0)
58 
59 
63 struct Entry {
68 
73  unsigned int latency;
74 };
75 
76 
81 
85 static struct sqlite3 *db;
86 
91 
95 static struct sqlite3_stmt *stmt_insert;
96 
97 
109 static int
110 free_iterator(void *cls,
111  const struct GNUNET_PeerIdentity *key,
112  void *value)
113 {
114  struct Entry *e = cls;
115 
118  GNUNET_free(e);
119  return GNUNET_YES;
120 }
121 
122 
129 static void
130 do_shutdown(void *cls)
131 {
133  ats = NULL;
134  if (NULL != stmt_insert)
135  {
136  sqlite3_finalize(stmt_insert);
137  stmt_insert = NULL;
138  }
139  GNUNET_break(SQLITE_OK == sqlite3_close(db));
140  db = NULL;
141  if (NULL != map)
142  {
146  map = NULL;
147  }
148 }
149 
163 static void
164 addr_info_cb(void *cls,
165  const struct GNUNET_HELLO_Address *address,
166  int address_active,
167  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
168  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
169  const struct GNUNET_ATS_Properties *prop)
170 {
171  static const char *query_insert =
172  "INSERT INTO ats_info("
173  " id,"
174  " val,"
175  " timestamp"
176  ") VALUES ("
177  " ?1,"
178  " ?2,"
179  " datetime('now')"
180  ");";
181  struct Entry *entry;
182  int latency; /* FIXME: type!? */
183 
184  if (NULL == address)
185  {
186  /* ATS service temporarily disconnected */
187  return;
188  }
189 
190  GNUNET_assert(NULL != db);
191  if (GNUNET_YES != address_active)
192  return;
193  latency = (int)prop->delay.rel_value_us;
194  entry = NULL;
196  &address->peer))
197  {
198  entry = GNUNET_CONTAINER_multipeermap_get(map, &address->peer);
199  GNUNET_assert(NULL != entry);
200  if (latency == entry->latency)
201  return;
202  }
203  if (NULL == stmt_insert)
204  {
205  if (SQLITE_OK != sqlite3_prepare_v2(db, query_insert, -1, &stmt_insert,
206  NULL))
207  {
208  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
209  goto err_shutdown;
210  }
211  }
212  if ((SQLITE_OK != sqlite3_bind_text(stmt_insert, 1,
213  GNUNET_i2s(&address->peer), -1,
214  SQLITE_STATIC)) ||
215  (SQLITE_OK != sqlite3_bind_int(stmt_insert, 2, latency)))
216  {
217  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
218  goto err_shutdown;
219  }
220  if (SQLITE_DONE != sqlite3_step(stmt_insert))
221  {
222  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
223  goto err_shutdown;
224  }
225  if (SQLITE_OK != sqlite3_reset(stmt_insert))
226  {
227  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
228  goto err_shutdown;
229  }
230  if (NULL == entry)
231  {
232  entry = GNUNET_new(struct Entry);
233  entry->id = address->peer;
235  &entry->id, entry,
237  }
238  entry->latency = latency;
239  return;
240 
241 err_shutdown:
243 }
244 
245 
254 static void
255 run(void *cls, char *const *args, const char *cfgfile,
256  const struct GNUNET_CONFIGURATION_Handle *c)
257 {
258  const char *query_create =
259  "CREATE TABLE ats_info ("
260  "id TEXT,"
261  "val INTEGER,"
262  "timestamp NUMERIC"
263  ");";
264  char *dbfile;
265 
266  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(c, "LATENCY-LOGGER",
267  "DBFILE",
268  &dbfile))
269  {
270  GNUNET_break(0);
271  return;
272  }
273  if (SQLITE_OK != sqlite3_open(dbfile, &db))
274  {
275  if (NULL != db)
276  {
277  LOG_SQLITE(db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
278  GNUNET_break(SQLITE_OK == sqlite3_close(db));
279  }
280  else
281  LOG(GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
282  GNUNET_free(dbfile);
283  return;
284  }
285  if (0 != sqlite3_exec(db, query_create, NULL, NULL, NULL))
286  DEBUG("SQLite Error: %d. Perhaps the database `%s' already exits.\n",
287  sqlite3_errcode(db), dbfile);
288  DEBUG("Opened database %s\n", dbfile);
289  GNUNET_free(dbfile);
290  dbfile = NULL;
291  ats = GNUNET_ATS_performance_init(c, &addr_info_cb, NULL);
294 }
295 
296 
300 int
301 main(int argc, char * const *argv)
302 {
303  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
305  };
306  int ret;
307 
308  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
309  return 2;
310  ret =
311  (GNUNET_OK ==
312  GNUNET_PROGRAM_run(argc, argv, "gnunet-daemon-latency-logger",
313  _("Daemon to log latency values of connections to neighbours"),
314  options, &run, NULL)) ? 0 : 1;
315  GNUNET_free((void*)argv);
316  return ret;
317 }
uint64_t rel_value_us
The actual value.
unsigned int latency
The last known value for latency.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_ATS_performance_done(struct GNUNET_ATS_PerformanceHandle *ph)
Client is done using the ATS performance subsystem, release resources.
static struct Experiment * e
#define LOG_SQLITE(db, msg, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
int GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
int main(int argc, char *const *argv)
Execution entry point.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
static int ret
Final status code.
Definition: gnunet-arm.c:89
static void addr_info_cb(void *cls, const struct GNUNET_HELLO_Address *address, int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, const struct GNUNET_ATS_Properties *prop)
Signature of a function that is called with QoS information about an address.
ATS performance characteristics for an address.
#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
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
struct GNUNET_PeerIdentity id
The peer&#39;s identity.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
static char * value
Value of the record to add/remove.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
static struct sqlite3 * db
The SQLite database handle.
static struct sqlite3_stmt * stmt_insert
Prepared statement for inserting values into the database table.
struct GNUNET_TIME_Relative delay
Delay.
static struct GNUNET_ATS_PerformanceHandle * ats
Handle to the ATS performance subsystem.
Internal representation of the hash map.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_PeerIdentity peer
For which peer is this an address?
32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
ATS Handle to obtain and/or modify performance information.
struct GNUNET_ATS_PerformanceHandle * GNUNET_ATS_performance_init(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_ATS_AddressInformationCallback addr_info_cb, void *addr_info_cb_cls)
Get handle to access performance API of the ATS subsystem.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:83
An address for communicating with a peer.
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
Automatic transport selection and outbound bandwidth determination.
static void do_shutdown(void *cls)
Shutdown.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
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
#define GNUNET_YES
Definition: gnunet_common.h:77
#define DEBUG(...)
Debug logging shorthand.
int GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL)...
static char * address
GNS address for this phone.
static int free_iterator(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Iterator over hash map entries.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
Entry type to be used in the map to store old latency values.
Definition: gnunet-bcd.c:84
#define GNUNET_free(ptr)
Wrapper around free.
#define LOG(type,...)
Logging shorthand.