]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3141] config parsers boilerplate
authorPiotrek Zadroga <piotrek@isc.org>
Mon, 12 Feb 2024 21:04:39 +0000 (22:04 +0100)
committerPiotrek Zadroga <piotrek@isc.org>
Fri, 23 Feb 2024 16:14:05 +0000 (17:14 +0100)
src/lib/dhcp/option4_dnr.cc
src/lib/dhcp/option4_dnr.h
src/lib/dhcp/option6_dnr.cc
src/lib/dhcp/option6_dnr.h

index a848d92d92be57943de1524e1d87119e7bb30611..f3985324c600171f470e7009edc4779359f84846 100644 (file)
@@ -48,42 +48,48 @@ Option4Dnr::pack(OutputBuffer& buf, bool check) const {
 
 void
 Option4Dnr::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
-    setData(begin, end);
-    while (begin != end) {
-        DnrInstance dnr_instance(V4);
-        if (std::distance(begin, end) < dnr_instance.getMinimalLength()) {
-            isc_throw(OutOfRange, dnr_instance.getLogPrefix()
-                                      << "DNR instance data truncated to size "
-                                      << std::distance(begin, end));
-        }
-
-        // Unpack DnrInstanceDataLength.
-        dnr_instance.unpackDnrInstanceDataLength(begin, end);
-
-        const OptionBufferConstIter dnr_instance_end = begin +
-                                                       dnr_instance.getDnrInstanceDataLength();
-
-        // Unpack Service priority.
-        dnr_instance.unpackServicePriority(begin);
+    if (convenient_notation_) {
+        // parse convenient notation
+        std::string config_txt = std::string(begin, end);
+        parseConfigData(config_txt);
+    } else {
+        setData(begin, end);
+        while (begin != end) {
+            DnrInstance dnr_instance(V4);
+            if (std::distance(begin, end) < dnr_instance.getMinimalLength()) {
+                isc_throw(OutOfRange, dnr_instance.getLogPrefix()
+                                          << "DNR instance data truncated to size "
+                                          << std::distance(begin, end));
+            }
+
+            // Unpack DnrInstanceDataLength.
+            dnr_instance.unpackDnrInstanceDataLength(begin, end);
+
+            const OptionBufferConstIter dnr_instance_end = begin +
+                                                           dnr_instance.getDnrInstanceDataLength();
+
+            // Unpack Service priority.
+            dnr_instance.unpackServicePriority(begin);
+
+            // Unpack ADN len + ADN.
+            dnr_instance.unpackAdn(begin, dnr_instance_end);
+
+            if (begin == dnr_instance_end) {
+                // ADN only mode, other fields are not included.
+                addDnrInstance(dnr_instance);
+                continue;
+            }
+
+            dnr_instance.setAdnOnlyMode(false);
+
+            // Unpack Addr Len + IPv4 Address(es).
+            dnr_instance.unpackAddresses(begin, dnr_instance_end);
+
+            // SvcParams (variable length) field is last.
+            dnr_instance.unpackSvcParams(begin, dnr_instance_end);
 
-        // Unpack ADN len + ADN.
-        dnr_instance.unpackAdn(begin, dnr_instance_end);
-
-        if (begin == dnr_instance_end) {
-            // ADN only mode, other fields are not included.
             addDnrInstance(dnr_instance);
-            continue;
         }
-
-        dnr_instance.setAdnOnlyMode(false);
-
-        // Unpack Addr Len + IPv4 Address(es).
-        dnr_instance.unpackAddresses(begin, dnr_instance_end);
-
-        // SvcParams (variable length) field is last.
-        dnr_instance.unpackSvcParams(begin, dnr_instance_end);
-
-        addDnrInstance(dnr_instance);
     }
 }
 
@@ -119,6 +125,11 @@ Option4Dnr::addDnrInstance(DnrInstance& dnr_instance) {
     dnr_instances_.push_back(dnr_instance);
 }
 
+void
+Option4Dnr::parseConfigData(const std::string& config_txt) {
+    // TBD
+}
+
 const std::unordered_set<std::string> DnrInstance::FORBIDDEN_SVC_PARAMS = {"ipv4hint", "ipv6hint"};
 
 DnrInstance::DnrInstance(Option::Universe universe)
index 221a5ee8ccfc07d6be0203560f547fb639904568..fee4c60a8af6ce2a70e1bdc51ad965ddcc392d5f 100644 (file)
@@ -546,6 +546,17 @@ private:
     /// @brief Flag stating whether the %Option was constructed with a convenient notation string,
     /// that needs custom parsing, or binary data.
     bool convenient_notation_;
+
+    /// @brief Parses a convenient notation of the option data, which may be used in config.
+    ///
+    /// As an alternative to the binary format,
+    /// we provide convenience option definition as a string in format:
+    /// TBD
+    ///
+    /// @param config_txt convenient notation of the option data received as string
+    ///
+    /// @throw BadValue Thrown in case parser found wrong format of received string.
+    void parseConfigData(const std::string& config_txt);
 };
 
 /// A pointer to the @c OptionDnr4 object.
index 389c3adeb276a409e0e93522fa703b01a823d4b9..e4daefce534de9a06380f6d129fd29be66f8032b 100644 (file)
@@ -55,31 +55,37 @@ Option6Dnr::packAddresses(util::OutputBuffer& buf) const {
 
 void
 Option6Dnr::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
-    if (std::distance(begin, end) < getMinimalLength()) {
-        isc_throw(OutOfRange, getLogPrefix()
-                                  << "data truncated to size " << std::distance(begin, end));
-    }
+    if (convenient_notation_) {
+        // parse convenient notation
+        std::string config_txt = std::string(begin, end);
+        parseConfigData(config_txt);
+    } else {
+        if (std::distance(begin, end) < getMinimalLength()) {
+            isc_throw(OutOfRange, getLogPrefix()
+                                      << "data truncated to size " << std::distance(begin, end));
+        }
 
-    setData(begin, end);
+        setData(begin, end);
 
-    // First two octets of Option data is Service Priority - this is mandatory field.
-    unpackServicePriority(begin);
+        // First two octets of Option data is Service Priority - this is mandatory field.
+        unpackServicePriority(begin);
 
-    // Next come two octets of ADN Length plus the ADN data itself (variable length).
-    // This is Opaque Data Tuple so let's use this class to retrieve the ADN data.
-    unpackAdn(begin, end);
+        // Next come two octets of ADN Length plus the ADN data itself (variable length).
+        // This is Opaque Data Tuple so let's use this class to retrieve the ADN data.
+        unpackAdn(begin, end);
 
-    if (begin == end) {
-        // ADN only mode, other fields are not included.
-        return;
-    }
+        if (begin == end) {
+            // ADN only mode, other fields are not included.
+            return;
+        }
 
-    adn_only_mode_ = false;
+        adn_only_mode_ = false;
 
-    unpackAddresses(begin, end);
+        unpackAddresses(begin, end);
 
-    // SvcParams (variable length) field is last.
-    unpackSvcParams(begin, end);
+        // SvcParams (variable length) field is last.
+        unpackSvcParams(begin, end);
+    }
 }
 
 std::string
@@ -143,5 +149,10 @@ Option6Dnr::unpackAddresses(OptionBufferConstIter& begin, OptionBufferConstIter
     }
 }
 
+void
+Option6Dnr::parseConfigData(const std::string& config_txt){
+    // TBD
+}
+
 }  // namespace dhcp
 }  // namespace isc
index f7fbeaa4d45c96b78011be92645589e55b06339d..4a453f05248843c0e834548c90edcc8b40a7a403 100644 (file)
@@ -146,6 +146,17 @@ private:
     /// @brief Flag stating whether the %Option was constructed with a convenient notation string,
     /// that needs custom parsing, or binary data.
     bool convenient_notation_;
+
+    /// @brief Parses a convenient notation of the option data, which may be used in config.
+    ///
+    /// As an alternative to the binary format,
+    /// we provide convenience option definition as a string in format:
+    /// TBD
+    ///
+    /// @param config_txt convenient notation of the option data received as string
+    ///
+    /// @throw BadValue Thrown in case parser found wrong format of received string.
+    void parseConfigData(const std::string& config_txt);
 };
 
 /// A pointer to the @c Option6Dnr object.