mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-22 11:46:05 +08:00
Added RKPortCheck class for determining if a remote host/port is listening for TCP connections. refs #714
This commit is contained in:
83
Code/Support/RKPortCheck.h
Normal file
83
Code/Support/RKPortCheck.h
Normal file
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// RKPortCheck.h
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 5/10/12.
|
||||
// Copyright (c) 2012 RestKit. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
The RKPortCheck class provides a simple interface for quickly testing
|
||||
the availability of a listening TCP port on a remote host by IP Address or
|
||||
Hostname.
|
||||
*/
|
||||
@interface RKPortCheck : NSObject
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
/// @name Creating a Port Check
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
Initializes the receiver with a given hostname or IP address as a string
|
||||
and a numeric TCP port number.
|
||||
|
||||
@param hostNameOrIPAddress A string containing the hostname or IP address to check.
|
||||
@param port The TCP port on the remote host to check for a listening server on.
|
||||
@return The receiver, initialized with host and port.
|
||||
*/
|
||||
- (id)initWithHost:(NSString *)hostNameOrIPAddress port:(NSUInteger)port;
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
/// @name Accessing Host and Port
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
The hostname or IP address the receiver is checking.
|
||||
*/
|
||||
@property (nonatomic, retain, readonly) NSString *host;
|
||||
|
||||
/**
|
||||
The TCP port to check for a listening server on.
|
||||
*/
|
||||
@property (nonatomic, assign, readonly) NSUInteger port;
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
/// @name Running the Check
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
Runs the check by creating a socket and attempting to connect to the
|
||||
target host and port via TCP. The
|
||||
*/
|
||||
- (void)run;
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
/// @name Inspecting Port Accessibility
|
||||
///-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
Returns a Boolean value indicating if the check has been run.
|
||||
|
||||
@return YES if the check has been run, otherwise NO.
|
||||
*/
|
||||
- (BOOL)hasRun;
|
||||
|
||||
/**
|
||||
Returns a Boolean value indicating if the host and port the receiver checked
|
||||
is open and listening for incoming connections.
|
||||
|
||||
@return YES if the port on the remote host is open, otherwise NO.
|
||||
*/
|
||||
- (BOOL)isOpen;
|
||||
|
||||
/**
|
||||
Returns a Boolean value indicating if the host and port the receiver checked
|
||||
is NOT open and listening for incoming connections.
|
||||
|
||||
@return YES if the port on the remote host is closed, otherwise NO.
|
||||
*/
|
||||
- (BOOL)isClosed;
|
||||
|
||||
@end
|
||||
99
Code/Support/RKPortCheck.m
Normal file
99
Code/Support/RKPortCheck.m
Normal file
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// RKPortCheck.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 5/10/12.
|
||||
// Copyright (c) 2012 RestKit. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RKPortCheck.h"
|
||||
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
@interface RKPortCheck ()
|
||||
@property (nonatomic, assign) struct sockaddr_in remote_saddr;
|
||||
@property (nonatomic, assign, getter = isOpen) BOOL open;
|
||||
@property (nonatomic, assign, getter = hasRun) BOOL run;
|
||||
@end
|
||||
|
||||
@implementation RKPortCheck
|
||||
|
||||
@synthesize host = _host;
|
||||
@synthesize port = _port;
|
||||
@synthesize remote_saddr = _remote_saddr;
|
||||
@synthesize open = _open;
|
||||
@synthesize run = _run;
|
||||
|
||||
- (id)initWithHost:(NSString *)hostNameOrIPAddress port:(NSUInteger)port
|
||||
{
|
||||
self = [self init];
|
||||
if (self) {
|
||||
_run = NO;
|
||||
_host = [hostNameOrIPAddress retain];
|
||||
_port = port;
|
||||
|
||||
struct sockaddr_in sa;
|
||||
char *hostNameOrIPAddressCString = (char *) [hostNameOrIPAddress UTF8String];
|
||||
int result = inet_pton(AF_INET, hostNameOrIPAddressCString, &(sa.sin_addr));
|
||||
if (result != 0) {
|
||||
// IP Address
|
||||
bzero(&_remote_saddr, sizeof(struct sockaddr_in));
|
||||
_remote_saddr.sin_len = sizeof(struct sockaddr_in);
|
||||
_remote_saddr.sin_family = AF_INET;
|
||||
_remote_saddr.sin_port = htons(port);
|
||||
inet_aton(hostNameOrIPAddressCString, &(_remote_saddr.sin_addr));
|
||||
} else {
|
||||
// Hostname
|
||||
struct hostent *hp;
|
||||
if ((hp = gethostbyname(hostNameOrIPAddressCString)) == 0) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
bzero(&_remote_saddr, sizeof(struct sockaddr_in));
|
||||
_remote_saddr.sin_family = AF_INET;
|
||||
_remote_saddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
|
||||
_remote_saddr.sin_port = htons(port);
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_host release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void)run
|
||||
{
|
||||
int sd;
|
||||
_run = YES;
|
||||
|
||||
// Create Internet domain socket
|
||||
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
_open = NO;
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to connect to the port
|
||||
_open = (connect(sd,(struct sockaddr *) &_remote_saddr, sizeof(_remote_saddr)) == 0);
|
||||
|
||||
if (_open) {
|
||||
close(sd);
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isOpen
|
||||
{
|
||||
NSAssert(self.hasRun, @"Cannot determine port availability. Check has not been run.");
|
||||
return _open;
|
||||
}
|
||||
|
||||
- (BOOL)isClosed
|
||||
{
|
||||
return !self.isOpen;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -504,6 +504,10 @@
|
||||
254A62BC14AD544200939BEE /* RKObjectPaginator.m in Sources */ = {isa = PBXBuildFile; fileRef = 254A62B814AD544200939BEE /* RKObjectPaginator.m */; };
|
||||
254A62C014AD591C00939BEE /* RKObjectPaginatorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 254A62BF14AD591C00939BEE /* RKObjectPaginatorTest.m */; };
|
||||
254A62C114AD591C00939BEE /* RKObjectPaginatorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 254A62BF14AD591C00939BEE /* RKObjectPaginatorTest.m */; };
|
||||
2572538D155C543000CB05ED /* RKPortCheck.h in Headers */ = {isa = PBXBuildFile; fileRef = 2572538B155C543000CB05ED /* RKPortCheck.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
2572538E155C543000CB05ED /* RKPortCheck.h in Headers */ = {isa = PBXBuildFile; fileRef = 2572538B155C543000CB05ED /* RKPortCheck.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
2572538F155C543000CB05ED /* RKPortCheck.m in Sources */ = {isa = PBXBuildFile; fileRef = 2572538C155C543000CB05ED /* RKPortCheck.m */; };
|
||||
25725390155C543000CB05ED /* RKPortCheck.m in Sources */ = {isa = PBXBuildFile; fileRef = 2572538C155C543000CB05ED /* RKPortCheck.m */; };
|
||||
257ABAB015112DD500CCAA76 /* NSManagedObjectContext+RKAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 257ABAAE15112DD400CCAA76 /* NSManagedObjectContext+RKAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
257ABAB115112DD500CCAA76 /* NSManagedObjectContext+RKAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 257ABAAE15112DD400CCAA76 /* NSManagedObjectContext+RKAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
257ABAB215112DD500CCAA76 /* NSManagedObjectContext+RKAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 257ABAAF15112DD400CCAA76 /* NSManagedObjectContext+RKAdditions.m */; };
|
||||
@@ -1039,6 +1043,8 @@
|
||||
254A62B714AD544200939BEE /* RKObjectPaginator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKObjectPaginator.h; sourceTree = "<group>"; };
|
||||
254A62B814AD544200939BEE /* RKObjectPaginator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKObjectPaginator.m; sourceTree = "<group>"; };
|
||||
254A62BF14AD591C00939BEE /* RKObjectPaginatorTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKObjectPaginatorTest.m; sourceTree = "<group>"; };
|
||||
2572538B155C543000CB05ED /* RKPortCheck.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKPortCheck.h; sourceTree = "<group>"; };
|
||||
2572538C155C543000CB05ED /* RKPortCheck.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKPortCheck.m; sourceTree = "<group>"; };
|
||||
257ABAAE15112DD400CCAA76 /* NSManagedObjectContext+RKAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectContext+RKAdditions.h"; sourceTree = "<group>"; };
|
||||
257ABAAF15112DD400CCAA76 /* NSManagedObjectContext+RKAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectContext+RKAdditions.m"; sourceTree = "<group>"; };
|
||||
257ABAB41511371C00CCAA76 /* NSManagedObject+RKAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+RKAdditions.h"; sourceTree = "<group>"; };
|
||||
@@ -1516,6 +1522,8 @@
|
||||
252EFB1A14D9A7CB004863C8 /* NSBundle+RKAdditions.m */,
|
||||
252A202B153471380078F8AD /* NSArray+RKAdditions.h */,
|
||||
252A202C153471380078F8AD /* NSArray+RKAdditions.m */,
|
||||
2572538B155C543000CB05ED /* RKPortCheck.h */,
|
||||
2572538C155C543000CB05ED /* RKPortCheck.m */,
|
||||
);
|
||||
path = Support;
|
||||
sourceTree = "<group>";
|
||||
@@ -2225,6 +2233,7 @@
|
||||
257ABAB61511371E00CCAA76 /* NSManagedObject+RKAdditions.h in Headers */,
|
||||
25079C6F151B93DB00266AE7 /* NSEntityDescription+RKAdditions.h in Headers */,
|
||||
252A202D153471380078F8AD /* NSArray+RKAdditions.h in Headers */,
|
||||
2572538D155C543000CB05ED /* RKPortCheck.h in Headers */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -2346,6 +2355,7 @@
|
||||
257ABAB71511371E00CCAA76 /* NSManagedObject+RKAdditions.h in Headers */,
|
||||
25079C70151B93DB00266AE7 /* NSEntityDescription+RKAdditions.h in Headers */,
|
||||
252A20311534714D0078F8AD /* NSArray+RKAdditions.h in Headers */,
|
||||
2572538E155C543000CB05ED /* RKPortCheck.h in Headers */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -2747,6 +2757,7 @@
|
||||
25079C71151B93DB00266AE7 /* NSEntityDescription+RKAdditions.m in Sources */,
|
||||
252A202E153471380078F8AD /* NSArray+RKAdditions.m in Sources */,
|
||||
25C954A715542A47005C9E08 /* RKTestConstants.m in Sources */,
|
||||
2572538F155C543000CB05ED /* RKPortCheck.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -2932,6 +2943,7 @@
|
||||
250B849E152B6F63002581F9 /* RKObjectMappingProvider+CoreData.m in Sources */,
|
||||
252A2030153471470078F8AD /* NSArray+RKAdditions.m in Sources */,
|
||||
25C954A815542A47005C9E08 /* RKTestConstants.m in Sources */,
|
||||
25725390155C543000CB05ED /* RKPortCheck.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user