20#import <Foundation/Foundation.h>
24#include <AssertMacros.h>
26NS_ASSUME_NONNULL_BEGIN
30@protocol SimplePingDelegate;
35typedef NS_ENUM(NSInteger, SimplePingAddressStyle) {
36 SimplePingAddressStyleAny,
37 SimplePingAddressStyleICMPv4,
38 SimplePingAddressStyleICMPv6
53@interface SimplePing : NSObject
55- (instancetype)init NS_UNAVAILABLE;
63- (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER;
68@property (nonatomic, copy, readonly) NSString * hostName;
75@property (nonatomic, weak, readwrite, nullable) id<SimplePingDelegate> delegate;
81@property (nonatomic, assign, readwrite) SimplePingAddressStyle addressStyle;
89@property (nonatomic, copy, readonly, nullable) NSData * hostAddress;
94@property (nonatomic, assign, readonly) sa_family_t hostAddressFamily;
101@property (nonatomic, assign, readonly) uint16_t identifier;
110@property (nonatomic, assign, readonly) uint16_t nextSequenceNumber;
140- (void)sendPingWithData:(nullable NSData *)data;
155@protocol SimplePingDelegate <NSObject>
170- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address;
183- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error;
198- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber;
214- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error;
226- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber;
246- (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet;
250#pragma mark * ICMP On-The-Wire Format
265 uint16_t sequenceNumber;
268typedef struct ICMPHeader ICMPHeader;
270__Check_Compile_Time(
sizeof(ICMPHeader) == 8);
271__Check_Compile_Time(offsetof(ICMPHeader, type) == 0);
272__Check_Compile_Time(offsetof(ICMPHeader, code) == 1);
273__Check_Compile_Time(offsetof(ICMPHeader, checksum) == 2);
274__Check_Compile_Time(offsetof(ICMPHeader, identifier) == 4);
275__Check_Compile_Time(offsetof(ICMPHeader, sequenceNumber) == 6);
278 ICMPv4TypeEchoRequest = 8,
279 ICMPv4TypeEchoReply = 0
283 ICMPv6TypeEchoRequest = 128,
284 ICMPv6TypeEchoReply = 129