Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert packet header to proplist for debugging

Idea stolen from klaar's cowboy commit:

klaar/cowboy@5e30ffb
  • Loading branch information...
commit 6920bd265efe1c1fdd0b5f3063c7a4d2b72a6119 1 parent a136922
Michael Santos authored
Showing with 22 additions and 4 deletions.
  1. +22 −4 examples/basic_firewall.erl
26 examples/basic_firewall.erl
View
@@ -59,6 +59,15 @@
out/2
]).
+-define(RECORD_TO_PROPLIST(Record),
+ fun(Val) ->
+ lists:zip(
+ record_info(fields, Record),
+ tl(tuple_to_list(Val))
+ )
+ end).
+
+
in(Packet, _State) ->
{IPv6Header, Payload} = pkt:ipv6(Packet),
in_1(IPv6Header, Payload).
@@ -67,7 +76,7 @@ in_1(#ipv6{next = ?IPPROTO_ICMPV6}, _) ->
ok;
in_1(#ipv6{next = ?IPPROTO_UDP}, Packet) ->
{UDPHeader, _} = pkt:udp(Packet),
- {block, in, UDPHeader};
+ {block, in, udphdr(UDPHeader)};
% $ cat /proc/sys/net/ipv4/ip_local_port_range
% 32768 61000
in_1(#ipv6{next = ?IPPROTO_TCP}, Packet) ->
@@ -78,7 +87,8 @@ in_1(#ipv6{next = ?IPPROTO_TCP}, Packet) ->
when Dport >= 32768; Dport =< 61000 -> ok;
#tcp{sport = 443, dport = Dport, ack = 1}
when Dport >= 32768; Dport =< 61000 -> ok;
- _ -> {block, in, TCPHeader}
+ _ ->
+ {block, in, tcphdr(TCPHeader)}
end.
@@ -90,12 +100,20 @@ out_1(#ipv6{next = ?IPPROTO_ICMPV6}, _) ->
ok;
out_1(#ipv6{next = ?IPPROTO_UDP}, Packet) ->
{UDPHeader, _} = pkt:udp(Packet),
- {block, out, UDPHeader};
+ {block, out, udphdr(UDPHeader)};
out_1(#ipv6{next = ?IPPROTO_TCP}, Packet) ->
{TCPHeader, _} = pkt:tcp(Packet),
case TCPHeader#tcp.dport of
22 -> ok;
80 -> ok;
443 -> ok;
- _ -> {block, out, TCPHeader}
+ _ -> {block, out, tcphdr(TCPHeader)}
end.
+
+tcphdr(Rec) ->
+ Fun = ?RECORD_TO_PROPLIST(tcp),
+ Fun(Rec).
+
+udphdr(Rec) ->
+ Fun = ?RECORD_TO_PROPLIST(udp),
+ Fun(Rec).
Please sign in to comment.
Something went wrong with that request. Please try again.