Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle IMEIs shorter than 15 digits #50

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion priv/ie_gen_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ ies() ->
{"DST", 2, integer},
{'_', 0}]},
{154, "IMEI", '_',
[{"IMEI", 64, {type, tbcd}},
[{"IMEI", 64, {type, imei}},
{'_', 0}]},
{155, "CAMEL Charging Information Container", '_',
[]},
Expand Down
11 changes: 9 additions & 2 deletions src/gtp_packet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,13 @@ encode_imsi(IMSI) ->
<< B:64/bits, _/binary>> = << (encode_tbcd(IMSI))/binary, -1:64 >>,
B.

decode_imei(Bin) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate functions which are already defined at lines 763 and 766

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's not. Those function are type specific. IMSI and IMEI accidentally use the same encoding, but they are not the same type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... got it. Thanks!

decode_tbcd(Bin).

encode_imei(IMEI) ->
<< B:64/bits, _/binary>> = << (encode_tbcd(IMEI))/binary, -1:64 >>,
B.

encode_v1_rai(#routeing_area_identity{
identity = #rai{plmn_id = {MCC, MNC}, lac = LAC, rac = RAC}}) ->
<<(encode_mccmnc(MCC, MNC))/binary, LAC:16, (RAC bsr 8):8>>.
Expand Down Expand Up @@ -1706,7 +1713,7 @@ decode_v1_element(<<M_timezone:8/integer,
decode_v1_element(<<M_imei:64/bits,
_/binary>>, 154, Instance) ->
#imei{instance = Instance,
imei = decode_tbcd(M_imei)};
imei = decode_imei(M_imei)};

decode_v1_element(<<>>, 155, Instance) ->
#camel_charging_information_container{instance = Instance};
Expand Down Expand Up @@ -2462,7 +2469,7 @@ encode_v1_element(#ms_time_zone{
encode_v1_element(#imei{
instance = Instance,
imei = M_imei}) ->
encode_v1_element(154, Instance, <<(encode_tbcd(M_imei)):64/bits>>);
encode_v1_element(154, Instance, <<(encode_imei(M_imei)):64/bits>>);

encode_v1_element(#camel_charging_information_container{
instance = Instance}) ->
Expand Down
11 changes: 11 additions & 0 deletions test/gtp_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ all() ->
test_v1_ignore_spare_bits,
test_g_pdu,
test_v2_pco_vendor_ext,
v1_invalid_imei,
v2_list,
partial_decode,
partial_encode,
Expand Down Expand Up @@ -282,6 +283,16 @@ test_v2_pco_vendor_ext(_Config) ->
?match(Data when is_binary(Data), (catch gtp_packet:encode(Msg))),
ok.

v1_invalid_imei() ->
[{doc, "Try to encode an IMEI shorter that 15 digits"}].
v1_invalid_imei(_Config) ->
%% IMEIs are 15 digits and IMEI-SVs are 16 digits, anything else is not a valid
%% IMEI. However, somehow 14 digit values are observed in the wild. Make sure that
%% encoder/decoder handles them, so that higher levels can deal with the values.
Msg = #gtp{version = v1, type = create_pdp_context_request, seq_no = 1, tei = 0,
ie = #{{imei, 0} => #imei{imei = <<"1234567890">>}}},
?equal(Msg, gtp_packet:decode(gtp_packet:encode(Msg))).

v2_list(_Config) ->
IEs =
[
Expand Down