source: trunk/macosx/PortChecker.m @ 1031

Last change on this file since 1031 was 1031, checked in by livings124, 16 years ago

fix a mistake in portchecker

File size: 5.1 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2006 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "PortChecker.h"
26#import "transmission.h"
27
28@implementation PortChecker
29
30- (id) initWithDelegate: (id) delegate
31{
32    if ((self = [super init]))
33    {
34        fDelegate = delegate;
35    }
36   
37    return self;
38}
39
40- (port_status_t) status
41{
42    return fStatus;
43}
44
45- (void) probePort: (int) portNumber
46{
47    NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
48                                [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
49                                cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0];
50   
51    NSURLConnection * portProbeConnection;
52    if ((portProbeConnection = [NSURLConnection connectionWithRequest: portProbeRequest delegate: self]))
53        fPortProbeData = [[NSMutableData data] retain];
54    else
55    {
56        NSLog(@"Unable to get port status: failed to initiate connection");
57        [self callBackWithStatus: PORT_STATUS_ERROR];
58    }
59}
60
61- (void) callBackWithStatus: (port_status_t) status
62{
63    fStatus = status;
64   
65    if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
66        [fDelegate portCheckerDidFinishProbing: self];
67}
68
69#pragma mark NSURLConnection delegate methods
70
71- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
72{
73    [fPortProbeData setLength: 0];
74}
75
76- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
77{
78    [fPortProbeData appendData: data];
79}
80
81- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
82{
83    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
84    [self callBackWithStatus: PORT_STATUS_ERROR];
85    [fPortProbeData release];
86}
87
88- (void) connectionDidFinishLoading: (NSURLConnection *) connection
89{
90    NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData
91                                        options: NSXMLDocumentTidyHTML error: nil];
92   
93    if (shieldsUpProbe)
94    {
95        NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil];
96        if ([nodes count] != 1)
97        {
98            NSArray * title = [shieldsUpProbe nodesForXPath: @"/html/head/title" error: nil];
99            // This may happen when we probe twice too quickly
100            if ([title count] != 1 || ![[[title objectAtIndex: 0] stringValue] isEqualToString:
101                                                                    @"NanoProbe System Already In Use"])
102            {
103                NSLog(@"Unable to get port status: invalid (outdated) XPath expression");
104                [[shieldsUpProbe XMLData] writeToFile: @"/tmp/shieldsUpProbe.html" atomically: YES];
105                [self callBackWithStatus: PORT_STATUS_ERROR];
106            }
107        }
108        else
109        {
110            NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
111                                                [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
112           
113            if ([portStatus isEqualToString: @"open"])
114                [self callBackWithStatus: PORT_STATUS_OPEN];
115            else if ([portStatus isEqualToString: @"stealth"])
116                [self callBackWithStatus: PORT_STATUS_STEALTH];
117            else if ([portStatus isEqualToString: @"closed"])
118                [self callBackWithStatus: PORT_STATUS_CLOSED];
119            else {
120                NSLog(@"Unable to get port status: unknown port state");
121                [self callBackWithStatus: PORT_STATUS_ERROR];
122            }
123        }
124    }
125    else
126    {
127        NSLog(@"Unable to get port status: failed to create xml document");
128        [self callBackWithStatus: PORT_STATUS_ERROR];
129    }
130
131    [fPortProbeData release];
132}
133
134@end
Note: See TracBrowser for help on using the repository browser.