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
|
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "SocketCore.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
#include "message.h"
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
#include <errno.h>
SocketCore::SocketCore():sockfd(-1) {
init();
}
SocketCore::SocketCore(int sockfd):sockfd(sockfd) {
init();
}
void SocketCore::init() {
use = 1;
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(int 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, strerror(errno));
}
socklen_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, strerror(errno));
}
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, strerror(errno));
}
if(listen(sockfd, 1) == -1) {
throw new DlAbortEx(EX_SOCKET_LISTEN, strerror(errno));
}
}
SocketCore* SocketCore::acceptConnection() const {
struct sockaddr_in sockaddr;
socklen_t len = sizeof(sockaddr);
memset((char*)&sockaddr, 0, sizeof(sockaddr));
int fd;
if((fd = accept(sockfd, (struct sockaddr*)&sockaddr, &len)) == -1) {
throw new DlAbortEx(EX_SOCKET_ACCEPT, strerror(errno));
}
SocketCore* s = new SocketCore(fd);
return s;
}
void SocketCore::getAddrInfo(pair<string, int>& 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, strerror(errno));
}
addrinfo.first = inet_ntoa(listenaddr.sin_addr);
addrinfo.second = ntohs(listenaddr.sin_port);
}
void SocketCore::getPeerInfo(pair<string, int>& peerinfo) const {
struct sockaddr_in peerin;
memset(&peerin, 0, sizeof(peerin));
int len = sizeof(peerin);
if(getpeername(sockfd, (struct sockaddr*)&peerin, (socklen_t*)&len) < 0) {
throw new DlAbortEx(EX_SOCKET_GET_PEER, strerror(errno));
}
peerinfo.first = inet_ntoa(peerin.sin_addr);
peerinfo.second = ntohs(peerin.sin_port);
}
void SocketCore::establishConnection(const string& host, int port) {
closeConnection();
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno));
}
socklen_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, strerror(errno));
}
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;
int 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
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && errno != EINPROGRESS) {
throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), strerror(errno));
}
}
void SocketCore::setBlockingMode() const {
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags&~O_NONBLOCK);
}
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) {
close(sockfd);
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(int timeout) const {
fd_set fds;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
int r = select(sockfd+1, NULL, &fds, NULL, &tv);
if(r == 1) {
return true;
} else if(r == 0) {
// time out
return false;
} else {
if(errno == EINPROGRESS) {
return false;
} else {
throw new DlRetryEx(EX_SOCKET_CHECK_WRITABLE, strerror(errno));
}
}
}
bool SocketCore::isReadable(int timeout) const {
#ifdef HAVE_LIBGNUTLS
if(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;
int r = select(sockfd+1, &fds, NULL, NULL, &tv);
if(r == 1) {
return true;
} else if(r == 0) {
// time out
return false;
} else {
if(errno == EINPROGRESS) {
return false;
} else {
throw new DlRetryEx(EX_SOCKET_CHECK_READABLE, strerror(errno));
}
}
}
void SocketCore::writeData(const char* data, int len, int timeout) {
int ret = 0;
if(!secure && (ret = send(sockfd, data, (size_t)len, 0)) != len
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
|| secure && (ret = SSL_write(ssl, data, len)) != len
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
|| secure && (ret = gnutls_record_send(sslSession, data, len)) != len
#endif // HAVE_LIBGNUTLS
) {
const char* errorMsg;
#ifdef HAVE_LIBGNUTLS
if(secure) {
errorMsg = gnutls_strerror(ret);
} else {
errorMsg = strerror(errno);
}
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
throw new DlRetryEx(EX_SOCKET_SEND, errorMsg);
}
}
void SocketCore::readData(char* data, int& len, int timeout) {
int ret = 0;
if(!secure && (ret = recv(sockfd, data, (size_t)len, 0)) < 0
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
|| secure && (ret = SSL_read(ssl, data, len)) < 0
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
|| secure && (ret = gnutlsRecv(data, len)) < 0
#endif // HAVE_LIBGNUTLS
) {
const char* errorMsg;
#ifdef HAVE_LIBGNUTLS
if(secure) {
errorMsg = gnutls_strerror(ret);
} else {
errorMsg = strerror(errno);
}
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
throw new DlRetryEx(EX_SOCKET_RECV, errorMsg);
}
len = ret;
}
void SocketCore::peekData(char* data, int& len, int timeout) {
int ret = 0;
if(!secure && (ret = recv(sockfd, data, (size_t)len, MSG_PEEK)) < 0
#ifdef HAVE_LIBSSL
// for SSL
// TODO handling len == 0 case required
|| secure && (ret = SSL_peek(ssl, data, len)) < 0
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
|| secure && (ret = gnutlsPeek(data, len)) < 0
#endif // HAVE_LIBGNUTLS
) {
const char* errorMsg;
#ifdef HAVE_LIBGNUTLS
if(secure) {
errorMsg = gnutls_strerror(ret);
} else {
errorMsg = strerror(errno);
}
#else // HAVE_LIBGNUTLS
errorMsg = strerror(errno);
#endif
throw new DlRetryEx(EX_SOCKET_PEEK, errorMsg);
}
len = ret;
}
#ifdef HAVE_LIBGNUTLS
int SocketCore::shiftPeekData(char* data, int len) {
if(peekBufLength <= len) {
memcpy(data, peekBuf, peekBufLength);
int 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, int 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;
}
int SocketCore::gnutlsRecv(char* data, int len) {
int plen = shiftPeekData(data, len);
if(plen < len) {
int 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;
}
}
int SocketCore::gnutlsPeek(char* data, int len) {
if(peekBufLength >= len) {
memcpy(data, peekBuf, len);
return len;
} else {
memcpy(data, peekBuf, peekBufLength);
int 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);
}
SSL_CTX_set_mode(sslCtx, SSL_MODE_AUTO_RETRY);
ssl = SSL_new(sslCtx);
if(ssl == NULL) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE);
}
if(SSL_set_fd(ssl, sockfd) == 0) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE);
}
// TODO handling return value == 0 case required
if(SSL_connect(ssl) <= 0) {
throw new DlAbortEx(EX_SSL_INIT_FAILURE);
}
secure = true;
}
#endif // HAVE_LIBSSL
#ifdef HAVE_LIBGNUTLS
if(!secure) {
const int 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);
int ret = gnutls_handshake(sslSession);
if(ret < 0) {
throw new DlAbortEx(gnutls_strerror(ret));
}
secure = true;
}
#endif // HAVE_LIBGNUTLS
}
|