Skip to content

Commit 0769ac3

Browse files
committed
traceroute done
1 parent 4748a80 commit 0769ac3

File tree

12 files changed

+730
-32
lines changed

12 files changed

+730
-32
lines changed

NetDiag.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/* Begin PBXBuildFile section */
1010
A6296E24683C84AC5548DB39 /* libPods-NetDiagTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F50835689CBA65323B9B2AA7 /* libPods-NetDiagTests.a */; };
1111
DF2B76EC1C33702D00643678 /* QNNPing.m in Sources */ = {isa = PBXBuildFile; fileRef = DF2B76EB1C33702D00643678 /* QNNPing.m */; };
12+
DF47525E1C740F0100EEAF4B /* PingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF47525D1C740F0100EEAF4B /* PingTest.m */; };
13+
DF4752601C743A8600EEAF4B /* ExternalIpTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF47525F1C743A8600EEAF4B /* ExternalIpTest.m */; };
14+
DF4752621C747D4A00EEAF4B /* TraceRouteTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF4752611C747D4A00EEAF4B /* TraceRouteTest.m */; };
1215
DF8DFFC51C60951D00174DA9 /* QNNNslookup.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFC41C60951D00174DA9 /* QNNNslookup.m */; };
1316
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */; };
1417
DF8DFFD41C61DA1B00174DA9 /* libNetDiag.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DFCEC6451C2EC01D0058EE65 /* libNetDiag.a */; };
@@ -54,6 +57,9 @@
5457
DF2B76EA1C33702D00643678 /* QNNPing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNPing.h; sourceTree = "<group>"; };
5558
DF2B76EB1C33702D00643678 /* QNNPing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNPing.m; sourceTree = "<group>"; };
5659
DF2B76ED1C33706900643678 /* QNNProtocols.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNProtocols.h; sourceTree = "<group>"; };
60+
DF47525D1C740F0100EEAF4B /* PingTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PingTest.m; sourceTree = "<group>"; };
61+
DF47525F1C743A8600EEAF4B /* ExternalIpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExternalIpTest.m; sourceTree = "<group>"; };
62+
DF4752611C747D4A00EEAF4B /* TraceRouteTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TraceRouteTest.m; sourceTree = "<group>"; };
5763
DF8DFFC31C60951D00174DA9 /* QNNNslookup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNNslookup.h; sourceTree = "<group>"; };
5864
DF8DFFC41C60951D00174DA9 /* QNNNslookup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNNslookup.m; sourceTree = "<group>"; };
5965
DF8DFFCF1C61DA1B00174DA9 /* NetDiagTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NetDiagTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -132,6 +138,9 @@
132138
DFA9E1771C6C8FE800C17111 /* RtmpTest.m */,
133139
DFA9E1791C6DB06D00C17111 /* NslookupTest.m */,
134140
DFA9E17D1C708A0000C17111 /* QNNHttpTest.m */,
141+
DF47525D1C740F0100EEAF4B /* PingTest.m */,
142+
DF47525F1C743A8600EEAF4B /* ExternalIpTest.m */,
143+
DF4752611C747D4A00EEAF4B /* TraceRouteTest.m */,
135144
);
136145
path = NetDiagTests;
137146
sourceTree = "<group>";
@@ -321,8 +330,11 @@
321330
buildActionMask = 2147483647;
322331
files = (
323332
DFA9E17A1C6DB06D00C17111 /* NslookupTest.m in Sources */,
333+
DF4752621C747D4A00EEAF4B /* TraceRouteTest.m in Sources */,
324334
DF8DFFDC1C61E6B200174DA9 /* QNNTestLogger.m in Sources */,
325335
DFA9E17E1C708A0000C17111 /* QNNHttpTest.m in Sources */,
336+
DF4752601C743A8600EEAF4B /* ExternalIpTest.m in Sources */,
337+
DF47525E1C740F0100EEAF4B /* PingTest.m in Sources */,
326338
DFA9E1781C6C8FE800C17111 /* RtmpTest.m in Sources */,
327339
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */,
328340
);

NetDiag/QNNExternalIp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@
1010

1111
@interface QNNExternalIp : NSObject
1212

13+
+(NSString*) externalIp;
14+
+(NSString*) externalDNS;
15+
16+
+(NSString*)checkExternal;
17+
1318
@end

NetDiag/QNNExternalIp.m

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,127 @@
88

99
#import "QNNExternalIp.h"
1010

11+
1112
@implementation QNNExternalIp
1213

