Search results

There are no results.

std.net.ip.Ipv4Address

class pub Ipv4Address

An IPv4 address.

Static methods

new

Show source code
Hide source code
fn pub static new(a: Int, b: Int, c: Int, d: Int) -> Ipv4Address {
  Ipv4Address(a: a, b: b, c: c, d: d)
}
fn pub static new(a: Int, b: Int, c: Int, d: Int) -> Ipv4Address

Returns a new IPv4 address using the given octets.

parse

Show source code
Hide source code
fn pub static parse(input: String) -> Option[Ipv4Address] {
  # No IPv4 address can be longer than 15 characters (255.255.255.255).
  if input.size > 15 { return Option.None }

  let segments = []
  let format = Format.Decimal

  input.split('.').each(fn (segment) {
    # This handles inputs such as "." and "..."
    if segment.empty? { return }

    # We don't support octal formats, as it was never part of the spec.
    # Ignoring them can lead to security issues, as described in
    # https://www.bleepingcomputer.com/news/security/go-rust-net-library-affected-by-critical-ip-address-validation-vulnerability/.
    if segment.byte(0) == ZERO_BYTE and segment.size > 1 { return }

    match Int.parse(segment, format) {
      case Some(int) if int >= IP_MINIMUM_VALUE and int <= IPV4_OCTET_MAXIMUM
      -> {
        segments.push(int)
      }
      case _ -> {}
    }
  })

  if segments.size != IPV4_OCTETS { return Option.None }

  Option.Some(
    Ipv4Address(
      a: segments.get(0),
      b: segments.get(1),
      c: segments.get(2),
      d: segments.get(3),
    ),
  )
}
fn pub static parse(input: String) -> Option[Ipv4Address]

Parses an IPv4 address literal (e.g. "1.2.3.4").

Examples

Parsing an IPv4 address:

import std.net.ip (Ipv4Address)

let addr = Ipv4Address.parse('1.2.3.4').get

addr.v4? # => true

Instance methods

!=

Show source code
Hide source code
fn pub !=(other: T) -> Bool {
  (self == other).false?
}
fn pub !=(other: T) -> Bool

Returns true if self and the given object are not equal to each other.

==

Show source code
Hide source code
fn pub ==(other: ref Ipv4Address) -> Bool {
  @a == other.a and @b == other.b and @c == other.c and @d == other.d
}
fn pub ==(other: ref Ipv4Address) -> Bool

Returns true if self and the given IP address are the same.

Examples

Comparing two IPv4 addresses:

import std.net.ip (Ipv4Address)

let addr1 = Ipv4Address.new(127, 0, 0, 1)
let addr2 = Ipv4Address.new(127, 0, 0, 1)
let addr3 = Ipv4Address.new(127, 0, 0, 2)

addr1 == addr2 # => true
addr1 == addr3 # => false

broadcast?

Show source code
Hide source code
fn pub broadcast? -> Bool {
  @a == IPV4_OCTET_MAXIMUM
    and @b == IPV4_OCTET_MAXIMUM
    and @c == IPV4_OCTET_MAXIMUM
    and @d == IPV4_OCTET_MAXIMUM
}
fn pub broadcast? -> Bool

Returns true if self is a broadcast address (255.255.255.255).

Examples

Checking if an IPv4 address is a broadcast address:

import std.net.ip (Ipv4Address)

Ipv4Address.new(127, 0, 0, 1).broadcast? # => false
Ipv4Address.new(255, 255, 255, 255).broadcast? # => true

clone

Show source code
Hide source code
fn pub clone -> Ipv4Address {
  Ipv4Address(a: @a, b: @b, c: @c, d: @d)
}
fn pub clone -> Ipv4Address

Creates a clone of self.

documentation?

Show source code
Hide source code
fn pub documentation? -> Bool {
  if @a == 192 and @b == 0 and @c == 2 { return true }

  if @a == 198 and @b == 51 and @c == 100 { return true }

  @a == 203 and @b == 0 and @c == 113
}
fn pub documentation? -> Bool

Returns true if self is in a range designated for documentation.

The following IPv4 ranges are designated for documentation:

  • 192.0.2.0/24 (TEST-NET-1)
  • 198.51.100.0/24 (TEST-NET-2)
  • 203.0.113.0/24 (TEST-NET-3)

Examples

Checking if an IPv4 address is a documentation address:

import std.net.ip (Ipv4Address)

Ipv4Address.new(192, 0, 2, 0).documentation? # => true
Ipv4Address.new(192, 1, 2, 0).documentation? # => false

fmt

Show source code
Hide source code
fn pub fmt(formatter: mut Formatter) {
  formatter.write(to_string)
}
fn pub fmt(formatter: mut Formatter)

