dummy_map_server.py revision ebfd5b56
1# Usage:
2#
3#   $ python dummy_map_server <bind-ip> <port>
4#
5
6import sys
7import socket
8import hmac
9import hashlib
10
11map_notify = (b"\x40\x00\x00\x01"
12    + "\x00\x00\x00\x00"
13    + "\x00\x00\x00\x00"
14    + "\x00\x01\x00\x14"  # key ID, Auth data length = 20
15    + "\x00\x00\x00\x00"
16    + "\x00\x00\x00\x00"
17    + "\x00\x00\x00\x00"
18    + "\x00\x00\x00\x00"
19    + "\x00\x00\x00\x00" # auth data
20    + "\x00\x01"
21    + "\x51\x80\x01\x18\x00\x00\x00\x00\x00\x01\x06\x00\x01\x00\x01\x01"
22    + "\x00\x00\x00\x04\x00\x01\x06\x00\x03\x01")
23
24notify_nonce_offset = 4
25notify_auth_data_len = 20
26register_nonce_offset = 4
27auth_data_offset = 16
28secret_key = 'password'
29
30
31def build_notify(nonce):
32  rp = bytearray(map_notify)
33
34  for i in range(0, 8):
35    rp[notify_nonce_offset + i] = nonce[i]
36
37  # compute hash
38  digest = hmac.new(secret_key, rp, hashlib.sha1).digest()
39
40  for i in range(0, notify_auth_data_len):
41    rp[auth_data_offset + i] = digest[i]
42
43  return rp
44
45
46def run(host, port):
47  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
48  server_address = (host, int(port))
49  sock.bind(server_address)
50
51  while True:
52    data, address = sock.recvfrom(4096)
53
54    # extract nonce from message
55    nonce = data[register_nonce_offset:register_nonce_offset+8]
56
57    rp = build_notify(nonce)
58    sock.sendto(rp, address)
59    print 'Replied to ', ''.join(x.encode('hex') for x in nonce)
60
61
62if __name__ == "__main__":
63  if len(sys.argv) < 2:
64    raise Exception('IP and port expected')
65
66  run(sys.argv[1], sys.argv[2])
67