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;
52 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
54 ("Failed to transmit message of type %u to `%s' service.\n"),
55 ntohs (msg->type), "peerinfo");
57 GNUNET_CLIENT_disconnect (cc->client);
61 msize = ntohs (msg->size);
62 GNUNET_assert (size >= msize);
63 memcpy (buf, msg, msize);
65 GNUNET_CLIENT_disconnect (cc->client);
73 * Add a host to the persistent list.
75 * @param cfg configuration to use
76 * @param sched scheduler to use
77 * @param peer identity of the peer
78 * @param hello the verified (!) HELLO message
81 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
82 struct GNUNET_SCHEDULER_Handle *sched,
83 const struct GNUNET_PeerIdentity *peer,
84 const struct GNUNET_HELLO_Message *hello)
86 struct GNUNET_CLIENT_Connection *client;
87 struct PeerAddMessage *pam;
89 struct CAFContext *cc;
91 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
94 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
95 _("Could not connect to `%s' service.\n"), "peerinfo");
98 hs = GNUNET_HELLO_size (hello);
99 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
100 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
101 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
102 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
103 memcpy (&pam[1], hello, hs);
104 cc = GNUNET_malloc (sizeof (struct CAFContext));
106 cc->msg = &pam->header;
107 GNUNET_CLIENT_notify_transmit_ready (client,
108 ntohs (pam->header.size),
109 ADD_PEER_TIMEOUT, ©_and_free, cc);
114 * Context for the info handler.
120 * Our connection to the PEERINFO service.
122 struct GNUNET_CLIENT_Connection *client;
125 * Function to call with information.
127 GNUNET_PEERINFO_Processor callback;
130 * Closure for callback.
135 * When should we time out?
137 struct GNUNET_TIME_Absolute timeout;
143 * Type of a function to call when we receive a message
147 * @param msg message received, NULL on timeout or fatal error
150 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
152 struct InfoContext *ic = cls;
153 const struct InfoMessage *im;
154 const struct GNUNET_HELLO_Message *hello;
159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
160 _("Failed to receive response from `%s' service.\n"),
162 ic->callback (ic->callback_cls, NULL, NULL, 1);
163 GNUNET_CLIENT_disconnect (ic->client);
167 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
169 ic->callback (ic->callback_cls, NULL, NULL, 0);
170 GNUNET_CLIENT_disconnect (ic->client);
174 ms = ntohs (msg->size);
175 if ((ms < sizeof (struct InfoMessage)) ||
176 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
179 ic->callback (ic->callback_cls, NULL, NULL, 2);
180 GNUNET_CLIENT_disconnect (ic->client);
184 im = (const struct InfoMessage *) msg;
186 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
188 hello = (const struct GNUNET_HELLO_Message *) &im[1];
189 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
192 ic->callback (ic->callback_cls, NULL, NULL, 2);
193 GNUNET_CLIENT_disconnect (ic->client);
198 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
199 GNUNET_CLIENT_receive (ic->client,
202 GNUNET_TIME_absolute_get_remaining (ic->timeout));
207 copy_then_receive (void *cls, size_t size, void *buf)
209 struct InfoContext *ic = cls;
210 const struct GNUNET_MessageHeader *msg =
211 (const struct GNUNET_MessageHeader *) &ic[1];
216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 ("Failed to transmit message of type %u to `%s' service.\n"),
219 ntohs (msg->type), "peerinfo");
220 ic->callback (ic->callback_cls, NULL, NULL, 1);
221 GNUNET_CLIENT_disconnect (ic->client);
225 msize = ntohs (msg->size);
226 GNUNET_assert (size >= msize);
227 memcpy (buf, msg, msize);
228 GNUNET_CLIENT_receive (ic->client,
231 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
253 GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
254 struct GNUNET_SCHEDULER_Handle *sched,
255 const struct GNUNET_PeerIdentity *peer,
257 struct GNUNET_TIME_Relative timeout,
258 GNUNET_PEERINFO_Processor callback,
261 struct GNUNET_CLIENT_Connection *client;
262 struct ListAllPeersMessage *lapm;
263 struct ListPeerMessage *lpm;
264 struct InfoContext *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");
272 callback (callback_cls, NULL, NULL, 2);
275 ihc = GNUNET_malloc (sizeof (struct InfoContext) +
276 sizeof (struct ListPeerMessage));
277 ihc->client = client;
278 ihc->callback = callback;
279 ihc->callback_cls = callback_cls;
280 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
283 lapm = (struct ListAllPeersMessage *) &ihc[1];
284 lapm->header.size = htons (hs = sizeof (struct ListAllPeersMessage));
285 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
286 lapm->trust_change = htonl (trust_delta);
290 lpm = (struct ListPeerMessage *) &ihc[1];
291 lpm->header.size = htons (hs = sizeof (struct ListPeerMessage));
292 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
293 lpm->trust_change = htonl (trust_delta);
294 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
296 GNUNET_CLIENT_notify_transmit_ready (client,
297 hs, timeout, ©_then_receive, ihc);
300 /* end of peerinfo_api.c */