forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireaddr.c
More file actions
200 lines (181 loc) · 4.81 KB
/
Copy pathwireaddr.c
File metadata and controls
200 lines (181 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <arpa/inet.h>
#include <assert.h>
#include <ccan/tal/str/str.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <common/wireaddr.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <wire/wire.h>
/* Returns false if we didn't parse it, and *cursor == NULL if malformed. */
bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr)
{
addr->type = fromwire_u8(cursor, max);
switch (addr->type) {
case ADDR_TYPE_IPV4:
addr->addrlen = 4;
break;
case ADDR_TYPE_IPV6:
addr->addrlen = 16;
break;
default:
return false;
}
fromwire(cursor, max, addr->addr, addr->addrlen);
addr->port = fromwire_u16(cursor, max);
return *cursor != NULL;
}
void towire_wireaddr(u8 **pptr, const struct wireaddr *addr)
{
if (!addr || addr->type == ADDR_TYPE_PADDING) {
towire_u8(pptr, ADDR_TYPE_PADDING);
return;
}
towire_u8(pptr, addr->type);
towire(pptr, addr->addr, addr->addrlen);
towire_u16(pptr, addr->port);
}
char *fmt_wireaddr(const tal_t *ctx, const struct wireaddr *a)
{
char addrstr[INET6_ADDRSTRLEN];
char *ret, *hex;
switch (a->type) {
case ADDR_TYPE_IPV4:
if (!inet_ntop(AF_INET, a->addr, addrstr, INET_ADDRSTRLEN))
return "Unprintable-ipv4-address";
return tal_fmt(ctx, "%s:%u", addrstr, a->port);
case ADDR_TYPE_IPV6:
if (!inet_ntop(AF_INET6, a->addr, addrstr, INET6_ADDRSTRLEN))
return "Unprintable-ipv6-address";
return tal_fmt(ctx, "[%s]:%u", addrstr, a->port);
case ADDR_TYPE_PADDING:
break;
}
hex = tal_hexstr(ctx, a->addr, a->addrlen);
ret = tal_fmt(ctx, "Unknown type %u %s:%u", a->type, hex, a->port);
tal_free(hex);
return ret;
}
REGISTER_TYPE_TO_STRING(wireaddr, fmt_wireaddr);
/* Valid forms:
*
* [anything]:<number>
* anything-without-colons-or-left-brace:<number>
* anything-without-colons
* string-with-multiple-colons
*
* Returns false if it wasn't one of these forms. If it returns true,
* it only overwrites *port if it was specified by <number> above.
*/
static bool separate_address_and_port(tal_t *ctx, const char *arg,
char **addr, u16 *port)
{
char *portcolon;
if (strstarts(arg, "[")) {
char *end = strchr(arg, ']');
if (!end)
return false;
/* Copy inside [] */
*addr = tal_strndup(ctx, arg + 1, end - arg - 1);
portcolon = strchr(end+1, ':');
} else {
portcolon = strchr(arg, ':');
if (portcolon) {
/* Disregard if there's more than one : or if it's at
the start or end */
if (portcolon != strrchr(arg, ':')
|| portcolon == arg
|| portcolon[1] == '\0')
portcolon = NULL;
}
if (portcolon)
*addr = tal_strndup(ctx, arg, portcolon - arg);
else
*addr = tal_strdup(ctx, arg);
}
if (portcolon) {
char *endp;
*port = strtol(portcolon + 1, &endp, 10);
return *port != 0 && *endp == '\0';
}
return true;
}
bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport,
const char **err_msg)
{
struct in6_addr v6;
struct in_addr v4;
struct sockaddr_in6 *sa6;
struct sockaddr_in *sa4;
struct addrinfo *addrinfo;
struct addrinfo hints;
int gai_err;
u16 port;
char *ip;
bool res;
tal_t *tmpctx = tal_tmpctx(NULL);
res = false;
port = defport;
if (err_msg)
*err_msg = NULL;
if (!separate_address_and_port(tmpctx, arg, &ip, &port))
goto finish;
if (streq(ip, "localhost"))
ip = "127.0.0.1";
else if (streq(ip, "ip6-localhost"))
ip = "::1";
memset(&addr->addr, 0, sizeof(addr->addr));
if (inet_pton(AF_INET, ip, &v4) == 1) {
addr->type = ADDR_TYPE_IPV4;
addr->addrlen = 4;
addr->port = port;
memcpy(&addr->addr, &v4, addr->addrlen);
res = true;
} else if (inet_pton(AF_INET6, ip, &v6) == 1) {
addr->type = ADDR_TYPE_IPV6;
addr->addrlen = 16;
addr->port = port;
memcpy(&addr->addr, &v6, addr->addrlen);
res = true;
}
/* Resolve with getaddrinfo */
if (!res) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_ADDRCONFIG;
gai_err = getaddrinfo(ip, tal_fmt(tmpctx, "%d", port),
&hints, &addrinfo);
if (gai_err != 0) {
if (err_msg)
*err_msg = gai_strerror(gai_err);
goto finish;
}
/* Use only the first found address */
if (addrinfo->ai_family == AF_INET) {
addr->type = ADDR_TYPE_IPV4;
addr->addrlen = 4;
addr->port = port;
sa4 = (struct sockaddr_in *) addrinfo->ai_addr;
memcpy(&addr->addr, &sa4->sin_addr, addr->addrlen);
res = true;
} else if (addrinfo->ai_family == AF_INET6) {
addr->type = ADDR_TYPE_IPV6;
addr->addrlen = 16;
addr->port = port;
sa6 = (struct sockaddr_in6 *) addrinfo->ai_addr;
memcpy(&addr->addr, &sa6->sin6_addr, addr->addrlen);
res = true;
}
/* Clean up */
freeaddrinfo(addrinfo);
}
finish:
if (!res && err_msg && !*err_msg)
*err_msg = "Error parsing hostname";
tal_free(tmpctx);
return res;
}