Oliver Smith | e53a34a | 2019-11-20 12:48:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | SPDX-License-Identifier: MIT |
| 4 | Copyright 2019 sysmocom s.f.m.c GmbH <info@sysmocom.de> |
| 5 | |
| 6 | WARNING: this is just a proof-of-concept implementation, it blocks for every |
| 7 | received SMPP request and is not suitable for servicing more than one request |
| 8 | at a time. |
| 9 | |
| 10 | Based on esme.py from RCCN (license changed with permission from author): |
| 11 | https://github.com/Rhizomatica/rccn/blob/master/rccn/esme.py |
| 12 | Copyright 2017 keith <keith@rhizomatica.org> |
| 13 | |
| 14 | Forward SMS to the receiver's SMSC, as determined with mslookup. |
| 15 | Requires smpplip (pip3 install --user smpplib) and osmo-mslookup-client. |
| 16 | |
| 17 | Example SMPP configuration for osmo-msc.cfg: |
| 18 | smpp |
| 19 | local-tcp-ip 127.0.0.1 2775 |
| 20 | policy closed |
| 21 | smpp-first |
| 22 | # outgoing to esme_dgsm.py |
| 23 | esme OSMPP |
| 24 | no alert-notifications |
| 25 | password foo |
| 26 | default-route |
| 27 | # incoming from esme_dgsm.py |
| 28 | esme ISMPP |
| 29 | no alert-notifications |
| 30 | password foo |
| 31 | """ |
| 32 | import argparse |
| 33 | import json |
| 34 | import logging |
| 35 | import smpplib |
| 36 | import subprocess |
| 37 | import time |
| 38 | |
| 39 | |
| 40 | def can_handle_pdu(pdu): |
| 41 | if not isinstance(pdu, smpplib.command.DeliverSM): |
| 42 | logging.info('PDU is not a DeliverSM, ignoring') |
| 43 | return False |
| 44 | |
| 45 | if int(pdu.dest_addr_ton) == smpplib.consts.SMPP_TON_INTL: |
| 46 | logging.info("Unable to handle SMS for %s: SMPP_TON_INTL" % |
| 47 | (pdu.destination_addr)) |
| 48 | return False |
| 49 | |
| 50 | return True |
| 51 | |
| 52 | |
| 53 | def query_mslookup(service_type, id, id_type='msisdn'): |
| 54 | query_str = '%s.%s.%s' % (service_type, id, id_type) |
| 55 | logging.info('mslookup: ' + query_str) |
| 56 | |
| 57 | result_line = subprocess.check_output(['osmo-mslookup-client', query_str, |
| 58 | '-f', 'json']) |
| 59 | if isinstance(result_line, bytes): |
| 60 | result_line = result_line.decode('ascii') |
| 61 | |
| 62 | logging.info('mslookup result: ' + result_line.rstrip()) |
| 63 | return json.loads(result_line) |
| 64 | |
| 65 | |
| 66 | def tx_sms(dst_host, dst_port, source, destination, registered_delivery, |
| 67 | unicode_text): |
| 68 | smpp_client = smpplib.client.Client(dst_host, dst_port, 90) |
| 69 | smpp_client.connect() |
| 70 | smpp_client.bind_transceiver(system_id=args.dst_id, password=args.dst_pass) |
| 71 | logging.info('Connected to destination SMSC (%s@%s:%s)' % (args.dst_id, |
| 72 | dst_host, dst_port)) |
| 73 | |
| 74 | pdu = smpp_client.send_message( |
| 75 | source_addr_ton=smpplib.consts.SMPP_TON_ALNUM, |
| 76 | source_addr_npi=smpplib.consts.SMPP_NPI_UNK, |
| 77 | source_addr=source.decode(), |
| 78 | dest_addr_ton=smpplib.consts.SMPP_TON_SBSCR, |
| 79 | dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN, |
| 80 | destination_addr=destination.decode(), |
| 81 | short_message=unicode_text, |
| 82 | registered_delivery=registered_delivery, |
| 83 | ) |
| 84 | |
| 85 | smpp_client.unbind() |
| 86 | smpp_client.disconnect() |
| 87 | del pdu |
| 88 | del smpp_client |
| 89 | |
| 90 | |
| 91 | def rx_deliver_sm(pdu): |
| 92 | if not can_handle_pdu(pdu): |
| 93 | return smpplib.consts.SMPP_ESME_RSYSERR |
| 94 | |
| 95 | msisdn = pdu.destination_addr.decode() |
| 96 | logging.info("Incoming SMS for: " + msisdn) |
| 97 | |
| 98 | if args.sleep: |
| 99 | logging.info("Sleeping for %i seconds" % (args.sleep)) |
| 100 | time.sleep(args.sleep) |
| 101 | logging.info("Sleep done") |
| 102 | |
Neels Hofmeyr | 81ac78d | 2020-01-09 04:44:41 +0100 | [diff] [blame] | 103 | if args.always_fail is not None: |
| 104 | return args.always_fail |
| 105 | |
Oliver Smith | e53a34a | 2019-11-20 12:48:12 +0100 | [diff] [blame] | 106 | result = query_mslookup("smpp.sms", msisdn) |
| 107 | if 'v4' not in result or not result['v4']: |
| 108 | logging.info('No IPv4 result from mslookup! This example only' |
| 109 | ' makes use of IPv4, dropping.') |
| 110 | return smpplib.consts.SMPP_ESME_RSYSERR |
| 111 | |
| 112 | dst_host, dst_port = result['v4'] |
| 113 | tx_sms(dst_host, dst_port, pdu.source_addr, |
| 114 | pdu.destination_addr, int(pdu.registered_delivery), |
| 115 | pdu.short_message) |
| 116 | |
| 117 | return smpplib.consts.SMPP_ESME_ROK |
| 118 | |
| 119 | |
| 120 | def smpp_bind(): |
| 121 | client = smpplib.client.Client(args.src_host, args.src_port, 90) |
| 122 | client.set_message_received_handler(rx_deliver_sm) |
| 123 | client.connect() |
| 124 | client.bind_transceiver(system_id=args.src_id, password=args.src_pass) |
| 125 | logging.info('Connected to source SMSC (%s@%s:%s)' % (args.src_id, |
| 126 | args.src_host, args.src_port)) |
| 127 | logging.info('Waiting for SMS...') |
| 128 | client.listen() |
| 129 | |
| 130 | |
| 131 | def main(): |
| 132 | global args |
| 133 | parser = argparse.ArgumentParser() |
| 134 | parser.add_argument('--src-host', default='127.0.0.1', |
| 135 | help='source SMSC (OsmoMSC) host (default: 127.0.0.1)') |
| 136 | parser.add_argument('--src-port', default=2775, type=int, |
| 137 | help='source SMSC (OsmoMSC) port (default: 2775)') |
| 138 | parser.add_argument('--src-id', default='OSMPP', |
| 139 | help='source system id, as configured in osmo-msc.cfg' |
| 140 | ' (default: OSMPP)') |
| 141 | parser.add_argument('--src-pass', default='foo', |
| 142 | help='source system password, as configured in' |
| 143 | ' osmo-msc.cfg (default: foo)') |
| 144 | parser.add_argument('--dst-id', default='ISMPP', |
| 145 | help='destination system id, as configured in' |
| 146 | ' osmo-msc.cfg (default: ISMPP)') |
| 147 | parser.add_argument('--dst-pass', default='foo', |
| 148 | help='destination system password, as configured in' |
| 149 | ' osmo-msc.cfg (default: foo)') |
| 150 | parser.add_argument('--sleep', default=0, type=float, |
| 151 | help='sleep time in seconds before forwarding an SMS,' |
| 152 | ' to test multithreading (default: 0)') |
Neels Hofmeyr | 81ac78d | 2020-01-09 04:44:41 +0100 | [diff] [blame] | 153 | parser.add_argument('--always-fail', default=None, metavar='SMPP_ESME_ERRCODE', |
| 154 | help='test delivery failure: always return an error code on Deliver-SM,' |
| 155 | ' pass an smpplib error code name like RDELIVERYFAILURE (see smpplib/consts.py),' |
| 156 | ' or an SMPP error code in hex digits') |
Oliver Smith | e53a34a | 2019-11-20 12:48:12 +0100 | [diff] [blame] | 157 | args = parser.parse_args() |
| 158 | |
| 159 | logging.basicConfig(level=logging.INFO, format='[%(asctime)s]' |
| 160 | ' (%(threadName)s) %(message)s', datefmt="%H:%M:%S") |
Neels Hofmeyr | 81ac78d | 2020-01-09 04:44:41 +0100 | [diff] [blame] | 161 | |
| 162 | if args.always_fail: |
| 163 | resolved = None |
| 164 | name = 'SMPP_ESME_' + args.always_fail |
| 165 | if hasattr(smpplib.consts, name): |
| 166 | resolved = getattr(smpplib.consts, name) |
| 167 | if resolved is None: |
| 168 | try: |
| 169 | resolved = int(args.always_fail, 16) |
| 170 | except ValueError: |
| 171 | resolved = None |
| 172 | if resolved is None: |
| 173 | print('Invalid argument for --always-fail: %r' % args.always_fail) |
| 174 | exit(1) |
| 175 | args.always_fail = resolved |
| 176 | logging.info('--always-fail: returning error code %s to all Deliver-SM' % hex(args.always_fail)) |
| 177 | |
Oliver Smith | e53a34a | 2019-11-20 12:48:12 +0100 | [diff] [blame] | 178 | smpp_bind() |
| 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | main() |
Neels Hofmeyr | 81ac78d | 2020-01-09 04:44:41 +0100 | [diff] [blame] | 183 | |
| 184 | # vim: expandtab tabstop=4 shiftwidth=4 |