Пример #1
0
def do_beaconing(args, interfaces=None):
    """Sends out beacons based on the given arguments, and waits for replies.

    :param args: The command-line arguments.
    :param interfaces: The interfaces to send out beacons on.
        Must be the result of `get_all_interfaces_definition()`.
    """
    if args.source is None:
        source_ip = '::'
    else:
        source_ip = args.source
    protocol = BeaconingSocketProtocol(reactor,
                                       process_incoming=True,
                                       debug=True,
                                       interface=source_ip,
                                       port=args.port,
                                       interfaces=interfaces)
    if args.destination is None:
        destination_ip = "::ffff:" + BEACON_IPV4_MULTICAST
    elif ':' not in args.destination:
        destination_ip = "::ffff:" + args.destination
    else:
        destination_ip = args.destination
    if "224.0.0.118" in destination_ip:
        protocol.send_multicast_beacons(interfaces, verbose=args.verbose)
    else:
        log.msg("Sending unicast beacon to '%s'..." % destination_ip)
        beacon = create_beacon_payload("solicitation")
        protocol.send_beacon(beacon, (destination_ip, BEACON_PORT))
    reactor.callLater(args.timeout, lambda: reactor.stop())
    reactor.run()
    return protocol
Пример #2
0
 def test_send_multicast_beacons(self):
     interfaces = {
         "eth0": {
             "enabled": True,
             "links": []
         },
         "eth1": {
             "enabled": True,
             "links": []
         },
         "eth2": {
             "enabled": True,
             "links": []
         },
     }
     self.patch(socket, "if_nametoindex", lambda name: int(name[3:]))
     protocol = BeaconingSocketProtocol(
         Clock(),
         port=0,
         process_incoming=False,
         loopback=True,
         interface="::",
         debug=True,
     )
     self.patch(services, "create_beacon_payload")
     send_mcast_mock = self.patch(protocol, "send_multicast_beacon")
     protocol.send_multicast_beacons(interfaces)
     # beaconing is sent for each interface ID
     self.assertEqual(
         send_mcast_mock.mock_calls,
         [call(0, ANY), call(1, ANY),
          call(2, ANY)],
     )