aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-01-18 11:07:46 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-01-18 11:07:46 +0000
commitdc8ac5f2e7117530a72cb972405aa7aea15d8a06 (patch)
tree3c9bbe7ef44fa80e7cb14df506d9c513a6249aec /src
parent4e12d1273df471ee0e79c996601eb4bbe3f1231b (diff)
downloadgnunet-dc8ac5f2e7117530a72cb972405aa7aea15d8a06.tar.gz
gnunet-dc8ac5f2e7117530a72cb972405aa7aea15d8a06.zip
address parsing
Diffstat (limited to 'src')
-rw-r--r--src/transport/plugin_transport_http_common.c108
-rw-r--r--src/transport/plugin_transport_http_common.h6
-rw-r--r--src/transport/test_http_common.c392
3 files changed, 458 insertions, 48 deletions
diff --git a/src/transport/plugin_transport_http_common.c b/src/transport/plugin_transport_http_common.c
index 0c9bfffc8..5c6d19791 100644
--- a/src/transport/plugin_transport_http_common.c
+++ b/src/transport/plugin_transport_http_common.c
@@ -27,13 +27,14 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29#include "gnunet_transport_plugin.h" 29#include "gnunet_transport_plugin.h"
30#include "plugin_transport_http_common.h"
30 31
31struct SplittedHTTPAddress 32struct SplittedHTTPAddress
32{ 33{
33 char *protocoll; 34 char *protocol;
34 char *host; 35 char *host;
35 char *port;
36 char *path; 36 char *path;
37 int port;
37}; 38};
38 39
39struct SplittedHTTPAddress * 40struct SplittedHTTPAddress *
@@ -41,14 +42,17 @@ http_split_address (const char * addr)
41{ 42{
42 struct SplittedHTTPAddress *sp; 43 struct SplittedHTTPAddress *sp;
43 char *src = GNUNET_strdup (addr); 44 char *src = GNUNET_strdup (addr);
44 char *protocoll_start = NULL; 45 char *protocol_start = NULL;
45 char *host_start = NULL; 46 char *host_start = NULL;
47 char *v6_end = NULL;
46 char *port_start = NULL; 48 char *port_start = NULL;
47 char *path_start = NULL; 49 char *path_start = NULL;
48 50
49 protocoll_start = src; 51 protocol_start = src;
50 sp = GNUNET_malloc (sizeof (struct SplittedHTTPAddress)); 52 sp = GNUNET_malloc (sizeof (struct SplittedHTTPAddress));
51 53
54 /* Address string consists of protocol://host[:port]path*/
55
52 host_start = strstr (src, "://"); 56 host_start = strstr (src, "://");
53 if (NULL == host_start) 57 if (NULL == host_start)
54 { 58 {
@@ -58,47 +62,94 @@ http_split_address (const char * addr)
58 } 62 }
59 63
60 host_start[0] = '\0'; 64 host_start[0] = '\0';
61 sp->protocoll = GNUNET_strdup (protocoll_start); 65 sp->protocol = GNUNET_strdup (protocol_start);
62 66
63 host_start += strlen ("://"); 67 host_start += strlen ("://");
64 if (strlen (host_start) == 0) 68 if (strlen (host_start) == 0)
65 if (NULL == host_start)
66 { 69 {
67 GNUNET_free (src); 70 GNUNET_free (src);
71 GNUNET_free (sp->protocol);
68 GNUNET_free (sp); 72 GNUNET_free (sp);
69 return NULL; 73 return NULL;
70 } 74 }
71 port_start = strstr (host_start, ":"); 75
72 if (NULL == port_start) 76 /* Find path start */
77 path_start = strchr (host_start, '/');
78 if (NULL != path_start)
73 { 79 {
74 path_start = strstr (host_start, "/"); 80 sp->path = GNUNET_strdup (path_start);
81 path_start[0] = '\0';
75 } 82 }
76 else 83 else
84 sp->path = GNUNET_strdup ("");
85
86 if (strlen(host_start) < 1)
77 { 87 {
78 port_start[0] = '\0'; 88 GNUNET_free (src);
79 port_start ++; 89 GNUNET_free (sp->protocol);
80 sp->host = GNUNET_strdup (host_start); 90 GNUNET_free (sp->path);
81 path_start = strstr (port_start, "/"); 91 GNUNET_free (sp);
92 return NULL;
82 } 93 }
83 94
84 if (NULL == path_start) 95 if (NULL != (port_start = strrchr (host_start, ':')))
85 {
86 if (NULL != port_start)
87 sp->port = GNUNET_strdup (port_start);
88 sp->path = NULL;
89 }
90 else
91 { 96 {
92 if (NULL != port_start) 97 /* *We COULD have a port, but also an IPv6 address! */
98 if (NULL != (v6_end = strchr(host_start, ']')))
99 {
100 if (v6_end < port_start)
101 {
102 /* IPv6 address + port */
103 port_start[0] = '\0';
104 port_start ++;
105 sp->port = atoi (port_start);
106 }
107 else
108 {
109 /* IPv6 address + no port */
110 if (0 == strcmp(sp->protocol, "https"))
111 sp->port = HTTPS_DEFAULT_PORT;
112 else if (0 == strcmp(sp->protocol, "http"))
113 sp->port = HTTP_DEFAULT_PORT;
114 }
115 }
116 else
93 { 117 {
94 path_start[0] = '\0'; 118 /* No IPv6 address */
95 sp->port = GNUNET_strdup (port_start); 119 port_start[0] = '\0';
96 path_start[0] = '/'; 120 port_start ++;
121 sp->port = atoi (port_start);
97 } 122 }
98 sp->path = GNUNET_strdup(path_start);
99 } 123 }
100 GNUNET_free (src); 124 else
101 fprintf (stderr, "protocoll: `%s', host `%s' port `%s' path `%s'\n", sp->protocoll, sp->host, sp->port, sp->path); 125 {
126 /* No ':' as port separator, default port for protocol */
127 if (0 == strcmp(sp->protocol, "https"))
128 sp->port = HTTPS_DEFAULT_PORT;
129 else if (0 == strcmp(sp->protocol, "http"))
130 sp->port = HTTP_DEFAULT_PORT;
131 else
132 {
133 GNUNET_break (0);
134 GNUNET_free (src);
135 GNUNET_free (sp->protocol);
136 GNUNET_free (sp->path);
137 GNUNET_free (sp);
138 return NULL;
139 }
140 }
141 if (strlen (host_start) > 0)
142 sp->host = GNUNET_strdup (host_start);
143 else
144 {
145 GNUNET_break (0);
146 GNUNET_free (src);
147 GNUNET_free (sp->protocol);
148 GNUNET_free (sp->path);
149 GNUNET_free (sp);
150 return NULL;
151 }
152 //fprintf (stderr, "addr: `%s' protocol: `%s', host `%s' port `%u' path `%s'\n", addr, sp->protocol, sp->host, sp->port, sp->path);
102 return sp; 153 return sp;
103} 154}
104 155
@@ -133,7 +184,6 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
133 asc (asc_cls, NULL); 184 asc (asc_cls, NULL);
134 return; 185 return;
135 } 186 }
136 http_split_address (addr);
137 asc (asc_cls, saddr); 187 asc (asc_cls, saddr);
138 asc (asc_cls, NULL); 188 asc (asc_cls, NULL);
139} 189}
@@ -314,7 +364,7 @@ http_common_address_get_size (const void *addr)
314 * @param addrlen2 address 2 length 364 * @param addrlen2 address 2 length
315 * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error 365 * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
316 */ 366 */
317int 367size_t
318http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2) 368http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
319{ 369{
320 const char *a1 = (const char *) addr1; 370 const char *a1 = (const char *) addr1;
diff --git a/src/transport/plugin_transport_http_common.h b/src/transport/plugin_transport_http_common.h
index 0cc6de5ee..31676c284 100644
--- a/src/transport/plugin_transport_http_common.h
+++ b/src/transport/plugin_transport_http_common.h
@@ -50,6 +50,10 @@
50 50
51#endif 51#endif
52 52
53#define HTTP_DEFAULT_PORT 80
54#define HTTPS_DEFAULT_PORT 443
55
56
53struct SplittedHTTPAddress; 57struct SplittedHTTPAddress;
54 58
55struct SplittedHTTPAddress * 59struct SplittedHTTPAddress *
@@ -147,7 +151,7 @@ http_common_socket_from_address (const void *addr, size_t addrlen, int *res);
147 * @return the size 151 * @return the size
148 */ 152 */
149size_t 153size_t
150http_common_address_get_size (void *addr); 154http_common_address_get_size (const void *addr);
151 155
152 156
153/** 157/**
diff --git a/src/transport/test_http_common.c b/src/transport/test_http_common.c
index 60aa3bfd3..05c4adb64 100644
--- a/src/transport/test_http_common.c
+++ b/src/transport/test_http_common.c
@@ -18,13 +18,8 @@
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20/** 20/**
21 * @file transport/test_transport_api.c 21 * @file transport/test_http_common.c
22 * @brief base test case for transport implementations 22 * @brief base test case for common http functionality
23 *
24 * This test case serves as a base for tcp, udp, and udp-nat
25 * transport test cases. Based on the executable being run
26 * the correct test case will be performed. Conservation of
27 * C code apparently.
28 */ 23 */
29#include "platform.h" 24#include "platform.h"
30#include "gnunet_transport_service.h" 25#include "gnunet_transport_service.h"
@@ -33,10 +28,10 @@
33 28
34struct SplittedHTTPAddress 29struct SplittedHTTPAddress
35{ 30{
36 char *protocoll; 31 char *protocol;
37 char *host; 32 char *host;
38 char *port;
39 char *path; 33 char *path;
34 int port;
40}; 35};
41 36
42void 37void
@@ -46,25 +41,386 @@ clean (struct SplittedHTTPAddress *addr)
46 { 41 {
47 GNUNET_free_non_null (addr->host); 42 GNUNET_free_non_null (addr->host);
48 GNUNET_free_non_null (addr->path); 43 GNUNET_free_non_null (addr->path);
49 GNUNET_free_non_null (addr->port); 44 GNUNET_free_non_null (addr->protocol);
50 GNUNET_free_non_null (addr->protocoll);
51 GNUNET_free_non_null (addr); 45 GNUNET_free_non_null (addr);
52 } 46 }
53} 47}
54 48
49
50int
51check (struct SplittedHTTPAddress *addr,
52 char * protocol,
53 char * host,
54 int port,
55 char * path)
56{
57
58 if (NULL == addr)
59 return GNUNET_NO;
60 if (((NULL == addr->protocol) && (NULL != protocol)) ||
61 ((NULL != addr->protocol) && (NULL == protocol)))
62 {
63 GNUNET_break (0);
64 return GNUNET_NO;
65 }
66 else if ((NULL != addr->protocol) && (NULL != protocol))
67 {
68 if (0 != strcmp(addr->protocol, protocol))
69 {
70 GNUNET_break (0);
71 return GNUNET_NO;
72 }
73 }
74
75 if (((NULL == addr->host) && (NULL != host)) ||
76 ((NULL != addr->host) && (NULL == host)))
77 {
78 GNUNET_break (0);
79 return GNUNET_NO;
80 }
81 else if ((NULL != addr->host) && (NULL != host))
82 {
83 if (0 != strcmp(addr->host, host))
84 {
85 GNUNET_break (0);
86 return GNUNET_NO;
87 }
88 }
89
90
91 if (((NULL == addr->path) && (NULL != path)) ||
92 ((NULL != addr->path) && (NULL == path)))
93 {
94 GNUNET_break (0);
95 return GNUNET_NO;
96 }
97 else if ((NULL != addr->path) && (NULL != path))
98 {
99 if (0 != strcmp(addr->path, path))
100 {
101 GNUNET_break (0);
102 return GNUNET_NO;
103 }
104 }
105
106 if ((addr->port != port))
107 {
108 GNUNET_break (0);
109 return GNUNET_NO;
110 }
111
112 return GNUNET_OK;
113}
114
115void
116test_hostname ()
117{
118 struct SplittedHTTPAddress * spa;
119 spa = http_split_address ("http://test.local");
120 if (NULL == spa)
121 {
122 GNUNET_break (0);
123 }
124 else
125 {
126 if (GNUNET_OK != check(spa, "http", "test.local", HTTP_DEFAULT_PORT, ""))
127 {
128 GNUNET_break (0);
129 }
130 clean (spa);
131 }
132
133 spa = http_split_address ("http://test.local");
134 if (NULL == spa)
135 {
136 GNUNET_break (0);
137 }
138 else
139 {
140 if (GNUNET_OK != check(spa, "http", "test.local", HTTP_DEFAULT_PORT, ""))
141 {
142 GNUNET_break (0);
143 }
144 clean (spa);
145 }
146
147
148 spa = http_split_address ("http://test.local/");
149 if (NULL == spa)
150 {
151 GNUNET_break (0);
152 }
153 else
154 {
155 if (GNUNET_OK != check(spa, "http", "test.local", HTTP_DEFAULT_PORT, "/"))
156 {
157 GNUNET_break (0);
158 }
159 clean (spa);
160 }
161
162 spa = http_split_address ("http://test.local/path");
163 if (NULL == spa)
164 {
165 GNUNET_break (0);
166 }
167 else
168 {
169 if (GNUNET_OK != check(spa, "http", "test.local", HTTP_DEFAULT_PORT, "/path"))
170 {
171 GNUNET_break (0);
172 }
173 clean (spa);
174 }
175
176 spa = http_split_address ("http://test.local/path/");
177 if (NULL == spa)
178 {
179 GNUNET_break (0);
180 }
181 else
182 {
183 if (GNUNET_OK != check(spa, "http", "test.local", HTTP_DEFAULT_PORT, "/path/"))
184 {
185 GNUNET_break (0);
186 }
187 clean (spa);
188
189
190 }
191
192 spa = http_split_address ("http://test.local:1000/path/");
193 if (NULL == spa)
194 {
195 GNUNET_break (0);
196 }
197 else
198 {
199 if (GNUNET_OK != check(spa, "http", "test.local", 1000, "/path/"))
200 {
201 GNUNET_break (0);
202 }
203 clean (spa);
204 }
205}
206
207void
208test_ipv4 ()
209{
210 struct SplittedHTTPAddress * spa;
211 spa = http_split_address ("http://127.0.0.1");
212 if (NULL == spa)
213 {
214 GNUNET_break (0);
215 }
216 else
217 {
218 if (GNUNET_OK != check(spa, "http", "127.0.0.1", HTTP_DEFAULT_PORT, ""))
219 {
220 GNUNET_break (0);
221 }
222 clean (spa);
223 }
224
225 spa = http_split_address ("http://127.0.0.1");
226 if (NULL == spa)
227 {
228 GNUNET_break (0);
229 }
230 else
231 {
232 if (GNUNET_OK != check(spa, "http", "127.0.0.1", HTTP_DEFAULT_PORT, ""))
233 {
234 GNUNET_break (0);
235 }
236 clean (spa);
237 }
238
239
240 spa = http_split_address ("http://127.0.0.1/");
241 if (NULL == spa)
242 {
243 GNUNET_break (0);
244 }
245 else
246 {
247 if (GNUNET_OK != check(spa, "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/"))
248 {
249 GNUNET_break (0);
250 }
251 clean (spa);
252 }
253
254 spa = http_split_address ("http://127.0.0.1/path");
255 if (NULL == spa)
256 {
257 GNUNET_break (0);
258 }
259 else
260 {
261 if (GNUNET_OK != check(spa, "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path"))
262 {
263 GNUNET_break (0);
264 }
265 clean (spa);
266 }
267
268 spa = http_split_address ("http://127.0.0.1/path/");
269 if (NULL == spa)
270 {
271 GNUNET_break (0);
272 }
273 else
274 {
275 if (GNUNET_OK != check(spa, "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path/"))
276 {
277 GNUNET_break (0);
278 }
279 clean (spa);
280
281
282 }
283
284 spa = http_split_address ("http://127.0.0.1:1000/path/");
285 if (NULL == spa)
286 {
287 GNUNET_break (0);
288 }
289 else
290 {
291 if (GNUNET_OK != check(spa, "http", "127.0.0.1", 1000, "/path/"))
292 {
293 GNUNET_break (0);
294 }
295 clean (spa);
296 }
297}
298
299void
300test_ipv6 ()
301{
302 struct SplittedHTTPAddress * spa;
303 spa = http_split_address ("http://[::1]");
304 if (NULL == spa)
305 {
306 GNUNET_break (0);
307 }
308 else
309 {
310 if (GNUNET_OK != check(spa, "http", "[::1]", HTTP_DEFAULT_PORT, ""))
311 {
312 GNUNET_break (0);
313 }
314 clean (spa);
315 }
316
317 spa = http_split_address ("http://[::1]");
318 if (NULL == spa)
319 {
320 GNUNET_break (0);
321 }
322 else
323 {
324 if (GNUNET_OK != check(spa, "http", "[::1]", HTTP_DEFAULT_PORT, ""))
325 {
326 GNUNET_break (0);
327 }
328 clean (spa);
329 }
330
331
332 spa = http_split_address ("http://[::1]/");
333 if (NULL == spa)
334 {
335 GNUNET_break (0);
336 }
337 else
338 {
339 if (GNUNET_OK != check(spa, "http", "[::1]", HTTP_DEFAULT_PORT, "/"))
340 {
341 GNUNET_break (0);
342 }
343 clean (spa);
344 }
345
346 spa = http_split_address ("http://[::1]/path");
347 if (NULL == spa)
348 {
349 GNUNET_break (0);
350 }
351 else
352 {
353 if (GNUNET_OK != check(spa, "http", "[::1]", HTTP_DEFAULT_PORT, "/path"))
354 {
355 GNUNET_break (0);
356 }
357 clean (spa);
358 }
359
360 spa = http_split_address ("http://[::1]/path/");
361 if (NULL == spa)
362 {
363 GNUNET_break (0);
364 }
365 else
366 {
367 if (GNUNET_OK != check(spa, "http", "[::1]", HTTP_DEFAULT_PORT, "/path/"))
368 {
369 GNUNET_break (0);
370 }
371 clean (spa);
372
373
374 }
375
376 spa = http_split_address ("http://[::1]:1000/path/");
377 if (NULL == spa)
378 {
379 GNUNET_break (0);
380 }
381 else
382 {
383 if (GNUNET_OK != check(spa, "http", "[::1]", 1000, "/path/"))
384 {
385 GNUNET_break (0);
386 }
387 clean (spa);
388 }
389}
390
55int 391int
56main (int argc, char *argv[]) 392main (int argc, char *argv[])
57{ 393{
58 int ret = 0; 394 int ret = 0;
395 struct SplittedHTTPAddress * spa;
396 GNUNET_log_setup ("test", "DEBUG", NULL);
397
398 spa = http_split_address ("");
399 if (NULL != spa)
400 {
401 clean (spa);
402 GNUNET_break (0);
403 }
404
405 http_split_address ("http://");
406 if (NULL != spa)
407 {
408 clean (spa);
409 GNUNET_break (0);
410 }
411
412 http_split_address ("://");
413 if (NULL != spa)
414 {
415 clean (spa);
416 GNUNET_break (0);
417 }
59 418
60 clean(http_split_address ("")); 419 test_hostname ();
61 clean(http_split_address ("http://")); 420 test_ipv4 ();
62 clean(http_split_address ("http://test/path")); 421 test_ipv6 ();
63 clean(http_split_address ("http://test:8999/path"));
64 clean(http_split_address ("http://1.2.3.4:8999/path"));
65 clean(http_split_address ("http://1.2.3.4:8999"));
66 422
67 return ret; 423 return ret;
68} 424}
69 425
70/* end of test_transport_api.c */ 426/* end of test_http_common.c */