2a7c990e4a604c67c174d5b2e22a16dc0be230e5
[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 information about peer `%s' from peerinfo database\n",
220               GNUNET_i2s (&im->peer));
221 #endif
222 #if DEBUG_PEERINFO
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Size of `%s' is %u bytes\n",
225               "HELLO",
226               (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello));
227 #endif  
228   ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
229   GNUNET_CLIENT_receive (ic->client,
230                          &info_handler,
231                          ic,
232                          GNUNET_TIME_absolute_get_remaining (ic->timeout));
233 }
234
235
236 /**
237  * Call a method for each known matching host and change
238  * its trust value.  The method will be invoked once for
239  * each host and then finally once with a NULL pointer.
240  * Note that the last call can be triggered by timeout or
241  * by simply being done; however, the trust argument will
242  * be set to zero if we are done and to 1 if we timed out.
243  *
244  * @param cfg configuration to use
245  * @param sched scheduler to use
246  * @param peer restrict iteration to this peer only (can be NULL)
247  * @param trust_delta how much to change the trust in all matching peers
248  * @param timeout how long to wait until timing out
249  * @param callback the method to call for each peer
250  * @param callback_cls closure for callback
251  * @return NULL on error, otherwise an iterator context
252  */
253 struct GNUNET_PEERINFO_IteratorContext *
254 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
255                          struct GNUNET_SCHEDULER_Handle *sched,
256                          const struct GNUNET_PeerIdentity *peer,
257                          int trust_delta,
258                          struct GNUNET_TIME_Relative timeout,
259                          GNUNET_PEERINFO_Processor callback,
260                          void *callback_cls)
261 {
262   struct GNUNET_CLIENT_Connection *client;
263   struct ListAllPeersMessage *lapm;
264   struct ListPeerMessage *lpm;
265   struct GNUNET_PEERINFO_IteratorContext *ihc;
266
267   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
268   if (client == NULL)
269     {
270       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271                   _("Could not connect to `%s' service.\n"), "peerinfo");
272       return NULL;
273     }
274 #if DEBUG_PEERINFO
275   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276               "Requesting list of peers from peerinfo database\n");
277 #endif
278   if (peer == NULL)
279     {
280       ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
281                            sizeof (struct ListAllPeersMessage));
282       lapm = (struct ListAllPeersMessage *) &ihc[1];
283       lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
284       lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
285       lapm->trust_change = htonl (trust_delta);
286     }
287   else
288     {
289       ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
290                            sizeof (struct ListPeerMessage));
291       lpm = (struct ListPeerMessage *) &ihc[1];
292       lpm->header.size = htons (sizeof (struct ListPeerMessage));
293       lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
294       lpm->trust_change = htonl (trust_delta);
295       memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
296     }
297   ihc->client = client;
298   ihc->callback = callback;
299   ihc->callback_cls = callback_cls;
300   ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
301   if (GNUNET_OK != 
302       GNUNET_CLIENT_transmit_and_get_response (client,
303                                                (const struct GNUNET_MessageHeader*) &ihc[1],
304                                                timeout,
305                                                GNUNET_YES,
306                                                &info_handler,
307                                                ihc))
308     {
309       GNUNET_break (0);
310       GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
311       GNUNET_free (ihc);
312       return NULL;
313     }
314   return ihc;
315 }
316
317
318 /**
319  * Cancel an iteration over peer information.
320  *
321  * @param ic context of the iterator to cancel
322  */
323 void
324 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
325 {
326   GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
327   GNUNET_free (ic);
328 }
329
330
331 /**
332  * Context for the info handler.
333  */
334 struct GNUNET_PEERINFO_NotifyContext
335 {
336
337   /**
338    * Our connection to the PEERINFO service.
339    */
340   struct GNUNET_CLIENT_Connection *client;
341
342   /**
343    * Function to call with information.
344    */
345   GNUNET_PEERINFO_Processor callback;
346
347   /**
348    * Closure for callback.
349    */
350   void *callback_cls;
351
352   /**
353    * Handle to our initial request for message transmission to
354    * the peerinfo service.
355    */
356   struct GNUNET_CLIENT_TransmitHandle *init;
357
358   /**
359    * Configuration.
360    */
361   const struct GNUNET_CONFIGURATION_Handle *cfg;
362
363   /**
364    * Scheduler.
365    */
366   struct GNUNET_SCHEDULER_Handle *sched;
367 };
368
369
370 /**
371  * Send a request to the peerinfo service to start being
372  * notified about all changes to peer information.
373  *
374  * @param nc our context
375  */
376 static void
377 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
378
379
380 /**
381  * Read notifications from the client handle and pass them
382  * to the callback.
383  *
384  * @param nc our context
385  */
386 static void
387 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
388
389
390 /**
391  * Receive a peerinfo information message, process it and
392  * go for more.
393  *
394  * @param cls closure
395  * @param msg message received, NULL on timeout or fatal error
396  */
397 static void
398 process_notification (void *cls,
399                       const struct
400                       GNUNET_MessageHeader * msg)
401 {
402   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
403   const struct InfoMessage *im;
404   const struct GNUNET_HELLO_Message *hello;
405   uint16_t ms;
406
407   if (msg == NULL)
408     {
409       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
410       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
411       request_notifications (nc);
412       return;
413     }
414   ms = ntohs (msg->size);
415   if ((ms < sizeof (struct InfoMessage)) ||
416       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
417     {
418       GNUNET_break (0);
419       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
420       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
421       request_notifications (nc);
422       return;
423     }
424   im = (const struct InfoMessage *) msg;
425   hello = NULL;
426   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
427     {
428       hello = (const struct GNUNET_HELLO_Message *) &im[1];
429       if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
430         {
431           GNUNET_break (0);
432           GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
433           nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
434           request_notifications (nc);
435           return;
436         }
437     }
438 #if DEBUG_PEERINFO
439   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440               "Received information about peer `%s' from peerinfo database\n",
441               GNUNET_i2s (&im->peer));
442 #endif
443   nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
444   receive_notifications (nc);
445 }
446
447
448 /**
449  * Read notifications from the client handle and pass them
450  * to the callback.
451  *
452  * @param nc our context
453  */
454 static void
455 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
456 {
457   GNUNET_CLIENT_receive (nc->client,
458                          &process_notification,
459                          nc,
460                          GNUNET_TIME_UNIT_FOREVER_REL);
461 }
462
463
464 /**
465  * Transmit our init-notify request, start receiving.
466  *
467  * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
468  * @param size number of bytes available in buf
469  * @param buf where the callee should write the message
470  * @return number of bytes written to buf
471  */
472 static size_t 
473 transmit_notify_request (void *cls,
474                          size_t size, 
475                          void *buf)
476 {
477   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
478   struct GNUNET_MessageHeader hdr;
479
480   nc->init = NULL;
481   if (buf == NULL)
482     {
483       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
484       nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
485       request_notifications (nc);
486       return 0;
487     }
488   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
489   hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
490   hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
491   memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
492   receive_notifications (nc);
493   return sizeof (struct GNUNET_MessageHeader);
494 }
495
496
497 /**
498  * Send a request to the peerinfo service to start being
499  * notified about all changes to peer information.
500  *
501  * @param nc our context
502  */
503 static void
504 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
505 {
506   GNUNET_assert (NULL == nc->init);
507   nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
508                                                  sizeof (struct GNUNET_MessageHeader),
509                                                  GNUNET_TIME_UNIT_FOREVER_REL,
510                                                  GNUNET_YES,
511                                                  &transmit_notify_request,
512                                                  nc);
513 }
514
515
516 /**
517  * Call a method whenever our known information about peers
518  * changes.  Initially calls the given function for all known
519  * peers and then only signals changes.
520  *
521  * @param cfg configuration to use
522  * @param sched scheduler to use
523  * @param callback the method to call for each peer
524  * @param callback_cls closure for callback
525  * @return NULL on error
526  */
527 struct GNUNET_PEERINFO_NotifyContext *
528 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
529                         struct GNUNET_SCHEDULER_Handle *sched,
530                         GNUNET_PEERINFO_Processor callback,
531                         void *callback_cls)
532 {
533   struct GNUNET_PEERINFO_NotifyContext *nc;
534   struct GNUNET_CLIENT_Connection *client;
535
536   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
537   if (client == NULL)
538     {      
539       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540                   _("Could not connect to `%s' service.\n"), "peerinfo");
541       return NULL;
542     }
543   nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
544   nc->sched = sched;
545   nc->cfg = cfg;
546   nc->client = client;
547   nc->callback = callback;
548   nc->callback_cls = callback_cls; 
549   request_notifications (nc);
550   return nc;
551 }
552
553
554 /**
555  * Stop notifying about changes.
556  *
557  * @param nc context to stop notifying
558  */
559 void
560 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
561 {
562   if (NULL != nc->init)
563     {
564       GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
565       nc->init = NULL;
566     }
567   GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
568   GNUNET_free (nc);
569 }
570
571
572 /* end of peerinfo_api.c */