summaryrefslogtreecommitdiff
path: root/src/transport/transport_api_address_to_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_address_to_string.c')
-rw-r--r--src/transport/transport_api_address_to_string.c222
1 files changed, 110 insertions, 112 deletions
diff --git a/src/transport/transport_api_address_to_string.c b/src/transport/transport_api_address_to_string.c
index 006e81f29..d5e383914 100644
--- a/src/transport/transport_api_address_to_string.c
+++ b/src/transport/transport_api_address_to_string.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 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 transport/transport_api_address_to_string.c 21 * @file transport/transport_api_address_to_string.c
22 * @author Christian Grothoff 22 * @author Christian Grothoff
@@ -33,8 +33,7 @@
33/** 33/**
34 * Context for the address lookup. 34 * Context for the address lookup.
35 */ 35 */
36struct GNUNET_TRANSPORT_AddressToStringContext 36struct GNUNET_TRANSPORT_AddressToStringContext {
37{
38 /** 37 /**
39 * Function to call with the human-readable address. 38 * Function to call with the human-readable address.
40 */ 39 */
@@ -49,7 +48,6 @@ struct GNUNET_TRANSPORT_AddressToStringContext
49 * Connection to the service. 48 * Connection to the service.
50 */ 49 */
51 struct GNUNET_MQ_Handle *mq; 50 struct GNUNET_MQ_Handle *mq;
52
53}; 51};
54 52
55 53
@@ -61,35 +59,35 @@ struct GNUNET_TRANSPORT_AddressToStringContext
61 * @return #GNUNET_OK if message is well-formed 59 * @return #GNUNET_OK if message is well-formed
62 */ 60 */
63static int 61static int
64check_reply (void *cls, 62check_reply(void *cls,
65 const struct AddressToStringResultMessage *atsm) 63 const struct AddressToStringResultMessage *atsm)
66{ 64{
67 uint16_t size = ntohs (atsm->header.size) - sizeof (*atsm); 65 uint16_t size = ntohs(atsm->header.size) - sizeof(*atsm);
68 const char *address; 66 const char *address;
69 int result; 67 int result;
70 uint32_t addr_len; 68 uint32_t addr_len;
71 69
72 result = (int) ntohl (atsm->res); 70 result = (int)ntohl(atsm->res);
73 addr_len = ntohl (atsm->addr_len); 71 addr_len = ntohl(atsm->addr_len);
74 if (GNUNET_SYSERR == result) 72 if (GNUNET_SYSERR == result)
75 return GNUNET_OK; 73 return GNUNET_OK;
76 if (0 == size) 74 if (0 == size)
77 {
78 if (GNUNET_OK != result)
79 { 75 {
80 GNUNET_break (0); 76 if (GNUNET_OK != result)
77 {
78 GNUNET_break(0);
79 return GNUNET_SYSERR;
80 }
81 return GNUNET_OK;
82 }
83 address = (const char *)&atsm[1];
84 if ((addr_len > size) ||
85 (address[addr_len - 1] != '\0'))
86 {
87 /* invalid reply */
88 GNUNET_break(0);
81 return GNUNET_SYSERR; 89 return GNUNET_SYSERR;
82 } 90 }
83 return GNUNET_OK;
84 }
85 address = (const char *) &atsm[1];
86 if ( (addr_len > size) ||
87 (address[addr_len -1] != '\0') )
88 {
89 /* invalid reply */
90 GNUNET_break (0);
91 return GNUNET_SYSERR;
92 }
93 return GNUNET_OK; 91 return GNUNET_OK;
94} 92}
95 93
@@ -101,40 +99,40 @@ check_reply (void *cls,
101 * @param msg message with the human-readable address 99 * @param msg message with the human-readable address
102 */ 100 */
103static void 101static void
104handle_reply (void *cls, 102handle_reply(void *cls,
105 const struct AddressToStringResultMessage *atsm) 103 const struct AddressToStringResultMessage *atsm)
106{ 104{
107 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; 105 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
108 uint16_t size = ntohs (atsm->header.size) - sizeof (*atsm); 106 uint16_t size = ntohs(atsm->header.size) - sizeof(*atsm);
109 const char *address; 107 const char *address;
110 int result; 108 int result;
111 109
112 result = (int) ntohl (atsm->res); 110 result = (int)ntohl(atsm->res);
113 if (GNUNET_SYSERR == result) 111 if (GNUNET_SYSERR == result)
114 { 112 {
115 /* expect more replies; as this is not the last 113 /* expect more replies; as this is not the last
116 call, we must pass the empty string for the address */ 114 call, we must pass the empty string for the address */
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
118 "Address resolution failed\n"); 116 "Address resolution failed\n");
119 alucb->cb (alucb->cb_cls, 117 alucb->cb(alucb->cb_cls,
120 "", 118 "",
121 GNUNET_NO); 119 GNUNET_NO);
122 return; 120 return;
123 } 121 }
124 if (0 == size) 122 if (0 == size)
125 { 123 {
126 /* we are done (successfully, without communication errors) */ 124 /* we are done (successfully, without communication errors) */
127 alucb->cb (alucb->cb_cls, 125 alucb->cb(alucb->cb_cls,
128 NULL, 126 NULL,
129 GNUNET_OK); 127 GNUNET_OK);
130 GNUNET_TRANSPORT_address_to_string_cancel (alucb); 128 GNUNET_TRANSPORT_address_to_string_cancel(alucb);
131 return; 129 return;
132 } 130 }
133 address = (const char *) &atsm[1]; 131 address = (const char *)&atsm[1];
134 /* return normal reply to caller, also expect more replies */ 132 /* return normal reply to caller, also expect more replies */
135 alucb->cb (alucb->cb_cls, 133 alucb->cb(alucb->cb_cls,
136 address, 134 address,
137 GNUNET_OK); 135 GNUNET_OK);
138} 136}
139 137
140 138
@@ -148,17 +146,17 @@ handle_reply (void *cls,
148 * @param error error code 146 * @param error error code
149 */ 147 */
150static void 148static void
151mq_error_handler (void *cls, 149mq_error_handler(void *cls,
152 enum GNUNET_MQ_Error error) 150 enum GNUNET_MQ_Error error)
153{ 151{
154 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; 152 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
155 153
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
157 "Disconnected from transport, address resolution failed\n"); 155 "Disconnected from transport, address resolution failed\n");
158 alucb->cb (alucb->cb_cls, 156 alucb->cb(alucb->cb_cls,
159 NULL, 157 NULL,
160 GNUNET_SYSERR); 158 GNUNET_SYSERR);
161 GNUNET_TRANSPORT_address_to_string_cancel (alucb); 159 GNUNET_TRANSPORT_address_to_string_cancel(alucb);
162} 160}
163 161
164 162
@@ -175,21 +173,21 @@ mq_error_handler (void *cls,
175 * @return handle to cancel the operation, NULL on error 173 * @return handle to cancel the operation, NULL on error
176 */ 174 */
177struct GNUNET_TRANSPORT_AddressToStringContext * 175struct GNUNET_TRANSPORT_AddressToStringContext *
178GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg, 176GNUNET_TRANSPORT_address_to_string(const struct GNUNET_CONFIGURATION_Handle *cfg,
179 const struct GNUNET_HELLO_Address *address, 177 const struct GNUNET_HELLO_Address *address,
180 int numeric, 178 int numeric,
181 struct GNUNET_TIME_Relative timeout, 179 struct GNUNET_TIME_Relative timeout,
182 GNUNET_TRANSPORT_AddressToStringCallback aluc, 180 GNUNET_TRANSPORT_AddressToStringCallback aluc,
183 void *aluc_cls) 181 void *aluc_cls)
184{ 182{
185 struct GNUNET_TRANSPORT_AddressToStringContext *alc 183 struct GNUNET_TRANSPORT_AddressToStringContext *alc
186 = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext); 184 = GNUNET_new(struct GNUNET_TRANSPORT_AddressToStringContext);
187 struct GNUNET_MQ_MessageHandler handlers[] = { 185 struct GNUNET_MQ_MessageHandler handlers[] = {
188 GNUNET_MQ_hd_var_size (reply, 186 GNUNET_MQ_hd_var_size(reply,
189 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY, 187 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY,
190 struct AddressToStringResultMessage, 188 struct AddressToStringResultMessage,
191 alc), 189 alc),
192 GNUNET_MQ_handler_end () 190 GNUNET_MQ_handler_end()
193 }; 191 };
194 size_t alen; 192 size_t alen;
195 size_t slen; 193 size_t slen;
@@ -198,49 +196,49 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cf
198 char *addrbuf; 196 char *addrbuf;
199 197
200 alen = address->address_length; 198 alen = address->address_length;
201 slen = strlen (address->transport_name) + 1; 199 slen = strlen(address->transport_name) + 1;
202 if ( (alen + slen >= GNUNET_MAX_MESSAGE_SIZE 200 if ((alen + slen >= GNUNET_MAX_MESSAGE_SIZE
203 - sizeof (struct AddressLookupMessage)) || 201 - sizeof(struct AddressLookupMessage)) ||
204 (alen >= GNUNET_MAX_MESSAGE_SIZE) || 202 (alen >= GNUNET_MAX_MESSAGE_SIZE) ||
205 (slen >= GNUNET_MAX_MESSAGE_SIZE) ) 203 (slen >= GNUNET_MAX_MESSAGE_SIZE))
206 { 204 {
207 GNUNET_break (0); 205 GNUNET_break(0);
208 GNUNET_free (alc); 206 GNUNET_free(alc);
209 return NULL; 207 return NULL;
210 } 208 }
211 alc->cb = aluc; 209 alc->cb = aluc;
212 alc->cb_cls = aluc_cls; 210 alc->cb_cls = aluc_cls;
213 alc->mq = GNUNET_CLIENT_connect (cfg, 211 alc->mq = GNUNET_CLIENT_connect(cfg,
214 "transport", 212 "transport",
215 handlers, 213 handlers,
216 &mq_error_handler, 214 &mq_error_handler,
217 alc); 215 alc);
218 if (NULL == alc->mq) 216 if (NULL == alc->mq)
219 { 217 {
220 GNUNET_break (0); 218 GNUNET_break(0);
221 GNUNET_free (alc); 219 GNUNET_free(alc);
222 return NULL; 220 return NULL;
223 } 221 }
224 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 222 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
225 "Client tries to resolve for peer `%s' address plugin %s len %u\n", 223 "Client tries to resolve for peer `%s' address plugin %s len %u\n",
226 GNUNET_i2s (&address->peer), 224 GNUNET_i2s(&address->peer),
227 address->transport_name, 225 address->transport_name,
228 (unsigned int) address->address_length); 226 (unsigned int)address->address_length);
229 env = GNUNET_MQ_msg_extra (msg, 227 env = GNUNET_MQ_msg_extra(msg,
230 alen + slen, 228 alen + slen,
231 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING); 229 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
232 msg->numeric_only = htons ((int16_t) numeric); 230 msg->numeric_only = htons((int16_t)numeric);
233 msg->addrlen = htons ((uint16_t) alen); 231 msg->addrlen = htons((uint16_t)alen);
234 msg->timeout = GNUNET_TIME_relative_hton (timeout); 232 msg->timeout = GNUNET_TIME_relative_hton(timeout);
235 addrbuf = (char *) &msg[1]; 233 addrbuf = (char *)&msg[1];
236 GNUNET_memcpy (addrbuf, 234 GNUNET_memcpy(addrbuf,
237 address->address, 235 address->address,
238 alen); 236 alen);
239 GNUNET_memcpy (&addrbuf[alen], 237 GNUNET_memcpy(&addrbuf[alen],
240 address->transport_name, 238 address->transport_name,
241 slen); 239 slen);
242 GNUNET_MQ_send (alc->mq, 240 GNUNET_MQ_send(alc->mq,
243 env); 241 env);
244 return alc; 242 return alc;
245} 243}
246 244
@@ -251,10 +249,10 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cf
251 * @param alc the context handle 249 * @param alc the context handle
252 */ 250 */
253void 251void
254GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *alc) 252GNUNET_TRANSPORT_address_to_string_cancel(struct GNUNET_TRANSPORT_AddressToStringContext *alc)
255{ 253{
256 GNUNET_MQ_destroy (alc->mq); 254 GNUNET_MQ_destroy(alc->mq);
257 GNUNET_free (alc); 255 GNUNET_free(alc);
258} 256}
259 257
260 258