indentation
[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, const struct GNUNET_MessageHeader *msg)
70 {
71   struct GNUNET_CORE_RequestContext *request_context = cls;
72   const struct ConnectNotifyMessage *connect_message;
73   uint32_t ats_count;
74   uint16_t msize;
75
76   /* Handle last message or error case, disconnect and clean up */
77   if ((msg == NULL) ||
78       ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
79        (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))))
80   {
81     if (request_context->peer_cb != NULL)
82       request_context->peer_cb (request_context->cb_cls, NULL, NULL);
83     GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
84     GNUNET_free (request_context);
85     return;
86   }
87
88   msize = ntohs (msg->size);
89   /* Handle incorrect message type or size, disconnect and clean up */
90   if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
91       (msize < sizeof (struct ConnectNotifyMessage)))
92   {
93     GNUNET_break (0);
94     if (request_context->peer_cb != NULL)
95       request_context->peer_cb (request_context->cb_cls, NULL, NULL);
96     GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
97     GNUNET_free (request_context);
98     return;
99   }
100   connect_message = (const struct ConnectNotifyMessage *) msg;
101   ats_count = ntohl (connect_message->ats_count);
102   if ((msize !=
103        sizeof (struct ConnectNotifyMessage) +
104        ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
105       (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
106        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, NULL, NULL);
111     GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
112     GNUNET_free (request_context);
113     return;
114   }
115   /* Normal case */
116   if (request_context->peer_cb != NULL)
117     request_context->peer_cb (request_context->cb_cls, &connect_message->peer,
118                               &connect_message->ats);
119   GNUNET_CLIENT_receive (request_context->client, &receive_info,
120                          request_context, GNUNET_TIME_UNIT_FOREVER_REL);
121 }
122
123 /**
124  * Function called to notify a client about the socket
125  * begin ready to queue more data.  "buf" will be
126  * NULL and "size" zero if the socket was closed for
127  * writing in the meantime.
128  *
129  * @param cls closure
130  * @param size number of bytes available in buf
131  * @param buf where the callee should write the message
132  * @return number of bytes written to buf
133  */
134 static size_t
135 transmit_request (void *cls, size_t size, void *buf)
136 {
137   struct GNUNET_MessageHeader *msg;
138   struct GNUNET_PeerIdentity *peer = cls;
139   int msize;
140
141   if (peer == NULL)
142     msize = sizeof (struct GNUNET_MessageHeader);
143   else
144     msize =
145         sizeof (struct GNUNET_MessageHeader) +
146         sizeof (struct GNUNET_PeerIdentity);
147
148   if ((size < msize) || (buf == NULL))
149     return 0;
150
151   msg = (struct GNUNET_MessageHeader *) buf;
152   msg->size = htons (msize);
153   if (peer != NULL)
154   {
155     msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED);
156     memcpy (&msg[1], peer, sizeof (struct GNUNET_PeerIdentity));
157   }
158   else
159     msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
160
161   return msize;
162 }
163
164 /**
165  * Iterate over all currently connected peers.
166  * Calls peer_cb with each connected peer, and then
167  * once with NULL to indicate that all peers have
168  * been handled.
169  *
170  * @param cfg configuration to use
171  * @param peer the specific peer to check for
172  * @param peer_cb function to call with the peer information
173  * @param cb_cls closure for peer_cb
174  *
175  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
176  */
177 int
178 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
179                                struct GNUNET_PeerIdentity *peer,
180                                GNUNET_CORE_ConnectEventHandler peer_cb,
181                                void *cb_cls)
182 {
183   struct GNUNET_CORE_RequestContext *request_context;
184   struct GNUNET_CLIENT_Connection *client;
185
186   client = GNUNET_CLIENT_connect ("core", cfg);
187   if (client == NULL)
188     return GNUNET_SYSERR;
189   GNUNET_assert (peer != NULL);
190   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
191   request_context->client = client;
192   request_context->peer_cb = peer_cb;
193   request_context->cb_cls = cb_cls;
194   request_context->peer = peer;
195
196   request_context->th =
197       GNUNET_CLIENT_notify_transmit_ready (client,
198                                            sizeof (struct GNUNET_MessageHeader)
199                                            +
200                                            sizeof (struct GNUNET_PeerIdentity),
201                                            GNUNET_TIME_relative_get_forever (),
202                                            GNUNET_YES, &transmit_request, peer);
203   GNUNET_assert (request_context->th != NULL);
204   GNUNET_CLIENT_receive (client, &receive_info, request_context,
205                          GNUNET_TIME_relative_get_forever ());
206   return GNUNET_OK;
207 }
208
209 /**
210  * Iterate over all currently connected peers.
211  * Calls peer_cb with each connected peer, and then
212  * once with NULL to indicate that all peers have
213  * been handled.
214  *
215  * @param cfg configuration to use
216  * @param peer_cb function to call with the peer information
217  * @param cb_cls closure for peer_cb
218  *
219  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
220  */
221 int
222 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
223                            GNUNET_CORE_ConnectEventHandler peer_cb,
224                            void *cb_cls)
225 {
226   struct GNUNET_CORE_RequestContext *request_context;
227   struct GNUNET_CLIENT_Connection *client;
228
229   client = GNUNET_CLIENT_connect ("core", cfg);
230   if (client == NULL)
231     return GNUNET_SYSERR;
232   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
233   request_context->client = client;
234   request_context->peer_cb = peer_cb;
235   request_context->cb_cls = cb_cls;
236
237   request_context->th =
238       GNUNET_CLIENT_notify_transmit_ready (client,
239                                            sizeof (struct GNUNET_MessageHeader),
240                                            GNUNET_TIME_relative_get_forever (),
241                                            GNUNET_YES, &transmit_request, NULL);
242
243   GNUNET_CLIENT_receive (client, &receive_info, request_context,
244                          GNUNET_TIME_relative_get_forever ());
245   return GNUNET_OK;
246 }
247
248 /* end of core_api_iterate_peers.c */