messua/addr

How we internally represent IP and socket addresses for use with rate limiting and blacklisting layers.

Types

Represents either an IPv4 or IPv6 address.

pub type IpAddr {
  IpV4(IpV4Addr)
  IpV6(IpV6Addr)
}

Constructors

Represents an IPv4 address.

pub type IpV4Addr {
  IpV4Addr(Int, Int, Int, Int)
}

Constructors

  • IpV4Addr(Int, Int, Int, Int)

Represents an IPv6 address.

pub type IpV6Addr {
  IpV6Addr(Int, Int, Int, Int, Int, Int, Int, Int)
}

Constructors

  • IpV6Addr(Int, Int, Int, Int, Int, Int, Int, Int)

Represents either an IPv4 or an IPv6 address in conjunction with a port number.

pub type SocketAddr {
  SocketAddr(addr: IpAddr, port: Int)
}

Constructors

  • SocketAddr(addr: IpAddr, port: Int)

Values

pub fn fmt_ip_addr(addr: IpAddr) -> String

Formats an IP address how you would expect.

addr.IpV4Addr(192, 168, 1, 1)
|> addr.IpV4()
|> addr.fmt_ip_addr()
|> io.println()
// 192.168.1.1

addr.IpV6Addr(10761, 33408, 1, 0, 0, 0, 0, 62334)
|> addr.IpV6()
|> addr.fmt_ip_addr()
|> io.println()
// [2A09:8280:0001:0000:0000:0000:0000:F37E]
pub fn fmt_socket_addr(addr: SocketAddr) -> String

Formats an IP address together with its port, how you would expect.

addr.IpV4Addr(192, 168, 1, 1)
|> addr.IpV4()
|> addr.SocketAddr(8000)
|> addr.fmt_socket_addr()
|> io.println()
// 192.168.1.1:8000
pub fn parse_ip_addr(str: String) -> Result(IpAddr, Nil)

Attempt to parse an IPV4 or IPV6 address in its canonical form.

let addr_strs = [
    "0.0.0.0",
    "192.168.1.1",
    "8.8.4.4",
    "2001:0db8:0000:0000:0000:8a2e:0370:7334",
    "2001:db8::8a2e:370:7334",
  ]
 
let assert Ok(addrs) =
  list.map(addr_strs, addr.parse_ip_addr)
  |> result.all()
 
list.each(addrs, fn(a) { string.inspect(a) |> io.println() })
 
// IpV4(IpV4Addr(0, 0, 0, 0))
// IpV4(IpV4Addr(192, 168, 1, 1))
// IpV4(IpV4Addr(8, 8, 4, 4))
// IpV6(IpV6Addr(8193, 3512, 0, 0, 0, 35374, 880, 29492))
// IpV6(IpV6Addr(8193, 3512, 0, 0, 0, 35374, 880, 29492))
Search Document