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 | |
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.
-rw-r--r-- | lib/diameter/src/base/diameter_types.erl | 39 | ||||
-rw-r--r-- | lib/diameter/test/diameter_codec_test.erl | 22 |
2 files changed, 18 insertions, 43 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>>. %% -------------------- diff --git a/lib/diameter/test/diameter_codec_test.erl b/lib/diameter/test/diameter_codec_test.erl index 6f1b7d0b7d..295d23912b 100644 --- a/lib/diameter/test/diameter_codec_test.erl +++ b/lib/diameter/test/diameter_codec_test.erl @@ -62,15 +62,13 @@ lib() -> %% Internal functions. lib({N,B}, {_,_,_} = T) -> - [] = run([[fun lib/2, A, B] || A <- element(N,T)]); + [] = run([[fun lib/2, A, B] || A <- element(N,T), is_tuple(A)]); lib(IP, B) -> - LA = tuple_to_list(IP), - {SA,Fun} = ip(LA), - [] = run([[fun lib/4, IP, B, Fun, A] || A <- [IP, SA]]). + [] = run([[fun lib/3, IP, B, A] || A <- [IP, ntoa(tuple_to_list(IP))]]). -lib(IP, B, Fun, A) -> - try Fun(A) of +lib(IP, B, A) -> + try diameter_lib:ipaddr(A) of IP when B -> ok catch @@ -78,12 +76,12 @@ lib(IP, B, Fun, A) -> ok end. -ip([_,_,_,_] = A) -> +ntoa([_,_,_,_] = A) -> [$.|S] = lists:append(["." ++ integer_to_list(N) || N <- A]), - {S, fun diameter_lib:ipaddr/1}; -ip([_,_,_,_,_,_,_,_] = A) -> + S; +ntoa([_,_,_,_,_,_,_,_] = A) -> [$:|S] = lists:flatten([":" ++ io_lib:format("~.16B", [N]) || N <- A]), - {S, fun diameter_lib:ipaddr/1}. + S. %% ------------------------------------------------------------------------ %% base/1 @@ -344,8 +342,8 @@ values('Float64') -> values('Address') -> {[{255,0,random(16#FF),1}, {65535,0,0,random(16#FFFF),0,0,0,1}], - [], - [{256,0,0,1}, {65536,0,0,0,0,0,0,1}]}; + ["127.0.0.1", "FFFF:FF::1.2.3.4"], + [{256,0,0,1}, {65536,0,0,0,0,0,0,1}, "256.0.0.1", "10000::1"]}; values('DiameterIdentity') -> {["x", "diameter.com"], |