2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2006-2012 Transmission authors and contributors
|
2007-09-16 01:02:06 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2012-08-13 00:52:04 +00:00
|
|
|
#define CHECKER_URL(port) [NSString stringWithFormat: @"http://portcheck.transmissionbt.com/%ld", port]
|
2009-12-28 20:05:33 +00:00
|
|
|
#define CHECK_FIRE 3.0
|
2007-12-19 20:46:00 +00:00
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@interface PortChecker (Private)
|
|
|
|
|
2009-11-08 06:01:29 +00:00
|
|
|
- (void) startProbe: (NSTimer *) timer;
|
2008-09-06 21:57:13 +00:00
|
|
|
|
2008-09-07 16:08:29 +00:00
|
|
|
- (void) callBackWithStatus: (port_status_t) status;
|
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation PortChecker
|
|
|
|
|
2008-09-07 16:08:29 +00:00
|
|
|
- (id) initForPort: (NSInteger) portNumber delay: (BOOL) delay withDelegate: (id) delegate
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2007-12-18 21:13:36 +00:00
|
|
|
fDelegate = delegate;
|
|
|
|
|
2007-12-18 20:10:23 +00:00
|
|
|
fStatus = PORT_STATUS_CHECKING;
|
|
|
|
|
2012-09-10 02:37:29 +00:00
|
|
|
fTimer = [[NSTimer scheduledTimerWithTimeInterval: CHECK_FIRE target: self selector: @selector(startProbe:) userInfo: [NSNumber numberWithInteger: portNumber] repeats: NO] retain];
|
2009-11-08 06:01:29 +00:00
|
|
|
if (!delay)
|
|
|
|
[fTimer fire];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-12-18 20:02:49 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2007-12-25 19:08:06 +00:00
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
2007-12-25 19:08:06 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[fConnection release];
|
|
|
|
[fPortProbeData release];
|
|
|
|
[super dealloc];
|
2007-12-18 20:02:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (port_status_t) status
|
|
|
|
{
|
|
|
|
return fStatus;
|
|
|
|
}
|
|
|
|
|
2007-12-25 19:14:45 +00:00
|
|
|
- (void) cancelProbe
|
2007-12-18 20:02:49 +00:00
|
|
|
{
|
2007-12-25 19:08:06 +00:00
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
2007-12-25 19:14:45 +00:00
|
|
|
fTimer = nil;
|
|
|
|
|
2007-12-18 20:02:49 +00:00
|
|
|
[fConnection cancel];
|
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
|
|
|
|
{
|
|
|
|
[fPortProbeData setLength: 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
|
|
|
|
{
|
|
|
|
[fPortProbeData appendData: data];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) connectionDidFinishLoading: (NSURLConnection *) connection
|
|
|
|
{
|
2008-07-19 14:32:11 +00:00
|
|
|
NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSUTF8StringEncoding];
|
2012-03-13 02:52:11 +00:00
|
|
|
[fPortProbeData release];
|
2007-12-31 02:17:27 +00:00
|
|
|
fPortProbeData = nil;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-07-19 14:32:11 +00:00
|
|
|
if (probeString)
|
2007-12-31 02:17:27 +00:00
|
|
|
{
|
2008-07-19 14:32:11 +00:00
|
|
|
if ([probeString isEqualToString: @"1"])
|
|
|
|
[self callBackWithStatus: PORT_STATUS_OPEN];
|
|
|
|
else if ([probeString isEqualToString: @"0"])
|
|
|
|
[self callBackWithStatus: PORT_STATUS_CLOSED];
|
2007-12-31 05:48:44 +00:00
|
|
|
else
|
|
|
|
{
|
2009-09-12 02:48:57 +00:00
|
|
|
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
|
2008-06-25 05:01:04 +00:00
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
2007-12-31 05:48:44 +00:00
|
|
|
}
|
2012-03-13 02:52:11 +00:00
|
|
|
[probeString release];
|
2007-12-31 02:17:27 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
2007-12-31 02:17:27 +00:00
|
|
|
{
|
2008-07-19 14:32:11 +00:00
|
|
|
NSLog(@"Unable to get port status: invalid data received");
|
2007-12-31 02:17:27 +00:00
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2008-09-06 21:57:13 +00:00
|
|
|
|
|
|
|
@implementation PortChecker (Private)
|
|
|
|
|
2009-11-08 06:01:29 +00:00
|
|
|
- (void) startProbe: (NSTimer *) timer
|
2008-09-06 21:57:13 +00:00
|
|
|
{
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
2008-09-06 21:57:13 +00:00
|
|
|
fTimer = nil;
|
|
|
|
|
2009-11-08 06:01:29 +00:00
|
|
|
NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: CHECKER_URL([[timer userInfo] integerValue])]
|
2008-12-26 05:57:51 +00:00
|
|
|
cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 15.0];
|
2008-09-06 21:57:13 +00:00
|
|
|
|
|
|
|
if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
|
|
|
|
fPortProbeData = [[NSMutableData alloc] init];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: failed to initiate connection");
|
|
|
|
[self callBackWithStatus: PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-07 16:08:29 +00:00
|
|
|
- (void) callBackWithStatus: (port_status_t) status
|
|
|
|
{
|
|
|
|
fStatus = status;
|
|
|
|
|
|
|
|
if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
|
|
|
|
[fDelegate performSelectorOnMainThread: @selector(portCheckerDidFinishProbing:) withObject: self waitUntilDone: NO];
|
|
|
|
}
|
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@end
|
|
|
|
|