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