Merge pull request #46 from keeshux/endianness-agnostic-dns

Endianness-agnostic IPv4/UInt32 conversions
This commit is contained in:
Davide De Rosa 2018-11-05 20:39:17 +01:00 committed by GitHub
commit dbd552116c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 18 deletions

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- IPv4/UInt32 conversions are not endianness-agnostic. [#46](https://github.com/keeshux/tunnelkit/pull/46)
## 1.3.0 (2018-10-28)
### Changed

View File

@ -96,29 +96,29 @@ public class DNSResolver {
}
public static func string(fromIPv4 ipv4: UInt32) -> String {
let a = UInt8((ipv4 >> 24) & UInt32(0xff))
let b = UInt8((ipv4 >> 16) & UInt32(0xff))
let c = UInt8((ipv4 >> 8) & UInt32(0xff))
let d = UInt8(ipv4 & UInt32(0xff))
return "\(a).\(b).\(c).\(d)"
var addr = in_addr(s_addr: CFSwapInt32HostToBig(ipv4))
var buf = Data(count: Int(INET_ADDRSTRLEN))
let bufCount = socklen_t(buf.count)
let resultPtr = buf.withUnsafeMutableBytes { (bufPtr) in
return withUnsafePointer(to: &addr) {
return inet_ntop(AF_INET, $0, bufPtr, bufCount)
}
}
guard let result = resultPtr else {
preconditionFailure()
}
return String(cString: result)
}
public static func ipv4(fromString string: String) -> UInt32? {
let comps = string.components(separatedBy: ".")
guard comps.count == 4 else {
var addr = in_addr()
let result = string.withCString {
inet_pton(AF_INET, $0, &addr)
}
guard result > 0 else {
return nil
}
var ipv4: UInt32 = 0
var bits: UInt32 = 32
comps.forEach {
guard let octet = UInt32($0), octet <= 255 else {
return
}
bits -= 8
ipv4 |= octet << bits
}
return ipv4
return CFSwapInt32BigToHost(addr.s_addr)
}
private init() {