2 This file is part of GNUnet.
3 Copyright (C) 2009-2014, 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file transport/transport_api_address_to_string.c
22 * @author Christian Grothoff
23 * @brief enable clients to convert addresses to human readable strings
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_transport_service.h"
31 #include "transport.h"
34 * Context for the address lookup.
36 struct GNUNET_TRANSPORT_AddressToStringContext
39 * Function to call with the human-readable address.
41 GNUNET_TRANSPORT_AddressToStringCallback cb;
49 * Connection to the service.
51 struct GNUNET_MQ_Handle *mq;
56 * Function called with responses from the service.
58 * @param cls our `struct GNUNET_TRANSPORT_AddressToStringContext *`
59 * @param msg message with the human-readable address
60 * @return #GNUNET_OK if message is well-formed
63 check_reply (void *cls,
64 const struct AddressToStringResultMessage *atsm)
66 uint16_t size = ntohs (atsm->header.size) - sizeof(*atsm);
71 result = (int) ntohl (atsm->res);
72 addr_len = ntohl (atsm->addr_len);
73 if (GNUNET_SYSERR == result)
77 if (GNUNET_OK != result)
84 address = (const char *) &atsm[1];
85 if ((addr_len > size) ||
86 (address[addr_len - 1] != '\0'))
97 * Function called with responses from the service.
99 * @param cls our `struct GNUNET_TRANSPORT_AddressToStringContext *`
100 * @param msg message with the human-readable address
103 handle_reply (void *cls,
104 const struct AddressToStringResultMessage *atsm)
106 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
107 uint16_t size = ntohs (atsm->header.size) - sizeof(*atsm);
111 result = (int) ntohl (atsm->res);
112 if (GNUNET_SYSERR == result)
114 /* expect more replies; as this is not the last
115 call, we must pass the empty string for the address */
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Address resolution failed\n");
118 alucb->cb (alucb->cb_cls,
125 /* we are done (successfully, without communication errors) */
126 alucb->cb (alucb->cb_cls,
129 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
132 address = (const char *) &atsm[1];
133 /* return normal reply to caller, also expect more replies */
134 alucb->cb (alucb->cb_cls,
141 * Generic error handler, called with the appropriate
142 * error code and the same closure specified at the creation of
144 * Not every message queue implementation supports an error handler.
146 * @param cls the `struct GNUNET_TRANSPORT_AddressToStringContext *`
147 * @param error error code
150 mq_error_handler (void *cls,
151 enum GNUNET_MQ_Error error)
153 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Disconnected from transport, address resolution failed\n");
157 alucb->cb (alucb->cb_cls,
160 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
165 * Convert a binary address into a human readable address.
167 * @param cfg configuration to use
168 * @param address address to convert (binary format)
169 * @param numeric should (IP) addresses be displayed in numeric form
170 * (otherwise do reverse DNS lookup)
171 * @param timeout how long is the lookup allowed to take at most
172 * @param aluc function to call with the results
173 * @param aluc_cls closure for @a aluc
174 * @return handle to cancel the operation, NULL on error
176 struct GNUNET_TRANSPORT_AddressToStringContext *
177 GNUNET_TRANSPORT_address_to_string (const struct
178 GNUNET_CONFIGURATION_Handle *cfg,
179 const struct GNUNET_HELLO_Address *address,
181 struct GNUNET_TIME_Relative timeout,
182 GNUNET_TRANSPORT_AddressToStringCallback
186 struct GNUNET_TRANSPORT_AddressToStringContext *alc
187 = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
188 struct GNUNET_MQ_MessageHandler handlers[] = {
189 GNUNET_MQ_hd_var_size (reply,
190 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY,
191 struct AddressToStringResultMessage,
193 GNUNET_MQ_handler_end ()
197 struct AddressLookupMessage *msg;
198 struct GNUNET_MQ_Envelope *env;
201 alen = address->address_length;
202 slen = strlen (address->transport_name) + 1;
203 if ((alen + slen >= GNUNET_MAX_MESSAGE_SIZE
204 - sizeof(struct AddressLookupMessage)) ||
205 (alen >= GNUNET_MAX_MESSAGE_SIZE) ||
206 (slen >= GNUNET_MAX_MESSAGE_SIZE))
213 alc->cb_cls = aluc_cls;
214 alc->mq = GNUNET_CLIENT_connect (cfg,
225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
226 "Client tries to resolve for peer `%s' address plugin %s len %u\n",
227 GNUNET_i2s (&address->peer),
228 address->transport_name,
229 (unsigned int) address->address_length);
230 env = GNUNET_MQ_msg_extra (msg,
232 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
233 msg->numeric_only = htons ((int16_t) numeric);
234 msg->addrlen = htons ((uint16_t) alen);
235 msg->timeout = GNUNET_TIME_relative_hton (timeout);
236 addrbuf = (char *) &msg[1];
237 GNUNET_memcpy (addrbuf,
240 GNUNET_memcpy (&addrbuf[alen],
241 address->transport_name,
243 GNUNET_MQ_send (alc->mq,
250 * Cancel request for address conversion.
252 * @param alc the context handle
255 GNUNET_TRANSPORT_address_to_string_cancel (struct
256 GNUNET_TRANSPORT_AddressToStringContext
259 GNUNET_MQ_destroy (alc->mq);
264 /* end of transport_api_address_to_string.c */