| 1 |
/* |
| 2 |
** CWTCPConnection.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2016-2020 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWTCPConnection.h> |
| 25 |
|
| 26 |
#import <Pantomime/io.h> |
| 27 |
#import <Pantomime/CWConstants.h> |
| 28 |
#import <Pantomime/CWDNSManager.h> |
| 29 |
|
| 30 |
#import <Foundation/NSException.h> |
| 31 |
#import <Foundation/NSRunLoop.h> //test |
| 32 |
#import <Foundation/NSValue.h> |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <errno.h> |
| 37 |
#ifdef __MINGW32__ |
| 38 |
#include <winsock2.h> |
| 39 |
#else |
| 40 |
#include <netinet/in.h> |
| 41 |
#include <sys/ioctl.h> |
| 42 |
#include <sys/socket.h> |
| 43 |
#include <netdb.h> |
| 44 |
#endif |
| 45 |
#include <sys/time.h> |
| 46 |
#include <sys/types.h> |
| 47 |
#include <string.h> |
| 48 |
#include <unistd.h> // For read() and write() and close() |
| 49 |
|
| 50 |
#ifdef MACOSX |
| 51 |
#include <sys/uio.h> // For read() and write() on OS X |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifndef FIONBIO |
| 55 |
#include <sys/filio.h> // For FIONBIO on Solaris |
| 56 |
#endif |
| 57 |
|
| •58 |
#define DEFAULT_TIMEOUT 600 |
| 59 |
|
| 60 |
// |
| 61 |
// |
| 62 |
// |
| 63 |
@interface CWTCPConnection (Private) |
| 64 |
|
| 65 |
- (void) _DNSResolutionCompleted: (NSNotification *) theNotification; |
| 66 |
- (void) _DNSResolutionFailed: (NSNotification *) theNotification; |
| 67 |
|
| 68 |
@end |
| 69 |
|
| 70 |
|
| 71 |
// |
| 72 |
// |
| 73 |
// |
| 74 |
@implementation CWTCPConnection |
| 75 |
|
| 76 |
+ (void) initialize |
| 77 |
{ |
| 78 |
SSL_library_init(); |
| 79 |
SSL_load_error_strings(); |
| 80 |
} |
| 81 |
|
| 82 |
// |
| 83 |
// |
| 84 |
// |
| 85 |
- (id) initWithName: (NSString *) theName |
| 86 |
port: (unsigned int) thePort |
| 87 |
background: (BOOL) theBOOL |
| 88 |
{ |
| 89 |
return [self initWithName: theName |
| 90 |
port: thePort |
| 91 |
connectionTimeout: DEFAULT_TIMEOUT |
| 92 |
readTimeout: DEFAULT_TIMEOUT |
| 93 |
writeTimeout: DEFAULT_TIMEOUT |
| 94 |
background: theBOOL]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// |
| 99 |
// This methods throws an exception if the connection timeout |
| 100 |
// is exhausted and the connection hasn't been established yet. |
| 101 |
// |
| 102 |
- (id) initWithName: (NSString *) theName |
| 103 |
port: (unsigned int) thePort |
| 104 |
connectionTimeout: (unsigned int) theConnectionTimeout |
| 105 |
readTimeout: (unsigned int) theReadTimeout |
| 106 |
writeTimeout: (unsigned int) theWriteTimeout |
| 107 |
background: (BOOL) theBOOL |
| 108 |
{ |
| 109 |
NSArray *addresses; |
| 110 |
|
| 111 |
struct sockaddr_in server; |
| 112 |
#ifdef __MINGW32__ |
| 113 |
u_long nonblock = 1; |
| 114 |
#else |
| 115 |
int nonblock = 1; |
| 116 |
#endif |
| 117 |
|
| 118 |
if (theName == nil || thePort <= 0) |
| 119 |
{ |
| 120 |
AUTORELEASE(self); |
| 121 |
return nil; |
| 122 |
} |
| 123 |
|
| 124 |
self = [super init]; |
| 125 |
if (self) |
| 126 |
{ |
| 127 |
_connectionTimeout = theConnectionTimeout; |
| 128 |
|
| 129 |
ASSIGN(_name, theName); |
| 130 |
_port = thePort; |
| 131 |
|
| 132 |
_dns_resolution_completed = ssl_handshaking = NO; |
| 133 |
_ssl = NULL; |
| 134 |
|
| 135 |
// We get the file descriptor associated to a socket |
| 136 |
_fd = socket(PF_INET, SOCK_STREAM, 0); |
| 137 |
|
| 138 |
if (_fd == -1) |
| 139 |
{ |
| 140 |
AUTORELEASE(self); |
| 141 |
return nil; |
| 142 |
} |
| 143 |
|
| 144 |
[[NSNotificationCenter defaultCenter] |
| 145 |
addObserver: self |
| 146 |
selector: @selector(_DNSResolutionCompleted:) |
| 147 |
name: PantomimeDNSResolutionCompleted |
| 148 |
object: nil]; |
| 149 |
|
| 150 |
[[NSNotificationCenter defaultCenter] |
| 151 |
addObserver: self |
| 152 |
selector: @selector(_DNSResolutionFailed:) |
| 153 |
name: PantomimeDNSResolutionFailed |
| 154 |
object: nil]; |
| 155 |
|
| 156 |
if (!theBOOL) |
| 157 |
{ |
| 158 |
addresses = [[CWDNSManager singleInstance] addressesForName: theName background: NO]; |
| 159 |
|
| 160 |
if (!addresses) |
| 161 |
{ |
| 162 |
safe_close(_fd); |
| 163 |
AUTORELEASE(self); |
| 164 |
return nil; |
| 165 |
} |
| 166 |
|
| 167 |
_dns_resolution_completed = YES; |
| 168 |
|
| 169 |
server.sin_family = AF_INET; |
| 170 |
server.sin_addr.s_addr = [(NSNumber *)[addresses objectAtIndex: 0] unsignedIntValue]; |
| 171 |
server.sin_port = htons(thePort); |
| 172 |
|
| 173 |
// If we don't connect in background, we must try to connect right away |
| 174 |
// and return on any errors. |
| 175 |
if (connect(_fd, (struct sockaddr *)&server, sizeof(server)) != 0) |
| 176 |
{ |
| 177 |
AUTORELEASE(self); |
| 178 |
return nil; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// We set the non-blocking I/O flag on _fd |
| 183 |
#ifdef __MINGW32__ |
| 184 |
if (ioctlsocket(_fd, FIONBIO, &nonblock) == -1) |
| 185 |
#else |
| 186 |
if (ioctl(_fd, FIONBIO, &nonblock) == -1) |
| 187 |
#endif |
| 188 |
{ |
| 189 |
safe_close(_fd); |
| 190 |
AUTORELEASE(self); |
| 191 |
return nil; |
| 192 |
} |
| 193 |
|
| 194 |
if (theBOOL) [[CWDNSManager singleInstance] addressesForName: theName background: YES]; |
| 195 |
} |
| 196 |
return self; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
// |
| 201 |
// |
| 202 |
// |
| 203 |
- (void) dealloc |
| 204 |
{ |
| 205 |
//NSLog(@"TCPConnection: -dealloc"); |
| 206 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
| 207 |
RELEASE(_name); |
| 208 |
|
| 209 |
if (_ssl) |
| 210 |
{ |
| 211 |
SSL_free(_ssl); |
| 212 |
} |
| 213 |
|
| 214 |
if (_ctx) |
| 215 |
{ |
| 216 |
SSL_CTX_free(_ctx); |
| 217 |
} |
| 218 |
|
| 219 |
[super dealloc]; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
// |
| 224 |
// This method is used to return the file descriptor |
| 225 |
// associated with our socket. |
| 226 |
// |
| 227 |
- (int) fd |
| 228 |
{ |
| 229 |
return _fd; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
// |
| 234 |
// |
| 235 |
// |
| 236 |
- (BOOL) isConnected |
| 237 |
{ |
| 238 |
struct timeval timeout; |
| 239 |
fd_set fdset; |
| 240 |
int value; |
| 241 |
|
| 242 |
if (!_dns_resolution_completed) |
| 243 |
{ |
| 244 |
return NO; |
| 245 |
} |
| 246 |
|
| 247 |
// We remove all descriptors from the set fdset |
| 248 |
FD_ZERO(&fdset); |
| 249 |
|
| 250 |
// We add the descriptor _fd to the fdset set |
| 251 |
FD_SET(_fd, &fdset); |
| 252 |
|
| 253 |
// We set the timeout for our connection |
| 254 |
timeout.tv_sec = 0; |
| 255 |
timeout.tv_usec = 1; |
| 256 |
|
| 257 |
value = select(_fd + 1, NULL, &fdset, NULL, &timeout); |
| 258 |
|
| 259 |
// An error occured.. |
| 260 |
if (value == -1) |
| 261 |
{ |
| 262 |
return NO; |
| 263 |
} |
| 264 |
// Our fdset has ready descriptors (for writability) |
| 265 |
else if (value > 0) |
| 266 |
{ |
| 267 |
#ifdef __MINGW32__ |
| 268 |
int size; |
| 269 |
#else |
| 270 |
socklen_t size; |
| 271 |
#endif |
| 272 |
int error; |
| 273 |
|
| 274 |
size = sizeof(error); |
| 275 |
|
| 276 |
// We get the options at the socket level (so we use SOL_SOCKET) |
| 277 |
// returns -1 on error, 0 on success |
| 278 |
if (getsockopt(_fd, SOL_SOCKET, SO_ERROR, &error, &size) == -1) |
| 279 |
{ |
| 280 |
return NO; |
| 281 |
} |
| 282 |
|
| 283 |
if (error != 0) |
| 284 |
{ |
| 285 |
#warning handle right way in CWService tick if the port was incorrectly specified |
| 286 |
return NO; |
| 287 |
} |
| 288 |
} |
| 289 |
// select() has returned 0 which means that the timeout has expired. |
| 290 |
else |
| 291 |
{ |
| 292 |
return NO; |
| 293 |
} |
| 294 |
|
| 295 |
return YES; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
// |
| 300 |
// |
| 301 |
// |
| 302 |
- (BOOL) isSSL |
| 303 |
{ |
| 304 |
return (_ssl ? YES : NO); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
// |
| 309 |
// other methods |
| 310 |
// |
| 311 |
- (void) close |
| 312 |
{ |
| 313 |
//NSLog(@"TCPConnection: -close"); |
| 314 |
|
| 315 |
if (_ssl) |
| 316 |
{ |
| 317 |
SSL_shutdown(_ssl); |
| 318 |
} |
| 319 |
|
| 320 |
safe_close(_fd); |
| 321 |
_fd = -1; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// |
| 326 |
// |
| 327 |
// |
| 328 |
- (ssize_t) read: (char *) buf |
| 329 |
length: (size_t) len |
| 330 |
{ |
| 331 |
if (ssl_handshaking) |
| 332 |
{ |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
if (_ssl) |
| 337 |
{ |
| 338 |
return SSL_read(_ssl, buf, (int)len); |
| 339 |
} |
| 340 |
|
| 341 |
return safe_recv(_fd, buf, len, 0); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
// |
| 346 |
// |
| 347 |
// |
| 348 |
- (ssize_t) write: (char *) buf |
| 349 |
length: (size_t) len |
| 350 |
{ |
| 351 |
if (ssl_handshaking) |
| 352 |
{ |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
if (_ssl) |
| 357 |
{ |
| 358 |
return SSL_write(_ssl, buf, (int)len); |
| 359 |
} |
| 360 |
|
| 361 |
return send(_fd, buf, len, 0); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
// |
| 366 |
// 0 -> success |
| 367 |
// -1 -> |
| 368 |
// -2 -> handshake error |
| 369 |
// |
| 370 |
- (int) startSSL |
| 371 |
{ |
| 372 |
int ret; |
| 373 |
|
| 374 |
// For now, we do not verify the certificates... |
| 375 |
_ctx = SSL_CTX_new(SSLv23_client_method()); |
| 376 |
SSL_CTX_set_verify(_ctx, SSL_VERIFY_NONE, NULL); |
| 377 |
SSL_CTX_set_mode(_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); |
| 378 |
|
| 379 |
// We then connect the SSL socket |
| 380 |
_ssl = SSL_new(_ctx); |
| 381 |
SSL_set_fd(_ssl, _fd); |
| 382 |
ret = SSL_connect(_ssl); |
| 383 |
|
| 384 |
if (ret != 1) |
| 385 |
{ |
| 386 |
int rc; |
| 387 |
|
| 388 |
rc = SSL_get_error(_ssl, ret); |
| 389 |
|
| 390 |
if ((rc != SSL_ERROR_WANT_READ) && (rc != SSL_ERROR_WANT_WRITE)) |
| 391 |
{ |
| 392 |
// SSL handshake error... |
| 393 |
//NSLog(@"SSL handshake error."); |
| 394 |
return -2; |
| 395 |
} |
| 396 |
else |
| 397 |
{ |
| 398 |
NSDate *limit; |
| 399 |
|
| 400 |
//NSLog(@"SSL handshaking... %d", rc); |
| 401 |
ssl_handshaking = YES; |
| 402 |
limit = [[NSDate alloc] initWithTimeIntervalSinceNow: DEFAULT_TIMEOUT]; |
| 403 |
|
| 404 |
while ((rc == SSL_ERROR_WANT_READ || rc == SSL_ERROR_WANT_WRITE) |
| 405 |
&& [limit timeIntervalSinceNow] > 0.0) |
| 406 |
{ |
| 407 |
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]]; |
| 408 |
ret = SSL_connect(_ssl); |
| 409 |
//NSLog(@"ret = %d", ret); |
| 410 |
if (ret != 1) |
| 411 |
{ |
| 412 |
//int e = errno; |
| 413 |
rc = SSL_get_error(_ssl, ret); |
| 414 |
//NSLog(@"%s errno = %s", ERR_error_string(rc, NULL), strerror(e)); |
| 415 |
} |
| 416 |
else |
| 417 |
{ |
| 418 |
rc = SSL_ERROR_NONE; |
| 419 |
} |
| 420 |
|
| 421 |
//NSLog(@"rc = %d", rc); |
| 422 |
} |
| 423 |
|
| 424 |
RELEASE(limit); |
| 425 |
|
| 426 |
if (rc != SSL_ERROR_NONE) |
| 427 |
{ |
| 428 |
//NSLog(@"ERROR DURING HANDSHAKING..."); |
| 429 |
//NSLog(@"unable to make SSL connection: %s", ERR_error_string(rc, NULL)); |
| 430 |
//ERR_print_errors_fp(stderr); |
| 431 |
ssl_handshaking = NO; |
| 432 |
SSL_free(_ssl); |
| 433 |
_ssl = NULL; |
| 434 |
return -2; |
| 435 |
} |
| 436 |
|
| 437 |
// We are done with handshaking |
| 438 |
ssl_handshaking = NO; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
// Everything went all right, let's tell our caller. |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
@end |
| 447 |
|
| 448 |
// |
| 449 |
// |
| 450 |
// |
| 451 |
@implementation CWTCPConnection (Private) |
| 452 |
|
| 453 |
- (void) _DNSResolutionCompleted: (NSNotification *) theNotification |
| 454 |
{ |
| 455 |
struct sockaddr_in server; |
| 456 |
|
| 457 |
if (![[[theNotification userInfo] objectForKey: @"Name"] isEqualToString: _name]) |
| 458 |
{ |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
NSDebugLog(@"DNS resolution completed for name |%@|", [[theNotification userInfo] objectForKey: @"Name"]); |
| 463 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
| 464 |
_dns_resolution_completed = YES; |
| 465 |
|
| 466 |
server.sin_family = AF_INET; |
| 467 |
server.sin_addr.s_addr = [[[theNotification userInfo] objectForKey: @"Address"] unsignedIntValue]; |
| 468 |
server.sin_port = htons(_port); |
| 469 |
|
| 470 |
// We initiate our connection to the socket |
| 471 |
if (connect(_fd, (struct sockaddr *)&server, sizeof(server)) == -1) |
| 472 |
{ |
| 473 |
#ifdef __MINGW32__ |
| 474 |
if (WSAGetLastError() == WSAEWOULDBLOCK) |
| 475 |
#else |
| 476 |
if (errno == EINPROGRESS) |
| 477 |
#endif |
| 478 |
{ |
| 479 |
return; |
| 480 |
} // if ( errno == EINPROGRESS ) ... |
| 481 |
else |
| 482 |
{ |
| 483 |
NSLog(@"Failed to connect asynchronously."); |
| 484 |
safe_close(_fd); |
| 485 |
} |
| 486 |
} // if ( connect(...) ) |
| 487 |
} |
| 488 |
|
| 489 |
// |
| 490 |
// |
| 491 |
// |
| 492 |
- (void) _DNSResolutionFailed: (NSNotification *) theNotification |
| 493 |
{ |
| 494 |
NSDebugLog(@"DNS resolution failed!"); |
| 495 |
|
| 496 |
_dns_resolution_completed = YES; |
| 497 |
safe_close(_fd); |
| 498 |
|
| 499 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
| 500 |
} |
| 501 |
|
| 502 |
@end |