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 connection to the client.
48 struct GNUNET_CLIENT_Connection *conn;
51 * The message queue to the client.
53 struct GNUNET_MQ_Handle *mq;
58 * Handler to single requests from the client.
60 struct GNUNET_RPS_Request_Handle
63 * The client issuing the request.
65 struct GNUNET_RPS_Handle *rps_handle;
68 * The id of the request.
73 * The callback to be called when we receive an answer.
75 GNUNET_RPS_NotifyReadyCB ready_cb;
78 * The closure for the callback.
85 * Array of Request_Handles.
87 struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
90 * Current length of req_handlers.
92 unsigned int req_handlers_size = 0;
96 * Struct used to pack the callback, its closure (provided by the caller)
97 * and the connection handler to the service to pass it to a callback function.
102 * Callback provided by the client
104 GNUNET_RPS_NotifyReadyCB cb;
107 * Closure provided by the client
112 * Handle to the service connection
114 struct GNUNET_CLIENT_Connection *service_conn;
119 * This function is called, when the service replies to our request.
120 * It calls the callback the caller gave us with the provided closure
121 * and disconnects afterwards.
123 * @param cls the closure
124 * @param message the message
127 handle_reply (void *cls,
128 const struct GNUNET_MessageHeader *message)
130 struct GNUNET_RPS_CS_ReplyMessage *msg;
131 struct GNUNET_PeerIdentity *peers;
132 struct GNUNET_RPS_Request_Handle *rh;
134 /* Give the peers back */
135 msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
139 ntohl (msg->num_peers),
142 peers = (struct GNUNET_PeerIdentity *) &msg[1];
143 rh = &req_handlers[ntohl (msg->id)];
144 rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers);
149 * Error handler for mq.
151 * This function is called whan mq encounters an error.
152 * Until now mq doesn't provide useful error messages.
154 * @param cls the closure
155 * @param error error code without specyfied meaning
158 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
161 LOG (GNUNET_ERROR_TYPE_WARNING, "Some problem with the message queue. error: %i\n\
170 * Connect to the rps service
172 * @param cfg configuration to use
173 * @return a handle to the service
175 struct GNUNET_RPS_Handle *
176 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
178 struct GNUNET_RPS_Handle *h;
179 //struct GNUNET_RPS_Request_Handle *rh;
180 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
181 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
182 GNUNET_MQ_HANDLERS_END
185 h = GNUNET_new(struct GNUNET_RPS_Handle);
186 //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
188 h->cfg = cfg; // FIXME |^
189 h->conn = GNUNET_CLIENT_connect("rps", cfg);
190 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
192 mq_error_handler, // TODO implement
200 * Request n random peers.
202 * @param rps_handle handle to the rps service
203 * @param num_req_peers number of peers we want to receive
204 * @param ready_cb the callback called when the peers are available
205 * @param cls closure given to the callback
206 * @return a handle to cancel this request
208 struct GNUNET_RPS_Request_Handle *
209 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
210 uint32_t num_req_peers,
211 GNUNET_RPS_NotifyReadyCB ready_cb,
214 struct GNUNET_RPS_Request_Handle *rh;
215 struct GNUNET_MQ_Envelope *ev;
216 struct GNUNET_RPS_CS_RequestMessage *msg;
218 // assert func != NULL
219 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
220 rh->rps_handle = rps_handle;
221 rh->id = req_handlers_size; // TODO ntoh
222 rh->ready_cb = ready_cb;
223 rh->ready_cb_cls = cls;
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
230 GNUNET_array_append (req_handlers, req_handlers_size, *rh);
231 //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
233 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
234 msg->num_peers = htonl (num_req_peers);
235 msg->id = htonl (rh->id);
236 GNUNET_MQ_send (rps_handle->mq, ev);
242 * Seed rps service with peerIDs.
244 * @param h handle to the rps service
245 * @param n number of peers to seed
246 * @param ids the ids of the peers seeded
249 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
251 const struct GNUNET_PeerIdentity *ids)
254 uint32_t num_peers_max;
255 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
256 struct GNUNET_MQ_Envelope *ev;
257 struct GNUNET_RPS_CS_SeedMessage *msg;
261 LOG (GNUNET_ERROR_TYPE_DEBUG,
262 "Client wants to seed %" PRIu32 " peers:\n",
264 for (i = 0 ; i < n ; i++)
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 GNUNET_i2s (&ids[i]));
270 /* The actual size the message occupies */
271 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
272 n * sizeof (struct GNUNET_PeerIdentity);
273 /* The number of peers that fits in one message together with
274 * the respective header */
275 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
276 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
277 sizeof (struct GNUNET_PeerIdentity);
278 tmp_peer_pointer = ids;
280 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
282 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
283 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
284 msg->num_peers = htonl (num_peers_max);
285 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
286 GNUNET_MQ_send (h->mq, ev);
289 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
290 n * sizeof (struct GNUNET_PeerIdentity);
291 /* Set pointer to beginning of next block of num_peers_max peers */
292 tmp_peer_pointer = &ids[num_peers_max];
295 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
296 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
297 msg->num_peers = htonl (n);
298 memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
300 GNUNET_MQ_send (h->mq, ev);
304 #ifdef ENABLE_MALICIOUS
306 * Turn RPS service to act malicious.
308 * @param h handle to the rps service
309 * @param type which type of malicious peer to turn to.
310 * 0 Don't act malicious at all
311 * 1 Try to maximise representation
312 * 2 Try to partition the network
313 * (isolate one peer from the rest)
314 * @param n number of @a ids
315 * @param ids the ids of the malicious peers
316 * if @type is 2 the last id is the id of the
317 * peer to be isolated from the rest
320 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
323 const struct GNUNET_PeerIdentity *peer_ids)
326 uint32_t num_peers_max;
327 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
328 struct GNUNET_MQ_Envelope *ev;
329 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
337 for (i = 0 ; i < num_peers ; i++)
338 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 GNUNET_i2s (&peer_ids[i]));
343 /* The actual size the message would occupy */
344 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
345 num_peers * sizeof (struct GNUNET_PeerIdentity);
346 /* The number of peers that fit in one message together with
347 * the respective header */
348 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
349 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
350 sizeof (struct GNUNET_PeerIdentity);
351 tmp_peer_pointer = peer_ids;
353 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
358 ev = GNUNET_MQ_msg_extra (msg,
359 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
360 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
361 msg->type = htonl (type);
362 msg->num_peers = htonl (num_peers_max);
365 msg->attacked_peer = peer_ids[num_peers];
368 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
370 GNUNET_MQ_send (h->mq, ev);
372 num_peers -= num_peers_max;
373 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
374 num_peers * sizeof (struct GNUNET_PeerIdentity);
375 /* Set pointer to beginning of next block of num_peers_max peers */
376 tmp_peer_pointer = &peer_ids[num_peers_max];
379 ev = GNUNET_MQ_msg_extra (msg,
380 num_peers * sizeof (struct GNUNET_PeerIdentity),
381 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
382 msg->type = htonl (type);
383 msg->num_peers = htonl (num_peers);
386 msg->attacked_peer = peer_ids[num_peers];
387 memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
389 GNUNET_MQ_send (h->mq, ev);
391 #endif /* ENABLE_MALICIOUS */
395 * Cancle an issued request.
397 * @param rh request handle of request to cancle
400 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
407 * Disconnect from the rps service
409 * @param h the handle to the rps service
412 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
415 GNUNET_CLIENT_disconnect (h->conn);
419 /* end of rps_api.c */