async def on_validate(syntax_names, user_data): aarq_apdu_syntax_name, aarq_apdu_entity = user_data if not asn1.is_oid_eq(aarq_apdu_syntax_name, _acse_syntax_name): raise Exception('invalid acse syntax name') aarq_apdu = _decode(aarq_apdu_entity) if aarq_apdu[0] != 'aarq': raise Exception('not aarq message') aarq_external = aarq_apdu[1]['user-information'][0] if aarq_external.direct_ref is not None: if not asn1.is_oid_eq(aarq_external.direct_ref, _encoder.syntax_name): raise Exception('invalid encoder identifier') _, called_ap_title = _get_ap_titles(aarq_apdu) _, called_ae_qualifier = _get_ae_qualifiers(aarq_apdu) _, called_ap_invocation_identifier = \ _get_ap_invocation_identifiers(aarq_apdu) _, called_ae_invocation_identifier = \ _get_ae_invocation_identifiers(aarq_apdu) aarq_user_data = (syntax_names.get_name(aarq_external.indirect_ref), aarq_external.data) user_validate_result = await aio.call(validate_cb, syntax_names, aarq_user_data) aare_apdu = _aare_apdu(syntax_names, user_validate_result, called_ap_title, called_ae_qualifier, called_ap_invocation_identifier, called_ae_invocation_identifier) return _acse_syntax_name, _encode(aare_apdu)
async def connect(request_cb: RequestCb, addr: Address, local_tsel: typing.Optional[int] = None, remote_tsel: typing.Optional[int] = None, local_ssel: typing.Optional[int] = None, remote_ssel: typing.Optional[int] = None, local_psel: typing.Optional[int] = None, remote_psel: typing.Optional[int] = None, local_ap_title: typing.Optional[asn1.ObjectIdentifier] = None, # NOQA remote_ap_title: typing.Optional[asn1.ObjectIdentifier] = None, # NOQA local_ae_qualifier: typing.Optional[int] = None, remote_ae_qualifier: typing.Optional[int] = None, user_data: typing.Optional[IdentifiedEntity] = None ) -> 'Connection': """Connect to ACSE server""" initiate_req = 'initiate-RequestPDU', { 'proposedMaxServOutstandingCalling': 5, 'proposedMaxServOutstandingCalled': 5, 'initRequestDetail': { 'proposedVersionNumber': 1, 'proposedParameterCBB': _parameter_cbb, 'servicesSupportedCalling': _service_support}} req_user_data = _encode(initiate_req) acse_conn = await acse.connect(syntax_name_list=[_mms_syntax_name], app_context_name=_mms_app_context_name, addr=addr, local_tsel=local_tsel, remote_tsel=remote_tsel, local_ssel=local_ssel, remote_ssel=remote_ssel, local_psel=local_psel, remote_psel=remote_psel, local_ap_title=local_ap_title, remote_ap_title=remote_ap_title, local_ae_qualifier=local_ae_qualifier, remote_ae_qualifier=remote_ae_qualifier, user_data=(_mms_syntax_name, req_user_data)) try: res_syntax_name, res_user_data = acse_conn.conn_res_user_data if not asn1.is_oid_eq(res_syntax_name, _mms_syntax_name): raise Exception("invalid syntax name") initiate_res = _decode(res_user_data) if initiate_res[0] != 'initiate-ResponsePDU': raise Exception("invalid initiate response") return _create_connection(request_cb, acse_conn) except Exception: await aio.uncancellable(acse_conn.async_close()) raise
async def connect( syntax_name_list: typing.List[asn1.ObjectIdentifier], app_context_name: asn1.ObjectIdentifier, addr: Address, local_tsel: typing.Optional[int] = None, remote_tsel: typing.Optional[int] = None, local_ssel: typing.Optional[int] = None, remote_ssel: typing.Optional[int] = None, local_psel: typing.Optional[int] = None, remote_psel: typing.Optional[int] = None, local_ap_title: typing.Optional[asn1.ObjectIdentifier] = None, # NOQA remote_ap_title: typing.Optional[asn1.ObjectIdentifier] = None, # NOQA local_ae_qualifier: typing.Optional[int] = None, remote_ae_qualifier: typing.Optional[int] = None, user_data: typing.Optional[IdentifiedEntity] = None) -> 'Connection': """Connect to ACSE server""" syntax_names = SyntaxNames([_acse_syntax_name, *syntax_name_list]) aarq_apdu = _aarq_apdu(syntax_names, app_context_name, local_ap_title, remote_ap_title, local_ae_qualifier, remote_ae_qualifier, user_data) copp_user_data = _acse_syntax_name, _encode(aarq_apdu) copp_conn = await copp.connect(syntax_names=syntax_names, addr=addr, local_tsel=local_tsel, remote_tsel=remote_tsel, local_ssel=local_ssel, remote_ssel=remote_ssel, local_psel=local_psel, remote_psel=remote_psel, user_data=copp_user_data) try: aare_apdu_syntax_name, aare_apdu_entity = copp_conn.conn_res_user_data if not asn1.is_oid_eq(aare_apdu_syntax_name, _acse_syntax_name): raise Exception("invalid syntax name") aare_apdu = _decode(aare_apdu_entity) if aare_apdu[0] != 'aare' or aare_apdu[1]['result'] != 0: raise Exception("invalid apdu") calling_ap_title, called_ap_title = _get_ap_titles(aarq_apdu) calling_ae_qualifier, called_ae_qualifier = _get_ae_qualifiers( aarq_apdu) return _create_connection(copp_conn, aarq_apdu, aare_apdu, calling_ap_title, called_ap_title, calling_ae_qualifier, called_ae_qualifier) except Exception: await aio.uncancellable(_close_connection(copp_conn, _abrt_apdu(1))) raise
async def _read_loop(self): close_apdu = _abrt_apdu(0) try: while True: syntax_name, entity = await self._copp_conn.read() if asn1.is_oid_eq(syntax_name, _acse_syntax_name): if entity[0] == 'abrt': close_apdu = None elif entity[0] == 'rlrq': close_apdu = _rlre_apdu() else: close_apdu = _abrt_apdu(1) break await self._read_queue.put((syntax_name, entity)) finally: self._async_group.close() self._read_queue.close() await aio.uncancellable( _close_connection(self._copp_conn, close_apdu))
async def on_validate(syntax_names, user_data): syntax_name, req_user_data = user_data if not asn1.is_oid_eq(syntax_name, _mms_syntax_name): raise Exception('invalid mms syntax name') initiate_req = _decode(req_user_data) if initiate_req[0] != 'initiate-RequestPDU': raise Exception('invalid initiate request') initiate_res = 'initiate-ResponsePDU', { 'negotiatedMaxServOutstandingCalling': 5, 'negotiatedMaxServOutstandingCalled': 5, 'negotiatedDataStructureNestingLevel': 4, # TODO compatibility 'initResponseDetail': { 'negotiatedVersionNumber': 1, 'negotiatedParameterCBB': _parameter_cbb, 'servicesSupportedCalled': _service_support}} if 'localDetailCalling' in initiate_req[1]: initiate_res[1]['localDetailCalled'] = \ initiate_req[1]['localDetailCalling'] res_user_data = _encode(initiate_res) return _mms_syntax_name, res_user_data
async def _read_loop(self): running = True try: while running: syntax_name, entity = await self._acse_conn.read() if not asn1.is_oid_eq(syntax_name, _mms_syntax_name): continue pdu = _decode(entity) running = await self._process_pdu(pdu) except asyncio.CancelledError: pdu = 'conclude-RequestPDU', None data = _mms_syntax_name, _encode(pdu) self._acse_conn.write(data) # TODO: wait for response raise finally: self._async_group.close() self._unconfirmed_queue.close() for response_future in self._response_futures.values(): if not response_future.done(): response_future.set_exception( ConnectionError('connection is not open')) await aio.uncancellable(self._acse_conn.async_close())
def get_id(self, syntax_name: asn1.ObjectIdentifier) -> int: """Get syntax id associated with name""" syntax_id, _ = util.first(self._syntax_names.items(), lambda i: asn1.is_oid_eq(i[1], syntax_name)) return syntax_id
def test_is_oid_eq(oid1, oid2, expected): assert asn1.is_oid_eq(oid1, oid2) is expected