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