14+
+(NSString*) externalIp{
15+
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://whatismyip.akamai.com"]];
16+
[urlRequest setHTTPMethod:@"GET"];
17+
18+
NSHTTPURLResponse *response = nil;
19+
NSError *httpError = nil;
20+
NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest
21+
returningResponse:&response
22+
error:&httpError];
23+
if (httpError != nil || d == nil) {
24+
return @"";
25+
}
26+
NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding];
27+
if (s == nil) {
28+
return @"";
29+
}
30+
return s;
31+
}
32+
33+
+(NSString*) externalDNS{
34+
return @"";
35+
}
36+
37+
+(NSString*)getDiagUrl{
38+
NSString* fetchurl = @"http://ns.pbt.cachecn.net/fast_tools/fetch_ldns_diag_client.php";
39+
40+
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:fetchurl]];
41+
[urlRequest setHTTPMethod:@"GET"];
42+
43+
NSHTTPURLResponse *response = nil;
44+
NSError *httpError = nil;
45+
NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest
46+
returningResponse:&response
47+
error:&httpError];
48+
if (httpError != nil || d == nil) {
49+
NSLog(@"fetch http error %@", httpError);
50+
return nil;
51+
}
52+
NSLog(@"fetch http code %d", response.statusCode);
53+
NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding];
54+
if (s == nil || [s isEqualToString:@""]) {
55+
NSLog(@"fetch http code %d", response.statusCode);
56+
return nil;
57+
}
58+
NSRange range = [s rangeOfString:@"<iframe src=\""];
59+
if (range.location > 4000) {
60+
return nil;
61+
}
62+
s = [s substringFromIndex:range.location + range.length];
63+
range = [s rangeOfString:@".php\""];
64+
if (range.location>4000) {
65+
return nil;
66+
}
67+
s = [s substringToIndex:range.location+4];
68+
return s;
69+
}
70+
71+
// this service provided by fastweb
72+
//<pre>
73+
//<table border="0" cellpadding="0" cellspacing="0" class="dns">
74+
//<tr>
75+
//<th>您的IP:</th>
76+
//<td width="128" >60.168.142.119</td>
77+
//<td>电信_安徽省_合肥市</td>
78+
//</tr>
79+
//<tr>
80+
//<th>您的DNS:</th>
81+
//<td width="128" >61.132.161.9</td>
82+
//<td>电信_安徽省_阜阳市</td>
83+
//</tr>
84+
//</table>
85+
//</pre>
86+
//<p class="result">您的DNS配置正确! </p>
87+
88+
+(NSString*)checkExternal{
89+
NSString* url = [QNNExternalIp getDiagUrl];
90+
if (url == nil) {
91+
return @"get fetch url failed";
92+
}
93+
94+
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
95+
[urlRequest setHTTPMethod:@"GET"];
96+
97+
NSHTTPURLResponse *response = nil;
98+
NSError *httpError = nil;
99+
NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest
100+
returningResponse:&response
101+
error:&httpError];
102+
if (httpError != nil || d == nil) {
103+
return @"check server error";
104+
}
105+
NSLog(@"http code %d", response.statusCode);
106+
NSString* s = [[NSString alloc]initWithData:d encoding:NSUTF8StringEncoding];
107+
if (s == nil || [s isEqualToString:@""]) {
108+
return @"invalid encoding";
109+
}
110+
111+
NSRange range = [s rangeOfString:@"<tr>"];
112+
s = [s substringFromIndex:range.location + range.length];
113+
range = [s rangeOfString:@"</table>"];
114+
s = [s substringToIndex:range.location];
115+
116+
s = [s stringByReplacingOccurrencesOfString:@"</" withString:@"<"];
117+
118+
s = [s stringByReplacingOccurrencesOfString:@"<tr>" withString:@""];
119+
s = [s stringByReplacingOccurrencesOfString:@"<th>" withString:@""];
120+
s = [s stringByReplacingOccurrencesOfString:@"<td>" withString:@""];
121+
s = [s stringByReplacingOccurrencesOfString:@"<td>" withString:@""];
122+
s = [s stringByReplacingOccurrencesOfString:@"<td width=\"128\" >" withString:@""];
123+
124+
s = [s stringByReplacingOccurrencesOfString:@"<table>" withString:@""];
125+
s = [s stringByReplacingOccurrencesOfString:@"<p class=\"result\">" withString:@""];
126+
s = [s stringByReplacingOccurrencesOfString:@"<pre>" withString:@""];
127+
s = [s stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
128+
129+
s = [s stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
130+
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
131+
return s;
132+
}
133+
13134
@end

NetDiag/QNNPing.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
#import <Foundation/Foundation.h>
1010
#import "QNNProtocols.h"
1111

12+
extern const int kQNNInvalidPingResponse;
13+
1214
@interface QNNPingResult : NSObject
1315

1416
@property (readonly) NSInteger code;
1517
@property (readonly) NSTimeInterval maxRtt;
1618
@property (readonly) NSTimeInterval minRtt;
1719
@property (readonly) NSTimeInterval avgRtt;
18-
@property (readonly) double lossRate;
20+
@property (readonly) NSInteger loss;
1921
@property (readonly) NSInteger count;
20-
@property (readonly) NSInteger interval;
22+
@property (readonly) NSTimeInterval totalTime;
23+
@property (readonly) NSTimeInterval stddev;
2124

2225
-(NSString*) description;
2326

0 commit comments

Comments
 (0)