a34f4c342127bbd37eafb0d293611c2ad9059d56
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 /**
22  * @file transport/plugin_transport_http_common.c
23  * @brief functionality shared by http client and server transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_transport_plugin.h"
30
31 /**
32  * Convert the transports address to a nice, human-readable
33  * format.
34  *
35  * @param cls closure
36  * @param type name of the transport that generated the address
37  * @param addr one of the addresses of the host, NULL for the last address
38  *        the specific address format depends on the transport
39  * @param addrlen length of the address
40  * @param numeric should (IP) addresses be displayed in numeric form?
41  * @param timeout after how long should we give up?
42  * @param asc function to call on each string
43  * @param asc_cls closure for asc
44  */
45 void
46 http_common_plugin_address_pretty_printer (void *cls, const char *type,
47                                         const void *addr, size_t addrlen,
48                                         int numeric,
49                                         struct GNUNET_TIME_Relative timeout,
50                                         GNUNET_TRANSPORT_AddressStringCallback
51                                         asc, void *asc_cls)
52 {
53   const char *saddr = (const char *) addr;
54   if (NULL == saddr)
55   {
56       asc (asc_cls, NULL);
57       return;
58   }
59   if (0 >= addrlen)
60   if (NULL == saddr)
61   {
62       asc (asc_cls, NULL);
63       return;
64   }
65   if (saddr[addrlen-1] != '\0')
66   if (NULL == saddr)
67   {
68       asc (asc_cls, NULL);
69       return;
70   }
71   asc (asc_cls, saddr);
72 }
73
74
75 /**
76  * Function called for a quick conversion of the binary address to
77  * a numeric address.  Note that the caller must not free the
78  * address and that the next call to this function is allowed
79  * to override the address again.
80  *
81  * @param cls closure
82  * @param addr binary address
83  * @param addrlen length of the address
84  * @return string representing the same address
85  */
86 const char *
87 http_common_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
88 {
89   const char *saddr = (const char *) addr;
90   if (NULL == saddr)
91       return NULL;
92   if (0 >= addrlen)
93     return NULL;
94   if (saddr[addrlen-1] != '\0')
95     return NULL;
96   return saddr;
97 }
98
99 /**
100  * Function called to convert a string address to
101  * a binary address.
102  *
103  * @param cls closure ('struct Plugin*')
104  * @param addr string address
105  * @param addrlen length of the address
106  * @param buf location to store the buffer
107  *        If the function returns GNUNET_SYSERR, its contents are undefined.
108  * @param added length of created address
109  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
110  */
111 int
112 http_common_plugin_string_to_address (void *cls,
113                         const char *addr,
114                         uint16_t addrlen,
115                         void **buf,
116                         size_t *added)
117 {
118   if (NULL == addr)
119       return GNUNET_SYSERR;
120   if (0 >= addrlen)
121     return GNUNET_SYSERR;
122   if (addr[addrlen-1] != '\0')
123     return GNUNET_SYSERR;
124
125   (*buf) = strdup (addr);
126   (*added) = strlen (addr) + 1;
127   return GNUNET_OK;
128 }
129
130 /**
131  * Create a HTTP address from a socketaddr
132  *
133  * @param protocol protocol
134  * @param addr sockaddr * address
135  * @param addrlen length of the address
136  * @return the string
137  */
138 char *
139 http_common_address_from_socket (const char *protocol, const struct sockaddr *addr, socklen_t addrlen)
140 {
141   char *res;
142   GNUNET_asprintf(&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
143   return res;
144 }
145
146 /**
147  * Create a socketaddr from a HTTP address
148  *
149  * @param addr sockaddr * address
150  * @param addrlen length of the address
151  * @param res the result:
152  * GNUNET_SYSERR, invalid input,
153  * GNUNET_YES: could convert to ip,
154  * GNUNET_NO: valid input but could not convert to ip (hostname?)
155  * @return the string
156  */
157 struct sockaddr *
158 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
159 {
160   struct sockaddr_storage *s;
161   char *addrs;
162   char *addrs_org;
163   char *addrs_end;
164   (*res) = GNUNET_SYSERR;
165
166   if (NULL == addr)
167     {
168       GNUNET_break (0);
169       return NULL;
170     }
171   if (0 >= addrlen)
172     {
173       GNUNET_break (0);
174       return NULL;
175     }
176   if (((char *) addr)[addrlen-1] != '\0')
177     {
178       GNUNET_break (0);
179       return NULL;
180     }
181
182   addrs_org = strdup ((char *) addr);
183   addrs = strstr (addrs_org , "://");
184   if (NULL == addrs)
185   {
186     GNUNET_break (0);
187     GNUNET_free (addrs_org);
188     return NULL;
189   }
190
191   if (strlen (addrs) < 3)
192   {
193     GNUNET_break (0);
194     GNUNET_free (addrs_org);
195     return NULL;
196   }
197
198   addrs += 3;
199
200   addrs_end = strchr (addrs, '/');
201   if (NULL != addrs_end)
202     addrs[strlen (addrs) - strlen(addrs_end)] = '\0';
203
204   s = GNUNET_malloc (sizeof (struct sockaddr_storage));
205   if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (addrs, strlen(addrs), s))
206   {
207     /* could be a hostname */
208     GNUNET_free (s);
209     GNUNET_free (addrs_org);
210     (*res) = GNUNET_NO;
211     return NULL;
212   }
213   else
214   {
215     if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
216     {
217       GNUNET_break (0);
218       GNUNET_free (s);
219       GNUNET_free (addrs_org);
220       (*res) = GNUNET_SYSERR;
221       return NULL;
222     }
223   }
224   (*res) = GNUNET_YES;
225   GNUNET_free (addrs_org);
226   return (struct sockaddr *) s;
227 }
228
229 /**
230  * Get the length of an address
231  *
232  * @param addr address
233  * @return the size
234  */
235 size_t
236 http_common_address_get_size (const void *addr)
237 {
238  return strlen (addr) + 1;
239 }
240
241 /**
242  * Compare addr1 to addr2
243  *
244  * @param addr1 address1
245  * @param addrlen1 address 1 length
246  * @param addr2 address2
247  * @param addrlen2 address 2 length
248  * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
249  */
250 int
251 http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
252 {
253   const char *a1 = (const char *) addr1;
254   const char *a2 = (const char *) addr2;
255
256   if (NULL == a1)
257       return GNUNET_SYSERR;
258   if (0 >= addrlen1)
259     return GNUNET_SYSERR;
260   if (a1[addrlen1-1] != '\0')
261     return GNUNET_SYSERR;
262
263   if (NULL == a2)
264       return GNUNET_SYSERR;
265   if (0 >= addrlen2)
266     return GNUNET_SYSERR;
267   if (a2[addrlen2-1] != '\0')
268     return GNUNET_SYSERR;
269
270   if (addrlen1 != addrlen2)
271     return GNUNET_NO;
272
273   if (0 == strcmp (addr1, addr2))
274     return GNUNET_YES;
275   return GNUNET_NO;
276 }
277
278
279
280 /* end of plugin_transport_http_common.c */