├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md └── SimplePingDemo ├── SimplePingDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── SimplePingDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── SimplePing │ ├── FFSimplePingHelper.h │ ├── FFSimplePingHelper.m │ ├── SimplePing.h │ └── SimplePing.m ├── ViewController.h ├── ViewController.m └── main.m ├── SimplePingDemoTests ├── Info.plist └── SimplePingDemoTests.m └── SimplePingDemoUITests ├── Info.plist └── SimplePingDemoUITests.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | # 39 | # Add this line if you want to avoid checking in source code from the Xcode workspace 40 | # *.xcworkspace 41 | 42 | # Carthage 43 | # 44 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 45 | # Carthage/Checkouts 46 | 47 | Carthage/Build 48 | 49 | # fastlane 50 | # 51 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 52 | # screenshots whenever they are needed. 53 | # For more information about the recommended setup visit: 54 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 55 | 56 | fastlane/report.xml 57 | fastlane/Preview.html 58 | fastlane/screenshots/**/*.png 59 | fastlane/test_output 60 | 61 | # Code Injection 62 | # 63 | # After new code Injection tools there's a generated folder /iOSInjectionProject 64 | # https://github.com/johnno1962/injectionforxcode 65 | 66 | iOSInjectionProject/ 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 wangzhe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimplePingDemo 2 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A7E5985B221ABD8700324BF3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E5985A221ABD8700324BF3 /* AppDelegate.m */; }; 11 | A7E5985E221ABD8700324BF3 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E5985D221ABD8700324BF3 /* ViewController.m */; }; 12 | A7E59861221ABD8700324BF3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A7E5985F221ABD8700324BF3 /* Main.storyboard */; }; 13 | A7E59863221ABD8800324BF3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A7E59862221ABD8800324BF3 /* Assets.xcassets */; }; 14 | A7E59866221ABD8800324BF3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A7E59864221ABD8800324BF3 /* LaunchScreen.storyboard */; }; 15 | A7E59869221ABD8800324BF3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E59868221ABD8800324BF3 /* main.m */; }; 16 | A7E59873221ABD8800324BF3 /* SimplePingDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E59872221ABD8800324BF3 /* SimplePingDemoTests.m */; }; 17 | A7E5987E221ABD8800324BF3 /* SimplePingDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E5987D221ABD8800324BF3 /* SimplePingDemoUITests.m */; }; 18 | A7E5988E221ABF2500324BF3 /* SimplePing.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E5988C221ABF2500324BF3 /* SimplePing.m */; }; 19 | A7E59891221ABF4800324BF3 /* FFSimplePingHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E59890221ABF4800324BF3 /* FFSimplePingHelper.m */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXContainerItemProxy section */ 23 | A7E5986F221ABD8800324BF3 /* PBXContainerItemProxy */ = { 24 | isa = PBXContainerItemProxy; 25 | containerPortal = A7E5984E221ABD8700324BF3 /* Project object */; 26 | proxyType = 1; 27 | remoteGlobalIDString = A7E59855221ABD8700324BF3; 28 | remoteInfo = SimplePingDemo; 29 | }; 30 | A7E5987A221ABD8800324BF3 /* PBXContainerItemProxy */ = { 31 | isa = PBXContainerItemProxy; 32 | containerPortal = A7E5984E221ABD8700324BF3 /* Project object */; 33 | proxyType = 1; 34 | remoteGlobalIDString = A7E59855221ABD8700324BF3; 35 | remoteInfo = SimplePingDemo; 36 | }; 37 | /* End PBXContainerItemProxy section */ 38 | 39 | /* Begin PBXFileReference section */ 40 | A7E59856221ABD8700324BF3 /* SimplePingDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SimplePingDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | A7E59859221ABD8700324BF3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 42 | A7E5985A221ABD8700324BF3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 43 | A7E5985C221ABD8700324BF3 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 44 | A7E5985D221ABD8700324BF3 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 45 | A7E59860221ABD8700324BF3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 46 | A7E59862221ABD8800324BF3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 47 | A7E59865221ABD8800324BF3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 48 | A7E59867221ABD8800324BF3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 49 | A7E59868221ABD8800324BF3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 50 | A7E5986E221ABD8800324BF3 /* SimplePingDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SimplePingDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 51 | A7E59872221ABD8800324BF3 /* SimplePingDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SimplePingDemoTests.m; sourceTree = ""; }; 52 | A7E59874221ABD8800324BF3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 53 | A7E59879221ABD8800324BF3 /* SimplePingDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SimplePingDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 54 | A7E5987D221ABD8800324BF3 /* SimplePingDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SimplePingDemoUITests.m; sourceTree = ""; }; 55 | A7E5987F221ABD8800324BF3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 56 | A7E5988C221ABF2500324BF3 /* SimplePing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimplePing.m; sourceTree = ""; }; 57 | A7E5988D221ABF2500324BF3 /* SimplePing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimplePing.h; sourceTree = ""; }; 58 | A7E5988F221ABF4800324BF3 /* FFSimplePingHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FFSimplePingHelper.h; sourceTree = ""; }; 59 | A7E59890221ABF4800324BF3 /* FFSimplePingHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FFSimplePingHelper.m; sourceTree = ""; }; 60 | /* End PBXFileReference section */ 61 | 62 | /* Begin PBXFrameworksBuildPhase section */ 63 | A7E59853221ABD8700324BF3 /* Frameworks */ = { 64 | isa = PBXFrameworksBuildPhase; 65 | buildActionMask = 2147483647; 66 | files = ( 67 | ); 68 | runOnlyForDeploymentPostprocessing = 0; 69 | }; 70 | A7E5986B221ABD8800324BF3 /* Frameworks */ = { 71 | isa = PBXFrameworksBuildPhase; 72 | buildActionMask = 2147483647; 73 | files = ( 74 | ); 75 | runOnlyForDeploymentPostprocessing = 0; 76 | }; 77 | A7E59876221ABD8800324BF3 /* Frameworks */ = { 78 | isa = PBXFrameworksBuildPhase; 79 | buildActionMask = 2147483647; 80 | files = ( 81 | ); 82 | runOnlyForDeploymentPostprocessing = 0; 83 | }; 84 | /* End PBXFrameworksBuildPhase section */ 85 | 86 | /* Begin PBXGroup section */ 87 | A7E5984D221ABD8700324BF3 = { 88 | isa = PBXGroup; 89 | children = ( 90 | A7E59858221ABD8700324BF3 /* SimplePingDemo */, 91 | A7E59871221ABD8800324BF3 /* SimplePingDemoTests */, 92 | A7E5987C221ABD8800324BF3 /* SimplePingDemoUITests */, 93 | A7E59857221ABD8700324BF3 /* Products */, 94 | ); 95 | sourceTree = ""; 96 | }; 97 | A7E59857221ABD8700324BF3 /* Products */ = { 98 | isa = PBXGroup; 99 | children = ( 100 | A7E59856221ABD8700324BF3 /* SimplePingDemo.app */, 101 | A7E5986E221ABD8800324BF3 /* SimplePingDemoTests.xctest */, 102 | A7E59879221ABD8800324BF3 /* SimplePingDemoUITests.xctest */, 103 | ); 104 | name = Products; 105 | sourceTree = ""; 106 | }; 107 | A7E59858221ABD8700324BF3 /* SimplePingDemo */ = { 108 | isa = PBXGroup; 109 | children = ( 110 | A7E5988B221ABD8F00324BF3 /* SimplePing */, 111 | A7E59859221ABD8700324BF3 /* AppDelegate.h */, 112 | A7E5985A221ABD8700324BF3 /* AppDelegate.m */, 113 | A7E5985C221ABD8700324BF3 /* ViewController.h */, 114 | A7E5985D221ABD8700324BF3 /* ViewController.m */, 115 | A7E5985F221ABD8700324BF3 /* Main.storyboard */, 116 | A7E59862221ABD8800324BF3 /* Assets.xcassets */, 117 | A7E59864221ABD8800324BF3 /* LaunchScreen.storyboard */, 118 | A7E59867221ABD8800324BF3 /* Info.plist */, 119 | A7E59868221ABD8800324BF3 /* main.m */, 120 | ); 121 | path = SimplePingDemo; 122 | sourceTree = ""; 123 | }; 124 | A7E59871221ABD8800324BF3 /* SimplePingDemoTests */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | A7E59872221ABD8800324BF3 /* SimplePingDemoTests.m */, 128 | A7E59874221ABD8800324BF3 /* Info.plist */, 129 | ); 130 | path = SimplePingDemoTests; 131 | sourceTree = ""; 132 | }; 133 | A7E5987C221ABD8800324BF3 /* SimplePingDemoUITests */ = { 134 | isa = PBXGroup; 135 | children = ( 136 | A7E5987D221ABD8800324BF3 /* SimplePingDemoUITests.m */, 137 | A7E5987F221ABD8800324BF3 /* Info.plist */, 138 | ); 139 | path = SimplePingDemoUITests; 140 | sourceTree = ""; 141 | }; 142 | A7E5988B221ABD8F00324BF3 /* SimplePing */ = { 143 | isa = PBXGroup; 144 | children = ( 145 | A7E5988D221ABF2500324BF3 /* SimplePing.h */, 146 | A7E5988C221ABF2500324BF3 /* SimplePing.m */, 147 | A7E5988F221ABF4800324BF3 /* FFSimplePingHelper.h */, 148 | A7E59890221ABF4800324BF3 /* FFSimplePingHelper.m */, 149 | ); 150 | path = SimplePing; 151 | sourceTree = ""; 152 | }; 153 | /* End PBXGroup section */ 154 | 155 | /* Begin PBXNativeTarget section */ 156 | A7E59855221ABD8700324BF3 /* SimplePingDemo */ = { 157 | isa = PBXNativeTarget; 158 | buildConfigurationList = A7E59882221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemo" */; 159 | buildPhases = ( 160 | A7E59852221ABD8700324BF3 /* Sources */, 161 | A7E59853221ABD8700324BF3 /* Frameworks */, 162 | A7E59854221ABD8700324BF3 /* Resources */, 163 | ); 164 | buildRules = ( 165 | ); 166 | dependencies = ( 167 | ); 168 | name = SimplePingDemo; 169 | productName = SimplePingDemo; 170 | productReference = A7E59856221ABD8700324BF3 /* SimplePingDemo.app */; 171 | productType = "com.apple.product-type.application"; 172 | }; 173 | A7E5986D221ABD8800324BF3 /* SimplePingDemoTests */ = { 174 | isa = PBXNativeTarget; 175 | buildConfigurationList = A7E59885221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemoTests" */; 176 | buildPhases = ( 177 | A7E5986A221ABD8800324BF3 /* Sources */, 178 | A7E5986B221ABD8800324BF3 /* Frameworks */, 179 | A7E5986C221ABD8800324BF3 /* Resources */, 180 | ); 181 | buildRules = ( 182 | ); 183 | dependencies = ( 184 | A7E59870221ABD8800324BF3 /* PBXTargetDependency */, 185 | ); 186 | name = SimplePingDemoTests; 187 | productName = SimplePingDemoTests; 188 | productReference = A7E5986E221ABD8800324BF3 /* SimplePingDemoTests.xctest */; 189 | productType = "com.apple.product-type.bundle.unit-test"; 190 | }; 191 | A7E59878221ABD8800324BF3 /* SimplePingDemoUITests */ = { 192 | isa = PBXNativeTarget; 193 | buildConfigurationList = A7E59888221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemoUITests" */; 194 | buildPhases = ( 195 | A7E59875221ABD8800324BF3 /* Sources */, 196 | A7E59876221ABD8800324BF3 /* Frameworks */, 197 | A7E59877221ABD8800324BF3 /* Resources */, 198 | ); 199 | buildRules = ( 200 | ); 201 | dependencies = ( 202 | A7E5987B221ABD8800324BF3 /* PBXTargetDependency */, 203 | ); 204 | name = SimplePingDemoUITests; 205 | productName = SimplePingDemoUITests; 206 | productReference = A7E59879221ABD8800324BF3 /* SimplePingDemoUITests.xctest */; 207 | productType = "com.apple.product-type.bundle.ui-testing"; 208 | }; 209 | /* End PBXNativeTarget section */ 210 | 211 | /* Begin PBXProject section */ 212 | A7E5984E221ABD8700324BF3 /* Project object */ = { 213 | isa = PBXProject; 214 | attributes = { 215 | LastUpgradeCheck = 1010; 216 | ORGANIZATIONNAME = MoGuJie; 217 | TargetAttributes = { 218 | A7E59855221ABD8700324BF3 = { 219 | CreatedOnToolsVersion = 10.1; 220 | }; 221 | A7E5986D221ABD8800324BF3 = { 222 | CreatedOnToolsVersion = 10.1; 223 | TestTargetID = A7E59855221ABD8700324BF3; 224 | }; 225 | A7E59878221ABD8800324BF3 = { 226 | CreatedOnToolsVersion = 10.1; 227 | TestTargetID = A7E59855221ABD8700324BF3; 228 | }; 229 | }; 230 | }; 231 | buildConfigurationList = A7E59851221ABD8700324BF3 /* Build configuration list for PBXProject "SimplePingDemo" */; 232 | compatibilityVersion = "Xcode 9.3"; 233 | developmentRegion = en; 234 | hasScannedForEncodings = 0; 235 | knownRegions = ( 236 | en, 237 | Base, 238 | ); 239 | mainGroup = A7E5984D221ABD8700324BF3; 240 | productRefGroup = A7E59857221ABD8700324BF3 /* Products */; 241 | projectDirPath = ""; 242 | projectRoot = ""; 243 | targets = ( 244 | A7E59855221ABD8700324BF3 /* SimplePingDemo */, 245 | A7E5986D221ABD8800324BF3 /* SimplePingDemoTests */, 246 | A7E59878221ABD8800324BF3 /* SimplePingDemoUITests */, 247 | ); 248 | }; 249 | /* End PBXProject section */ 250 | 251 | /* Begin PBXResourcesBuildPhase section */ 252 | A7E59854221ABD8700324BF3 /* Resources */ = { 253 | isa = PBXResourcesBuildPhase; 254 | buildActionMask = 2147483647; 255 | files = ( 256 | A7E59866221ABD8800324BF3 /* LaunchScreen.storyboard in Resources */, 257 | A7E59863221ABD8800324BF3 /* Assets.xcassets in Resources */, 258 | A7E59861221ABD8700324BF3 /* Main.storyboard in Resources */, 259 | ); 260 | runOnlyForDeploymentPostprocessing = 0; 261 | }; 262 | A7E5986C221ABD8800324BF3 /* Resources */ = { 263 | isa = PBXResourcesBuildPhase; 264 | buildActionMask = 2147483647; 265 | files = ( 266 | ); 267 | runOnlyForDeploymentPostprocessing = 0; 268 | }; 269 | A7E59877221ABD8800324BF3 /* Resources */ = { 270 | isa = PBXResourcesBuildPhase; 271 | buildActionMask = 2147483647; 272 | files = ( 273 | ); 274 | runOnlyForDeploymentPostprocessing = 0; 275 | }; 276 | /* End PBXResourcesBuildPhase section */ 277 | 278 | /* Begin PBXSourcesBuildPhase section */ 279 | A7E59852221ABD8700324BF3 /* Sources */ = { 280 | isa = PBXSourcesBuildPhase; 281 | buildActionMask = 2147483647; 282 | files = ( 283 | A7E5985E221ABD8700324BF3 /* ViewController.m in Sources */, 284 | A7E59869221ABD8800324BF3 /* main.m in Sources */, 285 | A7E5985B221ABD8700324BF3 /* AppDelegate.m in Sources */, 286 | A7E59891221ABF4800324BF3 /* FFSimplePingHelper.m in Sources */, 287 | A7E5988E221ABF2500324BF3 /* SimplePing.m in Sources */, 288 | ); 289 | runOnlyForDeploymentPostprocessing = 0; 290 | }; 291 | A7E5986A221ABD8800324BF3 /* Sources */ = { 292 | isa = PBXSourcesBuildPhase; 293 | buildActionMask = 2147483647; 294 | files = ( 295 | A7E59873221ABD8800324BF3 /* SimplePingDemoTests.m in Sources */, 296 | ); 297 | runOnlyForDeploymentPostprocessing = 0; 298 | }; 299 | A7E59875221ABD8800324BF3 /* Sources */ = { 300 | isa = PBXSourcesBuildPhase; 301 | buildActionMask = 2147483647; 302 | files = ( 303 | A7E5987E221ABD8800324BF3 /* SimplePingDemoUITests.m in Sources */, 304 | ); 305 | runOnlyForDeploymentPostprocessing = 0; 306 | }; 307 | /* End PBXSourcesBuildPhase section */ 308 | 309 | /* Begin PBXTargetDependency section */ 310 | A7E59870221ABD8800324BF3 /* PBXTargetDependency */ = { 311 | isa = PBXTargetDependency; 312 | target = A7E59855221ABD8700324BF3 /* SimplePingDemo */; 313 | targetProxy = A7E5986F221ABD8800324BF3 /* PBXContainerItemProxy */; 314 | }; 315 | A7E5987B221ABD8800324BF3 /* PBXTargetDependency */ = { 316 | isa = PBXTargetDependency; 317 | target = A7E59855221ABD8700324BF3 /* SimplePingDemo */; 318 | targetProxy = A7E5987A221ABD8800324BF3 /* PBXContainerItemProxy */; 319 | }; 320 | /* End PBXTargetDependency section */ 321 | 322 | /* Begin PBXVariantGroup section */ 323 | A7E5985F221ABD8700324BF3 /* Main.storyboard */ = { 324 | isa = PBXVariantGroup; 325 | children = ( 326 | A7E59860221ABD8700324BF3 /* Base */, 327 | ); 328 | name = Main.storyboard; 329 | sourceTree = ""; 330 | }; 331 | A7E59864221ABD8800324BF3 /* LaunchScreen.storyboard */ = { 332 | isa = PBXVariantGroup; 333 | children = ( 334 | A7E59865221ABD8800324BF3 /* Base */, 335 | ); 336 | name = LaunchScreen.storyboard; 337 | sourceTree = ""; 338 | }; 339 | /* End PBXVariantGroup section */ 340 | 341 | /* Begin XCBuildConfiguration section */ 342 | A7E59880221ABD8800324BF3 /* Debug */ = { 343 | isa = XCBuildConfiguration; 344 | buildSettings = { 345 | ALWAYS_SEARCH_USER_PATHS = NO; 346 | CLANG_ANALYZER_NONNULL = YES; 347 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 348 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 349 | CLANG_CXX_LIBRARY = "libc++"; 350 | CLANG_ENABLE_MODULES = YES; 351 | CLANG_ENABLE_OBJC_ARC = YES; 352 | CLANG_ENABLE_OBJC_WEAK = YES; 353 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 354 | CLANG_WARN_BOOL_CONVERSION = YES; 355 | CLANG_WARN_COMMA = YES; 356 | CLANG_WARN_CONSTANT_CONVERSION = YES; 357 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 358 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 359 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 360 | CLANG_WARN_EMPTY_BODY = YES; 361 | CLANG_WARN_ENUM_CONVERSION = YES; 362 | CLANG_WARN_INFINITE_RECURSION = YES; 363 | CLANG_WARN_INT_CONVERSION = YES; 364 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 365 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 366 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 367 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 368 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 369 | CLANG_WARN_STRICT_PROTOTYPES = YES; 370 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 371 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 372 | CLANG_WARN_UNREACHABLE_CODE = YES; 373 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 374 | CODE_SIGN_IDENTITY = "iPhone Developer"; 375 | COPY_PHASE_STRIP = NO; 376 | DEBUG_INFORMATION_FORMAT = dwarf; 377 | ENABLE_STRICT_OBJC_MSGSEND = YES; 378 | ENABLE_TESTABILITY = YES; 379 | GCC_C_LANGUAGE_STANDARD = gnu11; 380 | GCC_DYNAMIC_NO_PIC = NO; 381 | GCC_NO_COMMON_BLOCKS = YES; 382 | GCC_OPTIMIZATION_LEVEL = 0; 383 | GCC_PREPROCESSOR_DEFINITIONS = ( 384 | "DEBUG=1", 385 | "$(inherited)", 386 | ); 387 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 388 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 389 | GCC_WARN_UNDECLARED_SELECTOR = YES; 390 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 391 | GCC_WARN_UNUSED_FUNCTION = YES; 392 | GCC_WARN_UNUSED_VARIABLE = YES; 393 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 394 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 395 | MTL_FAST_MATH = YES; 396 | ONLY_ACTIVE_ARCH = YES; 397 | SDKROOT = iphoneos; 398 | }; 399 | name = Debug; 400 | }; 401 | A7E59881221ABD8800324BF3 /* Release */ = { 402 | isa = XCBuildConfiguration; 403 | buildSettings = { 404 | ALWAYS_SEARCH_USER_PATHS = NO; 405 | CLANG_ANALYZER_NONNULL = YES; 406 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 407 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 408 | CLANG_CXX_LIBRARY = "libc++"; 409 | CLANG_ENABLE_MODULES = YES; 410 | CLANG_ENABLE_OBJC_ARC = YES; 411 | CLANG_ENABLE_OBJC_WEAK = YES; 412 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 413 | CLANG_WARN_BOOL_CONVERSION = YES; 414 | CLANG_WARN_COMMA = YES; 415 | CLANG_WARN_CONSTANT_CONVERSION = YES; 416 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 417 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 418 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 419 | CLANG_WARN_EMPTY_BODY = YES; 420 | CLANG_WARN_ENUM_CONVERSION = YES; 421 | CLANG_WARN_INFINITE_RECURSION = YES; 422 | CLANG_WARN_INT_CONVERSION = YES; 423 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 424 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 425 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 426 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 427 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 428 | CLANG_WARN_STRICT_PROTOTYPES = YES; 429 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 430 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 431 | CLANG_WARN_UNREACHABLE_CODE = YES; 432 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 433 | CODE_SIGN_IDENTITY = "iPhone Developer"; 434 | COPY_PHASE_STRIP = NO; 435 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 436 | ENABLE_NS_ASSERTIONS = NO; 437 | ENABLE_STRICT_OBJC_MSGSEND = YES; 438 | GCC_C_LANGUAGE_STANDARD = gnu11; 439 | GCC_NO_COMMON_BLOCKS = YES; 440 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 441 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 442 | GCC_WARN_UNDECLARED_SELECTOR = YES; 443 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 444 | GCC_WARN_UNUSED_FUNCTION = YES; 445 | GCC_WARN_UNUSED_VARIABLE = YES; 446 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 447 | MTL_ENABLE_DEBUG_INFO = NO; 448 | MTL_FAST_MATH = YES; 449 | SDKROOT = iphoneos; 450 | VALIDATE_PRODUCT = YES; 451 | }; 452 | name = Release; 453 | }; 454 | A7E59883221ABD8800324BF3 /* Debug */ = { 455 | isa = XCBuildConfiguration; 456 | buildSettings = { 457 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 458 | CODE_SIGN_STYLE = Automatic; 459 | INFOPLIST_FILE = SimplePingDemo/Info.plist; 460 | LD_RUNPATH_SEARCH_PATHS = ( 461 | "$(inherited)", 462 | "@executable_path/Frameworks", 463 | ); 464 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemo; 465 | PRODUCT_NAME = "$(TARGET_NAME)"; 466 | TARGETED_DEVICE_FAMILY = "1,2"; 467 | }; 468 | name = Debug; 469 | }; 470 | A7E59884221ABD8800324BF3 /* Release */ = { 471 | isa = XCBuildConfiguration; 472 | buildSettings = { 473 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 474 | CODE_SIGN_STYLE = Automatic; 475 | INFOPLIST_FILE = SimplePingDemo/Info.plist; 476 | LD_RUNPATH_SEARCH_PATHS = ( 477 | "$(inherited)", 478 | "@executable_path/Frameworks", 479 | ); 480 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemo; 481 | PRODUCT_NAME = "$(TARGET_NAME)"; 482 | TARGETED_DEVICE_FAMILY = "1,2"; 483 | }; 484 | name = Release; 485 | }; 486 | A7E59886221ABD8800324BF3 /* Debug */ = { 487 | isa = XCBuildConfiguration; 488 | buildSettings = { 489 | BUNDLE_LOADER = "$(TEST_HOST)"; 490 | CODE_SIGN_STYLE = Automatic; 491 | INFOPLIST_FILE = SimplePingDemoTests/Info.plist; 492 | LD_RUNPATH_SEARCH_PATHS = ( 493 | "$(inherited)", 494 | "@executable_path/Frameworks", 495 | "@loader_path/Frameworks", 496 | ); 497 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemoTests; 498 | PRODUCT_NAME = "$(TARGET_NAME)"; 499 | TARGETED_DEVICE_FAMILY = "1,2"; 500 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SimplePingDemo.app/SimplePingDemo"; 501 | }; 502 | name = Debug; 503 | }; 504 | A7E59887221ABD8800324BF3 /* Release */ = { 505 | isa = XCBuildConfiguration; 506 | buildSettings = { 507 | BUNDLE_LOADER = "$(TEST_HOST)"; 508 | CODE_SIGN_STYLE = Automatic; 509 | INFOPLIST_FILE = SimplePingDemoTests/Info.plist; 510 | LD_RUNPATH_SEARCH_PATHS = ( 511 | "$(inherited)", 512 | "@executable_path/Frameworks", 513 | "@loader_path/Frameworks", 514 | ); 515 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemoTests; 516 | PRODUCT_NAME = "$(TARGET_NAME)"; 517 | TARGETED_DEVICE_FAMILY = "1,2"; 518 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SimplePingDemo.app/SimplePingDemo"; 519 | }; 520 | name = Release; 521 | }; 522 | A7E59889221ABD8800324BF3 /* Debug */ = { 523 | isa = XCBuildConfiguration; 524 | buildSettings = { 525 | CODE_SIGN_STYLE = Automatic; 526 | INFOPLIST_FILE = SimplePingDemoUITests/Info.plist; 527 | LD_RUNPATH_SEARCH_PATHS = ( 528 | "$(inherited)", 529 | "@executable_path/Frameworks", 530 | "@loader_path/Frameworks", 531 | ); 532 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemoUITests; 533 | PRODUCT_NAME = "$(TARGET_NAME)"; 534 | TARGETED_DEVICE_FAMILY = "1,2"; 535 | TEST_TARGET_NAME = SimplePingDemo; 536 | }; 537 | name = Debug; 538 | }; 539 | A7E5988A221ABD8800324BF3 /* Release */ = { 540 | isa = XCBuildConfiguration; 541 | buildSettings = { 542 | CODE_SIGN_STYLE = Automatic; 543 | INFOPLIST_FILE = SimplePingDemoUITests/Info.plist; 544 | LD_RUNPATH_SEARCH_PATHS = ( 545 | "$(inherited)", 546 | "@executable_path/Frameworks", 547 | "@loader_path/Frameworks", 548 | ); 549 | PRODUCT_BUNDLE_IDENTIFIER = MLPF.SimplePingDemoUITests; 550 | PRODUCT_NAME = "$(TARGET_NAME)"; 551 | TARGETED_DEVICE_FAMILY = "1,2"; 552 | TEST_TARGET_NAME = SimplePingDemo; 553 | }; 554 | name = Release; 555 | }; 556 | /* End XCBuildConfiguration section */ 557 | 558 | /* Begin XCConfigurationList section */ 559 | A7E59851221ABD8700324BF3 /* Build configuration list for PBXProject "SimplePingDemo" */ = { 560 | isa = XCConfigurationList; 561 | buildConfigurations = ( 562 | A7E59880221ABD8800324BF3 /* Debug */, 563 | A7E59881221ABD8800324BF3 /* Release */, 564 | ); 565 | defaultConfigurationIsVisible = 0; 566 | defaultConfigurationName = Release; 567 | }; 568 | A7E59882221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemo" */ = { 569 | isa = XCConfigurationList; 570 | buildConfigurations = ( 571 | A7E59883221ABD8800324BF3 /* Debug */, 572 | A7E59884221ABD8800324BF3 /* Release */, 573 | ); 574 | defaultConfigurationIsVisible = 0; 575 | defaultConfigurationName = Release; 576 | }; 577 | A7E59885221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemoTests" */ = { 578 | isa = XCConfigurationList; 579 | buildConfigurations = ( 580 | A7E59886221ABD8800324BF3 /* Debug */, 581 | A7E59887221ABD8800324BF3 /* Release */, 582 | ); 583 | defaultConfigurationIsVisible = 0; 584 | defaultConfigurationName = Release; 585 | }; 586 | A7E59888221ABD8800324BF3 /* Build configuration list for PBXNativeTarget "SimplePingDemoUITests" */ = { 587 | isa = XCConfigurationList; 588 | buildConfigurations = ( 589 | A7E59889221ABD8800324BF3 /* Debug */, 590 | A7E5988A221ABD8800324BF3 /* Release */, 591 | ); 592 | defaultConfigurationIsVisible = 0; 593 | defaultConfigurationName = Release; 594 | }; 595 | /* End XCConfigurationList section */ 596 | }; 597 | rootObject = A7E5984E221ABD8700324BF3 /* Project object */; 598 | } 599 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/SimplePing/FFSimplePingHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // FFSimplePingHelper.h 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface FFSimplePingHelper : NSObject 14 | 15 | - (instancetype)initWithHostName:(NSString*)hostName; 16 | @property(nonatomic, readonly) NSString *hostName; 17 | - (void)startPing; 18 | - (void)stopPing; 19 | 20 | @end 21 | 22 | NS_ASSUME_NONNULL_END 23 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/SimplePing/FFSimplePingHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // FFSimplePingHelper.m 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import "FFSimplePingHelper.h" 10 | #import "SimplePing.h" 11 | #include 12 | 13 | //发送数据的间隙 14 | #define PING_TIME_INTERVAL 1 15 | //超时时间 16 | #define PING_TIMEOUT 1.5 17 | //发送的ping的总次数 18 | #define kSequenceNumber 20 19 | 20 | #define PING_TIMEOUT_DELAY 1000 21 | 22 | @interface FFSimplePingHelper () 23 | 24 | @property (nonatomic, strong) SimplePing *simplePing; 25 | @property (nonatomic, strong) NSTimer *timer; 26 | /**目标域名的IP地址*/ 27 | @property (nonatomic, copy) NSString *iPAddress; 28 | /**开始发送数据的时间*/ 29 | @property (nonatomic) NSTimeInterval startTimeInterval; 30 | /**消耗的时间*/ 31 | @property (nonatomic) NSTimeInterval delayTime; 32 | /**接收到数据或者丢失的数据的次数*/ 33 | @property (nonatomic, assign) NSInteger receivedOrDelayCount; 34 | /**发出的数据包*/ 35 | @property (nonatomic) NSUInteger sendPackets; 36 | /**收到的数据包*/ 37 | @property (nonatomic) NSUInteger receivePackets; 38 | /**丢包率*/ 39 | @property (nonatomic, assign) double packetLoss; 40 | 41 | @end 42 | 43 | @implementation FFSimplePingHelper 44 | 45 | - (instancetype)initWithHostName:(NSString*)hostName 46 | { 47 | if (self = [super init]){ 48 | self.simplePing = [[SimplePing alloc] initWithHostName:@"www.baidu.com"]; 49 | self.simplePing.addressStyle = SimplePingAddressStyleAny; 50 | self.simplePing.delegate = self; 51 | self.sendPackets = 0; 52 | self.receivePackets = 0; 53 | } 54 | return self; 55 | } 56 | 57 | - (void)startPing{ 58 | [self.simplePing start]; 59 | } 60 | 61 | #pragma mark - SimplePingDelegate 62 | 63 | // [self.simplePing start] 成功之后 64 | - (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address{ 65 | self.iPAddress = [self displayIPFormAddress:address]; 66 | self.timer = [NSTimer scheduledTimerWithTimeInterval:PING_TIME_INTERVAL repeats:YES block:^(NSTimer * _Nonnull timer) { 67 | [self sendPingData]; 68 | }]; 69 | } 70 | 71 | - (void)sendPingData{ 72 | //执行到指定次数后停止时间搓 73 | if (self.receivedOrDelayCount == kSequenceNumber) { 74 | [self stopPing]; 75 | } 76 | 77 | self.startTimeInterval = [NSDate timeIntervalSinceReferenceDate]; 78 | [self.simplePing sendPingWithData:nil]; 79 | //超时问题处理 80 | [self performSelector:@selector(pingTimeout) withObject:nil afterDelay:PING_TIMEOUT]; 81 | } 82 | 83 | // [self.simplePing start] 失败 84 | - (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error{ 85 | 86 | NSString *failCreateLog = [NSString stringWithFormat:@"#%ld try create failed: %@", self.receivedOrDelayCount,[self shortErrorFromError:error]]; 87 | NSLog(@"didFailWithError:%@",failCreateLog); 88 | //启动发送data失败 89 | [self stopPing]; 90 | [self cancelTimeOut]; 91 | } 92 | 93 | //取消超时 94 | - (void)cancelTimeOut{ 95 | [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(pingTimeout) object:nil]; 96 | } 97 | 98 | //sendPingWithData 发送数据成功 99 | - (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber{ 100 | self.sendPackets++; 101 | [self cancelTimeOut]; 102 | } 103 | 104 | // sendPingWithData 发送数据失败,并返回错误信息 105 | - (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error{ 106 | self.receivedOrDelayCount++; 107 | [self cancelTimeOut]; 108 | NSString *sendFailLog = [NSString stringWithFormat:@"#%u send failed: %@",sequenceNumber,[self shortErrorFromError:error]]; 109 | NSLog(@"didFailToSendPacket:%@",sendFailLog); 110 | } 111 | 112 | //发送数据后接收到主机返回应答数据的回调 113 | - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber{ 114 | 115 | self.receivedOrDelayCount++; 116 | [self cancelTimeOut]; 117 | 118 | self.receivePackets++; 119 | 120 | self.packetLoss = (double)((self.sendPackets - self.receivePackets) * 1.f / self.sendPackets * 100); 121 | 122 | self.delayTime = ([NSDate timeIntervalSinceReferenceDate] - self.startTimeInterval) * 1000; 123 | 124 | if (self.receivedOrDelayCount == kSequenceNumber) { 125 | [self stopPing]; 126 | } 127 | 128 | NSLog(@"ip:%@ #%u received, size=%zu time=%f loss=%f", self.iPAddress,sequenceNumber, packet.length,self.delayTime,self.packetLoss); 129 | 130 | } 131 | 132 | // 收到的未知的数据包 133 | - (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet{ 134 | 135 | } 136 | 137 | 138 | -(void)pingTimeout{ 139 | 140 | NSLog(@"++++超时了+++"); 141 | if (self.timer) { 142 | self.receivedOrDelayCount++; 143 | 144 | self.packetLoss = (double)((self.sendPackets - self.receivePackets) * 1.f / self.sendPackets * 100); 145 | 146 | if (self.receivedOrDelayCount == kSequenceNumber) { 147 | [self stopPing]; 148 | } 149 | } 150 | } 151 | 152 | -(void)stopPing{ 153 | [self.simplePing stop]; 154 | self.simplePing = nil; 155 | 156 | if (self.timer) { 157 | [self.timer invalidate]; 158 | self.timer = nil; 159 | } 160 | self.delayTime = 0; 161 | } 162 | 163 | /** 164 | * 将ping接收的数据转换成ip地址 165 | * @param address 接受的ping数据 166 | */ 167 | -(NSString *)displayIPFormAddress:(NSData *)address 168 | { 169 | int err; 170 | NSString *result; 171 | char hostStr[NI_MAXHOST]; 172 | 173 | result = nil; 174 | 175 | if (address != nil) { 176 | err = getnameinfo([address bytes], (socklen_t)[address length], hostStr, sizeof(hostStr), 177 | NULL, 0, NI_NUMERICHOST); 178 | if (err == 0) { 179 | result = [NSString stringWithCString:hostStr encoding:NSASCIIStringEncoding]; 180 | assert(result != nil); 181 | } 182 | } 183 | 184 | return result; 185 | } 186 | 187 | 188 | /* 189 | * 解析错误数据并翻译 190 | */ 191 | - (NSString *)shortErrorFromError:(NSError *)error 192 | { 193 | NSString *result; 194 | NSNumber *failureNum; 195 | int failure; 196 | const char *failureStr; 197 | 198 | assert(error != nil); 199 | 200 | result = nil; 201 | 202 | // Handle DNS errors as a special case. 203 | 204 | if ([[error domain] isEqual:(NSString *)kCFErrorDomainCFNetwork] && 205 | ([error code] == kCFHostErrorUnknown)) { 206 | failureNum = [[error userInfo] objectForKey:(id)kCFGetAddrInfoFailureKey]; 207 | if ([failureNum isKindOfClass:[NSNumber class]]) { 208 | failure = [failureNum intValue]; 209 | if (failure != 0) { 210 | failureStr = gai_strerror(failure); 211 | if (failureStr != NULL) { 212 | result = [NSString stringWithUTF8String:failureStr]; 213 | assert(result != nil); 214 | } 215 | } 216 | } 217 | } 218 | 219 | // Otherwise try various properties of the error object. 220 | 221 | if (result == nil) { 222 | result = [error localizedFailureReason]; 223 | } 224 | if (result == nil) { 225 | result = [error localizedDescription]; 226 | } 227 | if (result == nil) { 228 | result = [error description]; 229 | } 230 | assert(result != nil); 231 | return result; 232 | } 233 | 234 | 235 | @end 236 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/SimplePing/SimplePing.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | An object wrapper around the low-level BSD Sockets ping function. 7 | 8 | Modify by guoqingwe. Set Ping timeout to 1.5s. 9 | */ 10 | 11 | @import Foundation; 12 | 13 | #include // for __Check_Compile_Time 14 | 15 | NS_ASSUME_NONNULL_BEGIN 16 | 17 | @protocol SimplePingDelegate; 18 | 19 | /*! Controls the IP address version used by SimplePing instances. 20 | */ 21 | 22 | typedef NS_ENUM(NSInteger, SimplePingAddressStyle) { 23 | SimplePingAddressStyleAny, ///< Use the first IPv4 or IPv6 address found; the default. 24 | SimplePingAddressStyleICMPv4, ///< Use the first IPv4 address found. 25 | SimplePingAddressStyleICMPv6 ///< Use the first IPv6 address found. 26 | }; 27 | 28 | /*! An object wrapper around the low-level BSD Sockets ping function. 29 | * \details To use the class create an instance, set the delegate and call `-start` 30 | * to start the instance on the current run loop. If things go well you'll soon get the 31 | * `-simplePing:didStartWithAddress:` delegate callback. From there you can can call 32 | * `-sendPingWithData:` to send a ping and you'll receive the 33 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:` and 34 | * `-simplePing:didReceiveUnexpectedPacket:` delegate callbacks as ICMP packets arrive. 35 | * 36 | * The class can be used from any thread but the use of any single instance must be 37 | * confined to a specific thread and that thread must run its run loop. 38 | */ 39 | 40 | @interface SimplePing : NSObject 41 | 42 | - (instancetype)init NS_UNAVAILABLE; 43 | 44 | /*! Initialise the object to ping the specified host. 45 | * \param hostName The DNS name of the host to ping; an IPv4 or IPv6 address in string form will 46 | * work here. 47 | * \returns The initialised object. 48 | */ 49 | 50 | - (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER; 51 | 52 | /*! A copy of the value passed to `-initWithHostName:`. 53 | */ 54 | 55 | @property (nonatomic, copy, readonly) NSString * hostName; 56 | 57 | /*! The delegate for this object. 58 | * \details Delegate callbacks are schedule in the default run loop mode of the run loop of the 59 | * thread that calls `-start`. 60 | */ 61 | 62 | @property (nonatomic, weak, readwrite, nullable) id delegate; 63 | 64 | /*! Controls the IP address version used by the object. 65 | * \details You should set this value before starting the object. 66 | */ 67 | 68 | @property (nonatomic, assign, readwrite) SimplePingAddressStyle addressStyle; 69 | 70 | /*! The address being pinged. 71 | * \details The contents of the NSData is a (struct sockaddr) of some form. The 72 | * value is nil while the object is stopped and remains nil on start until 73 | * `-simplePing:didStartWithAddress:` is called. 74 | */ 75 | 76 | @property (nonatomic, copy, readonly, nullable) NSData * hostAddress; 77 | 78 | /*! The address family for `hostAddress`, or `AF_UNSPEC` if that's nil. 79 | */ 80 | 81 | @property (nonatomic, assign, readonly) sa_family_t hostAddressFamily; 82 | 83 | /*! The identifier used by pings by this object. 84 | * \details When you create an instance of this object it generates a random identifier 85 | * that it uses to identify its own pings. 86 | */ 87 | 88 | @property (nonatomic, assign, readonly) uint16_t identifier; 89 | 90 | /*! The next sequence number to be used by this object. 91 | * \details This value starts at zero and increments each time you send a ping (safely 92 | * wrapping back to zero if necessary). The sequence number is included in the ping, 93 | * allowing you to match up requests and responses, and thus calculate ping times and 94 | * so on. 95 | */ 96 | 97 | @property (nonatomic, assign, readonly) uint16_t nextSequenceNumber; 98 | 99 | /*! Starts the object. 100 | * \details You should set up the delegate and any ping parameters before calling this. 101 | * 102 | * If things go well you'll soon get the `-simplePing:didStartWithAddress:` delegate 103 | * callback, at which point you can start sending pings (via `-sendPingWithData:`) and 104 | * will start receiving ICMP packets (either ping responses, via the 105 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:` delegate callback, or 106 | * unsolicited ICMP packets, via the `-simplePing:didReceiveUnexpectedPacket:` delegate 107 | * callback). 108 | * 109 | * If the object fails to start, typically because `hostName` doesn't resolve, you'll get 110 | * the `-simplePing:didFailWithError:` delegate callback. 111 | * 112 | * It is not correct to start an already started object. 113 | */ 114 | 115 | - (void)start; 116 | 117 | /*! Sends a ping packet containing the specified data. 118 | * \details Sends an actual ping. 119 | * 120 | * The object must be started when you call this method and, on starting the object, you must 121 | * wait for the `-simplePing:didStartWithAddress:` delegate callback before calling it. 122 | * \param data Some data to include in the ping packet, after the ICMP header, or nil if you 123 | * want the packet to include a standard 56 byte payload (resulting in a standard 64 byte 124 | * ping). 125 | */ 126 | 127 | - (void)sendPingWithData:(nullable NSData *)data; 128 | 129 | /*! Stops the object. 130 | * \details You should call this when you're done pinging. 131 | * 132 | * It's safe to call this on an object that's stopped. 133 | */ 134 | 135 | - (void)stop; 136 | 137 | @end 138 | 139 | /*! A delegate protocol for the SimplePing class. 140 | */ 141 | 142 | @protocol SimplePingDelegate 143 | 144 | @optional 145 | 146 | /*! A SimplePing delegate callback, called once the object has started up. 147 | * \details This is called shortly after you start the object to tell you that the 148 | * object has successfully started. On receiving this callback, you can call 149 | * `-sendPingWithData:` to send pings. 150 | * 151 | * If the object didn't start, `-simplePing:didFailWithError:` is called instead. 152 | * \param pinger The object issuing the callback. 153 | * \param address The address that's being pinged; at the time this delegate callback 154 | * is made, this will have the same value as the `hostAddress` property. 155 | */ 156 | 157 | - (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address; 158 | 159 | /*! A SimplePing delegate callback, called if the object fails to start up. 160 | * \details This is called shortly after you start the object to tell you that the 161 | * object has failed to start. The most likely cause of failure is a problem 162 | * resolving `hostName`. 163 | * 164 | * By the time this callback is called, the object has stopped (that is, you don't 165 | * need to call `-stop` yourself). 166 | * \param pinger The object issuing the callback. 167 | * \param error Describes the failure. 168 | */ 169 | 170 | - (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error; 171 | 172 | /*! A SimplePing delegate callback, called when the object has successfully sent a ping packet. 173 | * \details Each call to `-sendPingWithData:` will result in either a 174 | * `-simplePing:didSendPacket:sequenceNumber:` delegate callback or a 175 | * `-simplePing:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 176 | * stop the object before you get the callback). These callbacks are currently delivered 177 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 178 | * considered API. 179 | * \param pinger The object issuing the callback. 180 | * \param packet The packet that was sent; this includes the ICMP header (`ICMPHeader`) and the 181 | * data you passed to `-sendPingWithData:` but does not include any IP-level headers. 182 | * \param sequenceNumber The ICMP sequence number of that packet. 183 | */ 184 | 185 | - (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 186 | 187 | /*! A SimplePing delegate callback, called when the object fails to send a ping packet. 188 | * \details Each call to `-sendPingWithData:` will result in either a 189 | * `-simplePing:didSendPacket:sequenceNumber:` delegate callback or a 190 | * `-simplePing:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 191 | * stop the object before you get the callback). These callbacks are currently delivered 192 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 193 | * considered API. 194 | * \param pinger The object issuing the callback. 195 | * \param packet The packet that was not sent; see `-simplePing:didSendPacket:sequenceNumber:` 196 | * for details. 197 | * \param sequenceNumber The ICMP sequence number of that packet. 198 | * \param error Describes the failure. 199 | */ 200 | 201 | - (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error; 202 | 203 | /*! A SimplePing delegate callback, called when the object receives a ping response. 204 | * \details If the object receives an ping response that matches a ping request that it 205 | * sent, it informs the delegate via this callback. Matching is primarily done based on 206 | * the ICMP identifier, although other criteria are used as well. 207 | * \param pinger The object issuing the callback. 208 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 209 | * follows that in the ICMP message but does not include any IP-level headers. 210 | * \param sequenceNumber The ICMP sequence number of that packet. 211 | */ 212 | 213 | - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 214 | 215 | /*! A SimplePing delegate callback, called when the object receives an unmatched ICMP message. 216 | * \details If the object receives an ICMP message that does not match a ping request that it 217 | * sent, it informs the delegate via this callback. The nature of ICMP handling in a 218 | * BSD kernel makes this a common event because, when an ICMP message arrives, it is 219 | * delivered to all ICMP sockets. 220 | * 221 | * IMPORTANT: This callback is especially common when using IPv6 because IPv6 uses ICMP 222 | * for important network management functions. For example, IPv6 routers periodically 223 | * send out Router Advertisement (RA) packets via Neighbor Discovery Protocol (NDP), which 224 | * is implemented on top of ICMP. 225 | * 226 | * For more on matching, see the discussion associated with 227 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:`. 228 | * \param pinger The object issuing the callback. 229 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 230 | * follows that in the ICMP message but does not include any IP-level headers. 231 | */ 232 | 233 | - (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet; 234 | 235 | @end 236 | 237 | #pragma mark * ICMP On-The-Wire Format 238 | 239 | /*! Describes the on-the-wire header format for an ICMP ping. 240 | * \details This defines the header structure of ping packets on the wire. Both IPv4 and 241 | * IPv6 use the same basic structure. 242 | * 243 | * This is declared in the header because clients of SimplePing might want to use 244 | * it parse received ping packets. 245 | */ 246 | 247 | struct ICMPHeader { 248 | uint8_t type; 249 | uint8_t code; 250 | uint16_t checksum; 251 | uint16_t identifier; 252 | uint16_t sequenceNumber; 253 | // data... 254 | }; 255 | typedef struct ICMPHeader ICMPHeader; 256 | 257 | __Check_Compile_Time(sizeof(ICMPHeader) == 8); 258 | __Check_Compile_Time(offsetof(ICMPHeader, type) == 0); 259 | __Check_Compile_Time(offsetof(ICMPHeader, code) == 1); 260 | __Check_Compile_Time(offsetof(ICMPHeader, checksum) == 2); 261 | __Check_Compile_Time(offsetof(ICMPHeader, identifier) == 4); 262 | __Check_Compile_Time(offsetof(ICMPHeader, sequenceNumber) == 6); 263 | 264 | enum { 265 | ICMPv4TypeEchoRequest = 8, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 266 | ICMPv4TypeEchoReply = 0 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 267 | }; 268 | 269 | enum { 270 | ICMPv6TypeEchoRequest = 128, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 271 | ICMPv6TypeEchoReply = 129 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 272 | }; 273 | 274 | NS_ASSUME_NONNULL_END 275 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/SimplePing/SimplePing.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | An object wrapper around the low-level BSD Sockets ping function. 7 | */ 8 | 9 | #import "SimplePing.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #pragma mark * IPv4 and ICMPv4 On-The-Wire Format 16 | 17 | /*! Describes the on-the-wire header format for an IPv4 packet. 18 | * \details This defines the header structure of IPv4 packets on the wire. We need 19 | * this in order to skip this header in the IPv4 case, where the kernel passes 20 | * it to us for no obvious reason. 21 | */ 22 | 23 | struct IPv4Header { 24 | uint8_t versionAndHeaderLength; 25 | uint8_t differentiatedServices; 26 | uint16_t totalLength; 27 | uint16_t identification; 28 | uint16_t flagsAndFragmentOffset; 29 | uint8_t timeToLive; 30 | uint8_t protocol; 31 | uint16_t headerChecksum; 32 | uint8_t sourceAddress[4]; 33 | uint8_t destinationAddress[4]; 34 | // options... 35 | // data... 36 | }; 37 | typedef struct IPv4Header IPv4Header; 38 | 39 | __Check_Compile_Time(sizeof(IPv4Header) == 20); 40 | __Check_Compile_Time(offsetof(IPv4Header, versionAndHeaderLength) == 0); 41 | __Check_Compile_Time(offsetof(IPv4Header, differentiatedServices) == 1); 42 | __Check_Compile_Time(offsetof(IPv4Header, totalLength) == 2); 43 | __Check_Compile_Time(offsetof(IPv4Header, identification) == 4); 44 | __Check_Compile_Time(offsetof(IPv4Header, flagsAndFragmentOffset) == 6); 45 | __Check_Compile_Time(offsetof(IPv4Header, timeToLive) == 8); 46 | __Check_Compile_Time(offsetof(IPv4Header, protocol) == 9); 47 | __Check_Compile_Time(offsetof(IPv4Header, headerChecksum) == 10); 48 | __Check_Compile_Time(offsetof(IPv4Header, sourceAddress) == 12); 49 | __Check_Compile_Time(offsetof(IPv4Header, destinationAddress) == 16); 50 | 51 | /*! Calculates an IP checksum. 52 | * \details This is the standard BSD checksum code, modified to use modern types. 53 | * \param buffer A pointer to the data to checksum. 54 | * \param bufferLen The length of that data. 55 | * \returns The checksum value, in network byte order. 56 | */ 57 | 58 | static uint16_t in_cksum(const void *buffer, size_t bufferLen) { 59 | // 60 | size_t bytesLeft; 61 | int32_t sum; 62 | const uint16_t * cursor; 63 | union { 64 | uint16_t us; 65 | uint8_t uc[2]; 66 | } last; 67 | uint16_t answer; 68 | 69 | bytesLeft = bufferLen; 70 | sum = 0; 71 | cursor = buffer; 72 | 73 | /* 74 | * Our algorithm is simple, using a 32 bit accumulator (sum), we add 75 | * sequential 16 bit words to it, and at the end, fold back all the 76 | * carry bits from the top 16 bits into the lower 16 bits. 77 | */ 78 | while (bytesLeft > 1) { 79 | sum += *cursor; 80 | cursor += 1; 81 | bytesLeft -= 2; 82 | } 83 | 84 | /* mop up an odd byte, if necessary */ 85 | if (bytesLeft == 1) { 86 | last.uc[0] = * (const uint8_t *) cursor; 87 | last.uc[1] = 0; 88 | sum += last.us; 89 | } 90 | 91 | /* add back carry outs from top 16 bits to low 16 bits */ 92 | sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 93 | sum += (sum >> 16); /* add carry */ 94 | answer = (uint16_t) ~sum; /* truncate to 16 bits */ 95 | 96 | return answer; 97 | } 98 | 99 | #pragma mark * SimplePing 100 | 101 | @interface SimplePing () 102 | 103 | // read/write versions of public properties 104 | 105 | @property (nonatomic, copy, readwrite, nullable) NSData * hostAddress; 106 | @property (nonatomic, assign, readwrite ) uint16_t nextSequenceNumber; 107 | 108 | // private properties 109 | 110 | /*! True if nextSequenceNumber has wrapped from 65535 to 0. 111 | */ 112 | 113 | @property (nonatomic, assign, readwrite) BOOL nextSequenceNumberHasWrapped; 114 | 115 | /*! A host object for name-to-address resolution. 116 | */ 117 | 118 | @property (nonatomic, strong, readwrite, nullable) CFHostRef host __attribute__ ((NSObject)); 119 | 120 | /*! A socket object for ICMP send and receive. 121 | */ 122 | 123 | @property (nonatomic, strong, readwrite, nullable) CFSocketRef socket __attribute__ ((NSObject)); 124 | 125 | @end 126 | 127 | @implementation SimplePing 128 | 129 | - (instancetype)initWithHostName:(NSString *)hostName { 130 | NSParameterAssert(hostName != nil); 131 | self = [super init]; 132 | if (self != nil) { 133 | self->_hostName = [hostName copy]; 134 | self->_identifier = (uint16_t) arc4random(); 135 | } 136 | return self; 137 | } 138 | 139 | - (void)dealloc { 140 | [self stop]; 141 | // Double check that -stop took care of _host and _socket. 142 | assert(self->_host == NULL); 143 | assert(self->_socket == NULL); 144 | } 145 | 146 | - (sa_family_t)hostAddressFamily { 147 | sa_family_t result; 148 | 149 | result = AF_UNSPEC; 150 | if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struct sockaddr)) ) { 151 | result = ((const struct sockaddr *) self.hostAddress.bytes)->sa_family; 152 | } 153 | return result; 154 | } 155 | 156 | /*! Shuts down the pinger object and tell the delegate about the error. 157 | * \param error Describes the failure. 158 | */ 159 | 160 | - (void)didFailWithError:(NSError *)error { 161 | id strongDelegate; 162 | 163 | assert(error != nil); 164 | 165 | // We retain ourselves temporarily because it's common for the delegate method 166 | // to release its last reference to us, which causes -dealloc to be called here. 167 | // If we then reference self on the return path, things go badly. I don't think 168 | // that happens currently, but I've got into the habit of doing this as a 169 | // defensive measure. 170 | 171 | CFAutorelease( CFBridgingRetain( self )); 172 | 173 | [self stop]; 174 | strongDelegate = self.delegate; 175 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didFailWithError:)] ) { 176 | [strongDelegate simplePing:self didFailWithError:error]; 177 | } 178 | } 179 | 180 | /*! Shuts down the pinger object and tell the delegate about the error. 181 | * \details This converts the CFStreamError to an NSError and then call through to 182 | * -didFailWithError: to do the real work. 183 | * \param streamError Describes the failure. 184 | */ 185 | 186 | - (void)didFailWithHostStreamError:(CFStreamError)streamError { 187 | NSDictionary * userInfo; 188 | NSError * error; 189 | 190 | if (streamError.domain == kCFStreamErrorDomainNetDB) { 191 | userInfo = @{(id) kCFGetAddrInfoFailureKey: @(streamError.error)}; 192 | } else { 193 | userInfo = nil; 194 | } 195 | error = [NSError errorWithDomain:(NSString *) kCFErrorDomainCFNetwork code:kCFHostErrorUnknown userInfo:userInfo]; 196 | 197 | [self didFailWithError:error]; 198 | } 199 | 200 | /*! Builds a ping packet from the supplied parameters. 201 | * \param type The packet type, which is different for IPv4 and IPv6. 202 | * \param payload Data to place after the ICMP header. 203 | * \param requiresChecksum Determines whether a checksum is calculated (IPv4) or not (IPv6). 204 | * \returns A ping packet suitable to be passed to the kernel. 205 | */ 206 | 207 | - (NSData *)pingPacketWithType:(uint8_t)type payload:(NSData *)payload requiresChecksum:(BOOL)requiresChecksum { 208 | NSMutableData * packet; 209 | ICMPHeader * icmpPtr; 210 | 211 | packet = [NSMutableData dataWithLength:sizeof(*icmpPtr) + payload.length]; 212 | assert(packet != nil); 213 | 214 | icmpPtr = packet.mutableBytes; 215 | icmpPtr->type = type; 216 | icmpPtr->code = 0; 217 | icmpPtr->checksum = 0; 218 | icmpPtr->identifier = OSSwapHostToBigInt16(self.identifier); 219 | icmpPtr->sequenceNumber = OSSwapHostToBigInt16(self.nextSequenceNumber); 220 | memcpy(&icmpPtr[1], [payload bytes], [payload length]); 221 | 222 | if (requiresChecksum) { 223 | // The IP checksum routine returns a 16-bit number that's already in correct byte order 224 | // (due to wacky 1's complement maths), so we just put it into the packet as a 16-bit unit. 225 | 226 | icmpPtr->checksum = in_cksum(packet.bytes, packet.length); 227 | } 228 | 229 | return packet; 230 | } 231 | 232 | - (void)sendPingWithData:(NSData *)data { 233 | int err; 234 | NSData * payload; 235 | NSData * packet; 236 | ssize_t bytesSent; 237 | id strongDelegate; 238 | 239 | // data may be nil 240 | NSParameterAssert(self.hostAddress != nil); // gotta wait for -simplePing:didStartWithAddress: 241 | 242 | // Construct the ping packet. 243 | 244 | payload = data; 245 | if (payload == nil) { 246 | payload = [[NSString stringWithFormat:@"%28zd bottles of beer on the wall", (ssize_t) 99 - (size_t) (self.nextSequenceNumber % 100) ] dataUsingEncoding:NSASCIIStringEncoding]; 247 | assert(payload != nil); 248 | 249 | // Our dummy payload is sized so that the resulting ICMP packet, including the ICMPHeader, is 250 | // 64-bytes, which makes it easier to recognise our packets on the wire. 251 | 252 | assert([payload length] == 56); 253 | } 254 | 255 | switch (self.hostAddressFamily) { 256 | case AF_INET: { 257 | packet = [self pingPacketWithType:ICMPv4TypeEchoRequest payload:payload requiresChecksum:YES]; 258 | } break; 259 | case AF_INET6: { 260 | packet = [self pingPacketWithType:ICMPv6TypeEchoRequest payload:payload requiresChecksum:NO]; 261 | } break; 262 | default: { 263 | assert(NO); 264 | } break; 265 | } 266 | assert(packet != nil); 267 | 268 | // Send the packet. 269 | 270 | if (self.socket == NULL) { 271 | bytesSent = -1; 272 | err = EBADF; 273 | } else { 274 | // Send Timeout:1.5s 275 | CFSocketNativeHandle sock = CFSocketGetNative(self->_socket); 276 | struct timeval tv; 277 | tv.tv_sec = 1; 278 | tv.tv_usec = 500000; 279 | 280 | setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(tv)); 281 | 282 | bytesSent = sendto( 283 | sock, 284 | packet.bytes, 285 | packet.length, 286 | 0, 287 | self.hostAddress.bytes, 288 | (socklen_t) self.hostAddress.length 289 | ); 290 | err = 0; 291 | if (bytesSent < 0) { 292 | err = errno; 293 | } 294 | } 295 | 296 | // Handle the results of the send. 297 | 298 | strongDelegate = self.delegate; 299 | if ( (bytesSent > 0) && (((NSUInteger) bytesSent) == packet.length) ) { 300 | 301 | // Complete success. Tell the client. 302 | 303 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didSendPacket:sequenceNumber:)] ) { 304 | [strongDelegate simplePing:self didSendPacket:packet sequenceNumber:self.nextSequenceNumber]; 305 | } 306 | } else { 307 | NSError * error; 308 | 309 | // Some sort of failure. Tell the client. 310 | 311 | if (err == 0) { 312 | err = ENOBUFS; // This is not a hugely descriptor error, alas. 313 | } 314 | error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]; 315 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didFailToSendPacket:sequenceNumber:error:)] ) { 316 | [strongDelegate simplePing:self didFailToSendPacket:packet sequenceNumber:self.nextSequenceNumber error:error]; 317 | } 318 | } 319 | 320 | self.nextSequenceNumber += 1; 321 | if (self.nextSequenceNumber == 0) { 322 | self.nextSequenceNumberHasWrapped = YES; 323 | } 324 | } 325 | 326 | /*! Calculates the offset of the ICMP header within an IPv4 packet. 327 | * \details In the IPv4 case the kernel returns us a buffer that includes the 328 | * IPv4 header. We're not interested in that, so we have to skip over it. 329 | * This code does a rough check of the IPv4 header and, if it looks OK, 330 | * returns the offset of the ICMP header. 331 | * \param packet The IPv4 packet, as returned to us by the kernel. 332 | * \returns The offset of the ICMP header, or NSNotFound. 333 | */ 334 | 335 | + (NSUInteger)icmpHeaderOffsetInIPv4Packet:(NSData *)packet { 336 | // Returns the offset of the ICMPv4Header within an IP packet. 337 | NSUInteger result; 338 | const struct IPv4Header * ipPtr; 339 | size_t ipHeaderLength; 340 | 341 | result = NSNotFound; 342 | if (packet.length >= (sizeof(IPv4Header) + sizeof(ICMPHeader))) { 343 | ipPtr = (const IPv4Header *) packet.bytes; 344 | if ( ((ipPtr->versionAndHeaderLength & 0xF0) == 0x40) && // IPv4 345 | ( ipPtr->protocol == IPPROTO_ICMP ) ) { 346 | ipHeaderLength = (ipPtr->versionAndHeaderLength & 0x0F) * sizeof(uint32_t); 347 | if (packet.length >= (ipHeaderLength + sizeof(ICMPHeader))) { 348 | result = ipHeaderLength; 349 | } 350 | } 351 | } 352 | return result; 353 | } 354 | 355 | /*! Checks whether the specified sequence number is one we sent. 356 | * \param sequenceNumber The incoming sequence number. 357 | * \returns YES if the sequence number looks like one we sent. 358 | */ 359 | 360 | - (BOOL)validateSequenceNumber:(uint16_t)sequenceNumber { 361 | if (self.nextSequenceNumberHasWrapped) { 362 | // If the sequence numbers have wrapped that we can't reliably check 363 | // whether this is a sequence number we sent. Rather, we check to see 364 | // whether the sequence number is within the last 120 sequence numbers 365 | // we sent. Note that the uint16_t subtraction here does the right 366 | // thing regardless of the wrapping. 367 | // 368 | // Why 120? Well, if we send one ping per second, 120 is 2 minutes, which 369 | // is the standard "max time a packet can bounce around the Internet" value. 370 | return ((uint16_t) (self.nextSequenceNumber - sequenceNumber)) < (uint16_t) 120; 371 | } else { 372 | return sequenceNumber < self.nextSequenceNumber; 373 | } 374 | } 375 | 376 | /*! Checks whether an incoming IPv4 packet looks like a ping response. 377 | * \details This routine modifies this `packet` data! It does this for two reasons: 378 | * 379 | * * It needs to zero out the `checksum` field of the ICMPHeader in order to do 380 | * its checksum calculation. 381 | * 382 | * * It removes the IPv4 header from the front of the packet. 383 | * \param packet The IPv4 packet, as returned to us by the kernel. 384 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 385 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 386 | */ 387 | 388 | - (BOOL)validatePing4ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 389 | BOOL result; 390 | NSUInteger icmpHeaderOffset; 391 | ICMPHeader * icmpPtr; 392 | uint16_t receivedChecksum; 393 | uint16_t calculatedChecksum; 394 | 395 | result = NO; 396 | 397 | icmpHeaderOffset = [[self class] icmpHeaderOffsetInIPv4Packet:packet]; 398 | if (icmpHeaderOffset != NSNotFound) { 399 | icmpPtr = (struct ICMPHeader *) (((uint8_t *) packet.mutableBytes) + icmpHeaderOffset); 400 | 401 | receivedChecksum = icmpPtr->checksum; 402 | icmpPtr->checksum = 0; 403 | calculatedChecksum = in_cksum(icmpPtr, packet.length - icmpHeaderOffset); 404 | icmpPtr->checksum = receivedChecksum; 405 | 406 | if (receivedChecksum == calculatedChecksum) { 407 | if ( (icmpPtr->type == ICMPv4TypeEchoReply) && (icmpPtr->code == 0) ) { 408 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 409 | uint16_t sequenceNumber; 410 | 411 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 412 | if ([self validateSequenceNumber:sequenceNumber]) { 413 | 414 | // Remove the IPv4 header off the front of the data we received, leaving us with 415 | // just the ICMP header and the ping payload. 416 | [packet replaceBytesInRange:NSMakeRange(0, icmpHeaderOffset) withBytes:NULL length:0]; 417 | 418 | *sequenceNumberPtr = sequenceNumber; 419 | result = YES; 420 | } 421 | } 422 | } 423 | } 424 | } 425 | 426 | return result; 427 | } 428 | 429 | /*! Checks whether an incoming IPv6 packet looks like a ping response. 430 | * \param packet The IPv6 packet, as returned to us by the kernel; note that this routine 431 | * could modify this data but does not need to in the IPv6 case. 432 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 433 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 434 | */ 435 | 436 | - (BOOL)validatePing6ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 437 | BOOL result; 438 | const ICMPHeader * icmpPtr; 439 | 440 | result = NO; 441 | 442 | if (packet.length >= sizeof(*icmpPtr)) { 443 | icmpPtr = packet.bytes; 444 | 445 | // In the IPv6 case we don't check the checksum because that's hard (we need to 446 | // cook up an IPv6 pseudo header and we don't have the ingredients) and unnecessary 447 | // (the kernel has already done this check). 448 | 449 | if ( (icmpPtr->type == ICMPv6TypeEchoReply) && (icmpPtr->code == 0) ) { 450 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 451 | uint16_t sequenceNumber; 452 | 453 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 454 | if ([self validateSequenceNumber:sequenceNumber]) { 455 | *sequenceNumberPtr = sequenceNumber; 456 | result = YES; 457 | } 458 | } 459 | } 460 | } 461 | return result; 462 | } 463 | 464 | /*! Checks whether an incoming packet looks like a ping response. 465 | * \param packet The packet, as returned to us by the kernel; note that may end up modifying 466 | * this data. 467 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 468 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 469 | */ 470 | 471 | - (BOOL)validatePingResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 472 | BOOL result; 473 | 474 | switch (self.hostAddressFamily) { 475 | case AF_INET: { 476 | result = [self validatePing4ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 477 | } break; 478 | case AF_INET6: { 479 | result = [self validatePing6ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 480 | } break; 481 | default: { 482 | assert(NO); 483 | result = NO; 484 | } break; 485 | } 486 | return result; 487 | } 488 | 489 | /*! Reads data from the ICMP socket. 490 | * \details Called by the socket handling code (SocketReadCallback) to process an ICMP 491 | * message waiting on the socket. 492 | */ 493 | 494 | - (void)readData { 495 | int err; 496 | struct sockaddr_storage addr; 497 | socklen_t addrLen; 498 | ssize_t bytesRead; 499 | void * buffer; 500 | enum { kBufferSize = 65535 }; 501 | 502 | // 65535 is the maximum IP packet size, which seems like a reasonable bound 503 | // here (plus it's what uses). 504 | 505 | buffer = malloc(kBufferSize); 506 | assert(buffer != NULL); 507 | 508 | // Actually read the data. We use recvfrom(), and thus get back the source address, 509 | // but we don't actually do anything with it. It would be trivial to pass it to 510 | // the delegate but we don't need it in this example. 511 | 512 | addrLen = sizeof(addr); 513 | bytesRead = recvfrom(CFSocketGetNative(self.socket), buffer, kBufferSize, 0, (struct sockaddr *) &addr, &addrLen); 514 | err = 0; 515 | if (bytesRead < 0) { 516 | err = errno; 517 | } 518 | 519 | // Process the data we read. 520 | 521 | if (bytesRead > 0) { 522 | NSMutableData * packet; 523 | id strongDelegate; 524 | uint16_t sequenceNumber; 525 | 526 | packet = [NSMutableData dataWithBytes:buffer length:(NSUInteger) bytesRead]; 527 | assert(packet != nil); 528 | 529 | // We got some data, pass it up to our client. 530 | 531 | strongDelegate = self.delegate; 532 | if ( [self validatePingResponsePacket:packet sequenceNumber:&sequenceNumber] ) { 533 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didReceivePingResponsePacket:sequenceNumber:)] ) { 534 | [strongDelegate simplePing:self didReceivePingResponsePacket:packet sequenceNumber:sequenceNumber]; 535 | } 536 | } else { 537 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didReceiveUnexpectedPacket:)] ) { 538 | [strongDelegate simplePing:self didReceiveUnexpectedPacket:packet]; 539 | } 540 | } 541 | } else { 542 | 543 | // We failed to read the data, so shut everything down. 544 | 545 | if (err == 0) { 546 | err = EPIPE; 547 | } 548 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 549 | } 550 | 551 | free(buffer); 552 | 553 | // Note that we don't loop back trying to read more data. Rather, we just 554 | // let CFSocket call us again. 555 | } 556 | 557 | /*! The callback for our CFSocket object. 558 | * \details This simply routes the call to our `-readData` method. 559 | * \param s See the documentation for CFSocketCallBack. 560 | * \param type See the documentation for CFSocketCallBack. 561 | * \param address See the documentation for CFSocketCallBack. 562 | * \param data See the documentation for CFSocketCallBack. 563 | * \param info See the documentation for CFSocketCallBack; this is actually a pointer to the 564 | * 'owning' object. 565 | */ 566 | 567 | static void SocketReadCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { 568 | // This C routine is called by CFSocket when there's data waiting on our 569 | // ICMP socket. It just redirects the call to Objective-C code. 570 | SimplePing * obj; 571 | 572 | obj = (__bridge SimplePing *) info; 573 | assert([obj isKindOfClass:[SimplePing class]]); 574 | 575 | #pragma unused(s) 576 | assert(s == obj.socket); 577 | #pragma unused(type) 578 | assert(type == kCFSocketReadCallBack); 579 | #pragma unused(address) 580 | assert(address == nil); 581 | #pragma unused(data) 582 | assert(data == nil); 583 | 584 | [obj readData]; 585 | } 586 | 587 | /*! Starts the send and receive infrastructure. 588 | * \details This is called once we've successfully resolved `hostName` in to 589 | * `hostAddress`. It's responsible for setting up the socket for sending and 590 | * receiving pings. 591 | */ 592 | 593 | - (void)startWithHostAddress { 594 | int err; 595 | int fd; 596 | 597 | assert(self.hostAddress != nil); 598 | 599 | // Open the socket. 600 | 601 | fd = -1; 602 | err = 0; 603 | switch (self.hostAddressFamily) { 604 | case AF_INET: { 605 | fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); 606 | if (fd < 0) { 607 | err = errno; 608 | } 609 | } break; 610 | case AF_INET6: { 611 | fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6); 612 | if (fd < 0) { 613 | err = errno; 614 | } 615 | } break; 616 | default: { 617 | err = EPROTONOSUPPORT; 618 | } break; 619 | } 620 | 621 | if (err != 0) { 622 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 623 | } else { 624 | CFSocketContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 625 | CFRunLoopSourceRef rls; 626 | id strongDelegate; 627 | 628 | // Wrap it in a CFSocket and schedule it on the runloop. 629 | 630 | self.socket = (CFSocketRef) CFAutorelease( CFSocketCreateWithNative(NULL, fd, kCFSocketReadCallBack, SocketReadCallback, &context) ); 631 | assert(self.socket != NULL); 632 | 633 | // The socket will now take care of cleaning up our file descriptor. 634 | 635 | assert( CFSocketGetSocketFlags(self.socket) & kCFSocketCloseOnInvalidate ); 636 | fd = -1; 637 | 638 | rls = CFSocketCreateRunLoopSource(NULL, self.socket, 0); 639 | assert(rls != NULL); 640 | 641 | CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode); 642 | 643 | CFRelease(rls); 644 | 645 | strongDelegate = self.delegate; 646 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didStartWithAddress:)] ) { 647 | [strongDelegate simplePing:self didStartWithAddress:self.hostAddress]; 648 | } 649 | } 650 | assert(fd == -1); 651 | } 652 | 653 | /*! Processes the results of our name-to-address resolution. 654 | * \details Called by our CFHost resolution callback (HostResolveCallback) when host 655 | * resolution is complete. We just latch the first appropriate address and kick 656 | * off the send and receive infrastructure. 657 | */ 658 | 659 | - (void)hostResolutionDone { 660 | Boolean resolved; 661 | NSArray * addresses; 662 | 663 | // Find the first appropriate address. 664 | 665 | addresses = (__bridge NSArray *) CFHostGetAddressing(self.host, &resolved); 666 | if ( resolved && (addresses != nil) ) { 667 | resolved = false; 668 | for (NSData * address in addresses) { 669 | const struct sockaddr * addrPtr; 670 | 671 | addrPtr = (const struct sockaddr *) address.bytes; 672 | if ( address.length >= sizeof(struct sockaddr) ) { 673 | switch (addrPtr->sa_family) { 674 | case AF_INET: { 675 | if (self.addressStyle != SimplePingAddressStyleICMPv6) { 676 | self.hostAddress = address; 677 | resolved = true; 678 | } 679 | } break; 680 | case AF_INET6: { 681 | if (self.addressStyle != SimplePingAddressStyleICMPv4) { 682 | self.hostAddress = address; 683 | resolved = true; 684 | } 685 | } break; 686 | } 687 | } 688 | if (resolved) { 689 | break; 690 | } 691 | } 692 | } 693 | 694 | // We're done resolving, so shut that down. 695 | 696 | [self stopHostResolution]; 697 | 698 | // If all is OK, start the send and receive infrastructure, otherwise stop. 699 | 700 | if (resolved) { 701 | [self startWithHostAddress]; 702 | } else { 703 | [self didFailWithError:[NSError errorWithDomain:(NSString *)kCFErrorDomainCFNetwork code:kCFHostErrorHostNotFound userInfo:nil]]; 704 | } 705 | } 706 | 707 | /*! The callback for our CFHost object. 708 | * \details This simply routes the call to our `-hostResolutionDone` or 709 | * `-didFailWithHostStreamError:` methods. 710 | * \param theHost See the documentation for CFHostClientCallBack. 711 | * \param typeInfo See the documentation for CFHostClientCallBack. 712 | * \param error See the documentation for CFHostClientCallBack. 713 | * \param info See the documentation for CFHostClientCallBack; this is actually a pointer to 714 | * the 'owning' object. 715 | */ 716 | 717 | static void HostResolveCallback(CFHostRef theHost, CFHostInfoType typeInfo, const CFStreamError *error, void *info) { 718 | // This C routine is called by CFHost when the host resolution is complete. 719 | // It just redirects the call to the appropriate Objective-C method. 720 | SimplePing * obj; 721 | 722 | obj = (__bridge SimplePing *) info; 723 | assert([obj isKindOfClass:[SimplePing class]]); 724 | 725 | #pragma unused(theHost) 726 | assert(theHost == obj.host); 727 | #pragma unused(typeInfo) 728 | assert(typeInfo == kCFHostAddresses); 729 | 730 | if ( (error != NULL) && (error->domain != 0) ) { 731 | [obj didFailWithHostStreamError:*error]; 732 | } else { 733 | [obj hostResolutionDone]; 734 | } 735 | } 736 | 737 | - (void)start { 738 | Boolean success; 739 | CFHostClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 740 | CFStreamError streamError; 741 | 742 | assert(self.host == NULL); 743 | assert(self.hostAddress == nil); 744 | 745 | self.host = (CFHostRef) CFAutorelease( CFHostCreateWithName(NULL, (__bridge CFStringRef) self.hostName) ); 746 | assert(self.host != NULL); 747 | 748 | CFHostSetClient(self.host, HostResolveCallback, &context); 749 | 750 | CFHostScheduleWithRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 751 | 752 | success = CFHostStartInfoResolution(self.host, kCFHostAddresses, &streamError); 753 | if ( ! success ) { 754 | [self didFailWithHostStreamError:streamError]; 755 | } 756 | } 757 | 758 | /*! Stops the name-to-address resolution infrastructure. 759 | */ 760 | 761 | - (void)stopHostResolution { 762 | // Shut down the CFHost. 763 | if (self.host != NULL) { 764 | CFHostSetClient(self.host, NULL, NULL); 765 | CFHostUnscheduleFromRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 766 | self.host = NULL; 767 | } 768 | } 769 | 770 | /*! Stops the send and receive infrastructure. 771 | */ 772 | 773 | - (void)stopSocket { 774 | if (self.socket != NULL) { 775 | CFSocketInvalidate(self.socket); 776 | self.socket = NULL; 777 | } 778 | } 779 | 780 | - (void)stop { 781 | [self stopHostResolution]; 782 | [self stopSocket]; 783 | 784 | // Junk the host address on stop. If the client calls -start again, we'll 785 | // re-resolve the host name. 786 | 787 | self.hostAddress = NULL; 788 | } 789 | 790 | @end 791 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "FFSimplePingHelper.h" 11 | 12 | @interface ViewController () 13 | #warning - 一定要定义全局的变量 14 | @property (nonatomic,strong) FFSimplePingHelper *simplePingHelper; 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view, typically from a nib. 22 | 23 | self.simplePingHelper = [[FFSimplePingHelper alloc] initWithHostName:@"www.apple.com"]; 24 | [self.simplePingHelper startPing]; 25 | } 26 | 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SimplePingDemo 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemoTests/SimplePingDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SimplePingDemoTests.m 3 | // SimplePingDemoTests 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SimplePingDemoTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SimplePingDemoTests 16 | 17 | - (void)setUp { 18 | // Put setup code here. This method is called before the invocation of each test method in the class. 19 | } 20 | 21 | - (void)tearDown { 22 | // Put teardown code here. This method is called after the invocation of each test method in the class. 23 | } 24 | 25 | - (void)testExample { 26 | // This is an example of a functional test case. 27 | // Use XCTAssert and related functions to verify your tests produce the correct results. 28 | } 29 | 30 | - (void)testPerformanceExample { 31 | // This is an example of a performance test case. 32 | [self measureBlock:^{ 33 | // Put the code you want to measure the time of here. 34 | }]; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemoUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /SimplePingDemo/SimplePingDemoUITests/SimplePingDemoUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SimplePingDemoUITests.m 3 | // SimplePingDemoUITests 4 | // 5 | // Created by wangzhe on 2019/2/18. 6 | // Copyright © 2019年 MoGuJie. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SimplePingDemoUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SimplePingDemoUITests 16 | 17 | - (void)setUp { 18 | // Put setup code here. This method is called before the invocation of each test method in the class. 19 | 20 | // In UI tests it is usually best to stop immediately when a failure occurs. 21 | self.continueAfterFailure = NO; 22 | 23 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 24 | [[[XCUIApplication alloc] init] launch]; 25 | 26 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 27 | } 28 | 29 | - (void)tearDown { 30 | // Put teardown code here. This method is called after the invocation of each test method in the class. 31 | } 32 | 33 | - (void)testExample { 34 | // Use recording to get started writing UI tests. 35 | // Use XCTAssert and related functions to verify your tests produce the correct results. 36 | } 37 | 38 | @end 39 | --------------------------------------------------------------------------------