aboutsummaryrefslogtreecommitdiff
path: root/src/sensor/gnunet-service-sensor_reporting_anomaly.c
blob: 9a7a4eeb05fe5221386b1495d7f70ece6c0da794 (plain) (blame)
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
/*
     This file is part of GNUnet.
     (C)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file sensor/gnunet-service-sensor_reporting_anomaly.c
 * @brief sensor service anomaly reporting functionality
 * @author Omar Tarabai
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "sensor.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_core_service.h"

#define LOG(kind,...) GNUNET_log_from (kind, "sensor-reporting-anomaly",__VA_ARGS__)

struct AnomalyReportingContext
{

  /**
   * DLL
   */
  struct AnomalyReportingContext *prev;

  /**
   * DLL
   */
  struct AnomalyReportingContext *next;

  /**
   * Sensor information
   */
  struct GNUNET_SENSOR_SensorInfo *sensor;

};

/**
 * Context of a connection to a peer through CORE
 */
struct CorePeerContext
{

  /**
   * DLL
   */
  struct CorePeerContext *prev;

  /**
   * DLL
   */
  struct CorePeerContext *next;

  /**
   * Peer identity of connected peer
   */
  struct GNUNET_PeerIdentity *peerid;

};


/**
 * Our configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Handle to core service
 */
static struct GNUNET_CORE_Handle *core;

/**
 * My peer id
 */
static struct GNUNET_PeerIdentity mypeerid;

/**
 * Head of DLL of anomaly reporting contexts
 */
static struct AnomalyReportingContext *arc_head;

/**
 * Tail of DLL of anomaly reporting contexts
 */
static struct AnomalyReportingContext *arc_tail;

/**
 * Head of DLL of CORE peer contexts
 */
static struct CorePeerContext *cp_head;

/**
 * Tail of DLL of CORE peer contexts
 */
static struct CorePeerContext *cp_tail;


/**
 * Destroy anomaly reporting context struct
 *
 * @param arc struct to destroy
 */
static void
destroy_anomaly_reporting_context (struct AnomalyReportingContext *arc)
{
  GNUNET_free (arc);
}


/**
 * Stop sensor anomaly reporting module
 */
void
SENSOR_reporting_anomaly_stop ()
{
  struct AnomalyReportingContext *arc;

  LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor anomaly reporting module.\n");
  //TODO: destroy core peer contexts
  //TODO: destroy core connection
  arc = arc_head;
  while (NULL != arc)
  {
    GNUNET_CONTAINER_DLL_remove (arc_head, arc_tail, arc);
    destroy_anomaly_reporting_context (arc);
    arc = arc_head;
  }
}


/**
 * Iterator for defined sensors
 * Watches sensors for anomaly status change to report
 *
 * @param cls unused
 * @param key unused
 * @param value a `struct GNUNET_SENSOR_SensorInfo *` with sensor information
 * @return #GNUNET_YES to continue iterations
 */
static int
init_sensor_reporting (void *cls, const struct GNUNET_HashCode *key,
                       void *value)
{
  struct GNUNET_SENSOR_SensorInfo *sensor = value;
  struct AnomalyReportingContext *arc;

  if (NULL == sensor->collection_point)
    return GNUNET_YES;
  LOG (GNUNET_ERROR_TYPE_INFO,
       "Reporting sensor `%s' anomalies to collection point `%s'.\n",
       sensor->name, GNUNET_i2s_full (sensor->collection_point));
  arc = GNUNET_new (struct AnomalyReportingContext);
  arc->sensor = sensor;
  GNUNET_CONTAINER_DLL_insert (arc_head, arc_tail, arc);
  //TODO
  return GNUNET_YES;
}


/**
 * Function called after #GNUNET_CORE_connect has succeeded (or failed
 * for good).  Note that the private key of the peer is intentionally
 * not exposed here; if you need it, your process should try to read
 * the private key file directly (which should work if you are
 * authorized...).  Implementations of this function must not call
 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
 * do this later).
 *
 * @param cls closure (unused)
 * @param my_identity ID of this peer, NULL if we failed
 */
static void
core_startup_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
{
  if (NULL == my_identity)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to connect to CORE service.\n"));
    SENSOR_reporting_anomaly_stop ();
    return;
  }
  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&mypeerid, my_identity))
  {
    LOG (GNUNET_ERROR_TYPE_ERROR,
         _("Peer identity received from CORE doesn't match ours.\n"));
    SENSOR_reporting_anomaly_stop ();
    return;
  }
}


/**
 * Method called whenever a given peer connects through CORE.
 *
 * @param cls closure (unused)
 * @param peer peer identity this notification is about
 */
static void
core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
{
  struct CorePeerContext *cp;

  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&mypeerid, peer))
    return;
  cp = GNUNET_new (struct CorePeerContext);
  cp->peerid = (struct GNUNET_PeerIdentity *)peer;
  GNUNET_CONTAINER_DLL_insert (cp_head, cp_tail, cp);
  //TODO: report to peer your anomaly status
}


/**
 * Method called whenever a CORE peer disconnects.
 *
 * @param cls closure (unused)
 * @param peer peer identity this notification is about
 */
static void
core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
{
  struct CorePeerContext *cp;

  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&mypeerid, peer))
    return;
  cp = cp_head;
  while (NULL != cp)
  {
    if (peer == cp->peerid)
    {
      GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp);
      //TODO: call peer context destroy function
      return;
    }
  }
  LOG (GNUNET_ERROR_TYPE_ERROR,
       _("Received disconnect notification from CORE"
         " for a peer we didn't know about.\n"));
}


/**
 * An inbound message is received from a peer through CORE.
 *
 * @param cls closure (unused)
 * @param peer the other peer involved
 * @param message the actual message
 * @return #GNUNET_OK to keep the connection open,
 *         #GNUNET_SYSERR to close connection to the peer (signal serious error)
 */
static int
core_inbound_cb (void *cls,
    const struct GNUNET_PeerIdentity *other,
    const struct GNUNET_MessageHeader *message)
{
  //TODO
  return GNUNET_OK;
}


/**
 * Start the sensor anomaly reporting module
 *
 * @param c our service configuration
 * @param sensors multihashmap of loaded sensors
 * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
 */
int
SENSOR_reporting_anomaly_start (const struct GNUNET_CONFIGURATION_Handle *c,
                                struct GNUNET_CONTAINER_MultiHashMap *sensors)
{
  static struct GNUNET_CORE_MessageHandler core_handlers[] = {
    {NULL, 0, 0}                //TODO
  };

  LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting sensor anomaly reporting module.\n");
  GNUNET_assert (NULL != sensors);
  cfg = c;
  core =
      GNUNET_CORE_connect (cfg, NULL, &core_startup_cb, core_connect_cb,
                           &core_disconnect_cb, core_inbound_cb, GNUNET_NO,
                           NULL, GNUNET_YES, core_handlers);
  GNUNET_CRYPTO_get_peer_identity (cfg, &mypeerid);
  GNUNET_CONTAINER_multihashmap_iterate (sensors, &init_sensor_reporting, NULL);
  return GNUNET_OK;
}

/* end of gnunet-service-sensor_reporting_anomaly.c */