source: trunk/macosx/PortChecker.m @ 1073

Last change on this file since 1073 was 1073, checked in by livings124, 15 years ago

get rid of some warnings when compiling

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#import "PrefsController.h"
28
29@implementation PortChecker
30
31- (id) initWithDelegate: (id) delegate
32{
33    if ((self = [super init]))
34    {
35        fDelegate = delegate;
36    }
37   
38    return self;
39}
40
41- (port_status_t) status
42{
43    return fStatus;
44}
45
46- (void) probePort: (int) portNumber
47{
48    NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
49                                [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
50                                cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0];
51   
52    NSURLConnection * portProbeConnection;
53    if ((portProbeConnection = [NSURLConnection connectionWithRequest: portProbeRequest delegate: self]))
54        fPortProbeData = [[NSMutableData data] retain];
55    else
56    {
57        NSLog(@"Unable to get port status: failed to initiate connection");
58        [self callBackWithStatus: PORT_STATUS_ERROR];
59    }
60}
61
62- (void) callBackWithStatus: (port_status_t) status
63{
64    fStatus = status;
65   
66    if (fDelegate && [fDelegate respondsToSelector: @selector(portCheckerDidFinishProbing:)])
67        [fDelegate portCheckerDidFinishProbing: self];
68}
69
70#pragma mark NSURLConnection delegate methods
71
72- (void) connection: (NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response
73{
74    [fPortProbeData setLength: 0];
75}
76
77- (void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
78{
79    [fPortProbeData appendData: data];
80}
81
82- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error
83{
84    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
85    [self callBackWithStatus: PORT_STATUS_ERROR];
86    [fPortProbeData release];
87}
88
89- (void) connectionDidFinishLoading: (NSURLConnection *) connection
90{
91    NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData
92                                        options: NSXMLDocumentTidyHTML error: nil];
93   
94    if (shieldsUpProbe)
95    {
96        NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil];
97        if ([nodes count] != 1)
98        {
99            NSArray * title = [shieldsUpProbe nodesForXPath: @"/html/head/title" error: nil];
100            // This may happen when we probe twice too quickly
101            if ([title count] != 1 || ![[[title objectAtIndex: 0] stringValue] isEqualToString:
102                                                                    @"NanoProbe System Already In Use"])
103            {
104                NSLog(@"Unable to get port status: invalid (outdated) XPath expression");
105                [[shieldsUpProbe XMLData] writeToFile: @"/tmp/shieldsUpProbe.html" atomically: YES];
106                [self callBackWithStatus: PORT_STATUS_ERROR];
107            }
108        }
109        else
110        {
111            NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
112                                                [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
113           
114            if ([portStatus isEqualToString: @"open"])
115                [self callBackWithStatus: PORT_STATUS_OPEN];
116            else if ([portStatus isEqualToString: @"stealth"])
117                [self callBackWithStatus: PORT_STATUS_STEALTH];
118            else if ([portStatus isEqualToString: @"closed"])
119                [self callBackWithStatus: PORT_STATUS_CLOSED];
120            else
121            {
122                NSLog(@"Unable to get port status: unknown port state");
123                [self callBackWithStatus: PORT_STATUS_ERROR];
124            }
125        }
126    }
127    else
128    {
129        NSLog(@"Unable to get port status: failed to create xml document");
130        [self callBackWithStatus: PORT_STATUS_ERROR];
131    }
132
133    [fPortProbeData release];
134}
135
136@end
Note: See TracBrowser for help on using the repository browser.