Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: data_coding should not be applied to all fields that are C-Octet Strings #188

Closed
Slavix72 opened this issue Dec 3, 2019 · 8 comments · Fixed by #190
Closed

Bug: data_coding should not be applied to all fields that are C-Octet Strings #188

Slavix72 opened this issue Dec 3, 2019 · 8 comments · Fixed by #190
Labels
bug Something isn't working

Comments

@Slavix72
Copy link

Slavix72 commented Dec 3, 2019

Python 3.8
Ubuntu 18.04
Naz 0.7.4
I want to send short message (or long short message = message payload) with message_encoding= 8.
I set client.codec_class.encoding eqaul 'ucs2' before client.submit_sm() call.
Naz creates smpp packet:
00 00 00 41 00 00 00 04 00 00 00 00 00 00 00 05 00 00 01 00 32 00 30 00 32 00 01 01 00 37 00 39 00 30 00 38 00 36 00 33 00 31 00 31 00 31 00 33 00 30 00 00 00 00 00 00 00 00 00 00 04 00 37 00 37
Naz encode text='77' to ucs2_codes = 0037 0037. But it encodes src_addr and dst_addr too.
SMSC can not receive such packet.
Packet with correct src_addr and dst_addr is
00 00 00 31 00 00 00 04 00 00 00 00 00 00 00 05 00 00 01 32 30 32 00 01 01 37 39 30 38 36 33 31 31 31 33 30 00 00 00 00 00 00 00 00 00 00 02 37 37
How to encode short_message only to ucs2_codes?

@Slavix72
Copy link
Author

Slavix72 commented Dec 3, 2019

I think that data_coding field is property of short_message, not client

@komuw
Copy link
Owner

komuw commented Dec 3, 2019

Hi @Slavix72
Thanks for reporting this. I can confirm that with the following;

client = naz.Client(
    smsc_host="127.0.0.1",
    smsc_port=2775,
    system_id="smppclient1",
    password="password",
    codec_class=naz.nazcodec.SimpleNazCodec(encoding="ucs2"),
)

I'm able to reproduce this. Smpp server logs;

2019.12.03 12:56:24 895 WARNING 19 Bind failed authentication check.
INFO    19 : BIND_TRANSCEIVER_RESP:
INFO    19 Hex dump (16) bytes:
INFO    19 00000010:80000009:0000000F:00000006:
INFO    19 
INFO    19 cmd_len=16,cmd_id=-2147483639,cmd_status=15,seq_no=6,system_id=SMPPSim

and naz logs;

{
    "timestamp": "2019-12-03 15:56:20,891",
    "event": "naz.Client.command_handlers",
    "smpp_command": "bind_transceiver_resp",
    "command_status": 15,
    "state": "Invalid System ID",
    "smsc_host": "127.0.0.1",
    "system_id": "smppclient1",
}

This is because of;

+ self.codec_class.encode(self.system_id)

@komuw
Copy link
Owner

komuw commented Dec 3, 2019

note to self: although nazcodec allows any python standard encoding[1];

encoder = codecs.getencoder(encoding)

naz.Client._find_data_coding does not play nice;

naz/naz/client.py

Lines 671 to 676 in c47f503

def _find_data_coding(encoding):
for key, val in SmppDataCoding.__dict__.items():
if not key.startswith("__"):
if encoding == val.code:
return val.value
raise ValueError("That encoding:{0} is not recognised.".format(encoding))

  1. https://docs.python.org/3/library/codecs.html#standard-encodings

@komuw
Copy link
Owner

komuw commented Dec 3, 2019

@Slavix72 I'll prepare a fix for this.

@komuw komuw added the bug Something isn't working label Dec 3, 2019
@komuw komuw changed the title Bug (?) with data coding Bug: data_coding should not be applied to all fields that are C-Octet Strings Dec 3, 2019
@komuw
Copy link
Owner

komuw commented Dec 3, 2019

note to self: although nazcodec allows any python standard encoding[1];

encoder = codecs.getencoder(encoding)

naz.Client._find_data_coding does not play nice;

naz/naz/client.py

Lines 671 to 676 in c47f503

def _find_data_coding(encoding):
for key, val in SmppDataCoding.__dict__.items():
if not key.startswith("__"):
if encoding == val.code:
return val.value
raise ValueError("That encoding:{0} is not recognised.".format(encoding))

  1. https://docs.python.org/3/library/codecs.html#standard-encodings

Actually we cant use all python standard encodings, only the ones defined in SMPP spec;

  1. class SmppDataCoding:
  2. Also see section 5.2.19 of SMPP spec

@komuw komuw closed this as completed in #190 Dec 5, 2019
komuw added a commit that referenced this issue Dec 5, 2019
…ields (#190)

What:
 - BugFix, `data_coding` should not be applied to all fields that are of type C-octet string
- Have the naz messsage protocol only use `utf8` for encoding/decoding

Why:
- fixes: #188
- A  C-Octet string, is a series of ASCII characters terminated with the NULL character.      
   see; section 3.1 of SMPP spec      
  Thus we need to encode any field that is a C-Octet strings as ascii and also terminate them with NULL char(chr(0).encode())     
eg, ` system_id.encode("ascii") + chr(0).encode()`     
  Also they should be decoded as such; `message_id.decode('ascii')`
@komuw
Copy link
Owner

komuw commented Dec 5, 2019

@Slavix72 this should now be fixed.

Try version https://pypi.org/project/naz/0.7.5/

Also please read the changelog: https://github.com/komuw/naz/blob/master/CHANGELOG.md#version-v075
there are a few other items that changed.

@komuw
Copy link
Owner

komuw commented Dec 5, 2019

this client should now work okay;

client = naz.Client(
    ...
    codec=naz.codec.SimpleCodec(encoding="ucs2"),
)

@komuw
Copy link
Owner

komuw commented Apr 29, 2020

I think that data_coding field is property of short_message, not client

@Slavix72 This has now been implemented: #201

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants