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