ex_wire v0.1.1 ExWire.Struct.Endpoint

Struct to represent an endpoint in ExWire.

Link to this section Summary

Functions

Returns a struct given an ip in binary form, plus an udp_port or tcp_port

Given an IPv4 or IPv6 address in binary form, returns the address in list form

Returns a port given a binary version of the port as input. Note: we return nil for an empty or zero binary

Versus decode/3, and given a module with an ip, a tcp_port and a udp_port, returns a tuple of encoded values

Given an ip address that’s an encoded as a list, returns that address encoded as a binary

Given a port, returns that port encoded in binary

Link to this section Types

Link to this type ip()
ip() :: [integer]
Link to this type ip_port()
ip_port() :: integer
Link to this type t()
t() :: %ExWire.Struct.Endpoint{ip: ip, tcp_port: ip_port | nil, udp_port: ip_port | nil}

Link to this section Functions

Link to this function decode(list)
decode(ExRLP.t) :: t

Returns a struct given an ip in binary form, plus an udp_port or tcp_port.

Examples

iex> ExWire.Struct.Endpoint.decode([<<1,2,3,4>>, <<>>, <<5>>])
%ExWire.Struct.Endpoint{
  ip: [1,2,3,4],
  udp_port: nil,
  tcp_port: 5,
}
Link to this function decode_ip(data)
decode_ip(binary) :: ip

Given an IPv4 or IPv6 address in binary form, returns the address in list form.

Examples

iex> ExWire.Struct.Endpoint.decode_ip(<<1,2,3,4>>)
[1, 2, 3, 4]

iex> ExWire.Struct.Endpoint.decode_ip(<<1::128>>)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

iex> ExWire.Struct.Endpoint.decode_ip(<<0xFF, 0xFF, 0xFF, 0xFF>>)
[255, 255, 255, 255]

iex> ExWire.Struct.Endpoint.decode_ip(<<127, 0, 0, 1>>)
[127, 0, 0, 1]

iex> ExWire.Struct.Endpoint.decode_ip(<<>>)
[]
Link to this function decode_port(data)

Returns a port given a binary version of the port as input. Note: we return nil for an empty or zero binary.

Examples

iex> ExWire.Struct.Endpoint.decode_port(<<>>)
nil

iex> ExWire.Struct.Endpoint.decode_port(<<0>>)
nil

iex> ExWire.Struct.Endpoint.decode_port(<<0, 0>>)
nil

iex> ExWire.Struct.Endpoint.decode_port(<<1>>)
1

iex> ExWire.Struct.Endpoint.decode_port(<<1, 0>>)
256
Link to this function encode(endpoint)
encode(t) :: ExRLP.t

Versus decode/3, and given a module with an ip, a tcp_port and a udp_port, returns a tuple of encoded values.

Examples

iex> ExWire.Struct.Endpoint.encode(%ExWire.Struct.Endpoint{ip: [1, 2, 3, 4], udp_port: nil, tcp_port: 5})
[<<1, 2, 3, 4>>, <<>>, <<0, 5>>]
Link to this function encode_ip(ip)
encode_ip(ip) :: binary

Given an ip address that’s an encoded as a list, returns that address encoded as a binary.

Examples

iex> ExWire.Struct.Endpoint.encode_ip([1, 2, 3, 4])
<<1, 2, 3, 4>>

iex> ExWire.Struct.Endpoint.encode_ip([])
<<>>
Link to this function encode_port(port)
encode_port(ip_port | nil) :: binary

Given a port, returns that port encoded in binary.

Examples

iex> ExWire.Struct.Endpoint.encode_port(256)
<<1, 0>>

iex> ExWire.Struct.Endpoint.encode_port(nil)
<<>>

iex> ExWire.Struct.Endpoint.encode_port(0)
<<0, 0>>