Formats self in a human-readable format for debugging purposes.

into_string

Show source code
Hide source code
fn pub move into_string -> String {
  to_string
}
fn pub move into_string -> String

Moves self into a String.

Show source code
Hide source code
fn pub link_local? -> Bool {
  @a == 169 and @b == 254
}
fn pub link_local? -> Bool

Returns true if self is link-local (169.254.0.0/16).

Examples

Checking if an address is link-local:

import std.net.ip (Ipv4Address)

Ipv4Address.new(169, 254, 0, 0).link_local? # => true
Ipv4Address.new(169, 254, 1, 0).link_local? # => true
Ipv4Address.new(169, 255, 1, 0).link_local? # => false

loopback?

Show source code
Hide source code
fn pub loopback? -> Bool {
  @a == 127
}
fn pub loopback? -> Bool

Returns true if self is a loopback address (127.0.0.0/8).

Examples

Checking if an address is a loopback address:

import std.net.ip (Ipv4Address)

Ipv4Address.new(127, 0, 0, 1).loopback? # => true
Ipv4Address.new(127, 0, 1, 1).loopback? # => true
Ipv4Address.new(255, 0, 0, 0).loopback? # => false

multicast?

Show source code
Hide source code
fn pub multicast? -> Bool {
  let first = @a

  first >= 224 and first <= 239
}
fn pub multicast? -> Bool

Returns true if self is a multicast address (244.0.0.0/4).

Examples

Checking if an address is a multicast address:

import std.net.ip (Ipv4Address)

Ipv4Address.new(224, 254, 0, 0).multicast? # => true
Ipv4Address.new(127, 0, 0, 1).multicast? # => false

private?

Show source code
Hide source code
fn pub private? -> Bool {
  if @a == 10 { return true }

  if @a == 172 and @b >= 16 and @b <= 31 { return true }

  @a == 192 and @b == 168
}
fn pub private? -> Bool

Returns true if self is a private address.

The following ranges are private IPv4 ranges:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16

Examples

Checking if an address is in a private range:

import std.net.ip (Ipv4Address)

Ipv4Address.new(10, 0, 0, 1).private? # => true
Ipv4Address.new(127, 0, 0, 1).private? # => false

to_ipv6_compatible

Show source code
Hide source code
fn pub to_ipv6_compatible -> Ipv6Address {
  Ipv6Address.new(
    0,
    0,
    0,
    0,
    0,
    0,
    octets_to_hextet(@a, @b),
    octets_to_hextet(@c, @d),
  )
}
fn pub to_ipv6_compatible -> Ipv6Address

Converts this IP address to an IPv4-compatible IPv6 address.

Examples

Converting an IPv4 address:

import std.net.ip (Ipv4Address, Ipv6Address)

let ipv4 = Ipv4Address.new(192, 0, 2, 255)
let ipv6 = ipv4.to_ipv6_compatible

ipv6.segments # => [0, 0, 0, 0, 0, 0, 0xc000, 0x2ff]

to_ipv6_mapped

Show source code
Hide source code
fn pub to_ipv6_mapped -> Ipv6Address {
  Ipv6Address.new(
    0,
    0,
    0,
    0,
    0,
    IPV6_HEXTET_MAXIMUM,
    octets_to_hextet(@a, @b),
    octets_to_hextet(@c, @d),
  )
}
fn pub to_ipv6_mapped -> Ipv6Address

Converts this IP address to an IPv4-mapped IPv6 address.

to_string

Show source code
Hide source code
fn pub to_string -> String {
  '${@a}.${@b}.${@c}.${@d}'
}
fn pub to_string -> String

Converts self to a String.

Examples

Converting an IPv4 address to a String:

import std.net.ip (Ipv4Address)

Ipv4Address.new.to_string # => '0.0.0.0'
Ipv4Address.new(127, 0, 0, 1) # => '127.0.0.1'

unspecified?

Show source code
Hide source code
fn pub unspecified? -> Bool {
  @a == 0 and @b == 0 and @c == 0 and @d == 0
}
fn pub unspecified? -> Bool

Returns true if self is the special "unspecified" address (0.0.0.0).

Examples

import std.net.ip (Ipv4Address)

Ipv4Address.new(0, 0, 0, 0).unspecified? # => true
Ipv4Address.new(0, 0, 0, 1).unspecified? # => false

Implemented traits

std.clone.

Clone

impl Clone[Ipv4Address] for Ipv4Address
std.cmp.

Equal

impl Equal[ref Ipv4Address] for Ipv4Address
std.fmt.

Format

impl Format for Ipv4Address
std.string.

IntoString

impl IntoString for Ipv4Address
std.string.

ToString

impl ToString for Ipv4Address