Grav v1.14.0

IP addresses

import "std/net" (not included in import "std") · namespace std.net

IpAddress parses and classifies IPv4 and IPv6 addresses; Cidr is a range of them written as address/prefix. Together they cover allow lists, per-client limits and checking user input.

grav
import "std"
import "std/net"

fn main() {
    let a = IpAddress.parse("2001:0db8:0000:0000:0000:0000:0000:0001")
    print(a.toString() + " v6=${a.isV6()}")
    print(IpAddress.parse("192.168.1.20").isPrivate())
    print(IpAddress.parse("8.8.8.8").isPublic())
    print(IpAddress.parse("::ffff:10.0.0.1").unmapped().toString())

    let office = Cidr.parse("10.20.0.0/16")
    for (ip in ["10.20.3.4", "10.21.0.1", "not an address"]) {
        print(ip + " -> ${office.containsText(ip)}")
    }
    print(Cidr.parse("172.16.99.1/12").toString())
}
Output
2001:db8::1 v6=true
true
true
10.0.0.1
10.20.3.4 -> true
10.21.0.1 -> false
not an address -> false
172.16.0.0/12
  • IpAddress.parse(text) takes either family (brackets and an IPv6 zone such as %eth0 are allowed) and throws ValueError otherwise; isValid(text) checks. toString() gives the canonical form, toBytes() the 4 or 16 bytes, equals(other) compares.
  • Kinds: isLoopback, isPrivate (10/8, 172.16/12, 192.168/16, fc00::/7), isLinkLocal, isMulticast, isUnspecified, isPublic (none of those, nor shared or documentation ranges). IPv4-mapped IPv6 addresses (::ffff:a.b.c.d) are classified as the IPv4 address inside (isV4Mapped, unmapped).
  • Cidr.parse("10.0.0.0/8"): contains(ip), containsText(text), prefix(), network(), toString(). A bare address is a range of one.
  • With HttpServer, req.ip() is the client's address: Cidr.parse("10.0.0.0/8").containsText(req.ip()) keeps an admin route internal.

API

class IpAddress

An IPv4 or IPv6 address.

Constructors

  • new IpAddress(raw: Bytes, family: int)

Static members

  • static fn parse(text: string) -> IpAddress — Parses "10.0.0.1", "::1", "2001:db8::2" (an IPv6 zone like "%eth0" is dropped). ValueError for anything else.
  • static fn isValid(text: string) -> bool — Whether text is an IPv4 or IPv6 address.
  • static fn fromBytes(b: Bytes) -> IpAddress — An address from its 4 or 16 bytes.

Methods

  • fn isV4() -> bool — Whether this is an IPv4 address.
  • fn isV6() -> bool — Whether this is an IPv6 address.
  • fn toBytes() -> Bytes — The 4 or 16 bytes.
  • fn toString() -> string — The address in canonical form.
  • fn equals(other: IpAddress) -> bool — Whether other is the same address.
  • fn isV4Mapped() -> bool — An IPv6 "::ffff:a.b.c.d" form of an IPv4 address.
  • fn unmapped() -> IpAddress — The IPv4 address inside an IPv4-mapped IPv6 one (else this address).
  • fn isLoopback() -> bool — 127.0.0.0/8 or ::1.
  • fn isPrivate() -> bool — A private network: 10/8, 172.16/12, 192.168/16 or fc00::/7.
  • fn isLinkLocal() -> bool — Link-local: 169.254/16 or fe80::/10.
  • fn isMulticast() -> bool — Multicast: 224/4 or ff00::/8.
  • fn isUnspecified() -> bool — 0.0.0.0 or ::.
  • fn isPublic() -> bool — A public (globally routed) address: none of the above.
  • fn rawBytes() -> Bytes — The raw bytes, for Cidr.

class Cidr

A range of addresses written as address/prefix: "10.0.0.0/8", "2001:db8::/32".

Constructors

  • new Cidr(base: IpAddress, bits: int)

Static members

  • static fn parse(text: string) -> Cidr — Parses "address/prefix" (a bare address is a range of one). ValueError for a bad address or prefix.
  • static fn isValid(text: string) -> bool — Whether text is a valid range.

Methods

  • fn contains(ip: IpAddress) -> bool — Whether ip is in the range (an IPv4-mapped IPv6 address counts as IPv4).
  • fn containsText(text: string) -> bool — Whether the address text is in the range (false for text that is not an address).
  • fn prefix() -> int — The prefix length.
  • fn network() -> IpAddress — The first address of the range (the address with the host bits cleared).
  • fn toString() -> string — "10.0.0.0/8".

Edit this page on GitHub