Skip to content

Commit d8f7f8d

Browse files
committed
Fixing issue robbiehanson#116 - connectToHost with empty host string. Other minor bug fixes, and adding more sample projects.
1 parent 92f70c4 commit d8f7f8d

32 files changed

+5250
-440
lines changed

GCD/GCDAsyncSocket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
235235
* Connects to the given host & port, via the optional interface, with an optional timeout.
236236
*
237237
* The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2").
238+
* The host may also be the special strings "localhost" or "loopback" to specify connecting
239+
* to a service on the local machine.
240+
*
238241
* The interface may be a name (e.g. "en1" or "lo0") or the corresponding IP address (e.g. "192.168.4.35").
239242
* The interface may also be used to specify the local port (see below).
240243
*

GCD/GCDAsyncSocket.m

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
12541254
{
12551255
LogTrace();
12561256

1257-
__block BOOL result = YES;
1257+
__block BOOL result = NO;
12581258
__block NSError *err = nil;
12591259

12601260
// CreateSocket Block
@@ -1331,8 +1331,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
13311331

13321332
if (delegate == nil) // Must have delegate set
13331333
{
1334-
result = NO;
1335-
13361334
NSString *msg = @"Attempting to accept without a delegate. Set a delegate first.";
13371335
err = [[self badConfigError:msg] retain];
13381336

@@ -1342,8 +1340,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
13421340

13431341
if (delegateQueue == NULL) // Must have delegate queue set
13441342
{
1345-
result = NO;
1346-
13471343
NSString *msg = @"Attempting to accept without a delegate queue. Set a delegate queue first.";
13481344
err = [[self badConfigError:msg] retain];
13491345

@@ -1356,8 +1352,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
13561352

13571353
if (isIPv4Disabled && isIPv6Disabled) // Must have IPv4 or IPv6 enabled
13581354
{
1359-
result = NO;
1360-
13611355
NSString *msg = @"Both IPv4 and IPv6 have been disabled. Must enable at least one protocol first.";
13621356
err = [[self badConfigError:msg] retain];
13631357

@@ -1367,8 +1361,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
13671361

13681362
if (![self isDisconnected]) // Must be disconnected
13691363
{
1370-
result = NO;
1371-
13721364
NSString *msg = @"Attempting to accept while connected or accepting connections. Disconnect first.";
13731365
err = [[self badConfigError:msg] retain];
13741366

@@ -1389,8 +1381,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
13891381

13901382
if ((interface4 == nil) && (interface6 == nil))
13911383
{
1392-
result = NO;
1393-
13941384
NSString *msg = @"Unknown interface. Specify valid interface by name (e.g. \"en1\") or IP address.";
13951385
err = [[self badParamError:msg] retain];
13961386

@@ -1400,8 +1390,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
14001390

14011391
if (isIPv4Disabled && (interface6 == nil))
14021392
{
1403-
result = NO;
1404-
14051393
NSString *msg = @"IPv4 has been disabled and specified interface doesn't support IPv6.";
14061394
err = [[self badParamError:msg] retain];
14071395

@@ -1411,8 +1399,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
14111399

14121400
if (isIPv6Disabled && (interface4 == nil))
14131401
{
1414-
result = NO;
1415-
14161402
NSString *msg = @"IPv6 has been disabled and specified interface doesn't support IPv4.";
14171403
err = [[self badParamError:msg] retain];
14181404

@@ -1432,8 +1418,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
14321418

14331419
if (socket4FD == SOCKET_NULL)
14341420
{
1435-
result = NO;
1436-
14371421
[pool drain];
14381422
return_from_block;
14391423
}
@@ -1456,8 +1440,6 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
14561440

14571441
if (socket6FD == SOCKET_NULL)
14581442
{
1459-
result = NO;
1460-
14611443
if (socket4FD != SOCKET_NULL)
14621444
{
14631445
close(socket4FD);
@@ -1541,6 +1523,8 @@ - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSErr
15411523
}
15421524

15431525
flags |= kSocketStarted;
1526+
1527+
result = YES;
15441528
[pool drain];
15451529
};
15461530

@@ -1807,14 +1791,26 @@ - (BOOL)connectToHost:(NSString *)host
18071791
{
18081792
LogTrace();
18091793

1810-
__block BOOL result = YES;
1794+
__block BOOL result = NO;
18111795
__block NSError *err = nil;
18121796

18131797
dispatch_block_t block = ^{
18141798
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
18151799

1816-
result = [self preConnectWithInterface:interface error:&err];
1817-
if (!result)
1800+
// Check for problems with host parameter
1801+
1802+
if (host == nil)
1803+
{
1804+
NSString *msg = @"Invalid host parameter (nil). Should be a domain name or IP address string.";
1805+
err = [[self badParamError:msg] retain];
1806+
1807+
[pool drain];
1808+
return_from_block;
1809+
}
1810+
1811+
// Run through standard pre-connect checks
1812+
1813+
if (![self preConnectWithInterface:interface error:&err])
18181814
{
18191815
[err retain];
18201816
[pool drain];
@@ -1846,6 +1842,7 @@ - (BOOL)connectToHost:(NSString *)host
18461842

18471843
[self startConnectTimeout:timeout];
18481844

1845+
result = YES;
18491846
[pool drain];
18501847
};
18511848

@@ -1882,7 +1879,7 @@ - (BOOL)connectToAddress:(NSData *)remoteAddr
18821879
{
18831880
LogTrace();
18841881

1885-
__block BOOL result = YES;
1882+
__block BOOL result = NO;
18861883
__block NSError *err = nil;
18871884

18881885
dispatch_block_t block = ^{
@@ -1945,8 +1942,7 @@ - (BOOL)connectToAddress:(NSData *)remoteAddr
19451942

19461943
// Run through standard pre-connect checks
19471944

1948-
result = [self preConnectWithInterface:interface error:&err];
1949-
if (!result)
1945+
if (![self preConnectWithInterface:interface error:&err])
19501946
{
19511947
[err retain];
19521948
[pool drain];
@@ -1967,6 +1963,7 @@ - (BOOL)connectToAddress:(NSData *)remoteAddr
19671963

19681964
[self startConnectTimeout:timeout];
19691965

1966+
result = YES;
19701967
[pool drain];
19711968
};
19721969

@@ -2163,6 +2160,8 @@ - (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error
21632160

21642161
NSAssert(dispatch_get_current_queue() == socketQueue, @"Must be dispatched on socketQueue");
21652162

2163+
LogVerbose(@"IPv4: %@:%hu", [[self class] hostFromAddress:address4], [[self class] portFromAddress:address4]);
2164+
LogVerbose(@"IPv6: %@:%hu", [[self class] hostFromAddress:address6], [[self class] portFromAddress:address6]);
21662165

21672166
// Determine socket type
21682167

@@ -2532,6 +2531,9 @@ - (void)closeWithError:(NSError *)error
25322531
[sslReadBuffer setLength:0];
25332532
if (sslContext)
25342533
{
2534+
// Getting a linker error here about SSLDisposeContext?
2535+
// You need to add the Security Framework to your application.
2536+
25352537
SSLDisposeContext(sslContext);
25362538
sslContext = NULL;
25372539
}

0 commit comments

Comments
 (0)