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
|
// vim:sw=2:ai
/*
* Copyright (C) 2010-2011 DeNA Co.,Ltd.. All rights reserved.
* Copyright (C) 2011 Kentoku SHIBA
* See COPYRIGHT.txt for details.
*/
#include <my_global.h>
#ifndef __WIN__
#include <sys/types.h>
#include <sys/un.h>
#endif
#include "mysql_version.h"
#include "hs_compat.h"
#if MYSQL_VERSION_ID < 50500
#include "mysql_priv.h"
#include <mysql/plugin.h>
#else
#if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
#include <my_global.h>
#endif
#include "sql_priv.h"
#include "probes_mysql.h"
#endif
#include "socket.hpp"
#include "string_util.hpp"
#include "fatal.hpp"
/*
struct sockaddr_un {
short sun_family;
char sun_path[108];
};
*/
namespace dena {
void
ignore_sigpipe()
{
#if defined(SIGPIPE) && !defined(__WIN__)
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
fatal_abort("SIGPIPE SIG_IGN");
}
#endif
}
void
socket_args::set(const config& conf)
{
timeout = (int) conf.get_int("timeout", 600);
listen_backlog = (int) conf.get_int("listen_backlog", 256);
String node = conf.get_str("host", "");
String port = conf.get_str("port", "");
if (node.length() || port.length()) {
if (family == AF_UNIX || !strcmp(node.c_ptr(), "/")) {
set_unix_domain(port.c_ptr());
} else {
const char *nd = !node.length() ? 0 : node.c_ptr();
if (resolve(nd, port.c_ptr()) != 0) {
String message("getaddrinfo failed: ", &my_charset_bin);
message.reserve(node.length() + sizeof(":") - 1 + port.length());
message.append(node);
message.q_append(":", sizeof(":") - 1);
message.append(port);
fatal_abort(message);
}
}
}
sndbuf = (int) conf.get_int("sndbuf", 0);
rcvbuf = (int) conf.get_int("rcvbuf", 0);
}
void
socket_args::set_unix_domain(const char *path)
{
#ifndef __WIN__
family = AF_UNIX;
addr = sockaddr_storage();
addrlen = sizeof(sockaddr_un);
sockaddr_un *const ap = reinterpret_cast<sockaddr_un *>(&addr);
ap->sun_family = AF_UNIX;
strncpy(ap->sun_path, path, sizeof(ap->sun_path) - 1);
#endif
}
int
socket_args::resolve(const char *node, const char *service)
{
const int flags = (node == 0) ? AI_PASSIVE : 0;
auto_addrinfo ai;
addr = sockaddr_storage();
addrlen = 0;
const int r = ai.resolve(node, service, flags, family, socktype, protocol);
if (r != 0) {
return r;
}
memcpy(&addr, ai.get()->ai_addr, ai.get()->ai_addrlen);
addrlen = ai.get()->ai_addrlen;
return 0;
}
int
socket_set_timeout(auto_file& fd, const socket_args& args, String& err_r)
{
if (!args.nonblocking) {
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
if (args.recv_timeout != 0) {
#ifndef __WIN__
struct timeval tv;
tv.tv_sec = args.recv_timeout;
tv.tv_usec = 0;
#else
int tv = args.recv_timeout * 1000;
#endif
if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO,
#ifndef __WIN__
(const void *) &tv,
#else
(const char *) &tv,
#endif
sizeof(tv)) != 0) {
return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
}
}
if (args.send_timeout != 0) {
#ifndef __WIN__
struct timeval tv;
tv.tv_sec = args.send_timeout;
tv.tv_usec = 0;
#else
int tv = args.send_timeout * 1000;
#endif
if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO,
#ifndef __WIN__
(const void *) &tv,
#else
(const char *) &tv,
#endif
sizeof(tv)) != 0) {
return errno_string("setsockopt SO_SNDTIMEO", errno, err_r);
}
}
#endif
}
return 0;
}
int
socket_set_options(auto_file& fd, const socket_args& args, String& err_r)
{
if (args.timeout != 0 && !args.nonblocking) {
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
#ifndef __WIN__
struct timeval tv;
tv.tv_sec = args.timeout;
tv.tv_usec = 0;
#else
int tv = args.timeout * 1000;
#endif
if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO,
#ifndef __WIN__
(const void *) &tv,
#else
(const char *) &tv,
#endif
sizeof(tv)) != 0) {
return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
}
#ifndef __WIN__
tv.tv_sec = args.timeout;
tv.tv_usec = 0;
#else
tv = args.timeout * 1000;
#endif
if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO,
#ifndef __WIN__
(const void *) &tv,
#else
(const char *) &tv,
#endif
sizeof(tv)) != 0) {
return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
}
#endif
}
#ifndef __WIN__
if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
return errno_string("fcntl O_NONBLOCK", errno, err_r);
}
#endif
if (args.sndbuf != 0) {
const int v = args.sndbuf;
if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF,
#ifndef __WIN__
(const void *) &v,
#else
(const char *) &v,
#endif
sizeof(v)) != 0) {
return errno_string("setsockopt SO_SNDBUF", errno, err_r);
}
}
if (args.rcvbuf != 0) {
const int v = args.rcvbuf;
if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF,
#ifndef __WIN__
(const void *) &v,
#else
(const char *) &v,
#endif
sizeof(v)) != 0) {
return errno_string("setsockopt SO_RCVBUF", errno, err_r);
}
}
return 0;
}
int
socket_open(auto_file& fd, const socket_args& args, String& err_r)
{
fd.reset((int)socket(args.family, args.socktype, args.protocol));
if (fd.get() < 0) {
return errno_string("socket", errno, err_r);
}
return socket_set_options(fd, args, err_r);
}
int
socket_connect(auto_file& fd, const socket_args& args, String& err_r)
{
int r = 0;
if ((r = socket_open(fd, args, err_r)) != 0) {
return r;
}
if (connect(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
args.addrlen) != 0) {
if (!args.nonblocking
#ifndef __WIN__
|| errno != EINPROGRESS
#endif
) {
return errno_string("connect", errno, err_r);
}
}
return 0;
}
int
socket_bind(auto_file& fd, const socket_args& args, String& err_r)
{
fd.reset((int)socket(args.family, args.socktype, args.protocol));
if (fd.get() < 0) {
return errno_string("socket", errno, err_r);
}
if (args.reuseaddr) {
#ifndef __WIN__
if (args.family == AF_UNIX) {
const sockaddr_un *const ap =
reinterpret_cast<const sockaddr_un *>(&args.addr);
if (unlink(ap->sun_path) != 0 && errno != ENOENT) {
return errno_string("unlink uds", errno, err_r);
}
} else {
#endif
int v = 1;
if (setsockopt(fd.get(), SOL_SOCKET, SO_REUSEADDR,
#ifndef __WIN__
(const void *) &v,
#else
(const char *) &v,
#endif
sizeof(v)) != 0) {
return errno_string("setsockopt SO_REUSEADDR", errno, err_r);
}
#ifndef __WIN__
}
#endif
}
if (bind(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
args.addrlen) != 0) {
return errno_string("bind", errno, err_r);
}
if (listen(fd.get(), args.listen_backlog) != 0) {
return errno_string("listen", errno, err_r);
}
#ifndef __WIN__
if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
return errno_string("fcntl O_NONBLOCK", errno, err_r);
}
#endif
return 0;
}
int
socket_accept(int listen_fd, auto_file& fd, const socket_args& args,
sockaddr_storage& addr_r, socklen_t& addrlen_r, String& err_r)
{
fd.reset((int)accept(listen_fd, reinterpret_cast<sockaddr *>(&addr_r),
&addrlen_r));
if (fd.get() < 0) {
return errno_string("accept", errno, err_r);
}
return socket_set_options(fd, args, err_r);
}
};
|