-allow caller ID to differ from zone used for resolution
[oweals/gnunet.git] / src / transport / transport_api_address_to_string.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 #include "platform.h"
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"
27
28 /**
29  * Context for the address lookup.
30  */
31 struct GNUNET_TRANSPORT_AddressToStringContext
32 {
33   /**
34    * Function to call with the human-readable address.
35    */
36   GNUNET_TRANSPORT_AddressToStringCallback cb;
37
38   /**
39    * Closure for cb.
40    */
41   void *cb_cls;
42
43   /**
44    * Connection to the service.
45    */
46   struct GNUNET_CLIENT_Connection *client;
47
48   /**
49    * When should this operation time out?
50    */
51   struct GNUNET_TIME_Absolute timeout;
52 };
53
54
55 /**
56  * Function called with responses from the service.
57  *
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
61  */
62 static void
63 address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg)
64 {
65   struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
66   struct AddressToStringResultMessage *atsm;
67   const char *address;
68   uint16_t size;
69   uint32_t result;
70   uint32_t addr_len;
71   char *empty_str = "";
72
73   if (msg == NULL)
74   {
75     alucb->cb (alucb->cb_cls, NULL, GNUNET_OK);
76     GNUNET_CLIENT_disconnect (alucb->client);
77     GNUNET_free (alucb);
78     return;
79   }
80   GNUNET_break (ntohs (msg->type) ==
81                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
82
83   size = ntohs (msg->size);
84   if (size < sizeof (struct AddressToStringResultMessage))
85   {
86     alucb->cb (alucb->cb_cls, NULL, GNUNET_OK);
87     GNUNET_CLIENT_disconnect (alucb->client);
88     GNUNET_free (alucb);
89     return;
90   }
91   atsm = (struct AddressToStringResultMessage *) msg;
92
93   result = ntohl (atsm->res);
94   addr_len = ntohl (atsm->addr_len);
95
96   if (size == (sizeof (struct AddressToStringResultMessage)))
97   {
98     /* done, success depends on result */
99     alucb->cb (alucb->cb_cls, NULL, result);
100     GNUNET_CLIENT_disconnect (alucb->client);
101     GNUNET_free (alucb);
102     return;
103   }
104
105   if (GNUNET_NO == result)
106   {
107     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Client %p failed to resolve address \n",
108         alucb->client);
109     GNUNET_break (0);
110     alucb->cb (alucb->cb_cls, empty_str, GNUNET_SYSERR);
111
112     /* expect more replies */
113     GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
114                            GNUNET_TIME_absolute_get_remaining (alucb->timeout));
115     return;
116   }
117
118   address = (const char *) &atsm[1];
119   if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) ||
120        (address[addr_len -1] != '\0') )
121   {
122     /* invalid reply */
123     GNUNET_break (0);
124     alucb->cb (alucb->cb_cls, NULL, GNUNET_SYSERR);
125     GNUNET_CLIENT_disconnect (alucb->client);
126     GNUNET_free (alucb);
127     return;
128   }
129
130   /* expect more replies */
131   GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
132                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
133   alucb->cb (alucb->cb_cls, address, GNUNET_OK);
134 }
135
136
137 /**
138  * Convert a binary address into a human readable address.
139  *
140  * @param cfg configuration to use
141  * @param address address to convert (binary format)
142  * @param numeric should (IP) addresses be displayed in numeric form
143  *                (otherwise do reverse DNS lookup)
144  * @param timeout how long is the lookup allowed to take at most
145  * @param aluc function to call with the results
146  * @param aluc_cls closure for aluc
147  * @return handle to cancel the operation, NULL on error
148  */
149 struct GNUNET_TRANSPORT_AddressToStringContext *
150 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
151                                     *cfg,
152                                     const struct GNUNET_HELLO_Address *address,
153                                     int numeric,
154                                     struct GNUNET_TIME_Relative timeout,
155                                     GNUNET_TRANSPORT_AddressToStringCallback
156                                     aluc, void *aluc_cls)
157 {
158   size_t len;
159   size_t alen;
160   size_t slen;
161   struct AddressLookupMessage *msg;
162   struct GNUNET_TRANSPORT_AddressToStringContext *alc;
163   struct GNUNET_CLIENT_Connection *client;
164   char *addrbuf;
165
166   GNUNET_assert (address != NULL);
167   alen = address->address_length;
168   slen = strlen (address->transport_name) + 1;
169   len = sizeof (struct AddressLookupMessage) + alen + slen;
170   if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
171   {
172     GNUNET_break (0);
173     return NULL;
174   }
175   client = GNUNET_CLIENT_connect ("transport", cfg);
176   if (NULL == client)
177     return NULL;
178   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Client %p tries to resolve for peer `%s'address len %u \n",
179       client, GNUNET_i2s (&address->peer), address->address_length);
180
181   msg = GNUNET_malloc (len);
182   msg->header.size = htons (len);
183   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
184   msg->numeric_only = htons ((int16_t) numeric);
185   msg->addrlen = htons ((uint16_t) alen);
186   msg->timeout = GNUNET_TIME_relative_hton (timeout);
187   addrbuf = (char *) &msg[1];
188   memcpy (addrbuf, address->address, alen);
189   memcpy (&addrbuf[alen], address->transport_name, slen);
190
191   alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
192   alc->cb = aluc;
193   alc->cb_cls = aluc_cls;
194   alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
195   alc->client = client;
196   GNUNET_assert (GNUNET_OK ==
197                  GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
198                                                           timeout, GNUNET_YES,
199                                                           &address_response_processor,
200                                                           alc));
201   GNUNET_free (msg);
202   return alc;
203 }
204
205
206 /**
207  * Cancel request for address conversion.
208  *
209  * @param pic the context handle
210  */
211 void
212 GNUNET_TRANSPORT_address_to_string_cancel (struct
213                                            GNUNET_TRANSPORT_AddressToStringContext
214                                            *pic)
215 {
216   GNUNET_CLIENT_disconnect (pic->client);
217   GNUNET_free (pic);
218 }
219
220
221
222 /* end of transport_api_address_to_string.c */