summaryrefslogtreecommitdiff
path: root/Userland/Services/DHCPClient/DHCPv4.cpp
blob: 2806cf5bed6de7a3f0f61bece3ace07dda31bfb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "DHCPv4.h"
#include <AK/Debug.h>

ParsedDHCPv4Options DHCPv4Packet::parse_options() const
{
    ParsedDHCPv4Options options;
    for (size_t index = 4; index < DHCPV4_OPTION_FIELD_MAX_LENGTH; ++index) {
        auto opt_name = *(const DHCPOption*)&m_options[index];
        switch (opt_name) {
        case DHCPOption::Pad:
            continue;
        case DHCPOption::End:
            goto escape;
        default:
            ++index;
            auto length = m_options[index];
            if ((size_t)length > DHCPV4_OPTION_FIELD_MAX_LENGTH - index) {
                dbgln("Bogus option length {} assuming forgotten END", length);
                break;
            }
            dbgln_if(DHCPV4_DEBUG, "DHCP Option {} with length {}", (u8)opt_name, length);
            ++index;
            options.options.set(opt_name, { length, &m_options[index] });
            index += length - 1;
            break;
        }
    }
escape:;
    return options;
}