-
Notifications
You must be signed in to change notification settings - Fork 0
/
addrinfo.cpp
316 lines (293 loc) · 6.83 KB
/
addrinfo.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
/**
* @file addrinfo.cpp
* @brief Addrinfoクラスの実装
*/
#include "addrinfo.hpp"
#include "socket.hpp"
#include <cstring>
#include <cstdlib>
#include <unistd.h>
namespace
{
const char *family_string(const int family)
{
switch (family) {
case PF_INET:
return "INET4";
case PF_INET6:
return "INET6";
default:
return "";
}
}
const char *socktype_string(const int socktype)
{
switch (socktype) {
case SOCK_STREAM:
return "STREAM";
case SOCK_DGRAM:
return "DGRAM";
default:
return "";
}
}
const char *protocol_string(const int protocol)
{
switch (protocol) {
case IPPROTO_TCP:
return "TCP";
case IPPROTO_UDP:
return "UDP";
default:
return "";
}
}
std::string sockaddr_in_string(const sockaddr_in *sin)
{
if (sin != NULL) {
char buf[128];
if (inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof buf) == NULL) {
perror("inet_ntop() failed");
return "";
}
char buf2[128];
uint16_t port = ntohs(sin->sin_port);
if (port != 0) {
snprintf(buf2, sizeof buf2, "%s:%d", buf, port);
return buf2;
} else {
return buf;
}
} else {
return "";
}
}
std::string addrinfo_string(const addrinfo *ai)
{
if (ai != NULL) {
std::string str("");
if (ai->ai_family != 0) {
str += family_string(ai->ai_family);
}
if (ai->ai_socktype) {
if (ai->ai_family != 0) {
str += " ";
}
str += socktype_string(ai->ai_socktype);
}
if (ai->ai_protocol) {
if (ai->ai_family != 0 || ai->ai_socktype) {
str += " ";
}
str += protocol_string(ai->ai_protocol);
}
return str;
} else {
return "";
}
}
void setup_addrinfo(addrinfo &ai, const int family, const int socktype, const int protocol, const int flags)
{
memset(&ai, 0, sizeof (addrinfo));
ai.ai_flags = flags;
ai.ai_family = family;
ai.ai_socktype = socktype;
ai.ai_protocol = protocol;
}
char *serv_by_port(const int port, const char *protocol)
{
char *result = NULL;
servent *se = getservbyport(htons(port), protocol);
if (se != NULL) {
result = strdup(se->s_name);
} else {
static const size_t RESULT_BUFSIZ = 12;
result = reinterpret_cast<char *>(malloc(RESULT_BUFSIZ));
if (result == NULL) {
perror("malloc() failed");
return NULL;
}
snprintf(result, RESULT_BUFSIZ, "%10d", port);
}
return result;
}
} // unnamed namespace
std::vector<Addrinfo *> Addrinfo::getaddrinfo(const char *host, const char *serv, const int family, const int socktype, const int protocol, const int flags)
{
addrinfo hints;
setup_addrinfo(hints, family, socktype, protocol, flags);
addrinfo *res0;
std::vector<Addrinfo *> v;
int ret = ::getaddrinfo(host, serv, &hints, &res0);
if (ret == 0) {
for (addrinfo *res = res0; res != NULL; res = res->ai_next) {
Addrinfo *ai = new Addrinfo(res);
v.push_back(ai);
}
}
freeaddrinfo(res0);
return v;
}
std::vector<Addrinfo *> Addrinfo::getaddrinfo(const char *host, const int port, const int family, const int socktype, const int protocol, const int flags)
{
char *serv = serv_by_port(port, NULL);
if (serv != NULL) {
std::vector<Addrinfo *> v(getaddrinfo(host, serv, family, socktype, protocol, flags));
free(serv);
return v;
} else {
std::vector<Addrinfo *> v;
return v;
}
}
static Addrinfo *new_addrinfo(const int pfamily, const int socktype, const int protocol, const char *host, const char *serv)
{
addrinfo hints;
setup_addrinfo(hints, pfamily, socktype, protocol, 0);
addrinfo *res0;
int ret = ::getaddrinfo(host, serv, &hints, &res0);
Addrinfo *ai = NULL;
if (ret == 0) {
ai = new Addrinfo(res0);
}
freeaddrinfo(res0);
return ai;
}
Addrinfo *Addrinfo::tcp(const char *host, const int port)
{
char *serv = serv_by_port(port, "tcp");
if (serv == NULL) {
return NULL;
}
Addrinfo *ai = tcp(host, serv);
free(serv);
return ai;
}
Addrinfo *Addrinfo::tcp(const char *host, const char *serv)
{
return new_addrinfo(PF_INET, SOCK_STREAM, IPPROTO_TCP, host, serv);
}
Addrinfo *Addrinfo::udp(const char *host, const int port)
{
char *serv = serv_by_port(port, "udp");
if (serv == NULL) {
return NULL;
}
Addrinfo *ai = udp(host, serv);
free(serv);
return ai;
}
Addrinfo *Addrinfo::udp(const char *host, const char *serv)
{
return new_addrinfo(PF_INET, SOCK_DGRAM, IPPROTO_UDP, host, serv);
}
/**
* @brief コンストラクタ
*
* @param [in] ai アドレス情報。
*/
Addrinfo::Addrinfo(const addrinfo *ai)
: ai_(NULL)
{
void *p = malloc(sizeof (addrinfo));
if (p != NULL) {
memcpy(p, ai, sizeof (addrinfo));
ai_ = reinterpret_cast<addrinfo *>(p);
p = malloc(ai->ai_addrlen);
if (p != NULL) {
memcpy(p, ai->ai_addr, ai->ai_addrlen);
sockaddr *addr = reinterpret_cast<sockaddr *>(p);
ai_->ai_addr = addr;
if (ai->ai_canonname != NULL) {
ai_->ai_canonname = strdup(ai->ai_canonname);
}
ai_->ai_next = NULL;
} else {
free(ai_);
ai_ = NULL;
}
}
}
Addrinfo::Addrinfo(const sockaddr_in *sin)
: ai_(NULL)
{
void *p = malloc(sizeof (addrinfo));
if (p != NULL) {
memset(p, 0, sizeof (addrinfo));
ai_ = reinterpret_cast<addrinfo *>(p);
ai_->ai_family = sin->sin_family;
ai_->ai_addrlen = sizeof (sockaddr_in);
p = malloc(sizeof (sockaddr_in));
if (p != NULL) {
memcpy(p, sin, sizeof (sockaddr_in));
sockaddr *addr = reinterpret_cast<sockaddr *>(p);
ai_->ai_addr = addr;
} else {
free(ai_);
ai_ = NULL;
}
}
}
Addrinfo::~Addrinfo()
{
if (ai_ != NULL) {
free(ai_->ai_canonname);
free(ai_->ai_addr);
}
free(ai_);
}
Socket *Addrinfo::bind()
{
int sock = socket(pfamily(), socktype(), protocol());
if (sock == -1) {
perror("socket() failed");
return NULL;
}
#if 1
int ok = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ok, sizeof ok) != 0) {
perror("setsockopt() failed");
close(sock);
return NULL;
}
#endif
int ret = ::bind(sock, addr(), addrlen());
if (ret != 0) {
perror("bind() failed");
close(sock);
return NULL;
}
return new Socket(sock);
}
Socket *Addrinfo::connect()
{
int sock = socket(pfamily(), socktype(), protocol());
if (sock == -1) {
perror("socket() failed");
return NULL;
}
int ret = ::connect(sock, addr(), addrlen());
if (ret != 0) {
perror("connect() failed");
close(sock);
return NULL;
}
return new Socket(sock);
}
std::string Addrinfo::inspect() const
{
std::string str("#<Addrinfo");
if (ai_ != NULL) {
str += ":";
if (ai_->ai_addr != NULL) {
str += " ";
str += sockaddr_in_string(addr_in());
}
if (ai_->ai_protocol) {
str += " ";
str += protocol_string(ai_->ai_protocol);
}
}
str += ">";
return str;
}