-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix various issues in icmp channel; add icmp header rewrite and corre…
…sponding tests
- Loading branch information
Showing
9 changed files
with
649 additions
and
437 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,53 @@ | ||
// | ||
// This source file is part of the LCLPing open source project | ||
// | ||
// Copyright (c) 2021-2023 Local Connectivity Lab and the project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE for license information | ||
// See CONTRIBUTORS for the list of project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
let charA = UInt8(UnicodeScalar("a").value) | ||
let char0 = UInt8(UnicodeScalar("0").value) | ||
|
||
private func itoh(_ value: UInt8) -> UInt8 { | ||
return (value > 9) ? (charA + value - 10) : (char0 + value) | ||
} | ||
|
||
private func htoi(_ value: UInt8) throws -> UInt8 { | ||
switch value { | ||
case char0...char0 + 9: | ||
return value - char0 | ||
case charA...charA + 5: | ||
return value - charA + 10 | ||
default: | ||
throw PingError.invalidHexFormat | ||
} | ||
} | ||
|
||
extension Data { | ||
|
||
/// Initialize the `Data` from the hex string value | ||
init(hexString: String) throws { | ||
self.init() | ||
|
||
if hexString.count % 2 != 0 || hexString.count == 0 { | ||
throw PingError.invalidHexFormat | ||
} | ||
|
||
let stringBytes: [UInt8] = Array(hexString.lowercased().data(using: String.Encoding.utf8)!) | ||
|
||
for i in stride(from: stringBytes.startIndex, to: stringBytes.endIndex - 1, by: 2) { | ||
let char1 = stringBytes[i] | ||
let char2 = stringBytes[i + 1] | ||
|
||
try self.append(htoi(char1) << 4 + htoi(char2)) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.