-
Notifications
You must be signed in to change notification settings - Fork 21
/
ss7calc.py
executable file
·361 lines (324 loc) · 9.83 KB
/
ss7calc.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
354
355
356
357
358
359
360
361
#!/usr/bin/env python
# encoding: utf-8
"""
ss7calc.py
Created by Philippe Langlois on 2009-11-18.
Copyright (c) 2009 P1 Security. All rights reserved.
http://www.p1security.com
Code under eGPL license. http://www.egpl.info
"""
import sys
import getopt
import os
help_message = '''Usage:
"-h", "--help"
displays this message
"-3", "--383"
set Signaling Point Code as 3-8-3 Format (Recommended, used by ITU)
"-6", "--662"
set Signaling Point Code as 6-6-2 Format
"-5", "--545"
set Signaling Point Code as 5-4-5 Format
"-i", "--int"
set Signaling Point Code as decimal format
"-h", "--hex"
set Signaling Point Code as heximal format
"-u", "--itu"
specify SPC as ITU
"-a", "--ansi"
specify SPC as ANSI
"-r", "--read"
pass file (or standard input if file is '-') and process the content as SPC values
Examples:
$ ./ss7calc.py -i 12345
SS7calc - SS7 Signaling Point Code calculator
SPC Decimal : 12345
Format : Unknown
Hex format : 3039
5-4-5 Format: 24-1-25
3-8-3 Format: 6-7-1
6-6-2 Format: 48-14-1
For more information:
http://en.wikipedia.org/wiki/Point_code
For online versions:
http://jhartman.webd.pl/pc/index.php
http://www.linuxfocus.org/~guido/javascript/ansi-point-code-converter.htm
'''
class SPC():
def __init__(self, intv = None):
self.spc = None
self.verbose = False
self.kind = None
self.csv = False
def set_int(self, intv):
if self.verbose: print(("Setting spc=%s" % intv))
self.spc = int(intv)
def set_hex(self, hexv):
self.spc = int(hexv,16)
if self.verbose: print(("Setting spc=%s" % self.spc))
def set_itu(self):
self.kind = "ITU"
self.kind_detail = "14 bits"
def set_ansi(self):
self.kind = "ANSI"
self.kind_detail = "24 bits"
def set_display_csv(self):
self.csv = True
def kind_string(self):
if self.kind is None:
return "Unknown"
else:
return "%s (%s)" % (self.kind.upper(), self.kind_detail)
def check_split(self, s):
l = s.split('-')
if len(l) is not 3:
print("Error: Wrong format, should be like A-B-C")
return None, None, None
else:
a, b, c = l
if self.verbose: print(("%s --> %d-%d-%d" % (s, a, b, c)))
return a, b, c
def set_545(self, spc545):
"""
>>> s = SPC()
>>> s.set_545('1-2-3')
>>> s.spc
579
>>>
"""
l = spc545.split('-')
if len(l) is not 3:
print("Error: Wrong format, should be like A-B-C")
return
else:
a, b, c = l
a = int(a)
b = int(b)
c = int(c)
if a > 2**5 or b > 2**4 or c > 2**5:
print(("Error: %s does not look like Signaling Point Code Format 5-4-5 (max=%d-%d-%d, min=0-0-0), maybe it is ANSI (8-8-8)?\n" % (spc545, 2**5, 2**4, 2**5)))
return
self.spc = a*2**9 + b*2**5 + c
def set_662(self, spc662):
"""
>>> s = SPC()
>>> s.set_662('1-2-3')
>>> s.spc
267
>>>
"""
l = spc662.split('-')
if len(l) is not 3:
print("Error: Wrong format, should be like A-B-C")
return
else:
a, b, c = l
a = int(a)
b = int(b)
c = int(c)
if a > 2**6 or b > 2**6 or c > 2**2:
print(("Error: %s does not look like Signaling Point Code Format 6-6-2 (max=%d-%d-%d, min=0-0-0), maybe it is ANSI (8-8-8)?\n" % (spc545, 2**6, 2**6, 2**2)))
return
self.spc = a*2**8 + b*2**2 + c
def set_383(self, s):
"""
>>> s = SPC()
>>> s.set_383('1-2-3')
>>> s.spc
2067
>>>
"""
a, b, c = self.check_split(s)
if a is not None:
a = int(a)
b = int(b)
c = int(c)
if a > 2**3 or b > 2**8 or c > 2**3:
print(("Error: %s does not look like Signaling Point Code Format 3-8-3 (max=%d-%d-%d, min=0-0-0), maybe it is ANSI (8-8-8)?\n" % (s, 2**3, 2**8, 2**3)))
return
self.spc = a*2**11 + b*2**3 + c
def get_545(self):
pc = int(self.spc)
a = pc >> 9
b = (pc- a*2**9) >> 5
c = pc - a*2**9 - b*2**5
return a, b, c
def to_545(self):
"""
from ss7calc import *
>>> s = SPC()
>>> s.set_int(1234)
>>> s.to_545()
'2-6-18'
>>>
"""
a, b, c = self.get_545()
return "%d-%d-%d"%(a,b,c)
# return ('-').join( ("%d"%a, "%d"%b, "%d"%c) )
def get_383(self):
pc = int(self.spc)
a = pc >> 11
b = (pc- a*2**11) >> 3
c = pc - a*2**11 - b*2**3
return a, b, c
def to_383(self):
"""
>>> s = SPC()
>>> s.set_int(1234)
>>> s.to_383()
'0-154-2'
>>>
"""
a, b, c = self.get_383()
return "%d-%d-%d"%(a,b,c)
# return ('-').join( ("%d"%a, "%d"%b, "%d"%c) )
def get_662(self):
pc = int(self.spc)
a = pc >> 8
b = (pc- a*2**8) >> 2
c = pc - a*2**8 - b*2**2
return a, b, c
def to_662(self):
"""
>>> s = SPC()
>>> s.set_int(1234)
>>> s.to_662()
'4-52-2'
>>>
"""
a, b, c = self.get_662()
return "%d-%d-%d"%(a,b,c)
def to_hex(self):
"""
>>> s = SPC()
>>> s.set_int(1234)
>>> s.to_hex()
"H'4D2"
>>>
"""
return ("H'%x" % self.spc).upper()
def display(self):
"""
from ss7calc import *
>>> s = SPC()
>>> s.set_int(1234)
>>> s.display()
SPC Decimal : 1234
Format : Unknown
Hex Format : H'4D2
5-4-5 Format: 2-6-18
3-8-3 Format: 0-154-2
6-6-2 Format: 4-52-2
<BLANKLINE>
>>>
"""
if self.csv is True:
print(("%d,%s,%s,%s,%s,%s" % (self.spc,
self.kind_string(),
self.to_hex(),
self.to_545(),
self.to_383(),
self.to_662())))
else:
print(("SPC Decimal : %d" % self.spc))
print(("Format : %s" % self.kind_string()))
print(("Hex Format : " + self.to_hex()))
print(("5-4-5 Format: " + self.to_545()))
print(("3-8-3 Format: " + self.to_383()))
print(("6-6-2 Format: " + self.to_662()))
print("")
def header(self):
"""
Displays header...
"""
if self.csv is True:
return "SS7calc,SS7 Signaling Point Code calculator,by Philippe Langlois,http://www.p1security.com\nSPC Decimal,Format,Hex format,5-4-5 Format,3-8-3 Format,6-6-2 Format"
else:
return "SS7calc - SS7 Signaling Point Code calculator\nby Philippe Langlois - http://www.p1security.com\n"
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
read_file = None
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "ho:vi:x:3:5:6:uar:c", ["help", "output=", "int=", "hex=", "383=", "545=", "662=", "itu", "ansi", "read=", "csv"])
except getopt.error as msg:
raise Usage(msg)
spc = SPC()
# option processing
for option, value in opts:
if option == "-v":
verbose = True
spc.verbose = True
#
if option in ("-h", "--help"):
print((spc.header()))
raise Usage(help_message)
#
if option in ("-o", "--output"):
output = value
#
if option in ("-3", "--383"):
spc.set_383(value)
spc.set_itu()
#
if option in ("-5", "--545"):
spc.set_545(value)
spc.set_itu()
#
if option in ("-6", "--662"):
spc.set_662(value)
spc.set_itu()
#
if option in ("-u", "--itu"):
spc.set_itu()
#
if option in ("-a", "--ansi"):
spc.set_ansi()
#
if option in ("-i", "--int"):
spc.set_int(value)
#
if option in ("-x", "--hex"):
spc.set_hex(value)
#
if option in ("-r", "--read"):
read_file = value
#
if option in ("-c", "--csv"):
spc.set_display_csv()
print((spc.header()))
if read_file is not None:
if read_file == "-":
content = sys.stdin.readlines()
else:
content = open(read_file).readlines()
s = spc
for index,line in enumerate(content):
s.set_int(line.strip())
s.display()
sys.exit()
if spc.spc is not None:
spc.display()
else:
print("Error: Please set a value for PC\n")
print(help_message)
except Usage as err:
print(sys.argv[0].split("/")[-1] + ": " + str(err.msg))
print("\t for help use --help")
return 2
if os.getenv("DOCTEST") == "1":
"""
Hurra for doctest:
DOCTEST=1 ./ss7calc.py -v
http://docs.python.org/library/doctest.html
But check this too:
http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy
"""
import doctest
sys.exit(doctest.testmod())
if __name__ == "__main__":
sys.exit(main())