aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_api_list_peers.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-01-27 00:36:47 +0100
committerChristian Grothoff <christian@grothoff.org>2019-01-27 00:36:47 +0100
commit65b339b7ce68fcbaec9df6f66e8ed45e60b39ce1 (patch)
treefff17cdb219935617877a5504bb8435712b80a61 /src/cadet/cadet_api_list_peers.c
parent8f884f001a70fee622fff0c0ac8c38a0634df789 (diff)
downloadgnunet-65b339b7ce68fcbaec9df6f66e8ed45e60b39ce1.tar.gz
gnunet-65b339b7ce68fcbaec9df6f66e8ed45e60b39ce1.zip
more work on #5385
Diffstat (limited to 'src/cadet/cadet_api_list_peers.c')
-rw-r--r--src/cadet/cadet_api_list_peers.c179
1 files changed, 110 insertions, 69 deletions
diff --git a/src/cadet/cadet_api_list_peers.c b/src/cadet/cadet_api_list_peers.c
index f6563a290..d53bcf65d 100644
--- a/src/cadet/cadet_api_list_peers.c
+++ b/src/cadet/cadet_api_list_peers.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V. 3 Copyright (C) 2011, 2017, 2019 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -18,7 +18,7 @@
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19*/
20/** 20/**
21 * @file cadet/cadet_api.c 21 * @file cadet/cadet_api_list_peers.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot 23 * @author Bartlomiej Polot
24 * @author Christian Grothoff 24 * @author Christian Grothoff
@@ -31,9 +31,8 @@
31#include "cadet_protocol.h" 31#include "cadet_protocol.h"
32 32
33 33
34
35/** 34/**
36 * Ugly legacy hack. 35 * Operation handle.
37 */ 36 */
38struct GNUNET_CADET_PeersLister 37struct GNUNET_CADET_PeersLister
39{ 38{
@@ -47,33 +46,34 @@ struct GNUNET_CADET_PeersLister
47 * Info callback closure for @c info_cb. 46 * Info callback closure for @c info_cb.
48 */ 47 */
49 void *peers_cb_cls; 48 void *peers_cb_cls;
50};
51 49
50 /**
51 * Message queue to talk to CADET service.
52 */
53 struct GNUNET_MQ_Handle *mq;
52 54
53/** 55 /**
54 * Send message of @a type to CADET service of @a h 56 * Configuration we use.
55 * 57 */
56 * @param h handle to CADET service 58 const struct GNUNET_CONFIGURATION_Handle *cfg;
57 * @param type message type of trivial information request to send
58 */
59static void
60send_info_request (struct GNUNET_CADET_Handle *h,
61 uint16_t type)
62{
63 struct GNUNET_MessageHeader *msg;
64 struct GNUNET_MQ_Envelope *env;
65 59
66 env = GNUNET_MQ_msg (msg, 60 /**
67 type); 61 * Task to reconnect.
68 GNUNET_MQ_send (h->mq, 62 */
69 env); 63 struct GNUNET_SCHEDULER_Task *reconnect_task;
70} 64
65 /**
66 * Backoff for reconnect attempts.
67 */
68 struct GNUNET_TIME_Relative backoff;
69
70};
71 71
72 72
73/** 73/**
74 * Check that message received from CADET service is well-formed. 74 * Check that message received from CADET service is well-formed.
75 * 75 *
76 * @param cls the `struct GNUNET_CADET_Handle` 76 * @param cls the `struct GNUNET_CADET_PeersLister`
77 * @param message the message we got 77 * @param message the message we got
78 * @return #GNUNET_OK if the message is well-formed, 78 * @return #GNUNET_OK if the message is well-formed,
79 * #GNUNET_SYSERR otherwise 79 * #GNUNET_SYSERR otherwise
@@ -94,60 +94,97 @@ check_get_peers (void *cls,
94} 94}
95 95
96 96
97// FIXME: use two different message types instead of this mess!
97/** 98/**
98 * Process a local reply about info on all tunnels, pass info to the user. 99 * Process a local reply about info on all tunnels, pass info to the user.
99 * 100 *
100 * @param cls Closure (Cadet handle). 101 * @param cls a `struct GNUNET_CADET_PeersLister`
101 * @param msg Message itself. 102 * @param msg Message itself.
102 */ 103 */
103static void 104static void
104handle_get_peers (void *cls, 105handle_get_peers (void *cls,
105 const struct GNUNET_MessageHeader *msg) 106 const struct GNUNET_MessageHeader *msg)
106{ 107{
107 struct GNUNET_CADET_Handle *h = cls; 108 struct GNUNET_CADET_PeersLister *pl = cls;
108 const struct GNUNET_CADET_LocalInfoPeer *info = 109 const struct GNUNET_CADET_LocalInfoPeer *info =
109 (const struct GNUNET_CADET_LocalInfoPeer *) msg; 110 (const struct GNUNET_CADET_LocalInfoPeer *) msg;
110 111
111 if (NULL == h->info_cb.peers_cb)
112 return;
113 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size)) 112 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
114 h->info_cb.peers_cb (h->info_cls, 113 pl->peers_cb (pl->peers_cb_cls,
115 &info->destination, 114 &info->destination,
116 (int) ntohs (info->tunnel), 115 (int) ntohs (info->tunnel),
117 (unsigned int) ntohs (info->paths), 116 (unsigned int) ntohs (info->paths),
118 0); 117 0);
119 else 118 else
120 h->info_cb.peers_cb (h->info_cls, 119 {
121 NULL, 120 pl->peers_cb (pl->peers_cb_cls,
122 0, 121 NULL,
123 0, 122 0,
124 0); 123 0,
124 0);
125 GNUNET_CADET_list_peers_cancel (pl);
126 }
127}
128
129
130/**
131 * Reconnect to the service and try again.
132 *
133 * @param cls a `struct GNUNET_CADET_PeersLister` operation
134 */
135static void
136reconnect (void *cls);
137
138
139/**
140 * Function called on connection trouble. Reconnects.
141 *
142 * @param cls a `struct GNUNET_CADET_PeersLister`
143 * @param error error code from MQ
144 */
145static void
146error_handler (void *cls,
147 enum GNUNET_MQ_Error error)
148{
149 struct GNUNET_CADET_PeersLister *pl = cls;
150
151 GNUNET_MQ_destroy (pl->mq);
152 pl->mq = NULL;
153 pl->backoff = GNUNET_TIME_randomized_backoff (pl->backoff,
154 GNUNET_TIME_UNIT_MINUTES);
155 pl->reconnect_task = GNUNET_SCHEDULER_add_delayed (pl->backoff,
156 &reconnect,
157 pl);
125} 158}
126 159
127 160
161/**
162 * Reconnect to the service and try again.
163 *
164 * @param cls a `struct GNUNET_CADET_PeersLister` operation
165 */
128static void 166static void
129reconnect (void *cls) 167reconnect (void *cls)
130{ 168{
131 struct GNUNET_CADET_ListTunnels *lt = cls; 169 struct GNUNET_CADET_PeersLister *pl = cls;
132 struct GNUNET_MQ_MessageHandler *handlers[] = { 170 struct GNUNET_MQ_MessageHandler handlers[] = {
133 GNUNET_MQ_hd_var_size (get_peers, 171 GNUNET_MQ_hd_var_size (get_peers,
134 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, 172 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
135 struct GNUNET_MessageHeader, 173 struct GNUNET_MessageHeader,
136 h), 174 pl),
137 GNUNET_MQ_handler_end () 175 GNUNET_MQ_handler_end ()
138 } 176 };
139 struct GNUNET_MessageHeader *msg; 177 struct GNUNET_MessageHeader *msg;
140 struct GNUNET_MQ_Envelope *env; 178 struct GNUNET_MQ_Envelope *env;
141 179
142 cm->mq = GNUNET_CLIENT_connect (cm->cfg, 180 pl->mq = GNUNET_CLIENT_connect (pl->cfg,
143 "cadet", 181 "cadet",
144 handlers, 182 handlers,
145 &error_handler, 183 &error_handler,
146 cm); 184 pl);
147
148 env = GNUNET_MQ_msg (msg, 185 env = GNUNET_MQ_msg (msg,
149 type); 186 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
150 GNUNET_MQ_send (cm->mq, 187 GNUNET_MQ_send (pl->mq,
151 env); 188 env);
152} 189}
153 190
@@ -157,50 +194,54 @@ reconnect (void *cls)
157 * The callback will be called for every peer known to the service. 194 * The callback will be called for every peer known to the service.
158 * Only one info request (of any kind) can be active at once. 195 * Only one info request (of any kind) can be active at once.
159 * 196 *
160 * WARNING: unstable API, likely to change in the future!
161 *
162 * @param h Handle to the cadet peer. 197 * @param h Handle to the cadet peer.
163 * @param callback Function to call with the requested data. 198 * @param callback Function to call with the requested data.
164 * @param callback_cls Closure for @c callback. 199 * @param callback_cls Closure for @c callback.
165 * @return #GNUNET_OK / #GNUNET_SYSERR 200 * @return NULL on error
166 */ 201 */
167struct GNUNET_CADET_PeersLister * 202struct GNUNET_CADET_PeersLister *
168GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg, 203GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
169 GNUNET_CADET_PeersCB callback, 204 GNUNET_CADET_PeersCB callback,
170 void *callback_cls) 205 void *callback_cls)
171{ 206{
172 if (NULL != h->info_cb.peers_cb) 207 struct GNUNET_CADET_PeersLister *pl;
208
209 if (NULL == callback)
173 { 210 {
174 GNUNET_break (0); 211 GNUNET_break (0);
175 return GNUNET_SYSERR; 212 return NULL;
213 }
214 pl = GNUNET_new (struct GNUNET_CADET_PeersLister);
215 pl->peers_cb = callback;
216 pl->peers_cb_cls = callback_cls;
217 pl->cfg = cfg;
218 reconnect (pl);
219 if (NULL == pl->mq)
220 {
221 GNUNET_free (pl);
222 return NULL;
176 } 223 }
177 send_info_request (h, 224 return pl;
178 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
179 h->info_cb.peers_cb = callback;
180 h->info_cls = callback_cls;
181 return GNUNET_OK;
182} 225}
183 226
184 227
185/** 228/**
186 * Cancel a peer info request. The callback will not be called (anymore). 229 * Cancel a peer info request. The callback will not be called (anymore).
187 * 230 *
188 * WARNING: unstable API, likely to change in the future! 231 * @param pl operation handle
189 *
190 * @param h Cadet handle.
191 * @return Closure given to GNUNET_CADET_get_peers(). 232 * @return Closure given to GNUNET_CADET_get_peers().
192 */ 233 */
193void * 234void *
194GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl) 235GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl)
195{ 236{
196 void *cls = h->info_cls; 237 void *ret = pl->peers_cb_cls;
197 238
198 h->info_cb.peers_cb = NULL; 239 if (NULL != pl->mq)
199 h->info_cls = NULL; 240 GNUNET_MQ_destroy (pl->mq);
200 return cls; 241 if (NULL != pl->reconnect_task)
242 GNUNET_SCHEDULER_cancel (pl->reconnect_task);
243 GNUNET_free (pl);
244 return ret;
201} 245}
202 246
203 247/* end of cadet_api_list_peers.c */
204
205
206