Skip to content

Commit ceebf9e

Browse files
authored
Merge pull request #4 from lansefengxinzi/ping
add ip and contents
2 parents f0070e9 + 605289d commit ceebf9e

File tree

6 files changed

+1423
-520
lines changed

6 files changed

+1423
-520
lines changed

NetDiag.xcodeproj/project.pbxproj

+1,356-510
Large diffs are not rendered by default.

NetDiag/QNNHttp.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@interface QNNHttpResult : NSObject
1313

1414
@property (readonly) NSInteger code;
15+
@property (readonly) NSString* ip;
1516
@property (readonly) NSTimeInterval duration;
1617
@property (readonly) NSDictionary* headers;
1718
@property (readonly) NSData* body;

NetDiag/QNNHttp.m

+30-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99
#import "QNNHttp.h"
10+
#import <arpa/inet.h>
11+
#import <netdb.h>
12+
#import <netinet/in.h>
1013

1114
@implementation QNNHttpResult
1215

@@ -22,11 +25,13 @@ - (NSString *)description {
2225
}
2326

2427
- (instancetype)init:(NSInteger)code
28+
ip:(NSString *)ip
2529
duration:(NSTimeInterval)duration
2630
headers:(NSDictionary *)headers
2731
body:(NSData *)body {
2832
if (self = [super init]) {
2933
_code = code;
34+
_ip = ip;
3035
_duration = duration;
3136
_headers = headers;
3237
_body = body;
@@ -55,10 +60,32 @@ - (instancetype)init:(NSString *)url
5560
return self;
5661
}
5762

63+
- (NSString *)reserveUrlToIp {
64+
NSString *ip = nil;
65+
NSString *urlStr = [[_url componentsSeparatedByString:@"//"] lastObject];
66+
67+
struct sockaddr_in addr;
68+
memset(&addr, 0, sizeof(addr));
69+
addr.sin_len = sizeof(addr);
70+
addr.sin_family = AF_INET;
71+
addr.sin_port = htons(80);
72+
addr.sin_addr.s_addr = inet_addr([urlStr UTF8String]);
73+
if (addr.sin_addr.s_addr == INADDR_NONE) {
74+
struct hostent *host = gethostbyname([urlStr UTF8String]);
75+
if (host == NULL || host->h_addr == NULL) {
76+
return ip;
77+
}
78+
addr.sin_addr = *(struct in_addr *)host->h_addr;
79+
ip = [NSString stringWithUTF8String:inet_ntoa(addr.sin_addr)];
80+
}
81+
return ip;
82+
}
83+
5884
- (void)run {
5985
if (_output) {
6086
[_output write:[NSString stringWithFormat:@"GET %@", _url]];
6187
}
88+
6289
NSDate *t1 = [NSDate date];
6390
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url]];
6491
[urlRequest setHTTPMethod:@"GET"];
@@ -84,11 +111,12 @@ - (void)run {
84111
return;
85112
}
86113
if (httpError != nil) {
87-
QNNHttpResult *result = [[QNNHttpResult alloc] init:httpError.code duration:duration headers:nil body:nil];
114+
QNNHttpResult *result = [[QNNHttpResult alloc] init:httpError.code ip:[self reserveUrlToIp] duration:duration headers:nil body:nil];
88115
_complete(result);
89116
return;
90117
}
91-
QNNHttpResult *result = [[QNNHttpResult alloc] init:response.statusCode duration:duration headers:response.allHeaderFields body:d];
118+
119+
QNNHttpResult *result = [[QNNHttpResult alloc] init:response.statusCode ip:[self reserveUrlToIp] duration:duration headers:response.allHeaderFields body:d];
92120
_complete(result);
93121
}
94122

NetDiag/QNNTcpPing.h

+4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
@interface QNNTcpPingResult : NSObject
1313

1414
@property (readonly) NSInteger code;
15+
@property (readonly) NSString* ip;
1516
@property (readonly) NSTimeInterval maxTime;
1617
@property (readonly) NSTimeInterval minTime;
1718
@property (readonly) NSTimeInterval avgTime;
19+
@property (readonly) NSInteger loss;
1820
@property (readonly) NSInteger count;
21+
@property (readonly) NSTimeInterval totalTime;
22+
@property (readonly) NSTimeInterval stddev;
1923

2024
- (NSString*)description;
2125

NetDiag/QNNTcpPing.m

+31-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
@interface QNNTcpPingResult ()
2121

2222
- (instancetype)init:(NSInteger)code
23+
ip:(NSString *)ip
2324
max:(NSTimeInterval)maxTime
2425
min:(NSTimeInterval)minTime
2526
avg:(NSTimeInterval)avgTime
26-
count:(NSInteger)count;
27+
loss:(NSInteger)loss
28+
count:(NSInteger)count
29+
totalTime:(NSTimeInterval)totalTime
30+
stddev:(NSTimeInterval)stddev;
2731
@end
2832

2933
@implementation QNNTcpPingResult
@@ -36,16 +40,24 @@ - (NSString *)description {
3640
}
3741

