aboutsummaryrefslogtreecommitdiff
path: root/src/hostlist/gnunet-daemon-hostlist.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-31 15:42:37 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-31 15:42:37 +0000
commit406c7d2d2d126c994a1fff13470b1f96439c6f9d (patch)
treedc5e7c62a706d03cee32734a19e183b9eddf88e2 /src/hostlist/gnunet-daemon-hostlist.c
parent8b2a3260e6aafc2ad31c8b7bff5f7d25b57bfc14 (diff)
downloadgnunet-406c7d2d2d126c994a1fff13470b1f96439c6f9d.tar.gz
gnunet-406c7d2d2d126c994a1fff13470b1f96439c6f9d.zip
convering more services to new core MQ API
Diffstat (limited to 'src/hostlist/gnunet-daemon-hostlist.c')
-rw-r--r--src/hostlist/gnunet-daemon-hostlist.c129
1 files changed, 88 insertions, 41 deletions
diff --git a/src/hostlist/gnunet-daemon-hostlist.c b/src/hostlist/gnunet-daemon-hostlist.c
index 21fab323b..44db59949 100644
--- a/src/hostlist/gnunet-daemon-hostlist.c
+++ b/src/hostlist/gnunet-daemon-hostlist.c
@@ -47,12 +47,7 @@ static int provide_hostlist;
47/** 47/**
48 * Handle to hostlist server's connect handler 48 * Handle to hostlist server's connect handler
49 */ 49 */
50static GNUNET_CORE_ConnectEventHandler server_ch; 50static GNUNET_CORE_ConnecTEventHandler server_ch;
51
52/**
53 * Handle to hostlist server's disconnect handler
54 */
55static GNUNET_CORE_DisconnectEventHandler server_dh;
56 51
57#endif 52#endif
58 53
@@ -81,17 +76,17 @@ static struct GNUNET_CORE_Handle *core;
81/** 76/**
82 * Handle to the hostlist client's advertisement handler 77 * Handle to the hostlist client's advertisement handler
83 */ 78 */
84static GNUNET_CORE_MessageCallback client_adv_handler; 79static GNUNET_HOSTLIST_UriHandler client_adv_handler;
85 80
86/** 81/**
87 * Handle to hostlist client's connect handler 82 * Handle to hostlist client's connect handler
88 */ 83 */
89static GNUNET_CORE_ConnectEventHandler client_ch; 84static GNUNET_CORE_ConnecTEventHandler client_ch;
90 85
91/** 86/**
92 * Handle to hostlist client's disconnect handler 87 * Handle to hostlist client's disconnect handler
93 */ 88 */
94static GNUNET_CORE_DisconnectEventHandler client_dh; 89static GNUNET_CORE_DisconnecTEventHandler client_dh;
95 90
96GNUNET_NETWORK_STRUCT_BEGIN 91GNUNET_NETWORK_STRUCT_BEGIN
97 92
@@ -146,17 +141,49 @@ core_init (void *cls,
146 * Core handler for p2p hostlist advertisements 141 * Core handler for p2p hostlist advertisements
147 * 142 *
148 * @param cls closure 143 * @param cls closure
149 * @param peer identity of the sender
150 * @param message advertisement message we got 144 * @param message advertisement message we got
151 * @return #GNUNET_OK on success 145 * @return #GNUNET_OK if message is well-formed
152 */ 146 */
153static int 147static int
154advertisement_handler (void *cls, 148check_advertisement (void *cls,
155 const struct GNUNET_PeerIdentity *peer, 149 const struct GNUNET_MessageHeader *message)
156 const struct GNUNET_MessageHeader *message)
157{ 150{
151 size_t size;
152 size_t uri_size;
153 const char *uri;
154
155 size = ntohs (message->size);
156 if (size <= sizeof (struct GNUNET_MessageHeader))
157 {
158 GNUNET_break_op (0);
159 return GNUNET_SYSERR;
160 }
161 uri = (const char *) &message[1];
162 uri_size = size - sizeof (struct GNUNET_MessageHeader);
163 if (uri[uri_size - 1] != '\0')
164 {
165 GNUNET_break_op (0);
166 return GNUNET_SYSERR;
167 }
168 return GNUNET_OK;
169}
170
171
172/**
173 * Core handler for p2p hostlist advertisements
174 *
175 * @param cls closure
176 * @param message advertisement message we got
177 * @return #GNUNET_OK on success
178 */
179static void
180handle_advertisement (void *cls,
181 const struct GNUNET_MessageHeader *message)
182{
183 const char *uri = (const char *) &message[1];
184
158 GNUNET_assert (NULL != client_adv_handler); 185 GNUNET_assert (NULL != client_adv_handler);
159 return (*client_adv_handler) (cls, peer, message); 186 (void) (*client_adv_handler) (uri);
160} 187}
161 188
162 189
@@ -166,20 +193,33 @@ advertisement_handler (void *cls,
166 * 193 *
167 * @param cls closure 194 * @param cls closure
168 * @param peer peer identity this notification is about 195 * @param peer peer identity this notification is about
196 * @param mq queue for sending messages to @a peer
197 * @return peer
169 */ 198 */
170static void 199static void *
171connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer) 200connect_handler (void *cls,
201 const struct GNUNET_PeerIdentity *peer,
202 struct GNUNET_MQ_Handle *mq)
172{ 203{
173 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity))) 204 if (0 == memcmp (&me,
174 return; 205 peer,
206 sizeof (struct GNUNET_PeerIdentity)))
207 return NULL;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "A new peer connected, notifying client and server\n"); 209 "A new peer connected, notifying client and server\n");
177 if (NULL != client_ch) 210 if (NULL != client_ch)
178 (*client_ch) (cls, peer); 211 GNUNET_assert (NULL ==
212 (*client_ch) (cls,
213 peer,
214 mq));
179#if HAVE_MHD 215#if HAVE_MHD
180 if (NULL != server_ch) 216 if (NULL != server_ch)
181 (*server_ch) (cls, peer); 217 GNUNET_assert (NULL ==
218 (*server_ch) (cls,
219 peer,
220 mq));
182#endif 221#endif
222 return (void *) peer;
183} 223}
184 224
185 225
@@ -192,18 +232,18 @@ connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
192 */ 232 */
193static void 233static void
194disconnect_handler (void *cls, 234disconnect_handler (void *cls,
195 const struct GNUNET_PeerIdentity *peer) 235 const struct GNUNET_PeerIdentity *peer,
236 void *internal_cls)
196{ 237{
197 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity))) 238 if (0 == memcmp (&me,
239 peer,
240 sizeof (struct GNUNET_PeerIdentity)))
198 return; 241 return;
199 /* call hostlist client disconnect handler */ 242 /* call hostlist client disconnect handler */
200 if (NULL != client_dh) 243 if (NULL != client_dh)
201 (*client_dh) (cls, peer); 244 (*client_dh) (cls,
202#if HAVE_MHD 245 peer,
203 /* call hostlist server disconnect handler */ 246 NULL);
204 if (NULL != server_dh)
205 (*server_dh) (cls, peer);
206#endif
207} 247}
208 248
209 249
@@ -220,7 +260,7 @@ cleaning_task (void *cls)
220 "Hostlist daemon is shutting down\n"); 260 "Hostlist daemon is shutting down\n");
221 if (NULL != core) 261 if (NULL != core)
222 { 262 {
223 GNUNET_CORE_disconnect (core); 263 GNUNET_CORE_disconnecT (core);
224 core = NULL; 264 core = NULL;
225 } 265 }
226 if (bootstrapping) 266 if (bootstrapping)
@@ -235,7 +275,8 @@ cleaning_task (void *cls)
235#endif 275#endif
236 if (NULL != stats) 276 if (NULL != stats)
237 { 277 {
238 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 278 GNUNET_STATISTICS_destroy (stats,
279 GNUNET_NO);
239 stats = NULL; 280 stats = NULL;
240 } 281 }
241} 282}
@@ -255,12 +296,15 @@ run (void *cls,
255 const char *cfgfile, 296 const char *cfgfile,
256 const struct GNUNET_CONFIGURATION_Handle *cfg) 297 const struct GNUNET_CONFIGURATION_Handle *cfg)
257{ 298{
258 static const struct GNUNET_CORE_MessageHandler learn_handlers[] = { 299 GNUNET_MQ_hd_var_size (advertisement,
259 {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0}, 300 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
260 {NULL, 0, 0} 301 struct GNUNET_MessageHeader);
302 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
303 make_advertisement_handler (NULL),
304 GNUNET_MQ_handler_end ()
261 }; 305 };
262 static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = { 306 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
263 {NULL, 0, 0} 307 GNUNET_MQ_handler_end ()
264 }; 308 };
265 if ((! bootstrapping) && (! learning) 309 if ((! bootstrapping) && (! learning)
266#if HAVE_MHD 310#if HAVE_MHD
@@ -279,24 +323,27 @@ run (void *cls,
279 return; 323 return;
280 } 324 }
281 if (bootstrapping) 325 if (bootstrapping)
282 GNUNET_HOSTLIST_client_start (cfg, stats, 326 GNUNET_HOSTLIST_client_start (cfg,
327 stats,
283 &client_ch, 328 &client_ch,
284 &client_dh, 329 &client_dh,
285 &client_adv_handler, 330 &client_adv_handler,
286 learning); 331 learning);
287 core = 332 core =
288 GNUNET_CORE_connect (cfg, NULL, 333 GNUNET_CORE_connecT (cfg,
334 NULL,
289 &core_init, 335 &core_init,
290 &connect_handler, 336 &connect_handler,
291 &disconnect_handler, 337 &disconnect_handler,
292 NULL, GNUNET_NO,
293 NULL, GNUNET_NO,
294 learning ? learn_handlers : no_learn_handlers); 338 learning ? learn_handlers : no_learn_handlers);
295 339
296 340
297#if HAVE_MHD 341#if HAVE_MHD
298 if (provide_hostlist) 342 if (provide_hostlist)
299 GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh, 343 GNUNET_HOSTLIST_server_start (cfg,
344 stats,
345 core,
346 &server_ch,
300 advertising); 347 advertising);
301#endif 348#endif
302 GNUNET_SCHEDULER_add_shutdown (&cleaning_task, 349 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,