summaryrefslogtreecommitdiffstats
path: root/tests/data_plane/vpp_lite_topo/scripts/dummy_map_server.py
blob: 886e412c649c6c53f17b5b32787ebf4019706a4a (plain)
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
# Usage:
#
#   $ python dummy_map_server <bind-ip> <port>
#

import sys
import socket
import hmac
import hashlib

map_notify = (b"\x40\x00\x00\x01"
    + "\x00\x00\x00\x00"
    + "\x00\x00\x00\x00"
    + "\x00\x01\x00\x14"  # key ID, Auth data length = 20
    + "\x00\x00\x00\x00"
    + "\x00\x00\x00\x00"
    + "\x00\x00\x00\x00"
    + "\x00\x00\x00\x00"
    + "\x00\x00\x00\x00" # auth data
    + "\x00\x01"
    + "\x51\x80\x01\x18\x00\x00\x00\x00\x00\x01\x06\x00\x01\x00\x01\x01"
    + "\x00\x00\x00\x04\x00\x01\x06\x00\x03\x01")

notify_nonce_offset = 4
notify_auth_data_len = 20
register_nonce_offset = 4
auth_data_offset = 16
secret_key = 'password'


def build_notify(nonce):
  rp = bytearray(map_notify)

  for i in range(0, 8):
    rp[notify_nonce_offset + i] = nonce[i]

  # compute hash
  digest = hmac.new(secret_key, rp, hashlib.sha1).digest()

  for i in range(0, notify_auth_data_len):
    rp[auth_data_offset + i] = digest[i]

  return rp


def run(host, port):
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  server_address = (host, int(port))
  sock.bind(server_address)

  while True:
    data, address = sock.recvfrom(4096)

    # extract nonce from message
    nonce = data[register_nonce_offset:register_nonce_offset+8]

    rp = build_notify(nonce)
    sock.sendto(rp, address)
    print 'Replied to ', ''.join(x.encode('hex') for x in nonce)


if __name__ == "__main__":
  if len(sys.argv) < 2:
    raise Exception('IP and port expected')

  run(sys.argv[1], sys.argv[2])