Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "DHCPv4.h"
8#include <AK/Debug.h>
9
10ParsedDHCPv4Options DHCPv4Packet::parse_options() const
11{
12 ParsedDHCPv4Options options;
13 for (size_t index = 4; index < DHCPV4_OPTION_FIELD_MAX_LENGTH; ++index) {
14 auto opt_name = *(DHCPOption const*)&m_options[index];
15 switch (opt_name) {
16 case DHCPOption::Pad:
17 continue;
18 case DHCPOption::End:
19 goto escape;
20 default:
21 ++index;
22 auto length = m_options[index];
23 if ((size_t)length > DHCPV4_OPTION_FIELD_MAX_LENGTH - index) {
24 dbgln("Bogus option length {} assuming forgotten END", length);
25 break;
26 }
27 dbgln_if(DHCPV4_DEBUG, "DHCP Option {} with length {}", (u8)opt_name, length);
28 ++index;
29 options.options.set(opt_name, { length, &m_options[index] });
30 index += length - 1;
31 break;
32 }
33 }
34escape:;
35 return options;
36}