def __dhcp4_client(self): """ Obtain IPv4 address and default gateway using DHCP """ def __send_dhcp_packet(dhcp_packet_tx): socket.send_to( UdpMetadata( local_ip_address=IPv4Address("0.0.0.0"), local_port=68, remote_ip_address=IPv4Address("255.255.255.255"), remote_port=67, raw_data=dhcp_packet_tx.get_raw_packet(), )) socket = UdpSocket() socket.bind(local_ip_address="0.0.0.0", local_port=68) dhcp_xid = random.randint(0, 0xFFFFFFFF) # Send DHCP Discover __send_dhcp_packet(dhcp_packet_tx=ps_dhcp.DhcpPacket( dhcp_xid=dhcp_xid, dhcp_chaddr=self.mac_unicast, dhcp_msg_type=ps_dhcp.DHCP_DISCOVER, dhcp_param_req_list= b"\x01\x1c\x02\x03\x0f\x06\x77\x0c\x2c\x2f\x1a\x79\x2a", dhcp_host_name="PyTCP", )) self.logger.debug("Sent out DHCP Discover message") # Wait for DHCP Offer if not (packet := socket.receive_from(timeout=5)): self.logger.warning("Timeout waiting for DHCP Offer message") socket.close() return None, None
dhcp_xid=dhcp_xid, dhcp_chaddr=self.mac_unicast, dhcp_msg_type=ps_dhcp.DHCP_DISCOVER, dhcp_param_req_list= b"\x01\x1c\x02\x03\x0f\x06\x77\x0c\x2c\x2f\x1a\x79\x2a", dhcp_host_name="PyTCP", )) self.logger.debug("Sent out DHCP Discover message") # Wait for DHCP Offer if not (packet := socket.receive_from(timeout=5)): self.logger.warning("Timeout waiting for DHCP Offer message") socket.close() return None, None dhcp_packet_rx = ps_dhcp.DhcpPacket(packet.raw_data) if dhcp_packet_rx.dhcp_msg_type != ps_dhcp.DHCP_OFFER: self.logger.warning("Didn't receive DHCP Offer message") socket.close() return None, None dhcp_srv_id = dhcp_packet_rx.dhcp_srv_id dhcp_yiaddr = dhcp_packet_rx.dhcp_yiaddr self.logger.debug( f"ClientUdpDhcp: Received DHCP Offer from {dhcp_packet_rx.dhcp_srv_id}" + f"IP: {dhcp_packet_rx.dhcp_yiaddr}, Mask: {dhcp_packet_rx.dhcp_subnet_mask}, Router: {dhcp_packet_rx.dhcp_router}" + f"DNS: {dhcp_packet_rx.dhcp_dns}, Domain: {dhcp_packet_rx.dhcp_domain_name}" )