Library that contains types for IP addresses and networks.
import IPAddress
IPv4Network(string: "10.0.0.0/8").contains(ipaddress: IPv4Address(string: "10.1.0.1")
Update your Package.swift
file to include these lines.
let package = Package(
dependencies: [
.package(url: "https://github.com/fdebrabander/swift-ipaddress.git", from: "0.0.1"),
],
targets: [
.target(
dependencies: ["IPAddress"]
)
]
)
Create IPv4Address and IPv4Network objects
let ip1 = try IPv4Address(string: "192.168.0.1")
let ip2 = try IPv4Address(string: "10.0.0.1")
let network = try IPv4Network(string: "10.0.0.0/8")
Compare IP addresses
assert(ip1 != ip2, "should not match")
Determine if a network contains an IP address
assert(network.contains(ipaddress: ip2), "should contain ip")
Conversion between the different types
// Create an IPv4Network for network 192.168.0.0/16
let ip = try IPv4Address(string: "192.168.1.10")
ip.network(withPrefix: 16)
For the full documentation see the Swift Package Index.