3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """Send an ARP request and verify the reply"""
20 from scapy.all import Ether, ARP
22 from resources.libraries.python.PacketVerifier import Interface
23 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
26 def parse_arguments():
27 """Parse arguments of the script passed through command line
29 :return: tuple of parsed arguments
31 args = TrafficScriptArg(['src_if', 'src_mac', 'dst_mac',
34 # check for mandatory parameters
35 params = (args.get_arg('tx_if'),
36 args.get_arg('src_mac'),
37 args.get_arg('dst_mac'),
38 args.get_arg('src_ip'),
39 args.get_arg('dst_ip'))
41 raise Exception('Missing mandatory parameter(s)!')
46 def arp_request_test():
47 """Send ARP request, expect a reply and verify its fields.
50 :raises RuntimeError: ARP reply timeout.
53 (src_if, src_mac, dst_mac, src_ip, dst_ip) = parse_arguments()
55 interface = Interface(src_if)
57 # build an ARP request
58 arp_request = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
59 ARP(psrc=src_ip, hwsrc=src_mac, pdst=dst_ip,
60 hwdst='ff:ff:ff:ff:ff:ff'))
63 interface.send_pkt(arp_request)
67 ether = interface.recv_pkt()
70 raise RuntimeError("ARP reply timeout")
72 # verify received packet
74 if not ether.haslayer(ARP):
75 raise RuntimeError('Unexpected packet: does not contain ARP ' +
76 'header "{}"'.format(ether.__repr__()))
81 if arp.op != arp_reply:
82 raise RuntimeError('expected op={}, received {}'.format(arp_reply,
84 if arp.ptype != 0x800:
85 raise RuntimeError('expected ptype=0x800, received {}'.
88 raise RuntimeError('expected hwlen=6, received {}'.
91 raise RuntimeError('expected plen=4, received {}'.format(arp.plen))
92 if arp.hwsrc != dst_mac:
93 raise RuntimeError('expected hwsrc={}, received {}'.
94 format(dst_mac, arp.hwsrc))
95 if arp.psrc != dst_ip:
96 raise RuntimeError('expected psrc={}, received {}'.
97 format(dst_ip, arp.psrc))
98 if arp.hwdst != src_mac:
99 raise RuntimeError('expected hwdst={}, received {}'.
100 format(src_mac, arp.hwdst))
101 if arp.pdst != src_ip:
102 raise RuntimeError('expected pdst={}, received {}'.
103 format(src_ip, arp.pdst))
106 except RuntimeError as ex:
107 print 'Error occurred: {}'.format(ex)
113 """Run the test and collect result"""
114 if arp_request_test():
119 if __name__ == '__main__':