clarify prettyprinter API and protocols, make sure implementations are consistent...
[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_CLIENT_disconnect (alucb->client);
80     GNUNET_free (alucb);
81     return;
82   }
83   GNUNET_break (ntohs (msg->type) ==
84                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
85
86   size = ntohs (msg->size);
87   if (size < sizeof (struct AddressToStringResultMessage))
88   {
89     GNUNET_break (0);
90     alucb->cb (alucb->cb_cls,
91                NULL,
92                GNUNET_SYSERR);
93     GNUNET_CLIENT_disconnect (alucb->client);
94     GNUNET_free (alucb);
95     return;
96   }
97   atsm = (const struct AddressToStringResultMessage *) msg;
98   result = (int) ntohl (atsm->res);
99   addr_len = ntohl (atsm->addr_len);
100   if (GNUNET_SYSERR == result)
101   {
102     /* expect more replies; as this is not the last
103        call, we must pass the empty string for the address */
104     alucb->cb (alucb->cb_cls,
105                "",
106                GNUNET_NO);
107     GNUNET_CLIENT_receive (alucb->client,
108                            &address_response_processor,
109                            alucb,
110                            GNUNET_TIME_UNIT_FOREVER_REL);
111     return;
112   }
113   if (size == (sizeof (struct AddressToStringResultMessage)))
114   {
115     if (GNUNET_OK != result)
116     {
117       GNUNET_break (0);
118       alucb->cb (alucb->cb_cls,
119                  NULL,
120                  GNUNET_SYSERR);
121       GNUNET_CLIENT_disconnect (alucb->client);
122       GNUNET_free (alucb);
123       return;
124     }
125     /* we are done (successfully, without communication errors) */
126     alucb->cb (alucb->cb_cls,
127                NULL,
128                GNUNET_OK);
129     GNUNET_CLIENT_disconnect (alucb->client);
130     GNUNET_free (alucb);
131     return;
132   }
133   address = (const char *) &atsm[1];
134   if ( (addr_len > (size - (sizeof (struct AddressToStringResultMessage)))) ||
135        (address[addr_len -1] != '\0') )
136   {
137     /* invalid reply */
138     GNUNET_break (0);
139     alucb->cb (alucb->cb_cls,
140                NULL,
141                GNUNET_SYSERR);
142     GNUNET_CLIENT_disconnect (alucb->client);
143     GNUNET_free (alucb);
144     return;
145   }
146   /* expect more replies */
147   GNUNET_CLIENT_receive (alucb->client,
148                          &address_response_processor,
149                          alucb,
150                          GNUNET_TIME_UNIT_FOREVER_REL);
151   /* return normal reply to caller */
152   alucb->cb (alucb->cb_cls,
153              address,
154              GNUNET_OK);
155 }
156
157
158 /**
159  * Convert a binary address into a human readable address.
160  *
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
169  */
170 struct GNUNET_TRANSPORT_AddressToStringContext *
171 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
172                                     const struct GNUNET_HELLO_Address *address,
173                                     int numeric,
174                                     struct GNUNET_TIME_Relative timeout,
175                                     GNUNET_TRANSPORT_AddressToStringCallback aluc,
176                                     void *aluc_cls)
177 {
178   size_t len;
179   size_t alen;
180   size_t slen;
181   struct AddressLookupMessage *msg;
182   struct GNUNET_TRANSPORT_AddressToStringContext *alc;
183   struct GNUNET_CLIENT_Connection *client;
184   char *addrbuf;
185
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)
191   {
192     GNUNET_break (0);
193     return NULL;
194   }
195   client = GNUNET_CLIENT_connect ("transport", cfg);
196   if (NULL == client)
197     return NULL;
198   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
199               "Client %p tries to resolve for peer `%s'address len %u \n",
200               client,
201               GNUNET_i2s (&address->peer),
202               address->address_length);
203
204   msg = GNUNET_malloc (len);
205   msg->header.size = htons (len);
206   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
207   msg->numeric_only = htons ((int16_t) numeric);
208   msg->addrlen = htons ((uint16_t) alen);
209   msg->timeout = GNUNET_TIME_relative_hton (timeout);
210   addrbuf = (char *) &msg[1];
211   memcpy (addrbuf,
212           address->address,
213           alen);
214   memcpy (&addrbuf[alen],
215           address->transport_name,
216           slen);
217   alc = GNUNET_new (struct GNUNET_TRANSPORT_AddressToStringContext);
218   alc->cb = aluc;
219   alc->cb_cls = aluc_cls;
220   alc->client = client;
221   GNUNET_assert (GNUNET_OK ==
222                  GNUNET_CLIENT_transmit_and_get_response (client,
223                                                           &msg->header,
224                                                           GNUNET_TIME_UNIT_FOREVER_REL,
225                                                           GNUNET_YES,
226                                                           &address_response_processor,
227                                                           alc));
228   GNUNET_free (msg);
229   return alc;
230 }
231
232
233 /**
234  * Cancel request for address conversion.
235  *
236  * @param pic the context handle
237  */
238 void
239 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic)
240 {
241   GNUNET_CLIENT_disconnect (pic->client);
242   GNUNET_free (pic);
243 }
244
245
246
247 /* end of transport_api_address_to_string.c */