aboutsummaryrefslogtreecommitdiff
path: root/lib/dhcp/dhcp.cc
blob: 1e5f7850975453e5726114e2d4f8d8307b799424 (plain) (blame)
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) 2022 Johannes Stoelp

#include "dhcp.h"
#include "utils.h"

std::optional<struct option_view> get_option(const u8* opt, usize len, dhcp_option search_tag) {
    const u8* end = opt + len;

    while (opt < end) {
        // Get tag of current option.
        dhcp_option tag = from_raw<dhcp_option>(*opt++);

        if (tag == dhcp_option::END) {
            break;
        }

        // Extract length of current option.
        usize len = *opt++;

        if (tag == search_tag) {
            if (static_cast<usize>(end - opt) < len) {
                // If length is malformed.
                return std::nullopt;
            } else {
                // Option found.
                return {{opt, len}};
            }
        }

        // Advance option iterator to beginning of next option.
        opt = opt + len;
    }

    // Option not found.
    return std::nullopt;
}