aboutsummaryrefslogtreecommitdiff
path: root/src/core/core_api_monitor_peers.c
blob: f5a3c0e98c9022a148e8400b51a66eb724f2a2fe (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
/*
     This file is part of GNUnet.
     (C) 2009-2014 Christian Grothoff (and other contributing authors)

     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 core/core_api_monitor_peers.c
 * @brief implementation of the peer_iterate function
 * @author Christian Grothoff
 * @author Nathan Evans
 */
#include "platform.h"
#include "gnunet_core_service.h"
#include "core.h"


/**
 * Handle to a CORE monitoring operation.
 */
struct GNUNET_CORE_MonitorHandle
{

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

  /**
   * Our connection to the service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Handle for transmitting a request.
   */
  struct GNUNET_CLIENT_TransmitHandle *th;

  /**
   * Function called with the peer.
   */
  GNUNET_CORE_MonitorCallback peer_cb;

  /**
   * Closure for @e peer_cb.
   */
  void *peer_cb_cls;

};


/**
 * Transmits the monitor request to the CORE service.
 *
 * Function is called to notify a client about the socket begin ready
 * to queue more data.  @a buf will be NULL and @a size zero if the
 * socket was closed for writing in the meantime.
 *
 * @param cls closure, our `struct GNUNET_CORE_MonitorHandle *`
 * @param size number of bytes available in @a buf
 * @param buf where the callee should write the message
 * @return number of bytes written to @a buf
 */
static size_t
transmit_monitor_request (void *cls,
                          size_t size,
                          void *buf);


/**
 * Protocol error, reconnect to CORE service and notify
 * client.
 *
 * @param mh monitoring session to reconnect to CORE
 */
static void
reconnect (struct GNUNET_CORE_MonitorHandle *mh)
{
  GNUNET_CLIENT_disconnect (mh->client);
  /* FIXME: use backoff? */
  mh->client = GNUNET_CLIENT_connect ("core", mh->cfg);
  GNUNET_assert (NULL != mh->client);
  mh->th =
    GNUNET_CLIENT_notify_transmit_ready (mh->client,
                                         sizeof (struct GNUNET_MessageHeader),
                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                         GNUNET_YES,
                                         &transmit_monitor_request, mh);
  /* notify callback about reconnect */
  mh->peer_cb (mh->peer_cb_cls,
               NULL,
               GNUNET_CORE_KX_CORE_DISCONNECT,
               GNUNET_TIME_UNIT_FOREVER_ABS);
}


/**
 * Receive reply from CORE service with information about a peer.
 *
 * @param cls our `struct  GNUNET_CORE_MonitorHandle *`
 * @param msg NULL on error or last entry
 */
static void
receive_info (void *cls,
              const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CORE_MonitorHandle *mh = cls;
  const struct MonitorNotifyMessage *mon_message;
  uint16_t msize;

  if (NULL == msg)
  {
    reconnect (mh);
    return;
  }
  msize = ntohs (msg->size);
  /* Handle incorrect message type or size, disconnect and clean up */
  if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY) ||
      (sizeof (struct MonitorNotifyMessage) != msize))
  {
    GNUNET_break (0);
    reconnect (mh);
    return;
  }
  mon_message = (const struct MonitorNotifyMessage *) msg;
  GNUNET_CLIENT_receive (mh->client,
                         &receive_info, mh,
                         GNUNET_TIME_UNIT_FOREVER_REL);
  mh->peer_cb (mh->peer_cb_cls,
               &mon_message->peer,
               (enum GNUNET_CORE_KxState) ntohl (mon_message->state),
               GNUNET_TIME_absolute_ntoh (mon_message->timeout));
}


/**
 * Transmits the monitor request to the CORE service.
 *
 * Function is called to notify a client about the socket begin ready
 * to queue more data.  @a buf will be NULL and @a size zero if the
 * socket was closed for writing in the meantime.
 *
 * @param cls closure, our `struct GNUNET_CORE_MonitorHandle *`
 * @param size number of bytes available in @a buf
 * @param buf where the callee should write the message
 * @return number of bytes written to @a buf
 */
static size_t
transmit_monitor_request (void *cls,
                          size_t size,
                          void *buf)
{
  struct GNUNET_CORE_MonitorHandle *mh = cls;
  struct GNUNET_MessageHeader *msg;
  int msize;

  mh->th = NULL;
  msize = sizeof (struct GNUNET_MessageHeader);
  if ((size < msize) || (NULL == buf))
  {
    reconnect (mh);
    return 0;
  }
  msg = (struct GNUNET_MessageHeader *) buf;
  msg->size = htons (msize);
  msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS);
  GNUNET_CLIENT_receive (mh->client,
                         &receive_info, mh,
                         GNUNET_TIME_UNIT_FOREVER_REL);
  return msize;
}


/**
 * Monitor connectivity and KX status of all peers known to CORE.
 * Calls @a peer_cb with the current status for each connected peer,
 * and then once with NULL to indicate that all peers that are
 * currently active have been handled.  After that, the iteration
 * continues until it is cancelled.  Normal users of the CORE API are
 * not expected to use this function.  It is different in that it
 * truly lists all connections (including those where the KX is in
 * progress), not just those relevant to the application.  This
 * function is used by special applications for diagnostics.
 *
 * @param cfg configuration handle
 * @param peer_cb function to call with the peer information
 * @param peer_cb_cls closure for @a peer_cb
 * @return NULL on error
 */
struct GNUNET_CORE_MonitorHandle *
GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                           GNUNET_CORE_MonitorCallback peer_cb,
                           void *peer_cb_cls)
{
  struct GNUNET_CORE_MonitorHandle *mh;
  struct GNUNET_CLIENT_Connection *client;

  GNUNET_assert (NULL != peer_cb);
  client = GNUNET_CLIENT_connect ("core", cfg);
  if (NULL == client)
    return NULL;
  mh = GNUNET_new (struct GNUNET_CORE_MonitorHandle);
  mh->cfg = cfg;
  mh->client = client;
  mh->peer_cb = peer_cb;
  mh->peer_cb_cls = peer_cb_cls;
  mh->th =
    GNUNET_CLIENT_notify_transmit_ready (client,
                                         sizeof (struct GNUNET_MessageHeader),
                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                         GNUNET_YES,
                                         &transmit_monitor_request, mh);
  return mh;
}


/**
 * Stop monitoring CORE activity.
 *
 * @param mh monitor to stop
 */
void
GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh)
{
  if (NULL != mh->th)
  {
    GNUNET_CLIENT_notify_transmit_ready_cancel (mh->th);
    mh->th = NULL;
  }
  if (NULL != mh->client)
  {
    GNUNET_CLIENT_disconnect (mh->client);
    mh->client = NULL;
  }
  GNUNET_free (mh);
}


/* end of core_api_monitor_peers.c */