-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsocketaddress.cpp
326 lines (278 loc) · 7.4 KB
/
socketaddress.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
316
317
318
319
320
321
322
323
324
325
326
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "base/socketaddress.h"
namespace wi {
extern void strncpyz(char *pszDst, const char *pszSrc, int cb);
}
namespace base {
SocketAddress::SocketAddress() {
hostname_ = NULL;
Clear();
}
SocketAddress::~SocketAddress() {
Clear();
}
SocketAddress::SocketAddress(const char *hostname, int port, bool use_dns) {
hostname_ = NULL;
Clear();
SetIP(hostname, use_dns);
SetPort(port);
}
SocketAddress::SocketAddress(const char *hostport, bool use_dns) {
hostname_ = NULL;
Clear();
// Parse hostname:port format
const char *colon = NULL;
for (const char *s = hostport; *s != 0; s++) {
if (*s == ':') {
colon = s;
}
}
if (colon == NULL) {
// Just hostname
SetIP(hostport, use_dns);
} else {
// Hostname:port
char hostnameT[256];
wi::strncpyz(hostnameT, hostport, (int)(colon - hostport + 1));
int port = atoi(colon + 1);
SetIP(hostnameT, use_dns);
SetPort(port);
}
}
SocketAddress::SocketAddress(dword ip, int port) {
hostname_ = NULL;
Clear();
SetIP(ip);
SetPort(port);
}
SocketAddress::SocketAddress(const SocketAddress& addr) {
hostname_ = NULL;
Clear();
this->operator=(addr);
}
void SocketAddress::Clear() {
delete[] hostname_;
hostname_ = NULL;
ip_ = 0;
port_ = 0;
}
SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
SetHostname(addr.hostname_);
ip_ = addr.ip_;
port_ = addr.port_;
return *this;
}
void SocketAddress::SetHostname(const char *hostname) {
if (hostname == hostname_)
return;
delete[] hostname_;
hostname_ = NULL;
if (hostname != NULL) {
int cb = (int)strlen(hostname) + 1;
hostname_ = new char[cb];
wi::strncpyz(hostname_, hostname, cb);
}
}
bool SocketAddress::IsHostnameEmpty() const {
return (hostname_ == NULL || hostname_[0] == 0);
}
void SocketAddress::SetIP(dword ip) {
SetHostname(NULL);
ip_ = ip;
}
bool SocketAddress::SetIP(const char *hostname, bool use_dns) {
SetHostname(hostname);
ip_ = 0;
return Resolve(true, use_dns);
}
void SocketAddress::SetResolvedIP(dword ip) {
ip_ = ip;
}
void SocketAddress::SetPort(int port) {
port_ = port;
}
dword SocketAddress::ip() const {
return ip_;
}
word SocketAddress::port() const {
return port_;
}
void SocketAddress::IPAsString(char *psz, int cb) const {
if (!IsHostnameEmpty()) {
wi::strncpyz(psz, hostname_, cb);
return;
}
IPToString(ip_, psz, cb);
}
void SocketAddress::PortAsString(char *psz, int cb) const {
char szT[32];
sprintf(szT, "%d", port_);
wi::strncpyz(psz, szT, cb);
}
void SocketAddress::ToString(char *psz, int cb) const {
IPAsString(psz, cb);
int cbT = (int)strlen(psz);
cb -= cbT + 1;
psz = &psz[cbT];
if (cb <= 0)
return;
*psz++ = ':';
cb--;
PortAsString(psz, cb);
}
const char *SocketAddress::ToString() const {
static char s_szT[128];
ToString(s_szT, sizeof(s_szT));
return s_szT;
}
bool SocketAddress::IsAny() const {
return (ip_ == 0);
}
bool SocketAddress::IsLocalIP() const {
return (ip_ >> 24) == 127;
}
bool SocketAddress::IsPrivateIP() const {
return ((ip_ >> 24) == 127) ||
((ip_ >> 24) == 10) ||
((ip_ >> 20) == ((172 << 4) | 1)) ||
((ip_ >> 16) == ((192 << 8) | 168));
}
bool SocketAddress::IsUnresolved() const {
return IsAny() && !IsHostnameEmpty();
}
bool SocketAddress::Resolve(bool force, bool use_dns) {
if (IsHostnameEmpty()) {
// nothing to resolve
} else if (!force && !IsAny()) {
// already resolved
} else if (dword ip = StringToIP(hostname_, use_dns)) {
ip_ = ip;
} else {
return false;
}
return true;
}
bool SocketAddress::operator ==(const SocketAddress& addr) const {
return EqualIPs(addr) && EqualPorts(addr);
}
bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
if (ip_ != addr.ip_) {
return false;
}
if (ip_ != 0) {
return true;
}
if (IsHostnameEmpty() != addr.IsHostnameEmpty()) {
return false;
}
if (hostname_ == NULL) {
return true;
}
return strcmp(hostname_, addr.hostname_) == 0;
}
bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
return (port_ == addr.port_);
}
dword SocketAddress::Hash() const {
dword h = 0;
h ^= ip_;
h ^= port_ | (port_ << 16);
return h;
}
void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
memset(saddr, 0, sizeof(*saddr));
saddr->sin_family = AF_INET;
saddr->sin_port = htons(port_);
if (0 == ip_) {
saddr->sin_addr.s_addr = INADDR_ANY;
} else {
saddr->sin_addr.s_addr = htonl(ip_);
}
}
void SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
SetIP(ntohl(saddr.sin_addr.s_addr));
SetPort(ntohs(saddr.sin_port));
}
void SocketAddress::IPToString(dword ip, char *psz, int cb) const {
char szT[64];
sprintf(szT, "%d.%d.%d.%d", (int)((ip >> 24) & 0xff),
(int)((ip >> 16) & 0xff), (int)((ip >> 8) & 0xff),
(int)((ip >> 0) & 0xff));
wi::strncpyz(psz, szT, cb);
}
dword SocketAddress::StringToIP(const char *hostname, bool use_dns) {
in_addr addr;
if (inet_aton(hostname, &addr) != 0) {
return ntohl(addr.s_addr);
} else if (use_dns) {
hostent *phost = gethostbyname(hostname);
if (phost == NULL) {
return 0;
}
return ntohl(*reinterpret_cast<dword *>(phost->h_addr_list[0]));
}
return 0;
}
void SocketAddress::GetHostname(char *psz, int cb) {
char hostname[256];
if (gethostname(hostname, sizeof(hostname)) == 0) {
wi::strncpyz(psz, hostname, cb);
} else {
if (cb > 0) {
*psz = 0;
}
}
}
bool SocketAddress::GetNetworkInfo(int n, char *pszName, int cb, dword *pip) {
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return false;
}
struct ifconf ifc;
ifc.ifc_len = 64 * sizeof(struct ifreq);
ifc.ifc_buf = new char[ifc.ifc_len];
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
delete [] ifc.ifc_buf;
close(fd);
return false;
}
int nT = 0;
struct ifreq* ptr = reinterpret_cast<struct ifreq*>(ifc.ifc_buf);
struct ifreq* end = reinterpret_cast<struct ifreq*>(ifc.ifc_buf + ifc.ifc_len);
while (ptr < end) {
struct sockaddr_in* inaddr = reinterpret_cast<struct sockaddr_in*>(&ptr->ifr_ifru.ifru_addr);
if (inaddr->sin_family == AF_INET) {
if (nT == n) {
wi::strncpyz(pszName, ptr->ifr_name, cb);
*pip = ntohl(inaddr->sin_addr.s_addr);
delete [] ifc.ifc_buf;
close(fd);
return true;
}
nT++;
}
#ifdef _SIZEOF_ADDR_IFREQ
ptr = reinterpret_cast<struct ifreq*>(
reinterpret_cast<char*>(ptr) + _SIZEOF_ADDR_IFREQ(*ptr));
#else
ptr++;
#endif
}
delete [] ifc.ifc_buf;
close(fd);
return false;
}
} // namespace base