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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 *h;
68 * The nuber 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;
136 peers = (struct GNUNET_PeerIdentity *) &msg[1];
137 rh = &req_handlers[msg->n];
138 rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers); // FIXME? ntohl ()
141 //GNUNET_CLIENT_disconnect(pack->service_conn);
146 * Error handler for mq.
148 * This function is called whan mq encounters an error.
149 * Until now mq doesn't provide useful error messages.
151 * @param cls the closure
152 * @param error error code without specyfied meaning
155 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
161 * Connect to the rps service
163 * @param cfg configuration to use
164 * @return a handle to the service
166 struct GNUNET_RPS_Handle *
167 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
169 struct GNUNET_RPS_Handle *h;
170 //struct GNUNET_RPS_Request_Handle *rh;
171 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
172 {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
173 GNUNET_MQ_HANDLERS_END
176 h = GNUNET_new(struct GNUNET_RPS_Handle);
177 //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
179 h->cfg = cfg; // FIXME |^
180 h->conn = GNUNET_CLIENT_connect("rps", cfg);
181 h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
183 mq_error_handler, // TODO implement
191 * Request n random peers.
193 * @param h handle to the rps service
194 * @param n number of peers we want to receive
195 * @param ready_cb the callback called when the peers are available
196 * @param cls closure given to the callback
197 * @return a handle to cancel this request
199 struct GNUNET_RPS_Request_Handle *
200 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
201 GNUNET_RPS_NotifyReadyCB ready_cb,
204 struct GNUNET_RPS_Request_Handle *rh;
205 struct GNUNET_MQ_Envelope *ev;
206 struct GNUNET_RPS_CS_RequestMessage *msg;
208 // assert func != NULL
209 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
211 rh->n = req_handlers_size; // TODO ntoh
212 rh->ready_cb = ready_cb;
213 rh->ready_cb_cls = cls;
215 GNUNET_array_append (req_handlers, req_handlers_size, *rh);
216 //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
218 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
219 msg->num_peers = htonl (n);
221 GNUNET_MQ_send (h->mq, ev);
227 * Seed rps service with peerIDs.
229 * @param h handle to the rps service
230 * @param n number of peers to seed
231 * @param ids the ids of the peers seeded
234 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
236 const struct GNUNET_PeerIdentity *ids)
238 uint32_t size_needed;
239 uint32_t num_peers_max;
240 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
241 struct GNUNET_MQ_Envelope *ev;
242 struct GNUNET_RPS_CS_SeedMessage *msg;
246 LOG (GNUNET_ERROR_TYPE_DEBUG,
247 "Client wants to seed %" PRIX32 " peers:\n",
249 for (i = 0 ; i < n ; i++)
250 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 GNUNET_i2s (&ids[i]));
255 /* The actual size the message occupies */
256 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
257 n * sizeof (struct GNUNET_PeerIdentity);
258 /* The number of peers that fits in one message together with
259 * the respective header */
260 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
261 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
262 sizeof (struct GNUNET_PeerIdentity);
263 tmp_peer_pointer = ids;
265 while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
267 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
268 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
269 msg->num_peers = htonl (num_peers_max);
270 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
271 GNUNET_MQ_send (h->mq, ev);
274 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
275 n * sizeof (struct GNUNET_PeerIdentity);
276 /* Set pointer to beginning of next block of num_peers_max peers */
277 tmp_peer_pointer = &ids[num_peers_max];
280 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
281 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
282 msg->num_peers = htonl (n);
283 memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
285 GNUNET_MQ_send (h->mq, ev);
291 * Turn RPS service to act malicious.
293 * @param h handle to the rps service
294 * @param type which type of malicious peer to turn to.
295 * 0 Don't act malicious at all
296 * 1 Try to maximise representation
297 * 2 Try to partition the network
298 * (isolate one peer from the rest)
299 * @param n number of @a ids
300 * @param ids the ids of the malicious peers
301 * if @type is 2 the last id is the id of the
302 * peer to be isolated from the rest
305 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
308 const struct GNUNET_PeerIdentity *ids)
310 uint32_t size_needed;
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_ActMaliciousMessage *msg;
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Client turns malicious with %" PRIX32 " other peers:\n",
321 for (i = 0 ; i < num_peers ; 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 num_peers * 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,
340 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
341 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
342 msg->type = htonl (type);
343 msg->num_peers = htonl (num_peers_max);
344 memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
345 GNUNET_MQ_send (h->mq, ev);
347 num_peers -= num_peers_max;
348 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
349 num_peers * sizeof (struct GNUNET_PeerIdentity);
350 /* Set pointer to beginning of next block of num_peers_max peers */
351 tmp_peer_pointer = &ids[num_peers_max];
354 ev = GNUNET_MQ_msg_extra (msg,
355 num_peers * sizeof (struct GNUNET_PeerIdentity),
356 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
357 msg->type = htonl (type);
358 msg->num_peers = htonl (num_peers);
359 memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
361 GNUNET_MQ_send (h->mq, ev);
364 #endif /* ENABLE_MALICIOUS */
368 * Cancle an issued request.
370 * @param rh request handle of request to cancle
373 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
380 * Disconnect from the rps service
382 * @param h the handle to the rps service
385 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
388 GNUNET_CLIENT_disconnect (h->conn);
392 /* end of rps_api.c */