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);
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Size of `%s' is %u bytes\n",
110 (unsigned int) GNUNET_HELLO_size (hello));
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));
119 cc->msg = &pam->header;
120 GNUNET_CLIENT_notify_transmit_ready (client,
121 ntohs (pam->header.size),
129 * Context for the info handler.
131 struct GNUNET_PEERINFO_IteratorContext
135 * Our connection to the PEERINFO service.
137 struct GNUNET_CLIENT_Connection *client;
140 * Function to call with information.
142 GNUNET_PEERINFO_Processor callback;
145 * Closure for callback.
150 * When should we time out?
152 struct GNUNET_TIME_Absolute timeout;
158 * Type of a function to call when we receive a message
162 * @param msg message received, NULL on timeout or fatal error
165 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
167 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
168 const struct InfoMessage *im;
169 const struct GNUNET_HELLO_Message *hello;
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
175 _("Failed to receive response from `%s' service.\n"),
177 ic->callback (ic->callback_cls, NULL, NULL, 1);
178 GNUNET_CLIENT_disconnect (ic->client);
182 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Received end of list of peers from peerinfo database\n");
188 ic->callback (ic->callback_cls, NULL, NULL, 0);
189 GNUNET_CLIENT_disconnect (ic->client);
193 ms = ntohs (msg->size);
194 if ((ms < sizeof (struct InfoMessage)) ||
195 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
198 ic->callback (ic->callback_cls, NULL, NULL, 2);
199 GNUNET_CLIENT_disconnect (ic->client);
203 im = (const struct InfoMessage *) msg;
205 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
207 hello = (const struct GNUNET_HELLO_Message *) &im[1];
208 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
211 ic->callback (ic->callback_cls, NULL, NULL, 2);
212 GNUNET_CLIENT_disconnect (ic->client);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Received information about peer `%s' from peerinfo database\n",
220 GNUNET_i2s (&im->peer));
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Size of `%s' is %u bytes\n",
226 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello));
228 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
229 GNUNET_CLIENT_receive (ic->client,
232 GNUNET_TIME_absolute_get_remaining (ic->timeout));
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.
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
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,
258 struct GNUNET_TIME_Relative timeout,
259 GNUNET_PEERINFO_Processor callback,
262 struct GNUNET_CLIENT_Connection *client;
263 struct ListAllPeersMessage *lapm;
264 struct ListPeerMessage *lpm;
265 struct GNUNET_PEERINFO_IteratorContext *ihc;
267 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("Could not connect to `%s' service.\n"), "peerinfo");
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Requesting list of peers from peerinfo database\n");
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);
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));
297 ihc->client = client;
298 ihc->callback = callback;
299 ihc->callback_cls = callback_cls;
300 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
302 GNUNET_CLIENT_transmit_and_get_response (client,
303 (const struct GNUNET_MessageHeader*) &ihc[1],
310 GNUNET_CLIENT_disconnect (ihc->client);
319 * Cancel an iteration over peer information.
321 * @param ic context of the iterator to cancel
324 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
326 GNUNET_CLIENT_disconnect (ic->client);
332 * Context for the info handler.
334 struct GNUNET_PEERINFO_NotifyContext
338 * Our connection to the PEERINFO service.
340 struct GNUNET_CLIENT_Connection *client;
343 * Function to call with information.
345 GNUNET_PEERINFO_Processor callback;
348 * Closure for callback.
353 * Handle to our initial request for message transmission to
354 * the peerinfo service.
356 struct GNUNET_CLIENT_TransmitHandle *init;
361 const struct GNUNET_CONFIGURATION_Handle *cfg;
366 struct GNUNET_SCHEDULER_Handle *sched;
371 * Send a request to the peerinfo service to start being
372 * notified about all changes to peer information.
374 * @param nc our context
377 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
381 * Read notifications from the client handle and pass them
384 * @param nc our context
387 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
391 * Receive a peerinfo information message, process it and
395 * @param msg message received, NULL on timeout or fatal error
398 process_notification (void *cls,
400 GNUNET_MessageHeader * msg)
402 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
403 const struct InfoMessage *im;
404 const struct GNUNET_HELLO_Message *hello;
409 GNUNET_CLIENT_disconnect (nc->client);
410 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
411 request_notifications (nc);
414 ms = ntohs (msg->size);
415 if ((ms < sizeof (struct InfoMessage)) ||
416 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
419 GNUNET_CLIENT_disconnect (nc->client);
420 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
421 request_notifications (nc);
424 im = (const struct InfoMessage *) msg;
426 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
428 hello = (const struct GNUNET_HELLO_Message *) &im[1];
429 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
432 GNUNET_CLIENT_disconnect (nc->client);
433 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
434 request_notifications (nc);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Received information about peer `%s' from peerinfo database\n",
441 GNUNET_i2s (&im->peer));
443 nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
444 receive_notifications (nc);
449 * Read notifications from the client handle and pass them
452 * @param nc our context
455 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
457 GNUNET_CLIENT_receive (nc->client,
458 &process_notification,
460 GNUNET_TIME_UNIT_FOREVER_REL);
465 * Transmit our init-notify request, start receiving.
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
473 transmit_notify_request (void *cls,
477 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
478 struct GNUNET_MessageHeader hdr;
483 GNUNET_CLIENT_disconnect (nc->client);
484 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
485 request_notifications (nc);
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);
498 * Send a request to the peerinfo service to start being
499 * notified about all changes to peer information.
501 * @param nc our context
504 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
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,
511 &transmit_notify_request,
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.
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
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,
533 struct GNUNET_PEERINFO_NotifyContext *nc;
534 struct GNUNET_CLIENT_Connection *client;
536 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540 _("Could not connect to `%s' service.\n"), "peerinfo");
543 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
547 nc->callback = callback;
548 nc->callback_cls = callback_cls;
549 request_notifications (nc);
555 * Stop notifying about changes.
557 * @param nc context to stop notifying
560 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
562 if (NULL != nc->init)
564 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
567 GNUNET_CLIENT_disconnect (nc->client);
572 /* end of peerinfo_api.c */