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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "SocketCore.h"
#include "message.h"
#include "a2netcompat.h"
#include "a2time.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifndef __MINGW32__
# define SOCKET_ERRNO (errno)
#else
# define SOCKET_ERRNO (WSAGetLastError())
#endif // __MINGW32__
SocketCore::SocketCore():sockfd(-1) {
init();
}
SocketCore::SocketCore(int32_t sockfd):sockfd(sockfd) {
init();
}
void SocketCore::init()
{
use = 1;
blocking = true;
secure = false;
#ifdef HAVE_LIBSSL
// for SSL
sslCtx = NULL;
ssl = NULL;
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
sslSession = NULL;
sslXcred = NULL;
peekBufMax = 4096;
peekBuf = new char[peekBufMax];
peekBufLength = 0;
#endif //HAVE_LIBGNUTLS
}
SocketCore::~SocketCore() {
closeConnection();
#ifdef HAVE_LIBGNUTLS
delete [] peekBuf;
#endif // HAVE_LIBGNUTLS
}
void SocketCore::beginListen(int32_t port)
{
closeConnection();
//sockfd = socket(AF_UNSPEC, SOCK_STREAM, PF_UNSPEC);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
throw new DlAbortEx(EX_SOCKET_OPEN, errorMsg());
}
SOCKOPT_T sockopt = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
#ifdef __MINGW32__
::closesocket(sockfd);
#else
close(sockfd);
#endif // __MINGW32__
sockfd = -1;
throw new DlAbortEx(EX_SOCKET_SET_OPT, errorMsg());
}
struct sockaddr_in sockaddr;
memset((char*)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = INADDR_ANY;
sockaddr.sin_port = htons(port);
if(bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == -1) {
throw new DlAbortEx(EX_SOCKET_BIND, errorMsg());
}
if(listen(sockfd, 1) == -1) {
throw new DlAbortEx(EX_SOCKET_LISTEN, errorMsg());
}
setNonBlockingMode();
}
SocketCore* SocketCore::acceptConnection() const
{
struct sockaddr_in sockaddr;
socklen_t len = sizeof(sockaddr);
memset((char*)&sockaddr, 0, sizeof(sockaddr));
int32_t fd;
if((fd = accept(sockfd, (struct sockaddr*)&sockaddr, &len)) == -1) {
throw new DlAbortEx(EX_SOCKET_ACCEPT, errorMsg());
}
SocketCore* s = new SocketCore(fd);
return s;
}
void SocketCore::getAddrInfo(pair<string, int32_t>& addrinfo) const
{
struct sockaddr_in listenaddr;
memset((char*)&listenaddr, 0, sizeof(listenaddr));
socklen_t len = sizeof(listenaddr);
if(getsockname(sockfd, (struct sockaddr*)&listenaddr, &len) == -1) {
throw new DlAbortEx(EX_SOCKET_GET_NAME, errorMsg());
}
addrinfo.first = inet_ntoa(listenaddr.sin_addr);
addrinfo.second = ntohs(listenaddr.sin_port);
}
void SocketCore::getPeerInfo(pair<string, int32_t>& peerinfo) const
{
struct sockaddr_in peerin;
memset(&peerin, 0, sizeof(peerin));
int32_t len = sizeof(peerin);
if(getpeername(sockfd, (struct sockaddr*)&peerin, (socklen_t*)&len) < 0) {
throw new DlAbortEx(EX_SOCKET_GET_PEER, errorMsg());
}
peerinfo.first = inet_ntoa(peerin.sin_addr);
peerinfo.second = ntohs(peerin.sin_port);
}
void SocketCore::establishConnection(const string& host, int32_t port)
{
closeConnection();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
throw new DlAbortEx(EX_SOCKET_OPEN, errorMsg());
}
SOCKOPT_T sockopt = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
close(sockfd);
sockfd = -1;
throw new DlAbortEx(EX_SOCKET_SET_OPT, errorMsg());
}
struct sockaddr_in sockaddr;
memset((char*)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(port);
if(inet_aton(host.c_str(), &sockaddr.sin_addr)) {
// ok
} else {
struct addrinfo ai;
memset((char*)&ai, 0, sizeof(ai));
ai.ai_flags = 0;
ai.ai_family = PF_INET;
ai.ai_socktype = SOCK_STREAM;
ai.ai_protocol = 0;
struct addrinfo* res;
int32_t ec;
if((ec = getaddrinfo(host.c_str(), NULL, &ai, &res)) != 0) {
throw new DlAbortEx(EX_RESOLVE_HOSTNAME,
host.c_str(), gai_strerror(ec));
}
sockaddr.sin_addr = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
freeaddrinfo(res);
}
// make socket non-blocking mode
setNonBlockingMode();
if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && SOCKET_ERRNO !=
#ifndef __MINGW32__
EINPROGRESS
#else
WSAEWOULDBLOCK
#endif // __MINGW32__
) {
throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), errorMsg());
}
}
void SocketCore::setNonBlockingMode()
{
#ifdef __MINGW32__
static u_long flag = 1;
if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
throw new DlAbortEx(EX_SOCKET_NONBLOCKING, errorMsg());
}
#else
int32_t flags = fcntl(sockfd, F_GETFL, 0);
// TODO add error handling
fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
#endif // __MINGW32__
blocking = false;
}
void SocketCore::setBlockingMode()
{
#ifdef __MINGW32__
static u_long flag = 0;
if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
throw new DlAbortEx(EX_SOCKET_BLOCKING, errorMsg());
}
#else
int32_t flags = fcntl(sockfd, F_GETFL, 0);
// TODO add error handling
fcntl(sockfd, F_SETFL, flags&(~O_NONBLOCK));
#endif // __MINGW32__
blocking = true;
}
void SocketCore::closeConnection()
{
#ifdef HAVE_LIBSSL
// for SSL
if(secure) {
SSL_shutdown(ssl);
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if(secure) {
gnutls_bye(sslSession, GNUTLS_SHUT_RDWR);
}
#endif // HAVE_LIBGNUTLS
if(sockfd != -1) {
#ifdef __MINGW32__
::closesocket(sockfd);
#else
close(sockfd);
#endif // __MINGW32__
sockfd = -1;
}
#ifdef HAVE_LIBSSL
// for SSL
if(secure) {
SSL_free(ssl);
SSL_CTX_free(sslCtx);
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if(secure) {
gnutls_deinit(sslSession);
gnutls_certificate_free_credentials(sslXcred);
}
#endif // HAVE_LIBGNUTLS
}
bool SocketCore::isWritable(int32_t timeout) const
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
int32_t r = select(sockfd+1, NULL, &fds, NULL, &tv);
if(r == 1) {
return true;
} else if(r == 0) {
// time out
return false;
} else {
if(SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EINTR) {
return false;
} else {
throw new DlRetryEx(EX_SOCKET_CHECK_WRITABLE, errorMsg());
}
}
}
bool SocketCore::isReadable(int32_t timeout) const
{
#ifdef HAVE_LIBGNUTLS
if(secure && peekBufLength > 0) {
return true;
}
#endif // HAVE_LIBGNUTLS
fd_set fds;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
int32_t r = select(sockfd+1, &fds, NULL, NULL, &tv);
if(r == 1) {
return true;
} else if(r == 0) {
// time out
return false;
} else {
if(SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EINTR) {
return false;
} else {
throw new DlRetryEx(EX_SOCKET_CHECK_READABLE, errorMsg());
}
}
}
void SocketCore::writeData(const char* data, int32_t len)
{
int32_t ret = 0;
if(!secure) {
if((ret = send(sockfd, data, (size_t)len, 0)) != len) {
throw new DlRetryEx(EX_SOCKET_SEND, errorMsg());
}
} else {
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
if((ret = SSL_write(ssl, data, len)) != len) {
throw new DlRetryEx(EX_SOCKET_SEND, ERR_error_string(ERR_get_error(), NULL));
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if((ret = gnutls_record_send(sslSession, data, len)) != len) {
throw new DlRetryEx(EX_SOCKET_SEND, gnutls_strerror(ret));
}
#endif // HAVE_LIBGNUTLS
}
}
void SocketCore::readData(char* data, int32_t& len)
{
int32_t ret = 0;
if(!secure) {
if ((ret = recv(sockfd, data, (size_t)len, 0)) < 0) {
throw new DlRetryEx(EX_SOCKET_RECV, errorMsg());
}
} else {
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
if ((ret = SSL_read(ssl, data, len)) < 0) {
throw new DlRetryEx(EX_SOCKET_RECV, ERR_error_string(ERR_get_error(), NULL));
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if ((ret = gnutlsRecv(data, len)) < 0) {
throw new DlRetryEx(EX_SOCKET_RECV, gnutls_strerror(ret));
}
#endif // HAVE_LIBGNUTLS
}
len = ret;
}
void SocketCore::peekData(char* data, int32_t& len)
{
int32_t ret = 0;
if(!secure) {
if ((ret = recv(sockfd, data, (size_t)len, MSG_PEEK)) < 0) {
throw new DlRetryEx(EX_SOCKET_PEEK, errorMsg());
}
} else {
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
if ((ret = SSL_peek(ssl, data, len)) < 0) {
throw new DlRetryEx(EX_SOCKET_PEEK, ERR_error_string(ERR_get_error(), NULL));
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if ((ret = gnutlsPeek(data, len)) < 0) {
throw new DlRetryEx(EX_SOCKET_PEEK, gnutls_strerror(ret));
}
#endif // HAVE_LIBGNUTLS
}
len = ret;
}
#ifdef HAVE_LIBGNUTLS
int32_t SocketCore::shiftPeekData(char* data, int32_t len)
{
if(peekBufLength <= len) {
memcpy(data, peekBuf, peekBufLength);
int32_t ret = peekBufLength;
peekBufLength = 0;
return ret;
} else {
memcpy(data, peekBuf, len);
char* temp = new char[peekBufMax];
memcpy(temp, peekBuf+len, peekBufLength-len);
delete [] peekBuf;
peekBuf = temp;
peekBufLength -= len;
return len;
}
}
void SocketCore::addPeekData(char* data, int32_t len)
{
if(peekBufLength+len > peekBufMax) {
char* temp = new char[peekBufMax+len];
memcpy(temp, peekBuf, peekBufLength);
delete [] peekBuf;
peekBuf = temp;
peekBufMax = peekBufLength+len;
}
memcpy(peekBuf+peekBufLength, data, len);
peekBufLength += len;
}
int32_t SocketCore::gnutlsRecv(char* data, int32_t len)
{
int32_t plen = shiftPeekData(data, len);
if(plen < len) {
int32_t ret = gnutls_record_recv(sslSession, data+plen, len-plen);
if(ret < 0) {
throw new DlRetryEx(EX_SOCKET_RECV, gnutls_strerror(ret));
}
return plen+ret;
} else {
return plen;
}
}
int32_t SocketCore::gnutlsPeek(char* data, int32_t len)
{
if(peekBufLength >= len) {
memcpy(data, peekBuf, len);
return len;
} else {
memcpy(data, peekBuf, peekBufLength);
int32_t ret = gnutls_record_recv(sslSession, data+peekBufLength, len-peekBufLength);
if(ret < 0) {
throw new DlRetryEx(EX_SOCKET_PEEK, gnutls_strerror(ret));
}
addPeekData(data+peekBufLength, ret);
return peekBufLength;
}
}
#endif // HAVE_LIBGNUTLS
void SocketCore::initiateSecureConnection()
{
#ifdef HAVE_LIBSSL
// for SSL
if(!secure) {
sslCtx = SSL_CTX_new(SSLv23_client_method());
if(sslCtx == NULL) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
}
SSL_CTX_set_mode(sslCtx, SSL_MODE_AUTO_RETRY);
ssl = SSL_new(sslCtx);
if(ssl == NULL) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
}
if(SSL_set_fd(ssl, sockfd) == 0) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
}
// TODO handling return value == 0 case required
int e = SSL_connect(ssl);
if (e <= 0) {
int ssl_error = SSL_get_error(ssl, e);
switch(ssl_error) {
case SSL_ERROR_NONE:
break;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_X509_LOOKUP:
case SSL_ERROR_ZERO_RETURN:
if (blocking) {
throw new DlAbortEx(EX_SSL_CONNECT_ERROR, ssl_error);
}
break;
case SSL_ERROR_SYSCALL:
throw new DlAbortEx(EX_SSL_IO_ERROR);
case SSL_ERROR_SSL:
throw new DlAbortEx(EX_SSL_PROTOCOL_ERROR);
default:
throw new DlAbortEx(EX_SSL_UNKNOWN_ERROR, ssl_error);
}
}
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if(!secure) {
const int32_t cert_type_priority[3] = { GNUTLS_CRT_X509,
GNUTLS_CRT_OPENPGP, 0
};
// while we do not support X509 certificate, most web servers require
// X509 stuff.
gnutls_certificate_allocate_credentials (&sslXcred);
gnutls_init(&sslSession, GNUTLS_CLIENT);
gnutls_set_default_priority(sslSession);
gnutls_kx_set_priority(sslSession, cert_type_priority);
// put the x509 credentials to the current session
gnutls_credentials_set(sslSession, GNUTLS_CRD_CERTIFICATE, sslXcred);
gnutls_transport_set_ptr(sslSession, (gnutls_transport_ptr_t)sockfd);
int32_t ret = gnutls_handshake(sslSession);
if(ret < 0) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE, gnutls_strerror(ret));
}
}
#endif // HAVE_LIBGNUTLS
secure = true;
}
/* static */ int SocketCore::error()
{
return SOCKET_ERRNO;
}
/* static */ const char *SocketCore::errorMsg()
{
return errorMsg(SOCKET_ERRNO);
}
/* static */ const char *SocketCore::errorMsg(const int err)
{
#ifndef __MINGW32__
return strerror(err);
#else
static char buf[256];
if (FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &buf,
sizeof(buf),
NULL
) == 0) {
snprintf(buf, sizeof(buf), EX_SOCKET_UNKNOWN_ERROR, err, err);
}
return buf;
#endif // __MINGW32__
}
|