|
@@ -19,6 +19,52 @@ namespace isc {
|
|
namespace dhcp {
|
|
namespace dhcp {
|
|
|
|
|
|
void
|
|
void
|
|
|
|
+OptionDataTypeUtil::readAddress(const std::vector<uint8_t>& buf,
|
|
|
|
+ const short family,
|
|
|
|
+ asiolink::IOAddress& address) {
|
|
|
|
+ using namespace isc::asiolink;
|
|
|
|
+ if (family == AF_INET) {
|
|
|
|
+ if (buf.size() < V4ADDRESS_LEN) {
|
|
|
|
+ isc_throw(BadDataTypeCast, "unavle to read data from the buffer as"
|
|
|
|
+ << " IPv4 address. Invalid buffer size: " << buf.size());
|
|
|
|
+ }
|
|
|
|
+ address = IOAddress::from_bytes(AF_INET, &buf[0]);
|
|
|
|
+ } else if (buf.size() == V6ADDRESS_LEN) {
|
|
|
|
+ if (buf.size() < V6ADDRESS_LEN) {
|
|
|
|
+ isc_throw(BadDataTypeCast, "unavle to read data from the buffer as"
|
|
|
|
+ << " IPv6 address. Invalid buffer size: " << buf.size());
|
|
|
|
+ }
|
|
|
|
+ address = IOAddress::from_bytes(AF_INET6, &buf[0]);
|
|
|
|
+ } else {
|
|
|
|
+ isc_throw(BadDataTypeCast, "unable to read data from the buffer as"
|
|
|
|
+ "IP address. Invalid family: " << family);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void
|
|
|
|
+OptionDataTypeUtil::writeAddress(const asiolink::IOAddress& address,
|
|
|
|
+ std::vector<uint8_t>& buf) {
|
|
|
|
+ if (address.getAddress().is_v4()) {
|
|
|
|
+ asio::ip::address_v4::bytes_type addr_bytes =
|
|
|
|
+ address.getAddress().to_v4().to_bytes();
|
|
|
|
+ // Increase the buffer size by the size of IPv4 address.
|
|
|
|
+ buf.resize(buf.size() + addr_bytes.size());
|
|
|
|
+ std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
|
|
|
|
+ buf.end());
|
|
|
|
+ } else if (address.getAddress().is_v6()) {
|
|
|
|
+ asio::ip::address_v6::bytes_type addr_bytes =
|
|
|
|
+ address.getAddress().to_v6().to_bytes();
|
|
|
|
+ // Incresase the buffer size by the size of IPv6 address.
|
|
|
|
+ buf.resize(buf.size() + addr_bytes.size());
|
|
|
|
+ std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
|
|
|
|
+ buf.end());
|
|
|
|
+ } else {
|
|
|
|
+ isc_throw(BadDataTypeCast, "the address " << address.toText()
|
|
|
|
+ << " is neither valid IPv4 not IPv6 address.");
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void
|
|
OptionDataTypeUtil::writeBinary(const std::string& hex_str,
|
|
OptionDataTypeUtil::writeBinary(const std::string& hex_str,
|
|
std::vector<uint8_t>& buf) {
|
|
std::vector<uint8_t>& buf) {
|
|
// Binary value means that the value is encoded as a string
|
|
// Binary value means that the value is encoded as a string
|
|
@@ -36,6 +82,21 @@ OptionDataTypeUtil::writeBinary(const std::string& hex_str,
|
|
buf.insert(buf.end(), binary.begin(), binary.end());
|
|
buf.insert(buf.end(), binary.begin(), binary.end());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+bool
|
|
|
|
+OptionDataTypeUtil::readBool(const std::vector<uint8_t>& buf) {
|
|
|
|
+ if (buf.size() < 1) {
|
|
|
|
+ isc_throw(BadDataTypeCast, "unable to read the buffer as boolean"
|
|
|
|
+ << " value. Invalid buffer size " << buf.size());
|
|
|
|
+ }
|
|
|
|
+ if (buf[0] == 1) {
|
|
|
|
+ return (true);
|
|
|
|
+ } else if (buf[0] == 0) {
|
|
|
|
+ return (false);
|
|
|
|
+ }
|
|
|
|
+ isc_throw(BadDataTypeCast, "unable to read the buffer as boolean"
|
|
|
|
+ << " value. Inavlid value " << static_cast<int>(buf[0]));
|
|
|
|
+}
|
|
|
|
+
|
|
void
|
|
void
|
|
OptionDataTypeUtil::writeBool(const bool value,
|
|
OptionDataTypeUtil::writeBool(const bool value,
|
|
std::vector<uint8_t>& buf) {
|
|
std::vector<uint8_t>& buf) {
|
|
@@ -47,26 +108,9 @@ OptionDataTypeUtil::writeBool(const bool value,
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
void
|
|
-OptionDataTypeUtil::writeAddress(const asiolink::IOAddress& address,
|
|
|
|
- std::vector<uint8_t>& buf) {
|
|
|
|
- if (address.getAddress().is_v4()) {
|
|
|
|
- asio::ip::address_v4::bytes_type addr_bytes =
|
|
|
|
- address.getAddress().to_v4().to_bytes();
|
|
|
|
- // Increase the buffer size by the size of IPv4 address.
|
|
|
|
- buf.resize(buf.size() + addr_bytes.size());
|
|
|
|
- std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
|
|
|
|
- buf.end());
|
|
|
|
- } else if (address.getAddress().is_v6()) {
|
|
|
|
- asio::ip::address_v6::bytes_type addr_bytes =
|
|
|
|
- address.getAddress().to_v6().to_bytes();
|
|
|
|
- // Incresase the buffer size by the size of IPv6 address.
|
|
|
|
- buf.resize(buf.size() + addr_bytes.size());
|
|
|
|
- std::copy_backward(addr_bytes.begin(), addr_bytes.end(),
|
|
|
|
- buf.end());
|
|
|
|
- } else {
|
|
|
|
- isc_throw(BadDataTypeCast, "the address " << address.toText()
|
|
|
|
- << " is neither valid IPv4 not IPv6 address.");
|
|
|
|
- }
|
|
|
|
|
|
+OptionDataTypeUtil::readString(const std::vector<uint8_t>& buf,
|
|
|
|
+ std::string& value) {
|
|
|
|
+ value.insert(value.end(), buf.begin(), buf.end());
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
void
|