Skip to content

SimplePingHelper with ARC and call by block #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PingTester.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
8D4CB64814C6754200875077 /* SimplePingHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D4CB64714C6754200875077 /* SimplePingHelper.m */; };
8D4CB64814C6754200875077 /* SimplePingHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D4CB64714C6754200875077 /* SimplePingHelper.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
8D54207F14C650C600B3689B /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D54207E14C650C600B3689B /* UIKit.framework */; };
8D54208114C650C600B3689B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D54208014C650C600B3689B /* Foundation.framework */; };
8D54208314C650C600B3689B /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D54208214C650C600B3689B /* CoreGraphics.framework */; };
Expand All @@ -19,7 +19,7 @@
8D5420AC14C651E700B3689B /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 8D5420A914C651E700B3689B /* [email protected] */; };
8D5420AD14C651E700B3689B /* Radio-Tower.png in Resources */ = {isa = PBXBuildFile; fileRef = 8D5420AA14C651E700B3689B /* Radio-Tower.png */; };
8D5420AE14C651E700B3689B /* PP Icons.txt in Resources */ = {isa = PBXBuildFile; fileRef = 8D5420AB14C651E700B3689B /* PP Icons.txt */; };
8D5420B714C6681000B3689B /* SimplePing.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D5420B614C6681000B3689B /* SimplePing.m */; };
8D5420B714C6681000B3689B /* SimplePing.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D5420B614C6681000B3689B /* SimplePing.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
8D5420B914C66A3000B3689B /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D5420B814C66A3000B3689B /* SystemConfiguration.framework */; };
8D5420BB14C66A5D00B3689B /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D5420BA14C66A5D00B3689B /* CFNetwork.framework */; };
/* End PBXBuildFile section */
Expand Down
18 changes: 17 additions & 1 deletion PingTester/FirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ - (void)tapPing {
[self log:@""];
[self log:@"-----------"];
[self log:@"Tapped Ping"];
[SimplePingHelper ping:self.ipAddr.text target:self sel:@selector(pingResult:)];
// [SimplePingHelper ping:self.ipAddr.text target:self sel:@selector(pingResult:)];


[SimplePingHelper ping:@"www.baidu.com"
callback:^(NSNumber *b){
if(b.boolValue)
{
NSLog(@"%@", @"success");
[self log:@"success"];

}
else
{
NSLog(@"%@", @"failure");
[self log:@"failure"];
}
}];
}

