2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
22 * @file peerinfo/peerinfo_api.c
23 * @brief API to access peerinfo service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
33 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 struct GNUNET_CLIENT_Connection *client;
39 struct GNUNET_MessageHeader *msg;
44 copy_and_free (void *cls, size_t size, void *buf)
46 struct CAFContext *cc = cls;
47 struct GNUNET_MessageHeader *msg = cc->msg;
53 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
55 ("Failed to transmit message of type %u to `%s' service.\n"),
56 ntohs (msg->type), "peerinfo");
59 GNUNET_CLIENT_disconnect (cc->client);
63 msize = ntohs (msg->size);
64 GNUNET_assert (size >= msize);
65 memcpy (buf, msg, msize);
67 GNUNET_CLIENT_disconnect (cc->client);
75 * Add a host to the persistent list.
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
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)
88 struct GNUNET_CLIENT_Connection *client;
89 struct PeerAddMessage *pam;
91 struct CAFContext *cc;
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 "Adding peer `%s' to peerinfo database\n",
98 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102 _("Could not connect to `%s' service.\n"), "peerinfo");
105 hs = GNUNET_HELLO_size (hello);
106 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
107 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
108 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
109 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
110 memcpy (&pam[1], hello, hs);
111 cc = GNUNET_malloc (sizeof (struct CAFContext));
113 cc->msg = &pam->header;
114 GNUNET_CLIENT_notify_transmit_ready (client,
115 ntohs (pam->header.size),
123 * Context for the info handler.
125 struct GNUNET_PEERINFO_IteratorContext
129 * Our connection to the PEERINFO service.
131 struct GNUNET_CLIENT_Connection *client;
134 * Function to call with information.
136 GNUNET_PEERINFO_Processor callback;
139 * Closure for callback.
144 * When should we time out?
146 struct GNUNET_TIME_Absolute timeout;
152 * Type of a function to call when we receive a message
156 * @param msg message received, NULL on timeout or fatal error
159 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
161 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
162 const struct InfoMessage *im;
163 const struct GNUNET_HELLO_Message *hello;
168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
169 _("Failed to receive response from `%s' service.\n"),
171 ic->callback (ic->callback_cls, NULL, NULL, 1);
172 GNUNET_CLIENT_disconnect (ic->client);
176 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Received end of list of peers from peerinfo database\n");
182 ic->callback (ic->callback_cls, NULL, NULL, 0);
183 GNUNET_CLIENT_disconnect (ic->client);
187 ms = ntohs (msg->size);
188 if ((ms < sizeof (struct InfoMessage)) ||
189 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
192 ic->callback (ic->callback_cls, NULL, NULL, 2);
193 GNUNET_CLIENT_disconnect (ic->client);
197 im = (const struct InfoMessage *) msg;
199 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
201 hello = (const struct GNUNET_HELLO_Message *) &im[1];
202 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
205 ic->callback (ic->callback_cls, NULL, NULL, 2);
206 GNUNET_CLIENT_disconnect (ic->client);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Received information about peer `%s' from peerinfo database\n",
214 GNUNET_i2s (&im->peer));
216 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
217 GNUNET_CLIENT_receive (ic->client,
220 GNUNET_TIME_absolute_get_remaining (ic->timeout));
225 * Call a method for each known matching host and change
226 * its trust value. The method will be invoked once for
227 * each host and then finally once with a NULL pointer.
228 * Note that the last call can be triggered by timeout or
229 * by simply being done; however, the trust argument will
230 * be set to zero if we are done and to 1 if we timed out.
232 * @param cfg configuration to use
233 * @param sched scheduler to use
234 * @param peer restrict iteration to this peer only (can be NULL)
235 * @param trust_delta how much to change the trust in all matching peers
236 * @param timeout how long to wait until timing out
237 * @param callback the method to call for each peer
238 * @param callback_cls closure for callback
239 * @return NULL on error, otherwise an iterator context
241 struct GNUNET_PEERINFO_IteratorContext *
242 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
243 struct GNUNET_SCHEDULER_Handle *sched,
244 const struct GNUNET_PeerIdentity *peer,
246 struct GNUNET_TIME_Relative timeout,
247 GNUNET_PEERINFO_Processor callback,
250 struct GNUNET_CLIENT_Connection *client;
251 struct ListAllPeersMessage *lapm;
252 struct ListPeerMessage *lpm;
253 struct GNUNET_PEERINFO_IteratorContext *ihc;
255 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
259 _("Could not connect to `%s' service.\n"), "peerinfo");
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Requesting list of peers from peerinfo database\n");
268 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
269 sizeof (struct ListAllPeersMessage));
270 lapm = (struct ListAllPeersMessage *) &ihc[1];
271 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
272 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
273 lapm->trust_change = htonl (trust_delta);
277 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
278 sizeof (struct ListPeerMessage));
279 lpm = (struct ListPeerMessage *) &ihc[1];
280 lpm->header.size = htons (sizeof (struct ListPeerMessage));
281 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
282 lpm->trust_change = htonl (trust_delta);
283 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
285 ihc->client = client;
286 ihc->callback = callback;
287 ihc->callback_cls = callback_cls;
288 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
290 GNUNET_CLIENT_transmit_and_get_response (client,
291 (const struct GNUNET_MessageHeader*) &ihc[1],
298 GNUNET_CLIENT_disconnect (ihc->client);
307 * Cancel an iteration over peer information.
309 * @param ic context of the iterator to cancel
312 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
314 GNUNET_CLIENT_disconnect (ic->client);
320 * Context for the info handler.
322 struct GNUNET_PEERINFO_NotifyContext
326 * Our connection to the PEERINFO service.
328 struct GNUNET_CLIENT_Connection *client;
331 * Function to call with information.
333 GNUNET_PEERINFO_Processor callback;
336 * Closure for callback.
341 * Handle to our initial request for message transmission to
342 * the peerinfo service.
344 struct GNUNET_CLIENT_TransmitHandle *init;
349 const struct GNUNET_CONFIGURATION_Handle *cfg;
354 struct GNUNET_SCHEDULER_Handle *sched;
359 * Send a request to the peerinfo service to start being
360 * notified about all changes to peer information.
362 * @param nc our context
365 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
369 * Read notifications from the client handle and pass them
372 * @param nc our context
375 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
379 * Receive a peerinfo information message, process it and
383 * @param msg message received, NULL on timeout or fatal error
386 process_notification (void *cls,
388 GNUNET_MessageHeader * msg)
390 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
391 const struct InfoMessage *im;
392 const struct GNUNET_HELLO_Message *hello;
397 GNUNET_CLIENT_disconnect (nc->client);
398 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
399 request_notifications (nc);
402 ms = ntohs (msg->size);
403 if ((ms < sizeof (struct InfoMessage)) ||
404 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
407 GNUNET_CLIENT_disconnect (nc->client);
408 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
409 request_notifications (nc);
412 im = (const struct InfoMessage *) msg;
414 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
416 hello = (const struct GNUNET_HELLO_Message *) &im[1];
417 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
420 GNUNET_CLIENT_disconnect (nc->client);
421 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
422 request_notifications (nc);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Received information about peer `%s' from peerinfo database\n",
429 GNUNET_i2s (&im->peer));
431 nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
432 receive_notifications (nc);
437 * Read notifications from the client handle and pass them
440 * @param nc our context
443 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
445 GNUNET_CLIENT_receive (nc->client,
446 &process_notification,
448 GNUNET_TIME_UNIT_FOREVER_REL);
453 * Transmit our init-notify request, start receiving.
455 * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
456 * @param size number of bytes available in buf
457 * @param buf where the callee should write the message
458 * @return number of bytes written to buf
461 transmit_notify_request (void *cls,
465 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
466 struct GNUNET_MessageHeader hdr;
471 GNUNET_CLIENT_disconnect (nc->client);
472 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
473 request_notifications (nc);
476 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
477 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
478 hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
479 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
480 receive_notifications (nc);
481 return sizeof (struct GNUNET_MessageHeader);
486 * Send a request to the peerinfo service to start being
487 * notified about all changes to peer information.
489 * @param nc our context
492 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
494 GNUNET_assert (NULL == nc->init);
495 nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
496 sizeof (struct GNUNET_MessageHeader),
497 GNUNET_TIME_UNIT_FOREVER_REL,
499 &transmit_notify_request,
505 * Call a method whenever our known information about peers
506 * changes. Initially calls the given function for all known
507 * peers and then only signals changes.
509 * @param cfg configuration to use
510 * @param sched scheduler to use
511 * @param callback the method to call for each peer
512 * @param callback_cls closure for callback
513 * @return NULL on error
515 struct GNUNET_PEERINFO_NotifyContext *
516 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
517 struct GNUNET_SCHEDULER_Handle *sched,
518 GNUNET_PEERINFO_Processor callback,
521 struct GNUNET_PEERINFO_NotifyContext *nc;
522 struct GNUNET_CLIENT_Connection *client;
524 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
528 _("Could not connect to `%s' service.\n"), "peerinfo");
531 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
535 nc->callback = callback;
536 nc->callback_cls = callback_cls;
537 request_notifications (nc);
543 * Stop notifying about changes.
545 * @param nc context to stop notifying
548 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
550 if (NULL != nc->init)
552 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
555 GNUNET_CLIENT_disconnect (nc->client);
560 /* end of peerinfo_api.c */