-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.cpp
345 lines (316 loc) · 11.1 KB
/
tcp.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
#include <string>
#include <iostream>
#include <cstring>
#include <stdexcept>
#include <sstream>
#include <mutex>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "tcp.hpp"
using namespace std;
TCPSock::TCPSock(void) {
sock_d = 0;
memset((char *) &addr, 0, sizeof(addr));
}
TCPSock::~TCPSock(void) {
if (ctx) SSL_CTX_free(ctx);
}
void TCPSock::close() {
if (sock_d >= 0) {
::close(sock_d);
}
}
void TCPSock::shutdown() {
::shutdown(sock_d, SHUT_RD);
}
std::string TCPSock::getAddr() {
static std::mutex mtx;
mtx.lock();
std::string s_addr(inet_ntoa(addr.sin_addr));
mtx.unlock();
return s_addr;
}
TCPServer::TCPServer(void) {
// create TCP socket
sock_d = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sock_d <= 0) {
throw std::runtime_error("Could not create socket.");
}
// initialize SSL
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
method = SSLv23_server_method();
SSL_library_init();
ctx = SSL_CTX_new(method);
if (ctx == NULL){
ERR_print_errors_fp(stderr);
abort();
}
}
void TCPServer::bind(int port) {
socklen_t sock_size = sizeof(addr);
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
if (::bind(sock_d, (struct sockaddr *)&addr, sock_size) < 0) {
throw std::runtime_error("Could not bind socket to address.");
}
}
void TCPServer::listen(int queue_size) {
if(::listen(sock_d, queue_size) < 0) {
throw std::runtime_error("Could not start listenning.");
}
}
void TCPServer::open_cert(const char* cert, const char* pkey) {
if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) != 1) {
throw std::runtime_error("Error loading SSL certificate.");
}
if (SSL_CTX_use_PrivateKey_file(ctx, pkey, SSL_FILETYPE_PEM) != 1) {
throw std::runtime_error("Error loading SSL private key.");
}
}
TCPConnection* TCPServer::accept() {
TCPConnection *con = new TCPConnection;
socklen_t sock_size;
sock_size = sizeof(con->addr);
con->sock_d = ::accept(sock_d, (struct sockaddr *) &(con->addr), &sock_size);
if (con->sock_d <= 0) {
delete con;
if (errno == EINTR) return NULL;
if (errno == EINVAL) return NULL;
if (errno == EAGAIN) return NULL;
if (errno == EWOULDBLOCK) return NULL;
throw std::runtime_error("Could not accept connection");
}
con->ctx = NULL;
con->ssl = SSL_new(ctx);
SSL_set_fd(con->ssl, con->sock_d);
if (SSL_accept(con->ssl) != 1) {
delete con;
unsigned long errCode;
stringstream errss;
do {
char errbuf[256];
errCode = ERR_get_error();
ERR_error_string_n(errCode, errbuf, 256);
errss << errbuf << endl;
} while (errCode != 0);
throw std::runtime_error(errss.str());
throw std::runtime_error("Could not accept connection (SSL)");
} else {
//
}
return con;
}
TCPClient::TCPClient(void) {
// create TCP socket
sock_d = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sock_d <= 0) {
throw std::runtime_error("Could not create socket.");
}
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
method = SSLv23_client_method();
SSL_library_init();
ctx = SSL_CTX_new(method);
if (ctx == NULL){
ERR_print_errors_fp(stderr);
abort();
}
ssl = SSL_new(ctx);
SSL_set_fd(ssl, sock_d);
}
void TCPClient::connect(std::string address, int port) {
socklen_t sock_size;
sock_size = sizeof(addr);
addr.sin_addr.s_addr = inet_addr(address.c_str());
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
int err;
err = ::connect(sock_d, (struct sockaddr *) &addr, sock_size);
if (err) {
switch (errno) {
case EADDRNOTAVAIL:
throw std::runtime_error("EADDRNOTAVAIL: Could not connect.");
break;
case EAFNOSUPPORT:
throw std::runtime_error("EAFNOSUPPORT: Could not connect.");
break;
case EALREADY:
throw std::runtime_error("EALREADY: Could not connect.");
break;
case EBADF:
throw std::runtime_error("EBADF: Could not connect.");
break;
case ECONNREFUSED:
throw std::runtime_error("ECONNREFUSED: Could not connect.");
break;
case EFAULT:
throw std::runtime_error("EFAULT: Could not connect.");
break;
case EINPROGRESS:
throw std::runtime_error("EINPROGRESS: Could not connect.");
break;
case EINTR:
throw std::runtime_error("EINTR: Could not connect.");
break;
case EISCONN:
throw std::runtime_error("EISCONN: Could not connect.");
break;
case ENETUNREACH:
throw std::runtime_error("ENETUNREACH: Could not connect.");
break;
case ENOTSOCK:
throw std::runtime_error("ENOTSOCK: Could not connect.");
break;
case EPROTOTYPE:
throw std::runtime_error("EPROTOTYPE: Could not connect.");
break;
case ETIMEDOUT:
throw std::runtime_error("ETIMEDOUT: Could not connect.");
break;
case EACCES:
throw std::runtime_error("EACCES: Could not connect.");
break;
case EADDRINUSE:
throw std::runtime_error("EADDRINUSE: Could not connect.");
break;
case ECONNRESET:
throw std::runtime_error("ECONNRESET: Could not connect.");
break;
case EHOSTUNREACH:
throw std::runtime_error("EHOSTUNREACH: Could not connect.");
break;
case EINVAL:
throw std::runtime_error("EINVAL: Could not connect.");
break;
case ENAMETOOLONG:
throw std::runtime_error("ENAMETOOLONG: Could not connect.");
break;
case ENETDOWN:
throw std::runtime_error("ENETDOWN: Could not connect.");
break;
case ENOBUFS:
throw std::runtime_error("ENOBUFS: Could not connect.");
break;
case ENOSR:
throw std::runtime_error("ENOSR: Could not connect.");
break;
case EOPNOTSUPP:
throw std::runtime_error("EOPNOTSUPP: Could not connect.");
break;
default:
throw std::runtime_error("Could not connect");
}
}
int aux = SSL_connect(ssl);
if (aux != 1) {
ERR_print_errors_fp(stderr);
throw std::runtime_error("could not start ssl");
}
}
TCPConnection::~TCPConnection() {
if (ssl) SSL_free(ssl);
}
TCPConnection::TCPConnection() {
socklen_t sock_size = sizeof(addr);
memset((char *) &addr, 0, sock_size);
ssl = NULL;
sock_d = 0;
}
int TCPConnection::getbuffersizes() {
socklen_t optlen = sizeof(sendbuffer);
getsockopt(sock_d, SOL_SOCKET, SO_SNDBUF, &sendbuffer, &optlen);
return sendbuffer;
}
ssize_t TCPConnection::send(char* buffer, size_t length) {
size_t total_sent = 0;
while (total_sent < length) {
ssize_t bytes;
bytes = ::SSL_write(ssl, &buffer[total_sent], length - total_sent);
if (bytes > 0) {
total_sent += bytes;
} else {
if (bytes < 0) {
switch (errno) {
case EBADF:
throw std::runtime_error("(send) EBADF: The socket argument is not a valid file descriptor.");
case ECONNRESET:
throw std::runtime_error("(send) ECONNRESET: A connection was forcibly closed by a peer.");
case EDESTADDRREQ:
throw std::runtime_error("(send) EDESTADDRREQ: The socket is not connection-mode and no peer address is set.");
case EFAULT:
throw std::runtime_error("(send) EFAULT: The buffer parameter can not be accessed.");
case EINTR:
throw std::runtime_error("(send) EINTR: A signal interrupted send() before any data was transmitted.");
case EMSGSIZE:
throw std::runtime_error("(send) EMSGSIZE: The message is too large be sent all at once, as the socket requires.");
case ENOTCONN:
throw std::runtime_error("(send) ENOTCONN: The socket is not connected or otherwise has not had the peer prespecified.");
case ENOTSOCK:
throw std::runtime_error("(send) ENOTSOCK: The socket argument does not refer to a socket.");
case EOPNOTSUPP:
throw std::runtime_error("(send) EOPNOTSUPP: The socket argument is associated with a socket that does not support one or more of the values set in flags.");
case EPIPE:
throw std::runtime_error("(send) EPIPE: The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM, the SIGPIPE signal is generated to the calling process.");
case EACCES:
throw std::runtime_error("(send) EACCES: The calling process does not have the appropriate privileges.");
case EIO:
throw std::runtime_error("(send) EIO: An I/O error occurred while reading from or writing to the file system.");
case ENETDOWN:
throw std::runtime_error("(send) ENETDOWN: The local interface used to reach the destination is down.");
case ENETUNREACH:
throw std::runtime_error("(send) ENETUNREACH: No route to the network is present.");
case ENOBUFS:
throw std::runtime_error("(send) ENOBUFS: Insufficient resources were available in the system to perform the operation.");
case ENOSR:
throw std::runtime_error("(send) ENOSR: There were insufficient STREAMS resources available for the operation to complete.");
default:
throw std::runtime_error("(send) send: Undefined error");
}
} else {
return 0;
}
}
}
return total_sent;
}
ssize_t TCPConnection::recv(char *buffer, size_t length) {
size_t total_received = 0;
while (total_received < length) {
ssize_t bytes;
bytes = ::SSL_read(ssl, &buffer[total_received], length - total_received);
if (bytes > 0) {
total_received += bytes;
} else {
if (bytes < 0) switch (errno) {
case EBADF:
throw std::runtime_error("(recv) EBADF: The argument sockfd is an invalid file descriptor.");
case ECONNREFUSED:
throw std::runtime_error("(recv) ECONNREFUSED: A remote host refused to allow the network connection (typically because it is not running the requested service).");
case EFAULT:
throw std::runtime_error("(recv) EFAULT: The receive buffer pointer(s) point outside the process's address space.");
case EINTR:
throw std::runtime_error("(recv) EINTR: The receive was interrupted by delivery of a signal before any data were available; see signal(7).");
case EINVAL:
throw std::runtime_error("(recv) EINVAL: Invalid argument passed.");
case ENOMEM:
throw std::runtime_error("(recv) ENOMEM: Could not allocate memory for recvmsg().");
case ENOTCONN:
throw std::runtime_error("(recv) ENOTCONN: The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).");
case ENOTSOCK:
throw std::runtime_error("(recv) ENOTSOCK: The file descriptor sockfd does not refer to a socket.");
default:
throw std::runtime_error("(recv) Undefined error");
} else {
return 0;
}
}
}
return total_received;
}