diff options
Diffstat (limited to 'src/ats/ats_api2_application.c')
-rw-r--r-- | src/ats/ats_api2_application.c | 203 |
1 files changed, 100 insertions, 103 deletions
diff --git a/src/ats/ats_api2_application.c b/src/ats/ats_api2_application.c index 444ac3ae3..2a21a116a 100644 --- a/src/ats/ats_api2_application.c +++ b/src/ats/ats_api2_application.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file ats/ats_api2_application.c | 21 | * @file ats/ats_api2_application.c |
22 | * @brief enable clients to ask ATS about establishing connections to peers | 22 | * @brief enable clients to ask ATS about establishing connections to peers |
@@ -28,14 +28,13 @@ | |||
28 | #include "ats2.h" | 28 | #include "ats2.h" |
29 | 29 | ||
30 | 30 | ||
31 | #define LOG(kind,...) GNUNET_log_from(kind, "ats-application-api", __VA_ARGS__) | 31 | #define LOG(kind, ...) GNUNET_log_from(kind, "ats-application-api", __VA_ARGS__) |
32 | 32 | ||
33 | 33 | ||
34 | /** | 34 | /** |
35 | * Handle for ATS address suggestion requests. | 35 | * Handle for ATS address suggestion requests. |
36 | */ | 36 | */ |
37 | struct GNUNET_ATS_ApplicationSuggestHandle | 37 | struct GNUNET_ATS_ApplicationSuggestHandle { |
38 | { | ||
39 | /** | 38 | /** |
40 | * ID of the peer for which address suggestion was requested. | 39 | * ID of the peer for which address suggestion was requested. |
41 | */ | 40 | */ |
@@ -61,9 +60,7 @@ struct GNUNET_ATS_ApplicationSuggestHandle | |||
61 | /** | 60 | /** |
62 | * Handle to the ATS subsystem for application management. | 61 | * Handle to the ATS subsystem for application management. |
63 | */ | 62 | */ |
64 | struct GNUNET_ATS_ApplicationHandle | 63 | struct GNUNET_ATS_ApplicationHandle { |
65 | { | ||
66 | |||
67 | /** | 64 | /** |
68 | * Our configuration. | 65 | * Our configuration. |
69 | */ | 66 | */ |
@@ -99,7 +96,7 @@ struct GNUNET_ATS_ApplicationHandle | |||
99 | * @param ch handle to use to re-connect. | 96 | * @param ch handle to use to re-connect. |
100 | */ | 97 | */ |
101 | static void | 98 | static void |
102 | reconnect (struct GNUNET_ATS_ApplicationHandle *ch); | 99 | reconnect(struct GNUNET_ATS_ApplicationHandle *ch); |
103 | 100 | ||
104 | 101 | ||
105 | /** | 102 | /** |
@@ -108,12 +105,12 @@ reconnect (struct GNUNET_ATS_ApplicationHandle *ch); | |||
108 | * @param cls handle to use to re-connect. | 105 | * @param cls handle to use to re-connect. |
109 | */ | 106 | */ |
110 | static void | 107 | static void |
111 | reconnect_task (void *cls) | 108 | reconnect_task(void *cls) |
112 | { | 109 | { |
113 | struct GNUNET_ATS_ApplicationHandle *ch = cls; | 110 | struct GNUNET_ATS_ApplicationHandle *ch = cls; |
114 | 111 | ||
115 | ch->task = NULL; | 112 | ch->task = NULL; |
116 | reconnect (ch); | 113 | reconnect(ch); |
117 | } | 114 | } |
118 | 115 | ||
119 | 116 | ||
@@ -123,17 +120,17 @@ reconnect_task (void *cls) | |||
123 | * @param ch our handle | 120 | * @param ch our handle |
124 | */ | 121 | */ |
125 | static void | 122 | static void |
126 | force_reconnect (struct GNUNET_ATS_ApplicationHandle *ch) | 123 | force_reconnect(struct GNUNET_ATS_ApplicationHandle *ch) |
127 | { | 124 | { |
128 | if (NULL != ch->mq) | 125 | if (NULL != ch->mq) |
129 | { | 126 | { |
130 | GNUNET_MQ_destroy (ch->mq); | 127 | GNUNET_MQ_destroy(ch->mq); |
131 | ch->mq = NULL; | 128 | ch->mq = NULL; |
132 | } | 129 | } |
133 | ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff); | 130 | ch->backoff = GNUNET_TIME_STD_BACKOFF(ch->backoff); |
134 | ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, | 131 | ch->task = GNUNET_SCHEDULER_add_delayed(ch->backoff, |
135 | &reconnect_task, | 132 | &reconnect_task, |
136 | ch); | 133 | ch); |
137 | } | 134 | } |
138 | 135 | ||
139 | 136 | ||
@@ -145,15 +142,15 @@ force_reconnect (struct GNUNET_ATS_ApplicationHandle *ch) | |||
145 | * @param error details about the error | 142 | * @param error details about the error |
146 | */ | 143 | */ |
147 | static void | 144 | static void |
148 | error_handler (void *cls, | 145 | error_handler(void *cls, |
149 | enum GNUNET_MQ_Error error) | 146 | enum GNUNET_MQ_Error error) |
150 | { | 147 | { |
151 | struct GNUNET_ATS_ApplicationHandle *ch = cls; | 148 | struct GNUNET_ATS_ApplicationHandle *ch = cls; |
152 | 149 | ||
153 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 150 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
154 | "ATS connection died (code %d), reconnecting\n", | 151 | "ATS connection died (code %d), reconnecting\n", |
155 | (int) error); | 152 | (int)error); |
156 | force_reconnect (ch); | 153 | force_reconnect(ch); |
157 | } | 154 | } |
158 | 155 | ||
159 | 156 | ||
@@ -167,9 +164,9 @@ error_handler (void *cls, | |||
167 | * failure (message queue no longer exists) | 164 | * failure (message queue no longer exists) |
168 | */ | 165 | */ |
169 | static int | 166 | static int |
170 | transmit_suggestion (void *cls, | 167 | transmit_suggestion(void *cls, |
171 | const struct GNUNET_PeerIdentity *peer, | 168 | const struct GNUNET_PeerIdentity *peer, |
172 | void *value) | 169 | void *value) |
173 | { | 170 | { |
174 | struct GNUNET_ATS_ApplicationHandle *ch = cls; | 171 | struct GNUNET_ATS_ApplicationHandle *ch = cls; |
175 | struct GNUNET_ATS_ApplicationSuggestHandle *sh = value; | 172 | struct GNUNET_ATS_ApplicationSuggestHandle *sh = value; |
@@ -178,12 +175,12 @@ transmit_suggestion (void *cls, | |||
178 | 175 | ||
179 | if (NULL == ch->mq) | 176 | if (NULL == ch->mq) |
180 | return GNUNET_SYSERR; | 177 | return GNUNET_SYSERR; |
181 | ev = GNUNET_MQ_msg (m, | 178 | ev = GNUNET_MQ_msg(m, |
182 | GNUNET_MESSAGE_TYPE_ATS_SUGGEST); | 179 | GNUNET_MESSAGE_TYPE_ATS_SUGGEST); |
183 | m->pk = htonl ((uint32_t) sh->pk); | 180 | m->pk = htonl((uint32_t)sh->pk); |
184 | m->bw = sh->bw; | 181 | m->bw = sh->bw; |
185 | m->peer = *peer; | 182 | m->peer = *peer; |
186 | GNUNET_MQ_send (ch->mq, ev); | 183 | GNUNET_MQ_send(ch->mq, ev); |
187 | return GNUNET_OK; | 184 | return GNUNET_OK; |
188 | } | 185 | } |
189 | 186 | ||
@@ -194,26 +191,26 @@ transmit_suggestion (void *cls, | |||
194 | * @param ch handle to use to re-connect. | 191 | * @param ch handle to use to re-connect. |
195 | */ | 192 | */ |
196 | static void | 193 | static void |
197 | reconnect (struct GNUNET_ATS_ApplicationHandle *ch) | 194 | reconnect(struct GNUNET_ATS_ApplicationHandle *ch) |
198 | { | 195 | { |
199 | static const struct GNUNET_MQ_MessageHandler handlers[] = { | 196 | static const struct GNUNET_MQ_MessageHandler handlers[] = { |
200 | { NULL, 0, 0 } | 197 | { NULL, 0, 0 } |
201 | }; | 198 | }; |
202 | 199 | ||
203 | GNUNET_assert (NULL == ch->mq); | 200 | GNUNET_assert(NULL == ch->mq); |
204 | ch->mq = GNUNET_CLIENT_connect (ch->cfg, | 201 | ch->mq = GNUNET_CLIENT_connect(ch->cfg, |
205 | "ats", | 202 | "ats", |
206 | handlers, | 203 | handlers, |
207 | &error_handler, | 204 | &error_handler, |
208 | ch); | 205 | ch); |
209 | if (NULL == ch->mq) | 206 | if (NULL == ch->mq) |
210 | { | 207 | { |
211 | force_reconnect (ch); | 208 | force_reconnect(ch); |
212 | return; | 209 | return; |
213 | } | 210 | } |
214 | GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, | 211 | GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests, |
215 | &transmit_suggestion, | 212 | &transmit_suggestion, |
216 | ch); | 213 | ch); |
217 | } | 214 | } |
218 | 215 | ||
219 | 216 | ||
@@ -224,15 +221,15 @@ reconnect (struct GNUNET_ATS_ApplicationHandle *ch) | |||
224 | * @return ats application handle, NULL on error | 221 | * @return ats application handle, NULL on error |
225 | */ | 222 | */ |
226 | struct GNUNET_ATS_ApplicationHandle * | 223 | struct GNUNET_ATS_ApplicationHandle * |
227 | GNUNET_ATS_application_init (const struct GNUNET_CONFIGURATION_Handle *cfg) | 224 | GNUNET_ATS_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg) |
228 | { | 225 | { |
229 | struct GNUNET_ATS_ApplicationHandle *ch; | 226 | struct GNUNET_ATS_ApplicationHandle *ch; |
230 | 227 | ||
231 | ch = GNUNET_new (struct GNUNET_ATS_ApplicationHandle); | 228 | ch = GNUNET_new(struct GNUNET_ATS_ApplicationHandle); |
232 | ch->cfg = cfg; | 229 | ch->cfg = cfg; |
233 | ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, | 230 | ch->sug_requests = GNUNET_CONTAINER_multipeermap_create(32, |
234 | GNUNET_YES); | 231 | GNUNET_YES); |
235 | reconnect (ch); | 232 | reconnect(ch); |
236 | return ch; | 233 | return ch; |
237 | } | 234 | } |
238 | 235 | ||
@@ -247,13 +244,13 @@ GNUNET_ATS_application_init (const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
247 | * @return #GNUNET_OK (continue to iterate) | 244 | * @return #GNUNET_OK (continue to iterate) |
248 | */ | 245 | */ |
249 | static int | 246 | static int |
250 | free_sug_handle (void *cls, | 247 | free_sug_handle(void *cls, |
251 | const struct GNUNET_PeerIdentity *key, | 248 | const struct GNUNET_PeerIdentity *key, |
252 | void *value) | 249 | void *value) |
253 | { | 250 | { |
254 | struct GNUNET_ATS_ApplicationSuggestHandle *cur = value; | 251 | struct GNUNET_ATS_ApplicationSuggestHandle *cur = value; |
255 | 252 | ||
256 | GNUNET_free (cur); | 253 | GNUNET_free(cur); |
257 | return GNUNET_OK; | 254 | return GNUNET_OK; |
258 | } | 255 | } |
259 | 256 | ||
@@ -264,23 +261,23 @@ free_sug_handle (void *cls, | |||
264 | * @param ch handle to release | 261 | * @param ch handle to release |
265 | */ | 262 | */ |
266 | void | 263 | void |
267 | GNUNET_ATS_application_done (struct GNUNET_ATS_ApplicationHandle *ch) | 264 | GNUNET_ATS_application_done(struct GNUNET_ATS_ApplicationHandle *ch) |
268 | { | 265 | { |
269 | if (NULL != ch->mq) | 266 | if (NULL != ch->mq) |
270 | { | 267 | { |
271 | GNUNET_MQ_destroy (ch->mq); | 268 | GNUNET_MQ_destroy(ch->mq); |
272 | ch->mq = NULL; | 269 | ch->mq = NULL; |
273 | } | 270 | } |
274 | if (NULL != ch->task) | 271 | if (NULL != ch->task) |
275 | { | 272 | { |
276 | GNUNET_SCHEDULER_cancel (ch->task); | 273 | GNUNET_SCHEDULER_cancel(ch->task); |
277 | ch->task = NULL; | 274 | ch->task = NULL; |
278 | } | 275 | } |
279 | GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, | 276 | GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests, |
280 | &free_sug_handle, | 277 | &free_sug_handle, |
281 | NULL); | 278 | NULL); |
282 | GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests); | 279 | GNUNET_CONTAINER_multipeermap_destroy(ch->sug_requests); |
283 | GNUNET_free (ch); | 280 | GNUNET_free(ch); |
284 | } | 281 | } |
285 | 282 | ||
286 | 283 | ||
@@ -298,31 +295,31 @@ GNUNET_ATS_application_done (struct GNUNET_ATS_ApplicationHandle *ch) | |||
298 | * @return suggest handle, NULL if a request is already pending | 295 | * @return suggest handle, NULL if a request is already pending |
299 | */ | 296 | */ |
300 | struct GNUNET_ATS_ApplicationSuggestHandle * | 297 | struct GNUNET_ATS_ApplicationSuggestHandle * |
301 | GNUNET_ATS_application_suggest (struct GNUNET_ATS_ApplicationHandle *ch, | 298 | GNUNET_ATS_application_suggest(struct GNUNET_ATS_ApplicationHandle *ch, |
302 | const struct GNUNET_PeerIdentity *peer, | 299 | const struct GNUNET_PeerIdentity *peer, |
303 | enum GNUNET_MQ_PreferenceKind pk, | 300 | enum GNUNET_MQ_PreferenceKind pk, |
304 | struct GNUNET_BANDWIDTH_Value32NBO bw) | 301 | struct GNUNET_BANDWIDTH_Value32NBO bw) |
305 | { | 302 | { |
306 | struct GNUNET_ATS_ApplicationSuggestHandle *s; | 303 | struct GNUNET_ATS_ApplicationSuggestHandle *s; |
307 | 304 | ||
308 | s = GNUNET_new (struct GNUNET_ATS_ApplicationSuggestHandle); | 305 | s = GNUNET_new(struct GNUNET_ATS_ApplicationSuggestHandle); |
309 | s->ch = ch; | 306 | s->ch = ch; |
310 | s->id = *peer; | 307 | s->id = *peer; |
311 | s->pk = pk; | 308 | s->pk = pk; |
312 | s->bw = bw; | 309 | s->bw = bw; |
313 | (void) GNUNET_CONTAINER_multipeermap_put (ch->sug_requests, | 310 | (void)GNUNET_CONTAINER_multipeermap_put(ch->sug_requests, |
314 | &s->id, | 311 | &s->id, |
315 | s, | 312 | s, |
316 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | 313 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); |
317 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 314 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
318 | "Requesting ATS to suggest address for `%s'\n", | 315 | "Requesting ATS to suggest address for `%s'\n", |
319 | GNUNET_i2s (peer)); | 316 | GNUNET_i2s(peer)); |
320 | if (NULL == ch->mq) | 317 | if (NULL == ch->mq) |
321 | return s; | 318 | return s; |
322 | GNUNET_assert (GNUNET_OK == | 319 | GNUNET_assert(GNUNET_OK == |
323 | transmit_suggestion (ch, | 320 | transmit_suggestion(ch, |
324 | &s->id, | 321 | &s->id, |
325 | s)); | 322 | s)); |
326 | return s; | 323 | return s; |
327 | } | 324 | } |
328 | 325 | ||
@@ -333,32 +330,32 @@ GNUNET_ATS_application_suggest (struct GNUNET_ATS_ApplicationHandle *ch, | |||
333 | * @param sh handle to stop | 330 | * @param sh handle to stop |
334 | */ | 331 | */ |
335 | void | 332 | void |
336 | GNUNET_ATS_application_suggest_cancel (struct GNUNET_ATS_ApplicationSuggestHandle *sh) | 333 | GNUNET_ATS_application_suggest_cancel(struct GNUNET_ATS_ApplicationSuggestHandle *sh) |
337 | { | 334 | { |
338 | struct GNUNET_ATS_ApplicationHandle *ch = sh->ch; | 335 | struct GNUNET_ATS_ApplicationHandle *ch = sh->ch; |
339 | struct GNUNET_MQ_Envelope *ev; | 336 | struct GNUNET_MQ_Envelope *ev; |
340 | struct ExpressPreferenceMessage *m; | 337 | struct ExpressPreferenceMessage *m; |
341 | 338 | ||
342 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 339 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
343 | "Telling ATS we no longer care for an address for `%s'\n", | 340 | "Telling ATS we no longer care for an address for `%s'\n", |
344 | GNUNET_i2s (&sh->id)); | 341 | GNUNET_i2s(&sh->id)); |
345 | GNUNET_assert (GNUNET_OK == | 342 | GNUNET_assert(GNUNET_OK == |
346 | GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, | 343 | GNUNET_CONTAINER_multipeermap_remove(ch->sug_requests, |
347 | &sh->id, | 344 | &sh->id, |
348 | sh)); | 345 | sh)); |
349 | if (NULL == ch->mq) | 346 | if (NULL == ch->mq) |
350 | { | 347 | { |
351 | GNUNET_free (sh); | 348 | GNUNET_free(sh); |
352 | return; | 349 | return; |
353 | } | 350 | } |
354 | ev = GNUNET_MQ_msg (m, | 351 | ev = GNUNET_MQ_msg(m, |
355 | GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL); | 352 | GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL); |
356 | m->pk = htonl ((uint32_t) sh->pk); | 353 | m->pk = htonl((uint32_t)sh->pk); |
357 | m->bw = sh->bw; | 354 | m->bw = sh->bw; |
358 | m->peer = sh->id; | 355 | m->peer = sh->id; |
359 | GNUNET_MQ_send (ch->mq, | 356 | GNUNET_MQ_send(ch->mq, |
360 | ev); | 357 | ev); |
361 | GNUNET_free (sh); | 358 | GNUNET_free(sh); |
362 | } | 359 | } |
363 | 360 | ||
364 | 361 | ||