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, GNUNET_NO);
63 msize = ntohs (msg->size);
64 GNUNET_assert (size >= msize);
65 memcpy (buf, msg, msize);
67 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
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, GNUNET_NO);
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, GNUNET_NO);
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, GNUNET_NO);
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, GNUNET_NO);
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),
222 GNUNET_i2s (&im->peer));
224 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
225 GNUNET_CLIENT_receive (ic->client,
228 GNUNET_TIME_absolute_get_remaining (ic->timeout));
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.
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
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,
254 struct GNUNET_TIME_Relative timeout,
255 GNUNET_PEERINFO_Processor callback,
258 struct GNUNET_CLIENT_Connection *client;
259 struct ListAllPeersMessage *lapm;
260 struct ListPeerMessage *lpm;
261 struct GNUNET_PEERINFO_IteratorContext *ihc;
263 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267 _("Could not connect to `%s' service.\n"), "peerinfo");
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Requesting list of peers from peerinfo database\n");
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);
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));
293 ihc->client = client;
294 ihc->callback = callback;
295 ihc->callback_cls = callback_cls;
296 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
298 GNUNET_CLIENT_transmit_and_get_response (client,
299 (const struct GNUNET_MessageHeader*) &ihc[1],
306 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
315 * Cancel an iteration over peer information.
317 * @param ic context of the iterator to cancel
320 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
322 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
329 /* end of peerinfo_api.c */