30f0a75c55a909bce9cf8b614d79ba037e2abe88
[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 cb_cls_pack *pack;
132   //struct GNUNET_RPS_Handle *h;
133   struct GNUNET_PeerIdentity *peers;
134   struct GNUNET_RPS_Request_Handle *rh;
135
136   /* Give the peers back */
137   msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
138   //pack = (struct cb_cls_pack *) cls;
139   //h = (struct GNUNET_RPS_Handle *) cls;
140   peers = (struct GNUNET_PeerIdentity *) &msg[1];
141   rh = &req_handlers[msg->n];
142   rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers);
143
144   /* Disconnect */
145   //GNUNET_CLIENT_disconnect(pack->service_conn);
146 }
147
148
149 /**
150  * Error handler for mq.
151  *
152  * This function is called whan mq encounters an error.
153  * Until now mq doesn't provide useful error messages.
154  *
155  * @param cls the closure
156  * @param error error code without specyfied meaning
157  */
158   static void
159 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
160 {
161   //TODO LOG
162 }
163
164 /**
165  * Connect to the rps service
166  *
167  * @param cfg configuration to use
168  * @return a handle to the service
169  */
170   struct GNUNET_RPS_Handle *
171 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
172 {
173   struct GNUNET_RPS_Handle *h;
174   //struct GNUNET_RPS_Request_Handle *rh;
175   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
176     {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
177     GNUNET_MQ_HANDLERS_END
178   };
179
180   h = GNUNET_new(struct GNUNET_RPS_Handle);
181   //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
182   //*h->cfg = *cfg;
183   h->cfg = cfg; // FIXME |^
184   h->conn = GNUNET_CLIENT_connect("rps", cfg);
185   h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
186                                                 mq_handlers,
187                                                 mq_error_handler, // TODO implement
188                                                 h);
189
190   return h;
191 }
192
193
194 /**
195  * Request n random peers.
196  *
197  * @param h handle to the rps service
198  * @param n number of peers we want to receive
199  * @param ready_cb the callback called when the peers are available
200  * @param cls closure given to the callback
201  * @return a handle to cancel this request
202  */
203   struct GNUNET_RPS_Request_Handle *
204 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n,
205                           GNUNET_RPS_NotifyReadyCB ready_cb,
206                           void *cls)
207 {
208   struct GNUNET_RPS_Request_Handle *rh;
209   struct GNUNET_MQ_Envelope *ev;
210   struct GNUNET_RPS_CS_RequestMessage *msg;
211
212   // assert func != NULL
213   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
214   rh->h = h;
215   rh->n = req_handlers_size; // TODO ntoh
216   rh->ready_cb = ready_cb;
217   rh->ready_cb_cls = cls;
218
219   GNUNET_array_append (req_handlers, req_handlers_size, *rh);
220   //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle));
221
222   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
223   msg->num_peers = htonl (n);
224   msg->n = rh->n;
225   GNUNET_MQ_send (h->mq, ev);
226   return rh;
227 }
228
229
230 /**
231  * Seed rps service with peerIDs.
232  *
233  * @param h handle to the rps service
234  * @param n number of peers to seed
235  * @param ids the ids of the peers seeded
236  */
237   void
238 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
239                      uint32_t n,
240                      const struct GNUNET_PeerIdentity *ids)
241 {
242   uint32_t size_needed;
243   uint32_t num_peers_max;
244   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
245   struct GNUNET_MQ_Envelope *ev;
246   struct GNUNET_RPS_CS_SeedMessage *msg;
247
248   unsigned int i;
249
250   LOG (GNUNET_ERROR_TYPE_DEBUG,
251        "Client wants to seed %" PRIX32 " peers:\n",
252        n);
253   for (i = 0 ; i < n ; i++)
254     LOG (GNUNET_ERROR_TYPE_DEBUG,
255          "%u. peer: %s\n",
256          i,
257          GNUNET_i2s (&ids[i]));
258
259   /* The actual size the message occupies */
260   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
261     n * sizeof (struct GNUNET_PeerIdentity);
262   /* The number of peers that fits in one message together with
263    * the respective header */
264   num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
265       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
266     sizeof (struct GNUNET_PeerIdentity);
267   tmp_peer_pointer = ids;
268
269   while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
270   {
271     ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
272         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
273     msg->num_peers = ntohl (num_peers_max);
274     memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
275     GNUNET_MQ_send (h->mq, ev);
276
277     n -= num_peers_max;
278     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
279                   n * sizeof (struct GNUNET_PeerIdentity);
280     /* Set pointer to beginning of next block of num_peers_max peers */
281     tmp_peer_pointer = &ids[num_peers_max];
282   }
283
284   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
285                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
286   msg->num_peers = htonl (n);
287   memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
288
289   GNUNET_MQ_send (h->mq, ev);
290 }
291
292
293 #if ENABLE_MALICIOUS
294 /**
295  * Turn RPS service to act malicious.
296  *
297  * @param h handle to the rps service
298  * @param type which type of malicious peer to turn to.
299  *             0 Don't act malicious at all
300  *             1 Try to maximise representation
301  *             2 Try to partition the network
302  *               (isolate one peer from the rest)
303  * @param n number of @a ids
304  * @param ids the ids of the malicious peers
305  *            if @type is 2 the last id is the id of the
306  *            peer to be isolated from the rest
307  */
308   void
309 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
310                           uint32_t type,
311                           uint32_t num_peers,
312                           const struct GNUNET_PeerIdentity *ids)
313 {
314   uint32_t size_needed;
315   uint32_t num_peers_max;
316   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
317   struct GNUNET_MQ_Envelope *ev;
318   struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
319
320   unsigned int i;
321
322   LOG (GNUNET_ERROR_TYPE_DEBUG,
323        "Client turns malicious with %" PRIX32 " other peers:\n",
324        n);
325   for (i = 0 ; i < n ; i++)
326     LOG (GNUNET_ERROR_TYPE_DEBUG,
327          "%u. peer: %s\n",
328          i,
329          GNUNET_i2s (&ids[i]));
330
331   /* The actual size the message occupies */
332   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
333     n * sizeof (struct GNUNET_PeerIdentity);
334   /* The number of peers that fits in one message together with
335    * the respective header */
336   num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
337       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
338     sizeof (struct GNUNET_PeerIdentity);
339   tmp_peer_pointer = ids;
340
341   while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
342   {
343     ev = GNUNET_MQ_msg_extra (msg,
344                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
345                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
346     msg->type = ntohl (type);
347     msg->num_peers = ntohl (num_peers_max);
348     memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
349     GNUNET_MQ_send (h->mq, ev);
350
351     n -= num_peers_max;
352     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
353                   n * sizeof (struct GNUNET_PeerIdentity);
354     /* Set pointer to beginning of next block of num_peers_max peers */
355     tmp_peer_pointer = &ids[num_peers_max];
356   }
357
358   ev = GNUNET_MQ_msg_extra (msg,
359                             n * sizeof (struct GNUNET_PeerIdentity),
360                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
361   msg->type = htonl (type);
362   msg->num_peers = htonl (n);
363   memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
364
365   GNUNET_MQ_send (h->mq, ev);
366
367 }
368 #endif /* ENABLE_MALICIOUS */
369
370
371 /**
372  * Cancle an issued request.
373  *
374  * @param rh request handle of request to cancle
375  */
376   void
377 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
378 {
379   // TODO
380 }
381
382
383 /**
384  * Disconnect from the rps service
385  *
386  * @param h the handle to the rps service
387  */
388   void
389 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
390 {
391   if (NULL != h->conn)
392     GNUNET_CLIENT_disconnect (h->conn);
393 }
394
395
396 /* end of rps_api.c */