summaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-daemon-latency-logger.c
blob: c13750db7128101db42db73424018c04a516daf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
      This file is part of GNUnet
      Copyright (C) 2008--2014 GNUnet e.V.

      GNUnet is free software: you can redistribute it and/or modify it
      under the terms of the GNU Affero General Public License as published
      by the Free Software Foundation, either version 3 of the License,
      or (at your option) any later version.

      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.

      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file testbed/gnunet-daemon-latency-logger.c
 * @brief log latency values from neighbour connections into an SQLite database
 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_ats_service.h"
#include <sqlite3.h>


/**
 * Logging shorthand
 */
#define LOG(type, ...)                           \
  GNUNET_log (type, __VA_ARGS__)

/**
 * Debug logging shorthand
 */
#define DEBUG(...)                              \
  LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)

/**
 * Log an error message at log-level 'level' that indicates
 * a failure of the command 'cmd' on file 'filename'
 * with the message given by strerror(errno).
 */
#define LOG_SQLITE(db, msg, level, cmd)                                 \
  do {                                                                  \
    GNUNET_log_from (level, "sqlite", _ ( \
                       "`%s' failed at %s:%d with error: %s\n"), \
                     cmd, __FILE__, __LINE__, sqlite3_errmsg (db));  \
    if (msg != NULL)                                                    \
      GNUNET_asprintf (msg, _ ("`%s' failed at %s:%u with error: %s"), cmd, \
                       __FILE__, __LINE__, sqlite3_errmsg (db));     \
  } while (0)


/**
 * Entry type to be used in the map to store old latency values
 */
struct Entry
{
  /**
   *  The peer's identity
   */
  struct GNUNET_PeerIdentity id;

  /**
   * The last known value for latency.
   * FIXME: type!
   */
  unsigned int latency;
};


/**
 * Handle to the map used to store old latency values for peers
 */
static struct GNUNET_CONTAINER_MultiPeerMap *map;

/**
 * The SQLite database handle
 */
static struct sqlite3 *db;

/**
 * Handle to the ATS performance subsystem
 */
static struct GNUNET_ATS_PerformanceHandle *ats;

/**
 * Prepared statement for inserting values into the database table
 */
static struct sqlite3_stmt *stmt_insert;


/**
 * @ingroup hashmap
 * Iterator over hash map entries.
 *
 * @param cls closure
 * @param key current public key
 * @param value value in the hash map
 * @return #GNUNET_YES if we should continue to
 *         iterate,
 *         #GNUNET_NO if not.
 */
static int
free_iterator (void *cls,
               const struct GNUNET_PeerIdentity *key,
               void *value)
{
  struct Entry *e = cls;

  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multipeermap_remove (map, key, e));
  GNUNET_free (e);
  return GNUNET_YES;
}


/**
 * Shutdown
 *
 * @param cls NULL
 * @return
 */
static void
do_shutdown (void *cls)
{
  GNUNET_ATS_performance_done (ats);
  ats = NULL;
  if (NULL != stmt_insert)
  {
    sqlite3_finalize (stmt_insert);
    stmt_insert = NULL;
  }
  GNUNET_break (SQLITE_OK == sqlite3_close (db));
  db = NULL;
  if (NULL != map)
  {
    GNUNET_assert (GNUNET_SYSERR !=
                   GNUNET_CONTAINER_multipeermap_iterate (map, free_iterator,
                                                          NULL));
    GNUNET_CONTAINER_multipeermap_destroy (map);
    map = NULL;
  }
}


/**
 * Signature of a function that is called with QoS information about an address.
 *
 * @param cls closure
 * @param address the address
 * @param address_active #GNUNET_YES if this address is actively used
 *        to maintain a connection to a peer;
 *        #GNUNET_NO if the address is not actively used;
 *        #GNUNET_SYSERR if this address is no longer available for ATS
 * @param bandwidth_out assigned outbound bandwidth for the connection
 * @param bandwidth_in assigned inbound bandwidth for the connection
 * @param prop performance data for the address (as far as known)
 */
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)
{
  static const char *query_insert =
    "INSERT INTO ats_info("
    " id,"
    " val,"
    " timestamp"
    ") VALUES ("
    " ?1,"
    " ?2,"
    " datetime('now')"
    ");";
  struct Entry *entry;
  int latency; /* FIXME: type!? */

  if (NULL == address)
  {
    /* ATS service temporarily disconnected */
    return;
  }

  GNUNET_assert (NULL != db);
  if (GNUNET_YES != address_active)
    return;
  latency = (int) prop->delay.rel_value_us;
  entry = NULL;
  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (map,
                                                            &address->peer))
  {
    entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
    GNUNET_assert (NULL != entry);
    if (latency == entry->latency)
      return;
  }
  if (NULL == stmt_insert)
  {
    if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
                                         NULL))
    {
      LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
      goto err_shutdown;
    }
  }
  if ((SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
                                       GNUNET_i2s (&address->peer), -1,
                                       SQLITE_STATIC)) ||
      (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)))
  {
    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
    goto err_shutdown;
  }
  if (SQLITE_DONE != sqlite3_step (stmt_insert))
  {
    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
    goto err_shutdown;
  }
  if (SQLITE_OK != sqlite3_reset (stmt_insert))
  {
    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
    goto err_shutdown;
  }
  if (NULL == entry)
  {
    entry = GNUNET_new (struct Entry);
    entry->id = address->peer;
    GNUNET_CONTAINER_multipeermap_put (map,
                                       &entry->id, entry,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }
  entry->latency = latency;
  return;

err_shutdown:
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Main function that will be run.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  const char *query_create =
    "CREATE TABLE ats_info ("
    "id TEXT,"
    "val INTEGER,"
    "timestamp NUMERIC"
    ");";
  char *dbfile;

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
                                                            "DBFILE",
                                                            &dbfile))
  {
    GNUNET_break (0);
    return;
  }
  if (SQLITE_OK != sqlite3_open (dbfile, &db))
  {
    if (NULL != db)
    {
      LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
      GNUNET_break (SQLITE_OK == sqlite3_close (db));
    }
    else
      LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
    GNUNET_free (dbfile);
    return;
  }
  if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
    DEBUG ("SQLite Error: %d.  Perhaps the database `%s' already exits.\n",
           sqlite3_errcode (db), dbfile);
  DEBUG ("Opened database %s\n", dbfile);
  GNUNET_free (dbfile);
  dbfile = NULL;
  ats = GNUNET_ATS_performance_init (c, &addr_info_cb, NULL);
  map = GNUNET_CONTAINER_multipeermap_create (30, GNUNET_YES);
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
}


/**
 * Execution entry point
 */
int
main (int argc, char *const *argv)
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
    return 2;
  ret =
    (GNUNET_OK ==
     GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
                         _ (
                           "Daemon to log latency values of connections to neighbours"),
                         options, &run, NULL)) ? 0 : 1;
  GNUNET_free_nz ((void *) argv);
  return ret;
}