source: trunk/macosx/PortChecker.m @ 956

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

Status on ports in prefs window. Thanks to cluthi on the forums for this.

File size: 4.6 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    self = [super init];
32    if (self != nil)
33        fDelegate = delegate;
34   
35    return self;
36}
37
38- (port_status_t) status
39{
40    return fStatus;
41}
42
43- (void) probePort: (int) portNumber
44{
45    NSURLRequest * portProbeRequest;
46   
47    portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
48                                        cachePolicy: NSURLRequestReloadIgnoringCacheData
49                                    timeoutInterval: 15.0];
50   
51    NSURLConnection *portProbeConnection = [NSURLConnection connectionWithRequest: portProbeRequest delegate: self];
52   
53    if (portProbeConnection)
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    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;
91    shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData options: NSXMLDocumentTidyHTML error: nil];
92   
93    if (shieldsUpProbe == nil)
94    {
95        NSLog(@"Unable to get port status: failed to create xml document");
96        [self callBackWithStatus: PORT_STATUS_ERROR];
97    }
98    else
99    {
100        NSArray *nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil];
101        if ([nodes count] != 1)
102        {
103            NSLog(@"Unable to get port status: invalid (outdated) XPath expression");
104            [self callBackWithStatus: PORT_STATUS_ERROR];
105        }
106        else
107        {
108            NSString *portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
109                                                [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
110           
111            if ([portStatus isEqualToString: @"open"])
112                [self callBackWithStatus: PORT_STATUS_OPEN];
113            else if ([portStatus isEqualToString: @"stealth"])
114                [self callBackWithStatus: PORT_STATUS_STEALTH];
115            else if ([portStatus isEqualToString: @"closed"])
116                [self callBackWithStatus: PORT_STATUS_CLOSED];
117            else {
118                NSLog(@"Unable to get port status: unknown port state");
119                [self callBackWithStatus: PORT_STATUS_ERROR];
120            }
121        }
122    }
123
124    [fPortProbeData release];
125}
126
127@end
Note: See TracBrowser for help on using the repository browser.