Permalink
Browse files

Add support for BSD loopback

  • Loading branch information...
1 parent dc5e739 commit 82190f545eea38fb11edf24b0e1b54c08625841e @msantos committed Mar 9, 2011
Showing with 41 additions and 4 deletions.
  1. +8 −0 include/pkt.hrl
  2. +33 −4 src/pkt.erl
View
@@ -15,9 +15,13 @@
-define(ARPOP_InREPLY, 9). % InARP reply
-define(ARPOP_NAK, 10). % (ATM)ARP NAK
+-define(DLT_NULL, 0). % BSD loopback
-define(DLT_EN10MB, 1). % Ethernet
-define(DLT_LINUX_SLL, 113). % Linux cooked sockets fake hdr
+-define(PF_INET, 2).
+-define(PF_INET6, 10).
+
-define(IPPROTO_IP, 0).
-define(IPPROTO_ICMP, 1).
-define(IPPROTO_TCP, 6).
@@ -58,6 +62,10 @@
pro = ?ETH_P_IP
}).
+-record(null, {
+ family = ?PF_INET
+ }).
+
-record(ether, {
dhost = <<0,0,0,0,0,0>>,
shost = <<0,0,0,0,0,0>>,
View
@@ -58,16 +58,26 @@
]).
-decapsulate(Data) ->
- decapsulate({ether, Data}, []).
-
decapsulate_dlt(Dlt, Data) ->
decapsulate({link_type(Dlt), Data}, []).
+
+decapsulate({DLT, Data}) when is_integer(DLT) ->
+ decapsulate({link_type(DLT), Data}, []);
+decapsulate({DLT, Data}) when is_atom(DLT) ->
+ decapsulate({DLT, Data}, []);
+decapsulate(Data) when is_binary(Data) ->
+ decapsulate({ether, Data}, []).
+
decapsulate(stop, Packet) ->
lists:reverse(Packet);
+
decapsulate({unsupported, Data}, Packet) ->
decapsulate(stop, [{unsupported, Data}|Packet]);
+
+decapsulate({null, Data}, Packet) when byte_size(Data) >= 16 ->
+ {Hdr, Payload} = null(Data),
+ decapsulate({family(Hdr#null.family), Payload}, [Hdr|Packet]);
decapsulate({linux_cooked, Data}, Packet) when byte_size(Data) >= 16 ->
{Hdr, Payload} = linux_cooked(Data),
decapsulate({ether_type(Hdr#linux_cooked.pro), Payload}, [Hdr|Packet]);
@@ -77,12 +87,14 @@ decapsulate({ether, Data}, Packet) when byte_size(Data) >= ?ETHERHDRLEN ->
decapsulate({arp, Data}, Packet) when byte_size(Data) >= 28 -> % IPv4 ARP
{Hdr, Payload} = arp(Data),
decapsulate(stop, [Payload, Hdr|Packet]);
+
decapsulate({ipv4, Data}, Packet) when byte_size(Data) >= ?IPV4HDRLEN ->
{Hdr, Payload} = ipv4(Data),
decapsulate({proto(Hdr#ipv4.p), Payload}, [Hdr|Packet]);
decapsulate({ipv6, Data}, Packet) when byte_size(Data) >= ?IPV6HDRLEN ->
{Hdr, Payload} = ipv6(Data),
decapsulate({proto(Hdr#ipv6.next), Payload}, [Hdr|Packet]);
+
decapsulate({tcp, Data}, Packet) when byte_size(Data) >= ?TCPHDRLEN ->
{Hdr, Payload} = tcp(Data),
decapsulate(stop, [Payload, Hdr|Packet]);
@@ -98,21 +110,38 @@ decapsulate({icmp, Data}, Packet) when byte_size(Data) >= ?ICMPHDRLEN ->
decapsulate({_, Data}, Packet) ->
decapsulate(stop, [{truncated, Data}|Packet]).
+
ether_type(?ETH_P_IP) -> ipv4;
ether_type(?ETH_P_IPV6) -> ipv6;
ether_type(?ETH_P_ARP) -> arp;
ether_type(_) -> unsupported.
+link_type(?DLT_NULL) -> null;
link_type(?DLT_EN10MB) -> ether;
-link_type(?DLT_LINUX_SLL) -> linux_cooked.
+link_type(?DLT_LINUX_SLL) -> linux_cooked;
+link_type(_) -> unsupported.
+family(?PF_INET) -> ipv4;
+family(?PF_INET6) -> ipv6;
+family(_) -> unsupported.
proto(?IPPROTO_ICMP) -> icmp;
proto(?IPPROTO_TCP) -> tcp;
proto(?IPPROTO_UDP) -> udp;
proto(?IPPROTO_SCTP) -> sctp;
proto(_) -> unsupported.
+
+%%
+%% BSD loopback
+%%
+null(<<Family:4/native-unsigned-integer-unit:8, Payload/binary>>) ->
+ {#null{
+ family = Family
+ }, Payload};
+null(#null{family = Family}) ->
+ <<Family:4/native-unsigned-integer-unit:8>>.
+
%%
%% Linux cooked capture ("-i any") - DLT_LINUX_SLL
%%

0 comments on commit 82190f5

Please sign in to comment.