i am a dumb dummy
[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   /**
36    * Our connection to the service.
37    */
38   struct GNUNET_CLIENT_Connection *client;
39
40   /**
41    * Handle for transmitting a request.
42    */
43   struct GNUNET_CLIENT_TransmitHandle *th;
44
45   /**
46    * Function called with the peer.
47    */
48   GNUNET_CORE_ConnectEventHandler peer_cb;
49
50   /**
51    * Closure for peer_cb.
52    */
53   void *cb_cls;
54
55 };
56
57
58 /**
59  * Receive reply from core service with information about a peer.
60  *
61  * @param cls our 'struct  GNUNET_CORE_RequestContext *'
62  * @param msg NULL on error or last entry
63  */
64 static void
65 receive_info (void *cls,
66               const struct GNUNET_MessageHeader *msg)
67 {
68   struct GNUNET_CORE_RequestContext *request_context = cls;
69   const struct ConnectNotifyMessage *connect_message;
70   uint32_t ats_count;
71   uint16_t msize;
72
73   /* Handle last message or error case, disconnect and clean up */
74   msize = ntohs (msg->size);
75   if ( (msg == NULL) ||
76       ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
77       (msize == sizeof (struct GNUNET_MessageHeader))) )
78     {
79       if (request_context->peer_cb != NULL)
80         request_context->peer_cb (request_context->cb_cls,
81                                   NULL, NULL);
82       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
83       GNUNET_free (request_context);
84       return;
85     }
86
87   /* Handle incorrect message type or size, disconnect and clean up */
88   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
89        (msize < sizeof (struct ConnectNotifyMessage)) )
90     {
91       GNUNET_break (0);
92       if (request_context->peer_cb != NULL)
93         request_context->peer_cb (request_context->cb_cls,
94                                   NULL, NULL);
95       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
96       GNUNET_free (request_context);
97       return;
98     }
99   connect_message = (const struct ConnectNotifyMessage *) msg;
100   ats_count = ntohl (connect_message->ats_count);
101   if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
102        (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&connect_message->ats)[ats_count].type)) )
103     {
104       GNUNET_break (0);
105       if (request_context->peer_cb != NULL)
106         request_context->peer_cb (request_context->cb_cls,
107                                   NULL, NULL);
108       GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
109       GNUNET_free (request_context);
110       return;
111     }
112   /* Normal case */
113   if (request_context->peer_cb != NULL)
114     request_context->peer_cb (request_context->cb_cls,
115                               &connect_message->peer,
116                               &connect_message->ats);
117   GNUNET_CLIENT_receive(request_context->client, 
118                         &receive_info, 
119                         request_context,
120                         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,
136                  size_t size, void *buf)
137 {
138   struct GNUNET_MessageHeader *msg;
139   if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL))
140     return 0;
141
142   msg = (struct GNUNET_MessageHeader *)buf;
143   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
144   msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
145   return sizeof(struct GNUNET_MessageHeader);
146 }
147
148 /**
149  * Obtain statistics and/or change preferences for the given peer.
150  *
151  * @param sched scheduler to use
152  * @param cfg configuration to use
153  * @param peer_cb function to call with the peer information
154  * @param cb_cls closure for peer_cb
155  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
156  */
157 int
158 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
159                            GNUNET_CORE_ConnectEventHandler peer_cb,
160                            void *cb_cls)
161 {
162   struct GNUNET_CORE_RequestContext *request_context;
163   struct GNUNET_CLIENT_Connection *client;
164
165   client = GNUNET_CLIENT_connect ("core", cfg);
166   if (client == NULL)
167     return GNUNET_SYSERR;
168   request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
169   request_context->client = client;
170   request_context->peer_cb = peer_cb;
171   request_context->cb_cls = cb_cls;
172
173   request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
174                                                             sizeof(struct GNUNET_MessageHeader),
175                                                             GNUNET_TIME_relative_get_forever(),
176                                                             GNUNET_YES,
177                                                             &transmit_request,
178                                                             NULL);
179
180   GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
181   return GNUNET_OK;
182 }
183
184 /* end of core_api_iterate_peers.c */