예제 #1
0
def test_handle_dhcp_handshake(dhcprequest) -> None:
    success_mock = Mock()
    failure_mock = Mock()
    req = dhcprequest(
        cls=DhcpAddressInitialRequest,
        success_clb=success_mock,
        failure_clb=failure_mock,
        requestor=Mock(),
    )

    offer = DhcpPacket()
    offer.source_address = ("123.123.123.123", 67)
    req.handle_dhcp_offer(offer)

    packet = DhcpPacket()
    packet.AddLine("op: 2")
    packet.AddLine("domain_name: scc.kit.edu")
    packet.AddLine("yiaddr: 1.2.3.4")
    packet.AddLine("router: 2.3.4.5")
    packet.AddLine("subnet_mask: 255.255.255.0")
    packet.AddLine("domain_name_server: 1.0.0.0,2.0.0.0,3.0.0.0")
    packet.SetOption("classless_static_route",
                     bytes([0, 4, 0, 0, 0, 16, 10, 12, 5, 0, 0, 0]))
    packet.AddLine("ip_address_lease_time: 9000")
    packet.AddLine("renewal_time_value: 300")
    packet.AddLine("ip_address_lease_time: 9000")
    packet.AddLine("rebinding_time_value: 7000")
    packet.source_address = ("123.123.123.123", 67)

    print(packet.str())

    expected_res = {
        "dns": ["1.0.0.0", "2.0.0.0", "3.0.0.0"],
        "domain": "scc.kit.edu",
        "ip_address": "1.2.3.4",
        "gateway": "4.0.0.0",
        "static_routes": [("10.12.0.0", "255.255.0.0", "5.0.0.0")],
        "subnet_mask": "255.255.255.0",
        "lease_timeout": 1580009000,
        "renewal_timeout": 1580000300,
        "rebinding_timeout": 1580007000,
    }

    req.handle_dhcp_ack(packet)
    success_mock.assert_called_once_with(expected_res)
예제 #2
0
    def recieve_next_dhcp_packet(self, timeout=30):
        print "Here"
        while True:
            data = select.select([self._socket], [], [], timeout)[0]

            if not len(data):
                return None

            packet_data, source_address = self._socket.recvfrom(2048)
            if packet_data:
                packet = DhcpPacket()
                packet.source_address = source_address
                packet.DecodePacket(packet_data)
                return packet
예제 #3
0
    def handle_socket(self) -> None:
        """Retrieves the next, waiting DHCP packet, parses it and calls the
        handler of the associated request.
        """
        try:
            data, source_address = self._socket.recvfrom(2048)
            if len(data) == 0:
                self._log.warning("unexpectedly received EOF!")
                return
            packet = DhcpPacket()
            packet.source_address = source_address
            packet.DecodePacket(data)

            if (not packet.IsDhcpPacket()) or (
                not packet.IsOption("dhcp_message_type")
            ):
                self._log.debug("Ignoring invalid packet")
                return

            dhcp_type = packet.GetOption("dhcp_message_type")[0]
            if dhcp_type not in self._DHCP_TYPE_HANDLERS:
                self._log.debug("Ignoring packet of unexpected DHCP type %d", dhcp_type)
                return

            xid = int.from_bytes(packet.GetOption('xid'), "big")
            if xid not in self._requests:
                self._log.debug("Ignoring answer with xid %r", xid)
                return

            request = self._requests[xid]
            clb_name = self._DHCP_TYPE_HANDLERS[dhcp_type]
            if not hasattr(request, clb_name):
                self._log.error("request has no callback '%s'", clb_name)
                return

            clb = getattr(request, clb_name)
            clb(packet)
        except Exception:
            self._log.exception('handling DHCP packet failed')