blob: 8bcc3fd9961bc43d74a01037331e2f3afaa5061c [file] [log] [blame]
Harald Weltec4ac9e02021-04-22 23:07:44 +02001module OPCAP_Adapter {
2
3/* OPCAP Adapter layer, sitting on top of OPCAP_CodecPort.
4 * test suites can 'inherit' in order to have a OPCAP connection to the IUT which they're testing
5 *
6 * (C) 2021 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 OPCAP_Types all;
17import from OPCAP_Templates all;
18import from OPCAP_CodecPort all;
19import from OPCAP_CodecPort_CtrlFunct all;
20import from IPL4asp_Types all;
21import from IPL4asp_PortType all;
22import from Socket_API_Definitions all;
23
24const integer NUM_OPCAP := 3;
25
26type component OPCAP_Adapter_CT {
27 /* down-facing port to OPCAP Codec port */
28 port OPCAP_CODEC_PT OPCAP[NUM_OPCAP];
29 var IPL4asp_Types.ConnectionId g_opcap_conn_id[NUM_OPCAP] := { -1, -1, -1 };
30}
31
32private function f_set_tcp_segmentation(integer idx) runs on OPCAP_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: 2, size of length: 2, delta: 4, multiplier: 1, big-endian */
36 OPCAP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(OPCAP[idx], g_opcap_conn_id[idx], vl_f, {2, 2, 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 OPCAP_Adapter_CT {
42 var IPL4asp_Types.Result res;
43 map(self:OPCAP[idx], system:OPCAP);
44 if (g_opcap_conn_id[idx] != -1) {
45 OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
46 g_opcap_conn_id[idx] := -1;
47 }
48 res := OPCAP_CodecPort_CtrlFunct.f_IPL4_connect(OPCAP[idx], remote_host, remote_port,
49 local_host, local_port, 0, { tcp :={} });
50 if (not ispresent(res.connId)) {
51 setverdict(fail, "Could not connect to OPCAP port, check your configuration ",
52 "{remote ", remote_host, ":", remote_port, " local ", local_host, ":", local_port, "}");
53 mtc.stop;
54 }
55 g_opcap_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 OPCAP_Adapter_CT {
63 var IPL4asp_Types.Result res;
64 map(self:OPCAP[idx], system:OPCAP);
65 if (g_opcap_conn_id[idx] != -1) {
66 OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
67 g_opcap_conn_id[idx] := -1;
68 }
69 res := OPCAP_CodecPort_CtrlFunct.f_IPL4_listen(OPCAP[idx], local_host, local_port, { tcp:={} });
70 if (not ispresent(res.connId)) {
71 setverdict(fail, "Could not bind to OPCAP port, check your configuration ",
72 "{local ", local_host, ":", local_port, "}");
73 mtc.stop;
74 }
75 g_opcap_conn_id[idx] := res.connId;
76
77 f_set_tcp_segmentation(idx);
78}
79
80function f_wait_client_connect(integer idx) runs on OPCAP_Adapter_CT {
81 var IPL4asp_Types.PortEvent rx_event;
82 OPCAP[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_opcap_conn_id[idx] := rx_event.connOpened.connId;
86 }
87}
88
89function f_disconnect(integer idx) runs on OPCAP_Adapter_CT {
90 OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
91 OPCAP[idx].clear;
92};
93
94function f_opcap_send(template (value) OPCAP_PDU pdu, integer idx := 0) runs on OPCAP_Adapter_CT {
95 OPCAP[idx].send(ts_OPCAP_Send(g_opcap_conn_id[idx], pdu));
96}
97
98function f_opcap_exp(template OPCAP_PDU exp, integer idx := 0) runs on OPCAP_Adapter_CT return OPCAP_PDU {
99 var OPCAP_RecvFrom rf;
100 OPCAP[idx].receive(tr_OPCAP_Recv(g_opcap_conn_id[idx], exp)) -> value rf;
101 return rf.msg;
102}
103
104
105}