/src/strongswan/src/libstrongswan/networking/streams/stream_tcp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2013 Martin Willi |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include <library.h> |
18 | | #include <errno.h> |
19 | | #include <unistd.h> |
20 | | #include <limits.h> |
21 | | |
22 | | #include "stream_tcp.h" |
23 | | |
24 | | /** |
25 | | * See header. |
26 | | */ |
27 | | int stream_parse_uri_tcp(char *uri, struct sockaddr *addr) |
28 | 0 | { |
29 | 0 | char *pos, buf[128]; |
30 | 0 | host_t *host; |
31 | 0 | u_long port; |
32 | 0 | int len; |
33 | |
|
34 | 0 | if (!strpfx(uri, "tcp://")) |
35 | 0 | { |
36 | 0 | return -1; |
37 | 0 | } |
38 | 0 | uri += strlen("tcp://"); |
39 | 0 | pos = strrchr(uri, ':'); |
40 | 0 | if (!pos) |
41 | 0 | { |
42 | 0 | return -1; |
43 | 0 | } |
44 | 0 | if (*uri == '[' && pos > uri && *(pos - 1) == ']') |
45 | 0 | { |
46 | | /* IPv6 URI */ |
47 | 0 | snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri - 2), uri + 1); |
48 | 0 | } |
49 | 0 | else |
50 | 0 | { |
51 | 0 | snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri), uri); |
52 | 0 | } |
53 | 0 | port = strtoul(pos + 1, &pos, 10); |
54 | 0 | if (port == ULONG_MAX || *pos || port > 65535) |
55 | 0 | { |
56 | 0 | return -1; |
57 | 0 | } |
58 | 0 | host = host_create_from_dns(buf, AF_UNSPEC, port); |
59 | 0 | if (!host) |
60 | 0 | { |
61 | 0 | return -1; |
62 | 0 | } |
63 | 0 | len = *host->get_sockaddr_len(host); |
64 | 0 | memcpy(addr, host->get_sockaddr(host), len); |
65 | 0 | host->destroy(host); |
66 | 0 | return len; |
67 | 0 | } |
68 | | |
69 | | /** |
70 | | * See header |
71 | | */ |
72 | | stream_t *stream_create_tcp(char *uri) |
73 | 0 | { |
74 | 0 | union { |
75 | 0 | struct sockaddr_in in; |
76 | 0 | struct sockaddr_in6 in6; |
77 | 0 | struct sockaddr sa; |
78 | 0 | } addr; |
79 | 0 | int fd, len; |
80 | |
|
81 | 0 | len = stream_parse_uri_tcp(uri, &addr.sa); |
82 | 0 | if (len == -1) |
83 | 0 | { |
84 | 0 | DBG1(DBG_NET, "invalid stream URI: '%s'", uri); |
85 | 0 | return NULL; |
86 | 0 | } |
87 | 0 | fd = socket(addr.sa.sa_family, SOCK_STREAM, 0); |
88 | 0 | if (fd < 0) |
89 | 0 | { |
90 | 0 | DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno)); |
91 | 0 | return NULL; |
92 | 0 | } |
93 | 0 | if (connect(fd, &addr.sa, len)) |
94 | 0 | { |
95 | 0 | DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno)); |
96 | 0 | close(fd); |
97 | 0 | return NULL; |
98 | 0 | } |
99 | 0 | return stream_create_from_fd(fd); |
100 | 0 | } |