IPAddress

main

IP address library for swift
fdebrabander/swift-ipaddress

Swift IP address library

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")

Including this library

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"]
        )
    ]
)

Quick guide

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.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 07:57:58 GMT-0900 (Hawaii-Aleutian Daylight Time)