packet_info: Implement packet parser (#14)

This commit is contained in:
Eishun Kondoh 2018-10-13 23:49:52 +09:00 committed by GitHub
parent 5049e0643a
commit 0e4cd32027
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 255 additions and 2 deletions

View file

@ -9,10 +9,13 @@ defmodule Tres.IPv4Address do
@typep in_addr_str :: String.t()
@doc """
iex> {{192, 168, 0, 1}, {255, 255, 255, 255}} = IPv4Address.parse(<<192, 168, 0, 1>>)
iex> {{192, 168, 0, 1}, {255, 255, 255, 255}} = IPv4Address.parse("192.168.0.1")
iex> {{192, 168, 0, 1}, {255, 255, 255, 0}} = IPv4Address.parse("192.168.0.1/24")
iex> {{192, 168, 0, 1}, {255, 255, 255, 0}} = IPv4Address.parse("192.168.0.1/24")
"""
@spec parse(in_addr_str) :: {in_addr, in_addr}
@spec parse(in_addr_str | binary()) :: {in_addr, in_addr}
def parse(<<a1, a2, a3, a4>>), do: {{a1, a2, a3, a4}, {255, 255, 255, 255}}
def parse(addr) do
case String.split(addr, ~r/\//) do
[^addr] ->