1 | /* $Id: DhcpMessage.h 70836 2018-01-31 14:55:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP Message and its de/serialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2018 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 | #ifndef _DHCP_MESSAGE_H_
|
---|
18 | #define _DHCP_MESSAGE_H_
|
---|
19 |
|
---|
20 | #include "Defs.h"
|
---|
21 | #include <iprt/net.h>
|
---|
22 | #include <string>
|
---|
23 | #include "ClientId.h"
|
---|
24 | #include "DhcpOptions.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | /* move to <iptr/net.h>? */
|
---|
28 | #define DHCP_OPTION_OVERLOAD_MASK 0x3
|
---|
29 | #define DHCP_OPTION_OVERLOAD_FILE 0x1
|
---|
30 | #define DHCP_OPTION_OVERLOAD_SNAME 0x2
|
---|
31 |
|
---|
32 |
|
---|
33 | class DhcpMessage
|
---|
34 | {
|
---|
35 | protected:
|
---|
36 | uint32_t m_xid;
|
---|
37 | uint16_t m_flags;
|
---|
38 |
|
---|
39 | RTMAC m_mac;
|
---|
40 |
|
---|
41 | RTNETADDRIPV4 m_ciaddr;
|
---|
42 | RTNETADDRIPV4 m_yiaddr;
|
---|
43 | RTNETADDRIPV4 m_siaddr;
|
---|
44 | RTNETADDRIPV4 m_giaddr;
|
---|
45 |
|
---|
46 | std::string m_sname;
|
---|
47 | std::string m_file;
|
---|
48 |
|
---|
49 | OptMessageType m_optMessageType;
|
---|
50 |
|
---|
51 | public:
|
---|
52 | DhcpMessage();
|
---|
53 |
|
---|
54 |
|
---|
55 | uint32_t xid() const { return m_xid; }
|
---|
56 |
|
---|
57 | uint16_t flags() const { return m_flags; }
|
---|
58 | bool broadcast() const { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; }
|
---|
59 |
|
---|
60 | const RTMAC &mac() const { return m_mac; }
|
---|
61 |
|
---|
62 | RTNETADDRIPV4 ciaddr() const { return m_ciaddr; }
|
---|
63 | RTNETADDRIPV4 yiaddr() const { return m_yiaddr; }
|
---|
64 | RTNETADDRIPV4 siaddr() const { return m_siaddr; }
|
---|
65 | RTNETADDRIPV4 giaddr() const { return m_giaddr; }
|
---|
66 |
|
---|
67 | void setCiaddr(RTNETADDRIPV4 addr) { m_ciaddr = addr; }
|
---|
68 | void setYiaddr(RTNETADDRIPV4 addr) { m_yiaddr = addr; }
|
---|
69 | void setSiaddr(RTNETADDRIPV4 addr) { m_siaddr = addr; }
|
---|
70 | void setGiaddr(RTNETADDRIPV4 addr) { m_giaddr = addr; }
|
---|
71 |
|
---|
72 | uint8_t messageType() const
|
---|
73 | {
|
---|
74 | Assert(m_optMessageType.present());
|
---|
75 | return m_optMessageType.value();
|
---|
76 | }
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | class DhcpClientMessage
|
---|
81 | : public DhcpMessage
|
---|
82 | {
|
---|
83 | protected:
|
---|
84 | rawopts_t m_rawopts;
|
---|
85 | ClientId m_id;
|
---|
86 | bool m_broadcasted;
|
---|
87 |
|
---|
88 | public:
|
---|
89 | static DhcpClientMessage *parse(bool broadcasted, const void *buf, size_t buflen);
|
---|
90 |
|
---|
91 | bool broadcasted() const { return m_broadcasted; }
|
---|
92 |
|
---|
93 | const rawopts_t &rawopts() const { return m_rawopts; }
|
---|
94 | const ClientId &clientId() const { return m_id; }
|
---|
95 |
|
---|
96 | void dump() const;
|
---|
97 |
|
---|
98 | protected:
|
---|
99 | int parseOptions(const void *buf, size_t buflen);
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 |
|
---|
104 | class DhcpServerMessage
|
---|
105 | : public DhcpMessage
|
---|
106 | {
|
---|
107 | protected:
|
---|
108 | RTNETADDRIPV4 m_dst;
|
---|
109 |
|
---|
110 | OptServerId m_optServerId;
|
---|
111 |
|
---|
112 | optmap_t m_optmap;
|
---|
113 |
|
---|
114 | public:
|
---|
115 | DhcpServerMessage(const DhcpClientMessage &req,
|
---|
116 | uint8_t messageType, RTNETADDRIPV4 serverAddr);
|
---|
117 |
|
---|
118 | RTNETADDRIPV4 dst() const { return m_dst; }
|
---|
119 | void setDst(RTNETADDRIPV4 aDst) { m_dst = aDst; }
|
---|
120 |
|
---|
121 | void maybeUnicast(const DhcpClientMessage &req);
|
---|
122 |
|
---|
123 | void addOption(DhcpOption *opt);
|
---|
124 | void addOption(const DhcpOption &opt)
|
---|
125 | {
|
---|
126 | addOption(opt.clone());
|
---|
127 | }
|
---|
128 |
|
---|
129 | void addOptions(const optmap_t &optmap);
|
---|
130 |
|
---|
131 | int encode(octets_t &data);
|
---|
132 | };
|
---|
133 |
|
---|
134 | #endif /* _DHCP_MESSAGE_H_ */
|
---|