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 struct GNUNET_CONFIGURATION_Handle *cfg;
46 * The connection to the client.
48 struct GNUNET_CLIENT_Connection *conn;
51 * The message queue to the client.
53 struct GNUNET_MQ_Handle *mq;
56 * Array of Request_Handles.
58 struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
61 * The id of the last request.
63 uint32_t current_request_id;
68 * Handler to single requests from the client.
70 struct GNUNET_RPS_Request_Handle
73 * The client issuing the request.
75 struct GNUNET_RPS_Handle *rps_handle;
78 * The id of the request.
83 * The callback to be called when we receive an answer.
85 GNUNET_RPS_NotifyReadyCB ready_cb;
88 * The closure for the callback.
95 * Struct used to pack the callback, its closure (provided by the caller)
96 * and the connection handler to the service to pass it to a callback function.
101 * Callback provided by the client
103 GNUNET_RPS_NotifyReadyCB cb;
106 * Closure provided by the client
111 * Handle to the service connection
113 struct GNUNET_CLIENT_Connection *service_conn;
118 * This function is called, when the service replies to our request.
119 * It calls the callback the caller gave us with the provided closure
120 * and disconnects afterwards.
122 * @param cls the closure
123 * @param message the message
126 handle_reply (void *cls,
127 const struct GNUNET_MessageHeader *message)
129 struct GNUNET_RPS_Handle *h = (struct GNUNET_RPS_Handle *) cls;
130 struct GNUNET_RPS_CS_ReplyMessage *msg;
131 struct GNUNET_PeerIdentity *peers;
132 struct GNUNET_RPS_Request_Handle *rh;
135 /* Give the peers back */
136 msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
137 id = ntohl (msg->id);
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
141 ntohl (msg->num_peers),
144 peers = (struct GNUNET_PeerIdentity *) &msg[1];
145 GNUNET_assert (GNUNET_YES ==
146 GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
147 rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
148 GNUNET_assert (NULL != rh);
149 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
150 rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers);
154 * Reconnect to the service
157 reconnect (struct GNUNET_RPS_Handle *h);
161 * Error handler for mq.
163 * This function is called whan mq encounters an error.
164 * Until now mq doesn't provide useful error messages.
166 * @param cls the closure
167 * @param error error code without specyfied meaning
170 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
172 struct GNUNET_RPS_Handle *h = cls;
174 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
183 * Reconnect to the service
186 reconnect (struct GNUNET_RPS_Handle *h)
188 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
189 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
190 GNUNET_MQ_HANDLERS_END
194 GNUNET_MQ_destroy (h->mq);
196 GNUNET_CLIENT_disconnect (h->conn);
197 h->conn = GNUNET_CLIENT_connect ("rps", h->cfg);
198 GNUNET_assert (NULL != h->conn);
199 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
201 mq_error_handler, // TODO implement
206 * Connect to the rps service
208 * @param cfg configuration to use
209 * @return a handle to the service
211 struct GNUNET_RPS_Handle *
212 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
214 struct GNUNET_RPS_Handle *h;
215 //struct GNUNET_RPS_Request_Handle *rh;
217 h = GNUNET_new(struct GNUNET_RPS_Handle);
218 h->cfg = GNUNET_CONFIGURATION_dup (cfg);
220 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
226 * Request n random peers.
228 * @param rps_handle handle to the rps service
229 * @param num_req_peers number of peers we want to receive
230 * @param ready_cb the callback called when the peers are available
231 * @param cls closure given to the callback
232 * @return a handle to cancel this request
234 struct GNUNET_RPS_Request_Handle *
235 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
236 uint32_t num_req_peers,
237 GNUNET_RPS_NotifyReadyCB ready_cb,
240 struct GNUNET_RPS_Request_Handle *rh;
241 struct GNUNET_MQ_Envelope *ev;
242 struct GNUNET_RPS_CS_RequestMessage *msg;
244 // assert func != NULL
245 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
246 rh->rps_handle = rps_handle;
247 rh->id = rps_handle->current_request_id++;
248 rh->ready_cb = ready_cb;
249 rh->ready_cb_cls = cls;
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
256 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
259 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
260 msg->num_peers = htonl (num_req_peers);
261 msg->id = htonl (rh->id);
262 GNUNET_MQ_send (rps_handle->mq, ev);
268 * Seed rps service with peerIDs.
270 * @param h handle to the rps service
271 * @param n number of peers to seed
272 * @param ids the ids of the peers seeded
275 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
277 const struct GNUNET_PeerIdentity *ids)
280 uint32_t num_peers_max;
281 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
282 struct GNUNET_MQ_Envelope *ev;
283 struct GNUNET_RPS_CS_SeedMessage *msg;
287 LOG (GNUNET_ERROR_TYPE_DEBUG,
288 "Client wants to seed %" PRIu32 " peers:\n",
290 for (i = 0 ; i < n ; i++)
291 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 GNUNET_i2s (&ids[i]));
296 /* The actual size the message occupies */
297 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
298 n * sizeof (struct GNUNET_PeerIdentity);
299 /* The number of peers that fits in one message together with
300 * the respective header */
301 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
302 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
303 sizeof (struct GNUNET_PeerIdentity);
304 tmp_peer_pointer = ids;
306 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
308 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
309 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
310 msg->num_peers = htonl (num_peers_max);
311 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
312 GNUNET_MQ_send (h->mq, ev);
315 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
316 n * sizeof (struct GNUNET_PeerIdentity);
317 /* Set pointer to beginning of next block of num_peers_max peers */
318 tmp_peer_pointer = &ids[num_peers_max];
321 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
322 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
323 msg->num_peers = htonl (n);
324 memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
326 GNUNET_MQ_send (h->mq, ev);
330 #ifdef ENABLE_MALICIOUS
332 * Turn RPS service to act malicious.
334 * @param h handle to the rps service
335 * @param type which type of malicious peer to turn to.
336 * 0 Don't act malicious at all
337 * 1 Try to maximise representation
338 * 2 Try to partition the network
339 * (isolate one peer from the rest)
340 * @param n number of @a ids
341 * @param ids the ids of the malicious peers
342 * if @type is 2 the last id is the id of the
343 * peer to be isolated from the rest
346 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
349 const struct GNUNET_PeerIdentity *peer_ids,
350 const struct GNUNET_PeerIdentity *target_peer)
353 uint32_t num_peers_max;
354 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
355 struct GNUNET_MQ_Envelope *ev;
356 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
364 for (i = 0 ; i < num_peers ; i++)
365 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 GNUNET_i2s (&peer_ids[i]));
370 /* The actual size the message would occupy */
371 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
372 num_peers * sizeof (struct GNUNET_PeerIdentity);
373 /* The number of peers that fit in one message together with
374 * the respective header */
375 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
376 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
377 sizeof (struct GNUNET_PeerIdentity);
378 tmp_peer_pointer = peer_ids;
380 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
385 ev = GNUNET_MQ_msg_extra (msg,
386 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
387 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
388 msg->type = htonl (type);
389 msg->num_peers = htonl (num_peers_max);
392 msg->attacked_peer = peer_ids[num_peers];
395 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
397 GNUNET_MQ_send (h->mq, ev);
399 num_peers -= num_peers_max;
400 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
401 num_peers * sizeof (struct GNUNET_PeerIdentity);
402 /* Set pointer to beginning of next block of num_peers_max peers */
403 tmp_peer_pointer = &peer_ids[num_peers_max];
406 ev = GNUNET_MQ_msg_extra (msg,
407 num_peers * sizeof (struct GNUNET_PeerIdentity),
408 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
409 msg->type = htonl (type);
410 msg->num_peers = htonl (num_peers);
413 msg->attacked_peer = *target_peer;
414 memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
416 GNUNET_MQ_send (h->mq, ev);
418 #endif /* ENABLE_MALICIOUS */
422 * Cancle an issued request.
424 * @param rh request handle of request to cancle
427 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
429 struct GNUNET_RPS_Handle *h;
430 struct GNUNET_MQ_Envelope *ev;
431 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Cancelling request with id %" PRIu32 "\n",
438 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
440 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
441 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
442 msg->id = htonl (rh->id);
443 GNUNET_MQ_send (rh->rps_handle->mq, ev);
448 * Disconnect from the rps service
450 * @param h the handle to the rps service
453 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
456 GNUNET_CLIENT_disconnect (h->conn);
457 GNUNET_CONFIGURATION_destroy (h->cfg);
458 GNUNET_MQ_destroy (h->mq);
459 if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
460 LOG (GNUNET_ERROR_TYPE_WARNING,
461 "Still waiting for requests\n");
462 GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
467 /* end of rps_api.c */