diff options
author | Anders Svensson <[email protected]> | 2013-07-30 16:48:27 +0200 |
---|---|---|
committer | Anders Svensson <[email protected]> | 2013-07-30 17:59:08 +0200 |
commit | 8e7402a8b4230d25d89ddd2492908b4206083392 (patch) | |
tree | 289008b35465740585d1ba25aed2ea3a693aa518 /lib/diameter/src/base | |
parent | ff4f753dbf02693c288d7e15cb3486359e73831f (diff) | |
download | otp-8e7402a8b4230d25d89ddd2492908b4206083392.tar.gz otp-8e7402a8b4230d25d89ddd2492908b4206083392.tar.bz2 otp-8e7402a8b4230d25d89ddd2492908b4206083392.zip |
Simplify Address encode/decode
By using binary comprehensions. Add string-valued addresses to codec
suite.
Diffstat (limited to 'lib/diameter/src/base')
-rw-r--r-- | lib/diameter/src/base/diameter_types.erl | 39 |
1 files changed, 8 insertions, 31 deletions
diff --git a/lib/diameter/src/base/diameter_types.erl b/lib/diameter/src/base/diameter_types.erl index 7ebf891819..8c07e84777 100644 --- a/lib/diameter/src/base/diameter_types.erl +++ b/lib/diameter/src/base/diameter_types.erl @@ -250,13 +250,10 @@ 'Address'(encode, zero) -> <<0:48>>; -'Address'(decode, <<1:16, B/binary>>) - when size(B) == 4 -> - list_to_tuple(binary_to_list(B)); - -'Address'(decode, <<2:16, B/binary>>) - when size(B) == 16 -> - list_to_tuple(v6dec(B, [])); +'Address'(decode, <<A:16, B/binary>>) + when 1 == A, 4 == size(B); + 2 == A, 16 == size(B) -> + list_to_tuple([N || <<N:A/unit:8>> <= B]); 'Address'(decode, <<A:16, _/binary>> = B) when 1 == A; @@ -264,30 +261,10 @@ ?INVALID_LENGTH(B); 'Address'(encode, T) -> - ipenc(diameter_lib:ipaddr(T)). - -ipenc(T) - when is_tuple(T), size(T) == 4 -> - B = list_to_binary(tuple_to_list(T)), - <<1:16, B/binary>>; - -ipenc(T) - when is_tuple(T), size(T) == 8 -> - B = v6enc(lists:reverse(tuple_to_list(T)), <<>>), - <<2:16, B/binary>>. - -v6dec(<<N:16, B/binary>>, Acc) -> - v6dec(B, [N | Acc]); - -v6dec(<<>>, Acc) -> - lists:reverse(Acc). - -v6enc([N | Rest], B) - when ?UINT(16,N) -> - v6enc(Rest, <<N:16, B/binary>>); - -v6enc([], B) -> - B. + Ns = tuple_to_list(diameter_lib:ipaddr(T)), %% length 4 or 8 + A = length(Ns) div 4, %% 1 or 2 + B = << <<N:A/unit:8>> || N <- Ns >>, + <<A:16, B/binary>>. %% -------------------- |