aboutsummaryrefslogtreecommitdiffstats
path: root/resources/traffic_scripts/arp_request.py
blob: 8c5b9c7c47581fc8e95424a17acf804f336c727c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python

# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Send an ARP request and verify the reply"""

import sys

from scapy.all import Ether, ARP

from resources.libraries.python.PacketVerifier import Interface
from resources.libraries.python.TrafficScriptArg import TrafficScriptArg


def parse_arguments():
    """Parse arguments of the script passed through command line

    :return: tuple of parsed arguments
    """
    args = TrafficScriptArg(['src_if', 'src_mac', 'dst_mac',
                             'src_ip', 'dst_ip'])

    # check for mandatory parameters
    params = (args.get_arg('tx_if'),
              args.get_arg('src_mac'),
              args.get_arg('dst_mac'),
              args.get_arg('src_ip'),
              args.get_arg('dst_ip'))
    if None in params:
        raise Exception('Missing mandatory parameter(s)!')

    return params


def arp_request_test():
    """Send ARP request, expect a reply and verify its fields.

    returns: test status
    :raises RuntimeError: ARP reply timeout.
    """
    test_passed = False
    (src_if, src_mac, dst_mac, src_ip, dst_ip) = parse_arguments()

    interface = Interface(src_if)

    # build an ARP request
    arp_request = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
                   ARP(psrc=src_ip, hwsrc=src_mac, pdst=dst_ip,
                       hwdst='ff:ff:ff:ff:ff:ff'))

    # send the request
    interface.send_pkt(arp_request)

    try:
        # wait for APR reply
        ether = interface.recv_pkt()

        if not ether:
            raise RuntimeError("ARP reply timeout")

        # verify received packet

        if not ether.haslayer(ARP):
            raise RuntimeError('Unexpected packet: does not contain ARP ' +
                               'header "{}"'.format(ether.__repr__()))

        arp = ether['ARP']
        arp_reply = 2

        if arp.op != arp_reply:
            raise RuntimeError('expected op={}, received {}'.format(arp_reply,
                                                                    arp.op))
        if arp.ptype != 0x800:
            raise RuntimeError('expected ptype=0x800, received {}'.
                               format(arp.ptype))
        if arp.hwlen != 6:
            raise RuntimeError('expected hwlen=6, received {}'.
                               format(arp.hwlen))
        if arp.plen != 4:
            raise RuntimeError('expected plen=4, received {}'.format(arp.plen))
        if arp.hwsrc != dst_mac:
            raise RuntimeError('expected hwsrc={}, received {}'.
                               format(dst_mac, arp.hwsrc))
        if arp.psrc != dst_ip:
            raise RuntimeError('expected psrc={}, received {}'.
                               format(dst_ip, arp.psrc))
        if arp.hwdst != src_mac:
            raise RuntimeError('expected hwdst={}, received {}'.
                               format(src_mac, arp.hwdst))
        if arp.pdst != src_ip:
            raise RuntimeError('expected pdst={}, received {}'.
                               format(src_ip, arp.pdst))
        test_passed = True

    except RuntimeError as ex:
        print 'Error occurred: {}'.format(ex)

    return test_passed


def main():
    """Run the test and collect result"""
    if arp_request_test():
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == '__main__':
    main()