This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Max gerrit-no-reply at lists.osmocom.orgHello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/1268 to look at the new patch set (#9). Add twisted-based IPA multiplex Add sample applications using twisted framework for IPA and CTRL multiplex. Change-Id: I07559df420b7fe8418f3412f45acd9a375e43bc5 Related: SYS#3028 --- A openbsc/contrib/twisted_ipa.py 1 file changed, 359 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/68/1268/9 diff --git a/openbsc/contrib/twisted_ipa.py b/openbsc/contrib/twisted_ipa.py new file mode 100755 index 0000000..1ce72a7 --- /dev/null +++ b/openbsc/contrib/twisted_ipa.py @@ -0,0 +1,359 @@ +#!/usr/bin/python3 +# -*- mode: python-mode; py-indent-tabs-mode: nil -*- +""" +/* + * Copyright (C) 2016 sysmocom s.f.m.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +""" + +from ipa import Ctrl, IPA +from twisted.internet.protocol import ReconnectingClientFactory +from twisted.internet import reactor, protocol +from twisted.protocols import basic + + +class IPACommon(basic.Int16StringReceiver): + """ + Generic IPA protocol handler: include some routines for simpler subprotocols + It's not intended as full implementation of all subprotocols, rather common ground and example code + """ + def osmo_CTRL(self, data): + """ + OSMO CTRL protocol + Placeholder, see corresponding derived class + """ + pass + + def osmo_MGCP(self, data): + """ + OSMO MGCP extension + """ + if self.factory.debug: + print('OSMO MGCP received %s' % data) + + def osmo_LAC(self, data): + """ + OSMO LAC extension + """ + if self.factory.debug: + print('OSMO LAC received %s' % data) + + def osmo_SMSC(self, data): + """ + OSMO SMSC extension + """ + if self.factory.debug: + print('OSMO SMSC received %s' % data) + + def osmo_ORC(self, data): + """ + OSMO ORC extension + """ + if self.factory.debug: + print('OSMO ORC received %s' % data) + + def osmo_GSUP(self, data): + """ + OSMO GSUP extension + """ + if self.factory.debug: + print('OSMO GSUP received %s' % data) + + def osmo_OAP(self, data): + """ + OSMO OAP extension + """ + if self.factory.debug: + print('OSMO OAP received %s' % data) + + def osmo_UNKNOWN(self, data): + """ + OSMO defaul extension handler + """ + if self.factory.debug: + print('OSMO unknown extension received %s' % data) + + def handle_RSL(self, d, pr, ex): + """ + RSL protocol handler + """ + if self.factory.debug: + print('IPA RSL received message with attribute %s' % ex) + + def handle_CCM(self, data, pr, msgt): + """ + CCM (IPA Connection Management) + Placeholder, see corresponding derived class + """ + pass + + def handle_SCCP(self, d, pr, ex): + """ + SCCP protocol handler + """ + if self.factory.debug: + print('IPA SCCP received message with attribute %s' % ex) + + def handle_OML(self, d, pr, ex): + """ + OML protocol handler + """ + if self.factory.debug: + print('IPA OML received message with attribute %s' % ex) + + def handle_OSMO(self, d, pr, ex): + """ + Dispatcher point for OSMO subprotocols based on extension name, lambda default should never happen + """ + method = getattr(self, 'osmo_' + IPA().ext(ex), lambda: "extension dispatch failure") + method(d) + + def handle_MGCP(self, d, pr, ex): + """ + MGCP protocol handler + """ + if self.factory.debug: + print('IPA MGCP received message with attribute %s' % ex) + + def handle_UNKNOWN(self, d, pr, ex): + """ + Default protocol handler + """ + if self.factory.debug: + print('IPA received message for %s (%s) protocol with attribute %s' % (IPA().proto(pr), pr, ex)) + + def process_chunk(self, data): + """ + Generic message dispatcher for IPA (sub)protocols based on protocol name, lambda default should never happen + """ + (_, pr, ex, d) = IPA().del_header(data, True) + if d is not None: + if self.factory.debug: + print('IPA received %s::%s [%d/%d] %s' % (IPA().proto(pr), IPA().ext_name(pr, ex), len(data), len(d), d)) + method = getattr(self, 'handle_' + IPA().proto(pr), lambda: "protocol dispatch failure") + method(d, pr, ex) + + def dataReceived(self, data): + """ + Override for dataReceived from Int16StringReceiver because of inherently incompatible interpretation of length + If default handler is used than we would always get off-by-1 error (Int16StringReceiver use equivalent of l + 2) + """ + if len(data): + (l, pr, ex, d) = IPA().del_header(data) + self.process_chunk(data[2:(l + 3)]) + self.dataReceived(data[(l + 3):]) + + def connectionMade(self): + """ + We have to resetDelay() here to drop internal state to default values to make reconnection logic work + Make sure to call this via super() if overriding to keep reconnection logic intact + """ + if self.factory.debug: + print('IPA connection made!') + self.factory.resetDelay() + + +class CCM(IPACommon): + """ + Implementation of CCM protocol for IPA multiplex + """ + def ack(self): + self.transport.write(IPA().id_ack()) + + def ping(self): + self.transport.write(IPA().ping()) + + def pong(self): + self.transport.write(IPA().pong()) + + def handle_CCM(self, data, pr, msgt): + """ + CCM (IPA Connection Management) + Only basic logic necessary for tests is implemented (ping-pong, id ack etc) + """ + if msgt == IPA.MSGT['ID_GET']: + self.transport.getHandle().sendall(IPA().id_resp(self.factory.ccm_id)) + # if we call + # self.transport.write(IPA().id_resp(self.factory.test_id)) + # instead, than we would have to also call + # reactor.callLater(1, self.ack) + # instead of self.ack() + # otherwise the writes will be glued together - hence the necessity for ugly hack with 1s timeout + # Note: this still might work depending on the IPA implementation details on the other side + self.ack() + # schedule PING in 4s + reactor.callLater(4, self.ping) + if msgt == IPA.MSGT['PING']: + self.pong() + + +class CTRL(IPACommon): + """ + Implementation of Osmocom control protocol for IPA multiplex + """ + def ctrl_SET(self, data, op, v): + """ + Handle CTRL SET command + """ + if self.factory.debug: + print('CTRL SET [%s] %s' % (op, v)) + + def ctrl_SET_REPLY(self, data, op, v): + """ + Handle CTRL SET reply + """ + if self.factory.debug: + print('CTRL SET REPLY [%s] %s' % (op, v)) + + def ctrl_GET(self, data, op, v): + """ + Handle CTRL GET command + """ + if self.factory.debug: + print('CTRL GET [%s] %s' % (op, v)) + + def ctrl_GET_REPLY(self, data, op, v): + """ + Handle CTRL GET reply + """ + if self.factory.debug: + print('CTRL GET REPLY [%s] %s' % (op, v)) + + def ctrl_TRAP(self, data, op, v): + """ + Handle CTRL TRAP command + """ + if self.factory.debug: + print('CTRL TRAP [%s] %s' % (op, v)) + + def ctrl_ERROR(self, data, op, v): + """ + Handle CTRL ERROR reply + """ + if self.factory.debug: + print('CTRL ERROR [%s] %s' % (op, v)) + + def osmo_CTRL(self, data): + """ + OSMO CTRL message dispatcher, lambda default should never happen + For basic tests only, appropriate handling routines should be replaced: see CtrlServer for example + """ + if self.factory.debug: + print('OSMO CTRL received %s::%s' % Ctrl().parse(data.decode('utf-8'))) + (cmd, op, v) = data.decode('utf-8').split(' ', 2) + method = getattr(self, 'ctrl_' + cmd, lambda: "CTRL unknown command") + method(data, op, v) + + +class IPAServer(CCM): + """ + Test implementation of IPA server + Demonstrate CCM opearation by overriding necessary bits from CCM + """ + def connectionMade(self): + """ + Keep reconnection logic working by calling routine from CCM + Initiate CCM upon connection + """ + print('IPA server connection made!') + super(IPAServer, self).connectionMade() + self.transport.write(IPA().id_get()) + + +class CtrlServer(CTRL): + """ + Test implementation of CTRL server + Demonstarte CTRL handling by overriding simpler routines from CTRL + """ + def connectionMade(self): + """ + Keep reconnection logic working by calling routine from CTRL + Send TRAP upon connection + Note: we can't use sendString() because of it's incompatibility with IPA interpretation of length prefix + """ + print('CTRL server connection made!') + super(CtrlServer, self).connectionMade() + self.transport.write(Ctrl().trap('LOL', 'what')) + self.transport.write(Ctrl().trap('rulez', 'XXX')) + + def reply(self, r): + self.transport.write(r.encode('utf-8')) + + def ctrl_SET(self, data, op, v): + """ + CTRL SET command: always succeed + """ + print('SET [%s] %s' % (op, v)) + self.reply('SET_REPLY %s %s' % (op, v)) + + def ctrl_GET(self, data, op, v): + """ + CTRL GET command: always fail + """ + print('GET [%s] %s' % (op, v)) + self.reply('ERROR %s No variable found' % op) + + +class IPAFactory(ReconnectingClientFactory): + """ + Generic IPA Client Factory which can be used to store state for various subprotocols and manage connections + Note: so far we do not really need separate Factory for acting as a server due to protocol simplicity + """ + protocol = IPACommon + debug = False + ccm_id = IPA().identity(unit = b'1515/0/1', mac = b'b0:0b:fa:ce:de:ad:be:ef', utype = b'sysmoBTS', name = b'StingRay', location = b'hell', sw = IPA.version.encode('utf-8')) + + def __init__(self, proto = None, debug = False, ccm_id = None): + if proto: + self.protocol = proto + if debug: + self.debug = debug + if ccm_id: + self.ccm_id = ccm_id + + def clientConnectionFailed(self, connector, reason): + """ + Only necessayr for as debugging aid - if we can somehow set parent's class noisy attribute than we can omit this method + """ + if self.debug: + print('IPAFactory connection failed:', reason.getErrorMessage()) + ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) + + def clientConnectionLost(self, connector, reason): + """ + Only necessayr for as debugging aid - if we can somehow set parent's class noisy attribute than we can omit this method + """ + if self.debug: + print('IPAFactory connection lost:', reason.getErrorMessage()) + ReconnectingClientFactory.clientConnectionLost(self, connector, reason) + + +if __name__ == '__main__': + print("Twisted IPA %s app with IPA v%s loaded." % ('v0.2', IPA.version)) + # test as CTRL client, for example by connecting to osmo-bsc to receive TRAP messages when osmo-bts-* connects to it: + reactor.connectTCP('localhost', 4250, IPAFactory(CTRL, debug = True)) + # test as CTRL server, for example using bsc_control.py to issue set/get commands: + #reactor.listenTCP(4249, IPAFactory(CtrlServer, debug = True)) + # test as IPA client, for example by connecting to osmo-nitb which would initiate A-bis/IP session: + #reactor.connectTCP('localhost', IPA.TCP_PORT_OML, IPAFactory(CCM, debug = True)) + #reactor.connectTCP('localhost', IPA.TCP_PORT_RSL, IPAFactory(CCM, debug = True)) + # test as IPA server, for example by running osmo-bts-* which would attempt to connect to us: + #reactor.listenTCP(IPA.TCP_PORT_RSL, IPAFactory(IPAServer, debug = True)) + #reactor.listenTCP(IPA.TCP_PORT_OML, IPAFactory(IPAServer, debug = True)) + reactor.run() -- To view, visit https://gerrit.osmocom.org/1268 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newpatchset Gerrit-Change-Id: I07559df420b7fe8418f3412f45acd9a375e43bc5 Gerrit-PatchSet: 9 Gerrit-Project: openbsc Gerrit-Branch: master Gerrit-Owner: Max <msuraev at sysmocom.de> Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org> Gerrit-Reviewer: Jenkins Builder