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