check
[oweals/gnunet.git] / src / core / core_api_iterate_peers.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 3, 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 core/core_api_iterate_peers.c
23  * @brief implementation of the peer_iterate function
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27 #include "platform.h"
28 #include "gnunet_core_service.h"
29 #include "core.h"
30
31
32 struct GNUNET_CORE_RequestContext
33 {
34   /**
35    * Our connection to the service.
36    */
37   struct GNUNET_CLIENT_Connection *client;
38
39   /**
40    * Handle for transmitting a request.
41    */
42   struct GNUNET_CLIENT_TransmitHandle *th;
43
44   /**
45    * Function called with the peer.
46    */
47   GNUNET_CORE_ConnectEventHandler peer_cb;
48
49   /**
50    * Peer to check for.
51    */
52   struct GNUNET_PeerIdentity *peer;
53
54   /**
55    * Closure for peer_cb.
56    */
57   void *cb_cls;
58
59 };
60
61
62 /**
63  * Receive reply from core service with information about a peer.
64  *
65  * @param cls our 'struct  GNUNET_CORE_RequestContext *'
66  * @param msg NULL on error or last entry
67  */
68 static void
69 receive_info (void *cls,
70               const struct GNUNET_MessageHeader *msg)
71 {
72   struct GNUNET_CORE_RequestContext *request_context = cls;
73   const struct ConnectNotifyMessage *connect_message;
74   uint32_t ats_count;
75   uint16_t msize;
76
77   /* Handle last message or error case, disconnect and clean up */
78   if ( (msg == NULL) ||
79       ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
80       (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) )
81     {
82       if (request_context->peer_cb != NULL)
83         request_context->peer_cb (request_context->cb_cls,
84                                   NULL, NULL);
85       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
86       GNUNET_free (request_context);
87       return;
88     }
89
90   msize = ntohs (msg->size);
91   /* Handle incorrect message type or size, disconnect and clean up */
92   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
93        (msize < sizeof (struct ConnectNotifyMessage)) )
94     {
95       GNUNET_break (0);
96       if (request_context->peer_cb != NULL)
97         request_context->peer_cb (request_context->cb_cls,
98                                   NULL, NULL);
99       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
100       GNUNET_free (request_context);
101       return;
102     }
103   connect_message = (const struct ConnectNotifyMessage *) msg;
104   ats_count = ntohl (connect_message->ats_count);
105   if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
106        (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&connect_message->ats)[ats_count].type)) )
107     {
108       GNUNET_break (0);
109       if (request_context->peer_cb != NULL)
110         request_context->peer_cb (request_context->cb_cls,
111                                   NULL, NULL);
112       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
113       GNUNET_free (request_context);
114       return;
115     }
116   /* Normal case */
117   if (request_context->peer_cb != NULL)
118     request_context->peer_cb (request_context->cb_cls,
119                               &connect_message->peer,
120                               &connect_message->ats);
121   GNUNET_CLIENT_receive(request_context->client, 
122                         &receive_info, 
123                         request_context,
124                         GNUNET_TIME_UNIT_FOREVER_REL);
125 }
126
127 /**
128  * Function called to notify a client about the socket
129  * begin ready to queue more data.  "buf" will be
130  * NULL and "size" zero if the socket was closed for
131  * writing in the meantime.
132  *
133  * @param cls closure
134  * @param size number of bytes available in buf
135  * @param buf where the callee should write the message
136  * @return number of bytes written to buf
137  */
138 static size_t
139 transmit_request(void *cls,
140                  size_t size, void *buf)
141 {
142   struct GNUNET_MessageHeader *msg;
143   struct GNUNET_PeerIdentity *peer = cls;
144   int msize;
145
146   if (peer == NULL)
147     msize = sizeof(struct GNUNET_MessageHeader);
148   else
149     msize = sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity);
150
151   if ((size < msize) || (buf == NULL))
152     return 0;
153
154   msg = (struct GNUNET_MessageHeader *)buf;
155   msg->size = htons (msize);
156   if (peer != NULL)
157     {
158       msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED);
159       memcpy(&msg[1], peer, sizeof(struct GNUNET_PeerIdentity));
160     }
161   else
162     msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
163
164   return msize;
165 }
166
167 /**
168  * Iterate over all currently connected peers.
169  * Calls peer_cb with each connected peer, and then
170  * once with NULL to indicate that all peers have
171  * been handled.
172  *
173  * @param cfg configuration to use
174  * @param peer the specific peer to check for
175  * @param peer_cb function to call with the peer information
176  * @param cb_cls closure for peer_cb
177  *
178  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
179  */
180 int
181 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
182                                struct GNUNET_PeerIdentity *peer,
183                                GNUNET_CORE_ConnectEventHandler peer_cb,
184                                void *cb_cls)
185 {
186   struct GNUNET_CORE_RequestContext *request_context;
187   struct GNUNET_CLIENT_Connection *client;
188
189   client = GNUNET_CLIENT_connect ("core", cfg);
190   if (client == NULL)
191     return GNUNET_SYSERR;
192   GNUNET_assert(peer != NULL);
193   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
194   request_context->client = client;
195   request_context->peer_cb = peer_cb;
196   request_context->cb_cls = cb_cls;
197   request_context->peer = peer;
198
199   request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
200                                                             sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity),
201                                                             GNUNET_TIME_relative_get_forever(),
202                                                             GNUNET_YES,
203                                                             &transmit_request,
204                                                             peer);
205   GNUNET_assert(request_context->th != NULL);
206   GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
207   return GNUNET_OK;
208 }
209
210 /**
211  * Iterate over all currently connected peers.
212  * Calls peer_cb with each connected peer, and then
213  * once with NULL to indicate that all peers have
214  * been handled.
215  *
216  * @param cfg configuration to use
217  * @param peer_cb function to call with the peer information
218  * @param cb_cls closure for peer_cb
219  *
220  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
221  */
222 int
223 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
224                            GNUNET_CORE_ConnectEventHandler peer_cb,
225                            void *cb_cls)
226 {
227   struct GNUNET_CORE_RequestContext *request_context;
228   struct GNUNET_CLIENT_Connection *client;
229
230   client = GNUNET_CLIENT_connect ("core", cfg);
231   if (client == NULL)
232     return GNUNET_SYSERR;
233   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
234   request_context->client = client;
235   request_context->peer_cb = peer_cb;
236   request_context->cb_cls = cb_cls;
237
238   request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
239                                                             sizeof(struct GNUNET_MessageHeader),
240                                                             GNUNET_TIME_relative_get_forever(),
241                                                             GNUNET_YES,
242                                                             &transmit_request,
243                                                             NULL);
244
245   GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
246   return GNUNET_OK;
247 }
248
249 /* end of core_api_iterate_peers.c */