- (void)pingResult:(NSNumber*)success {
Expand Down
122 changes: 56 additions & 66 deletions PingTester/SimplePing.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Written by: DTS

Copyright: Copyright (c) 2010 Apple Inc. All Rights Reserved.
Copyright: Copyright (c) 2010-2012 Apple Inc. All Rights Reserved.

Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
Expand All @@ -32,7 +32,7 @@
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis.
The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
Expand All @@ -54,7 +54,7 @@
#import <Foundation/Foundation.h>

#if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
#import <CFNetwork/CFNetwork.h>
#import <CFNetwork/CFNetwork.h>
#else
#import <CoreServices/CoreServices.h>
#endif
Expand All @@ -68,46 +68,36 @@
@protocol SimplePingDelegate;

@interface SimplePing : NSObject
{
NSString * _hostName;
NSData * _hostAddress;
CFHostRef _host;
CFSocketRef _socket;

id<SimplePingDelegate> _delegate;
uint16_t _identifier; // host byte order
uint16_t _nextSequenceNumber; // host byte order
}

+ (SimplePing *)simplePingWithHostName:(NSString *)hostName; // chooses first IPv4 address
+ (SimplePing *)simplePingWithHostAddress:(NSData *)hostAddress; // contains (struct sockaddr)

@property (nonatomic, assign, readwrite) id<SimplePingDelegate> delegate;
@property (nonatomic, weak, readwrite) id<SimplePingDelegate> delegate;

@property (nonatomic, copy, readonly) NSString * hostName;
@property (nonatomic, copy, readonly) NSData * hostAddress;
@property (nonatomic, assign, readonly) uint16_t identifier;
@property (nonatomic, assign, readonly) uint16_t nextSequenceNumber;
@property (nonatomic, copy, readonly ) NSString * hostName;
@property (nonatomic, copy, readonly ) NSData * hostAddress;
@property (nonatomic, assign, readonly ) uint16_t identifier;
@property (nonatomic, assign, readonly ) uint16_t nextSequenceNumber;

- (void)start;
// Starts the pinger object pinging. You should call this after
// you've setup the delegate and any ping parameters.
// Starts the pinger object pinging. You should call this after
// you've setup the delegate and any ping parameters.

- (void)sendPingWithData:(NSData *)data;
// Sends an actual ping. Pass nil for data to use a standard 56 byte payload (resulting in a
// standard 64 byte ping). Otherwise pass a non-nil value and it will be appended to the
// ICMP header.
//
// Do not try to send a ping before you receive the -simplePing:didStartWithAddress: delegate
// callback.
// Sends an actual ping. Pass nil for data to use a standard 56 byte payload (resulting in a
// standard 64 byte ping). Otherwise pass a non-nil value and it will be appended to the
// ICMP header.
//
// Do not try to send a ping before you receive the -simplePing:didStartWithAddress: delegate
// callback.

- (void)stop;
// Stops the pinger object. You should call this when you're done
// pinging.
// Stops the pinger object. You should call this when you're done
// pinging.

+ (const struct ICMPHeader *)icmpInPacket:(NSData *)packet;
// Given a valid IP packet contains an ICMP , returns the address of the ICMP header that
// follows the IP header. This doesn't do any significant validation of the packet.
// Given a valid IP packet contains an ICMP , returns the address of the ICMP header that
// follows the IP header. This doesn't do any significant validation of the packet.

@end

Expand All @@ -116,32 +106,32 @@
@optional

- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address;
// Called after the SimplePing has successfully started up. After this callback, you
// can start sending pings via -sendPingWithData:
// Called after the SimplePing has successfully started up. After this callback, you
// can start sending pings via -sendPingWithData:

- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error;
// If this is called, the SimplePing object has failed. By the time this callback is
// called, the object has stopped (that is, you don't need to call -stop yourself).
// If this is called, the SimplePing object has failed. By the time this callback is
// called, the object has stopped (that is, you don't need to call -stop yourself).

// IMPORTANT: On the send side the packet does not include an IP header.
// On the receive side, it does. In that case, use +[SimplePing icmpInPacket:]
// IMPORTANT: On the send side the packet does not include an IP header.
// On the receive side, it does. In that case, use +[SimplePing icmpInPacket:]
// to find the ICMP header within the packet.

- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet;
// Called whenever the SimplePing object has successfully sent a ping packet.
// Called whenever the SimplePing object has successfully sent a ping packet.

- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error;
// Called whenever the SimplePing object tries and fails to send a ping packet.
// Called whenever the SimplePing object tries and fails to send a ping packet.

- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet;
// Called whenever the SimplePing object receives an ICMP packet that looks like
// a response to one of our pings (that is, has a valid ICMP checksum, has
// an identifier that matches our identifier, and has a sequence number in
// the range of sequence numbers that we've sent out).
// Called whenever the SimplePing object receives an ICMP packet that looks like
// a response to one of our pings (that is, has a valid ICMP checksum, has
// an identifier that matches our identifier, and has a sequence number in
// the range of sequence numbers that we've sent out).

- (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet;
// Called whenever the SimplePing object receives an ICMP packet that does not
// look like a response to one of our pings.
// Called whenever the SimplePing object receives an ICMP packet that does not
// look like a response to one of our pings.

@end

Expand All @@ -152,18 +142,18 @@
// IP header structure:

struct IPHeader {
uint8_t versionAndHeaderLength;
uint8_t differentiatedServices;
uint16_t totalLength;
uint16_t identification;
uint16_t flagsAndFragmentOffset;
uint8_t timeToLive;
uint8_t protocol;
uint16_t headerChecksum;
uint8_t sourceAddress[4];
uint8_t destinationAddress[4];
// options...
// data...
uint8_t versionAndHeaderLength;
uint8_t differentiatedServices;
uint16_t totalLength;
uint16_t identification;
uint16_t flagsAndFragmentOffset;
uint8_t timeToLive;
uint8_t protocol;
uint16_t headerChecksum;
uint8_t sourceAddress[4];
uint8_t destinationAddress[4];
// options...
// data...
};
typedef struct IPHeader IPHeader;

Expand All @@ -182,19 +172,19 @@ check_compile_time(offsetof(IPHeader, destinationAddress) == 16);
// ICMP type and code combinations:

enum {
kICMPTypeEchoReply = 0, // code is always 0
kICMPTypeEchoRequest = 8 // code is always 0
kICMPTypeEchoReply = 0, // code is always 0
kICMPTypeEchoRequest = 8 // code is always 0
};

// ICMP header structure:

struct ICMPHeader {
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t identifier;
uint16_t sequenceNumber;
// data...
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t identifier;
uint16_t sequenceNumber;
// data...
};
typedef struct ICMPHeader ICMPHeader;

Expand Down
Loading