forked from farrokhi/pyprotosim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libSmpp.py
353 lines (316 loc) · 10.8 KB
/
libSmpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
######################################################################
# Copyright (c) 2012-2014, Sergej Srepfler <[email protected]>
# February 2012 -
# Version 0.3.2, Last change on Apr 21, 2014
# This software is distributed under the terms of BSD license.
######################################################################
# All functions needed to build/decode SMPP (Short Message Peer to Peer) messages
import xml.dom.minidom as minidom
import struct
import codecs
import socket
import sys
import logging
import time
import string
FLAG_RES = 0x80000000
TON_Unknown = 0
TON_International = 1
TON_National = 2
TON_NetworkSpecific = 3
TON_SubscriberNumber= 4
TON_Alphanumeric = 5
TON_Abbreviated = 6
NPI_Unknown = 0
NPI_ISDN = 1
NPI_Data = 3
NPI_Telex = 4
NPI_LandMobile = 6
NPI_National = 8
NPI_Private = 9
NPI_ERMES = 10
NPI_IP = 14
NPI_WAPClientId = 18
MS_ENROUTE = 1 #The message is in enroute state.
MS_DELIVERED = 2 #Message is delivered to destination
MS_EXPIRED = 3 #Message validity period has expired.
MS_DELETED = 4 #Message has been deleted.
MS_UNDELIVERABLE = 5 #Message is undeliverable
MS_ACCEPTED = 6 #Message is in accepted state
MS_UNKNOWN = 7 #Message is in invalid state
MS_REJECTED = 8 #Message is in a rejected state
# Header fields
# Include common routines for all modules
ERROR = -1
# Hopefully let's keep dictionary definition compatibile
class dictItem:
def __init__(self):
self.code=0
self.name=""
self.type=""
self.mandatory=[]
self.desc=""
class HDRItem:
def __init__(self):
self.len=0
self.operation=0
self.result=0
self.sequence=0
self.msg=""
self.mandatory=[]
self.optional=[]
# Load simplified dictionary from <file>
def LoadDictionary(file):
global dict_msg
global dict_tag
doc = minidom.parse(file)
node = doc.documentElement
dict_msg = doc.getElementsByTagName("msg")
dict_tag = doc.getElementsByTagName("tag")
# Find Command definition in dictionary: 2->bind_transmitter
def dictMSGcode2name(code):
global dict_msg
cmd=ERROR
for cmd in dict_msg:
cName=cmd.getAttribute("name")
cCode=cmd.getAttribute("code")
if code==cCode:
return cName
dbg="Unknown command",code
bailOut(dbg)
def dictFindMandatoryAVP(code):
global dict_msg
ret=[]
for command in dict_msg:
cCode=command.getAttribute("code")
if code==cCode:
for cMandatory in command.getElementsByTagName("mandatory"):
cName=cMandatory.getAttribute("name")
ret.append(cName)
return ret
return ERROR
def dictFindOptionalAVPbyCode(code):
for command in dict_tag:
cCode=command.getAttribute("code")
cName=command.getAttribute("name")
if code==cCode:
return cName
return ERROR
def dictFindOptionalAVPbyName(name):
for command in dict_optional:
cCode=command.getAttribute("code")
cName=command.getAttribute("name")
if name==cName:
return cCode
return ERROR
def dictFindDetails(code,mName):
global dict_msg
for command in dict_msg:
cCode=command.getAttribute("code")
if code==cCode:
for cMandatory in command.getElementsByTagName("mandatory"):
cName=cMandatory.getAttribute("name")
cType=cMandatory.getAttribute("type")
cMax=cMandatory.getAttribute("max")
if cName==mName:
return cName,cType,cMax
dbg="Unknown",mName,"for code",code
bailOut(dbg)
def dictFindTagDetails(oName):
global dict_tag
for command in dict_tag:
cName=command.getAttribute("name")
cType=command.getAttribute("type")
cCode=command.getAttribute("code")
if cName==oName:
return cName,cType,cCode
dbg="Unknown",oName
bailOut(dbg)
#----------------------------------------------------------------------
#
# Decoding section
#
def decode_Integer32(data):
ret=struct.unpack("!U",data.decode("hex"))[0]
return int(ret)
def decode_Int(data):
return ord(data.decode("hex"))
def decode_Integer16(data):
ret=struct.unpack("!H",data.decode("hex"))[0]
return int(ret)
def decode_as(msg,cType,cMax):
if cType=="C-OS":
(sValue,msg)=smart_chop(msg,cMax)
return (sValue.decode('hex'),msg)
if cType=="Byte":
(sValue,msg)=chop_msg(msg,2)
return (str(decode_Int(sValue)),msg)
if cType=="Word":
(sValue,msg)=chop_msg(msg,4)
return (str(decode_Integer16(sValue)),msg)
if cType=="OctetString":
(sValue,msg)=chop_msg(msg,cMax)
return (sValue,msg)
if cType=="None":
return('',msg)
dbg="Unknown type",cType
bailOut(dbg)
#----------------------------------------------------------------------
# Quit program with error
def bailOut(msg):
logging.error(msg)
sys.exit(1)
#Split message into parts (remove field from remaining body)
def chop_msg(msg,size):
return (msg[0:size],msg[size:])
def smart_chop(msg,cMax):
ret=''
count=0
while msg[:2]!='00':
(cc,msg)=chop_msg(msg,2)
ret=ret+cc
count+=1
if count==cMax:
break
if len(msg)==0:
return (ret,msg)
if count!=cMax:
(cc,msg)=chop_msg(msg,2)
return (ret,msg)
#----------------------------------------------------------------------
# 0 1 2 3
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Command length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Command id |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Command status |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Sequence number |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Message=first Mandatory, then Optional parameters.....
# Main message decoding routine
# Input: diameter message as HEX string
# Result: class H with splitted message (header+message)
# AVPs in message are NOT splitted
def stripHdr(H,msg):
dbg="Incoming SMPP msg",msg
logging.info(dbg)
if len(msg)==0:
return ERROR
(slen,msg)=chop_msg(msg,8)
(soperation,msg)=chop_msg(msg,8)
(sresult,msg)=chop_msg(msg,8)
(ssequence,msg)=chop_msg(msg,8)
dbg="Split hdr","L",slen,"I",soperation,"S",sresult,"N",ssequence,"D",msg
logging.debug(dbg)
H.len=decode_Integer32(slen)
H.operation=soperation
H.result=decode_Integer32(sresult)
H.sequence=decode_Integer32(ssequence)
dbg=dictMSGcode2name(soperation)
logging.info(dbg)
H.msg=msg
return
# Split AVPs from message
# Input: H.msg as hex string
# Result: list of undecoded AVPs
def splitMsgAVPs(H):
ret=[]
dbg="Undecoded msg",H.msg
opt=decodeMandatory(H)
decodeOptional(H,opt)
dbg="Mandatory:",H.mandatory
logging.info(dbg)
dbg="Optional:",H.optional
logging.info(dbg)
return
def decodeMandatory(H):
msg=H.msg
ret=[]
for mandatory in dictFindMandatoryAVP(H.operation):
cName,cType,cMax=dictFindDetails(H.operation,mandatory)
dbg="mandatory param:",cName,cType,cMax
logging.debug(dbg)
# Fix to get previously defined length
#e.g short_message and short_message_len
# For OctetString, previously decoded value is len
if cType=="OctetString":
cMax=int(ret[-1].split('=')[1])
(data,msg)=decode_as(msg,cType,cMax)
ret.append(cName+'='+data)
H.mandatory=ret
return msg
# 0 1 2 3
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Tag | Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Value |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
def decodeOptional(H,msg):
ret=[]
while msg!='':
(sTag,msg)=chop_msg(msg,4)
(sLen,msg)=chop_msg(msg,4)
vLen=decode_Integer16(sLen)
(sValue,msg)=chop_msg(msg,2*vLen)
cName=dictFindOptionalAVPbyCode(sTag)
#----------------------------------------------------------------------
def packHdr(H):
# first add all avps into single string if needed
if H.msg=="":
H.msg=encodeMandatory(H)+encodeOptional(H)
# since all data is hex ecoded, divide by 2 and add header length
H.len=len(H.msg)/2+16
ret="%08X" % H.len+H.operation + "%08X"%int(H.result)+"%08X"%int(H.sequence)
ret=ret+H.msg
dbg="Header fields","L",H.len,"O",H.operation,"R",H.result,"S",H.sequence,"M",H.msg
logging.debug(dbg)
dbg="SMPP hdr+data",ret
logging.info(dbg)
return ret
def encodeMandatory(H):
msg=''
for mandatory in dictFindMandatoryAVP(H.operation):
cName,cType,cMax=dictFindDetails(H.operation,mandatory)
dbg="mandatory param:",cName,cType,cMax
logging.debug(dbg)
for v in H.mandatory:
if string.find(v,cName+'=')==0:
msg+=encodeAVP(cType,v[len(cName)+1:])
dbg="Encoded mandatory:",msg
logging.info(dbg)
return msg
def encodeOptional(H):
# NOT Implemented yet - I never needed them
msg=''
return msg
def encodeAVP(cType,value):
dbg="AVP:",cType,value
logging.debug(dbg)
if cType=="C-OS":
return value.encode("hex")+"00"
if cType=="Byte":
return "%02X"%int(value)
if cType=="Word":
return "%04X"%int(value)
if cType=="OctetString":
return value.encode("hex")
if cType=="None":
return ""
dbg="Unknown type",cType
bailOut(dbg)
#----------------------------------------------------------------------
# Connect to host:port (TCP)
def Connect(host,port):
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
return sock
######################################################
# History
# 0.3.1 - Nov 16 '12 - initial version
# 0.3.2 - Apr 21 '14 - updated to work with new dictionary