2006-09-28 21:14:49 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "PortChecker.h"
|
|
|
|
|
|
|
|
@implementation PortChecker
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (id) initWithDelegate: (id) delegate
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2006-09-28 21:14:49 +00:00
|
|
|
fDelegate = delegate;
|
2006-09-28 21:54:23 +00:00
|
|
|
}
|
2006-09-28 21:14:49 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (port_status_t) status
|
|
|
|
{
|
|
|
|
return fStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) probePort: (int) portNumber
|
|
|
|
{
|
2006-09-28 21:54:23 +00:00
|
|
|
NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
|
|
|
|
[NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
|
|
|
|
cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0];
|
2006-09-28 21:14:49 +00:00
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
NSURLConnection * portProbeConnection;
|
|
|
|
if ((portProbeConnection = [NSURLConnection connectionWithRequest: portProbeRequest delegate: self]))
|
2006-09-28 21:14:49 +00:00
|
|
|
fPortProbeData = [[NSMutableData data] retain];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: failed to initiate connection");
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (void) callBackWithStatus: (port_status_t) status
|
|
|
|
{
|
2006-09-28 21:14:49 +00:00
|
|
|
fStatus = status;
|
|
|
|
|
|
|
|
if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
|
2006-11-19 15:37:57 +00:00
|
|
|
[fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
|
2006-09-28 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark NSURLConnection delegate methods
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
|
2006-09-28 21:14:49 +00:00
|
|
|
{
|
|
|
|
[fPortProbeData setLength: 0];
|
|
|
|
}
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
|
2006-09-28 21:14:49 +00:00
|
|
|
{
|
|
|
|
[fPortProbeData appendData: data];
|
|
|
|
}
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
|
2006-09-28 21:14:49 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
[fPortProbeData release];
|
|
|
|
}
|
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
- (void) connectionDidFinishLoading: (NSURLConnection *) connection
|
2006-09-28 21:14:49 +00:00
|
|
|
{
|
2006-09-28 21:54:23 +00:00
|
|
|
NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData
|
|
|
|
options: NSXMLDocumentTidyHTML error: nil];
|
2006-09-28 21:14:49 +00:00
|
|
|
|
2006-09-28 21:54:23 +00:00
|
|
|
if (shieldsUpProbe)
|
2006-09-28 21:14:49 +00:00
|
|
|
{
|
2006-09-28 21:54:23 +00:00
|
|
|
NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil];
|
2006-09-28 21:14:49 +00:00
|
|
|
if ([nodes count] != 1)
|
|
|
|
{
|
2006-10-24 20:34:13 +00:00
|
|
|
NSArray * title = [shieldsUpProbe nodesForXPath: @"/html/head/title" error: nil];
|
|
|
|
// This may happen when we probe twice too quickly
|
2006-10-25 13:15:47 +00:00
|
|
|
if ([title count] != 1 || ![[[title objectAtIndex: 0] stringValue] isEqualToString:
|
2006-10-24 20:34:13 +00:00
|
|
|
@"NanoProbe System Already In Use"])
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: invalid (outdated) XPath expression");
|
|
|
|
[[shieldsUpProbe XMLData] writeToFile: @"/tmp/shieldsUpProbe.html" atomically: YES];
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
2006-09-28 21:14:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-28 21:54:23 +00:00
|
|
|
NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
|
2006-09-28 21:14:49 +00:00
|
|
|
[[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
|
|
|
|
|
|
|
|
if ([portStatus isEqualToString: @"open"])
|
|
|
|
[self callBackWithStatus: PORT_STATUS_OPEN];
|
|
|
|
else if ([portStatus isEqualToString: @"stealth"])
|
|
|
|
[self callBackWithStatus: PORT_STATUS_STEALTH];
|
|
|
|
else if ([portStatus isEqualToString: @"closed"])
|
|
|
|
[self callBackWithStatus: PORT_STATUS_CLOSED];
|
2006-10-25 13:31:49 +00:00
|
|
|
else
|
|
|
|
{
|
2006-09-28 21:14:49 +00:00
|
|
|
NSLog(@"Unable to get port status: unknown port state");
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-28 21:54:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: failed to create xml document");
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
2006-09-28 21:14:49 +00:00
|
|
|
|
|
|
|
[fPortProbeData release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|