1 | |
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
6 | * See COPYRIGHT.txt for details. |
7 | */ |
8 | |
9 | #include <my_global.h> |
10 | |
11 | #include <stdexcept> |
12 | #include <string.h> |
13 | #include <signal.h> |
14 | #include <sys/un.h> |
15 | |
16 | #include "socket.hpp" |
17 | #include "string_util.hpp" |
18 | #include "fatal.hpp" |
19 | |
20 | namespace dena { |
21 | |
22 | void |
23 | ignore_sigpipe() |
24 | { |
25 | if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { |
26 | fatal_abort("SIGPIPE SIG_IGN" ); |
27 | } |
28 | } |
29 | |
30 | void |
31 | socket_args::set(const config& conf) |
32 | { |
33 | timeout = conf.get_int("timeout" , 600); |
34 | listen_backlog = conf.get_int("listen_backlog" , 256); |
35 | std::string node = conf.get_str("host" , "" ); |
36 | std::string port = conf.get_str("port" , "" ); |
37 | if (!node.empty() || !port.empty()) { |
38 | if (family == AF_UNIX || node == "/" ) { |
39 | set_unix_domain(port.c_str()); |
40 | } else { |
41 | const char *nd = node.empty() ? 0 : node.c_str(); |
42 | if (resolve(nd, port.c_str()) != 0) { |
43 | fatal_abort("getaddrinfo failed: " + node + ":" + port); |
44 | } |
45 | } |
46 | } |
47 | sndbuf = conf.get_int("sndbuf" , 0); |
48 | rcvbuf = conf.get_int("rcvbuf" , 0); |
49 | } |
50 | |
51 | void |
52 | socket_args::set_unix_domain(const char *path) |
53 | { |
54 | family = AF_UNIX; |
55 | addr = sockaddr_storage(); |
56 | addrlen = sizeof(sockaddr_un); |
57 | sockaddr_un *const ap = reinterpret_cast<sockaddr_un *>(&addr); |
58 | ap->sun_family = AF_UNIX; |
59 | strncpy(ap->sun_path, path, sizeof(ap->sun_path) - 1); |
60 | } |
61 | |
62 | int |
63 | socket_args::resolve(const char *node, const char *service) |
64 | { |
65 | const int flags = (node == 0) ? AI_PASSIVE : 0; |
66 | auto_addrinfo ai; |
67 | addr = sockaddr_storage(); |
68 | addrlen = 0; |
69 | const int r = ai.resolve(node, service, flags, family, socktype, protocol); |
70 | if (r != 0) { |
71 | return r; |
72 | } |
73 | memcpy(&addr, ai.get()->ai_addr, ai.get()->ai_addrlen); |
74 | addrlen = ai.get()->ai_addrlen; |
75 | return 0; |
76 | } |
77 | |
78 | int |
79 | socket_set_options(auto_file& fd, const socket_args& args, std::string& err_r) |
80 | { |
81 | if (args.timeout != 0 && !args.nonblocking) { |
82 | struct timeval tv; |
83 | tv.tv_sec = args.timeout; |
84 | tv.tv_usec = 0; |
85 | if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) { |
86 | return errno_string("setsockopt SO_RCVTIMEO" , errno, err_r); |
87 | } |
88 | tv.tv_sec = args.timeout; |
89 | tv.tv_usec = 0; |
90 | if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) { |
91 | return errno_string("setsockopt SO_RCVTIMEO" , errno, err_r); |
92 | } |
93 | } |
94 | if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { |
95 | return errno_string("fcntl O_NONBLOCK" , errno, err_r); |
96 | } |
97 | if (args.sndbuf != 0) { |
98 | const int v = args.sndbuf; |
99 | if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) != 0) { |
100 | return errno_string("setsockopt SO_SNDBUF" , errno, err_r); |
101 | } |
102 | } |
103 | if (args.rcvbuf != 0) { |
104 | const int v = args.rcvbuf; |
105 | if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) != 0) { |
106 | return errno_string("setsockopt SO_RCVBUF" , errno, err_r); |
107 | } |
108 | } |
109 | return 0; |
110 | } |
111 | |
112 | int |
113 | socket_open(auto_file& fd, const socket_args& args, std::string& err_r) |
114 | { |
115 | fd.reset(socket(args.family, args.socktype, args.protocol)); |
116 | if (fd.get() < 0) { |
117 | return errno_string("socket" , errno, err_r); |
118 | } |
119 | return socket_set_options(fd, args, err_r); |
120 | } |
121 | |
122 | int |
123 | socket_connect(auto_file& fd, const socket_args& args, std::string& err_r) |
124 | { |
125 | int r = 0; |
126 | if ((r = socket_open(fd, args, err_r)) != 0) { |
127 | return r; |
128 | } |
129 | if (connect(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr), |
130 | args.addrlen) != 0) { |
131 | if (!args.nonblocking || errno != EINPROGRESS) { |
132 | return errno_string("connect" , errno, err_r); |
133 | } |
134 | } |
135 | return 0; |
136 | } |
137 | |
138 | int |
139 | socket_bind(auto_file& fd, const socket_args& args, std::string& err_r) |
140 | { |
141 | fd.reset(socket(args.family, args.socktype, args.protocol)); |
142 | if (fd.get() < 0) { |
143 | return errno_string("socket" , errno, err_r); |
144 | } |
145 | if (args.reuseaddr) { |
146 | if (args.family == AF_UNIX) { |
147 | const sockaddr_un *const ap = |
148 | reinterpret_cast<const sockaddr_un *>(&args.addr); |
149 | if (unlink(ap->sun_path) != 0 && errno != ENOENT) { |
150 | return errno_string("unlink uds" , errno, err_r); |
151 | } |
152 | } else { |
153 | int v = 1; |
154 | if (setsockopt(fd.get(), SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0) { |
155 | return errno_string("setsockopt SO_REUSEADDR" , errno, err_r); |
156 | } |
157 | } |
158 | } |
159 | if (bind(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr), |
160 | args.addrlen) != 0) { |
161 | return errno_string("bind" , errno, err_r); |
162 | } |
163 | if (listen(fd.get(), args.listen_backlog) != 0) { |
164 | return errno_string("listen" , errno, err_r); |
165 | } |
166 | if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { |
167 | return errno_string("fcntl O_NONBLOCK" , errno, err_r); |
168 | } |
169 | return 0; |
170 | } |
171 | |
172 | int |
173 | socket_accept(int listen_fd, auto_file& fd, const socket_args& args, |
174 | sockaddr_storage& addr_r, size_socket& addrlen_r, std::string& err_r) |
175 | { |
176 | fd.reset(accept(listen_fd, reinterpret_cast<sockaddr *>(&addr_r), |
177 | &addrlen_r)); |
178 | if (fd.get() < 0) { |
179 | return errno_string("accept" , errno, err_r); |
180 | } |
181 | return socket_set_options(fd, args, err_r); |
182 | } |
183 | |
184 | }; |
185 | |
186 | |