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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
/* $Id: Config.h $ */
/** @file
* Config.h
*/
/*
* Copyright (C) 2013-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef VBOX_INCLUDED_SRC_DHCP_Config_h
#define VBOX_INCLUDED_SRC_DHCP_Config_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/asm-math.h>
#include <iprt/cpp/utils.h>
#include <VBox/com/ptr.h>
#include <VBox/com/string.h>
#include <VBox/com/VirtualBox.h>
#include "../NetLib/cpp/utils.h"
class RawOption
{
public:
RawOption()
{
/** @todo r=bird: this is crazy. */
RT_ZERO(*this);
}
uint8_t u8OptId;
uint8_t cbRawOpt;
uint8_t au8RawOpt[255];
};
class ClientData;
class Client;
class Lease;
class BaseConfigEntity;
class NetworkConfigEntity;
class HostConfigEntity;
class ClientMatchCriteria;
class ConfigurationManager;
/*
* it's a basic representation of
* of out undestanding what client is
* XXX: Client might sends Option 61 (RFC2132 9.14 "Client-identifier") signalling
* that we may identify it in special way
*
* XXX: Client might send Option 60 (RFC2132 9.13 "Vendor class undentifier")
* in response it's expected server sends Option 43 (RFC2132 8.4. "Vendor Specific Information")
*/
class Client
{
friend class Lease;
friend class ConfigurationManager;
public:
Client();
void initWithMac(const RTMAC& mac);
bool operator== (const RTMAC& mac) const;
const RTMAC& getMacAddress() const;
/** Dumps client query */
void dump();
Lease lease();
const Lease lease() const;
public:
static const Client NullClient;
private:
Client(ClientData *);
SharedPtr<ClientData> m;
};
bool operator== (const Lease&, const Lease&);
bool operator!= (const Lease&, const Lease&);
bool operator< (const Lease&, const Lease&);
typedef std::map<uint8_t, RawOption> MapOptionId2RawOption;
typedef MapOptionId2RawOption::iterator MapOptionId2RawOptionIterator;
typedef MapOptionId2RawOption::const_iterator MapOptionId2RawOptionConstIterator;
typedef MapOptionId2RawOption::value_type MapOptionId2RawOptionValue;
namespace xml {
class ElementNode;
}
class Lease
{
friend class Client;
friend bool operator== (const Lease&, const Lease&);
//friend int ConfigurationManager::loadFromFile(const std::string&);
friend class ConfigurationManager;
public:
Lease();
Lease(const Client&);
bool isExpired() const;
void expire();
/* Depending on phase *Expiration and phaseStart initialize different values. */
void bindingPhase(bool);
void phaseStart(uint64_t u64Start);
bool isInBindingPhase() const;
/* returns 0 if in binding state */
uint64_t issued() const;
void setExpiration(uint32_t);
uint32_t getExpiration() const;
RTNETADDRIPV4 getAddress() const;
void setAddress(RTNETADDRIPV4);
const NetworkConfigEntity *getConfig() const;
void setConfig(NetworkConfigEntity *);
const MapOptionId2RawOption& options() const;
bool toXML(xml::ElementNode *) const;
bool fromXML(const xml::ElementNode *);
public:
static const Lease NullLease;
private:
Lease(ClientData *);
SharedPtr<ClientData> m;
};
typedef std::vector<Client> VecClient;
typedef VecClient::iterator VecClientIterator;
typedef VecClient::const_iterator VecClientConstIterator;
typedef std::vector<RTMAC> MacAddressContainer;
typedef MacAddressContainer::iterator MacAddressIterator;
typedef std::vector<RTNETADDRIPV4> Ipv4AddressContainer;
typedef Ipv4AddressContainer::iterator Ipv4AddressIterator;
typedef Ipv4AddressContainer::const_iterator Ipv4AddressConstIterator;
typedef std::map<Lease, RTNETADDRIPV4> MapLease2Ip4Address;
typedef MapLease2Ip4Address::iterator MapLease2Ip4AddressIterator;
typedef MapLease2Ip4Address::const_iterator MapLease2Ip4AddressConstIterator;
typedef MapLease2Ip4Address::value_type MapLease2Ip4AddressPair;
/**
*
*/
class ClientMatchCriteria
{
public:
virtual bool check(const Client&) const {return false;};
};
class ORClientMatchCriteria: ClientMatchCriteria
{
ClientMatchCriteria* m_left;
ClientMatchCriteria* m_right;
ORClientMatchCriteria(ClientMatchCriteria *left, ClientMatchCriteria *right)
{
m_left = left;
m_right = right;
}
virtual bool check(const Client& client) const
{
return (m_left->check(client) || m_right->check(client));
}
};
class ANDClientMatchCriteria: ClientMatchCriteria
{
public:
ANDClientMatchCriteria(ClientMatchCriteria *left, ClientMatchCriteria *right)
{
m_left = left;
m_right = right;
}
virtual bool check(const Client& client) const
{
return (m_left->check(client) && m_right->check(client));
}
private:
ClientMatchCriteria* m_left;
ClientMatchCriteria* m_right;
};
class AnyClientMatchCriteria: public ClientMatchCriteria
{
public:
virtual bool check(const Client&) const
{
return true;
}
};
class MACClientMatchCriteria: public ClientMatchCriteria
{
public:
MACClientMatchCriteria(const RTMAC& mac):m_mac(mac){}
virtual bool check(const Client& client) const;
private:
RTMAC m_mac;
};
#if 0
/* XXX: Later */
class VmSlotClientMatchCriteria: public ClientMatchCriteria
{
str::string VmName;
uint8_t u8Slot;
virtual bool check(const Client& client)
{
return ( client.VmName == VmName
&& ( u8Slot == (uint8_t)~0 /* any */
|| client.u8Slot == u8Slot));
}
};
#endif
/* Option 60 */
class ClassClientMatchCriteria: ClientMatchCriteria{};
/* Option 61 */
class ClientIdentifierMatchCriteria: ClientMatchCriteria{};
class BaseConfigEntity
{
public:
BaseConfigEntity(const ClientMatchCriteria *criteria = NULL,
int matchingLevel = 0)
: m_criteria(criteria),
m_MatchLevel(matchingLevel){};
virtual ~BaseConfigEntity(){};
/* XXX */
int add(BaseConfigEntity *cfg)
{
m_children.push_back(cfg);
return 0;
}
/* Should return how strong matching */
virtual int match(Client& client, BaseConfigEntity **cfg);
virtual uint32_t expirationPeriod() const = 0;
protected:
const ClientMatchCriteria *m_criteria;
int m_MatchLevel;
std::vector<BaseConfigEntity *> m_children;
};
class NullConfigEntity: public BaseConfigEntity
{
public:
NullConfigEntity(){}
virtual ~NullConfigEntity(){}
int add(BaseConfigEntity *) const { return 0;}
virtual uint32_t expirationPeriod() const {return 0;}
};
class ConfigEntity: public BaseConfigEntity
{
public:
/* range */
/* match conditions */
ConfigEntity(std::string& name,
const BaseConfigEntity *cfg,
const ClientMatchCriteria *criteria,
int matchingLevel = 0):
BaseConfigEntity(criteria, matchingLevel),
m_name(name),
m_parentCfg(cfg),
m_u32ExpirationPeriod(0)
{
unconst(m_parentCfg)->add(this);
}
virtual uint32_t expirationPeriod() const
{
if (!m_u32ExpirationPeriod)
return m_parentCfg->expirationPeriod();
else
return m_u32ExpirationPeriod;
}
/* XXX: private:*/
std::string m_name;
const BaseConfigEntity *m_parentCfg;
uint32_t m_u32ExpirationPeriod;
};
/**
* Network specific entries
*/
class NetworkConfigEntity:public ConfigEntity
{
public:
/* Address Pool matching with network declaration */
NetworkConfigEntity(std::string name,
const BaseConfigEntity *cfg,
const ClientMatchCriteria *criteria,
int matchlvl,
const RTNETADDRIPV4& networkID,
const RTNETADDRIPV4& networkMask,
const RTNETADDRIPV4& lowerIP,
const RTNETADDRIPV4& upperIP):
ConfigEntity(name, cfg, criteria, matchlvl),
m_NetworkID(networkID),
m_NetworkMask(networkMask),
m_UpperIP(upperIP),
m_LowerIP(lowerIP)
{
};
NetworkConfigEntity(std::string name,
const BaseConfigEntity *cfg,
const ClientMatchCriteria *criteria,
const RTNETADDRIPV4& networkID,
const RTNETADDRIPV4& networkMask):
ConfigEntity(name, cfg, criteria, 5),
m_NetworkID(networkID),
m_NetworkMask(networkMask)
{
m_UpperIP.u = m_NetworkID.u | (~m_NetworkMask.u);
m_LowerIP.u = m_NetworkID.u;
};
const RTNETADDRIPV4& upperIp() const {return m_UpperIP;}
const RTNETADDRIPV4& lowerIp() const {return m_LowerIP;}
const RTNETADDRIPV4& networkId() const {return m_NetworkID;}
const RTNETADDRIPV4& netmask() const {return m_NetworkMask;}
private:
RTNETADDRIPV4 m_NetworkID;
RTNETADDRIPV4 m_NetworkMask;
RTNETADDRIPV4 m_UpperIP;
RTNETADDRIPV4 m_LowerIP;
};
/**
* Host specific entry
* Address pool is contains one element
*/
class HostConfigEntity: public NetworkConfigEntity
{
public:
HostConfigEntity(const RTNETADDRIPV4& addr,
std::string name,
const NetworkConfigEntity *cfg,
const ClientMatchCriteria *criteria):
NetworkConfigEntity(name,
static_cast<const ConfigEntity*>(cfg), criteria, 10,
cfg->networkId(), cfg->netmask(), addr, addr)
{
/* upper addr == lower addr */
}
};
class RootConfigEntity: public NetworkConfigEntity
{
public:
RootConfigEntity(std::string name, uint32_t expirationPeriod);
virtual ~RootConfigEntity(){};
};
#if 0
/**
* Shared regions e.g. some of configured networks declarations
* are cover each other.
* XXX: Shared Network is join on Network config entities with possible
* overlaps in address pools. for a moment we won't configure and use them them
*/
class SharedNetworkConfigEntity: public NetworkEntity
{
public:
SharedNetworkConfigEntity(){}
int match(const Client& client) const { return m_criteria.match(client)? 3 : 0;}
SharedNetworkConfigEntity(NetworkEntity& network)
{
Networks.push_back(network);
}
virtual ~SharedNetworkConfigEntity(){}
std::vector<NetworkConfigEntity> Networks;
};
#endif
class ConfigurationManager
{
public:
static ConfigurationManager* getConfigurationManager();
static int extractRequestList(PCRTNETBOOTP pDhcpMsg, size_t cbDhcpMsg, RawOption& rawOpt);
int loadFromFile(const com::Utf8Str&);
int saveToFile();
/**
*
*/
Client getClientByDhcpPacket(const RTNETBOOTP *pDhcpMsg, size_t cbDhcpMsg);
/**
* XXX: it's could be done on DHCPOFFER or on DHCPACK (rfc2131 gives freedom here
* 3.1.2, what is strict that allocation should do address check before real
* allocation)...
*/
Lease allocateLease4Client(const Client& client, PCRTNETBOOTP pDhcpMsg, size_t cbDhcpMsg);
/**
* We call this before DHCPACK sent and after DHCPREQUEST received ...
* when requested configuration is acceptable.
*/
int commitLease4Client(Client& client);
/**
* Expires client lease.
*/
int expireLease4Client(Client& client);
static int findOption(uint8_t uOption, PCRTNETBOOTP pDhcpMsg, size_t cbDhcpMsg, RawOption& opt);
NetworkConfigEntity *addNetwork(NetworkConfigEntity *pCfg,
const RTNETADDRIPV4& networkId,
const RTNETADDRIPV4& netmask,
RTNETADDRIPV4& UpperAddress,
RTNETADDRIPV4& LowerAddress);
HostConfigEntity *addHost(NetworkConfigEntity*, const RTNETADDRIPV4&, ClientMatchCriteria*);
int addToAddressList(uint8_t u8OptId, RTNETADDRIPV4& address);
int flushAddressList(uint8_t u8OptId);
int setString(uint8_t u8OptId, const std::string& str);
const std::string& getString(uint8_t u8OptId);
const Ipv4AddressContainer& getAddressList(uint8_t u8OptId);
private:
ConfigurationManager():m(NULL){}
void init();
~ConfigurationManager();
bool isAddressTaken(const RTNETADDRIPV4& addr, Lease& lease);
bool isAddressTaken(const RTNETADDRIPV4& addr);
public:
/* nulls */
const Ipv4AddressContainer m_empty;
const std::string m_noString;
private:
struct Data;
Data *m;
};
class NetworkManager
{
public:
static NetworkManager *getNetworkManager(ComPtr<IDHCPServer> aDhcpServer = ComPtr<IDHCPServer>());
const RTNETADDRIPV4& getOurAddress() const;
const RTNETADDRIPV4& getOurNetmask() const;
const RTMAC& getOurMac() const;
void setOurAddress(const RTNETADDRIPV4& aAddress);
void setOurNetmask(const RTNETADDRIPV4& aNetmask);
void setOurMac(const RTMAC& aMac);
bool handleDhcpReqDiscover(PCRTNETBOOTP pDhcpMsg, size_t cb);
bool handleDhcpReqRequest(PCRTNETBOOTP pDhcpMsg, size_t cb);
bool handleDhcpReqDecline(PCRTNETBOOTP pDhcpMsg, size_t cb);
bool handleDhcpReqRelease(PCRTNETBOOTP pDhcpMsg, size_t cb);
void setService(const VBoxNetHlpUDPService *);
private:
NetworkManager();
~NetworkManager();
int offer4Client(const Client& lease, uint32_t u32Xid, uint8_t *pu8ReqList, int cReqList);
int ack(const Client& lease, uint32_t u32Xid, uint8_t *pu8ReqList, int cReqList);
int nak(const Client& lease, uint32_t u32Xid);
int prepareReplyPacket4Client(const Client& client, uint32_t u32Xid);
int doReply(const Client& client, const std::vector<RawOption>& extra);
int processParameterReqList(const Client& client, const uint8_t *pu8ReqList, int cReqList, std::vector<RawOption>& extra);
private:
static NetworkManager *g_NetworkManager;
private:
struct Data;
Data *m;
};
extern const ClientMatchCriteria *g_AnyClient;
extern RootConfigEntity *g_RootConfig;
extern const NullConfigEntity *g_NullConfig;
/**
* Helper class for stuffing DHCP options into a reply packet.
*/
class VBoxNetDhcpWriteCursor
{
private:
uint8_t *m_pbCur; /**< The current cursor position. */
uint8_t *m_pbEnd; /**< The end the current option space. */
uint8_t *m_pfOverload; /**< Pointer to the flags of the overload option. */
uint8_t m_fUsed; /**< Overload fields that have been used. */
PRTNETDHCPOPT m_pOpt; /**< The current option. */
PRTNETBOOTP m_pDhcp; /**< The DHCP packet. */
bool m_fOverflowed; /**< Set if we've overflowed, otherwise false. */
public:
/** Instantiate an option cursor for the specified DHCP message. */
VBoxNetDhcpWriteCursor(PRTNETBOOTP pDhcp, size_t cbDhcp) :
m_pbCur(&pDhcp->bp_vend.Dhcp.dhcp_opts[0]),
m_pbEnd((uint8_t *)pDhcp + cbDhcp),
m_pfOverload(NULL),
m_fUsed(0),
m_pOpt(NULL),
m_pDhcp(pDhcp),
m_fOverflowed(false)
{
AssertPtr(pDhcp);
Assert(cbDhcp > RT_UOFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts[10]));
}
/** Destructor. */
~VBoxNetDhcpWriteCursor()
{
m_pbCur = m_pbEnd = m_pfOverload = NULL;
m_pOpt = NULL;
m_pDhcp = NULL;
}
/**
* Try use the bp_file field.
* @returns true if not overloaded, false otherwise.
*/
bool useBpFile(void)
{
if ( m_pfOverload
&& (*m_pfOverload & 1))
return false;
m_fUsed |= 1 /* bp_file flag*/;
return true;
}
/**
* Try overload more BOOTP fields
*/
bool overloadMore(void)
{
/* switch option area. */
uint8_t *pbNew;
uint8_t *pbNewEnd;
uint8_t fField;
if (!(m_fUsed & 1))
{
fField = 1;
pbNew = &m_pDhcp->bp_file[0];
pbNewEnd = &m_pDhcp->bp_file[sizeof(m_pDhcp->bp_file)];
}
else if (!(m_fUsed & 2))
{
fField = 2;
pbNew = &m_pDhcp->bp_sname[0];
pbNewEnd = &m_pDhcp->bp_sname[sizeof(m_pDhcp->bp_sname)];
}
else
return false;
if (!m_pfOverload)
{
/* Add an overload option. */
*m_pbCur++ = RTNET_DHCP_OPT_OPTION_OVERLOAD;
*m_pbCur++ = fField;
m_pfOverload = m_pbCur;
*m_pbCur++ = 1; /* bp_file flag */
}
else
*m_pfOverload |= fField;
/* pad current option field */
while (m_pbCur != m_pbEnd)
*m_pbCur++ = RTNET_DHCP_OPT_PAD; /** @todo not sure if this stuff is at all correct... */
/* switch */
m_pbCur = pbNew;
m_pbEnd = pbNewEnd;
return true;
}
/**
* Begin an option.
*
* @returns true on success, false if we're out of space.
*
* @param uOption The option number.
* @param cb The amount of data.
*/
bool begin(uint8_t uOption, size_t cb)
{
/* Check that the data of the previous option has all been written. */
Assert( !m_pOpt
|| (m_pbCur - m_pOpt->dhcp_len == (uint8_t *)(m_pOpt + 1)));
AssertMsg(cb <= 255, ("%#x\n", cb));
/* Check if we need to overload more stuff. */
if ((uintptr_t)(m_pbEnd - m_pbCur) < cb + 2 + (m_pfOverload ? 1 : 3))
{
m_pOpt = NULL;
if (!overloadMore())
{
m_fOverflowed = true;
AssertMsgFailedReturn(("%u %#x\n", uOption, cb), false);
}
if ((uintptr_t)(m_pbEnd - m_pbCur) < cb + 2 + 1)
{
m_fOverflowed = true;
AssertMsgFailedReturn(("%u %#x\n", uOption, cb), false);
}
}
/* Emit the option header. */
m_pOpt = (PRTNETDHCPOPT)m_pbCur;
m_pOpt->dhcp_opt = uOption;
m_pOpt->dhcp_len = (uint8_t)cb;
m_pbCur += 2;
return true;
}
/**
* Puts option data.
*
* @param pvData The data.
* @param cb The amount to put.
*/
void put(void const *pvData, size_t cb)
{
Assert(m_pOpt || m_fOverflowed);
if (RT_LIKELY(m_pOpt))
{
Assert((uintptr_t)m_pbCur - (uintptr_t)(m_pOpt + 1) + cb <= (size_t)m_pOpt->dhcp_len);
memcpy(m_pbCur, pvData, cb);
m_pbCur += cb;
}
}
/**
* Puts an IPv4 Address.
*
* @param IPv4Addr The address.
*/
void putIPv4Addr(RTNETADDRIPV4 IPv4Addr)
{
put(&IPv4Addr, 4);
}
/**
* Adds an IPv4 address option.
*
* @returns true/false just like begin().
*
* @param uOption The option number.
* @param IPv4Addr The address.
*/
bool optIPv4Addr(uint8_t uOption, RTNETADDRIPV4 IPv4Addr)
{
if (!begin(uOption, 4))
return false;
putIPv4Addr(IPv4Addr);
return true;
}
/**
* Adds an option taking 1 or more IPv4 address.
*
* If the vector contains no addresses, the option will not be added.
*
* @returns true/false just like begin().
*
* @param uOption The option number.
* @param rIPv4Addrs Reference to the address vector.
*/
bool optIPv4Addrs(uint8_t uOption, std::vector<RTNETADDRIPV4> const &rIPv4Addrs)
{
size_t const c = rIPv4Addrs.size();
if (!c)
return true;
if (!begin(uOption, 4*c))
return false;
for (size_t i = 0; i < c; i++)
putIPv4Addr(rIPv4Addrs[i]);
return true;
}
/**
* Puts an 8-bit integer.
*
* @param u8 The integer.
*/
void putU8(uint8_t u8)
{
put(&u8, 1);
}
/**
* Adds an 8-bit integer option.
*
* @returns true/false just like begin().
*
* @param uOption The option number.
* @param u8 The integer
*/
bool optU8(uint8_t uOption, uint8_t u8)
{
if (!begin(uOption, 1))
return false;
putU8(u8);
return true;
}
/**
* Puts an 32-bit integer (network endian).
*
* @param u32 The integer.
*/
void putU32(uint32_t u32)
{
put(&u32, 4);
}
/**
* Adds an 32-bit integer (network endian) option.
*
* @returns true/false just like begin().
*
* @param uOption The option number.
* @param u32 The integer.
*/
bool optU32(uint8_t uOption, uint32_t u32)
{
if (!begin(uOption, 4))
return false;
putU32(u32);
return true;
}
/**
* Puts a std::string.
*
* @param rStr Reference to the string.
*/
void putStr(std::string const &rStr)
{
put(rStr.c_str(), rStr.size());
}
/**
* Adds an std::string option if the string isn't empty.
*
* @returns true/false just like begin().
*
* @param uOption The option number.
* @param rStr Reference to the string.
*/
bool optStr(uint8_t uOption, std::string const &rStr)
{
const size_t cch = rStr.size();
if (!cch)
return true;
if (!begin(uOption, cch))
return false;
put(rStr.c_str(), cch);
return true;
}
/**
* Whether we've overflowed.
*
* @returns true on overflow, false otherwise.
*/
bool hasOverflowed(void) const
{
return m_fOverflowed;
}
/**
* Adds the terminating END option.
*
* The END will always be added as we're reserving room for it, however, we
* might have dropped previous options due to overflows and that is what the
* return status indicates.
*
* @returns true on success, false on a (previous) overflow.
*/
bool optEnd(void)
{
Assert((uintptr_t)(m_pbEnd - m_pbCur) < 4096);
*m_pbCur++ = RTNET_DHCP_OPT_END;
return !hasOverflowed();
}
};
#endif /* !VBOX_INCLUDED_SRC_DHCP_Config_h */
|