886e412c649c6c53f17b5b32787ebf4019706a4a
[one.git] / tests / data_plane / vpp_lite_topo / scripts / dummy_map_server.py
1 # Usage:
2 #
3 #   $ python dummy_map_server <bind-ip> <port>
4 #
5
6 import sys
7 import socket
8 import hmac
9 import hashlib
10
11 map_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
24 notify_nonce_offset = 4
25 notify_auth_data_len = 20
26 register_nonce_offset = 4
27 auth_data_offset = 16
28 secret_key = 'password'
29
30
31 def 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
46 def 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
62 if __name__ == "__main__":
63   if len(sys.argv) < 2:
64     raise Exception('IP and port expected')
65
66   run(sys.argv[1], sys.argv[2])