-simplify
[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_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"
29
30 /**
31  * Context for the address lookup.
32  */
33 struct GNUNET_TRANSPORT_AddressToStringContext
34 {
35   /**
36    * Function to call with the human-readable address.
37    */
38   GNUNET_TRANSPORT_AddressToStringCallback cb;
39
40   /**
41    * Closure for cb.
42    */
43   void *cb_cls;
44
45   /**
46    * Connection to the service.
47    */
48   struct GNUNET_CLIENT_Connection *client;
49
50   /**
51    * When should this operation time out?
52    */
53   struct GNUNET_TIME_Absolute timeout;
54 };
55
56
57 /**
58  * Function called with responses from the service.
59  *
60  * @param cls our 'struct GNUNET_TRANSPORT_AddressLookupContext*'
61  * @param msg NULL on timeout or error, otherwise presumably a
62  *        message with the human-readable address
63  */
64 static void
65 address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg)
66 {
67   struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls;
68   const char *address;
69   uint16_t size;
70
71   if (msg == NULL)
72   {
73     alucb->cb (alucb->cb_cls, NULL);
74     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
75     GNUNET_free (alucb);
76     return;
77   }
78   GNUNET_break (ntohs (msg->type) ==
79                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
80   size = ntohs (msg->size);
81   if (size == sizeof (struct GNUNET_MessageHeader))
82   {
83     /* done! */
84     alucb->cb (alucb->cb_cls, NULL);
85     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
86     GNUNET_free (alucb);
87     return;
88   }
89   address = (const char *) &msg[1];
90   if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
91   {
92     /* invalid reply */
93     GNUNET_break (0);
94     alucb->cb (alucb->cb_cls, NULL);
95     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
96     GNUNET_free (alucb);
97     return;
98   }
99   /* expect more replies */
100   GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
101                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
102   alucb->cb (alucb->cb_cls, address);
103 }
104
105
106 /**
107  * Convert a binary address into a human readable address.
108  *
109  * @param cfg configuration to use
110  * @param address address to convert (binary format)
111  * @param numeric should (IP) addresses be displayed in numeric form
112  *                (otherwise do reverse DNS lookup)
113  * @param timeout how long is the lookup allowed to take at most
114  * @param aluc function to call with the results
115  * @param aluc_cls closure for aluc
116  * @return handle to cancel the operation, NULL on error
117  */
118 struct GNUNET_TRANSPORT_AddressToStringContext *
119 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
120                                     *cfg,
121                                     const struct GNUNET_HELLO_Address *address,
122                                     int numeric,
123                                     struct GNUNET_TIME_Relative timeout,
124                                     GNUNET_TRANSPORT_AddressToStringCallback
125                                     aluc, void *aluc_cls)
126 {
127   size_t len;
128   size_t alen;
129   size_t slen;
130   struct AddressLookupMessage *msg;
131   struct GNUNET_TRANSPORT_AddressToStringContext *alc;
132   struct GNUNET_CLIENT_Connection *client;
133   char *addrbuf;
134
135   GNUNET_assert (address != NULL);
136   alen = address->address_length;
137   slen = strlen (address->transport_name) + 1;
138   len = sizeof (struct AddressLookupMessage) + alen + slen;
139   if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
140   {
141     GNUNET_break (0);
142     return NULL;
143   }
144
145   client = GNUNET_CLIENT_connect ("transport", cfg);
146   if (client == NULL)
147     return NULL;
148 #if DEBUG_TRANSPORT
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_TRANSPORT_address_to_string\n");
150 #endif
151   msg = GNUNET_malloc (len);
152   msg->header.size = htons (len);
153   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
154   msg->numeric_only = htons ((int16_t) numeric);
155   msg->addrlen = htons ((uint16_t) alen);
156   msg->timeout = GNUNET_TIME_relative_hton (timeout);
157   addrbuf = (char *) &msg[1];
158   memcpy (addrbuf, address->address, alen);
159   memcpy (&addrbuf[alen], address->transport_name, slen);
160
161   alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_AddressToStringContext));
162   alc->cb = aluc;
163   alc->cb_cls = aluc_cls;
164   alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
165   alc->client = client;
166   GNUNET_assert (GNUNET_OK ==
167                  GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
168                                                           timeout, GNUNET_YES,
169                                                           &address_response_processor,
170                                                           alc));
171   GNUNET_free (msg);
172   return alc;
173 }
174
175
176 /**
177  * Cancel request for address conversion.
178  *
179  * @param alc handle for the request to cancel
180  */
181 void
182 GNUNET_TRANSPORT_address_to_string_cancel (struct
183                                            GNUNET_TRANSPORT_AddressToStringContext
184                                            *alc)
185 {
186   GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
187   GNUNET_free (alc);
188 }
189
190
191
192 /* end of transport_api_address_to_string.c */