diff options
Diffstat (limited to 'src/core/core_api.c')
-rw-r--r-- | src/core/core_api.c | 1071 |
1 files changed, 1071 insertions, 0 deletions
diff --git a/src/core/core_api.c b/src/core/core_api.c new file mode 100644 index 000000000..10fa0ccdd --- /dev/null +++ b/src/core/core_api.c | |||
@@ -0,0 +1,1071 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 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 2, 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.c | ||
23 | * @brief core service; this is the main API for encrypted P2P | ||
24 | * communications | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_core_service.h" | ||
29 | #include "core.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Context for the core service connection. | ||
34 | */ | ||
35 | struct GNUNET_CORE_Handle | ||
36 | { | ||
37 | |||
38 | /** | ||
39 | * Our scheduler. | ||
40 | */ | ||
41 | struct GNUNET_SCHEDULER_Handle *sched; | ||
42 | |||
43 | /** | ||
44 | * Configuration we're using. | ||
45 | */ | ||
46 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
47 | |||
48 | /** | ||
49 | * Closure for the various callbacks. | ||
50 | */ | ||
51 | void *cls; | ||
52 | |||
53 | /** | ||
54 | * Function to call once we've handshaked with the core service. | ||
55 | */ | ||
56 | GNUNET_CORE_StartupCallback init; | ||
57 | |||
58 | /** | ||
59 | * Function to call whenever we're notified about a peer connecting. | ||
60 | */ | ||
61 | GNUNET_CORE_ClientEventHandler connects; | ||
62 | |||
63 | /** | ||
64 | * Function to call whenever we're notified about a peer disconnecting. | ||
65 | */ | ||
66 | GNUNET_CORE_ClientEventHandler disconnects; | ||
67 | |||
68 | /** | ||
69 | * Function to call whenever we're asked to generate traffic | ||
70 | * (data provided to be transmitted back to the service). | ||
71 | */ | ||
72 | GNUNET_CORE_BufferFillCallback bfc; | ||
73 | |||
74 | /** | ||
75 | * Function to call whenever we receive an inbound message. | ||
76 | */ | ||
77 | GNUNET_CORE_MessageCallback inbound_notify; | ||
78 | |||
79 | /** | ||
80 | * Function to call whenever we receive an outbound message. | ||
81 | */ | ||
82 | GNUNET_CORE_MessageCallback outbound_notify; | ||
83 | |||
84 | /** | ||
85 | * Function handlers for messages of particular type. | ||
86 | */ | ||
87 | const struct GNUNET_CORE_MessageHandler *handlers; | ||
88 | |||
89 | /** | ||
90 | * Our connection to the service. | ||
91 | */ | ||
92 | struct GNUNET_CLIENT_Connection *client; | ||
93 | |||
94 | /** | ||
95 | * Handle for our current transmission request. | ||
96 | */ | ||
97 | struct GNUNET_NETWORK_TransmitHandle *th; | ||
98 | |||
99 | /** | ||
100 | * Head of doubly-linked list of pending requests. | ||
101 | */ | ||
102 | struct GNUNET_CORE_TransmitHandle *pending_head; | ||
103 | |||
104 | /** | ||
105 | * Tail of doubly-linked list of pending requests. | ||
106 | */ | ||
107 | struct GNUNET_CORE_TransmitHandle *pending_tail; | ||
108 | |||
109 | /** | ||
110 | * Currently submitted request (or NULL) | ||
111 | */ | ||
112 | struct GNUNET_CORE_TransmitHandle *submitted; | ||
113 | |||
114 | /** | ||
115 | * How long to wait until we time out the connection attempt? | ||
116 | */ | ||
117 | struct GNUNET_TIME_Absolute startup_timeout; | ||
118 | |||
119 | /** | ||
120 | * ID of reconnect task (if any). | ||
121 | */ | ||
122 | GNUNET_SCHEDULER_TaskIdentifier reconnect_task; | ||
123 | |||
124 | /** | ||
125 | * Number of entries in the handlers array. | ||
126 | */ | ||
127 | unsigned int hcnt; | ||
128 | |||
129 | /** | ||
130 | * For inbound notifications without a specific handler, do | ||
131 | * we expect to only receive headers? | ||
132 | */ | ||
133 | int inbound_hdr_only; | ||
134 | |||
135 | /** | ||
136 | * For outbound notifications without a specific handler, do | ||
137 | * we expect to only receive headers? | ||
138 | */ | ||
139 | int outbound_hdr_only; | ||
140 | |||
141 | /** | ||
142 | * Are we currently disconnected and hence unable to forward | ||
143 | * requests? | ||
144 | */ | ||
145 | int currently_down; | ||
146 | }; | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Handle for a transmission request. | ||
151 | */ | ||
152 | struct GNUNET_CORE_TransmitHandle | ||
153 | { | ||
154 | |||
155 | /** | ||
156 | * We keep active transmit handles in a doubly-linked list. | ||
157 | */ | ||
158 | struct GNUNET_CORE_TransmitHandle *next; | ||
159 | |||
160 | /** | ||
161 | * We keep active transmit handles in a doubly-linked list. | ||
162 | */ | ||
163 | struct GNUNET_CORE_TransmitHandle *prev; | ||
164 | |||
165 | /** | ||
166 | * Corresponding core handle. | ||
167 | */ | ||
168 | struct GNUNET_CORE_Handle *ch; | ||
169 | |||
170 | /** | ||
171 | * Function that will be called to get the actual request | ||
172 | * (once we are ready to transmit this request to the core). | ||
173 | * The function will be called with a NULL buffer to signal | ||
174 | * timeout. | ||
175 | */ | ||
176 | GNUNET_NETWORK_TransmitReadyNotify get_message; | ||
177 | |||
178 | /** | ||
179 | * Closure for get_message. | ||
180 | */ | ||
181 | void *get_message_cls; | ||
182 | |||
183 | /** | ||
184 | * If this entry is for a configuration request, pointer | ||
185 | * to the information callback; otherwise NULL. | ||
186 | */ | ||
187 | GNUNET_CORE_PeerConfigurationInfoCallback info; | ||
188 | |||
189 | /** | ||
190 | * Closure for info. | ||
191 | */ | ||
192 | void *info_cls; | ||
193 | |||
194 | /** | ||
195 | * If this entry is for a transmission request, pointer | ||
196 | * to the notify callback; otherwise NULL. | ||
197 | */ | ||
198 | GNUNET_NETWORK_TransmitReadyNotify notify; | ||
199 | |||
200 | /** | ||
201 | * Closure for notify. | ||
202 | */ | ||
203 | void *notify_cls; | ||
204 | |||
205 | /** | ||
206 | * Peer the request is about. | ||
207 | */ | ||
208 | struct GNUNET_PeerIdentity peer; | ||
209 | |||
210 | /** | ||
211 | * Timeout for this handle. | ||
212 | */ | ||
213 | struct GNUNET_TIME_Absolute timeout; | ||
214 | |||
215 | /** | ||
216 | * ID of timeout task. | ||
217 | */ | ||
218 | GNUNET_SCHEDULER_TaskIdentifier timeout_task; | ||
219 | |||
220 | /** | ||
221 | * How important is this message? | ||
222 | */ | ||
223 | uint32_t priority; | ||
224 | |||
225 | /** | ||
226 | * Size of this request. | ||
227 | */ | ||
228 | uint16_t msize; | ||
229 | |||
230 | |||
231 | }; | ||
232 | |||
233 | |||
234 | /** | ||
235 | * Function called when we are ready to transmit our | ||
236 | * "START" message (or when this operation timed out). | ||
237 | * | ||
238 | * @param cls closure | ||
239 | * @param size number of bytes available in buf | ||
240 | * @param buf where the callee should write the message | ||
241 | * @return number of bytes written to buf | ||
242 | */ | ||
243 | static size_t transmit_start (void *cls, size_t size, void *buf); | ||
244 | |||
245 | |||
246 | /** | ||
247 | * Our current client connection went down. Clean it up | ||
248 | * and try to reconnect! | ||
249 | */ | ||
250 | static void | ||
251 | reconnect (struct GNUNET_CORE_Handle *h) | ||
252 | { | ||
253 | GNUNET_CLIENT_disconnect (h->client); | ||
254 | h->currently_down = GNUNET_YES; | ||
255 | h->client = GNUNET_CLIENT_connect (h->sched, "core", h->cfg); | ||
256 | h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
257 | sizeof (struct InitMessage) + | ||
258 | sizeof (uint16_t) * h->hcnt, | ||
259 | GNUNET_TIME_UNIT_SECONDS, | ||
260 | &transmit_start, h); | ||
261 | } | ||
262 | |||
263 | |||
264 | /** | ||
265 | * The given request hit its timeout. Remove from the | ||
266 | * doubly-linked list and call the respective continuation. | ||
267 | * | ||
268 | * @param cls the transmit handle of the request that timed out | ||
269 | * @param tc context, can be NULL (!) | ||
270 | */ | ||
271 | static void | ||
272 | timeout_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
273 | { | ||
274 | struct GNUNET_CORE_TransmitHandle *th = cls; | ||
275 | struct GNUNET_CORE_Handle *h; | ||
276 | |||
277 | h = th->ch; | ||
278 | th->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK; | ||
279 | GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL)); | ||
280 | GNUNET_CORE_notify_transmit_ready_cancel (th); | ||
281 | } | ||
282 | |||
283 | |||
284 | /** | ||
285 | * Function called when we are ready to transmit a request from our | ||
286 | * request list (or when this operation timed out). | ||
287 | * | ||
288 | * @param cls closure | ||
289 | * @param size number of bytes available in buf | ||
290 | * @param buf where the callee should write the message | ||
291 | * @return number of bytes written to buf | ||
292 | */ | ||
293 | static size_t | ||
294 | request_start (void *cls, size_t size, void *buf) | ||
295 | { | ||
296 | struct GNUNET_CORE_Handle *h = cls; | ||
297 | struct GNUNET_CORE_TransmitHandle *th; | ||
298 | size_t ret; | ||
299 | |||
300 | h->th = NULL; | ||
301 | th = h->pending_head; | ||
302 | if (buf == NULL) | ||
303 | { | ||
304 | timeout_request (th, NULL); | ||
305 | return 0; | ||
306 | } | ||
307 | /* create new timeout task (in case core takes too long to respond!) */ | ||
308 | th->timeout_task = GNUNET_SCHEDULER_add_delayed (h->sched, | ||
309 | GNUNET_NO, | ||
310 | GNUNET_SCHEDULER_PRIORITY_KEEP, | ||
311 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
312 | GNUNET_TIME_absolute_get_remaining | ||
313 | (th->timeout), | ||
314 | &timeout_request, th); | ||
315 | /* remove th from doubly-linked pending list, move to submitted */ | ||
316 | GNUNET_assert (th->prev == NULL); | ||
317 | h->pending_head = th->next; | ||
318 | if (th->next == NULL) | ||
319 | h->pending_tail = NULL; | ||
320 | else | ||
321 | th->next->prev = NULL; | ||
322 | GNUNET_assert (h->submitted == NULL); | ||
323 | h->submitted = th; | ||
324 | GNUNET_assert (size >= th->msize); | ||
325 | ret = th->get_message (th->get_message_cls, size, buf); | ||
326 | GNUNET_assert (ret <= size); | ||
327 | return ret; | ||
328 | } | ||
329 | |||
330 | |||
331 | /** | ||
332 | * Check the list of pending requests, send the next | ||
333 | * one to the core. | ||
334 | */ | ||
335 | static void | ||
336 | trigger_next_request (struct GNUNET_CORE_Handle *h) | ||
337 | { | ||
338 | struct GNUNET_CORE_TransmitHandle *th; | ||
339 | if (h->currently_down) | ||
340 | return; /* connection temporarily down */ | ||
341 | if (NULL == (th = h->pending_head)) | ||
342 | return; /* no requests pending */ | ||
343 | GNUNET_assert (NULL == h->th); | ||
344 | GNUNET_SCHEDULER_cancel (h->sched, th->timeout_task); | ||
345 | th->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK; | ||
346 | h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
347 | th->msize, | ||
348 | GNUNET_TIME_absolute_get_remaining | ||
349 | (th->timeout), &request_start, | ||
350 | h); | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
355 | * cls is a pointer to a 32 bit number followed by that | ||
356 | * amount of data. If possible, copy to buf and return | ||
357 | * number of bytes copied. Always free the buffer. | ||
358 | */ | ||
359 | static size_t | ||
360 | copy_and_free (void *cls, size_t size, void *buf) | ||
361 | { | ||
362 | char *cbuf = cls; | ||
363 | uint32_t have; | ||
364 | |||
365 | memcpy (&have, cbuf, sizeof (uint32_t)); | ||
366 | if (have > size) | ||
367 | { | ||
368 | /* timeout / error case */ | ||
369 | GNUNET_free (cbuf); | ||
370 | return 0; | ||
371 | } | ||
372 | memcpy (buf, cbuf + sizeof (uint32_t), have); | ||
373 | GNUNET_free (cbuf); | ||
374 | return have; | ||
375 | } | ||
376 | |||
377 | |||
378 | /** | ||
379 | * Call bfc callback to solicit traffic for the given peer. | ||
380 | */ | ||
381 | static void | ||
382 | solicit_traffic (struct GNUNET_CORE_Handle *h, | ||
383 | const struct GNUNET_PeerIdentity *peer, uint32_t amount) | ||
384 | { | ||
385 | char buf[amount]; | ||
386 | size_t have; | ||
387 | char *cbuf; | ||
388 | |||
389 | have = h->bfc (h->cls, peer, buf, amount); | ||
390 | if (have == 0) | ||
391 | return; | ||
392 | GNUNET_assert (have >= sizeof (struct GNUNET_MessageHeader)); | ||
393 | cbuf = GNUNET_malloc (have + sizeof (uint32_t)); | ||
394 | memcpy (cbuf, &have, sizeof (uint32_t)); | ||
395 | memcpy (cbuf + sizeof (uint32_t), buf, have); | ||
396 | GNUNET_CORE_notify_transmit_ready (h, | ||
397 | 0, | ||
398 | GNUNET_TIME_UNIT_SECONDS, | ||
399 | peer, have, ©_and_free, cbuf); | ||
400 | } | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Handler for most messages received from the core. | ||
405 | */ | ||
406 | static void | ||
407 | main_handler (void *cls, const struct GNUNET_MessageHeader *msg) | ||
408 | { | ||
409 | struct GNUNET_CORE_Handle *h = cls; | ||
410 | unsigned int hpos; | ||
411 | const struct ConnectNotifyMessage *cnm; | ||
412 | const struct NotifyTrafficMessage *ntm; | ||
413 | const struct ConfigurationInfoMessage *cim; | ||
414 | const struct SolicitTrafficMessage *stm; | ||
415 | const struct GNUNET_MessageHeader *em; | ||
416 | uint16_t msize; | ||
417 | uint16_t et; | ||
418 | uint32_t ss; | ||
419 | const struct GNUNET_CORE_MessageHandler *mh; | ||
420 | |||
421 | if (msg == NULL) | ||
422 | { | ||
423 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
424 | _ | ||
425 | ("Client was disconnected from core service, trying to reconnect.\n")); | ||
426 | reconnect (h); | ||
427 | return; | ||
428 | } | ||
429 | msize = ntohs (msg->size); | ||
430 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
431 | "Processing message of type %u and size %u from core service\n", | ||
432 | ntohs (msg->type), msize); | ||
433 | switch (ntohs (msg->type)) | ||
434 | { | ||
435 | case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT: | ||
436 | if (NULL == h->connects) | ||
437 | { | ||
438 | GNUNET_break (0); | ||
439 | break; | ||
440 | } | ||
441 | if (msize != sizeof (struct ConnectNotifyMessage)) | ||
442 | { | ||
443 | GNUNET_break (0); | ||
444 | break; | ||
445 | } | ||
446 | cnm = (const struct ConnectNotifyMessage *) msg; | ||
447 | break; | ||
448 | case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT: | ||
449 | if (NULL == h->disconnects) | ||
450 | { | ||
451 | GNUNET_break (0); | ||
452 | break; | ||
453 | } | ||
454 | if (msize != sizeof (struct ConnectNotifyMessage)) | ||
455 | { | ||
456 | GNUNET_break (0); | ||
457 | break; | ||
458 | } | ||
459 | cnm = (const struct ConnectNotifyMessage *) msg; | ||
460 | break; | ||
461 | case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND: | ||
462 | if (msize < | ||
463 | sizeof (struct NotifyTrafficMessage) + | ||
464 | sizeof (struct GNUNET_MessageHeader)) | ||
465 | { | ||
466 | GNUNET_break (0); | ||
467 | break; | ||
468 | } | ||
469 | ntm = (const struct NotifyTrafficMessage *) msg; | ||
470 | em = (const struct GNUNET_MessageHeader *) &ntm[1]; | ||
471 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
472 | "Received message of type %u from peer `%4s'\n", | ||
473 | ntohs (em->type), GNUNET_i2s (&ntm->peer)); | ||
474 | if ((GNUNET_NO == h->inbound_hdr_only) && | ||
475 | (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage))) | ||
476 | { | ||
477 | GNUNET_break (0); | ||
478 | break; | ||
479 | } | ||
480 | et = ntohs (em->type); | ||
481 | for (hpos = 0; hpos < h->hcnt; hpos++) | ||
482 | { | ||
483 | mh = &h->handlers[hpos]; | ||
484 | if (mh->type != et) | ||
485 | continue; | ||
486 | if ((mh->expected_size != ntohs (em->size)) && | ||
487 | (mh->expected_size != 0)) | ||
488 | { | ||
489 | GNUNET_break (0); | ||
490 | continue; | ||
491 | } | ||
492 | if (GNUNET_OK != | ||
493 | h->handlers[hpos].callback (h->cls, &ntm->peer, em)) | ||
494 | { | ||
495 | /* error in processing, disconnect ! */ | ||
496 | reconnect (h); | ||
497 | return; | ||
498 | } | ||
499 | } | ||
500 | if (NULL != h->inbound_notify) | ||
501 | h->inbound_notify (h->cls, &ntm->peer, em); | ||
502 | break; | ||
503 | case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND: | ||
504 | if (msize < | ||
505 | sizeof (struct NotifyTrafficMessage) + | ||
506 | sizeof (struct GNUNET_MessageHeader)) | ||
507 | { | ||
508 | GNUNET_break (0); | ||
509 | break; | ||
510 | } | ||
511 | ntm = (const struct NotifyTrafficMessage *) msg; | ||
512 | em = (const struct GNUNET_MessageHeader *) &ntm[1]; | ||
513 | if ((GNUNET_NO == h->outbound_hdr_only) && | ||
514 | (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage))) | ||
515 | { | ||
516 | GNUNET_break (0); | ||
517 | break; | ||
518 | } | ||
519 | if (NULL == h->outbound_notify) | ||
520 | { | ||
521 | GNUNET_break (0); | ||
522 | break; | ||
523 | } | ||
524 | h->outbound_notify (h->cls, &ntm->peer, em); | ||
525 | break; | ||
526 | case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO: | ||
527 | if (msize != sizeof (struct ConfigurationInfoMessage)) | ||
528 | { | ||
529 | GNUNET_break (0); | ||
530 | break; | ||
531 | } | ||
532 | if (NULL == h->submitted) | ||
533 | break; | ||
534 | cim = (const struct ConfigurationInfoMessage *) msg; | ||
535 | |||
536 | /* process configuration data */ | ||
537 | if (h->submitted->info != NULL) | ||
538 | h->submitted->info (h->submitted->info_cls, | ||
539 | &h->submitted->peer, | ||
540 | ntohl (cim->bpm_in), | ||
541 | ntohl (cim->bpm_out), | ||
542 | GNUNET_TIME_relative_ntoh (cim->latency), | ||
543 | (int) ntohl (cim->reserved_amount), | ||
544 | cim->preference); | ||
545 | /* done, clean up! */ | ||
546 | GNUNET_CORE_notify_transmit_ready_cancel (h->submitted); | ||
547 | trigger_next_request (h); | ||
548 | break; | ||
549 | case GNUNET_MESSAGE_TYPE_CORE_SOLICIT_TRAFFIC: | ||
550 | if (msize != sizeof (struct SolicitTrafficMessage)) | ||
551 | { | ||
552 | GNUNET_break (0); | ||
553 | break; | ||
554 | } | ||
555 | stm = (const struct SolicitTrafficMessage *) msg; | ||
556 | if (NULL == h->bfc) | ||
557 | { | ||
558 | GNUNET_break (0); | ||
559 | break; | ||
560 | } | ||
561 | ss = ntohl (stm->solicit_size); | ||
562 | if ((ss > GNUNET_SERVER_MAX_MESSAGE_SIZE) || | ||
563 | (ss + sizeof (struct SendMessage) > GNUNET_SERVER_MAX_MESSAGE_SIZE)) | ||
564 | { | ||
565 | GNUNET_break (0); | ||
566 | break; | ||
567 | } | ||
568 | solicit_traffic (h, &stm->peer, ss); | ||
569 | break; | ||
570 | default: | ||
571 | GNUNET_break (0); | ||
572 | break; | ||
573 | } | ||
574 | GNUNET_CLIENT_receive (h->client, | ||
575 | &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); | ||
576 | } | ||
577 | |||
578 | |||
579 | |||
580 | /** | ||
581 | * Function called when we are ready to transmit our | ||
582 | * "START" message (or when this operation timed out). | ||
583 | * | ||
584 | * @param cls closure | ||
585 | * @param size number of bytes available in buf | ||
586 | * @param buf where the callee should write the message | ||
587 | * @return number of bytes written to buf | ||
588 | */ | ||
589 | static size_t transmit_start (void *cls, size_t size, void *buf); | ||
590 | |||
591 | |||
592 | /** | ||
593 | * Function called on the first message received from | ||
594 | * the service (contains our public key, etc.). | ||
595 | * Should trigger calling the init callback | ||
596 | * and then start our regular message processing. | ||
597 | * | ||
598 | * @param cls closure | ||
599 | * @param msg message received, NULL on timeout or fatal error | ||
600 | */ | ||
601 | static void | ||
602 | init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg) | ||
603 | { | ||
604 | struct GNUNET_CORE_Handle *h = cls; | ||
605 | const struct InitReplyMessage *m; | ||
606 | GNUNET_CORE_StartupCallback init; | ||
607 | struct GNUNET_PeerIdentity my_identity; | ||
608 | |||
609 | if ((msg == NULL) || | ||
610 | (ntohs (msg->size) != sizeof (struct InitReplyMessage)) || | ||
611 | (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY)) | ||
612 | { | ||
613 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
614 | _ | ||
615 | ("Error connecting to core service (failed to receive `%s' message).\n"), | ||
616 | "INIT_REPLY"); | ||
617 | GNUNET_break (msg == NULL); | ||
618 | transmit_start (h, 0, NULL); | ||
619 | return; | ||
620 | } | ||
621 | m = (const struct InitReplyMessage *) msg; | ||
622 | /* start our message processing loop */ | ||
623 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
624 | _ | ||
625 | ("Successfully connected to core service, starting processing loop.\n")); | ||
626 | h->currently_down = GNUNET_NO; | ||
627 | trigger_next_request (h); | ||
628 | GNUNET_CLIENT_receive (h->client, | ||
629 | &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); | ||
630 | if (NULL != (init = h->init)) | ||
631 | { | ||
632 | /* mark so we don't call init on reconnect */ | ||
633 | h->init = NULL; | ||
634 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
635 | _("Successfully connected to core service.\n")); | ||
636 | GNUNET_CRYPTO_hash (&m->publicKey, | ||
637 | sizeof (struct | ||
638 | GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
639 | &my_identity.hashPubKey); | ||
640 | init (h->cls, h, &my_identity, &m->publicKey); | ||
641 | } | ||
642 | } | ||
643 | |||
644 | |||
645 | static void | ||
646 | reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
647 | { | ||
648 | struct GNUNET_CORE_Handle *h = cls; | ||
649 | h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK; | ||
650 | reconnect (h); | ||
651 | } | ||
652 | |||
653 | |||
654 | /** | ||
655 | * Function called when we are ready to transmit our | ||
656 | * "START" message (or when this operation timed out). | ||
657 | * | ||
658 | * @param cls closure | ||
659 | * @param size number of bytes available in buf | ||
660 | * @param buf where the callee should write the message | ||
661 | * @return number of bytes written to buf | ||
662 | */ | ||
663 | static size_t | ||
664 | transmit_start (void *cls, size_t size, void *buf) | ||
665 | { | ||
666 | struct GNUNET_CORE_Handle *h = cls; | ||
667 | struct InitMessage *init; | ||
668 | uint16_t *ts; | ||
669 | uint16_t msize; | ||
670 | uint32_t opt; | ||
671 | unsigned int hpos; | ||
672 | struct GNUNET_TIME_Relative delay; | ||
673 | |||
674 | h->th = NULL; | ||
675 | if (size == 0) | ||
676 | { | ||
677 | if ((h->init == NULL) || | ||
678 | (GNUNET_TIME_absolute_get ().value < h->startup_timeout.value)) | ||
679 | { | ||
680 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
681 | _("Failed to connect to core service, retrying.\n")); | ||
682 | delay = GNUNET_TIME_absolute_get_remaining (h->startup_timeout); | ||
683 | if ((h->init == NULL) || (delay.value > 1000)) | ||
684 | delay = GNUNET_TIME_UNIT_SECONDS; | ||
685 | if (h->init == NULL) | ||
686 | h->startup_timeout = | ||
687 | GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES); | ||
688 | h->reconnect_task = | ||
689 | GNUNET_SCHEDULER_add_delayed (h->sched, GNUNET_NO, | ||
690 | GNUNET_SCHEDULER_PRIORITY_IDLE, | ||
691 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
692 | delay, &reconnect_task, h); | ||
693 | return 0; | ||
694 | } | ||
695 | /* timeout on initial connect */ | ||
696 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
697 | _("Failed to connect to core service, giving up.\n")); | ||
698 | h->init (h->cls, NULL, NULL, NULL); | ||
699 | GNUNET_CORE_disconnect (h); | ||
700 | return 0; | ||
701 | } | ||
702 | msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage); | ||
703 | GNUNET_assert (size >= msize); | ||
704 | init = buf; | ||
705 | init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT); | ||
706 | init->header.size = htons (msize); | ||
707 | opt = GNUNET_CORE_OPTION_NOTHING; | ||
708 | if (h->connects != NULL) | ||
709 | opt |= GNUNET_CORE_OPTION_SEND_CONNECT; | ||
710 | if (h->disconnects != NULL) | ||
711 | opt |= GNUNET_CORE_OPTION_SEND_DISCONNECT; | ||
712 | if (h->bfc != NULL) | ||
713 | opt |= GNUNET_CORE_OPTION_SEND_BFC; | ||
714 | if (h->inbound_notify != NULL) | ||
715 | { | ||
716 | if (h->inbound_hdr_only) | ||
717 | opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND; | ||
718 | else | ||
719 | opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND; | ||
720 | } | ||
721 | if (h->outbound_notify != NULL) | ||
722 | { | ||
723 | if (h->outbound_hdr_only) | ||
724 | opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND; | ||
725 | else | ||
726 | opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND; | ||
727 | } | ||
728 | init->options = htonl (opt); | ||
729 | ts = (uint16_t *) & init[1]; | ||
730 | for (hpos = 0; hpos < h->hcnt; hpos++) | ||
731 | ts[hpos] = htons (h->handlers[hpos].type); | ||
732 | GNUNET_CLIENT_receive (h->client, | ||
733 | &init_reply_handler, | ||
734 | h, | ||
735 | GNUNET_TIME_absolute_get_remaining (h-> | ||
736 | startup_timeout)); | ||
737 | return sizeof (struct InitMessage) + h->hcnt * sizeof (uint16_t); | ||
738 | } | ||
739 | |||
740 | |||
741 | /** | ||
742 | * Connect to the core service. Note that the connection may | ||
743 | * complete (or fail) asynchronously. | ||
744 | * | ||
745 | * @param sched scheduler to use | ||
746 | * @param cfg configuration to use | ||
747 | * @param timeout after how long should we give up trying to connect to the core service? | ||
748 | * @param cls closure for the various callbacks that follow (including handlers in the handlers array) | ||
749 | * @param init callback to call on timeout or once we have successfully | ||
750 | * connected to the core service | ||
751 | * @param connects function to call on peer connect, can be NULL | ||
752 | * @param disconnects function to call on peer disconnect / timeout, can be NULL | ||
753 | * @param bfc function to call to fill up spare bandwidth, can be NULL | ||
754 | * @param inbound_notify function to call for all inbound messages, can be NULL | ||
755 | * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the | ||
756 | * GNUNET_MessageHeader and hence we do not need to give it the full message; | ||
757 | * can be used to improve efficiency, ignored if inbound_notify is NULLL | ||
758 | * @param outbound_notify function to call for all outbound messages, can be NULL | ||
759 | * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the | ||
760 | * GNUNET_MessageHeader and hence we do not need to give it the full message | ||
761 | * can be used to improve efficiency, ignored if outbound_notify is NULLL | ||
762 | * @param handlers callbacks for messages we care about, NULL-terminated | ||
763 | */ | ||
764 | void | ||
765 | GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched, | ||
766 | struct GNUNET_CONFIGURATION_Handle *cfg, | ||
767 | struct GNUNET_TIME_Relative timeout, | ||
768 | void *cls, | ||
769 | GNUNET_CORE_StartupCallback init, | ||
770 | GNUNET_CORE_ClientEventHandler connects, | ||
771 | GNUNET_CORE_ClientEventHandler disconnects, | ||
772 | GNUNET_CORE_BufferFillCallback bfc, | ||
773 | GNUNET_CORE_MessageCallback inbound_notify, | ||
774 | int inbound_hdr_only, | ||
775 | GNUNET_CORE_MessageCallback outbound_notify, | ||
776 | int outbound_hdr_only, | ||
777 | const struct GNUNET_CORE_MessageHandler *handlers) | ||
778 | { | ||
779 | struct GNUNET_CORE_Handle *h; | ||
780 | |||
781 | GNUNET_assert (init != NULL); | ||
782 | h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle)); | ||
783 | h->sched = sched; | ||
784 | h->cfg = cfg; | ||
785 | h->cls = cls; | ||
786 | h->init = init; | ||
787 | h->connects = connects; | ||
788 | h->disconnects = disconnects; | ||
789 | h->bfc = bfc; | ||
790 | h->inbound_notify = inbound_notify; | ||
791 | h->outbound_notify = outbound_notify; | ||
792 | h->inbound_hdr_only = inbound_hdr_only; | ||
793 | h->outbound_hdr_only = outbound_hdr_only; | ||
794 | h->handlers = handlers; | ||
795 | h->client = GNUNET_CLIENT_connect (sched, "core", cfg); | ||
796 | h->startup_timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
797 | h->hcnt = 0; | ||
798 | while (handlers[h->hcnt].callback != NULL) | ||
799 | h->hcnt++; | ||
800 | GNUNET_assert (h->hcnt < | ||
801 | (GNUNET_SERVER_MAX_MESSAGE_SIZE - | ||
802 | sizeof (struct InitMessage)) / sizeof (uint16_t)); | ||
803 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
804 | "Trying to connect to core service in next %llu ms.\n", | ||
805 | timeout.value); | ||
806 | h->th = | ||
807 | GNUNET_CLIENT_notify_transmit_ready (h->client, | ||
808 | sizeof (struct InitMessage) + | ||
809 | sizeof (uint16_t) * h->hcnt, timeout, | ||
810 | &transmit_start, h); | ||
811 | } | ||
812 | |||
813 | |||
814 | /** | ||
815 | * Disconnect from the core service. | ||
816 | * | ||
817 | * @param handle connection to core to disconnect | ||
818 | */ | ||
819 | void | ||
820 | GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle) | ||
821 | { | ||
822 | if (handle->th != NULL) | ||
823 | GNUNET_NETWORK_notify_transmit_ready_cancel (handle->th); | ||
824 | if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) | ||
825 | GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task); | ||
826 | GNUNET_CLIENT_disconnect (handle->client); | ||
827 | GNUNET_free (handle); | ||
828 | } | ||
829 | |||
830 | |||
831 | /** | ||
832 | * Build the configure message. | ||
833 | */ | ||
834 | static size_t | ||
835 | produce_configure_message (void *cls, size_t size, void *buf) | ||
836 | { | ||
837 | struct GNUNET_CORE_TransmitHandle *th = cls; | ||
838 | struct GNUNET_CORE_Handle *ch = th->ch; | ||
839 | |||
840 | if (buf == NULL) | ||
841 | { | ||
842 | /* communicate handle timeout/error! */ | ||
843 | if (th->info != NULL) | ||
844 | th->info (th->info_cls, NULL, 0, 0, GNUNET_TIME_UNIT_ZERO, 0, 0.0); | ||
845 | if (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) | ||
846 | GNUNET_CORE_notify_transmit_ready_cancel (th); | ||
847 | if (ch->submitted == th) | ||
848 | ch->submitted = NULL; | ||
849 | trigger_next_request (ch); | ||
850 | return 0; | ||
851 | } | ||
852 | GNUNET_assert (size >= sizeof (struct RequestConfigureMessage)); | ||
853 | memcpy (buf, &th[1], sizeof (struct RequestConfigureMessage)); | ||
854 | if (th->prev == NULL) | ||
855 | ch->pending_head = th->next; | ||
856 | else | ||
857 | th->prev->next = th->next; | ||
858 | if (th->next == NULL) | ||
859 | ch->pending_tail = th->prev; | ||
860 | else | ||
861 | th->next->prev = th->prev; | ||
862 | GNUNET_assert (ch->submitted == NULL); | ||
863 | ch->submitted = th; | ||
864 | return sizeof (struct RequestConfigureMessage); | ||
865 | } | ||
866 | |||
867 | |||
868 | /** | ||
869 | * Obtain statistics and/or change preferences for the given peer. | ||
870 | * | ||
871 | * @param handle connection to core to use | ||
872 | * @param peer identifies the peer | ||
873 | * @param timeout after how long should we give up (and call "info" with NULL | ||
874 | * for "peer" to signal an error)? | ||
875 | * @param bpm_out set to the current bandwidth limit (sending) for this peer, | ||
876 | * caller should set "bpm_out" to "-1" to avoid changing | ||
877 | * the current value; otherwise "bpm_out" will be lowered to | ||
878 | * the specified value; passing a pointer to "0" can be used to force | ||
879 | * us to disconnect from the peer; "bpm_out" might not increase | ||
880 | * as specified since the upper bound is generally | ||
881 | * determined by the other peer! | ||
882 | * @param amount reserve N bytes for receiving, negative | ||
883 | * amounts can be used to undo a (recent) reservation; | ||
884 | * @param preference increase incoming traffic share preference by this amount; | ||
885 | * in the absence of "amount" reservations, we use this | ||
886 | * preference value to assign proportional bandwidth shares | ||
887 | * to all connected peers | ||
888 | * @param info function to call with the resulting configuration information | ||
889 | * @param info_cls closure for info | ||
890 | */ | ||
891 | void | ||
892 | GNUNET_CORE_peer_configure (struct GNUNET_CORE_Handle *handle, | ||
893 | const struct GNUNET_PeerIdentity *peer, | ||
894 | struct GNUNET_TIME_Relative timeout, | ||
895 | unsigned int bpm_out, | ||
896 | int amount, | ||
897 | double preference, | ||
898 | GNUNET_CORE_PeerConfigurationInfoCallback info, | ||
899 | void *info_cls) | ||
900 | { | ||
901 | struct RequestConfigureMessage *rcm; | ||
902 | struct GNUNET_CORE_TransmitHandle *th; | ||
903 | |||
904 | th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle) + | ||
905 | sizeof (struct RequestConfigureMessage)); | ||
906 | /* append to list */ | ||
907 | th->prev = handle->pending_tail; | ||
908 | if (handle->pending_tail == NULL) | ||
909 | handle->pending_head = th; | ||
910 | else | ||
911 | handle->pending_tail->next = th; | ||
912 | th->ch = handle; | ||
913 | th->get_message = &produce_configure_message; | ||
914 | th->get_message_cls = th; | ||
915 | th->info = info; | ||
916 | th->info_cls = info_cls; | ||
917 | th->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
918 | th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched, | ||
919 | GNUNET_NO, | ||
920 | GNUNET_SCHEDULER_PRIORITY_KEEP, | ||
921 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
922 | timeout, | ||
923 | &timeout_request, th); | ||
924 | th->msize = sizeof (struct RequestConfigureMessage); | ||
925 | rcm = (struct RequestConfigureMessage *) &th[1]; | ||
926 | rcm->header.size = htons (sizeof (struct RequestConfigureMessage)); | ||
927 | rcm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONFIGURE); | ||
928 | rcm->reserved = htonl (0); | ||
929 | rcm->limit_outbound_bpm = htonl (bpm_out); | ||
930 | rcm->reserve_inbound = htonl (amount); | ||
931 | rcm->preference_change = preference; | ||
932 | rcm->peer = *peer; | ||
933 | if (handle->pending_head == th) | ||
934 | trigger_next_request (handle); | ||
935 | } | ||
936 | |||
937 | |||
938 | /** | ||
939 | * Build the message requesting data transmission. | ||
940 | */ | ||
941 | static size_t | ||
942 | produce_send (void *cls, size_t size, void *buf) | ||
943 | { | ||
944 | struct GNUNET_CORE_TransmitHandle *th = cls; | ||
945 | struct GNUNET_CORE_Handle *h; | ||
946 | struct SendMessage *sm; | ||
947 | size_t dt; | ||
948 | GNUNET_NETWORK_TransmitReadyNotify notify; | ||
949 | void *notify_cls; | ||
950 | |||
951 | h = th->ch; | ||
952 | if (buf == NULL) | ||
953 | { | ||
954 | /* timeout or error */ | ||
955 | GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL)); | ||
956 | if (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) | ||
957 | GNUNET_CORE_notify_transmit_ready_cancel (th); | ||
958 | trigger_next_request (h); | ||
959 | return 0; | ||
960 | } | ||
961 | GNUNET_assert (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK); | ||
962 | sm = (struct SendMessage *) buf; | ||
963 | sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND); | ||
964 | sm->priority = htonl (th->priority); | ||
965 | sm->deadline = GNUNET_TIME_absolute_hton (th->timeout); | ||
966 | sm->peer = th->peer; | ||
967 | notify = th->notify; | ||
968 | notify_cls = th->notify_cls; | ||
969 | GNUNET_CORE_notify_transmit_ready_cancel (th); | ||
970 | trigger_next_request (h); | ||
971 | GNUNET_assert (size >= sizeof (struct SendMessage)); | ||
972 | dt = notify (notify_cls, size - sizeof (struct SendMessage), &sm[1]); | ||
973 | sm->header.size = htons (dt + sizeof (struct SendMessage)); | ||
974 | GNUNET_assert (dt + sizeof (struct SendMessage) < size); | ||
975 | return dt + sizeof (struct SendMessage); | ||
976 | } | ||
977 | |||
978 | |||
979 | /** | ||
980 | * Ask the core to call "notify" once it is ready to transmit the | ||
981 | * given number of bytes to the specified "target". If we are not yet | ||
982 | * connected to the specified peer, a call to this function will cause | ||
983 | * us to try to establish a connection. | ||
984 | * | ||
985 | * @param handle connection to core service | ||
986 | * @param priority how important is the message? | ||
987 | * @param maxdelay how long can the message wait? | ||
988 | * @param target who should receive the message, | ||
989 | * use NULL for this peer (loopback) | ||
990 | * @param notify_size how many bytes of buffer space does notify want? | ||
991 | * @param notify function to call when buffer space is available | ||
992 | * @param notify_cls closure for notify | ||
993 | * @return non-NULL if the notify callback was queued, | ||
994 | * NULL if we can not even queue the request (insufficient | ||
995 | * memory); if NULL is returned, "notify" will NOT be called. | ||
996 | */ | ||
997 | struct GNUNET_CORE_TransmitHandle * | ||
998 | GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, | ||
999 | unsigned int priority, | ||
1000 | struct GNUNET_TIME_Relative maxdelay, | ||
1001 | const struct GNUNET_PeerIdentity *target, | ||
1002 | size_t notify_size, | ||
1003 | GNUNET_NETWORK_TransmitReadyNotify notify, | ||
1004 | void *notify_cls) | ||
1005 | { | ||
1006 | struct GNUNET_CORE_TransmitHandle *th; | ||
1007 | |||
1008 | GNUNET_assert (notify_size + sizeof (struct SendMessage) < | ||
1009 | GNUNET_SERVER_MAX_MESSAGE_SIZE); | ||
1010 | th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle)); | ||
1011 | th->ch = handle; | ||
1012 | /* append to list */ | ||
1013 | th->prev = handle->pending_tail; | ||
1014 | if (handle->pending_tail == NULL) | ||
1015 | handle->pending_head = th; | ||
1016 | else | ||
1017 | handle->pending_tail->next = th; | ||
1018 | th->get_message = &produce_send; | ||
1019 | th->get_message_cls = th; | ||
1020 | th->notify = notify; | ||
1021 | th->notify_cls = notify_cls; | ||
1022 | th->peer = *target; | ||
1023 | th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay); | ||
1024 | th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched, | ||
1025 | GNUNET_NO, | ||
1026 | GNUNET_SCHEDULER_PRIORITY_KEEP, | ||
1027 | GNUNET_SCHEDULER_NO_PREREQUISITE_TASK, | ||
1028 | maxdelay, | ||
1029 | &timeout_request, th); | ||
1030 | th->priority = priority; | ||
1031 | th->msize = sizeof (struct SendMessage) + notify_size; | ||
1032 | /* was the request queue previously empty? */ | ||
1033 | if (handle->pending_head == th) | ||
1034 | trigger_next_request (handle); | ||
1035 | return NULL; | ||
1036 | } | ||
1037 | |||
1038 | |||
1039 | /** | ||
1040 | * Cancel the specified transmission-ready notification. | ||
1041 | * | ||
1042 | * @param h handle that was returned by "notify_transmit_ready". | ||
1043 | */ | ||
1044 | void | ||
1045 | GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle | ||
1046 | *h) | ||
1047 | { | ||
1048 | struct GNUNET_CORE_Handle *handle = h->ch; | ||
1049 | |||
1050 | if (handle->submitted == h) | ||
1051 | { | ||
1052 | handle->submitted = NULL; | ||
1053 | } | ||
1054 | else | ||
1055 | { | ||
1056 | if (h->prev == NULL) | ||
1057 | handle->pending_head = h->next; | ||
1058 | else | ||
1059 | h->prev->next = h->next; | ||
1060 | if (h->next == NULL) | ||
1061 | handle->pending_tail = h->prev; | ||
1062 | else | ||
1063 | h->next->prev = h->prev; | ||
1064 | } | ||
1065 | if (h->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) | ||
1066 | GNUNET_SCHEDULER_cancel (handle->sched, h->timeout_task); | ||
1067 | GNUNET_free (h); | ||
1068 | } | ||
1069 | |||
1070 | |||
1071 | /* end of core_api.c */ | ||