- s/#if/#ifdef/
[oweals/gnunet.git] / src / rps / rps_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file rps/rps_api.c
23  * @brief API for rps
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "rps.h"
29 #include "gnunet_rps_service.h"
30
31 #include <inttypes.h>
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
34
35 /**
36  * Handler to handle requests from a client.
37  */
38 struct GNUNET_RPS_Handle
39 {
40   /**
41    * The handle to the client configuration.
42    */
43   const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45   /**
46    * The connection to the client.
47    */
48   struct GNUNET_CLIENT_Connection *conn;
49
50   /**
51    * The message queue to the client.
52    */
53   struct GNUNET_MQ_Handle *mq;
54 };
55
56
57 /**
58  * Handler to single requests from the client.
59  */
60 struct GNUNET_RPS_Request_Handle
61 {
62   /**
63    * The client issuing the request.
64    */
65   struct GNUNET_RPS_Handle *h;
66
67   /**
68    * The nuber of the request.
69    */
70   uint64_t n;
71
72   /**
73    * The callback to be called when we receive an answer.
74    */
75   GNUNET_RPS_NotifyReadyCB ready_cb;
76
77   /**
78    * The closure for the callback.
79    */
80   void *ready_cb_cls;
81 };
82
83
84 /**
85  * Array of Request_Handles.
86  */
87 struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
88
89 /**
90  * Current length of req_handlers.
91  */
92 unsigned int req_handlers_size = 0;
93
94
95 /**
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.
98  */
99 struct cb_cls_pack
100 {
101   /**
102    * Callback provided by the client
103    */
104   GNUNET_RPS_NotifyReadyCB cb;
105
106   /**
107    * Closure provided by the client
108    */
109   void *cls;
110
111   /**
112    * Handle to the service connection
113    */
114  struct GNUNET_CLIENT_Connection *service_conn;
115 };
116
117
118 /**
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.
122  *
123  * @param cls the closure
124  * @param message the message
125  */
126   static void
127 handle_reply (void *cls,
128               const struct GNUNET_MessageHeader *message)
129 {
130   struct GNUNET_RPS_CS_ReplyMessage *msg;
131   struct GNUNET_PeerIdentity *peers;
132   struct GNUNET_RPS_Request_Handle *rh;
133
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 ()
139
140   /* Disconnect */
141   //GNUNET_CLIENT_disconnect(pack->service_conn);
142 }
143
144
145 /**
146  * Error handler for mq.
147  *
148  * This function is called whan mq encounters an error.
149  * Until now mq doesn't provide useful error messages.
150  *
151  * @param cls the closure
152  * @param error error code without specyfied meaning
153  */
154   static void
155 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
156 {
157   //TODO LOG
158   LOG (GNUNET_ERROR_TYPE_WARNING, "Some problem with the message queue. error: %i\n\
159        1: READ,\n\
160        2: WRITE,\n\
161        4: TIMEOUT\n",
162        error);
163
164 }
165
166 /**
167  * Connect to the rps service
168  *
169  * @param cfg configuration to use
170  * @return a handle to the service
171  */
172   struct GNUNET_RPS_Handle *
173 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
174 {
175   struct GNUNET_RPS_Handle *h;
176   //struct GNUNET_RPS_Request_Handle *rh;
177   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
178     {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
179     GNUNET_MQ_HANDLERS_END
180   };
181
182   h = GNUNET_new(struct GNUNET_RPS_Handle);
183   //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
184   //*h->cfg = *cfg;
185   h->cfg = cfg; // FIXME |^
186   h->conn = GNUNET_CLIENT_connect("rps", cfg);
187   h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
188                                                 mq_handlers,
189                                                 mq_error_handler, // TODO implement
190                                                 h);
191
192   return h;
193 }
194
195
196 /**
197  * Request n random peers.
198  *
199  * @param h handle to the rps service
200  * @param n number of peers we want to receive
201  * @param ready_cb the callback called when the peers are available
202  * @param cls closure given to the callback
203  * @return a handle to cancel this request
204  */
205   struct GNUNET_RPS_Request_Handle *
206 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
207                           GNUNET_RPS_NotifyReadyCB ready_cb,
208                           void *cls)
209 {
210   struct GNUNET_RPS_Request_Handle *rh;
211   struct GNUNET_MQ_Envelope *ev;
212   struct GNUNET_RPS_CS_RequestMessage *msg;
213
214   // assert func != NULL
215   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
216   rh->h = h;
217   rh->n = req_handlers_size; // TODO ntoh
218   rh->ready_cb = ready_cb;
219   rh->ready_cb_cls = cls;
220
221   GNUNET_array_append (req_handlers, req_handlers_size, *rh);
222   //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
223
224   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
225   msg->num_peers = htonl (n);
226   msg->n = rh->n;
227   GNUNET_MQ_send (h->mq, ev);
228   return rh;
229 }
230
231
232 /**
233  * Seed rps service with peerIDs.
234  *
235  * @param h handle to the rps service
236  * @param n number of peers to seed
237  * @param ids the ids of the peers seeded
238  */
239   void
240 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
241                      uint32_t n,
242                      const struct GNUNET_PeerIdentity *ids)
243 {
244   uint32_t size_needed;
245   uint32_t num_peers_max;
246   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
247   struct GNUNET_MQ_Envelope *ev;
248   struct GNUNET_RPS_CS_SeedMessage *msg;
249
250   unsigned int i;
251
252   LOG (GNUNET_ERROR_TYPE_DEBUG,
253        "Client wants to seed %" PRIX32 " peers:\n",
254        n);
255   for (i = 0 ; i < n ; i++)
256     LOG (GNUNET_ERROR_TYPE_DEBUG,
257          "%u. peer: %s\n",
258          i,
259          GNUNET_i2s (&ids[i]));
260
261   /* The actual size the message occupies */
262   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
263     n * sizeof (struct GNUNET_PeerIdentity);
264   /* The number of peers that fits in one message together with
265    * the respective header */
266   num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
267       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
268     sizeof (struct GNUNET_PeerIdentity);
269   tmp_peer_pointer = ids;
270
271   while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
272   {
273     ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
274         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
275     msg->num_peers = htonl (num_peers_max);
276     memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
277     GNUNET_MQ_send (h->mq, ev);
278
279     n -= num_peers_max;
280     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
281                   n * sizeof (struct GNUNET_PeerIdentity);
282     /* Set pointer to beginning of next block of num_peers_max peers */
283     tmp_peer_pointer = &ids[num_peers_max];
284   }
285
286   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
287                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
288   msg->num_peers = htonl (n);
289   memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
290
291   GNUNET_MQ_send (h->mq, ev);
292 }
293
294
295 #ifdef ENABLE_MALICIOUS
296 /**
297  * Turn RPS service to act malicious.
298  *
299  * @param h handle to the rps service
300  * @param type which type of malicious peer to turn to.
301  *             0 Don't act malicious at all
302  *             1 Try to maximise representation
303  *             2 Try to partition the network
304  *               (isolate one peer from the rest)
305  * @param n number of @a ids
306  * @param ids the ids of the malicious peers
307  *            if @type is 2 the last id is the id of the
308  *            peer to be isolated from the rest
309  */
310   void
311 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
312                           uint32_t type,
313                           uint32_t num_peers,
314                           const struct GNUNET_PeerIdentity *ids)
315 {
316   uint32_t size_needed;
317   uint32_t num_peers_max;
318   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
319   struct GNUNET_MQ_Envelope *ev;
320   struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
321
322   unsigned int i;
323
324   LOG (GNUNET_ERROR_TYPE_DEBUG,
325        "Client turns malicious with %" PRIX32 " other peers:\n",
326        num_peers);
327   for (i = 0 ; i < num_peers ; i++)
328     LOG (GNUNET_ERROR_TYPE_DEBUG,
329          "%u. peer: %s\n",
330          i,
331          GNUNET_i2s (&ids[i]));
332
333   /* The actual size the message occupies */
334   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
335     num_peers * sizeof (struct GNUNET_PeerIdentity);
336   /* The number of peers that fits in one message together with
337    * the respective header */
338   num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
339       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
340     sizeof (struct GNUNET_PeerIdentity);
341   tmp_peer_pointer = ids;
342
343   while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
344   {
345     ev = GNUNET_MQ_msg_extra (msg,
346                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
347                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
348     msg->type = htonl (type);
349     msg->num_peers = htonl (num_peers_max);
350     memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
351     GNUNET_MQ_send (h->mq, ev);
352
353     num_peers -= num_peers_max;
354     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
355                   num_peers * sizeof (struct GNUNET_PeerIdentity);
356     /* Set pointer to beginning of next block of num_peers_max peers */
357     tmp_peer_pointer = &ids[num_peers_max];
358   }
359
360   ev = GNUNET_MQ_msg_extra (msg,
361                             num_peers * sizeof (struct GNUNET_PeerIdentity),
362                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
363   msg->type = htonl (type);
364   msg->num_peers = htonl (num_peers);
365   memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
366
367   GNUNET_MQ_send (h->mq, ev);
368
369 }
370 #endif /* ENABLE_MALICIOUS */
371
372
373 /**
374  * Cancle an issued request.
375  *
376  * @param rh request handle of request to cancle
377  */
378   void
379 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
380 {
381   // TODO
382 }
383
384
385 /**
386  * Disconnect from the rps service
387  *
388  * @param h the handle to the rps service
389  */
390   void
391 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
392 {
393   if (NULL != h->conn)
394     GNUNET_CLIENT_disconnect (h->conn);
395 }
396
397
398 /* end of rps_api.c */