diff options
-rw-r--r-- | src/transport/transport_api_get_hello.c | 146 | ||||
-rw-r--r-- | src/transport/transport_api_offer_hello.c | 149 |
2 files changed, 295 insertions, 0 deletions
diff --git a/src/transport/transport_api_get_hello.c b/src/transport/transport_api_get_hello.c new file mode 100644 index 000000000..8087159c6 --- /dev/null +++ b/src/transport/transport_api_get_hello.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013, 2016 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/transport_api.c | ||
23 | * @brief library to obtain our HELLO from our transport service | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_arm_service.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "gnunet_transport_service.h" | ||
33 | #include "transport.h" | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Linked list of functions to call whenever our HELLO is updated. | ||
38 | */ | ||
39 | struct GNUNET_TRANSPORT_GetHelloHandle | ||
40 | { | ||
41 | |||
42 | /** | ||
43 | * This is a doubly linked list. | ||
44 | */ | ||
45 | struct GNUNET_TRANSPORT_GetHelloHandle *next; | ||
46 | |||
47 | /** | ||
48 | * This is a doubly linked list. | ||
49 | */ | ||
50 | struct GNUNET_TRANSPORT_GetHelloHandle *prev; | ||
51 | |||
52 | /** | ||
53 | * Transport handle. | ||
54 | */ | ||
55 | struct GNUNET_TRANSPORT_Handle *handle; | ||
56 | |||
57 | /** | ||
58 | * Callback to call once we got our HELLO. | ||
59 | */ | ||
60 | GNUNET_TRANSPORT_HelloUpdateCallback rec; | ||
61 | |||
62 | /** | ||
63 | * Task for calling the HelloUpdateCallback when we already have a HELLO | ||
64 | */ | ||
65 | struct GNUNET_SCHEDULER_Task *notify_task; | ||
66 | |||
67 | /** | ||
68 | * Closure for @e rec. | ||
69 | */ | ||
70 | void *rec_cls; | ||
71 | |||
72 | }; | ||
73 | |||
74 | |||
75 | |||
76 | /** | ||
77 | * Task to call the HelloUpdateCallback of the GetHelloHandle | ||
78 | * | ||
79 | * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle` | ||
80 | */ | ||
81 | static void | ||
82 | call_hello_update_cb_async (void *cls) | ||
83 | { | ||
84 | struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls; | ||
85 | |||
86 | GNUNET_assert (NULL != ghh->handle->my_hello); | ||
87 | GNUNET_assert (NULL != ghh->notify_task); | ||
88 | ghh->notify_task = NULL; | ||
89 | ghh->rec (ghh->rec_cls, | ||
90 | ghh->handle->my_hello); | ||
91 | } | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Obtain the HELLO message for this peer. The callback given in this function | ||
96 | * is never called synchronously. | ||
97 | * | ||
98 | * @param handle connection to transport service | ||
99 | * @param rec function to call with the HELLO, sender will be our peer | ||
100 | * identity; message and sender will be NULL on timeout | ||
101 | * (handshake with transport service pending/failed). | ||
102 | * cost estimate will be 0. | ||
103 | * @param rec_cls closure for @a rec | ||
104 | * @return handle to cancel the operation | ||
105 | */ | ||
106 | struct GNUNET_TRANSPORT_GetHelloHandle * | ||
107 | GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle, | ||
108 | GNUNET_TRANSPORT_HelloUpdateCallback rec, | ||
109 | void *rec_cls) | ||
110 | { | ||
111 | struct GNUNET_TRANSPORT_GetHelloHandle *hwl; | ||
112 | |||
113 | hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle); | ||
114 | hwl->rec = rec; | ||
115 | hwl->rec_cls = rec_cls; | ||
116 | hwl->handle = handle; | ||
117 | GNUNET_CONTAINER_DLL_insert (handle->hwl_head, | ||
118 | handle->hwl_tail, | ||
119 | hwl); | ||
120 | if (NULL != handle->my_hello) | ||
121 | hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async, | ||
122 | hwl); | ||
123 | return hwl; | ||
124 | } | ||
125 | |||
126 | |||
127 | /** | ||
128 | * Stop receiving updates about changes to our HELLO message. | ||
129 | * | ||
130 | * @param ghh handle to cancel | ||
131 | */ | ||
132 | void | ||
133 | GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh) | ||
134 | { | ||
135 | struct GNUNET_TRANSPORT_Handle *handle = ghh->handle; | ||
136 | |||
137 | if (NULL != ghh->notify_task) | ||
138 | GNUNET_SCHEDULER_cancel (ghh->notify_task); | ||
139 | GNUNET_CONTAINER_DLL_remove (handle->hwl_head, | ||
140 | handle->hwl_tail, | ||
141 | ghh); | ||
142 | GNUNET_free (ghh); | ||
143 | } | ||
144 | |||
145 | |||
146 | /* end of transport_api_hello.c */ | ||
diff --git a/src/transport/transport_api_offer_hello.c b/src/transport/transport_api_offer_hello.c new file mode 100644 index 000000000..0abce2d62 --- /dev/null +++ b/src/transport/transport_api_offer_hello.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013, 2016 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/transport_api_offer_hello.c | ||
23 | * @brief library to offer HELLOs to transport service | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | /** | ||
28 | * Entry in linked list for all offer-HELLO requests. | ||
29 | */ | ||
30 | struct GNUNET_TRANSPORT_OfferHelloHandle | ||
31 | { | ||
32 | /** | ||
33 | * For the DLL. | ||
34 | */ | ||
35 | struct GNUNET_TRANSPORT_OfferHelloHandle *prev; | ||
36 | |||
37 | /** | ||
38 | * For the DLL. | ||
39 | */ | ||
40 | struct GNUNET_TRANSPORT_OfferHelloHandle *next; | ||
41 | |||
42 | /** | ||
43 | * Transport service handle we use for transmission. | ||
44 | */ | ||
45 | struct GNUNET_TRANSPORT_Handle *th; | ||
46 | |||
47 | /** | ||
48 | * Transmission handle for this request. | ||
49 | */ | ||
50 | struct GNUNET_TRANSPORT_TransmitHandle *tth; | ||
51 | |||
52 | /** | ||
53 | * Function to call once we are done. | ||
54 | */ | ||
55 | GNUNET_SCHEDULER_TaskCallback cont; | ||
56 | |||
57 | /** | ||
58 | * Closure for @e cont | ||
59 | */ | ||
60 | void *cls; | ||
61 | |||
62 | /** | ||
63 | * The HELLO message to be transmitted. | ||
64 | */ | ||
65 | struct GNUNET_MessageHeader *msg; | ||
66 | }; | ||
67 | |||
68 | |||
69 | |||
70 | /** | ||
71 | * Offer the transport service the HELLO of another peer. Note that | ||
72 | * the transport service may just ignore this message if the HELLO is | ||
73 | * malformed or useless due to our local configuration. | ||
74 | * | ||
75 | * @param handle connection to transport service | ||
76 | * @param hello the hello message | ||
77 | * @param cont continuation to call when HELLO has been sent, | ||
78 | * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail | ||
79 | * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success | ||
80 | * @param cont_cls closure for @a cont | ||
81 | * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure, | ||
82 | * in case of failure @a cont will not be called | ||
83 | * | ||
84 | */ | ||
85 | struct GNUNET_TRANSPORT_OfferHelloHandle * | ||
86 | GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle, | ||
87 | const struct GNUNET_MessageHeader *hello, | ||
88 | GNUNET_SCHEDULER_TaskCallback cont, | ||
89 | void *cont_cls) | ||
90 | { | ||
91 | struct GNUNET_TRANSPORT_OfferHelloHandle *ohh; | ||
92 | struct GNUNET_MessageHeader *msg; | ||
93 | struct GNUNET_PeerIdentity peer; | ||
94 | uint16_t size; | ||
95 | |||
96 | if (NULL == handle->mq) | ||
97 | return NULL; | ||
98 | GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO); | ||
99 | size = ntohs (hello->size); | ||
100 | GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader)); | ||
101 | if (GNUNET_OK != | ||
102 | GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, | ||
103 | &peer)) | ||
104 | { | ||
105 | GNUNET_break (0); | ||
106 | return NULL; | ||
107 | } | ||
108 | |||
109 | msg = GNUNET_malloc (size); | ||
110 | memcpy (msg, hello, size); | ||
111 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
112 | "Offering HELLO message of `%s' to transport for validation.\n", | ||
113 | GNUNET_i2s (&peer)); | ||
114 | ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle); | ||
115 | ohh->th = handle; | ||
116 | ohh->cont = cont; | ||
117 | ohh->cls = cont_cls; | ||
118 | ohh->msg = msg; | ||
119 | ohh->tth = schedule_control_transmit (handle, | ||
120 | size, | ||
121 | &send_hello, | ||
122 | ohh); | ||
123 | GNUNET_CONTAINER_DLL_insert (handle->oh_head, | ||
124 | handle->oh_tail, | ||
125 | ohh); | ||
126 | return ohh; | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * Cancel the request to transport to offer the HELLO message | ||
132 | * | ||
133 | * @param ohh the handle for the operation to cancel | ||
134 | */ | ||
135 | void | ||
136 | GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh) | ||
137 | { | ||
138 | struct GNUNET_TRANSPORT_Handle *th = ohh->th; | ||
139 | |||
140 | cancel_control_transmit (ohh->th, ohh->tth); | ||
141 | GNUNET_CONTAINER_DLL_remove (th->oh_head, | ||
142 | th->oh_tail, | ||
143 | ohh); | ||
144 | GNUNET_free (ohh->msg); | ||
145 | GNUNET_free (ohh); | ||
146 | } | ||
147 | |||
148 | |||
149 | /* end of transport_api_offer_hello.c */ | ||