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_client_lib.h"
22 #include "gnunet_arm_service.h"
23 #include "gnunet_hello_lib.h"
24 #include "gnunet_protocols.h"
25 #include "gnunet_server_lib.h"
26 #include "gnunet_time_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "transport.h"
31 * Context for the address lookup.
33 struct AddressLookupCtx
36 * Function to call with the human-readable address.
38 GNUNET_TRANSPORT_AddressLookUpCallback cb;
46 * Connection to the service.
48 struct GNUNET_CLIENT_Connection *client;
51 * When should this operation time out?
53 struct GNUNET_TIME_Absolute timeout;
58 * Function called with responses from the service.
60 * @param cls our 'struct AddressLookupCtx*'
61 * @param msg NULL on timeout or error, otherwise presumably a
62 * message with the human-readable address
65 address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg)
67 struct AddressLookupCtx *alucb = cls;
73 alucb->cb (alucb->cb_cls, NULL);
74 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
78 GNUNET_break (ntohs (msg->type) ==
79 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
80 size = ntohs (msg->size);
81 if (size == sizeof (struct GNUNET_MessageHeader))
84 alucb->cb (alucb->cb_cls, NULL);
85 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
89 address = (const char *) &msg[1];
90 if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
94 alucb->cb (alucb->cb_cls, NULL);
95 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
99 /* expect more replies */
100 GNUNET_CLIENT_receive (alucb->client,
101 &address_response_processor, alucb,
102 GNUNET_TIME_absolute_get_remaining (alucb->timeout));
103 alucb->cb (alucb->cb_cls, address);
108 * Convert a binary address into a human readable address.
110 * @param cfg configuration to use
111 * @param address address to convert (binary format)
112 * @param addressLen number of bytes in address
113 * @param numeric should (IP) addresses be displayed in numeric form
114 * (otherwise do reverse DNS lookup)
115 * @param nameTrans name of the transport to which the address belongs
116 * @param timeout how long is the lookup allowed to take at most
117 * @param aluc function to call with the results
118 * @param aluc_cls closure for aluc
121 GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
125 const char *nameTrans,
126 struct GNUNET_TIME_Relative timeout,
127 GNUNET_TRANSPORT_AddressLookUpCallback aluc,
132 struct AddressLookupMessage *msg;
133 struct AddressLookupCtx *aluCB;
134 struct GNUNET_CLIENT_Connection *client;
137 slen = strlen (nameTrans) + 1;
138 len = sizeof (struct AddressLookupMessage) + addressLen + slen;
139 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
142 aluc (aluc_cls, NULL);
145 client = GNUNET_CLIENT_connect ("transport", cfg);
148 aluc (aluc_cls, NULL);
151 msg = GNUNET_malloc (len);
152 msg->header.size = htons (len);
153 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP);
154 msg->numeric_only = htonl (numeric);
155 msg->timeout = GNUNET_TIME_relative_hton (timeout);
156 msg->addrlen = htonl (addressLen);
157 addrbuf = (char *) &msg[1];
158 memcpy (addrbuf, address, addressLen);
159 memcpy (&addrbuf[addressLen], nameTrans, slen);
160 aluCB = GNUNET_malloc (sizeof (struct AddressLookupCtx));
162 aluCB->cb_cls = aluc_cls;
163 aluCB->timeout = GNUNET_TIME_relative_to_absolute (timeout);
164 aluCB->client = client;
165 GNUNET_assert (GNUNET_OK ==
166 GNUNET_CLIENT_transmit_and_get_response (client,
170 &address_response_processor,
175 /* end of transport_api_address_lookup.c */