def _add_option_list(self, packet: DhcpPacket) -> None: # 'classless_static_route' must be requested before 'router'. packet.AddLine( "parameter_request_list: subnet_mask," "classless_static_route,router," "domain_name_server,domain_name,renewal_time_value," "rebinding_time_value" )
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)
def _generate_base_packet(self) -> DhcpPacket: packet = DhcpPacket() packet.AddLine("op: BOOTREQUEST") packet.AddLine("htype: 1") packet.SetOption("xid", self._xid.to_bytes(4, "big")) # We're the gateway. packet.SetOption("giaddr", self._local_ip.packed) if self._target_addr: packet.SetOption( "relay_agent", bytes((DHCP_SUBOPTION_LINKSEL, DHCP_SUBOPTION_LINKSEL_LEN)) + self._target_addr.packed, ) # Request IP address, etc. for the following client identifier. packet.SetOption("client_identifier", self._client_identifier) # We pretend to be a gateway, so the packet hop count is > 0 here. packet.AddLine("hops: 1") return packet