summaryrefslogtreecommitdiffstats
path: root/src/tcap_helper.erl
blob: eb4051931d962afd7db387e43594c26bf5408079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
% TCAP codec helper functions for test scripts and the like

% (C) 2011 by Harald Welte <laforge@gnumonks.org>
%
% All Rights Reserved
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

-module(tcap_helper).
-author('Harald Welte <laforge@gnumonks.org>').

-include("tcap_asn.hrl").

-export([build_inv_comp/3, build_inv_comp/4,
	 build_retres_comp/2, build_retreslast_comp/2, build_retreslast_comp/1,
	 enc_component/1,
	 build_begin/4, build_begin/3, build_end/3,
	 build_inv_begin/5, build_retresl_end/4
 	]).

format_id(undefined) ->
	asn1_NOVALUE;
format_id(Int) when is_integer(Int) ->
	{present, Int}.

process_uint32(Int) when is_integer(Int) ->
	<<Int:32/big>>;
process_uint32(Int) when is_list(Int); is_binary(Int) ->
	Int.

process_undefined(undefined) ->
	asn1_NOVALUE;
process_undefined(Other) ->
	Other.


% build a decoded Invoke component
build_inv_comp(InvIdIn, Opcode, Argument, LinkedIdIn) ->
	InvId = format_id(InvIdIn),
	LinkedId = format_id(LinkedIdIn),
	{invoke, #'Invoke'{invokeId = InvId, linkedId = LinkedId,
				opcode = {local, Opcode},
				argument = Argument}}.
build_inv_comp(InvIdIn, Opcode, Argument) ->
	build_inv_comp(InvIdIn, Opcode, Argument, undefined).

% build a BER-encoded ReturnResult component
build_retres_comp(InvIdIn, Data) ->
	InvId = format_id(InvIdIn),
	{resurnResult, #'ReturnResult'{invokeId = InvId, result = Data}}.

% build a BER-encoded ReturnResultLast component
build_retreslast_comp(InvIdIn, Data) ->
	InvId = format_id(InvIdIn),
	{returnResultLast, #'ReturnResult'{invokeId = InvId, result = Data}}.
build_retreslast_comp(InvIdIn) ->
	build_retreslast_comp(InvIdIn, asn1_NOVALUE).

% helper function for BER encoding a Component
enc_component(Comp) ->
	{ok, Ret} = tcap_asn:encode('Component', Comp),
	Ret.

% helper function for BER-encoding the DialoguePDU and wrapping it in 'EXTERNAL' type
enc_dialg_ext(Dpdu) ->
	{ok, DpduEnc} = tcap_asn:encode('DialoguePDU', Dpdu),
	ExtPdu = #'EXTERNAL'{'direct-reference' = {0,0,17,773,1,1,1},
			     'encoding' = {'single-ASN1-type', DpduEnc}},
	ExtPdu.

% Build a binary-encoded 'Begin' DialoguePortion message with specified components
build_begin(OtidIn, ACname, ComponentsIn) ->
	build_begin(OtidIn, ACname, asn1_NOVALUE, ComponentsIn).
build_begin(OtidIn, ACname, UserDlgInfoIn, ComponentsIn) ->
	Otid = process_uint32(OtidIn),
	UserDlgInfo = process_undefined(UserDlgInfoIn),
	Dpdu = {dialogueRequest, #'AARQ-apdu'{'protocol-version' = [version1],
					      'application-context-name' = ACname,
					      'user-information' = UserDlgInfo}},
	ExtPdu = enc_dialg_ext(Dpdu),
	{ok, EncComponents} = tcap_asn:encode('Components', ComponentsIn),
	Msg={'begin', #'Begin'{otid = Otid, dialoguePortion = ExtPdu, components = EncComponents}},
	enc_msg(Msg).

% Build a binary-encoded 'End' DialoguePortion message with specified components
build_end(DtidIn, ACname, ComponentsIn) ->
	Dtid = process_uint32(DtidIn),
	Dpdu = {dialogueResponse, #'AARE-apdu'{'protocol-version' = [version1],
						'application-context-name' = ACname,
						'result' = 0}},
	ExtPdu = enc_dialg_ext(Dpdu),
	{ok, EncComponents} = tcap_asn:encode('Components', ComponentsIn),
	Msg = {'end', #'End'{dtid = Dtid, dialoguePortion = ExtPdu, components = EncComponents}},
	enc_msg(Msg).


enc_msg(Dlg) ->
	{ok, Ret} = tcap_asn:encode('TCMessage', Dlg),
	Ret.


% build a BER-encoded Begin dialogue with Invoke component
build_inv_begin(DlgId, InvId, ACname, Opcode, Argument) ->
	C = build_inv_comp(InvId, Opcode, Argument),
	build_begin(DlgId, ACname, [C]).

% build a BER-encoded End dialogue with ReturnResultLast component
build_retresl_end(Dtid, InvId, ACname, Argument) ->
	C = build_retreslast_comp(InvId, Argument),
	build_end(Dtid, ACname, [C]).