blob: 278a2f241123256ba6b06ae985d5b8976f6c7e52 [file] [log] [blame]
Harald Weltecd451ef2019-05-06 17:20:44 +02001module CBSP_Adapter {
2
3/* CBSP Adapter layer, sitting on top of CBSP_CodecPort.
4 * test suites can 'inherit' in order to have a CBSP connection to the IUT which they're testing
5 *
6 * (C) 2018-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 CBSP_Types all;
17import from CBSP_Templates all;
18import from CBSP_CodecPort all;
19import from CBSP_CodecPort_CtrlFunct all;
20import from IPL4asp_Types all;
21import from IPL4asp_PortType all;
22import from Socket_API_Definitions all;
23
24const integer NUM_CBSP := 3;
25
26type component CBSP_Adapter_CT {
27 /* down-facing port to CBSP Codec port */
28 port CBSP_CODEC_PT CBSP[NUM_CBSP];
29 var IPL4asp_Types.ConnectionId g_cbsp_conn_id[NUM_CBSP] := { -1, -1, -1 };
30}
31
32private function f_set_tcp_segmentation(integer idx) runs on CBSP_Adapter_CT {
33 /* Set function for dissecting the binary stream into packets */
34 var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen);
35 /* Offset: 1, size of length: 3, delta: 4, multiplier: 1, big-endian */
36 CBSP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(CBSP[idx], g_cbsp_conn_id[idx], vl_f, {1, 3, 4, 1, 0});
37}
38
39function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port,
40 charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
41runs on CBSP_Adapter_CT {
42 var IPL4asp_Types.Result res;
43 map(self:CBSP[idx], system:CBSP);
Pau Espin Pedrol28aa4662020-09-15 18:32:59 +020044 if (g_cbsp_conn_id[idx] != -1) {
45 CBSP_CodecPort_CtrlFunct.f_IPL4_close(CBSP[idx], g_cbsp_conn_id[idx], {tcp := {}});
46 g_cbsp_conn_id[idx] := -1;
47 }
Harald Weltecd451ef2019-05-06 17:20:44 +020048 res := CBSP_CodecPort_CtrlFunct.f_IPL4_connect(CBSP[idx], remote_host, remote_port,
49 local_host, local_port, 0, { tcp :={} });
50 if (not ispresent(res.connId)) {
Neels Hofmeyr79440042020-07-29 00:49:27 +020051 setverdict(fail, "Could not connect to CBSP port, check your configuration ",
52 "{remote ", remote_host, ":", remote_port, " local ", local_host, ":", local_port, "}");
Harald Weltecd451ef2019-05-06 17:20:44 +020053 mtc.stop;
54 }
55 g_cbsp_conn_id[idx] := res.connId;
56
57 f_set_tcp_segmentation(idx);
58}
59
60/* Function to use to bind to a local port as IPA server, accepting remote clients */
61function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
62runs on CBSP_Adapter_CT {
63 var IPL4asp_Types.Result res;
64 map(self:CBSP[idx], system:CBSP);
Pau Espin Pedrol28aa4662020-09-15 18:32:59 +020065 if (g_cbsp_conn_id[idx] != -1) {
66 CBSP_CodecPort_CtrlFunct.f_IPL4_close(CBSP[idx], g_cbsp_conn_id[idx], {tcp := {}});
67 g_cbsp_conn_id[idx] := -1;
68 }
Harald Weltecd451ef2019-05-06 17:20:44 +020069 res := CBSP_CodecPort_CtrlFunct.f_IPL4_listen(CBSP[idx], local_host, local_port, { tcp:={} });
Pau Espin Pedrol28aa4662020-09-15 18:32:59 +020070 if (not ispresent(res.connId)) {
71 setverdict(fail, "Could not bind to CBSP port, check your configuration ",
72 "{local ", local_host, ":", local_port, "}");
73 mtc.stop;
74 }
Harald Weltecd451ef2019-05-06 17:20:44 +020075 g_cbsp_conn_id[idx] := res.connId;
76
77 f_set_tcp_segmentation(idx);
78}
79
Pau Espin Pedroldb247f82022-08-01 17:55:22 +020080function f_wait_client_connect(integer idx := 0) runs on CBSP_Adapter_CT {
81 var IPL4asp_Types.PortEvent rx_event;
82 CBSP[idx].receive(IPL4asp_Types.PortEvent:{connOpened:=?}) -> value rx_event {
83 log("Connection from ", rx_event.connOpened.remName, ":", rx_event.connOpened.remPort);
84 /* we want to store the client's connId, not the 'bind socket' one */
85 g_cbsp_conn_id[idx] := rx_event.connOpened.connId;
86 }
87 f_set_tcp_segmentation(idx);
88}
89
Harald Weltecd451ef2019-05-06 17:20:44 +020090function f_cbsp_send(template (value) CBSP_PDU pdu, integer idx := 0) runs on CBSP_Adapter_CT {
91 CBSP[idx].send(ts_CBSP_Send(g_cbsp_conn_id[idx], pdu));
92}
93
94function f_cbsp_exp(template CBSP_PDU exp, integer idx := 0) runs on CBSP_Adapter_CT return CBSP_PDU {
95 var CBSP_RecvFrom rf;
96 CBSP[idx].receive(tr_CBSP_Recv(g_cbsp_conn_id[idx], exp)) -> value rf;
97 return rf.msg;
98}
99
100
101}