summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/Dhcpd/DhcpMessage.h
blob: f92e8074f703a104cef6c429169d34eaf44a7ebb (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* $Id: DhcpMessage.h $ */
/** @file
 * DHCP Message and its de/serialization.
 */

/*
 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#ifndef VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h
#define VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include "DhcpdInternal.h"
#include <iprt/net.h>
#include <iprt/cpp/ministring.h>
#include "ClientId.h"
#include "DhcpOptions.h"


/**
 * Base class for internal DHCP client and server message representations.
 */
class DhcpMessage
{
protected:
    uint32_t        m_xid;
    uint16_t        m_flags;

    RTMAC           m_mac;

    RTNETADDRIPV4   m_ciaddr;
    RTNETADDRIPV4   m_yiaddr;
    RTNETADDRIPV4   m_siaddr;
    RTNETADDRIPV4   m_giaddr;

#if 0 /* not currently unused, so avoid wasting time on them for now.  */
    RTCString       m_sname;  /**< @note Not necessarily UTF-8 clean. */
    RTCString       m_file;   /**< @note Not necessarily UTF-8 clean. */
#endif

    OptMessageType  m_optMessageType;

protected:
    DhcpMessage();

public:
    /** @name Accessors
     * @{ */
    uint32_t        xid() const RT_NOEXCEPT                     { return m_xid; }

    uint16_t        flags() const RT_NOEXCEPT                   { return m_flags; }
    bool            broadcast() const RT_NOEXCEPT               { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; }

    const RTMAC     &mac() const RT_NOEXCEPT                    { return m_mac; }

    RTNETADDRIPV4   ciaddr() const RT_NOEXCEPT                  { return m_ciaddr; }
    RTNETADDRIPV4   yiaddr() const RT_NOEXCEPT                  { return m_yiaddr; }
    RTNETADDRIPV4   siaddr() const RT_NOEXCEPT                  { return m_siaddr; }
    RTNETADDRIPV4   giaddr() const RT_NOEXCEPT                  { return m_giaddr; }

    void            setCiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_ciaddr = addr; }
    void            setYiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_yiaddr = addr; }
    void            setSiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_siaddr = addr; }
    void            setGiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT   { m_giaddr = addr; }

    uint8_t         messageType() const RT_NOEXCEPT
    {
        Assert(m_optMessageType.present());
        return m_optMessageType.value();
    }
    /** @} */

    void            dump() const RT_NOEXCEPT;
};


/**
 * Decoded DHCP client message.
 *
 * This is the internal decoded representation of a DHCP message picked up from
 * the wire.
 */
class DhcpClientMessage
    : public DhcpMessage
{
protected:
    rawopts_t       m_rawopts;
    ClientId        m_id;
    bool            m_broadcasted;

public:
    static DhcpClientMessage *parse(bool broadcasted, const void *buf, size_t buflen);

    /** @name Getters
     * @{ */
    bool            broadcasted() const RT_NOEXCEPT             { return m_broadcasted; }
    const rawopts_t &rawopts() const RT_NOEXCEPT                { return m_rawopts; }
    const ClientId &clientId() const RT_NOEXCEPT                { return m_id; }
    /** @} */

    void            dump() const RT_NOEXCEPT;

protected:
    int             i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT;
};



/**
 * DHCP server message for encoding.
 */
class DhcpServerMessage
    : public DhcpMessage
{
protected:
    RTNETADDRIPV4   m_dst;
    OptServerId     m_optServerId;
    optmap_t        m_optmap;

public:
    DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageType, RTNETADDRIPV4 serverAddr);

    /** @name Accessors
     * @{ */
    RTNETADDRIPV4   dst() const RT_NOEXCEPT                     { return m_dst; }
    void            setDst(RTNETADDRIPV4 aDst) RT_NOEXCEPT      { m_dst = aDst; }

    void            maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT;

    void            addOption(DhcpOption *opt);
    void            addOption(const DhcpOption &opt)            { addOption(opt.clone()); }

    void            addOptions(const optmap_t &optmap);
    /** @} */

    int             encode(octets_t &data);
    void            dump() const RT_NOEXCEPT;
};

#endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h */