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

[sccp] Ansi support #29

Merged
merged 4 commits into from
Aug 9, 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
19 changes: 19 additions & 0 deletions include/sccp.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
-define(SCCP_IEI_HOP_COUNTER, 2#00010001).
-define(SCCP_IEI_IMPORTANCE, 2#00010010).
-define(SCCP_IEI_LONG_DATA, 2#00010011).
%% ANSI T1.112.3
-define(SCCP_IEI_ANSI_MESSAGE_TYPE_INTERWORKING, 2#11111000).
-define(SCCP_IEI_ANSI_INTERMEDIATE_NETWORK_SELECTION, 2#11111001).
-define(SCCP_IEI_ANSI_INTERMEDIATE_SIGNALING_NETWORK_IDENTIFICATION, 2#11111010).


%% 3.4.2.2 Subsystem number
-define(SCCP_SSN_UNKNOWN, 2#00000000).
Expand Down Expand Up @@ -117,6 +122,16 @@
-define(SCCP_IEI_RETURN_CAUSE_HOP_COUNTER_VIOLATION, 2#0000_1100).
-define(SCCP_IEI_RETURN_CAUSE_SEGMENTATION_NOT_SUPPORTED, 2#0000_1101).
-define(SCCP_IEI_RETURN_CAUSE_SEGMENTATION_FAILURE, 2#0000_1110).
%% T1.112.3
-define(SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_CHANGE_FAILURE, 2#1111_0111).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_INS_ROUTING_REQUEST, 2#1111_1000).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_ISNI_ROUTING_REQUEST, 2#1111_1001).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_UNAUTHORIZED_MESSAGE, 2#1111_1010).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_INCOMPATIBILITY, 2#1111_1011).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_CONSTRAINED_ROUTING, 2#1111_1100).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_REDUNDANT_ISNI_CONSTRAINED_ROUTING_INFORMATION, 2#1111_1101).
-define(SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_IDENTIFICATION, 2#1111_1110).


%% 3.13 Reset cause
-define(SCCP_IEI_RESET_CAUSE_END_USER_ORIGINATED, 2#0000_0000).
Expand Down Expand Up @@ -169,3 +184,7 @@
-define(SCCP_SCMG_SUBSYSTEM_OUT_OF_SERVICE_REQUEST, 2#0000_0100).
-define(SCCP_SCMG_SUBSYSTEM_OUT_OF_SERVICE_GRANT, 2#0000_0101).
-define(SCCP_SCMG_SUBSYSTEM_CONGESTED, 2#0000_0110).
%% T1.112.2
-define(SCCP_SCMG_ANSI_SUBSYSTEM_BACKUP_ROUTING, 2#11111101).
-define(SCCP_SCMG_ANSI_SUBSYSTEM_NORMAL_ROUTING, 2#11111110).
-define(SCCP_SCMG_ANSI_SUBSYSTEM_ROUTING_STATUS_TEST, 2#11111111).
1 change: 0 additions & 1 deletion src/otc_m3ua.erl
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,6 @@ compose_network_indicator(national) -> ?MTP3_NETIND_NATIONAL;
compose_network_indicator(national_spare) -> ?MTP3_NETIND_NATIONAL_SPARE;
compose_network_indicator({reserved, R}) -> R.


-spec decode_point_code({binary(), binary()}) -> [itu_point_code() | ansi_point_code()].
decode_point_code({<<Mask:8/big>>, <<PCbin:24/big>>}) ->
MaskBits = trunc(math:pow(2, Mask) - 1),
Expand Down
218 changes: 197 additions & 21 deletions src/otc_sccp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ decode_msg(xudt, Bin, Opts) ->
<<PC:1/binary, HC:1/binary, Bin1/binary>> = Bin,
[CdPA, CgPA, D, OptBin] = separate_fields(absolute_pointers(Bin1, NumPointers, true)),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(Opts)],
CalledPartyAddress = decode_parameter(called_party_address, CdPA, Opts),
CallingPartyAddress = decode_parameter(calling_party_address, CgPA, Opts),
Optionals = decode_parameters(OptBin, AllowedParameters, Opts),
Expand All @@ -339,8 +339,8 @@ decode_msg(xudts, Bin, Opts) ->
<<RC:1/binary, HC:1/binary, Bin1/binary>> = Bin,
[CdPA, CgPA, D, OptBin] = separate_fields(absolute_pointers(Bin1, NumPointers, true)),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(Opts)],
CalledPartyAddress = decode_parameter(called_party_address, CdPA, Opts),
CallingPartyAddress = decode_parameter(calling_party_address, CgPA, Opts),
Optionals = decode_parameters(OptBin, AllowedParameters, Opts),
Expand All @@ -361,8 +361,8 @@ decode_msg(ludt, Bin, Opts) ->
LD = binary:part(Bin1, LDP+1, LDL-1),
OptBin = binary:part(Bin1, byte_size(Bin1), -OptBinP),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(Opts)],
CalledPartyAddress = decode_parameter(called_party_address, CdPA, Opts),
CallingPartyAddress = decode_parameter(calling_party_address, CgPA, Opts),
Optionals = decode_parameters(OptBin, AllowedParameters, Opts),
Expand All @@ -383,8 +383,8 @@ decode_msg(ludts, Bin, Opts) ->
LD = binary:part(Bin1, LDP+1, LDL-1),
OptBin = binary:part(Bin1, byte_size(Bin1), -OptBinP),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(Opts)],
CalledPartyAddress = decode_parameter(called_party_address, CdPA, Opts),
CallingPartyAddress = decode_parameter(calling_party_address, CgPA, Opts),
Optionals = decode_parameters(OptBin, AllowedParameters, Opts),
Expand All @@ -394,7 +394,6 @@ decode_msg(ludts, Bin, Opts) ->
calling_party_address => CallingPartyAddress,
long_data => LD}.


absolute_pointers(Bin, NumPointers) ->
absolute_pointers(Bin, NumPointers, false).

Expand Down Expand Up @@ -786,8 +785,8 @@ encode_msg(xudt,
CgPALen = byte_size(CgPA),
DLen = byte_size(D),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(#{address_type => ansi})],
OptBin = encode_parameters(Msg, AllowedParameters),
OptPointer = case byte_size(OptBin) > 0 of
true ->
Expand All @@ -813,8 +812,8 @@ encode_msg(xudts,
CgPALen = byte_size(CgPA),
DLen = byte_size(D),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(#{address_type => ansi})],
OptBin = encode_parameters(Msg, AllowedParameters),
OptPointer = case byte_size(OptBin) > 0 of
true ->
Expand All @@ -840,8 +839,8 @@ encode_msg(ludt,
CgPALen = byte_size(CgPA),
LDLen = byte_size(LD),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(#{address_type => ansi})],
OptBin = encode_parameters(Msg, AllowedParameters),
OptPointer = case byte_size(OptBin) > 0 of
true ->
Expand All @@ -867,8 +866,8 @@ encode_msg(ludts,
CgPALen = byte_size(CgPA),
LDLen = byte_size(LD),
AllowedParameters = [{segmentation, 6},
{importance, 3},
{end_of_optional_parameters, 1}],
{importance, 3}
|ansi_parameters(#{address_type => ansi})],
OptBin = encode_parameters(Msg, AllowedParameters),
OptPointer = case byte_size(OptBin) > 0 of
true ->
Expand All @@ -881,6 +880,15 @@ encode_msg(ludts,
CdPALen:8/big, CdPA/binary, CgPALen:8/big, CgPA/binary, LDLen:16/big, LD/binary,
OptBin/binary>>.

ansi_parameters(#{address_type := ansi}) ->
[{intermediate_signaling_network_identification, {3, 18}},
{intermediate_network_selection, {5, 7}},
{message_type_interworking, 2},
{end_of_optional_parameters, 1}
];
ansi_parameters(_) ->
[{end_of_optional_parameters, 1}].

decode_parameters(Bin, Parameters, Opts) ->
decode_parameters(Bin, Parameters, #{}, Opts).

Expand Down Expand Up @@ -938,7 +946,10 @@ parse_iei(?SCCP_IEI_CREDIT) -> credit;
parse_iei(?SCCP_IEI_DATA) -> data;
parse_iei(?SCCP_IEI_SEGMENTATION) -> segmentation;
parse_iei(?SCCP_IEI_HOP_COUNTER) -> hop_counter;
parse_iei(?SCCP_IEI_IMPORTANCE) -> importance.
parse_iei(?SCCP_IEI_IMPORTANCE) -> importance;
parse_iei(?SCCP_IEI_ANSI_INTERMEDIATE_SIGNALING_NETWORK_IDENTIFICATION) -> intermediate_signaling_network_identification;
parse_iei(?SCCP_IEI_ANSI_INTERMEDIATE_NETWORK_SELECTION) -> intermediate_network_selection;
parse_iei(?SCCP_IEI_ANSI_MESSAGE_TYPE_INTERWORKING) -> message_type_interworking.

%% compose_iei(destination_local_reference) -> ?SCCP_IEI_DESTINATION_LOCAL_REFERENCE;
%% compose_iei(source_local_reference) -> ?SCCP_IEI_SOURCE_LOCAL_REFERENCE;
Expand All @@ -958,7 +969,10 @@ compose_iei(credit) -> ?SCCP_IEI_CREDIT;
compose_iei(data) -> ?SCCP_IEI_DATA;
compose_iei(segmentation) -> ?SCCP_IEI_SEGMENTATION;
compose_iei(hop_counter) -> ?SCCP_IEI_HOP_COUNTER;
compose_iei(importance) -> ?SCCP_IEI_IMPORTANCE.
compose_iei(importance) -> ?SCCP_IEI_IMPORTANCE;
compose_iei(intermediate_signaling_network_identification) -> ?SCCP_IEI_ANSI_INTERMEDIATE_SIGNALING_NETWORK_IDENTIFICATION;
compose_iei(intermediate_network_selection) -> ?SCCP_IEI_ANSI_INTERMEDIATE_NETWORK_SELECTION;
compose_iei(message_type_interworking) -> ?SCCP_IEI_ANSI_MESSAGE_TYPE_INTERWORKING.

decode_parameter(destination_local_reference, Bin, _Opts) ->
Bin;
Expand Down Expand Up @@ -1029,6 +1043,58 @@ decode_parameter(importance, Bin, _Opts) ->
Importance;
decode_parameter(long_data, Bin, _Opts) ->
Bin;
decode_parameter(intermediate_signaling_network_identification, Bin, _Opts) ->
<<Counter:3, TI:1, _Res:1, IRI:2, MI:1, RCE:TI/binary, NIDs/binary>> = Bin,
NS = case RCE of
<<>> ->
#{};
<<_:6, NetSpec:2>> ->
#{network_specific => NetSpec}
end,
NetworkIDs = [NI || <<NI:2/binary>> <= NIDs],
NS#{counter => Counter,
network_identifiers => NetworkIDs,
isni_routing_identificator => case IRI of
2#00 -> neither_constrained_nor_suggested;
2#01 -> constrained;
2#10 -> reserved;
2#11 -> spare
end,
identify_networks => 1 =:= MI};
decode_parameter(intermediate_network_selection, Bin, _Opts) ->
<<Counter:2, _Res:2, ToR:2, IT:2, NIDs/binary>> = Bin,
NetworkIDs = case byte_size(NIDs) of
4 ->
<<NI1:2/binary, NI2:2/binary>> = NIDs,
[NI1, NI2];
2 ->
[NIDs]
end,
#{counter => Counter,
network_identifiers => NetworkIDs,
type_of_routing => case ToR of
2#00 -> neither_constrained_nor_suggested;
2#01 -> constrained;
2#10 -> suggested;
2#11 -> reserved
end,
information_type => case IT of
2#00 -> ss7;
2#01 -> reserved;
_ -> {network_specific, IT}
end
};
decode_parameter(message_type_interworking, Bin, _Opts) ->
<<_Res:1, Drop:1, _Spare:3, OMT:3>> = Bin,
#{parameter_can_be_dropped => 1 =:= Drop,
original_message_type => case OMT of
2#000 -> unqualified;
2#001 -> udt;
2#010 -> xudt;
2#011 -> ludt;
_ -> spare
end
};
decode_parameter(_, _, _Opts) ->
undefined.

Expand Down Expand Up @@ -1103,6 +1169,14 @@ parse_return_cause(?SCCP_IEI_RETURN_CAUSE_SCCP_FAILURE) -> sccp_failure;
parse_return_cause(?SCCP_IEI_RETURN_CAUSE_HOP_COUNTER_VIOLATION) -> hop_counter_violation;
parse_return_cause(?SCCP_IEI_RETURN_CAUSE_SEGMENTATION_NOT_SUPPORTED) -> segmentation_not_supported;
parse_return_cause(?SCCP_IEI_RETURN_CAUSE_SEGMENTATION_FAILURE) -> segmentation_failure;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_CHANGE_FAILURE) -> message_change_failure;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_INS_ROUTING_REQUEST) -> invalid_ins_routing_request;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_ISNI_ROUTING_REQUEST) -> invalid_isni_routing_request;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_UNAUTHORIZED_MESSAGE) -> unauthorized_message;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_INCOMPATIBILITY) -> message_incompatibility;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_CONSTRAINED_ROUTING) -> insi_constrained_routing;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_REDUNDANT_ISNI_CONSTRAINED_ROUTING_INFORMATION) -> redundant_isni_constrained_routing_information;
parse_return_cause(?SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_IDENTIFICATION) -> isni_identification;
parse_return_cause(V) when V >= 2#0000_1111; V =< 2#1110_0100 -> {reserved_international, V};
parse_return_cause(V) when V >= 2#1110_0101; V =< 2#1111_1110 -> {reserved_national, V};
parse_return_cause(V) -> {reserved, V}.
Expand All @@ -1122,6 +1196,14 @@ compose_return_cause(sccp_failure) -> ?SCCP_IEI_RETURN_CAUSE_SCCP_FAILURE;
compose_return_cause(hop_counter_violation) -> ?SCCP_IEI_RETURN_CAUSE_HOP_COUNTER_VIOLATION;
compose_return_cause(segmentation_not_supported) -> ?SCCP_IEI_RETURN_CAUSE_SEGMENTATION_NOT_SUPPORTED;
compose_return_cause(segmentation_failure) -> ?SCCP_IEI_RETURN_CAUSE_SEGMENTATION_FAILURE;
compose_return_cause(message_change_failure) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_CHANGE_FAILURE;
compose_return_cause(invalid_ins_routing_request) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_INS_ROUTING_REQUEST;
compose_return_cause(invalid_isni_routing_request) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_INVALID_ISNI_ROUTING_REQUEST;
compose_return_cause(unauthorized_message) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_UNAUTHORIZED_MESSAGE;
compose_return_cause(message_incompatibility) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_MESSAGE_INCOMPATIBILITY;
compose_return_cause(insi_constrained_routing) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_CONSTRAINED_ROUTING;
compose_return_cause(redundant_isni_constrained_routing_information) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_REDUNDANT_ISNI_CONSTRAINED_ROUTING_INFORMATION;
compose_return_cause(isni_identification) -> ?SCCP_IEI_ANSI_RETURN_CAUSE_ISNI_IDENTIFICATION;
compose_return_cause({reserved_international, V}) -> V;
compose_return_cause({reserved_national, V}) -> V;
compose_return_cause({reserved, V}) -> V.
Expand Down Expand Up @@ -1273,7 +1355,7 @@ encode_parameter(refusal_cause, RC) ->
encode_parameter(data, Bin) ->
Bin;
encode_parameter(segmentation, V) ->
F = case maps:get(first_segment_indication, V, true) of
F = case maps:get(first_segment_indication, V, true) of
true ->
0;
_ ->
Expand All @@ -1289,11 +1371,105 @@ encode_parameter(hop_counter, HC) ->
encode_parameter(importance, Importance) ->
<<0:5, Importance:3>>;
encode_parameter(long_data, Bin) ->
Bin.
Bin;
encode_parameter(intermediate_signaling_network_identification, V) ->
#{counter := Counter,
network_identifiers := NetworkIDs,
isni_routing_identificator := RoutingID,
identify_networks := IN} = V,
IRI = case RoutingID of
neither_constrained_nor_suggested -> 2#00;
constrained -> 2#01;
reserved -> 2#10;
spare -> 2#11
end,

RCE = case maps:get(network_specific, V, undefined) of
undefined ->
<<>>;
NetSpec ->
<<0:6, NetSpec:2>>
end,
TI = byte_size(RCE),
NIDs = binary:list_to_bin(NetworkIDs),
MI = otc_util:true_to_one(IN),
<<Counter:3, TI:1, 0:1, IRI:2, MI:1, RCE:TI/binary, NIDs/binary>>;
encode_parameter(intermediate_network_selection, V) ->
#{counter := Counter,
network_identifiers := NetworkIDs,
type_of_routing := TypeOfRouting,
information_type := InformationType
} = V,
ToR = case TypeOfRouting of
neither_constrained_nor_suggested -> 2#00;
constrained -> 2#01;
suggested -> 2#10;
reserved -> 2#11
end,
IT = case InformationType of
ss7 -> 2#00;
reserved -> 2#01;
{network_specific, I} -> I
end,
NIDs = binary:list_to_bin(NetworkIDs),
<<Counter:2, 0:2, ToR:2, IT:2, NIDs/binary>>;
encode_parameter(message_type_interworking, V) ->
#{parameter_can_be_dropped := D,
original_message_type := OriginalMessageType
} = V,
Drop = otc_util:true_to_one(D),
OMT = case OriginalMessageType of
unqualified -> 2#000;
udt -> 2#001;
xudt -> 2#010;
ludt -> 2#011;
spare -> 2#111
end,
<<0:1, Drop:1, 0:3, OMT:3>>.

decode_address(Bin, #{address_type := ansi}) ->
decode_ansi_address(Bin);
decode_address(Bin, _Opts) ->
decode_itu_address(Bin).

%% T1.112.3
decode_ansi_address(<<NR:1, RI:1, GTI:4, PCI:1, SSNI:1, Bin0/binary>>) ->
{SSN, Bin1} = case SSNI of
0 -> {undefined, Bin0};
1 -> <<SSN0:8/big, Rest0/binary>> = Bin0,
{parse_ssn(SSN0), Rest0}
end,
{PC, Bin2} = case PCI of
0 -> {undefined, Bin1};
1 -> <<NCM:8, NC:8, NI:8, Rest1/binary>> = Bin1,
{<<NI:8, NC:8, NCM:8>>, Rest1}
end,
GT = case GTI of
2#0001 ->
%% global title includes translation type,
%% numbering plan and encoding scheme
<<TT:8/big, NP:4, ES:4, GT0/binary>> = Bin2,
GT1 = decode_gt_part(ES, GT0),
GT1#{translation_type => TT,
numbering_plan => NP};
2#0010 ->
%% global title includes translation type
%% only
<<TT:8/big, GT0/binary>> = Bin2,
#{translation_type => TT,
address => GT0}
end,
RoutingInd = case RI of
1 -> subsystem_number;
0 -> global_title
end,
#{national_use_indicator => 1 =:= NR,
routing_indicator => RoutingInd,
global_title => GT,
subsystem_number => SSN,
point_code => PC
}.

decode_itu_address(<<NR:1, RI:1, GTI:4, SSNI:1, PCI:1, Bin0/binary>>) ->
{PC, Bin1} = case PCI of
0 -> {undefined, Bin0};
Expand Down
Loading
Loading