diff options
author | Patrick Meyer <git@the-space.agency> | 2022-05-02 00:54:27 +0200 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-05-03 12:37:51 +0430 |
commit | 5510b98dc8dda6910a892596ba380b60120efb04 (patch) | |
tree | ffe73adabb5a3322e8a547a0ab2a943c1dfc55b6 /Userland/Services/DHCPClient/DHCPv4Client.cpp | |
parent | 7b76bc2b4983b5fc0c648c5ae0bd9e3f402518fc (diff) | |
download | serenity-5510b98dc8dda6910a892596ba380b60120efb04.zip |
DHCPClient: Don't crash if DHCP options contain no gateway
Fixes #13879
Diffstat (limited to 'Userland/Services/DHCPClient/DHCPv4Client.cpp')
-rw-r--r-- | Userland/Services/DHCPClient/DHCPv4Client.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Userland/Services/DHCPClient/DHCPv4Client.cpp b/Userland/Services/DHCPClient/DHCPv4Client.cpp index c579663fde..24a25ab127 100644 --- a/Userland/Services/DHCPClient/DHCPv4Client.cpp +++ b/Userland/Services/DHCPClient/DHCPv4Client.cpp @@ -67,7 +67,7 @@ static bool send(InterfaceDescriptor const& iface, DHCPv4Packet const& packet, C return true; } -static void set_params(InterfaceDescriptor const& iface, IPv4Address const& ipv4_addr, IPv4Address const& netmask, IPv4Address const& gateway) +static void set_params(InterfaceDescriptor const& iface, IPv4Address const& ipv4_addr, IPv4Address const& netmask, Optional<IPv4Address> const& gateway) { int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (fd < 0) { @@ -99,13 +99,16 @@ static void set_params(InterfaceDescriptor const& iface, IPv4Address const& ipv4 dbgln("ERROR: ioctl(SIOCSIFNETMASK) :: {}", strerror(errno)); } + if (!gateway.has_value()) + return; + // set the default gateway struct rtentry rt; memset(&rt, 0, sizeof(rt)); rt.rt_dev = const_cast<char*>(iface.ifname.characters()); rt.rt_gateway.sa_family = AF_INET; - ((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = gateway.to_in_addr_t(); + ((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = gateway.value().to_in_addr_t(); rt.rt_flags = RTF_UP | RTF_GATEWAY; if (ioctl(fd, SIOCADDRT, &rt) < 0) { @@ -251,7 +254,12 @@ void DHCPv4Client::handle_ack(DHCPv4Packet const& packet, ParsedDHCPv4Options co dhcp_discover(interface); }, this); - set_params(transaction->interface, new_ip, options.get<IPv4Address>(DHCPOption::SubnetMask).value(), options.get_many<IPv4Address>(DHCPOption::Router, 1).first()); + + Optional<IPv4Address> gateway; + if (auto routers = options.get_many<IPv4Address>(DHCPOption::Router, 1); !routers.is_empty()) + gateway = routers.first(); + + set_params(transaction->interface, new_ip, options.get<IPv4Address>(DHCPOption::SubnetMask).value(), gateway); } void DHCPv4Client::handle_nak(DHCPv4Packet const& packet, ParsedDHCPv4Options const& options) |