-dead code elimination
[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   uint16_t msize;
74
75   /* Handle last message or error case, disconnect and clean up */
76   if ((msg == NULL) ||
77       ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
78        (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))))
79   {
80     if (request_context->peer_cb != NULL)
81       request_context->peer_cb (request_context->cb_cls, NULL);
82     GNUNET_CLIENT_disconnect (request_context->client);
83     GNUNET_free (request_context);
84     return;
85   }
86
87   msize = ntohs (msg->size);
88   /* Handle incorrect message type or size, disconnect and clean up */
89   if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
90       (msize < sizeof (struct ConnectNotifyMessage)))
91   {
92     GNUNET_break (0);
93     if (request_context->peer_cb != NULL)
94       request_context->peer_cb (request_context->cb_cls, NULL);
95     GNUNET_CLIENT_disconnect (request_context->client);
96     GNUNET_free (request_context);
97     return;
98   }
99   connect_message = (const struct ConnectNotifyMessage *) msg;
100   if (msize != sizeof (struct ConnectNotifyMessage))
101   {
102     GNUNET_break (0);
103     if (request_context->peer_cb != NULL)
104       request_context->peer_cb (request_context->cb_cls, NULL);
105     GNUNET_CLIENT_disconnect (request_context->client);
106     GNUNET_free (request_context);
107     return;
108   }
109   /* Normal case */
110   if (request_context->peer_cb != NULL)
111     request_context->peer_cb (request_context->cb_cls, &connect_message->peer);
112   GNUNET_CLIENT_receive (request_context->client, &receive_info,
113                          request_context, GNUNET_TIME_UNIT_FOREVER_REL);
114 }
115
116
117 /**
118  * Function called to notify a client about the socket
119  * begin ready to queue more data.  "buf" will be
120  * NULL and "size" zero if the socket was closed for
121  * writing in the meantime.
122  *
123  * @param cls closure
124  * @param size number of bytes available in buf
125  * @param buf where the callee should write the message
126  * @return number of bytes written to buf
127  */
128 static size_t
129 transmit_request (void *cls, size_t size, void *buf)
130 {
131   struct GNUNET_MessageHeader *msg;
132   struct GNUNET_PeerIdentity *peer = cls;
133   int msize;
134
135   if (peer == NULL)
136     msize = sizeof (struct GNUNET_MessageHeader);
137   else
138     msize =
139         sizeof (struct GNUNET_MessageHeader) +
140         sizeof (struct GNUNET_PeerIdentity);
141   if ((size < msize) || (buf == NULL))
142     return 0;
143   msg = (struct GNUNET_MessageHeader *) buf;
144   msg->size = htons (msize);
145   if (peer != NULL)
146   {
147     msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED);
148     memcpy (&msg[1], peer, sizeof (struct GNUNET_PeerIdentity));
149   }
150   else
151     msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
152
153   return msize;
154 }
155
156
157
158 /**
159  * Iterate over all currently connected peers.
160  * Calls peer_cb with each connected peer, and then
161  * once with NULL to indicate that all peers have
162  * been handled.
163  *
164  * @param cfg configuration to use
165  * @param peer_cb function to call with the peer information
166  * @param cb_cls closure for peer_cb
167  *
168  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
169  */
170 int
171 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
172                            GNUNET_CORE_ConnectEventHandler peer_cb,
173                            void *cb_cls)
174 {
175   struct GNUNET_CORE_RequestContext *request_context;
176   struct GNUNET_CLIENT_Connection *client;
177
178   client = GNUNET_CLIENT_connect ("core", cfg);
179   if (client == NULL)
180     return GNUNET_SYSERR;
181   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
182   request_context->client = client;
183   request_context->peer_cb = peer_cb;
184   request_context->cb_cls = cb_cls;
185
186   request_context->th =
187       GNUNET_CLIENT_notify_transmit_ready (client,
188                                            sizeof (struct GNUNET_MessageHeader),
189                                            GNUNET_TIME_UNIT_FOREVER_REL,
190                                            GNUNET_YES, &transmit_request, NULL);
191
192   GNUNET_CLIENT_receive (client, &receive_info, request_context,
193                          GNUNET_TIME_UNIT_FOREVER_REL);
194   return GNUNET_OK;
195 }
196
197 /* end of core_api_iterate_peers.c */