Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an arp cache module.

  • Loading branch information...
commit 9b595488228a177965c9b23408fd828f21b9ad3c 1 parent d69452b
@archaelus authored
Showing with 44 additions and 0 deletions.
  1. +40 −0 src/enet_arp_cache.erl
  2. +4 −0 src/enet_arp_cache.hrl
View
40 src/enet_arp_cache.erl
@@ -0,0 +1,40 @@
+%% @copyright Geoff Cant
+%% @author Geoff Cant <nem@erlang.geek.nz>
+%% @version {@vsn}, {@date} {@time}
+%% @doc ARP cache datastructure
+%% @end
+-module(enet_arp_cache).
+
+-include("enet_arp_cache.hrl").
+
+-opaque cache() :: [#entry{}].
+
+-export_type([cache/0]).
+
+-export([new/0
+ ,lookup_ip_addr/2
+ ,lookup_eth_addr/2
+ ,publish/3
+ ]).
+
+-spec new() -> cache().
+new() ->
+ [].
+
+lookup_eth_addr(EthAddr, Cache) ->
+ case lists:keyfind(EthAddr, #entry.ethaddr, Cache) of
+ #entry{} = E -> E;
+ false -> not_found
+ end.
+
+lookup_ip_addr(IpAddr, Cache) ->
+ case lists:keyfind(IpAddr, #entry.ipaddr, Cache) of
+ #entry{} = E -> E;
+ false -> not_found
+ end.
+
+publish(EthAddr, IpAddr, Cache) ->
+ [#entry{ethaddr = EthAddr,
+ ipaddr = IpAddr,
+ publish = true}
+ | Cache].
View
4 src/enet_arp_cache.hrl
@@ -0,0 +1,4 @@
+-record(entry, {ethaddr :: enet_eth:address(),
+ ipaddr :: enet_ipv4:address(),
+ publish = false :: boolean(),
+ expiry :: erlang:timestamp()}).
Please sign in to comment.
Something went wrong with that request. Please try again.