1 | /* $Id: DhcpMessage.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP Message and its de/serialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h
|
---|
29 | #define VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include "DhcpdInternal.h"
|
---|
35 | #include <iprt/net.h>
|
---|
36 | #include <iprt/cpp/ministring.h>
|
---|
37 | #include "ClientId.h"
|
---|
38 | #include "DhcpOptions.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Base class for internal DHCP client and server message representations.
|
---|
43 | */
|
---|
44 | class DhcpMessage
|
---|
45 | {
|
---|
46 | protected:
|
---|
47 | uint32_t m_xid;
|
---|
48 | uint16_t m_flags;
|
---|
49 |
|
---|
50 | RTMAC m_mac;
|
---|
51 |
|
---|
52 | RTNETADDRIPV4 m_ciaddr;
|
---|
53 | RTNETADDRIPV4 m_yiaddr;
|
---|
54 | RTNETADDRIPV4 m_siaddr;
|
---|
55 | RTNETADDRIPV4 m_giaddr;
|
---|
56 |
|
---|
57 | #if 0 /* not currently unused, so avoid wasting time on them for now. */
|
---|
58 | RTCString m_sname; /**< @note Not necessarily UTF-8 clean. */
|
---|
59 | RTCString m_file; /**< @note Not necessarily UTF-8 clean. */
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | OptMessageType m_optMessageType;
|
---|
63 |
|
---|
64 | protected:
|
---|
65 | DhcpMessage();
|
---|
66 |
|
---|
67 | public:
|
---|
68 | /** @name Accessors
|
---|
69 | * @{ */
|
---|
70 | uint32_t xid() const RT_NOEXCEPT { return m_xid; }
|
---|
71 |
|
---|
72 | uint16_t flags() const RT_NOEXCEPT { return m_flags; }
|
---|
73 | bool broadcast() const RT_NOEXCEPT { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; }
|
---|
74 |
|
---|
75 | const RTMAC &mac() const RT_NOEXCEPT { return m_mac; }
|
---|
76 |
|
---|
77 | RTNETADDRIPV4 ciaddr() const RT_NOEXCEPT { return m_ciaddr; }
|
---|
78 | RTNETADDRIPV4 yiaddr() const RT_NOEXCEPT { return m_yiaddr; }
|
---|
79 | RTNETADDRIPV4 siaddr() const RT_NOEXCEPT { return m_siaddr; }
|
---|
80 | RTNETADDRIPV4 giaddr() const RT_NOEXCEPT { return m_giaddr; }
|
---|
81 |
|
---|
82 | void setCiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_ciaddr = addr; }
|
---|
83 | void setYiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_yiaddr = addr; }
|
---|
84 | void setSiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_siaddr = addr; }
|
---|
85 | void setGiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_giaddr = addr; }
|
---|
86 |
|
---|
87 | uint8_t messageType() const RT_NOEXCEPT
|
---|
88 | {
|
---|
89 | Assert(m_optMessageType.present());
|
---|
90 | return m_optMessageType.value();
|
---|
91 | }
|
---|
92 | /** @} */
|
---|
93 |
|
---|
94 | void dump() const RT_NOEXCEPT;
|
---|
95 | };
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Decoded DHCP client message.
|
---|
100 | *
|
---|
101 | * This is the internal decoded representation of a DHCP message picked up from
|
---|
102 | * the wire.
|
---|
103 | */
|
---|
104 | class DhcpClientMessage
|
---|
105 | : public DhcpMessage
|
---|
106 | {
|
---|
107 | protected:
|
---|
108 | rawopts_t m_rawopts;
|
---|
109 | ClientId m_id;
|
---|
110 | bool m_broadcasted;
|
---|
111 |
|
---|
112 | public:
|
---|
113 | static DhcpClientMessage *parse(bool broadcasted, const void *buf, size_t buflen);
|
---|
114 |
|
---|
115 | /** @name Getters
|
---|
116 | * @{ */
|
---|
117 | bool broadcasted() const RT_NOEXCEPT { return m_broadcasted; }
|
---|
118 | const rawopts_t &rawopts() const RT_NOEXCEPT { return m_rawopts; }
|
---|
119 | const ClientId &clientId() const RT_NOEXCEPT { return m_id; }
|
---|
120 | /** @} */
|
---|
121 |
|
---|
122 | void dump() const RT_NOEXCEPT;
|
---|
123 |
|
---|
124 | protected:
|
---|
125 | int i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT;
|
---|
126 | };
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * DHCP server message for encoding.
|
---|
132 | */
|
---|
133 | class DhcpServerMessage
|
---|
134 | : public DhcpMessage
|
---|
135 | {
|
---|
136 | protected:
|
---|
137 | RTNETADDRIPV4 m_dst;
|
---|
138 | OptServerId m_optServerId;
|
---|
139 | optmap_t m_optmap;
|
---|
140 |
|
---|
141 | public:
|
---|
142 | DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageType, RTNETADDRIPV4 serverAddr);
|
---|
143 |
|
---|
144 | /** @name Accessors
|
---|
145 | * @{ */
|
---|
146 | RTNETADDRIPV4 dst() const RT_NOEXCEPT { return m_dst; }
|
---|
147 | void setDst(RTNETADDRIPV4 aDst) RT_NOEXCEPT { m_dst = aDst; }
|
---|
148 |
|
---|
149 | void maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT;
|
---|
150 |
|
---|
151 | void addOption(DhcpOption *opt);
|
---|
152 | void addOption(const DhcpOption &opt) { addOption(opt.clone()); }
|
---|
153 |
|
---|
154 | void addOptions(const optmap_t &optmap);
|
---|
155 | /** @} */
|
---|
156 |
|
---|
157 | int encode(octets_t &data);
|
---|
158 | void dump() const RT_NOEXCEPT;
|
---|
159 | };
|
---|
160 |
|
---|
161 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpMessage_h */
|
---|