move
[oweals/gnunet.git] / src / peerinfo / peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file peerinfo/peerinfo_api.c
23  * @brief API to access peerinfo service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
31 #include "peerinfo.h"
32
33 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
34
35
36 struct CAFContext
37 {
38   struct GNUNET_CLIENT_Connection *client;
39   struct GNUNET_MessageHeader *msg;
40 };
41
42
43 static size_t
44 copy_and_free (void *cls, size_t size, void *buf)
45 {
46   struct CAFContext *cc = cls;
47   struct GNUNET_MessageHeader *msg = cc->msg;
48   uint16_t msize;
49
50   if (buf == NULL)
51     {
52 #if DEBUG_PEERINFO
53       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
54                   _
55                   ("Failed to transmit message of type %u to `%s' service.\n"),
56                   ntohs (msg->type), "peerinfo");
57 #endif
58       GNUNET_free (msg);
59       GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
60       GNUNET_free (cc);
61       return 0;
62     }
63   msize = ntohs (msg->size);
64   GNUNET_assert (size >= msize);
65   memcpy (buf, msg, msize);
66   GNUNET_free (msg);
67   GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
68   GNUNET_free (cc);
69   return msize;
70 }
71
72
73
74 /**
75  * Add a host to the persistent list.
76  *
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
81  */
82 void
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)
87 {
88   struct GNUNET_CLIENT_Connection *client;
89   struct PeerAddMessage *pam;
90   uint16_t hs;
91   struct CAFContext *cc;
92
93 #if DEBUG_PEERINFO
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95               "Adding peer `%s' to peerinfo database\n",
96               GNUNET_i2s(peer));
97 #endif
98   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
99   if (client == NULL)
100     {
101       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102                   _("Could not connect to `%s' service.\n"), "peerinfo");
103       return;
104     }
105   hs = GNUNET_HELLO_size (hello);
106 #if DEBUG_PEERINFO
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Size of `%s' is %u bytes\n",
109               "HELLO",
110               (unsigned int) GNUNET_HELLO_size (hello));
111 #endif  
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));
118   cc->client = client;
119   cc->msg = &pam->header;
120   GNUNET_CLIENT_notify_transmit_ready (client,
121                                        ntohs (pam->header.size),
122                                        ADD_PEER_TIMEOUT, 
123                                        GNUNET_NO,
124                                        &copy_and_free, cc);
125 }
126
127
128 /**
129  * Context for the info handler.
130  */
131 struct GNUNET_PEERINFO_IteratorContext
132 {
133
134   /**
135    * Our connection to the PEERINFO service.
136    */
137   struct GNUNET_CLIENT_Connection *client;
138
139   /**
140    * Function to call with information.
141    */
142   GNUNET_PEERINFO_Processor callback;
143
144   /**
145    * Closure for callback.
146    */
147   void *callback_cls;
148
149   /**
150    * When should we time out?
151    */
152   struct GNUNET_TIME_Absolute timeout;
153
154 };
155
156
157 /**
158  * Type of a function to call when we receive a message
159  * from the service.
160  *
161  * @param cls closure
162  * @param msg message received, NULL on timeout or fatal error
163  */
164 static void
165 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
166 {
167   struct GNUNET_PEERINFO_IteratorContext *ic = cls;
168   const struct InfoMessage *im;
169   const struct GNUNET_HELLO_Message *hello;
170   uint16_t ms;
171
172   if (msg == NULL)
173     {
174       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
175                   _("Failed to receive response from `%s' service.\n"),
176                   "peerinfo");
177       ic->callback (ic->callback_cls, NULL, NULL, 1);
178       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
179       GNUNET_free (ic);
180       return;
181     }
182   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
183     {
184 #if DEBUG_PEERINFO
185       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186                   "Received end of list of peers from peerinfo database\n");
187 #endif
188       ic->callback (ic->callback_cls, NULL, NULL, 0);
189       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
190       GNUNET_free (ic);
191       return;
192     }
193   ms = ntohs (msg->size);
194   if ((ms < sizeof (struct InfoMessage)) ||
195       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
196     {
197       GNUNET_break (0);
198       ic->callback (ic->callback_cls, NULL, NULL, 2);
199       GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
200       GNUNET_free (ic);
201       return;
202     }
203   im = (const struct InfoMessage *) msg;
204   hello = NULL;
205   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
206     {
207       hello = (const struct GNUNET_HELLO_Message *) &im[1];
208       if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
209         {
210           GNUNET_break (0);
211           ic->callback (ic->callback_cls, NULL, NULL, 2);
212           GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
213           GNUNET_free (ic);
214           return;
215         }
216     }
217 #if DEBUG_PEERINFO
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),
221               "HELLO",
222               GNUNET_i2s (&im->peer));
223 #endif
224   ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
225   GNUNET_CLIENT_receive (ic->client,
226                          &info_handler,
227                          ic,
228                          GNUNET_TIME_absolute_get_remaining (ic->timeout));
229 }
230
231
232 /**
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.
239  *
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
248  */
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,
253                          int trust_delta,
254                          struct GNUNET_TIME_Relative timeout,
255                          GNUNET_PEERINFO_Processor callback,
256                          void *callback_cls)
257 {
258   struct GNUNET_CLIENT_Connection *client;
259   struct ListAllPeersMessage *lapm;
260   struct ListPeerMessage *lpm;
261   struct GNUNET_PEERINFO_IteratorContext *ihc;
262
263   client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
264   if (client == NULL)
265     {
266       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267                   _("Could not connect to `%s' service.\n"), "peerinfo");
268       return NULL;
269     }
270 #if DEBUG_PEERINFO
271   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272               "Requesting list of peers from peerinfo database\n");
273 #endif
274   if (peer == NULL)
275     {
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);
282     }
283   else
284     {
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));
292     }
293   ihc->client = client;
294   ihc->callback = callback;
295   ihc->callback_cls = callback_cls;
296   ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
297   if (GNUNET_OK != 
298       GNUNET_CLIENT_transmit_and_get_response (client,
299                                                (const struct GNUNET_MessageHeader*) &ihc[1],
300                                                timeout,
301                                                GNUNET_YES,
302                                                &info_handler,
303                                                ihc))
304     {
305       GNUNET_break (0);
306       GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
307       GNUNET_free (ihc);
308       return NULL;
309     }
310   return ihc;
311 }
312
313
314 /**
315  * Cancel an iteration over peer information.
316  *
317  * @param ic context of the iterator to cancel
318  */
319 void
320 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
321 {
322   GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
323   GNUNET_free (ic);
324 }
325
326
327
328
329 /* end of peerinfo_api.c */