5
* Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved.
6
* See COPYRIGHT.txt for details.
18
#include "string_util.hpp"
26
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
27
fatal_abort("SIGPIPE SIG_IGN");
32
socket_args::set(const config& conf)
34
timeout = conf.get_int("timeout", 600);
35
listen_backlog = conf.get_int("listen_backlog", 256);
36
std::string node = conf.get_str("host", "");
37
std::string port = conf.get_str("port", "");
38
if (!node.empty() || !port.empty()) {
39
if (family == AF_UNIX || node == "/") {
40
set_unix_domain(port.c_str());
42
const char *nd = node.empty() ? 0 : node.c_str();
43
if (resolve(nd, port.c_str()) != 0) {
44
fatal_exit("getaddrinfo failed: " + node + ":" + port);
48
sndbuf = conf.get_int("sndbuf", 0);
49
rcvbuf = conf.get_int("rcvbuf", 0);
53
socket_args::set_unix_domain(const char *path)
56
addr = sockaddr_storage();
57
addrlen = sizeof(sockaddr_un);
58
sockaddr_un *const ap = reinterpret_cast<sockaddr_un *>(&addr);
59
ap->sun_family = AF_UNIX;
60
strncpy(ap->sun_path, path, sizeof(ap->sun_path) - 1);
64
socket_args::resolve(const char *node, const char *service)
66
const int flags = (node == 0) ? AI_PASSIVE : 0;
68
addr = sockaddr_storage();
70
const int r = ai.resolve(node, service, flags, family, socktype, protocol);
74
memcpy(&addr, ai.get()->ai_addr, ai.get()->ai_addrlen);
75
addrlen = ai.get()->ai_addrlen;
80
socket_set_options(auto_file& fd, const socket_args& args, std::string& err_r)
82
if (args.timeout != 0 && !args.nonblocking) {
83
struct timeval tv = { };
84
tv.tv_sec = args.timeout;
86
if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
87
return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
89
tv.tv_sec = args.timeout;
91
if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
92
return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
95
if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
96
return errno_string("fcntl O_NONBLOCK", errno, err_r);
98
if (args.sndbuf != 0) {
99
const int v = args.sndbuf;
100
if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) != 0) {
101
return errno_string("setsockopt SO_SNDBUF", errno, err_r);
104
if (args.rcvbuf != 0) {
105
const int v = args.rcvbuf;
106
if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) != 0) {
107
return errno_string("setsockopt SO_RCVBUF", errno, err_r);
114
socket_open(auto_file& fd, const socket_args& args, std::string& err_r)
116
fd.reset(socket(args.family, args.socktype, args.protocol));
118
return errno_string("socket", errno, err_r);
120
return socket_set_options(fd, args, err_r);
124
socket_connect(auto_file& fd, const socket_args& args, std::string& err_r)
127
if ((r = socket_open(fd, args, err_r)) != 0) {
130
if (connect(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
131
args.addrlen) != 0) {
132
if (!args.nonblocking || errno != EINPROGRESS) {
133
return errno_string("connect", errno, err_r);
140
socket_bind(auto_file& fd, const socket_args& args, std::string& err_r)
142
fd.reset(socket(args.family, args.socktype, args.protocol));
144
return errno_string("socket", errno, err_r);
146
if (args.reuseaddr) {
147
if (args.family == AF_UNIX) {
148
const sockaddr_un *const ap =
149
reinterpret_cast<const sockaddr_un *>(&args.addr);
150
if (unlink(ap->sun_path) != 0 && errno != ENOENT) {
151
return errno_string("unlink uds", errno, err_r);
155
if (setsockopt(fd.get(), SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0) {
156
return errno_string("setsockopt SO_REUSEADDR", errno, err_r);
160
if (bind(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
161
args.addrlen) != 0) {
162
return errno_string("bind", errno, err_r);
164
if (listen(fd.get(), args.listen_backlog) != 0) {
165
return errno_string("listen", errno, err_r);
167
if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
168
return errno_string("fcntl O_NONBLOCK", errno, err_r);
174
socket_accept(int listen_fd, auto_file& fd, const socket_args& args,
175
sockaddr_storage& addr_r, socklen_t& addrlen_r, std::string& err_r)
177
fd.reset(accept(listen_fd, reinterpret_cast<sockaddr *>(&addr_r),
180
return errno_string("accept", errno, err_r);
182
return socket_set_options(fd, args, err_r);