Skip to content
Browse files

Clean up DNS packet encoding

Use binary matching to compose DNS packets.

label/1 (creates dotted domain names) takes a binary, not a list.

Formatting changes.
  • Loading branch information...
1 parent 825dd09 commit 4247d3f9b5828f275946fb2da93845d71c6bfe27 @msantos committed Nov 4, 2010
Showing with 27 additions and 37 deletions.
  1. +27 −37 seds/src/seds_proxy.erl
View
64 seds/src/seds_proxy.erl
@@ -129,13 +129,15 @@ connect(timeout, #state{ip = IP, port = Port} = State) ->
]),
{next_state, proxy, State#state{s = Socket}}.
-proxy({dns_query, IP, Port, #dns_rec{
+proxy({dns_query, IP, Port,
+ #dns_rec{
header = Header,
qdlist = [#dns_query{
domain = Domain,
type = Type
}|_]
- } = Rec, Data}, #state{
+ } = Rec, Data},
+ #state{
sum = Sum,
dnsfd = DNSSocket,
s = Socket
@@ -188,15 +190,15 @@ proxy({dns_query, IP, Port, #dns_rec{
{Payload, Rest} = data(Type, Data),
Response = Rec#dns_rec{
- header = Header#dns_header{
- qr = true,
- ra = true
- },
- anlist = [#dns_rr{
- domain = Domain,
- type = Type,
- data = Payload
- }]},
+ header = Header#dns_header{
+ qr = true,
+ ra = true
+ },
+ anlist = [#dns_rr{
+ domain = Domain,
+ type = Type,
+ data = Payload
+ }]},
Packet = inet_dns:encode(Response),
@@ -231,39 +233,27 @@ data(Type, Data) when is_list(Data) ->
data(Type, list_to_binary(lists:reverse(Data)));
% TXT records
+data(txt, <<D1:?MAXDATA/bytes, D2:?MAXDATA/bytes, Rest/binary>>) ->
+ {[base64:encode_to_string(D1), base64:encode_to_string(D2)], Rest};
+data(txt, <<D1:?MAXDATA/bytes, Rest/binary>>) ->
+ {[base64:encode_to_string(D1)], Rest};
data(txt, Data) ->
- case byte_size(Data) of
- N when N > ?MAXDATA * 2 ->
- <<D1:?MAXDATA/bytes, D2:?MAXDATA/bytes, Rest/binary>> = Data,
- {[base64:encode_to_string(D1), base64:encode_to_string(D2)], Rest};
- N when N > ?MAXDATA ->
- <<D1:?MAXDATA/bytes, Rest/binary>> = Data,
- {[base64:encode_to_string(D1)], Rest};
- _ ->
- {[base64:encode_to_string(Data)], <<>>}
- end;
+ {[base64:encode_to_string(Data)], <<>>};
% NULL records
+data(null, <<D1:(?MAXDATA*2)/bytes, Rest/binary>>) ->
+ {base64:encode(D1), Rest};
data(null, Data) ->
- case byte_size(Data) of
- N when N > ?MAXDATA * 2 ->
- <<D1:(?MAXDATA*2)/bytes, Rest/binary>> = Data,
- {base64:encode(D1), Rest};
- _ ->
- {base64:encode(Data), <<>>}
- end;
+ {base64:encode(Data), <<>>};
% CNAME records
+data(cname, <<D1:?MAXDATA/bytes, Rest/binary>>) ->
+ {label(base32:encode(D1)), Rest};
data(cname, Data) ->
- case byte_size(Data) of
- N when N > ?MAXDATA ->
- <<D1:?MAXDATA/bytes, Rest/binary>> = Data,
- {label(base32:encode(D1)), Rest};
- _ ->
- {label(base32:encode(Data)), <<>>}
- end.
-
-label(String) when length(String) < ?MAXLABEL ->
+ {label(base32:encode(Data)), <<>>}.
+
+
+label(String) when byte_size(String) < ?MAXLABEL ->
String;
label(String) ->
re:replace(String, ".{63}", "&.", [global, {return, list}]).

0 comments on commit 4247d3f

Please sign in to comment.
Something went wrong with that request. Please try again.