5dd3413e1ebe9df9c1e2d166f43c954856312aa5
[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
55   if ( (NULL == saddr) ||
56        (0 >= addrlen) ||
57        ('\0' != saddr[addrlen-1]) )
58   {
59       asc (asc_cls, NULL);
60       return;
61   }
62   asc (asc_cls, saddr);
63   asc (asc_cls, NULL);
64 }
65
66
67 /**
68  * Function called for a quick conversion of the binary address to
69  * a numeric address.  Note that the caller must not free the
70  * address and that the next call to this function is allowed
71  * to override the address again.
72  *
73  * @param cls closure
74  * @param addr binary address
75  * @param addrlen length of the address
76  * @return string representing the same address
77  */
78 const char *
79 http_common_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
80 {
81   const char *saddr = (const char *) addr;
82   if (NULL == saddr)
83       return NULL;
84   if (0 >= addrlen)
85     return NULL;
86   if (saddr[addrlen-1] != '\0')
87     return NULL;
88   return saddr;
89 }
90
91 /**
92  * Function called to convert a string address to
93  * a binary address.
94  *
95  * @param cls closure ('struct Plugin*')
96  * @param addr string address
97  * @param addrlen length of the address
98  * @param buf location to store the buffer
99  *        If the function returns GNUNET_SYSERR, its contents are undefined.
100  * @param added length of created address
101  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
102  */
103 int
104 http_common_plugin_string_to_address (void *cls,
105                         const char *addr,
106                         uint16_t addrlen,
107                         void **buf,
108                         size_t *added)
109 {
110   if (NULL == addr)
111       return GNUNET_SYSERR;
112   if (0 >= addrlen)
113     return GNUNET_SYSERR;
114   if (addr[addrlen-1] != '\0')
115     return GNUNET_SYSERR;
116
117   (*buf) = strdup (addr);
118   (*added) = strlen (addr) + 1;
119   return GNUNET_OK;
120 }
121
122 /**
123  * Create a HTTP address from a socketaddr
124  *
125  * @param protocol protocol
126  * @param addr sockaddr * address
127  * @param addrlen length of the address
128  * @return the string
129  */
130 char *
131 http_common_address_from_socket (const char *protocol, const struct sockaddr *addr, socklen_t addrlen)
132 {
133   char *res;
134   GNUNET_asprintf(&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
135   return res;
136 }
137
138 /**
139  * Create a socketaddr from a HTTP address
140  *
141  * @param addr sockaddr * address
142  * @param addrlen length of the address
143  * @param res the result:
144  * GNUNET_SYSERR, invalid input,
145  * GNUNET_YES: could convert to ip,
146  * GNUNET_NO: valid input but could not convert to ip (hostname?)
147  * @return the string
148  */
149 struct sockaddr *
150 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
151 {
152   struct sockaddr_storage *s;
153   char *addrs;
154   char *addrs_org;
155   char *addrs_end;
156   (*res) = GNUNET_SYSERR;
157
158   if (NULL == addr)
159     {
160       GNUNET_break (0);
161       return NULL;
162     }
163   if (0 >= addrlen)
164     {
165       GNUNET_break (0);
166       return NULL;
167     }
168   if (((char *) addr)[addrlen-1] != '\0')
169     {
170       GNUNET_break (0);
171       return NULL;
172     }
173
174   addrs_org = strdup ((char *) addr);
175   addrs = strstr (addrs_org , "://");
176   if (NULL == addrs)
177   {
178     GNUNET_break (0);
179     GNUNET_free (addrs_org);
180     return NULL;
181   }
182
183   if (strlen (addrs) < 3)
184   {
185     GNUNET_break (0);
186     GNUNET_free (addrs_org);
187     return NULL;
188   }
189
190   addrs += 3;
191
192   addrs_end = strchr (addrs, '/');
193   if (NULL != addrs_end)
194     addrs[strlen (addrs) - strlen(addrs_end)] = '\0';
195
196   s = GNUNET_malloc (sizeof (struct sockaddr_storage));
197   if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (addrs, strlen(addrs), s))
198   {
199     /* could be a hostname */
200     GNUNET_free (s);
201     GNUNET_free (addrs_org);
202     (*res) = GNUNET_NO;
203     return NULL;
204   }
205   else
206   {
207     if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
208     {
209       GNUNET_break (0);
210       GNUNET_free (s);
211       GNUNET_free (addrs_org);
212       (*res) = GNUNET_SYSERR;
213       return NULL;
214     }
215   }
216   (*res) = GNUNET_YES;
217   GNUNET_free (addrs_org);
218   return (struct sockaddr *) s;
219 }
220
221 /**
222  * Get the length of an address
223  *
224  * @param addr address
225  * @return the size
226  */
227 size_t
228 http_common_address_get_size (const void *addr)
229 {
230  return strlen (addr) + 1;
231 }
232
233 /**
234  * Compare addr1 to addr2
235  *
236  * @param addr1 address1
237  * @param addrlen1 address 1 length
238  * @param addr2 address2
239  * @param addrlen2 address 2 length
240  * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
241  */
242 int
243 http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
244 {
245   const char *a1 = (const char *) addr1;
246   const char *a2 = (const char *) addr2;
247
248   if (NULL == a1)
249       return GNUNET_SYSERR;
250   if (0 >= addrlen1)
251     return GNUNET_SYSERR;
252   if (a1[addrlen1-1] != '\0')
253     return GNUNET_SYSERR;
254
255   if (NULL == a2)
256       return GNUNET_SYSERR;
257   if (0 >= addrlen2)
258     return GNUNET_SYSERR;
259   if (a2[addrlen2-1] != '\0')
260     return GNUNET_SYSERR;
261
262   if (addrlen1 != addrlen2)
263     return GNUNET_NO;
264
265   if (0 == strcmp (addr1, addr2))
266     return GNUNET_YES;
267   return GNUNET_NO;
268 }
269
270
271
272 /* end of plugin_transport_http_common.c */