]> git.evergreen-ils.org Git - working/SIPServer.git/blob - acstest.py
Fix checksum generation so that it actually works with clients.
[working/SIPServer.git] / acstest.py
1 #!/usr/bin/python
2 # Copyright (C) 2006-2008  Georgia Public Library Service
3
4 # Author: David J. Fiander
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of version 2 of the GNU General Public
8 # License as published by the Free Software Foundation.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public
16 # License along with this program; if not, write to the Free
17 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 # MA 02111-1307 USA
19
20 import operator
21 import socket
22 from time import strftime;
23
24 def SipSocket(host='localhost', port=5300):
25     so = socket.socket()
26     so.connect((host, port))
27     return so
28
29 def login(so, uname='scclient', passwd='clientpwd', locn='The basement',
30           seqno=0):
31     port = so.getpeername()[1]
32     if port == 5300:
33         resp = send(so, '9300CN%s|CO%s|CP%s|' % (uname, passwd, locn), seqno)
34         print "Received", repr(resp)
35         print "Verified: ", verify(resp)
36     else:
37         raise "Logging in is only support for the raw transport on port 5300"
38
39 def send(so, msg, seqno=0):
40     if seqno:
41         msg += 'AY' + str(seqno)[0] + 'AZ'
42         msg += ('%04X' % calculate_cksum(msg))
43     msg += '\r'
44     print 'Sending', repr(msg)
45     so.send(msg)
46     resp = so.recv(1000)
47     return resp, verify(resp)
48
49 def calculate_cksum(msg):
50     return (-reduce(operator.add, map(ord, msg)) & 0xFFFF)
51
52 def sipdate():
53     return(strftime("%Y%m%d    %H%M%S"))
54
55 def verify(msg):
56     if msg[-1] == '\r': msg = msg[:-2]
57     if msg[-6:-4] == 'AZ':
58         cksum = calculate_cksum(msg[:-4])
59         return (msg[-4:] == ('%04X' % cksum))
60     # If there's no checksum, then the message is ok
61     return True