示例#1
0
    def test_name_to_index(self):
        # This test relies on the fact that ifconfig returns interfaces in
        # sequential order according to their interface index.

        ifconfig = subprocess.Popen(['/sbin/ifconfig'], stdout=subprocess.PIPE)
        expected_if_idx = 0
        for line in ifconfig.stdout.readlines():
            m = re.search('^(\w+\d):', line)
            if m:
                expected_if_idx += 1
                if_name = m.group(1)
                if_idx = name_to_index(if_name)
                self.assertNotEquals(
                    if_idx,
                    0,
                    msg='Error looking up interface index for %s' % (if_name))
                self.assertEquals(
                    if_idx,
                    expected_if_idx,
                    msg='Interface index for %s was %d, expected %d' %
                    (if_name, if_idx, expected_if_idx))
        self.assertNotEquals(expected_if_idx,
                             0,
                             msg="Couldn't find any interfaces to test")

        self.assertRaises(aplib.oserrors.ENXIO, name_to_index,
                          'invalidinterface')
示例#2
0
文件: icmp6.py 项目: bobveznat/aplib
def send_na(ip, mac, if_name):
    # Do we need the mac given that we have if_name?
    # ifmedia.ethernet_mac('em0') -> '00:13:72:66:e2:42'
    # maybe an optional arg so that it can be overridden (spoofed?)

    icmp_packet = NDNeighborAdvert(ip, mac).pack()

    s = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6)

    # The hoplimit must be 255 so that the receiver can verify that the
    # packet has not been forwarded. RFC 3542 sec 6.3
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)

    # Set the interface to use for multicast with this socket
    if_idx = name_to_index(if_name)
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, if_idx)

    # Tell the OS not to loop this packet back to ourselves.
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)

    s.sendto(icmp_packet, ('ff02::1', 0))
示例#3
0
    def test_name_to_index(self):
        # This test relies on the fact that ifconfig returns interfaces in
        # sequential order according to their interface index.

        ifconfig = subprocess.Popen(['/sbin/ifconfig'], stdout=subprocess.PIPE)
        expected_if_idx = 0
        for line in ifconfig.stdout.readlines():
            m = re.search('^(\w+\d):', line)
            if m:
                expected_if_idx += 1
                if_name = m.group(1)
                if_idx = name_to_index(if_name)
                self.assertNotEquals(if_idx, 0,
                    msg='Error looking up interface index for %s' %(if_name))
                self.assertEquals(if_idx, expected_if_idx,
                    msg='Interface index for %s was %d, expected %d' %(
                        if_name, if_idx, expected_if_idx))
        self.assertNotEquals(expected_if_idx, 0,
            msg="Couldn't find any interfaces to test")

        self.assertRaises(aplib.oserrors.ENXIO, name_to_index, 'invalidinterface')