forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.cpp
315 lines (272 loc) · 9.05 KB
/
address.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* class Address implementation, just a nice wrapping around struct
* sockaddr_storage.
*/
#include "swift.h"
using namespace swift;
#define addr_debug false
Address::Address()
{
if (addr_debug)
fprintf(stderr,"Addres::Address()\n");
clear();
}
Address::Address(const char* ip, uint16_t port)
{
if (addr_debug)
fprintf(stderr,"Addres::Address(ip=%s,port=%" PRIu32 ")\n", ip, port);
clear();
set_ip(ip,AF_UNSPEC);
set_port(port);
}
Address::Address(const char* ip_port)
{
if (addr_debug)
fprintf(stderr,"Addres::Address(ip_port=%s)\n", ip_port);
clear();
if (strlen(ip_port)>=1024 || strlen(ip_port) == 0)
return;
char ipp[1024];
strncpy(ipp,ip_port,1024);
if (ipp[0] == '[') {
// IPV6 in square brackets following RFC2732
char* closesb = strchr(ipp,']');
if (closesb == NULL)
return;
char* semi = strchr(closesb,':');
*closesb = '\0';
if (semi) { // has port
*semi = '\0';
set_ipv6(ipp+1);
set_port(semi+1);
} else {
set_ipv6(ipp+1);
}
} else {
char* semi = strchr(ipp,':');
if (semi) {
*semi = 0;
set_ipv4(ipp);
set_port(semi+1);
} else {
if (strchr(ipp, '.')) {
set_ipv4(ipp);
set_port((uint16_t)0);
} else { // Arno: if just port, then IPv6
set_ipv6("::0");
set_port(ipp);
}
}
}
}
Address::Address(uint32_t ipv4addr, uint16_t port)
{
if (addr_debug)
fprintf(stderr,"Addres::Address(ipv4addr=%08x,port=%" PRIu32 ")\n", ipv4addr, port);
clear();
set_ipv4(ipv4addr);
set_port(port);
}
Address::Address(struct in6_addr ipv6addr, uint16_t port)
{
clear();
set_ipv6(ipv6addr);
set_port(port);
}
void Address::set_port(uint16_t port)
{
if (addr.ss_family == AF_INET) {
struct sockaddr_in *addr4ptr = (struct sockaddr_in *)&addr;
addr4ptr->sin_port = htons(port);
} else {
struct sockaddr_in6 *addr6ptr = (struct sockaddr_in6 *)&addr;
addr6ptr->sin6_port = htons(port);
}
}
void Address::set_port(const char* port_str)
{
int p;
if (sscanf(port_str,"%i",&p))
set_port(p);
}
void Address::set_ipv4(uint32_t ipv4)
{
addr.ss_family = AF_INET;
struct sockaddr_in *addr4ptr = (struct sockaddr_in *)&addr;
addr4ptr->sin_addr.s_addr = htonl(ipv4);
}
void Address::set_ipv6(struct in6_addr &ipv6)
{
addr.ss_family = AF_INET6;
struct sockaddr_in6 *addr6ptr = (struct sockaddr_in6 *)&addr;
memcpy(&addr6ptr->sin6_addr.s6_addr,&ipv6.s6_addr,sizeof(ipv6.s6_addr));
}
void Address::clear()
{
memset(&addr,0,sizeof(struct sockaddr_storage));
addr.ss_family = AF_UNSPEC;
}
uint32_t Address::ipv4() const
{
if (addr.ss_family == AF_INET) {
struct sockaddr_in *addr4ptr = (struct sockaddr_in *)&addr;
return ntohl(addr4ptr->sin_addr.s_addr);
} else
return (uint32_t)INADDR_ANY;
}
struct in6_addr Address::ipv6() const {
if (addr.ss_family == AF_INET6) {
struct sockaddr_in6 *addr6ptr = (struct sockaddr_in6 *)&addr;
return addr6ptr->sin6_addr;
} else
return in6addr_any;
}
uint16_t Address::port() const
{
if (addr.ss_family == AF_INET) {
struct sockaddr_in *addr4ptr = (struct sockaddr_in *)&addr;
return ntohs(addr4ptr->sin_port);
} else {
struct sockaddr_in6 *addr6ptr = (struct sockaddr_in6 *)&addr;
return ntohs(addr6ptr->sin6_port);
}
}
bool Address::operator == (const Address& b) const
{
if (addr.ss_family == AF_UNSPEC && b.addr.ss_family == AF_UNSPEC) {
// For comparing empty Address-es
return true;
} else if (addr.ss_family == AF_INET && b.addr.ss_family == AF_INET) {
struct sockaddr_in *aaddr4ptr = (struct sockaddr_in *)&addr;
struct sockaddr_in *baddr4ptr = (struct sockaddr_in *)&b.addr;
return aaddr4ptr->sin_port == baddr4ptr->sin_port &&
aaddr4ptr->sin_addr.s_addr==baddr4ptr->sin_addr.s_addr;
} else if (addr.ss_family == AF_INET6 && b.addr.ss_family == AF_INET6) {
struct sockaddr_in6 *aaddr6ptr = (struct sockaddr_in6 *)&addr;
struct sockaddr_in6 *baddr6ptr = (struct sockaddr_in6 *)&b.addr;
return aaddr6ptr->sin6_port == baddr6ptr->sin6_port &&
!memcmp(&aaddr6ptr->sin6_addr.s6_addr,&baddr6ptr->sin6_addr.s6_addr,sizeof(struct sockaddr_in6));
} else { // IPv4-mapped IP6 addr
struct sockaddr_in6 *xaddr6ptr = NULL;
struct sockaddr_in *yaddr4ptr = NULL;
if (addr.ss_family == AF_INET6 && b.addr.ss_family == AF_INET) {
xaddr6ptr = (struct sockaddr_in6 *)&addr;
yaddr4ptr = (struct sockaddr_in *)&b.addr;
} else {
xaddr6ptr = (struct sockaddr_in6 *)&b.addr;
yaddr4ptr = (struct sockaddr_in *)&addr;
}
// Convert IPv4 to IPv4-mapped IPv6 RFC4291
struct sockaddr_in6 y6map;
y6map.sin6_port = yaddr4ptr->sin_port;
int i=0;
for (i=0; i<10; i++)
y6map.sin6_addr.s6_addr[i] = 0x00;
for (i=10; i<12; i++)
y6map.sin6_addr.s6_addr[i] = 0xFF;
memcpy(&y6map.sin6_addr.s6_addr[i], &yaddr4ptr->sin_addr.s_addr, 4);
struct sockaddr_in6 *yaddr6ptr = (struct sockaddr_in6 *)&y6map;
return xaddr6ptr->sin6_port == yaddr6ptr->sin6_port &&
!memcmp(&xaddr6ptr->sin6_addr.s6_addr,&yaddr6ptr->sin6_addr.s6_addr,sizeof(struct in6_addr));
}
}
std::string Address::str() const
{
return ipstr(true);
}
std::string Address::ipstr(bool includeport) const
{
char node[256];
char service[256];
if (addr_debug)
fprintf(stderr,"Address::ipstr(includeport=%d): addr family %d\n", includeport, addr.ss_family);
if (addr.ss_family == AF_UNSPEC)
return "AF_UNSPEC";
/*
if (addr.ss_family == AF_INET) {
struct sockaddr_in *addr4ptr = (struct sockaddr_in *)&addr;
fprintf(stderr,"Address::ipstr:v4 OCTET %08lx\n", addr4ptr->sin_addr.s_addr );
}
else {
struct sockaddr_in6 *addr6ptr = (struct sockaddr_in6 *)&addr;
for (int i=0; i<16; i++)
fprintf(stderr,"Address::ipstr:v6 OCTET %02x\n", addr6ptr->sin6_addr.s6_addr[i] );
}*/
// See RFC3493
// Arno, 2013-06-05: pass real IP sockaddr length
int ret = getnameinfo((const struct sockaddr *)&addr, get_family_sockaddr_length(),
node, (socklen_t)sizeof(node),
service, (socklen_t)sizeof(service),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ret == 0) {
// Strip off zone index e.g. 2001:610:110:6e1:7578:776f:e141:d2bb%3435973836
std::string nodestr(node);
int idx = nodestr.find("%");
if (idx != std::string::npos)
nodestr = nodestr.substr(0,idx);
if (includeport)
return nodestr+":"+std::string(service);
else
return nodestr;
} else {
print_error("getnameinfo error");
return "getnameinfo failed";
}
}
bool Address::is_private() const
{
if (addr.ss_family == AF_INET) {
uint32_t no = ipv4();
uint8_t no0 = no>>24,no1 = (no>>16)&0xff;
if (no0 == 10) return true;
else if (no0 == 172 && no1 >= 16 && no1 <= 31) return true;
else if (no0 == 192 && no1 == 168) return true;
else return false;
} else {
// IPv6 Link-local address RFC4291
struct in6_addr s6 = ipv6();
return IN6_IS_ADDR_LINKLOCAL(&s6);
}
}
void Address::set_ipv4(const char* ip_str)
{
set_ip(ip_str,AF_INET);
}
void Address::set_ipv6(const char* ip_str)
{
set_ip(ip_str,AF_INET6);
}
void Address::set_ip(const char* ip_str, int family)
{
if (addr_debug)
fprintf(stderr,"Address::set_ip: %s family %d\n", ip_str, family);
struct addrinfo hint;
hint.ai_flags = AI_PASSIVE;
hint.ai_family = family;
hint.ai_socktype = 0;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
struct addrinfo *results=NULL;
int ret = getaddrinfo(ip_str, NULL, &hint, &results);
if (ret == 0) {
// Copy sockaddr to sockaddr_storage
memcpy(&addr,results->ai_addr,results->ai_addrlen);
if (addr_debug)
fprintf(stderr,"Address::set_ip: result %s\n", this->str().c_str());
}
if (results != NULL)
freeaddrinfo(results);
}
socklen_t Address::get_family_sockaddr_length() const
{
if (addr.ss_family == AF_INET) {
return sizeof(struct sockaddr_in);
} else if (addr.ss_family == AF_INET6) {
return sizeof(struct sockaddr_in6);
} else {
return 0;
}
}