3842
- (instancetype)init:(NSInteger)code
43+
ip:(NSString *)ip
3944
max:(NSTimeInterval)maxTime
4045
min:(NSTimeInterval)minTime
4146
avg:(NSTimeInterval)avgTime
42-
count:(NSInteger)count {
47+
loss:(NSInteger)loss
48+
count:(NSInteger)count
49+
totalTime:(NSTimeInterval)totalTime
50+
stddev:(NSTimeInterval)stddev {
4351
if (self = [super init]) {
4452
_code = code;
53+
_ip = ip;
4554
_minTime = minTime;
4655
_avgTime = avgTime;
4756
_maxTime = maxTime;
57+
_loss = loss;
4858
_count = count;
59+
_totalTime = totalTime;
60+
_stddev = stddev;
4961
}
5062
return self;
5163
}
@@ -82,6 +94,7 @@ - (instancetype)init:(NSString *)host
8294
}
8395

8496
- (void)run {
97+
NSDate *begin = [NSDate date];
8598
[self.output write:[NSString stringWithFormat:@"connect to host %@:%lu ...\n", _host, (unsigned long)_port]];
8699
struct sockaddr_in addr;
87100
memset(&addr, 0, sizeof(addr));
@@ -95,18 +108,19 @@ - (void)run {
95108
[self.output write:@"Problem accessing the DNS"];
96109
if (_complete != nil) {
97110
dispatch_async(dispatch_get_main_queue(), ^(void) {
98-
_complete([self buildResult:-1006 durations:nil count:0]);
111+
_complete([self buildResult:-1006 ip:nil durations:nil loss:0 count:0 totalTime:0]);
99112
});
100113
}
101114
return;
102115
}
103116
addr.sin_addr = *(struct in_addr *)host->h_addr;
104117
[self.output write:[NSString stringWithFormat:@"connect to ip %s:%lu ...\n", inet_ntoa(addr.sin_addr), (unsigned long)_port]];
105118
}
106-
119+
NSString *ip = [NSString stringWithUTF8String:inet_ntoa(addr.sin_addr)];
107120
NSTimeInterval *intervals = (NSTimeInterval *)malloc(sizeof(NSTimeInterval) * _count);
108121
int index = 0;
109122
int r = 0;
123+
int loss = 0;
110124
do {
111125
NSDate *t1 = [NSDate date];
112126
r = [self connect:&addr];
@@ -116,6 +130,7 @@ - (void)run {
116130
[self.output write:[NSString stringWithFormat:@"connected to %s:%lu, %f ms\n", inet_ntoa(addr.sin_addr), (unsigned long)_port, duration * 1000]];
117131
} else {
118132
[self.output write:[NSString stringWithFormat:@"connect failed to %s:%lu, %f ms, error %d\n", inet_ntoa(addr.sin_addr), (unsigned long)_port, duration * 1000, r]];
133+
loss++;
119134
}
120135

121136
if (index < _count && !_stopped && r == 0) {
@@ -128,22 +143,27 @@ - (void)run {
128143
if (_stopped) {
129144
code = kQNNRequestStoped;
130145
}
146+
__block NSDate *startDate = begin;
131147
dispatch_async(dispatch_get_main_queue(), ^(void) {
132-
_complete([self buildResult:code durations:intervals count:index]);
148+
_complete([self buildResult:code ip:ip durations:intervals loss:loss count:index totalTime:[[NSDate date] timeIntervalSinceDate:startDate] * 1000]);
133149
});
134150
}
135151
free(intervals);
136152
}
137153

138154
- (QNNTcpPingResult *)buildResult:(NSInteger)code
155+
ip:(NSString *)ip
139156
durations:(NSTimeInterval *)durations
140-
count:(NSInteger)count {
157+
loss:(NSInteger)loss
158+
count:(NSInteger)count
159+
totalTime:(NSTimeInterval)time {
141160
if (code != 0 && code != kQNNRequestStoped) {
142-
return [[QNNTcpPingResult alloc] init:code max:0 min:0 avg:0 count:1];
161+
return [[QNNTcpPingResult alloc] init:code ip:ip max:0 min:0 avg:0 loss:1 count:1 totalTime:time stddev:0];
143162
}
144163
NSTimeInterval max = 0;
145164
NSTimeInterval min = 10000000;
146165
NSTimeInterval sum = 0;
166+
NSTimeInterval sum2 = 0;
147167
for (int i = 0; i < count; i++) {
148168
if (durations[i] > max) {
149169
max = durations[i];
@@ -152,9 +172,12 @@ - (QNNTcpPingResult *)buildResult:(NSInteger)code
152172
min = durations[i];
153173
}
154174
sum += durations[i];
175+
sum2 += durations[i] * durations[i];
155176
}
156177
NSTimeInterval avg = sum / count;
157-
return [[QNNTcpPingResult alloc] init:code max:max min:min avg:avg count:count];
178+
NSTimeInterval avg2 = sum2 / count;
179+
NSTimeInterval stddev = sqrt(avg2 - avg * avg);
180+
return [[QNNTcpPingResult alloc] init:code ip:ip max:max min:min avg:avg loss:loss count:count totalTime:time stddev:stddev];
158181
}
159182

160183
- (int)connect:(struct sockaddr_in *)addr {

NetDiagTests/QNNHttpTest.m

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ - (void)testOK {
3333
__block BOOL run = NO;
3434
[QNNHttp start:@"http://www.baidu.com" output:[[QNNTestLogger alloc] init] complete:^(QNNHttpResult* r) {
3535
XCTAssertNotNil(r, @"need result");
36+
XCTAssertNotNil(r.ip, @"need ip");
3637
XCTAssertNotNil(r.headers, @"need headers");
3738
XCTAssertNotNil(r.body, @"need body");
3839
XCTAssertEqual(200, r.code, @"normal code");

0 commit comments

Comments
 (0)