-doxygen
[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_AddressLookupContext*'
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   const char *address;
67   uint16_t size;
68
69   if (msg == NULL)
70   {
71     alucb->cb (alucb->cb_cls, NULL);
72     GNUNET_CLIENT_disconnect (alucb->client);
73     GNUNET_free (alucb);
74     return;
75   }
76   GNUNET_break (ntohs (msg->type) ==
77                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
78   size = ntohs (msg->size);
79   if (size == sizeof (struct GNUNET_MessageHeader))
80   {
81     /* done! */
82     alucb->cb (alucb->cb_cls, NULL);
83     GNUNET_CLIENT_disconnect (alucb->client);
84     GNUNET_free (alucb);
85     return;
86   }
87   address = (const char *) &msg[1];
88   if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
89   {
90     /* invalid reply */
91     GNUNET_break (0);
92     alucb->cb (alucb->cb_cls, NULL);
93     GNUNET_CLIENT_disconnect (alucb->client);
94     GNUNET_free (alucb);
95     return;
96   }
97   /* expect more replies */
98   GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb,
99                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
100   alucb->cb (alucb->cb_cls, address);
101 }
102
103
104 /**
105  * Convert a binary address into a human readable address.
106  *
107  * @param cfg configuration to use
108  * @param address address to convert (binary format)
109  * @param numeric should (IP) addresses be displayed in numeric form
110  *                (otherwise do reverse DNS lookup)
111  * @param timeout how long is the lookup allowed to take at most
112  * @param aluc function to call with the results
113  * @param aluc_cls closure for aluc
114  * @return handle to cancel the operation, NULL on error
115  */
116 struct GNUNET_TRANSPORT_AddressToStringContext *
117 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
118                                     *cfg,
119                                     const struct GNUNET_HELLO_Address *address,
120                                     int numeric,
121                                     struct GNUNET_TIME_Relative timeout,
122                                     GNUNET_TRANSPORT_AddressToStringCallback
123                                     aluc, void *aluc_cls)
124 {
125   size_t len;
126   size_t alen;
127   size_t slen;
128   struct AddressLookupMessage *msg;
129   struct GNUNET_TRANSPORT_AddressToStringContext *alc;
130   struct GNUNET_CLIENT_Connection *client;
131   char *addrbuf;
132
133   GNUNET_assert (address != NULL);
134   alen = address->address_length;
135   slen = strlen (address->transport_name) + 1;
136   len = sizeof (struct AddressLookupMessage) + alen + slen;
137   if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
138   {
139     GNUNET_break (0);
140     return NULL;
141   }
142   client = GNUNET_CLIENT_connect ("transport", cfg);
143   if (NULL == client)
144     return NULL;
145   msg = GNUNET_malloc (len);
146   msg->header.size = htons (len);
147   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING);
148   msg->numeric_only = htons ((int16_t) numeric);
149   msg->addrlen = htons ((uint16_t) alen);
150   msg->timeout = GNUNET_TIME_relative_hton (timeout);
151   addrbuf = (char *) &msg[1];
152   memcpy (addrbuf, address->address, alen);
153   memcpy (&addrbuf[alen], address->transport_name, slen);
154
155   alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_AddressToStringContext));
156   alc->cb = aluc;
157   alc->cb_cls = aluc_cls;
158   alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
159   alc->client = client;
160   GNUNET_assert (GNUNET_OK ==
161                  GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
162                                                           timeout, GNUNET_YES,
163                                                           &address_response_processor,
164                                                           alc));
165   GNUNET_free (msg);
166   return alc;
167 }
168
169
170 /**
171  * Cancel request for address conversion.
172  *
173  * @param alc handle for the request to cancel
174  */
175 void
176 GNUNET_TRANSPORT_address_to_string_cancel (struct
177                                            GNUNET_TRANSPORT_AddressToStringContext
178                                            *alc)
179 {
180   GNUNET_CLIENT_disconnect (alc->client);
181   GNUNET_free (alc);
182 }
183
184
185
186 /* end of transport_api_address_to_string.c */