UniPRT SDK v1.0.0.0
 
Loading...
Searching...
No Matches
SimplePing.h
1//
2// SimplePing.h
3// TSCPrinters SDK
4//
5// Created by Realbuber on 2024/4/11.
6//
7
8#ifndef SimplePing_h
9#define SimplePing_h
10
11
12#endif /* SimplePing_h */
13/*
14 Copyright (C) 2016 Apple Inc. All Rights Reserved.
15 See LICENSE.txt for this sample’s licensing information
16
17 Abstract:
18 An object wrapper around the low-level BSD Sockets ping function.
19 */
20#import <Foundation/Foundation.h>
21
22//@import Foundation;
23
24#include <AssertMacros.h> // for __Check_Compile_Time
25
26NS_ASSUME_NONNULL_BEGIN
27
30@protocol SimplePingDelegate;
31
35typedef NS_ENUM(NSInteger, SimplePingAddressStyle) {
36 SimplePingAddressStyleAny,
37 SimplePingAddressStyleICMPv4,
38 SimplePingAddressStyleICMPv6
39};
40
53@interface SimplePing : NSObject
54
55- (instancetype)init NS_UNAVAILABLE;
56
63- (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER;
64
68@property (nonatomic, copy, readonly) NSString * hostName;
69
75@property (nonatomic, weak, readwrite, nullable) id<SimplePingDelegate> delegate;
76
81@property (nonatomic, assign, readwrite) SimplePingAddressStyle addressStyle;
82
89@property (nonatomic, copy, readonly, nullable) NSData * hostAddress;
90
94@property (nonatomic, assign, readonly) sa_family_t hostAddressFamily;
95
101@property (nonatomic, assign, readonly) uint16_t identifier;
102
110@property (nonatomic, assign, readonly) uint16_t nextSequenceNumber;
111
128- (void)start;
129
140- (void)sendPingWithData:(nullable NSData *)data;
141
148- (void)stop;
149
150@end
151
155@protocol SimplePingDelegate <NSObject>
156
157@optional
158
170- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address;
171
183- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error;
184
198- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber;
199
214- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error;
215
226- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber;
227
246- (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet;
247
248@end
249
250#pragma mark * ICMP On-The-Wire Format
251
260struct ICMPHeader {
261 uint8_t type;
262 uint8_t code;
263 uint16_t checksum;
264 uint16_t identifier;
265 uint16_t sequenceNumber;
266 // data...
267};
268typedef struct ICMPHeader ICMPHeader;
269
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);
276
277enum {
278 ICMPv4TypeEchoRequest = 8,
279 ICMPv4TypeEchoReply = 0
280};
281
282enum {
283 ICMPv6TypeEchoRequest = 128,
284 ICMPv6TypeEchoReply = 129
285};
286
289NS_ASSUME_NONNULL_END