2 This file is part of GNUnet.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
29 #include "gnunet_rps_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
36 * Handler to handle requests from a client.
38 struct GNUNET_RPS_Handle
41 * The handle to the client configuration.
43 const struct GNUNET_CONFIGURATION_Handle *cfg;
46 * The message queue to the client.
48 struct GNUNET_MQ_Handle *mq;
51 * Array of Request_Handles.
53 struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
56 * The id of the last request.
58 uint32_t current_request_id;
63 * Handler to single requests from the client.
65 struct GNUNET_RPS_Request_Handle
68 * The client issuing the request.
70 struct GNUNET_RPS_Handle *rps_handle;
73 * The id of the request.
78 * The callback to be called when we receive an answer.
80 GNUNET_RPS_NotifyReadyCB ready_cb;
83 * The closure for the callback.
90 * Struct used to pack the callback, its closure (provided by the caller)
91 * and the connection handler to the service to pass it to a callback function.
96 * Callback provided by the client
98 GNUNET_RPS_NotifyReadyCB cb;
101 * Closure provided by the client
106 * Handle to the service connection
108 struct GNUNET_CLIENT_Connection *service_conn;
114 * This function is called, when the service replies to our request.
115 * It verifies that @a msg is well-formed.
117 * @param cls the closure
118 * @param msg the message
119 * @return #GNUNET_OK if @a msg is well-formed
122 check_reply (void *cls,
123 const struct GNUNET_RPS_CS_ReplyMessage *msg)
125 uint16_t msize = ntohs (msg->header.size);
126 uint32_t num_peers = ntohl (msg->num_peers);
128 msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
129 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
130 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
133 return GNUNET_SYSERR;
140 * This function is called, when the service replies to our request.
141 * It calls the callback the caller gave us with the provided closure
142 * and disconnects afterwards.
144 * @param cls the closure
145 * @param msg the message
148 handle_reply (void *cls,
149 const struct GNUNET_RPS_CS_ReplyMessage *msg)
151 struct GNUNET_RPS_Handle *h = cls;
152 struct GNUNET_PeerIdentity *peers;
153 struct GNUNET_RPS_Request_Handle *rh;
156 /* Give the peers back */
157 id = ntohl (msg->id);
158 LOG (GNUNET_ERROR_TYPE_DEBUG,
159 "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
160 ntohl (msg->num_peers),
163 peers = (struct GNUNET_PeerIdentity *) &msg[1];
164 GNUNET_assert (GNUNET_YES ==
165 GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
166 rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
167 GNUNET_assert (NULL != rh);
168 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
169 rh->ready_cb (rh->ready_cb_cls,
170 ntohl (msg->num_peers),
176 * Reconnect to the service
179 reconnect (struct GNUNET_RPS_Handle *h);
183 * Error handler for mq.
185 * This function is called whan mq encounters an error.
186 * Until now mq doesn't provide useful error messages.
188 * @param cls the closure
189 * @param error error code without specyfied meaning
192 mq_error_handler (void *cls,
193 enum GNUNET_MQ_Error error)
195 struct GNUNET_RPS_Handle *h = cls;
197 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
207 * Reconnect to the service
210 reconnect (struct GNUNET_RPS_Handle *h)
212 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
213 GNUNET_MQ_hd_var_size (reply,
214 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
215 struct GNUNET_RPS_CS_ReplyMessage,
217 GNUNET_MQ_handler_end ()
221 GNUNET_MQ_destroy (h->mq);
222 h->mq = GNUNET_CLIENT_connecT (h->cfg,
231 * Connect to the rps service
233 * @param cfg configuration to use
234 * @return a handle to the service
236 struct GNUNET_RPS_Handle *
237 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
239 struct GNUNET_RPS_Handle *h;
241 h = GNUNET_new (struct GNUNET_RPS_Handle);
249 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
255 * Request n random peers.
257 * @param rps_handle handle to the rps service
258 * @param num_req_peers number of peers we want to receive
259 * @param ready_cb the callback called when the peers are available
260 * @param cls closure given to the callback
261 * @return a handle to cancel this request
263 struct GNUNET_RPS_Request_Handle *
264 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
265 uint32_t num_req_peers,
266 GNUNET_RPS_NotifyReadyCB ready_cb,
269 struct GNUNET_RPS_Request_Handle *rh;
270 struct GNUNET_MQ_Envelope *ev;
271 struct GNUNET_RPS_CS_RequestMessage *msg;
273 // assert func != NULL
274 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
275 rh->rps_handle = rps_handle;
276 rh->id = rps_handle->current_request_id++;
277 rh->ready_cb = ready_cb;
278 rh->ready_cb_cls = cls;
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
285 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
288 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
289 msg->num_peers = htonl (num_req_peers);
290 msg->id = htonl (rh->id);
291 GNUNET_MQ_send (rps_handle->mq, ev);
297 * Seed rps service with peerIDs.
299 * @param h handle to the rps service
300 * @param n number of peers to seed
301 * @param ids the ids of the peers seeded
304 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
306 const struct GNUNET_PeerIdentity *ids)
309 uint32_t num_peers_max;
310 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
311 struct GNUNET_MQ_Envelope *ev;
312 struct GNUNET_RPS_CS_SeedMessage *msg;
316 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "Client wants to seed %" PRIu32 " peers:\n",
319 for (i = 0 ; i < n ; i++)
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 GNUNET_i2s (&ids[i]));
325 /* The actual size the message occupies */
326 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
327 n * sizeof (struct GNUNET_PeerIdentity);
328 /* The number of peers that fits in one message together with
329 * the respective header */
330 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
331 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
332 sizeof (struct GNUNET_PeerIdentity);
333 tmp_peer_pointer = ids;
335 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
337 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
338 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
339 msg->num_peers = htonl (num_peers_max);
340 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
341 GNUNET_MQ_send (h->mq, ev);
344 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
345 n * sizeof (struct GNUNET_PeerIdentity);
346 /* Set pointer to beginning of next block of num_peers_max peers */
347 tmp_peer_pointer = &ids[num_peers_max];
350 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
351 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
352 msg->num_peers = htonl (n);
353 GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
355 GNUNET_MQ_send (h->mq, ev);
359 #ifdef ENABLE_MALICIOUS
361 * Turn RPS service to act malicious.
363 * @param h handle to the rps service
364 * @param type which type of malicious peer to turn to.
365 * 0 Don't act malicious at all
366 * 1 Try to maximise representation
367 * 2 Try to partition the network
368 * (isolate one peer from the rest)
369 * @param n number of @a ids
370 * @param ids the ids of the malicious peers
371 * if @type is 2 the last id is the id of the
372 * peer to be isolated from the rest
375 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
378 const struct GNUNET_PeerIdentity *peer_ids,
379 const struct GNUNET_PeerIdentity *target_peer)
382 uint32_t num_peers_max;
383 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
384 struct GNUNET_MQ_Envelope *ev;
385 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
393 for (i = 0 ; i < num_peers ; i++)
394 LOG (GNUNET_ERROR_TYPE_DEBUG,
397 GNUNET_i2s (&peer_ids[i]));
399 /* The actual size the message would occupy */
400 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
401 num_peers * sizeof (struct GNUNET_PeerIdentity);
402 /* The number of peers that fit in one message together with
403 * the respective header */
404 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
405 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
406 sizeof (struct GNUNET_PeerIdentity);
407 tmp_peer_pointer = peer_ids;
409 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
411 LOG (GNUNET_ERROR_TYPE_DEBUG,
412 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
414 ev = GNUNET_MQ_msg_extra (msg,
415 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
416 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
417 msg->type = htonl (type);
418 msg->num_peers = htonl (num_peers_max);
421 msg->attacked_peer = peer_ids[num_peers];
422 GNUNET_memcpy (&msg[1],
424 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
426 GNUNET_MQ_send (h->mq, ev);
428 num_peers -= num_peers_max;
429 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
430 num_peers * sizeof (struct GNUNET_PeerIdentity);
431 /* Set pointer to beginning of next block of num_peers_max peers */
432 tmp_peer_pointer = &peer_ids[num_peers_max];
435 ev = GNUNET_MQ_msg_extra (msg,
436 num_peers * sizeof (struct GNUNET_PeerIdentity),
437 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
438 msg->type = htonl (type);
439 msg->num_peers = htonl (num_peers);
442 msg->attacked_peer = *target_peer;
443 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
445 GNUNET_MQ_send (h->mq, ev);
447 #endif /* ENABLE_MALICIOUS */
451 * Cancle an issued request.
453 * @param rh request handle of request to cancle
456 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
458 struct GNUNET_RPS_Handle *h;
459 struct GNUNET_MQ_Envelope *ev;
460 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 "Cancelling request with id %" PRIu32 "\n",
467 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
469 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
470 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
471 msg->id = htonl (rh->id);
472 GNUNET_MQ_send (rh->rps_handle->mq, ev);
477 * Disconnect from the rps service
479 * @param h the handle to the rps service
482 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
484 GNUNET_MQ_destroy (h->mq);
485 if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
486 LOG (GNUNET_ERROR_TYPE_WARNING,
487 "Still waiting for requests\n");
488 GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
493 /* end of rps_api.c */