blob: 046f597cb94e790e2343d08badf1cb8ca9a308fb [file] [log] [blame]
Harald Welte02acf822019-09-23 06:05:52 +02001module SABP_Adapter {
2
3/* SABP Adapter layer, sitting on top of SABP_CodecPort.
4 * test suites can 'inherit' in order to have a SABP connection to the IUT which they're testing
5 *
6 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
7 * All rights reserved.
8 *
9 * Released under the terms of GNU General Public License, Version 2 or
10 * (at your option) any later version.
11 */
12
13
14import from Osmocom_Types all;
15import from General_Types all;
16import from SABP_Types all;
17import from SABP_PDU_Descriptions all;
18import from SABP_Templates all;
19import from SABP_CodecPort all;
20import from SABP_CodecPort_CtrlFunct all;
21import from IPL4asp_Types all;
22import from IPL4asp_PortType all;
Harald Welte08332302019-08-01 09:54:40 +020023import from Socket_API_Definitions all;
Harald Welte02acf822019-09-23 06:05:52 +020024
25const integer SABP_HDR_LEN := 3;
26
27const integer NUM_SABP := 3;
28
29type component SABP_Adapter_CT {
30 /* down-facing port to SABP Codec port */
31 port SABP_CODEC_PT SABP[NUM_SABP];
32 var IPL4asp_Types.ConnectionId g_sabp_conn_id[NUM_SABP] := { -1, -1, -1 };
33}
34
35/*! parse a single APER length determinant. Return -1 if input insufficient or -2 if invalid */
36private function f_aper_len_det(in octetstring stream, out integer len_len) return integer {
37 if (lengthof(stream) < 1) {
38 return -1;
39 }
40
41 select (stream[0] and4b 'C0'O) {
42 case ('00'O) {
43 /* total length is encoded in this octet */
44 len_len := 1;
45 return oct2int(stream[0]);
46 }
47 case ('80'O) {
48 /* total length (up to 16k) encoded in two octets */
49 if (lengthof(stream) < 2) {
50 return -1;
51 }
52 len_len := 2;
53 return (oct2int(stream[0] and4b '3F'O) * 256) + oct2int(stream[1]);
54 }
55 case ('C0'O) {
56 /* total length not known, encoded in chunks; first chunk length now known */
57 len_len := 1;
58 return oct2int(stream[0] and4b '3F'O) * 16384;
59 }
60 case else {
61 return -2;
62 }
63
64 }
65}
66
67/* The callback function has to return the length of the message if completely received. It has to return
68 * "-1" if the length cannot be determined. If the message is incomplete, but the length can be
69 * determined, then the function should return the length. In this case the callback function will not be
70 * called again for the given message - possibly increasing the performance. Alternatively the function may
71 * always return "-1" when the message is incomplete.
72 * If the callback function detects that the it will be impossible to determine the length of the message,
73 * even receiving more octets, should return "-2". In this case the connection will be closed and the
74 * length calculation error will be reported. */
Harald Welte08332302019-08-01 09:54:40 +020075private function f_APER_getMsgLen(in octetstring stream, inout Socket_API_Definitions.ro_integer args) return integer {
Harald Welte02acf822019-09-23 06:05:52 +020076 var integer stream_len := lengthof(stream);
77 var integer hdr_len := args[0];
78 var octetstring stream_nohdr;
79 var integer len, len_len;
80
81 if (stream_len < hdr_len + 1) {
82 return -1;
83 }
84 stream_nohdr := substr(stream, hdr_len, stream_len-hdr_len);
85
86 len := f_aper_len_det(stream_nohdr, len_len);
87 if (len < 0) {
88 /* error: return to caller */
89 return len;
90 }
91 if (len < 16384) {
92 /* full length is known: return to caller */
93 return hdr_len + len_len + len;
94 } else {
95 /* 'cursor' to next length indicator */
96 var integer cur := hdr_len + len_len + len;
97 /* iterate the whole chain of chunks */
98 while (true) {
99 if (stream_len < cur + 1) {
100 return -1;
101 }
102 len := f_aper_len_det(substr(stream, cur, stream_len-cur), len_len);
103 if (len < 0) {
104 /* error: return to caller */
105 return len;
106 }
107 if (len < 16384) {
108 /* final chunk: segment with less than 16384 bytes */
109 return cur + len_len + len;
110 } else {
111 /* point to next chunk */
112 cur := cur + len_len + len;
113 }
114 }
115 }
116 /* not reached */
117 return -2;
118}
119
120private function f_set_tcp_segmentation(integer idx) runs on SABP_Adapter_CT {
121 /* Set function for dissecting the binary stream into packets */
122 var f_IPL4_getMsgLen vl_f := refers(f_APER_getMsgLen);
123 /* Offset: 1, size of length: 3, delta: 4, multiplier: 1, big-endian */
124 SABP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(SABP[idx], g_sabp_conn_id[idx], vl_f, {SABP_HDR_LEN});
125}
126
127function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port,
128 charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
129runs on SABP_Adapter_CT {
130 var IPL4asp_Types.Result res;
131 map(self:SABP[idx], system:SABP);
132 res := SABP_CodecPort_CtrlFunct.f_IPL4_connect(SABP[idx], remote_host, remote_port,
133 local_host, local_port, 0, { tcp :={} });
134 if (not ispresent(res.connId)) {
135 setverdict(fail, "Could not connect to SABP port, check your configuration");
136 mtc.stop;
137 }
138 g_sabp_conn_id[idx] := res.connId;
139
140 f_set_tcp_segmentation(idx);
141}
142
143/* Function to use to bind to a local port as IPA server, accepting remote clients */
144function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
145runs on SABP_Adapter_CT {
146 var IPL4asp_Types.Result res;
147 map(self:SABP[idx], system:SABP);
148 res := SABP_CodecPort_CtrlFunct.f_IPL4_listen(SABP[idx], local_host, local_port, { tcp:={} });
149 g_sabp_conn_id[idx] := res.connId;
150
151 f_set_tcp_segmentation(idx);
152}
153
154function f_sabp_send(template (value) SABP_PDU pdu, integer idx := 0) runs on SABP_Adapter_CT {
155 SABP[idx].send(ts_SABP_Send(g_sabp_conn_id[idx], pdu));
156}
157
158function f_sabp_exp(template SABP_PDU exp, integer idx := 0) runs on SABP_Adapter_CT return SABP_PDU {
159 var SABP_RecvFrom rf;
160 SABP[idx].receive(tr_SABP_Recv(g_sabp_conn_id[idx], exp)) -> value rf;
161 return rf.msg;
162}
163
164
165}