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),
116 * Context for the info handler.
122 * Our connection to the PEERINFO service.
124 struct GNUNET_CLIENT_Connection *client;
127 * Function to call with information.
129 GNUNET_PEERINFO_Processor callback;
132 * Closure for callback.
137 * When should we time out?
139 struct GNUNET_TIME_Absolute timeout;
145 * Type of a function to call when we receive a message
149 * @param msg message received, NULL on timeout or fatal error
152 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
154 struct InfoContext *ic = cls;
155 const struct InfoMessage *im;
156 const struct GNUNET_HELLO_Message *hello;
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
162 _("Failed to receive response from `%s' service.\n"),
164 ic->callback (ic->callback_cls, NULL, NULL, 1);
165 GNUNET_CLIENT_disconnect (ic->client);
169 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
171 ic->callback (ic->callback_cls, NULL, NULL, 0);
172 GNUNET_CLIENT_disconnect (ic->client);
176 ms = ntohs (msg->size);
177 if ((ms < sizeof (struct InfoMessage)) ||
178 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
181 ic->callback (ic->callback_cls, NULL, NULL, 2);
182 GNUNET_CLIENT_disconnect (ic->client);
186 im = (const struct InfoMessage *) msg;
188 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
190 hello = (const struct GNUNET_HELLO_Message *) &im[1];
191 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
194 ic->callback (ic->callback_cls, NULL, NULL, 2);
195 GNUNET_CLIENT_disconnect (ic->client);
200 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
201 GNUNET_CLIENT_receive (ic->client,
204 GNUNET_TIME_absolute_get_remaining (ic->timeout));
209 * Call a method for each known matching host and change
210 * its trust value. The method will be invoked once for
211 * each host and then finally once with a NULL pointer.
212 * Note that the last call can be triggered by timeout or
213 * by simply being done; however, the trust argument will
214 * be set to zero if we are done and to 1 if we timed out.
216 * @param cfg configuration to use
217 * @param sched scheduler to use
218 * @param peer restrict iteration to this peer only (can be NULL)
219 * @param trust_delta how much to change the trust in all matching peers
220 * @param timeout how long to wait until timing out
221 * @param callback the method to call for each peer
222 * @param callback_cls closure for callback
225 GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
226 struct GNUNET_SCHEDULER_Handle *sched,
227 const struct GNUNET_PeerIdentity *peer,
229 struct GNUNET_TIME_Relative timeout,
230 GNUNET_PEERINFO_Processor callback,
233 struct GNUNET_CLIENT_Connection *client;
234 struct ListAllPeersMessage *lapm;
235 struct ListPeerMessage *lpm;
236 struct InfoContext *ihc;
239 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243 _("Could not connect to `%s' service.\n"), "peerinfo");
244 callback (callback_cls, NULL, NULL, 2);
247 ihc = GNUNET_malloc (sizeof (struct InfoContext) +
248 sizeof (struct ListPeerMessage));
249 ihc->client = client;
250 ihc->callback = callback;
251 ihc->callback_cls = callback_cls;
252 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
255 lapm = (struct ListAllPeersMessage *) &ihc[1];
256 lapm->header.size = htons (hs = sizeof (struct ListAllPeersMessage));
257 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
258 lapm->trust_change = htonl (trust_delta);
262 lpm = (struct ListPeerMessage *) &ihc[1];
263 lpm->header.size = htons (hs = sizeof (struct ListPeerMessage));
264 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
265 lpm->trust_change = htonl (trust_delta);
266 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
269 GNUNET_CLIENT_transmit_and_get_response (client,
270 (const struct GNUNET_MessageHeader*) &ihc[1],
277 ihc->callback (ihc->callback_cls, NULL, NULL, 1);
278 GNUNET_CLIENT_disconnect (ihc->client);
284 /* end of peerinfo_api.c */