2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
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.
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.
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.
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_CLIENT_Connection *client;
57 * Function called with responses from the service.
59 * @param cls our `struct GNUNET_TRANSPORT_AddressToStringContext *`
60 * @param msg NULL on timeout or error, otherwise presumably a
61 * message with the human-readable address
64 address_response_processor (void *cls,
65 const struct GNUNET_MessageHeader *msg)
67 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
68 const struct AddressToStringResultMessage *atsm;
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77 "Disconnected from transport, address resolution failed\n");
78 alucb->cb (alucb->cb_cls,
81 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
84 GNUNET_break (ntohs (msg->type) ==
85 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
87 size = ntohs (msg->size);
88 if (size < sizeof (struct AddressToStringResultMessage))
91 alucb->cb (alucb->cb_cls,
94 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
97 atsm = (const struct AddressToStringResultMessage *) msg;
98 result = (int) ntohl (atsm->res);
99 addr_len = ntohl (atsm->addr_len);
100 if (GNUNET_SYSERR == result)
102 /* expect more replies; as this is not the last
103 call, we must pass the empty string for the address */
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Address resolution failed\n");
106 alucb->cb (alucb->cb_cls,
109 GNUNET_CLIENT_receive (alucb->client,
110 &address_response_processor,
112 GNUNET_TIME_UNIT_FOREVER_REL);
115 if (size == (sizeof (struct AddressToStringResultMessage)))
117 if (GNUNET_OK != result)
120 alucb->cb (alucb->cb_cls,
123 GNUNET_CLIENT_disconnect (alucb->client);
127 /* we are done (successfully, without communication errors) */
128 alucb->cb (alucb->cb_cls,
131 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
134 address = (const char *) &atsm[1];
135 if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) ||
136 (address[addr_len -1] != '\0') )
140 alucb->cb (alucb->cb_cls,
143 GNUNET_TRANSPORT_address_to_string_cancel (alucb);
146 /* expect more replies */
147 GNUNET_CLIENT_receive (alucb->client,
148 &address_response_processor,
150 GNUNET_TIME_UNIT_FOREVER_REL);
151 /* return normal reply to caller */
152 alucb->cb (alucb->cb_cls,
159 * Convert a binary address into a human readable address.
161 * @param cfg configuration to use
162 * @param address address to convert (binary format)
163 * @param numeric should (IP) addresses be displayed in numeric form
164 * (otherwise do reverse DNS lookup)
165 * @param timeout how long is the lookup allowed to take at most
166 * @param aluc function to call with the results
167 * @param aluc_cls closure for @a aluc
168 * @return handle to cancel the operation, NULL on error
170 struct GNUNET_TRANSPORT_AddressToStringContext *
171 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
172 const struct GNUNET_HELLO_Address *address,
174 struct GNUNET_TIME_Relative timeout,
175 GNUNET_TRANSPORT_AddressToStringCallback aluc,
181 struct AddressLookupMessage *msg;
182 struct GNUNET_TRANSPORT_AddressToStringContext *alc;
183 struct GNUNET_CLIENT_Connection *client;
186 GNUNET_assert (NULL != address);
187 alen = address->address_length;
188 slen = strlen (address->transport_name) + 1;
189 len = sizeof (struct AddressLookupMessage) + alen + slen;
190 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
195 client = GNUNET_CLIENT_connect ("transport", cfg);
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 "Client %p tries to resolve for peer `%s' address plugin %s len %u\n",
204 GNUNET_i2s (&address->peer),
205 address->transport_name,
206 address->address_length);
208 msg = GNUNET_malloc (len);
209 msg->header.size = htons (len);
210 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
211 msg->numeric_only = htons ((int16_t) numeric);
212 msg->addrlen = htons ((uint16_t) alen);
213 msg->timeout = GNUNET_TIME_relative_hton (timeout);
214 addrbuf = (char *) &msg[1];
218 memcpy (&addrbuf[alen],
219 address->transport_name,
221 alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
223 alc->cb_cls = aluc_cls;
224 alc->client = client;
225 GNUNET_assert (GNUNET_OK ==
226 GNUNET_CLIENT_transmit_and_get_response (client,
228 GNUNET_TIME_UNIT_FOREVER_REL,
230 &address_response_processor,
238 * Cancel request for address conversion.
240 * @param pic the context handle
243 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic)
245 GNUNET_CLIENT_disconnect (pic->client);
251 /* end of transport_api_address_to_string.c */