summaryrefslogtreecommitdiffstats
path: root/tests/data_plane/vpp_lite_topo/scripts/dhcp_client.py
blob: 8e49426ed16ec96a715b0767287fd3f2385c6d90 (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
import sys
from scapy.all import *

def p(s):
    print 'DHCP client: {}'.format(s)

def main(argv):
  src_mac = argv[1]
  dhcp_src = argv[2]

  # needed for scapy not to match replies since DHCP uses broadcast addresses
  # which wouldn't work
  conf.checkIPaddr = False

  while True:
    discover = Ether(dst='ff:ff:ff:ff:ff:ff', src=src_mac)/ \
      IP(src='0.0.0.0', dst='255.255.255.255')/ \
      UDP(dport=67,sport=68)/ \
      BOOTP(op=1, chaddr=src_mac)/ \
      DHCP(options=[('message-type', 'discover'), ('end')])

    ans,unans = srp(discover, timeout=3)
    for snd,rcv in ans:
      if rcv[IP].src == dhcp_src:
        exit(0)
      else:
        p('Unexpected DHCP packet source address! ({})'.format(rcv[IP].src))
        exit(1)

if __name__ == "__main__":
  main(sys.argv)