• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6
7from autotest_lib.server.hosts import ssh_host
8
9RA_SCRIPT = 'sendra.py'
10SCAPY = 'scapy-2.2.0.tar.gz'
11SCAPY_INSTALL_COMMAND = 'sudo python setup.py install'
12PROC_NET_SNMP6 = '/proc/net/snmp6'
13MULTICAST_ADDR = '33:33:00:00:00:01'
14IFACE = 'managed0'
15LIFETIME = 180
16
17
18class IPutils(object):
19
20    def __init__(self, host):
21        """Initializes an IP utility interface.
22
23        @param host: Router host object.
24
25        """
26        self.host = host
27        self.install_path = self.host.run('mktemp -d').stdout.rstrip()
28
29
30    def install_scapy(self):
31        """Installs scapy on the target device. Scapy and all related files and
32        scripts will be installed in a temp directory under /tmp.
33
34        """
35        scapy = os.path.join(self.install_path, SCAPY)
36        ap_sshhost = ssh_host.SSHHost(hostname=self.host.hostname)
37        current_dir = os.path.dirname(os.path.realpath(__file__))
38        send_ra_script = os.path.join(current_dir, RA_SCRIPT)
39        send_scapy = os.path.join(current_dir, SCAPY)
40        ap_sshhost.send_file(send_scapy, self.install_path)
41        ap_sshhost.send_file(send_ra_script, self.install_path)
42
43        self.host.run('tar -xvf %s -C %s' % (scapy, self.install_path))
44        self.host.run('cd %s; %s' % (self.install_path, SCAPY_INSTALL_COMMAND))
45
46
47    def cleanup_scapy(self):
48        """Remove all scapy related files and scripts from device.
49
50        @param host: Router host object.
51
52        """
53        self.host.run('rm -rf %s' % self.install_path)
54
55
56    def send_ra(self, mac=MULTICAST_ADDR, interval=1, count=None, iface=IFACE,
57                lifetime=LIFETIME):
58        """Invoke scapy and send RA to the device.
59
60        @param host: Router host object.
61        @param mac: string HWAddr/MAC address to send the packets to.
62        @param interval: int Time to sleep between consecutive packets.
63        @param count: int Number of packets to be sent.
64        @param iface: string of the WiFi interface to use for sending packets.
65        @param lifetime: int original RA's router lifetime in seconds.
66
67        """
68        scapy_command = os.path.join(self.install_path, RA_SCRIPT)
69        options = ' -m %s -i %d -c %d -l %d -in %s' %(mac, interval, count,
70                                                      lifetime, iface)
71        self.host.run(scapy_command + options)
72
73
74    def get_icmp6intype134(self, host):
75        """Read the value of Icmp6InType134 and return integer.
76
77        @param host: DUT host object.
78
79        @returns integer value >0 if grep is successful; 0 otherwise.
80
81        """
82        ra_count_str = host.run(
83                       'grep Icmp6InType134 %s || true' % PROC_NET_SNMP6).stdout
84        if ra_count_str:
85            return int(ra_count_str.split()[1])
86        # If grep failed it means that there is no entry for Icmp6InType134 in file.
87        return 0
88