-
Notifications
You must be signed in to change notification settings - Fork 126
/
socket.cpp
312 lines (257 loc) · 8.49 KB
/
socket.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
// socket.cpp
//
// --------------------------------------------------------------------------
// This file is part of the "sockpp" C++ socket library.
//
// Copyright (c) 2014-2017 Frank Pagliughi
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// --------------------------------------------------------------------------
#include "sockpp/socket.h"
#include "sockpp/exception.h"
#include <algorithm>
#include <cstring>
#include <fcntl.h>
// Used to explicitly ignore the returned value of a function call.
#define ignore_result(x) if (x) {}
using namespace std::chrono;
namespace sockpp {
/////////////////////////////////////////////////////////////////////////////
// Some aux functions
timeval to_timeval(const microseconds& dur)
{
const seconds sec = duration_cast<seconds>(dur);
timeval tv;
#if defined(_WIN32)
tv.tv_sec = long(sec.count());
#else
tv.tv_sec = time_t(sec.count());
#endif
tv.tv_usec = suseconds_t(duration_cast<microseconds>(dur - sec).count());
return tv;
}
/////////////////////////////////////////////////////////////////////////////
// socket
/////////////////////////////////////////////////////////////////////////////
int socket::get_last_error()
{
#if defined(_WIN32)
return ::WSAGetLastError();
#else
int err = errno;
return err;
#endif
}
// --------------------------------------------------------------------------
bool socket::close(socket_t h)
{
#if defined(_WIN32)
return ::closesocket(h) >= 0;
#else
return ::close(h) >= 0;
#endif
}
// --------------------------------------------------------------------------
// Closes the socket and updates the last error on failure.
// --------------------------------------------------------------------------
void socket::initialize()
{
#if defined(_WIN32)
WSADATA wsadata;
::WSAStartup(MAKEWORD(2, 0), &wsadata);
#else
// Don't signal on socket write errors.
::signal(SIGPIPE, SIG_IGN);
#endif
}
// --------------------------------------------------------------------------
void socket::destroy()
{
#if defined(_WIN32)
::WSACleanup();
#endif
}
// --------------------------------------------------------------------------
socket socket::create(int domain, int type, int protocol /*=0*/)
{
socket sock(::socket(domain, type, protocol));
if (!sock)
sock.clear(get_last_error());
return sock;
}
// --------------------------------------------------------------------------
socket socket::clone() const
{
socket_t h = INVALID_SOCKET;
#if defined(_WIN32)
WSAPROTOCOL_INFO protInfo;
if (::WSADuplicateSocket(handle_, ::GetCurrentProcessId(), &protInfo) == 0)
h = ::WSASocket(AF_INET, SOCK_STREAM, 0, &protInfo, 0, WSA_FLAG_OVERLAPPED);
// TODO: Set lastErr_ on failure
#else
h = ::dup(handle_);
#endif
return socket(h);
}
// --------------------------------------------------------------------------
std::tuple<socket, socket> socket::pair(int domain, int type, int protocol /*=0*/)
{
socket sock0, sock1;
#if !defined(_WIN32)
int sv[2];
int ret = ::socketpair(domain, type, protocol, sv);
if (ret == 0) {
sock0.reset(sv[0]);
sock1.reset(sv[1]);
}
else {
int err = get_last_error();
sock0.clear(err);
sock1.clear(err);
}
#else
sock0.clear(ENOTSUP);
sock1.clear(ENOTSUP);
#endif
// TODO: Should we set an "unsupported" error on Windows?
return std::make_tuple<socket, socket>(std::move(sock0), std::move(sock1));
}
// --------------------------------------------------------------------------
void socket::reset(socket_t h /*=INVALID_SOCKET*/)
{
socket_t oh = handle_;
handle_ = h;
if (oh != INVALID_SOCKET)
close(oh);
clear();
}
// --------------------------------------------------------------------------
// Binds the socket to the specified address.
bool socket::bind(const sock_address& addr)
{
return check_ret_bool(::bind(handle_, addr.sockaddr_ptr(), addr.size()));
}
// --------------------------------------------------------------------------
// Gets the local address to which the socket is bound.
sock_address_any socket::address() const
{
auto addrStore = sockaddr_storage{};
socklen_t len = sizeof(sockaddr_storage);
if (!check_ret_bool(::getsockname(handle_,
reinterpret_cast<sockaddr*>(&addrStore), &len)))
return sock_address_any{};
return sock_address_any(addrStore, len);
}
// --------------------------------------------------------------------------
// Gets the address of the remote peer, if this socket is connected.
sock_address_any socket::peer_address() const
{
auto addrStore = sockaddr_storage{};
socklen_t len = sizeof(sockaddr_storage);
if (!check_ret_bool(::getpeername(handle_,
reinterpret_cast<sockaddr*>(&addrStore), &len)))
return sock_address_any{};
return sock_address_any(addrStore, len);
}
// --------------------------------------------------------------------------
bool socket::get_option(int level, int optname, void* optval, socklen_t* optlen) const
{
#if defined(_WIN32)
int len = static_cast<int>(*optlen);
return check_ret_bool(::getsockopt(handle_, level, optname,
static_cast<char*>(optval), &len));
*optlen = static_cast<socklen_t>(len);
#else
return check_ret_bool(::getsockopt(handle_, level, optname, optval, optlen));
#endif
}
// --------------------------------------------------------------------------
bool socket::set_option(int level, int optname, const void* optval, socklen_t optlen)
{
#if defined(_WIN32)
return check_ret_bool(::setsockopt(handle_, level, optname,
static_cast<const char*>(optval),
static_cast<int>(optlen)));
#else
return check_ret_bool(::setsockopt(handle_, level, optname, optval, optlen));
#endif
}
/// --------------------------------------------------------------------------
bool socket::set_non_blocking(bool on /*=true*/)
{
#if defined(_WIN32)
unsigned long mode = on ? 1 : 0;
return check_ret_bool(::ioctlsocket(handle_, FIONBIO, &mode));
#else
/**
* TODO: Consider a generic function:
* bool set_flag(int flag, bool on=true);
* Used like:
* set_flag(O_NONBLOCK, on);
*/
int flags = ::fcntl(handle_, F_GETFL, 0);
if (flags == -1) {
set_last_error();
return false;
}
flags = on ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK);
if (::fcntl(handle_, F_SETFL, flags) == -1) {
set_last_error();
return false;
}
return true;
#endif
}
// --------------------------------------------------------------------------
// Gets a description of the last error encountered.
std::string socket::error_str(int err)
{
return sys_error::error_str(err);
}
// --------------------------------------------------------------------------
// Shuts down all or part of the connection.
bool socket::shutdown(int how /*=SHUT_RDWR*/)
{
return check_ret_bool(::shutdown(handle_, how));
}
// --------------------------------------------------------------------------
// Closes the socket and updates the last error on failure.
bool socket::close()
{
if (handle_ != INVALID_SOCKET) {
if (!close(release())){
set_last_error();
return false;
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
// End namespace sockpp
}