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/>.
19 * @file transport/transport_api_address_to_string.c
20 * @author Christian Grothoff
21 * @brief enable clients to convert addresses to human readable strings
24 #include "gnunet_util_lib.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_transport_service.h"
29 #include "transport.h"
32 * Context for the address lookup.
34 struct GNUNET_TRANSPORT_AddressToStringContext
37 * Function to call with the human-readable address.
39 GNUNET_TRANSPORT_AddressToStringCallback cb;
47 * Connection to the service.
49 struct GNUNET_MQ_Handle *mq;
55 * Function called with responses from the service.
57 * @param cls our `struct GNUNET_TRANSPORT_AddressToStringContext *`
58 * @param msg message with the human-readable address
59 * @return #GNUNET_OK if message is well-formed
62 check_reply (void *cls,
63 const struct AddressToStringResultMessage *atsm)
65 uint16_t size = ntohs (atsm->header.size) - sizeof (*atsm);
70 result = (int) ntohl (atsm->res);
71 addr_len = ntohl (atsm->addr_len);
72 if (GNUNET_SYSERR == result)
76 if (GNUNET_OK != result)
83 address = (const char *) &atsm[1];
84 if ( (addr_len > size) ||
85 (address[addr_len -1] != '\0') )
96 * Function called with responses from the service.
98 * @param cls our `struct GNUNET_TRANSPORT_AddressToStringContext *`
99 * @param msg message with the human-readable address
102 handle_reply (void *cls,
103 const struct AddressToStringResultMessage *atsm)
105 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
106 uint16_t size = ntohs (atsm->header.size) - sizeof (*atsm);
110 result = (int) ntohl (atsm->res);
111 if (GNUNET_SYSERR == result)
113 /* expect more replies; as this is not the last
114 call, we must pass the empty string for the address */
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Address resolution failed\n");
117 alucb->cb (alucb->cb_cls,
124 /* we are done (successfully, without communication errors) */
125 alucb->cb (alucb->cb_cls,
128 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
131 address = (const char *) &atsm[1];
132 /* return normal reply to caller, also expect more replies */
133 alucb->cb (alucb->cb_cls,
140 * Generic error handler, called with the appropriate
141 * error code and the same closure specified at the creation of
143 * Not every message queue implementation supports an error handler.
145 * @param cls the `struct GNUNET_TRANSPORT_AddressToStringContext *`
146 * @param error error code
149 mq_error_handler (void *cls,
150 enum GNUNET_MQ_Error error)
152 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Disconnected from transport, address resolution failed\n");
156 alucb->cb (alucb->cb_cls,
159 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
164 * Convert a binary address into a human readable address.
166 * @param cfg configuration to use
167 * @param address address to convert (binary format)
168 * @param numeric should (IP) addresses be displayed in numeric form
169 * (otherwise do reverse DNS lookup)
170 * @param timeout how long is the lookup allowed to take at most
171 * @param aluc function to call with the results
172 * @param aluc_cls closure for @a aluc
173 * @return handle to cancel the operation, NULL on error
175 struct GNUNET_TRANSPORT_AddressToStringContext *
176 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
177 const struct GNUNET_HELLO_Address *address,
179 struct GNUNET_TIME_Relative timeout,
180 GNUNET_TRANSPORT_AddressToStringCallback aluc,
183 struct GNUNET_TRANSPORT_AddressToStringContext *alc
184 = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
185 struct GNUNET_MQ_MessageHandler handlers[] = {
186 GNUNET_MQ_hd_var_size (reply,
187 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY,
188 struct AddressToStringResultMessage,
190 GNUNET_MQ_handler_end ()
194 struct AddressLookupMessage *msg;
195 struct GNUNET_MQ_Envelope *env;
198 alen = address->address_length;
199 slen = strlen (address->transport_name) + 1;
200 if ( (alen + slen >= GNUNET_MAX_MESSAGE_SIZE
201 - sizeof (struct AddressLookupMessage)) ||
202 (alen >= GNUNET_MAX_MESSAGE_SIZE) ||
203 (slen >= GNUNET_MAX_MESSAGE_SIZE) )
210 alc->cb_cls = aluc_cls;
211 alc->mq = GNUNET_CLIENT_connect (cfg,
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "Client tries to resolve for peer `%s' address plugin %s len %u\n",
224 GNUNET_i2s (&address->peer),
225 address->transport_name,
226 (unsigned int) address->address_length);
227 env = GNUNET_MQ_msg_extra (msg,
229 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
230 msg->numeric_only = htons ((int16_t) numeric);
231 msg->addrlen = htons ((uint16_t) alen);
232 msg->timeout = GNUNET_TIME_relative_hton (timeout);
233 addrbuf = (char *) &msg[1];
234 GNUNET_memcpy (addrbuf,
237 GNUNET_memcpy (&addrbuf[alen],
238 address->transport_name,
240 GNUNET_MQ_send (alc->mq,
247 * Cancel request for address conversion.
249 * @param alc the context handle
252 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *alc)
254 GNUNET_MQ_destroy (alc->mq);
259 /* end of transport_api_address_to_string.c */