]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5061] Updated DB parser and applied change to MySQL
authorFrancis Dupont <fdupont@isc.org>
Fri, 27 Jan 2017 15:20:50 +0000 (16:20 +0100)
committerFrancis Dupont <fdupont@isc.org>
Fri, 27 Jan 2017 15:20:50 +0000 (16:20 +0100)
src/lib/dhcpsrv/mysql_connection.cc
src/lib/dhcpsrv/parsers/dbaccess_parser.cc
src/lib/dhcpsrv/parsers/dbaccess_parser.h

index 7de0869f0db0bc19203efc3990ab08532fc39c6e..b027c6e009b43a77317b6e88e134b83025cd43d1 100644 (file)
@@ -66,6 +66,33 @@ MySqlConnection::openDatabase() {
         // No host.  Fine, we'll use "localhost"
     }
 
+    unsigned int port = 0;
+    string sport;
+    try {
+        sport = getParameter("port");
+    } catch (...) {
+        // No port parameter, we are going to use the default port.
+        sport = "";
+    }
+
+    if (sport.size() > 0) {
+        // Port was given, so try to convert it to an integer.
+
+        try {
+            port = boost::lexical_cast<unsigned int>(sport);
+        } catch (...) {
+            // Port given but could not be converted to an unsigned int.
+            // Just fall back to the default value.
+            port = 0;
+        }
+
+        // The port is only valid when it is in the 0..65535 range.
+        // Again fall back to the default when the given value is invalid.
+        if (port > numeric_limits<uint16_t>::max()) {
+            port = 0;
+        }
+    }
+
     const char* user = NULL;
     string suser;
     try {
@@ -173,7 +200,7 @@ MySqlConnection::openDatabase() {
     // because no row matching the WHERE clause was found, or because a
     // row was found but no data was altered.
     MYSQL* status = mysql_real_connect(mysql_, host, user, password, name,
-                                       0, NULL, CLIENT_FOUND_ROWS);
+                                       port, NULL, CLIENT_FOUND_ROWS);
     if (status != mysql_) {
         isc_throw(DbOpenError, mysql_error(mysql_));
     }
index dd51a9c3eb6c90666868d633ae6d5178a990a9f4..e4231dc1e28b54e2c370b77342b13760391cf2a4 100644 (file)
@@ -55,6 +55,7 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db,
 
     int64_t lfc_interval = 0;
     int64_t timeout = 0;
+    int64_t port = 0;
     // 2. Update the copy with the passed keywords.
     BOOST_FOREACH(ConfigPair param, database_config->mapValue()) {
         try {
@@ -72,6 +73,11 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db,
                 values_copy[param.first] =
                     boost::lexical_cast<std::string>(timeout);
 
+            } else if (param.first == "port") {
+                port = param.second->intValue();
+                values_copy[param.first] =
+                    boost::lexical_cast<std::string>(port);
+
             } else {
                 values_copy[param.first] = param.second->stringValue();
             }
@@ -128,6 +134,16 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db,
                   << " (" << value->getPosition() << ")");
     }
 
+    // e. Check that the port is within a reasonable range.
+    if ((port < 0) ||
+        (port > std::numeric_limits<uint16_t>::max())) {
+        ConstElementPtr value = database_config->get("port");
+        isc_throw(DhcpConfigError, "port value: " << port
+                  << " is out of range, expected value: 0.."
+                  << std::numeric_limits<uint16_t>::max()
+                  << " (" << value->getPosition() << ")");
+    }
+
     // 4. If all is OK, update the stored keyword/value pairs.  We do this by
     // swapping contents - values_copy is destroyed immediately after the
     // operation (when the method exits), so we are not interested in its new
index 8cb716d7d2a4f3edc2e04a11db0252ffef97e3ab..5460504d79ebe7ce9108366eecf242ba71352c37 100644 (file)
@@ -57,6 +57,7 @@ public:
     /// - "type" is "memfile", "mysql" or "postgresql"
     /// - "lfc-interval" is a number from the range of 0 to 4294967295.
     /// - "connect-timeout" is a number from the range of 0 to 4294967295.
+    /// - "port" is a number from the range of 0 to 65535.
     ///
     /// Once all has been validated, constructs the database access string
     /// expected by the lease manager.
@@ -65,10 +66,9 @@ public:
     /// @param database_config The configuration value for the "*-database"
     ///        identifier.
     ///
-    /// @throw isc::BadValue The 'type' keyword contains an unknown database
-    ///        type.
-    /// @throw isc::dhcp::MissingTypeKeyword The 'type' keyword is missing from
-    ///        the list of database access keywords.
+    /// @throw isc::dhcp::DhcpConfigError The 'type' keyword contains an
+    ///        unknown database type or is missing from the list of
+    ///        database access keywords.
     void parse(isc::dhcp::CfgDbAccessPtr& cfg_db,
                isc::data::ConstElementPtr database_config);
 
@@ -94,7 +94,6 @@ protected:
     /// @return Database access string
     std::string getDbAccessString() const;
 
-
 private:
 
     std::map<std::string, std::string> values_; ///< Stored parameter values