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.
52 (src_if, src_mac, dst_mac, src_ip, dst_ip) = parse_arguments()
54 interface = Interface(src_if)
56 # build an ARP request
57 arp_request = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
58 ARP(psrc=src_ip, hwsrc=src_mac, pdst=dst_ip,
59 hwdst='ff:ff:ff:ff:ff:ff'))
62 interface.send_pkt(arp_request)
66 ether = interface.recv_pkt()
68 # verify received packet
70 if not ether.haslayer(ARP):
71 raise RuntimeError('Unexpected packet: does not contain ARP ' +
72 'header "{}"'.format(ether.__repr__()))
77 if arp.op != arp_reply:
78 raise RuntimeError('expected op={}, received {}'.format(arp_reply,
80 if arp.ptype != 0x800:
81 raise RuntimeError('expected ptype=0x800, received {}'.
84 raise RuntimeError('expected hwlen=6, received {}'.
87 raise RuntimeError('expected plen=4, received {}'.format(arp.plen))
88 if arp.hwsrc != dst_mac:
89 raise RuntimeError('expected hwsrc={}, received {}'.
90 format(dst_mac, arp.hwsrc))
91 if arp.psrc != dst_ip:
92 raise RuntimeError('expected psrc={}, received {}'.
93 format(dst_ip, arp.psrc))
94 if arp.hwdst != src_mac:
95 raise RuntimeError('expected hwdst={}, received {}'.
96 format(src_mac, arp.hwdst))
97 if arp.pdst != src_ip:
98 raise RuntimeError('expected pdst={}, received {}'.
99 format(src_ip, arp.pdst))
102 except RuntimeError as ex:
103 print 'Error occurred: {}'.format(ex)
111 """Run the test and collect result"""
112 if arp_request_test():
117 if __name__ == '__main__':