diff --git a/library/VPCD_Adapter.ttcn b/library/VPCD_Adapter.ttcn new file mode 100644 index 000000000..289be06e7 --- /dev/null +++ b/library/VPCD_Adapter.ttcn @@ -0,0 +1,73 @@ +module VPCD_Adapter { + +/* VPCD Adapter layer, sitting on top of VPCD_CodecPort. + * test suites can 'inherit' in order to have a VPCD connection to the IUT which they're testing + * + * (C) 2018-2019 by Harald Welte + * All rights reserved. + * + * Released under the terms of GNU General Public License, Version 2 or + * (at your option) any later version. + */ + +import from VPCD_Types all; +import from VPCD_CodecPort all; +import from VPCD_CodecPort_CtrlFunct all; +import from IPL4asp_Types all; +import from IPL4asp_PortType all; +import from Socket_API_Definitions all; + +modulepar { + integer mp_vpcd_port := 35963; + charstring mp_vpcd_host := "127.0.0.1"; +} + +type component VPCD_Adapter_CT { + port VPCD_CODEC_PT VPCD; + var integer g_vpcd_conn_id; +}; + + +private function f_set_tcp_segmentation() runs on VPCD_Adapter_CT { + /* Set function for dissecting the binary stream into packets */ + var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen); + /* Offset: 0, size of length: 2, delta: 0, multiplier: 1, big-endian: 0 */ + VPCD_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(VPCD, g_vpcd_conn_id, vl_f, {0, 2, 2, 1, 0}); +} + +function f_connect(charstring remote_host := mp_vpcd_host, integer remote_port := mp_vpcd_port) +runs on VPCD_Adapter_CT { + var IPL4asp_Types.Result res; + map(self:VPCD, system:VPCD); + res := VPCD_CodecPort_CtrlFunct.f_IPL4_connect(VPCD, remote_host, remote_port, "", 0, 0, + { tcp := {} }); + if (not ispresent(res.connId)) { + setverdict(fail, "Could not connect to VPCD at ", remote_host, ":", remote_port, + ", check your configuration"); + mtc.stop; + } + g_vpcd_conn_id := res.connId; + f_set_tcp_segmentation(); +} + +function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port) +runs on VPCD_Adapter_CT { + var IPL4asp_Types.Result res; + map(self:VPCD, system:VPCD); + res := VPCD_CodecPort_CtrlFunct.f_IPL4_listen(VPCD, local_host, local_port, { tcp:={} }); + g_vpcd_conn_id := res.connId; + f_set_tcp_segmentation(); +} + +function f_vpcd_send(template (value) VPCD_PDU pdu) runs on VPCD_Adapter_CT { + VPCD.send(ts_VPCD_Send(g_vpcd_conn_id, pdu)); +} + +function f_vpcd_exp(template VPCD_PDU exp) runs on VPCD_Adapter_CT return VPCD_PDU { + var VPCD_RecvFrom rf; + VPCD.receive(tr_VPCD_Recv(g_vpcd_conn_id, exp)) -> value rf; + return rf.msg; +} + + +} diff --git a/library/VPCD_CodecPort.ttcn b/library/VPCD_CodecPort.ttcn new file mode 100644 index 000000000..a9ae97fd0 --- /dev/null +++ b/library/VPCD_CodecPort.ttcn @@ -0,0 +1,64 @@ +module VPCD_CodecPort { + +/* Simple VPCD Codec Port, translating between raw TCP octetstring payload + * towards the IPL4asp port provider, and VPCD primitives + * which carry the decoded VPCD data types as payload. + * + * (C) 2018 by Harald Welte + * All rights reserved. + * + * Released under the terms of GNU General Public License, Version 2 or + * (at your option) any later version. + */ + + +import from IPL4asp_PortType all; +import from IPL4asp_Types all; +import from VPCD_Types all; + +type record VPCD_RecvFrom { + ConnectionId connId, + VPCD_PDU msg +} + +type record VPCD_Send { + ConnectionId connId, + VPCD_PDU msg +} + +template (value) VPCD_Send ts_VPCD_Send(ConnectionId conn_id, template (value) VPCD_PDU msg) := { + connId := conn_id, + msg := msg +} + +template VPCD_RecvFrom tr_VPCD_Recv(template ConnectionId conn_id, template VPCD_PDU msg) := { + connId := conn_id, + msg := msg +} + +private function IPL4_to_VPCD_RecvFrom(in ASP_RecvFrom pin, out VPCD_RecvFrom pout) { + pout.connId := pin.connId; + pout.msg := dec_VPCD_PDU(pin.msg); +} with { extension "prototype(fast)" } + +private function VPCD_to_IPL4_Send(in VPCD_Send pin, out ASP_Send pout) { + pout.connId := pin.connId; + pout.proto := { tcp := {} }; + pout.msg := enc_VPCD_PDU(pin.msg); +} with { extension "prototype(fast)" } + +type port VPCD_CODEC_PT message { + out VPCD_Send; + in VPCD_RecvFrom, + ASP_ConnId_ReadyToRelease, + ASP_Event; +} with { extension "user IPL4asp_PT + out(VPCD_Send -> ASP_Send: function(VPCD_to_IPL4_Send)) + in(ASP_RecvFrom -> VPCD_RecvFrom: function(IPL4_to_VPCD_RecvFrom); + ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple; + ASP_Event -> ASP_Event: simple)" +} + + + +} diff --git a/library/VPCD_CodecPort_CtrlFunct.ttcn b/library/VPCD_CodecPort_CtrlFunct.ttcn new file mode 100644 index 000000000..62b79c5c3 --- /dev/null +++ b/library/VPCD_CodecPort_CtrlFunct.ttcn @@ -0,0 +1,52 @@ +module VPCD_CodecPort_CtrlFunct { + + import from VPCD_CodecPort all; + import from IPL4asp_Types all; + + external function f_IPL4_listen( + inout VPCD_CODEC_PT portRef, + in HostName locName, + in PortNumber locPort, + in ProtoTuple proto, + in OptionList options := {} + ) return Result; + + external function f_IPL4_connect( + inout VPCD_CODEC_PT portRef, + in HostName remName, + in PortNumber remPort, + in HostName locName, + in PortNumber locPort, + in ConnectionId connId, + in ProtoTuple proto, + in OptionList options := {} + ) return Result; + + external function f_IPL4_close( + inout VPCD_CODEC_PT portRef, + in ConnectionId id, + in ProtoTuple proto := { unspecified := {} } + ) return Result; + + external function f_IPL4_setUserData( + inout VPCD_CODEC_PT portRef, + in ConnectionId id, + in UserData userData + ) return Result; + + external function f_IPL4_getUserData( + inout VPCD_CODEC_PT portRef, + in ConnectionId id, + out UserData userData + ) return Result; + + external function f_IPL4_setGetMsgLen( + inout VPCD_CODEC_PT portRef, + in ConnectionId id, + inout f_IPL4_getMsgLen f, + in ro_integer msgLenArgs + ); + + +} + diff --git a/library/VPCD_CodecPort_CtrlFunctDef.cc b/library/VPCD_CodecPort_CtrlFunctDef.cc new file mode 100644 index 000000000..74c1176b1 --- /dev/null +++ b/library/VPCD_CodecPort_CtrlFunctDef.cc @@ -0,0 +1,66 @@ +#include "IPL4asp_PortType.hh" +#include "VPCD_CodecPort.hh" +#include "IPL4asp_PT.hh" + +namespace VPCD__CodecPort__CtrlFunct { + + IPL4asp__Types::Result f__IPL4__listen( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::HostName& locName, + const IPL4asp__Types::PortNumber& locPort, + const IPL4asp__Types::ProtoTuple& proto, + const IPL4asp__Types::OptionList& options) + { + return f__IPL4__PROVIDER__listen(portRef, locName, locPort, proto, options); + } + + IPL4asp__Types::Result f__IPL4__connect( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::HostName& remName, + const IPL4asp__Types::PortNumber& remPort, + const IPL4asp__Types::HostName& locName, + const IPL4asp__Types::PortNumber& locPort, + const IPL4asp__Types::ConnectionId& connId, + const IPL4asp__Types::ProtoTuple& proto, + const IPL4asp__Types::OptionList& options) + { + return f__IPL4__PROVIDER__connect(portRef, remName, remPort, + locName, locPort, connId, proto, options); + } + + IPL4asp__Types::Result f__IPL4__close( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::ConnectionId& connId, + const IPL4asp__Types::ProtoTuple& proto) + { + return f__IPL4__PROVIDER__close(portRef, connId, proto); + } + + IPL4asp__Types::Result f__IPL4__setUserData( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::ConnectionId& connId, + const IPL4asp__Types::UserData& userData) + { + return f__IPL4__PROVIDER__setUserData(portRef, connId, userData); + } + + IPL4asp__Types::Result f__IPL4__getUserData( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::ConnectionId& connId, + IPL4asp__Types::UserData& userData) + { + return f__IPL4__PROVIDER__getUserData(portRef, connId, userData); + } + + void f__IPL4__setGetMsgLen( + VPCD__CodecPort::VPCD__CODEC__PT& portRef, + const IPL4asp__Types::ConnectionId& connId, + Socket__API__Definitions::f__getMsgLen& f, + const Socket__API__Definitions::ro__integer& msgLenArgs) + { + return f__IPL4__PROVIDER__setGetMsgLen(portRef, connId, f, msgLenArgs); + } + + +} + diff --git a/library/VPCD_Types.ttcn b/library/VPCD_Types.ttcn new file mode 100644 index 000000000..5caa0be69 --- /dev/null +++ b/library/VPCD_Types.ttcn @@ -0,0 +1,75 @@ +module VPCD_Types { + +/* VPCD/VPICC Types, implementing the protocol used by vpcd/vpicc of + * vsmartcard.git by Frank Morgner. + */ + +import from General_Types all; +import from Osmocom_Types all; + +type enumerated VPCD_CtrlCmd { + VPCD_CTRL_OFF (0), + VPCD_CTRL_ON (1), + VPCD_CTRL_RESET (2), + VPCD_CTRL_ATR (4) +} with { variant "FIELDLENGTH(8)" }; + +type union VPCD_MsgUnion { + VPCD_CtrlCmd ctrl, + octetstring data +}; + +type record VPCD_PDU { + uint16_t len, + VPCD_MsgUnion u +} with { + variant (len) "LENGTHTO(u)" + variant (u) "CROSSTAG( + ctrl, len = 1; + data, OTHERWISE)" +}; + + +template (value) VPCD_PDU ts_VPCD_CTRL(template (value) VPCD_CtrlCmd cmd) := { + len := 0, // overwritten + u := { + ctrl := cmd + } +} +template (value) VPCD_PDU ts_VPCD_CTRL_OFF := ts_VPCD_CTRL(VPCD_CTRL_OFF); +template (value) VPCD_PDU ts_VPCD_CTRL_ON := ts_VPCD_CTRL(VPCD_CTRL_ON); +template (value) VPCD_PDU ts_VPCD_CTRL_RESET := ts_VPCD_CTRL(VPCD_CTRL_RESET); +template (value) VPCD_PDU ts_VPCD_CTRL_ATR := ts_VPCD_CTRL(VPCD_CTRL_ATR); +template (value) VPCD_PDU ts_VPCD_DATA(template (value) octetstring data) := { + len := 0, //overwritten + u := { + data := data + } +} + +template (present) VPCD_PDU tr_VPCD_CTRL(template (present) VPCD_CtrlCmd cmd) := { + len := ?, + u := { + ctrl := cmd + } +} +template (present) VPCD_PDU tr_VPCD_CTRL_OFF := tr_VPCD_CTRL(VPCD_CTRL_OFF); +template (present) VPCD_PDU tr_VPCD_CTRL_ON := tr_VPCD_CTRL(VPCD_CTRL_ON); +template (present) VPCD_PDU tr_VPCD_CTRL_RESET := tr_VPCD_CTRL(VPCD_CTRL_RESET); +template (present) VPCD_PDU tr_VPCD_CTRL_ATR := tr_VPCD_CTRL(VPCD_CTRL_ATR); +template (present) VPCD_PDU tr_VPCD_DATA(template (present) octetstring data) := { + len := ?, + u := { + data := data + } +} + +external function enc_VPCD_PDU(in VPCD_PDU msg) return octetstring + with { extension "prototype(convert) encode(RAW)" }; + +external function dec_VPCD_PDU(in octetstring msg) return VPCD_PDU + with { extension "prototype(convert) decode(RAW)" }; + + + +} with { encode "RAW" };