2 This file is part of GNUnet.
3 (C) 2009, 2010 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 #include "gnunet_util_lib.h"
22 #include "gnunet_arm_service.h"
23 #include "gnunet_hello_lib.h"
24 #include "gnunet_protocols.h"
25 #include "gnunet_transport_service.h"
26 #include "transport.h"
29 * Context for the address lookup.
31 struct GNUNET_TRANSPORT_AddressToStringContext
34 * Function to call with the human-readable address.
36 GNUNET_TRANSPORT_AddressToStringCallback cb;
44 * Connection to the service.
46 struct GNUNET_CLIENT_Connection *client;
49 * When should this operation time out?
51 struct GNUNET_TIME_Absolute timeout;
56 * Function called with responses from the service.
58 * @param cls our 'struct GNUNET_TRANSPORT_AddressToStringContext*'
59 * @param msg NULL on timeout or error, otherwise presumably a
60 * message with the human-readable address
63 address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg)
65 struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
66 struct AddressToStringResultMessage *atsm;
75 alucb->cb (alucb->cb_cls, NULL, GNUNET_OK);
76 GNUNET_CLIENT_disconnect (alucb->client);
80 GNUNET_break (ntohs (msg->type) ==
81 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
83 size = ntohs (msg->size);
84 if (size < sizeof (struct AddressToStringResultMessage))
86 alucb->cb (alucb->cb_cls, NULL, GNUNET_OK);
87 GNUNET_CLIENT_disconnect (alucb->client);
91 atsm = (struct AddressToStringResultMessage *) msg;
93 result = ntohl (atsm->res);
94 addr_len = ntohl (atsm->addr_len);
96 if (size == (sizeof (struct AddressToStringResultMessage)))
98 /* done, success depends on result */
99 alucb->cb (alucb->cb_cls, NULL, result);
100 GNUNET_CLIENT_disconnect (alucb->client);
105 if (GNUNET_NO == result)
107 alucb->cb (alucb->cb_cls, empty_str, GNUNET_SYSERR);
109 /* expect more replies */
110 GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
111 GNUNET_TIME_absolute_get_remaining (alucb->timeout));
115 address = (const char *) &atsm[1];
116 if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) ||
117 (address[addr_len -1] != '\0') )
121 alucb->cb (alucb->cb_cls, NULL, GNUNET_SYSERR);
122 GNUNET_CLIENT_disconnect (alucb->client);
127 /* expect more replies */
128 GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
129 GNUNET_TIME_absolute_get_remaining (alucb->timeout));
131 if (GNUNET_NO == result)
133 alucb->cb (alucb->cb_cls, empty_str, GNUNET_SYSERR);
137 alucb->cb (alucb->cb_cls, address, GNUNET_OK);
143 * Convert a binary address into a human readable address.
145 * @param cfg configuration to use
146 * @param address address to convert (binary format)
147 * @param numeric should (IP) addresses be displayed in numeric form
148 * (otherwise do reverse DNS lookup)
149 * @param timeout how long is the lookup allowed to take at most
150 * @param aluc function to call with the results
151 * @param aluc_cls closure for aluc
152 * @return handle to cancel the operation, NULL on error
154 struct GNUNET_TRANSPORT_AddressToStringContext *
155 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
157 const struct GNUNET_HELLO_Address *address,
159 struct GNUNET_TIME_Relative timeout,
160 GNUNET_TRANSPORT_AddressToStringCallback
161 aluc, void *aluc_cls)
166 struct AddressLookupMessage *msg;
167 struct GNUNET_TRANSPORT_AddressToStringContext *alc;
168 struct GNUNET_CLIENT_Connection *client;
171 GNUNET_assert (address != NULL);
172 alen = address->address_length;
173 slen = strlen (address->transport_name) + 1;
174 len = sizeof (struct AddressLookupMessage) + alen + slen;
175 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
180 client = GNUNET_CLIENT_connect ("transport", cfg);
183 msg = GNUNET_malloc (len);
184 msg->header.size = htons (len);
185 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
186 msg->numeric_only = htons ((int16_t) numeric);
187 msg->addrlen = htons ((uint16_t) alen);
188 msg->timeout = GNUNET_TIME_relative_hton (timeout);
189 addrbuf = (char *) &msg[1];
190 memcpy (addrbuf, address->address, alen);
191 memcpy (&addrbuf[alen], address->transport_name, slen);
193 alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
195 alc->cb_cls = aluc_cls;
196 alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
197 alc->client = client;
198 GNUNET_assert (GNUNET_OK ==
199 GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
201 &address_response_processor,
209 * Cancel request for address conversion.
211 * @param pic the context handle
214 GNUNET_TRANSPORT_address_to_string_cancel (struct
215 GNUNET_TRANSPORT_AddressToStringContext
218 GNUNET_CLIENT_disconnect (pic->client);
224 /* end of transport_api_address_to_string.c */