diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-01-26 20:49:59 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-01-26 20:49:59 +0100 |
commit | 7fc78674a6d3f2edb41e32cea59200908d0b344b (patch) | |
tree | b99608184fa08dcc6cd3e297369cb2761a03524f /src/cadet/cadet_api_get_peer.c | |
parent | a5a54c51011a9498e72b2a9341b35333c6beef1a (diff) | |
download | gnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.tar.gz gnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.zip |
starting with #5385 in earnest
Diffstat (limited to 'src/cadet/cadet_api_get_peer.c')
-rw-r--r-- | src/cadet/cadet_api_get_peer.c | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/src/cadet/cadet_api_get_peer.c b/src/cadet/cadet_api_get_peer.c new file mode 100644 index 000000000..b29b66e1c --- /dev/null +++ b/src/cadet/cadet_api_get_peer.c | |||
@@ -0,0 +1,219 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 GNUnet e.V. | ||
4 | |||
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 | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/cadet_api_get_peer.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | struct GNUNET_CADET_GetPeer | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Monitor callback | ||
43 | */ | ||
44 | GNUNET_CADET_PeerCB peer_cb; | ||
45 | |||
46 | /** | ||
47 | * Info callback closure for @c info_cb. | ||
48 | */ | ||
49 | void *peer_cb_cls; | ||
50 | |||
51 | |||
52 | }; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Send message of @a type to CADET service of @a h | ||
57 | * | ||
58 | * @param h handle to CADET service | ||
59 | * @param type message type of trivial information request to send | ||
60 | */ | ||
61 | static void | ||
62 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
63 | uint16_t type) | ||
64 | { | ||
65 | struct GNUNET_MessageHeader *msg; | ||
66 | struct GNUNET_MQ_Envelope *env; | ||
67 | |||
68 | env = GNUNET_MQ_msg (msg, | ||
69 | type); | ||
70 | GNUNET_MQ_send (h->mq, | ||
71 | env); | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Check that message received from CADET service is well-formed. | ||
77 | * | ||
78 | * @param cls the `struct GNUNET_CADET_Handle` | ||
79 | * @param message the message we got | ||
80 | * @return #GNUNET_OK if the message is well-formed, | ||
81 | * #GNUNET_SYSERR otherwise | ||
82 | */ | ||
83 | static int | ||
84 | check_get_peer (void *cls, | ||
85 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
86 | { | ||
87 | size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer); | ||
88 | size_t esize; | ||
89 | |||
90 | (void) cls; | ||
91 | esize = ntohs (message->header.size); | ||
92 | if (esize < msize) | ||
93 | { | ||
94 | GNUNET_break (0); | ||
95 | return GNUNET_SYSERR; | ||
96 | } | ||
97 | if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity))) | ||
98 | { | ||
99 | GNUNET_break (0); | ||
100 | return GNUNET_SYSERR; | ||
101 | } | ||
102 | return GNUNET_OK; | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Process a local peer info reply, pass info to the user. | ||
108 | * | ||
109 | * @param cls Closure (Cadet handle). | ||
110 | * @param message Message itself. | ||
111 | */ | ||
112 | static void | ||
113 | handle_get_peer (void *cls, | ||
114 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
115 | { | ||
116 | struct GNUNET_CADET_Handle *h = cls; | ||
117 | const struct GNUNET_PeerIdentity *paths_array; | ||
118 | unsigned int paths; | ||
119 | unsigned int path_length; | ||
120 | int neighbor; | ||
121 | unsigned int peers; | ||
122 | |||
123 | if (NULL == h->info_cb.peer_cb) | ||
124 | return; | ||
125 | |||
126 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
127 | "number of paths %u\n", | ||
128 | ntohs (message->paths)); | ||
129 | |||
130 | paths = ntohs (message->paths); | ||
131 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
132 | peers = (ntohs (message->header.size) - sizeof (*message)) | ||
133 | / sizeof (struct GNUNET_PeerIdentity); | ||
134 | path_length = 0; | ||
135 | neighbor = GNUNET_NO; | ||
136 | |||
137 | for (unsigned int i = 0; i < peers; i++) | ||
138 | { | ||
139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
140 | " %s\n", | ||
141 | GNUNET_i2s (&paths_array[i])); | ||
142 | path_length++; | ||
143 | if (0 == memcmp (&paths_array[i], &message->destination, | ||
144 | sizeof (struct GNUNET_PeerIdentity))) | ||
145 | { | ||
146 | if (1 == path_length) | ||
147 | neighbor = GNUNET_YES; | ||
148 | path_length = 0; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | /* Call Callback with tunnel info. */ | ||
153 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
154 | h->info_cb.peer_cb (h->info_cls, | ||
155 | &message->destination, | ||
156 | (int) ntohs (message->tunnel), | ||
157 | neighbor, | ||
158 | paths, | ||
159 | paths_array, | ||
160 | (int) ntohs (message->offset), | ||
161 | (int) ntohs (message->finished_with_paths)); | ||
162 | } | ||
163 | |||
164 | |||
165 | |||
166 | |||
167 | /** | ||
168 | * Request information about a peer known to the running cadet peer. | ||
169 | * The callback will be called for the tunnel once. | ||
170 | * Only one info request (of any kind) can be active at once. | ||
171 | * | ||
172 | * WARNING: unstable API, likely to change in the future! | ||
173 | * | ||
174 | * @param h Handle to the cadet peer. | ||
175 | * @param id Peer whose tunnel to examine. | ||
176 | * @param callback Function to call with the requested data. | ||
177 | * @param callback_cls Closure for @c callback. | ||
178 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
179 | */ | ||
180 | int | ||
181 | GNUNET_CADET_get_peer (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
182 | const struct GNUNET_PeerIdentity *id, | ||
183 | GNUNET_CADET_PeerCB callback, | ||
184 | void *callback_cls) | ||
185 | { | ||
186 | struct GNUNET_CADET_LocalInfo *msg; | ||
187 | struct GNUNET_MQ_Envelope *env; | ||
188 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
189 | |||
190 | GNUNET_MQ_hd_var_size (get_peers, | ||
191 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, | ||
192 | struct GNUNET_MessageHeader, | ||
193 | h), | ||
194 | GNUNET_MQ_hd_var_size (get_peer, | ||
195 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER, | ||
196 | struct GNUNET_CADET_LocalInfoPeer, | ||
197 | h), | ||
198 | GNUNET_MQ_handler_end () | ||
199 | |||
200 | |||
201 | if (NULL != h->info_cb.peer_cb) | ||
202 | { | ||
203 | GNUNET_break (0); | ||
204 | return GNUNET_SYSERR; | ||
205 | } | ||
206 | env = GNUNET_MQ_msg (msg, | ||
207 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER); | ||
208 | msg->peer = *id; | ||
209 | GNUNET_MQ_send (h->mq, | ||
210 | env); | ||
211 | h->info_cb.peer_cb = callback; | ||
212 | h->info_cls = callback_cls; | ||
213 | return GNUNET_OK; | ||
214 | } | ||
215 | |||
216 | |||
217 | |||
218 | |||
219 | |||