-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS Ping functionality #602
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1d5b4a5
Added ping button and rearranged traceroute button
robmaceachern 3d64a5d
Progress on ping. Refactored some traceroute ui config to be reusued.
robmaceachern 5edb20c
Merge branch 'master' into rnm/463-ping
robmaceachern 6a1ae1c
Started working on adding the ping locations view
robmaceachern 0cfb7ec
Progress on ping functionality
robmaceachern b080cea
Automatically start ping after selection
robmaceachern b268177
Cleanup
robmaceachern c40c835
Improve delegate method name
robmaceachern 688c6a2
Remove target-specific deployment target and update project deploymen…
robmaceachern 0553482
Update credits.html
robmaceachern 7fea593
Remove unnecessary availability check and remove some unnecessary fra…
robmaceachern c2fa693
Remove CarPlay, Mac, and Apple Watch icon entries
robmaceachern 2bc5783
Expose immutable array of packet records
robmaceachern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// SCPingUtility.swift | ||
// Internet Map | ||
// | ||
// Created by Robert MacEachern on 2019-02-21. | ||
// Copyright © 2019 Peer1. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc protocol SCPingUtilityDelegate { | ||
|
||
/// Invoked immediately before a ping packet is sent. | ||
func pingUtilityWillSendPing(_ pingUtility: SCPingUtility) | ||
|
||
/// Invoked each time a response packet is received. Includes all packet records seen so far. | ||
func pingUtility(_ pingUtility: SCPingUtility, didReceiveResponse records: [SCPacketRecord]) | ||
|
||
/// Invoked when an error occurs with sending a packet or a more general ICMP failure. | ||
func pingUtility(_ pingUtility: SCPingUtility, didFailWithError error: Error) | ||
|
||
/// Invoked when the ping utility has finished. Includes all packet records. | ||
func pingUtility(_ pingUtility: SCPingUtility, didFinishWithRecords records: [SCPacketRecord]) | ||
} | ||
|
||
@objc class SCPingUtility: NSObject { | ||
|
||
let ipAddress: String | ||
let count: Int | ||
let ttl: Int | ||
|
||
/// The interval between sending packets. | ||
let wait: TimeInterval | ||
|
||
@objc var delegate: SCPingUtilityDelegate? | ||
|
||
@objc public var packetRecords: [SCPacketRecord] { | ||
let records = icmpUtility.packetRecords as! [SCPacketRecord] | ||
records.forEach { record in | ||
// The ICMPHeader sequence numbers are big endian. Need to convert them before looking them up. | ||
let responseHeader = responsePacketHeaders.first(where: { CFSwapInt16BigToHost($0.0.sequenceNumber) == record.sequenceNumber}) | ||
record.arrival = responseHeader?.1 | ||
record.rtt = responseHeader == nil ? 0 : Float(record.arrival.timeIntervalSince1970 - record.departure.timeIntervalSince1970) * 1000.0 | ||
record.responseAddress = responseHeader == nil ? nil : ipAddress | ||
record.timedOut = responseHeader == nil && (Date().timeIntervalSince1970 - record.departure.timeIntervalSince1970) > wait | ||
} | ||
return records | ||
} | ||
|
||
private var icmpUtility: SCIcmpPacketUtility | ||
private var hasStarted: Bool = false | ||
private var responsePacketHeaders: [(ICMPHeader, arrival: Date)] = [] | ||
|
||
@objc init(ipAddress: String, count: Int, ttl: Int, wait: TimeInterval) { | ||
self.ipAddress = ipAddress | ||
self.count = count | ||
self.ttl = ttl | ||
self.wait = wait | ||
self.icmpUtility = SCIcmpPacketUtility(hostAddress: ipAddress) | ||
super.init() | ||
self.icmpUtility.delegate = self | ||
} | ||
|
||
@objc func start() { | ||
guard !hasStarted else { | ||
NSLog("WARNING: Attempting to restart a SCPingUtility that has alread been started. Create a new instance instead.") | ||
return | ||
} | ||
icmpUtility.start() | ||
hasStarted = true | ||
} | ||
|
||
@objc func finish() { | ||
icmpUtility.stop() | ||
self.delegate?.pingUtility(self, didFinishWithRecords: packetRecords) | ||
} | ||
|
||
private func sendPingPacketIfNecessary() { | ||
if icmpUtility.nextSequenceNumber < count { | ||
self.delegate?.pingUtilityWillSendPing(self) | ||
icmpUtility.sendPacket(with: nil, andTTL: ttl) | ||
DispatchQueue.main.asyncAfter(deadline: .now() + wait) { | ||
self.sendPingPacketIfNecessary() | ||
} | ||
} else { | ||
NSLog("SCPingUtility reached number of ping packets to send") | ||
finish() | ||
} | ||
} | ||
} | ||
|
||
extension SCPingUtility: SCIcmpPacketUtilityDelegate { | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didSendPacket packet: Data!) { | ||
NSLog("SCPingUtility SCIcmpPacketUtility.didSendPacket") | ||
} | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didFailWithError error: Error!) { | ||
NSLog("SCPingUtility SCIcmpPacketUtility.didFailWithError \(error.localizedDescription)") | ||
self.delegate?.pingUtility(self, didFailWithError: error) | ||
} | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didStartWithAddress address: Data!) { | ||
NSLog("SCPingUtility SCIcmpPacketUtility.didStartWithAddress") | ||
sendPingPacketIfNecessary() | ||
} | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didReceiveUnexpectedPacket packet: Data!) { | ||
NSLog("SCPingUtility SCIcmpPacketUtility.didReceiveUnexpectedPacket") | ||
} | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didFailToSendPacket packet: Data!, error: Error!) { | ||
NSLog("didFailToSendPacket \(error.localizedDescription)") | ||
self.delegate?.pingUtility(self, didFailWithError: error) | ||
} | ||
|
||
func scIcmpPacketUtility(_ packetUtility: SCIcmpPacketUtility!, didReceiveResponsePacket packet: Data!, arrivedAt dateTime: Date!) { | ||
NSLog("SCPingUtility SCIcmpPacketUtility.didReceiveResponsePacket at time \(dateTime!)") | ||
guard let icmpPacket = SCIcmpPacketUtility.icmp(inPacket: packet)?.pointee else { | ||
fatalError("Unable to access icmp packet") | ||
} | ||
|
||
responsePacketHeaders.append((icmpPacket, dateTime)) | ||
self.delegate?.pingUtility(self, didReceiveResponse: self.packetRecords) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Force cast here seems like it shouldn't be necessary, as the ObjC code is annotated with the right type. Was it maybe from before the annotation as added and never got removed?
Alternatively, is it maybe complaining because it things it might be nullable, and there needs to be a nullable annotation on that as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was confused by this too. For some reason the generic type isn't bridging to Swift. The interface swift sees is:
A bit of google searching led me to this Swift bug: Swift does not honor NSMutableArray generics from objective-c classes. I could try the workaround of changing the public interface to
NSArray<SCPacketRecord *>
as long as nothing else depends on modifying that array.