NetBurner 3.5.0
PDF Version
 
dhcpv6_internal.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef __DHCPV6_INTERNAL_H
6#define __DHCPV6_INTERNAL_H
7
8#include <constants.h>
9#include <nettypes.h>
10#include <udp.h>
11//#include <ipv6/ipv6_interface.h>
12#include <ipv6/dhcpv6_const.h>
13#include <ipv6/dhcpv6_msg.h>
14
15#define CLIENT_LONG_SERVID 18
16
17//#define DHCPv6_DEBUG
18
19#ifdef DHCPv6_DEBUG
20#define DHCPv6_DBSHOW(msg) msg.ShowMsg()
21#define DHCPv6_DBIPRINTF(...) \
22 { \
23 iprintf("\n[T:%d-%ld]%s:%s:%d::", OSTaskID(), TimeTick, __FILE__, __FUNCTION__, __LINE__); \
24 iprintf(__VA_ARGS__); \
25 }
26#else
27#define DHCPv6_DBSHOW(msg) ((void)0)
28#define DHCPv6_DBIPRINTF(...) ((void)0)
29#endif
30
31class IPv6Interface;
32struct IPV6_DHCPD;
33
34namespace NB
35{
36namespace V6
37{
38namespace DHCPv6
39{
40class DHCPv6Message
41{
42 private:
43 UDPPacket Msg;
44 DHCP_Message *dhcp_msg;
45 uint16_t msgLen;
46 bool txNRx;
47
48 uint32_t opts_present[4]; // 128-bit bitfield for option numbers
49
50 public:
51 static const IPADDR6 ALL_RELAY_AND_SERVERS;
52 static const IPADDR6 ALL_SERVERS;
53
54 DHCPv6Message();
55 DHCPv6Message(DHCP_Message *msg, uint16_t len);
56 DHCPv6Message(UDPPacket *msg);
57 ~DHCPv6Message();
58
59#ifdef DHCPv6_DEBUG
60 void ShowMsg(); // Display DHCPv6 message for debug
61#endif
62
63 PoolPtr ReleaseBuffer();
64
65 inline void ClrMsg()
66 {
67 Msg.ResetData();
68 dhcp_msg = (DHCP_Message *)Msg.GetDataBuffer();
69 txNRx = true;
70 }
71 inline void SetTypeAndXid(MessageType type, uint32_t xid)
72 {
73 ClrMsg();
74 xid &= 0xFFFFFF; // 24 bit mask
75 xid |= type << 24;
76 beuint32_t tXid = xid; // needed for Little Endian hosts
77 Msg.AddData((uint8_t *)&tXid, 4);
78 msgLen += 4;
79 }
80
81 void AddOption(Option &opt, uint16_t overrideLen = 0);
82
83 void AddClientID();
84 void AddORO(Option_ID ids[], uint16_t len, int ifNum);
85 void AddElapsedTime(uint16_t elapsedTime);
86 void AddIA_NA(uint32_t IA_ID, uint32_t T1, uint32_t T2, uint16_t optLen = 0);
87 void AddFQDN(const char *name);
88
89 inline MessageType GetType() { return (MessageType)((dhcp_msg->typeAndXid) >> 24); }
90 inline uint32_t GetXid() { return (dhcp_msg->typeAndXid) & 0xFFFFFF; }
91
92 Option *GetOption(Option_ID optID, Option *lastOpt = NULL);
93 inline IPADDR6 GetSourceAddress() { return Msg.GetSourceAddress(); }
94
95 void SendMsg(const IPADDR6 &ServerIP = ALL_RELAY_AND_SERVERS, int via_interface = -1);
96
97 static void SendSolicit(uint32_t ifNum, uint32_t xid, uint32_t timeElapsed);
98 static void SendRequest(uint32_t ifNum, uint32_t xid, uint32_t timeElapsed, Opt::ClientServerID *serverID, Opt::IA_NA *ia_na);
99 static void SendInfoReq(uint32_t ifNum, uint32_t xid);
100};
101
102class DHCPClient
103{
104 public:
105 enum ClientState
106 {
107 STATE_SOLICIT_START = 0,
108 STATE_SOLICIT_DLYED = 1,
109 STATE_SOLICIT_MSG_REC = 2,
110 STATE_REQUEST_SENDING = 3,
111 STATE_CONFIRMED = 4,
112 STATE_RENEWING = 5,
113 STATE_REBINDING = 6,
114 STATE_INFO_REQ_SENDING = 7,
115 STATE_INFO_REP_REC = 8,
116 STATE_STOPPED = 9
117 };
118
119 private:
120 ClientState state;
121 IPv6Interface *myInterface;
122 uint32_t nextActionTick;
123 uint32_t currentDelay;
124 uint32_t retransmitCount;
125 uint32_t startTick;
126 uint32_t xid;
127 uint8_t maxPrefSeen;
128 bool hasDns;
129 IPV6_DHCPD *activeDHCPD;
130 uint8_t activeServerID[CLIENT_LONG_SERVID]; // Space to contain the ServerID of the leasing server
131
132 PoolPtr actingMessage; // DHCP advert/reply to act on
133
134 void ClearInfo();
135 inline void SetState(ClientState newState)
136 {
137 DHCPv6_DBIPRINTF("Setting state: %s", GetStateString(newState));
138 state = newState;
139 }
140 static char const *GetStateString(ClientState state);
141
142 void SetCurrentDelay(uint32_t delayBase);
143 void IncrementCurrentDelay(uint32_t maxDelay);
144 uint32_t GetNewXid();
145
146 void ProcessReply(DHCPv6Message &msg);
147 void ProcessReply_Info(DHCPv6Message &msg);
148 void ProcessReply_Solicit(DHCPv6Message &msg, bool needServerMatch);
149
150 void ProcessReply_DNSOpt(IPV6_DHCPD *server, Opt::DNS_Servers *dnsOpt, uint32_t time);
151
152 void ProcessAdvert(DHCPv6Message &msg);
153 void ProcessReconfig(DHCPv6Message &msg);
154
155 void SendSolicit();
156 void SendRequest();
157 void SendRenewRebind(bool renew);
158
159 public:
160 DHCPClient(IPv6Interface *intf);
161 inline uint32_t GetNextActionTick() { return nextActionTick; }
162 void Tick();
163 void ProcessMsg(DHCPv6Message &msg);
164 void StartInfoReq();
165 void StartSolicit();
166 inline ClientState GetState() { return state; }
167
168 void ShowInfo();
169
170 static void ProcessDHCPAvail(IPv6Interface *intf, uint8_t raFlags);
171 static void ProcessDHCPMsg(UDPPacket *pkt);
172};
173
174// these are in the NB::V6::DHCPv6 namespace
175extern Option_ID *(*pAddOROCB)(int ifNum);
176extern void (*pRequestOptionCB)(int ifNum, DHCPv6Message &msg);
177extern void (*pReplyCB)(int ifNum, DHCPv6Message &msg);
178
179} // namespace DHCPv6
180} // namespace V6
181} // namespace NB
182
183#endif /* ----- #ifndef __DHCPV6_INTERNAL_H ----- */
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
UDP Packet Class.
Definition udp.h:81
IPADDR GetSourceAddress(void)
Get the source IP address a UDP Packet object.
Definition udp.h:285
void ResetData(void)
Set the data size of a UDP Packet object to 0.
void AddData(puint8_t pData, uint16_t len)
Add a number of data bytes to a UDP Packet object.
puint8_t GetDataBuffer(bool bReAllocateIfNeeded=false)
Get a pointer to the UDP Packet object's data buffer.
@ Option
Option.
Definition nbWifiConstants.h:385
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
NetBurner User Datagram Protocol Header File.