fix
[oweals/gnunet.git] / src / peerinfo / peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2007, 2009 Christian Grothoff (and other contributing authors)
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 2, 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 peerinfo/peerinfo_api.c
23  * @brief API to access peerinfo service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
31 #include "peerinfo.h"
32
33 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
34
35
36 struct CAFContext
37 {
38   struct GNUNET_CLIENT_Connection *client;
39   struct GNUNET_MessageHeader *msg;
40 };
41
42
43 static size_t
44 copy_and_free (void *cls, size_t size, void *buf)
45 {
46   struct CAFContext *cc = cls;
47   struct GNUNET_MessageHeader *msg = cc->msg;
48   uint16_t msize;
49
50   if (buf == NULL)
51     {
52 #if DEBUG_PEERINFO
53       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
54                   _
55                   ("Failed to transmit message of type %u to `%s' service.\n"),
56                   ntohs (msg->type), "peerinfo");
57 #endif
58       GNUNET_free (msg);
59       GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
60       GNUNET_free (cc);
61       return 0;
62     }
63   msize = ntohs (msg->size);
64   GNUNET_assert (size >= msize);
65   memcpy (buf, msg, msize);
66   GNUNET_free (msg);
67   GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
68   GNUNET_free (cc);
69   return msize;
70 }
71
72
73
74 /**
75  * Add a host to the persistent list.
76  *
77  * @param cfg configuration to use
78  * @param sched scheduler to use
79  * @param peer identity of the peer
80  * @param hello the verified (!) HELLO message
81  */
82 void
83 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
84                           struct GNUNET_SCHEDULER_Handle *sched,
85                           const struct GNUNET_PeerIdentity *peer,
86                           const struct GNUNET_HELLO_Message *hello)
87 {
88   struct GNUNET_CLIENT_Connection *client;
89   struct PeerAddMessage *pam;
90   uint16_t hs;
91   struct CAFContext *cc;
92
93 #if DEBUG_PEERINFO
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95               "Adding peer `%s' to peerinfo database\n",
96               GNUNET_i2s(peer));
97 #endif
98   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
99   if (client == NULL)
100     {
101       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102                   _("Could not connect to `%s' service.\n"), "peerinfo");
103       return;
104     }
105   hs = GNUNET_HELLO_size (hello);
106 #if DEBUG_PEERINFO
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Size of `%s' is %u bytes\n",
109               "HELLO",
110               (unsigned int) GNUNET_HELLO_size (hello));
111 #endif  
112   pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
113   pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
114   pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
115   memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
116   memcpy (&pam[1], hello, hs);
117   cc = GNUNET_malloc (sizeof (struct CAFContext));
118   cc->client = client;
119   cc->msg = &pam->header;
120   GNUNET_CLIENT_notify_transmit_ready (client,
121                                        ntohs (pam->header.size),
122                                        ADD_PEER_TIMEOUT, 
123                                        GNUNET_NO,
124                                        &copy_and_free, cc);
125 }
126
127
128 /**
129  * Context for the info handler.
130  */
131 struct GNUNET_PEERINFO_IteratorContext
132 {
133
134   /**
135    * Our connection to the PEERINFO service.
136    */
137   struct GNUNET_CLIENT_Connection *client;
138
139   /**
140    * Function to call with information.
141    */
142   GNUNET_PEERINFO_Processor callback;
143
144   /**
145    * Closure for callback.
146    */
147   void *callback_cls;
148
149   /**
150    * When should we time out?
151    */
152   struct GNUNET_TIME_Absolute timeout;
153
154 };
155
156
157 /**
158  * Type of a function to call when we receive a message
159  * from the service.
160  *
161  * @param cls closure
162  * @param msg message received, NULL on timeout or fatal error
163  */
164 static void
165 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
166 {
167   struct GNUNET_PEERINFO_IteratorContext *ic = cls;
168   const struct InfoMessage *im;
169   const struct GNUNET_HELLO_Message *hello;
170   uint16_t ms;
171
172   if (msg == NULL)
173     {
174       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
175                   _("Failed to receive response from `%s' service.\n"),
176                   "peerinfo");
177       ic->callback (ic->callback_cls, NULL, NULL, 1);
178       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
179       GNUNET_free (ic);
180       return;
181     }
182   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
183     {
184 #if DEBUG_PEERINFO
185       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186                   "Received end of list of peers from peerinfo database\n");
187 #endif
188       ic->callback (ic->callback_cls, NULL, NULL, 0);
189       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
190       GNUNET_free (ic);
191       return;
192     }
193   ms = ntohs (msg->size);
194   if ((ms < sizeof (struct InfoMessage)) ||
195       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
196     {
197       GNUNET_break (0);
198       ic->callback (ic->callback_cls, NULL, NULL, 2);
199       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
200       GNUNET_free (ic);
201       return;
202     }
203   im = (const struct InfoMessage *) msg;
204   hello = NULL;
205   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
206     {
207       hello = (const struct GNUNET_HELLO_Message *) &im[1];
208       if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
209         {
210           GNUNET_break (0);
211           ic->callback (ic->callback_cls, NULL, NULL, 2);
212           GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
213           GNUNET_free (ic);
214           return;
215         }
216     }
217 #if DEBUG_PEERINFO
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
220               (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
221               "HELLO",
222               GNUNET_i2s (&im->peer));
223 #endif
224   ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
225   GNUNET_CLIENT_receive (ic->client,
226                          &info_handler,
227                          ic,
228                          GNUNET_TIME_absolute_get_remaining (ic->timeout));
229 }
230
231
232 /**
233  * Call a method for each known matching host and change
234  * its trust value.  The method will be invoked once for
235  * each host and then finally once with a NULL pointer.
236  * Note that the last call can be triggered by timeout or
237  * by simply being done; however, the trust argument will
238  * be set to zero if we are done and to 1 if we timed out.
239  *
240  * @param cfg configuration to use
241  * @param sched scheduler to use
242  * @param peer restrict iteration to this peer only (can be NULL)
243  * @param trust_delta how much to change the trust in all matching peers
244  * @param timeout how long to wait until timing out
245  * @param callback the method to call for each peer
246  * @param callback_cls closure for callback
247  * @return NULL on error, otherwise an iterator context
248  */
249 struct GNUNET_PEERINFO_IteratorContext *
250 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
251                          struct GNUNET_SCHEDULER_Handle *sched,
252                          const struct GNUNET_PeerIdentity *peer,
253                          int trust_delta,
254                          struct GNUNET_TIME_Relative timeout,
255                          GNUNET_PEERINFO_Processor callback,
256                          void *callback_cls)
257 {
258   struct GNUNET_CLIENT_Connection *client;
259   struct ListAllPeersMessage *lapm;
260   struct ListPeerMessage *lpm;
261   struct GNUNET_PEERINFO_IteratorContext *ihc;
262
263   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
264   if (client == NULL)
265     {
266       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267                   _("Could not connect to `%s' service.\n"), "peerinfo");
268       return NULL;
269     }
270 #if DEBUG_PEERINFO
271   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272               "Requesting list of peers from peerinfo database\n");
273 #endif
274   if (peer == NULL)
275     {
276       ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
277                            sizeof (struct ListAllPeersMessage));
278       lapm = (struct ListAllPeersMessage *) &ihc[1];
279       lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
280       lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
281       lapm->trust_change = htonl (trust_delta);
282     }
283   else
284     {
285       ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
286                            sizeof (struct ListPeerMessage));
287       lpm = (struct ListPeerMessage *) &ihc[1];
288       lpm->header.size = htons (sizeof (struct ListPeerMessage));
289       lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
290       lpm->trust_change = htonl (trust_delta);
291       memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
292     }
293   ihc->client = client;
294   ihc->callback = callback;
295   ihc->callback_cls = callback_cls;
296   ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
297   if (GNUNET_OK != 
298       GNUNET_CLIENT_transmit_and_get_response (client,
299                                                (const struct GNUNET_MessageHeader*) &ihc[1],
300                                                timeout,
301                                                GNUNET_YES,
302                                                &info_handler,
303                                                ihc))
304     {
305       GNUNET_break (0);
306       GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
307       GNUNET_free (ihc);
308       return NULL;
309     }
310   return ihc;
311 }
312
313
314 /**
315  * Cancel an iteration over peer information.
316  *
317  * @param ic context of the iterator to cancel
318  */
319 void
320 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
321 {
322   GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
323   GNUNET_free (ic);
324 }
325
326
327 /**
328  * Context for the info handler.
329  */
330 struct GNUNET_PEERINFO_NotifyContext
331 {
332
333   /**
334    * Our connection to the PEERINFO service.
335    */
336   struct GNUNET_CLIENT_Connection *client;
337
338   /**
339    * Function to call with information.
340    */
341   GNUNET_PEERINFO_Processor callback;
342
343   /**
344    * Closure for callback.
345    */
346   void *callback_cls;
347
348   /**
349    * Handle to our initial request for message transmission to
350    * the peerinfo service.
351    */
352   struct GNUNET_CLIENT_TransmitHandle *init;
353
354   /**
355    * Configuration.
356    */
357   const struct GNUNET_CONFIGURATION_Handle *cfg;
358
359   /**
360    * Scheduler.
361    */
362   struct GNUNET_SCHEDULER_Handle *sched;
363 };
364
365
366 /**
367  * Send a request to the peerinfo service to start being
368  * notified about all changes to peer information.
369  *
370  * @param nc our context
371  */
372 static void
373 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
374
375
376 /**
377  * Read notifications from the client handle and pass them
378  * to the callback.
379  *
380  * @param nc our context
381  */
382 static void
383 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
384
385
386 /**
387  * Receive a peerinfo information message, process it and
388  * go for more.
389  *
390  * @param cls closure
391  * @param msg message received, NULL on timeout or fatal error
392  */
393 static void
394 process_notification (void *cls,
395                       const struct
396                       GNUNET_MessageHeader * msg)
397 {
398   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
399   const struct InfoMessage *im;
400   const struct GNUNET_HELLO_Message *hello;
401   uint16_t ms;
402
403   if (msg == NULL)
404     {
405       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
406       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
407       request_notifications (nc);
408       return;
409     }
410   ms = ntohs (msg->size);
411   if ((ms < sizeof (struct InfoMessage)) ||
412       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
413     {
414       GNUNET_break (0);
415       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
416       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
417       request_notifications (nc);
418       return;
419     }
420   im = (const struct InfoMessage *) msg;
421   hello = NULL;
422   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
423     {
424       hello = (const struct GNUNET_HELLO_Message *) &im[1];
425       if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
426         {
427           GNUNET_break (0);
428           GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
429           nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
430           request_notifications (nc);
431           return;
432         }
433     }
434 #if DEBUG_PEERINFO
435   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436               "Received information about peer `%s' from peerinfo database\n",
437               GNUNET_i2s (&im->peer));
438 #endif
439   nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
440   receive_notifications (nc);
441 }
442
443
444 /**
445  * Read notifications from the client handle and pass them
446  * to the callback.
447  *
448  * @param nc our context
449  */
450 static void
451 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
452 {
453   GNUNET_CLIENT_receive (nc->client,
454                          &process_notification,
455                          nc,
456                          GNUNET_TIME_UNIT_FOREVER_REL);
457 }
458
459
460 /**
461  * Transmit our init-notify request, start receiving.
462  *
463  * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
464  * @param size number of bytes available in buf
465  * @param buf where the callee should write the message
466  * @return number of bytes written to buf
467  */
468 static size_t 
469 transmit_notify_request (void *cls,
470                          size_t size, 
471                          void *buf)
472 {
473   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
474   struct GNUNET_MessageHeader hdr;
475
476   nc->init = NULL;
477   if (buf == NULL)
478     {
479       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
480       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
481       request_notifications (nc);
482       return 0;
483     }
484   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
485   hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
486   hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
487   memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
488   receive_notifications (nc);
489   return sizeof (struct GNUNET_MessageHeader);
490 }
491
492
493 /**
494  * Send a request to the peerinfo service to start being
495  * notified about all changes to peer information.
496  *
497  * @param nc our context
498  */
499 static void
500 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
501 {
502   GNUNET_assert (NULL == nc->init);
503   nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
504                                                  sizeof (struct GNUNET_MessageHeader),
505                                                  GNUNET_TIME_UNIT_FOREVER_REL,
506                                                  GNUNET_YES,
507                                                  &transmit_notify_request,
508                                                  nc);
509 }
510
511
512 /**
513  * Call a method whenever our known information about peers
514  * changes.  Initially calls the given function for all known
515  * peers and then only signals changes.
516  *
517  * @param cfg configuration to use
518  * @param sched scheduler to use
519  * @param callback the method to call for each peer
520  * @param callback_cls closure for callback
521  * @return NULL on error
522  */
523 struct GNUNET_PEERINFO_NotifyContext *
524 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
525                         struct GNUNET_SCHEDULER_Handle *sched,
526                         GNUNET_PEERINFO_Processor callback,
527                         void *callback_cls)
528 {
529   struct GNUNET_PEERINFO_NotifyContext *nc;
530   struct GNUNET_CLIENT_Connection *client;
531
532   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
533   if (client == NULL)
534     {      
535       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
536                   _("Could not connect to `%s' service.\n"), "peerinfo");
537       return NULL;
538     }
539   nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
540   nc->sched = sched;
541   nc->cfg = cfg;
542   nc->client = client;
543   nc->callback = callback;
544   nc->callback_cls = callback_cls; 
545   request_notifications (nc);
546   return nc;
547 }
548
549
550 /**
551  * Stop notifying about changes.
552  *
553  * @param nc context to stop notifying
554  */
555 void
556 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
557 {
558   if (NULL != nc->init)
559     {
560       GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
561       nc->init = NULL;
562     }
563   GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
564   GNUNET_free (nc);
565 }
566
567
568 /* end of peerinfo_api.c */