-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock.cpp
418 lines (380 loc) · 15.3 KB
/
sock.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright © 2023 Charles Kerr. All rights reserved.
//
#include "sock.hpp"
#include <stdexcept>
#include <cstring>
#include <array>
#include <algorithm>
#if !defined(_WIN32)
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/select.h>
#else
#include <WinBase.h>
#include <ws2tcpip.h>
#endif
using namespace std::string_literals;
namespace util {
namespace net {
// Declare errormsg in netutil.cpp
auto errormsg(int error) -> std::string;
//========================================================================
auto socket_t::create() ->void {
this->fd = ::socket(static_cast<int>(type),SOCK_STREAM,0);
if (fd == socket_error){
#if defined(_WIN32)
auto error = WSAGetLastError();
#else
auto error = errno;
#endif
throw std::runtime_error("Unable to create socket: "s+errormsg(error));
}
initialize();
}
//========================================================================
auto socket_t::initialize() ->void {
#if defined(__linux__)
sflag = MSG_NOSIGNAL;
#elif !defined(_WIN32)
auto value = int(1);
auto status = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
if (status != 0) {
throw std::runtime_error("Unable to set socket to nosignal : "s+errormsg(errno));
}
#endif
}
//========================================================================
socket_t::socket_t():sflag(0),rflag(0),fd(invalid_socket),type(ipclass::ip4),blocking(true){
}
//========================================================================
socket_t::socket_t(ipclass type):socket_t(){
this->type = type ;
create();
}
//========================================================================
socket_t::socket_t(ipclass type, descrip_t descriptor):socket_t(){
this->type = type ;
fd = descriptor;
initialize();
}
//========================================================================
auto socket_t::descriptor() const ->descrip_t {
return fd;
}
//========================================================================
// General methods
//========================================================================
//========================================================================
auto socket_t::setBlocking(bool value)->void {
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
#if defined(_WIN32)
unsigned long mode = 0;
if (!value) {
mode = 1 ;
}
auto status = ioctlsocket(fd, FIONBIO, &mode);
if (status != 0) {
throw std::runtime_error("Error setting socket block state: " + errormsg(WSAGetLastError()));
}
#else
auto current = fcntl(fd,F_GETFL,0) & ~O_NONBLOCK ; // Clear the nonblock flag
if (!value) {
current |= O_NONBLOCK;
}
int status = fcntl(fd, F_SETFL,current) ;
if (status == -1) {
throw std::runtime_error("Error setting socket block state: " + errormsg(errno));
}
#endif
blocking = value;
}
//========================================================================
auto socket_t::close() ->void {
auto msg = std::string();
if (valid()){
#if !defined(_WIN32)
auto error = ::close(fd);
if (error == -1) {
msg =errormsg(errno);
}
#else
auto error = closesocket(fd);
if (error !=0) {
msg = errormsg( WSAGetLastError());
}
#endif
if (!msg.empty()){
throw std::runtime_error("Error closing socket: "s + msg);
}
}
}
//========================================================================
auto socket_t::valid() const ->bool {
return fd != invalid_socket;
}
//========================================================================
auto socket_t::peer() const ->std::pair<std::string,std::string> {
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
auto name = std::string();
auto port = std::string();
struct sockaddr_in addr4 ;
struct sockaddr_in6 addr6 ;
auto size = 0 ;
sockaddr * addr = nullptr ;
auto ip = std::array<char,50>();
auto service = std::array<char,50>();
std::fill(ip.begin(),ip.end(),0);
std::fill(service.begin(),service.end(),0);
if (type == ipclass::ip4){
size = sizeof(addr4);
addr = reinterpret_cast<sockaddr*>(&addr4);
}
else if (type == ipclass::ip6){
size = sizeof(addr6);
addr = reinterpret_cast<sockaddr*>(&addr6);
}
auto status = getpeername(fd, reinterpret_cast<sockaddr*>(&addr), reinterpret_cast<socklen_t*>(&size));
if (status != 0){
#if defined (_WIN32)
throw std::runtime_error("Unable to retreive peer: "s + errormsg(WSAGetLastError()));
#else
throw std::runtime_error("Unable to retreive peer: "s + errormsg(errno));
#endif
}
status = getnameinfo(reinterpret_cast<const sockaddr*>(&addr),static_cast<socklen_t>(size),ip.data(),static_cast<socklen_t>(ip.size()),service.data(),static_cast<socklen_t>(service.size()),NI_NUMERICSERV|NI_NUMERICHOST);
if (status != 0 ){
#if defined (_WIN32)
throw std::runtime_error("Unable to convert peer to string: "s + errormsg(WSAGetLastError()));
#else
throw std::runtime_error("Unable to convert peer to string: "s + errormsg(errno));
#endif
}
name = ip.data();
port = service.data();
return std::make_pair(name, port);
}
//========================================================================
auto socket_t::bind(const std::string &port, const std::string &ipaddress,bool reuse)->void {
if (!this->valid()){
create();
}
if (reuse){
#if defined(_WIN32)
BOOL value = 1;
auto status = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&value), sizeof(value));
if (status != 0) {
throw std::runtime_error("Error setting socket reuse: "s + errormsg(WSAGetLastError()));
}
#else
auto value = int(1);
auto status = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
if (status != 0) {
throw std::runtime_error("Error setting socket reuse: "s + errormsg(errno));
}
#endif
}
auto status = int(0);
struct addrinfo hints;
struct addrinfo *res; // will point to the results
std::memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC;
if (type == ipclass::ip4){
hints.ai_family = AF_INET ;
}
else if (type == ipclass::ip6){
hints.ai_family = AF_INET6;
}
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
status = getaddrinfo(ipaddress.c_str(), port.c_str(), &hints, &res);
if (status != 0){
#if defined(_WIN32)
throw std::runtime_error("Error obtaining ip information: "s + errormsg(WSAGetLastError()));
#else
throw std::runtime_error("Error obtaining ip information: "s + std::string(gai_strerror(status)));
#endif
}
#if defined(_WIN32)
status = ::bind(fd, res->ai_addr, static_cast<int>(res->ai_addrlen));
#else
status = ::bind(fd, res->ai_addr, res->ai_addrlen);
#endif
if (status != 0){
#if defined(_WIN32)
auto error = WSAGetLastError();
#else
auto error = errno;
#endif
freeaddrinfo(res);
throw std::runtime_error("Error binding: "s + errormsg(error));
}
freeaddrinfo(res);
}
//========================================================================
// Server methods
//========================================================================
//========================================================================
auto socket_t::listen(int backlog )->void {
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
auto status = ::listen(fd,backlog);
if (status != 0){
#if defined(_WIN32)
throw std::runtime_error("Error listening on socket: "s + errormsg(WSAGetLastError()));
#else
throw std::runtime_error("Error listening on socket: "s + errormsg(errno));
#endif
}
}
//========================================================================
auto socket_t::accept() ->std::optional<socket_t>{
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
struct sockaddr_storage their_addr;
auto addr_size = sizeof(their_addr);
auto status = ::accept(fd,reinterpret_cast<struct sockaddr *>(&their_addr),reinterpret_cast<socklen_t*>(&addr_size));
if (status ==socket_error){
#if defined(_WIN32)
auto error = WSAGetLastError();
if (error != WSAEWOULDBLOCK){
throw std::runtime_error("Error listening on socket: "s + errormsg(error));
}
#else
auto error = errno;
if (error != EWOULDBLOCK){
throw std::runtime_error("Error listening on socket: "s + errormsg(error));
}
#endif
return {};
}
return socket_t(type,status);
}
//========================================================================
// Client methods
//========================================================================
//========================================================================
auto socket_t::connect(const std::string &ipaddress, const std::string &port,int sec_timeout) ->bool {
if (!this->valid()){
this->create();
}
#if defined(_WIN32)
auto blockerror = WSAEWOULDBLOCK;
#else
auto blockerror = EINPROGRESS;
#endif
auto status = int(0);
struct addrinfo hints;
struct addrinfo *res; // will point to the results
std::memset(&hints,0,sizeof(hints));
hints.ai_family = AF_INET; // IP4
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
if ((status = getaddrinfo(ipaddress.c_str(), port.c_str(), &hints, &res)) != 0) {
#if defined(_WIN32)
std::string msg = errormsg(WSAGetLastError());
#else
std::string msg = gai_strerror(status);
#endif
throw std::runtime_error("Error obtaining ip information: "s + msg);
}
auto current_state = blocking;
setBlocking(false);// We are going to use select with a timeout
timeval delay;
delay.tv_sec=sec_timeout;
delay.tv_usec = 0 ;
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(fd,&fdset);
#if defined(_WIN32)
status = ::connect(fd,res->ai_addr,static_cast<socklen_t>(res->ai_addrlen));
#else
status = ::connect(fd, res->ai_addr, res->ai_addrlen);
#endif
if (status ==socket_error){
#if defined(_WIN32)
auto failure = WSAGetLastError();
#else
auto failure =errno;
#endif
if (failure == blockerror){
// Ok, it might be ok still
status = ::select(static_cast<int>(fd+1), &fdset,nullptr, nullptr, &delay);
if ((status == 0) || (status == socket_error)){
try {
// Ok, the socket timed out. We close the socket
this->close();
}
catch(...){
}
freeaddrinfo(res);
return false ;
}
}
else {
// Real error
#if defined(_WIN32)
auto error = WSAGetLastError();
#else
auto error = errno;
#endif
freeaddrinfo(res);
throw std::runtime_error("Connection error: "s+errormsg(error));
}
}
freeaddrinfo(res);
setBlocking(current_state);// set the state back
return true ;
}
//========================================================================
auto socket_t::read(std::uint8_t *data,amount_t amount, bool peek) -> amount_t {
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
auto status = amount_t(0);
auto flag = rflag;
if (peek) {
flag |= MSG_PEEK;
}
if (amount > 0) {
auto status = recv(fd, reinterpret_cast<char*>(data), amount, flag);
if (status == socket_error) {
#if defined(_WIN32)
auto msg = errormsg(WSAGetLastError());
#else
auto msg = errormsg(errno);
#endif
throw std::runtime_error("Error on socket write: "s + msg);
}
}
return status ;
}
//========================================================================
auto socket_t::write(const std::uint8_t* data, amount_t amount) -> amount_t {
if (!this->valid()){
throw std::runtime_error("Socket unitialized");
}
auto status = amount_t(0);
if (amount > 0) {
auto status = send(fd, reinterpret_cast<const char*>(data), amount, sflag);
if (status == socket_error) {
#if defined(_WIN32)
auto msg = errormsg(WSAGetLastError());
#else
auto msg = errormsg(errno);
#endif
throw std::runtime_error("Error on socket write: "s + msg);
}
}
return status;
}
}
}