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 check_reply (NULL, msg);
158 /* Give the peers back */
159 id = ntohl (msg->id);
160 LOG (GNUNET_ERROR_TYPE_DEBUG,
161 "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
162 ntohl (msg->num_peers),
165 peers = (struct GNUNET_PeerIdentity *) &msg[1];
166 GNUNET_assert (GNUNET_YES ==
167 GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
168 rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
169 GNUNET_assert (NULL != rh);
170 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
171 rh->ready_cb (rh->ready_cb_cls,
172 ntohl (msg->num_peers),
178 * Reconnect to the service
181 reconnect (struct GNUNET_RPS_Handle *h);
185 * Error handler for mq.
187 * This function is called whan mq encounters an error.
188 * Until now mq doesn't provide useful error messages.
190 * @param cls the closure
191 * @param error error code without specyfied meaning
194 mq_error_handler (void *cls,
195 enum GNUNET_MQ_Error error)
197 struct GNUNET_RPS_Handle *h = cls;
199 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
209 * Reconnect to the service
212 reconnect (struct GNUNET_RPS_Handle *h)
214 GNUNET_MQ_hd_var_size (reply,
215 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
216 struct GNUNET_RPS_CS_ReplyMessage);
217 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
218 make_reply_handler (h),
219 GNUNET_MQ_handler_end ()
223 GNUNET_MQ_destroy (h->mq);
224 h->mq = GNUNET_CLIENT_connecT (h->cfg,
233 * Connect to the rps service
235 * @param cfg configuration to use
236 * @return a handle to the service
238 struct GNUNET_RPS_Handle *
239 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
241 struct GNUNET_RPS_Handle *h;
243 h = GNUNET_new (struct GNUNET_RPS_Handle);
251 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
257 * Request n random peers.
259 * @param rps_handle handle to the rps service
260 * @param num_req_peers number of peers we want to receive
261 * @param ready_cb the callback called when the peers are available
262 * @param cls closure given to the callback
263 * @return a handle to cancel this request
265 struct GNUNET_RPS_Request_Handle *
266 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
267 uint32_t num_req_peers,
268 GNUNET_RPS_NotifyReadyCB ready_cb,
271 struct GNUNET_RPS_Request_Handle *rh;
272 struct GNUNET_MQ_Envelope *ev;
273 struct GNUNET_RPS_CS_RequestMessage *msg;
275 // assert func != NULL
276 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
277 rh->rps_handle = rps_handle;
278 rh->id = rps_handle->current_request_id++;
279 rh->ready_cb = ready_cb;
280 rh->ready_cb_cls = cls;
282 LOG (GNUNET_ERROR_TYPE_DEBUG,
283 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
287 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
290 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
291 msg->num_peers = htonl (num_req_peers);
292 msg->id = htonl (rh->id);
293 GNUNET_MQ_send (rps_handle->mq, ev);
299 * Seed rps service with peerIDs.
301 * @param h handle to the rps service
302 * @param n number of peers to seed
303 * @param ids the ids of the peers seeded
306 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
308 const struct GNUNET_PeerIdentity *ids)
311 uint32_t num_peers_max;
312 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
313 struct GNUNET_MQ_Envelope *ev;
314 struct GNUNET_RPS_CS_SeedMessage *msg;
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Client wants to seed %" PRIu32 " peers:\n",
321 for (i = 0 ; i < n ; i++)
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 GNUNET_i2s (&ids[i]));
327 /* The actual size the message occupies */
328 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
329 n * sizeof (struct GNUNET_PeerIdentity);
330 /* The number of peers that fits in one message together with
331 * the respective header */
332 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
333 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
334 sizeof (struct GNUNET_PeerIdentity);
335 tmp_peer_pointer = ids;
337 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
339 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
340 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
341 msg->num_peers = htonl (num_peers_max);
342 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
343 GNUNET_MQ_send (h->mq, ev);
346 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
347 n * sizeof (struct GNUNET_PeerIdentity);
348 /* Set pointer to beginning of next block of num_peers_max peers */
349 tmp_peer_pointer = &ids[num_peers_max];
352 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
353 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
354 msg->num_peers = htonl (n);
355 memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
357 GNUNET_MQ_send (h->mq, ev);
361 #ifdef ENABLE_MALICIOUS
363 * Turn RPS service to act malicious.
365 * @param h handle to the rps service
366 * @param type which type of malicious peer to turn to.
367 * 0 Don't act malicious at all
368 * 1 Try to maximise representation
369 * 2 Try to partition the network
370 * (isolate one peer from the rest)
371 * @param n number of @a ids
372 * @param ids the ids of the malicious peers
373 * if @type is 2 the last id is the id of the
374 * peer to be isolated from the rest
377 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
380 const struct GNUNET_PeerIdentity *peer_ids,
381 const struct GNUNET_PeerIdentity *target_peer)
384 uint32_t num_peers_max;
385 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
386 struct GNUNET_MQ_Envelope *ev;
387 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
391 LOG (GNUNET_ERROR_TYPE_DEBUG,
392 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
395 for (i = 0 ; i < num_peers ; i++)
396 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 GNUNET_i2s (&peer_ids[i]));
401 /* The actual size the message would occupy */
402 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
403 num_peers * sizeof (struct GNUNET_PeerIdentity);
404 /* The number of peers that fit in one message together with
405 * the respective header */
406 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
407 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
408 sizeof (struct GNUNET_PeerIdentity);
409 tmp_peer_pointer = peer_ids;
411 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
413 LOG (GNUNET_ERROR_TYPE_DEBUG,
414 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
416 ev = GNUNET_MQ_msg_extra (msg,
417 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
418 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
419 msg->type = htonl (type);
420 msg->num_peers = htonl (num_peers_max);
423 msg->attacked_peer = peer_ids[num_peers];
426 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
428 GNUNET_MQ_send (h->mq, ev);
430 num_peers -= num_peers_max;
431 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
432 num_peers * sizeof (struct GNUNET_PeerIdentity);
433 /* Set pointer to beginning of next block of num_peers_max peers */
434 tmp_peer_pointer = &peer_ids[num_peers_max];
437 ev = GNUNET_MQ_msg_extra (msg,
438 num_peers * sizeof (struct GNUNET_PeerIdentity),
439 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
440 msg->type = htonl (type);
441 msg->num_peers = htonl (num_peers);
444 msg->attacked_peer = *target_peer;
445 memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
447 GNUNET_MQ_send (h->mq, ev);
449 #endif /* ENABLE_MALICIOUS */
453 * Cancle an issued request.
455 * @param rh request handle of request to cancle
458 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
460 struct GNUNET_RPS_Handle *h;
461 struct GNUNET_MQ_Envelope *ev;
462 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 "Cancelling request with id %" PRIu32 "\n",
469 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
471 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
472 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
473 msg->id = htonl (rh->id);
474 GNUNET_MQ_send (rh->rps_handle->mq, ev);
479 * Disconnect from the rps service
481 * @param h the handle to the rps service
484 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
486 GNUNET_MQ_destroy (h->mq);
487 if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
488 LOG (GNUNET_ERROR_TYPE_WARNING,
489 "Still waiting for requests\n");
490 GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
495 /* end of rps_api.c */