]> the.earth.li Git - onak.git/blobdiff - keydb_db4.c
Add ability to drop overly large packets
[onak.git] / keydb_db4.c
index 369788ea6133c06f92d2b4567be8693f65a8984e..f3d20dbcafd3ff8521981b455a05c110418f8dec 100644 (file)
@@ -13,8 +13,7 @@
  * more details.
  *
  * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <sys/types.h>
@@ -23,6 +22,8 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -37,7 +38,9 @@
 #include "decodekey.h"
 #include "keystructs.h"
 #include "mem.h"
+#include "ll.h"
 #include "log.h"
+#include "onak.h"
 #include "onak-conf.h"
 #include "parsekey.h"
 #include "wordlist.h"
@@ -161,8 +164,9 @@ static void db4_endtrans(struct onak_dbctx *dbctx)
  *     we're running with a newer version of db4 than the database was
  *     created with.
  */
-static int db4_upgradedb(struct onak_db4_dbctx *privctx)
+static int db4_upgradedb(struct onak_dbctx *dbctx)
 {
+       struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
        DB *curdb = NULL;
        int ret;
        int i;
@@ -171,7 +175,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
        struct stat statbuf;
        ssize_t written;
 
-       snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+       snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
                        DB4_UPGRADE_FILE);
        lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
        if (lockfile_fd < 0) {
@@ -190,7 +194,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
        if (written != strlen(buf)) {
                logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: "
                                "%s", strerror(errno));
-               snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+               snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
                                DB4_UPGRADE_FILE);
                unlink(buf);
                return -1;
@@ -200,14 +204,14 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
        ret = db_env_create(&privctx->dbenv, 0);
        if (ret == 0) {
                privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
-               privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
+               privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0);
                privctx->dbenv = NULL;
        }
        for (i = 0; i < privctx->numdbs; i++) {
                ret = db_create(&curdb, NULL, 0);
                if (ret == 0) {
                        snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
-                               config.db_dir, i);
+                               dbctx->config->location, i);
                        logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                        curdb->upgrade(curdb, buf, 0);
                        curdb->close(curdb, 0);
@@ -220,7 +224,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
 
        ret = db_create(&curdb, NULL, 0);
        if (ret == 0) {
-               snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
+               snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location);
                logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                curdb->upgrade(curdb, buf, 0);
                curdb->close(curdb, 0);
@@ -232,7 +236,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
 
        ret = db_create(&curdb, NULL, 0);
        if (ret == 0) {
-               snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
+               snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location);
                logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                curdb->upgrade(curdb, buf, 0);
                curdb->close(curdb, 0);
@@ -244,7 +248,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
 
        ret = db_create(&curdb, NULL, 0);
        if (ret == 0) {
-               snprintf(buf, sizeof(buf) - 1, "%s/id64db", config.db_dir);
+               snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location);
                logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                curdb->upgrade(curdb, buf, 0);
                curdb->close(curdb, 0);
@@ -256,7 +260,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
 
        ret = db_create(&curdb, NULL, 0);
        if (ret == 0) {
-               snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
+               snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location);
                logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                curdb->upgrade(curdb, buf, 0);
                curdb->close(curdb, 0);
@@ -268,7 +272,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
 
        ret = db_create(&curdb, NULL, 0);
        if (ret == 0) {
-               snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
+               snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location);
                logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
                curdb->upgrade(curdb, buf, 0);
                curdb->close(curdb, 0);
@@ -278,7 +282,7 @@ static int db4_upgradedb(struct onak_db4_dbctx *privctx)
                        db_strerror(ret));
        }
 
-       snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+       snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
                        DB4_UPGRADE_FILE);
        unlink(buf);
 
@@ -1626,7 +1630,7 @@ static void db4_cleanupdb(struct onak_dbctx *dbctx)
  *     this file are called in order to allow the DB to be initialized ready
  *     for access.
  */
-struct onak_dbctx *keydb_db4_init(bool readonly)
+struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
 {
        char       buf[1024];
        FILE      *numdb = NULL;
@@ -1642,6 +1646,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
        if (dbctx == NULL) {
                return NULL;
        }
+       dbctx->config = dbcfg;
        dbctx->priv = privctx = calloc(1, sizeof(*privctx));
        if (privctx == NULL) {
                free(dbctx);
@@ -1651,7 +1656,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
        /* Default to 16 key data DBs */
        privctx->numdbs = 16;
 
-       snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+       snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
                        DB4_UPGRADE_FILE);
        ret = stat(buf, &statbuf);
        while ((ret == 0) || (errno != ENOENT)) {
@@ -1666,7 +1671,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
        }
        ret = 0;
 
-       snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
+       snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
        numdb = fopen(buf, "r");
        if (numdb != NULL) {
                if (fgets(buf, sizeof(buf), numdb) != NULL) {
@@ -1706,12 +1711,14 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
         * Up the number of locks we're allowed at once. We base this on
         * the maximum number of keys we're going to return.
         */
-       maxlocks = config.maxkeys * 16;
-       if (maxlocks < 1000) {
-               maxlocks = 1000;
+       if (ret == 0) {
+               maxlocks = config.maxkeys * 16;
+               if (maxlocks < 1000) {
+                       maxlocks = 1000;
+               }
+               privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
+               privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
        }
-       privctx->dbenv->set_lk_max_locks(privctx->dbenv, maxlocks);
-       privctx->dbenv->set_lk_max_objects(privctx->dbenv, maxlocks);
 
        /*
         * Enable deadlock detection so that we don't block indefinitely on
@@ -1728,7 +1735,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
        }
 
        if (ret == 0) {
-               ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
+               ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
                                DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
                                DB_INIT_TXN |
                                DB_CREATE,
@@ -1737,7 +1744,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
                if (ret == DB_VERSION_MISMATCH) {
                        privctx->dbenv->close(privctx->dbenv, 0);
                        privctx->dbenv = NULL;
-                       ret = db4_upgradedb(privctx);
+                       ret = db4_upgradedb(dbctx);
                        if (ret == 0) {
                                ret = db_env_create(&privctx->dbenv, 0);
                        }
@@ -1747,7 +1754,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
                                privctx->dbenv->set_lk_detect(privctx->dbenv,
                                        DB_LOCK_DEFAULT);
                                ret = privctx->dbenv->open(privctx->dbenv,
-                                       config.db_dir,
+                                       dbcfg->location,
                                        DB_INIT_LOG | DB_INIT_MPOOL |
                                        DB_INIT_LOCK | DB_INIT_TXN |
                                        DB_CREATE | DB_RECOVER,
@@ -1766,7 +1773,7 @@ struct onak_dbctx *keydb_db4_init(bool readonly)
                if (ret != 0) {
                        logthing(LOGTHING_CRITICAL,
                                        "Error opening db environment: %s (%s)",
-                                       config.db_dir,
+                                       dbcfg->location,
                                        db_strerror(ret));
                        if (privctx->dbenv != NULL) {
                                privctx->dbenv->close(privctx->dbenv, 0);