From 91f99691c115388c0f4b779b9781f709e510f4b6 Mon Sep 17 00:00:00 2001
From: Daniel Salzman <daniel.salzman@nic.cz>
Date: Fri, 4 Dec 2015 13:03:08 +0100
Subject: [PATCH] libknot: split errcode to simple errcode and error functions

---
 Knot.files                                |   5 +-
 src/Makefile.am                           |   5 +-
 src/contrib/base32hex.c                   |   2 +-
 src/contrib/base64.c                      |   2 +-
 src/contrib/net.c                         |   2 +-
 src/contrib/sockaddr.c                    |   2 +-
 src/contrib/wire_ctx.h                    |   2 +-
 src/knot/conf/base.c                      |   1 +
 src/libknot/errcode.h                     | 215 +++++++++++++++++++---
 src/libknot/{errcode.c => error.c}        |   2 +-
 src/libknot/error.h                       |  38 ++++
 src/libknot/internal/errcode.c            |  67 -------
 src/libknot/internal/errcode.h            | 169 -----------------
 src/libknot/internal/hhash.c              |   2 +-
 src/libknot/internal/namedb/namedb_lmdb.c |   2 +-
 src/libknot/internal/namedb/namedb_trie.c |   2 +-
 src/libknot/internal/trie/hat-trie.h      |   2 +-
 src/libknot/libknot.h                     |   2 +-
 src/libknot/yparser/yparser.c             |   2 +-
 src/libknot/yparser/ypbody.c              |   2 +-
 src/libknot/yparser/ypbody.rl             |   2 +-
 src/libknot/yparser/ypformat.c            |   2 +-
 src/libknot/yparser/ypscheme.c            |   3 +-
 src/libknot/yparser/yptrafo.c             |   3 +-
 tests/wire_ctx.c                          |   2 +-
 25 files changed, 254 insertions(+), 284 deletions(-)
 rename src/libknot/{errcode.c => error.c} (99%)
 create mode 100644 src/libknot/error.h
 delete mode 100644 src/libknot/internal/errcode.c
 delete mode 100644 src/libknot/internal/errcode.h

diff --git a/Knot.files b/Knot.files
index 7ec976ee6a..d51ce82dae 100644
--- a/Knot.files
+++ b/Knot.files
@@ -361,10 +361,9 @@ src/libknot/dname.c
 src/libknot/dname.h
 src/libknot/dnssec/rrset-sign.c
 src/libknot/dnssec/rrset-sign.h
-src/libknot/errcode.c
 src/libknot/errcode.h
-src/libknot/internal/errcode.c
-src/libknot/internal/errcode.h
+src/libknot/error.c
+src/libknot/error.h
 src/libknot/internal/hhash.c
 src/libknot/internal/hhash.h
 src/libknot/internal/lists.c
diff --git a/src/Makefile.am b/src/Makefile.am
index c13e437eba..2c33848f5b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -51,6 +51,7 @@ nobase_libknot_la_HEADERS =			\
 	libknot/dname.h				\
 	libknot/dnssec/rrset-sign.h		\
 	libknot/errcode.h			\
+	libknot/error.h				\
 	libknot/libknot.h			\
 	libknot/packet/compr.h			\
 	libknot/packet/pkt.h			\
@@ -80,7 +81,6 @@ nobase_libknot_la_HEADERS =			\
 
 libknot_internal_ladir = $(includedir)
 nobase_libknot_internal_la_HEADERS = 		\
-	libknot/internal/errcode.h		\
 	libknot/internal/hhash.h		\
 	libknot/internal/lists.h		\
 	libknot/internal/macros.h		\
@@ -120,7 +120,7 @@ libknot_la_SOURCES =				\
 	libknot/descriptor.c			\
 	libknot/dname.c				\
 	libknot/dnssec/rrset-sign.c		\
-	libknot/errcode.c			\
+	libknot/error.c				\
 	libknot/packet/compr.c			\
 	libknot/packet/pkt.c			\
 	libknot/packet/rrset-wire.c		\
@@ -149,7 +149,6 @@ libknot_yparser_la_SOURCES = 			\
 	$(nobase_libknot_yparser_la_HEADERS)
 
 libknot_internal_la_SOURCES = 			\
-	libknot/internal/errcode.c		\
 	libknot/internal/hhash.c		\
 	libknot/internal/lists.c		\
 	libknot/internal/mempattern.c		\
diff --git a/src/contrib/base32hex.c b/src/contrib/base32hex.c
index a0fc999f0e..66e95a8318 100644
--- a/src/contrib/base32hex.c
+++ b/src/contrib/base32hex.c
@@ -15,7 +15,7 @@
  */
 
 #include "contrib/base32hex.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 #include <stdlib.h>
 #include <stdint.h>
diff --git a/src/contrib/base64.c b/src/contrib/base64.c
index 2a1641a49e..c4df2f5a13 100644
--- a/src/contrib/base64.c
+++ b/src/contrib/base64.c
@@ -15,7 +15,7 @@
  */
 
 #include "contrib/base64.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 #include <stdlib.h>
 #include <stdint.h>
diff --git a/src/contrib/net.c b/src/contrib/net.c
index 1d31a7dbbb..737c9142d7 100644
--- a/src/contrib/net.c
+++ b/src/contrib/net.c
@@ -23,7 +23,7 @@
 #include <sys/uio.h>
 #include <unistd.h>
 
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 #include "contrib/net.h"
 #include "contrib/sockaddr.h"
 
diff --git a/src/contrib/sockaddr.c b/src/contrib/sockaddr.c
index 9155c81c10..a571e0c7a0 100644
--- a/src/contrib/sockaddr.c
+++ b/src/contrib/sockaddr.c
@@ -20,7 +20,7 @@
 #include <netdb.h>
 
 #include "libknot/consts.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 #include "contrib/sockaddr.h"
 #include "contrib/openbsd/strlcpy.h"
 
diff --git a/src/contrib/wire_ctx.h b/src/contrib/wire_ctx.h
index d239354d74..72845aaaa3 100644
--- a/src/contrib/wire_ctx.h
+++ b/src/contrib/wire_ctx.h
@@ -24,7 +24,7 @@
 #include <sys/types.h>
 
 #include "contrib/endian.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 /*!
  * \brief Struct to keep the wire context.
diff --git a/src/knot/conf/base.c b/src/knot/conf/base.c
index d3394cc9a7..ed5301c6b4 100644
--- a/src/knot/conf/base.c
+++ b/src/knot/conf/base.c
@@ -26,6 +26,7 @@
 #include "knot/common/log.h"
 #include "knot/nameserver/query_module.h"
 #include "knot/nameserver/internet.h"
+#include "libknot/libknot.h"
 #include "libknot/yparser/ypformat.h"
 #include "libknot/yparser/yptrafo.h"
 #include "contrib/sockaddr.h"
diff --git a/src/libknot/errcode.h b/src/libknot/errcode.h
index 4fb4241698..c41ca4689f 100644
--- a/src/libknot/errcode.h
+++ b/src/libknot/errcode.h
@@ -1,41 +1,208 @@
+/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 /*!
-* \file errcode.h
+* \file
 *
-* \author Lubos Slovak <lubos.slovak@nic.cz>
-* \author Marek Vavrusa <marek.vavrusa@nic.cz>
+* \brief Knot error codes.
 *
-* \brief Error codes and function for getting error message.
-*
-* \addtogroup common_lib
+* \addtogroup libknot
 * @{
 */
-/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+#pragma once
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+#include <errno.h>
 
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+/*! \brief Error codes used in the library. */
+enum knot_error {
+	KNOT_EOK = 0,
 
-#pragma once
+	/* Directly mapped error codes. */
+	KNOT_ENOMEM        = -ENOMEM,
+	KNOT_EINVAL        = -EINVAL,
+	KNOT_ENOTSUP       = -ENOTSUP,
+	KNOT_EBUSY         = -EBUSY,
+	KNOT_EAGAIN        = -EAGAIN,
+	KNOT_EACCES        = -EACCES,
+	KNOT_ECONNREFUSED  = -ECONNREFUSED,
+	KNOT_EISCONN       = -EISCONN,
+	KNOT_EADDRINUSE    = -EADDRINUSE,
+	KNOT_ENOENT        = -ENOENT,
+	KNOT_EEXIST        = -EEXIST,
+	KNOT_ERANGE        = -ERANGE,
+	KNOT_EADDRNOTAVAIL = -EADDRNOTAVAIL,
+
+	KNOT_ERROR_MIN = -1000,
+
+	/* General errors. */
+	KNOT_ERROR = KNOT_ERROR_MIN,
+	KNOT_ENOTRUNNING,
+	KNOT_EPARSEFAIL,
+	KNOT_ESEMCHECK,
+	KNOT_EEXPIRED,
+	KNOT_EUPTODATE,
+	KNOT_EFEWDATA,
+	KNOT_ESPACE,
+	KNOT_EMALF,
+	KNOT_ECRYPTO,
+	KNOT_ENSEC3PAR,
+	KNOT_ENSEC3CHAIN,
+	KNOT_EOUTOFZONE,
+	KNOT_EHASH,
+	KNOT_EZONEINVAL,
+	KNOT_EZONENOENT,
+	KNOT_ENOZONE,
+	KNOT_ENONODE,
+	KNOT_ENOMASTER,
+	KNOT_EDNAMEPTR,
+	KNOT_EPAYLOAD,
+	KNOT_EPREREQ,
+	KNOT_ETTL,
+	KNOT_ENOXFR,
+	KNOT_ENOIXFR,
+	KNOT_EXFRREFUSED,
+	KNOT_EDENIED,
+	KNOT_ECONN,
+	KNOT_ETIMEOUT,
+	KNOT_EIXFRSPACE,
+	KNOT_ECNAME,
+	KNOT_ENODIFF,
+	KNOT_EDSDIGESTLEN,
+	KNOT_ENOTSIG,
+	KNOT_ELIMIT,
+	KNOT_EWRITABLE,
+	KNOT_EOF,
+	KNOT_ESYSTEM,
+	KNOT_EFILE,
+
+	/* Control states. */
+	KNOT_CTL_STOP,
+	KNOT_CTL_ACCEPTED,
+	KNOT_CTL_ARG_REQ,
+
+	/* Network errors. */
+	KNOT_NET_EADDR,
+	KNOT_NET_ESOCKET,
+	KNOT_NET_ECONNECT,
+	KNOT_NET_ESEND,
+	KNOT_NET_ERECV,
+	KNOT_NET_ETIMEOUT,
+
+	/* Encoding errors. */
+	KNOT_BASE64_ESIZE,
+	KNOT_BASE64_ECHAR,
+	KNOT_BASE32HEX_ESIZE,
+	KNOT_BASE32HEX_ECHAR,
+
+	/* TSIG errors. */
+	KNOT_TSIG_EBADSIG,
+	KNOT_TSIG_EBADKEY,
+	KNOT_TSIG_EBADTIME,
+	KNOT_TSIG_EBADTRUNC,
+
+	/* Key parsing errors. */
+	KNOT_KEY_EPUBLIC_KEY_OPEN,
+	KNOT_KEY_EPRIVATE_KEY_OPEN,
+	KNOT_KEY_EPUBLIC_KEY_INVALID,
 
-#include "libknot/internal/errcode.h"
+	/* DNSSEC errors. */
+	KNOT_DNSSEC_EMISSINGKEYTYPE,
+	KNOT_DNSSEC_ENOKEY,
+
+	/* Yparser errors. */
+	KNOT_YP_ECHAR_TAB,
+	KNOT_YP_EINVAL_ITEM,
+	KNOT_YP_EINVAL_ID,
+	KNOT_YP_EINVAL_DATA,
+	KNOT_YP_EINVAL_INDENT,
+	KNOT_YP_ENOTSUP_DATA,
+	KNOT_YP_ENOTSUP_ID,
+	KNOT_YP_ENODATA,
+	KNOT_YP_ENOID,
+
+	/* Configuration errors. */
+	KNOT_CONF_EMPTY,
+	KNOT_CONF_EVERSION,
+	KNOT_CONF_EREDEFINE,
+	KNOT_CONF_ETXN,
+	KNOT_CONF_ENOTXN,
+	KNOT_CONF_EMANYTXN,
+
+	/* Processing error. */
+	KNOT_LAYER_ERROR,
+
+	KNOT_ERROR_MAX = -501
+};
 
 /*!
- * \brief Returns error message for the given error code.
+ * \brief Map POSIX errno code to Knot error code.
  *
- * \param code Error code.
+ * \param code Errno code to transform (set -1 to use the current errno).
+ *
+ * \return Mapped errno or KNOT_ERROR if unknown.
+ */
+inline static int knot_map_errno_code(int code)
+{
+	if (code < 0) {
+		code = errno;
+	}
+
+	typedef struct {
+		int errno_code;
+		int libknot_code;
+	} err_table_t;
+
+	#define ERR_ITEM(name) { name, KNOT_##name }
+	static const err_table_t errno_to_errcode[] = {
+		ERR_ITEM(ENOMEM),
+		ERR_ITEM(EINVAL),
+		ERR_ITEM(ENOTSUP),
+		ERR_ITEM(EBUSY),
+		ERR_ITEM(EAGAIN),
+		ERR_ITEM(EACCES),
+		ERR_ITEM(ECONNREFUSED),
+		ERR_ITEM(EISCONN),
+		ERR_ITEM(EADDRINUSE),
+		ERR_ITEM(ENOENT),
+		ERR_ITEM(EEXIST),
+		ERR_ITEM(ERANGE),
+		ERR_ITEM(EADDRNOTAVAIL),
+
+		/* Terminator - default value. */
+		{ 0, KNOT_ERROR }
+	};
+	#undef ERR_ITEM
+
+	const err_table_t *err = errno_to_errcode;
+
+	while (err->errno_code != 0 && err->errno_code != code) {
+		err++;
+	}
+
+	return err->libknot_code;
+}
+
+/*!
+ * \brief Get a POSIX errno mapped to Knot error code.
  *
- * \return String containing the error message.
+ * \return Mapped errno or KNOT_ERROR if unknown.
  */
-const char *knot_strerror(int code);
+inline static int knot_map_errno(void)
+{
+	return knot_map_errno_code(-1);
+}
 
 /*! @} */
diff --git a/src/libknot/errcode.c b/src/libknot/error.c
similarity index 99%
rename from src/libknot/errcode.c
rename to src/libknot/error.c
index fefe9caabb..70f36d2e3e 100644
--- a/src/libknot/errcode.c
+++ b/src/libknot/error.c
@@ -19,7 +19,7 @@
 #include <lmdb.h>
 #endif
 
-#include "libknot/errcode.h"
+#include "libknot/error.h"
 #include "libknot/internal/macros.h"
 #include "dnssec/error.h"
 
diff --git a/src/libknot/error.h b/src/libknot/error.h
new file mode 100644
index 0000000000..6721611a0d
--- /dev/null
+++ b/src/libknot/error.h
@@ -0,0 +1,38 @@
+/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/*!
+* \file
+*
+* \brief Error codes and function for getting error message.
+*
+* \addtogroup libknot
+* @{
+*/
+
+#pragma once
+
+#include "libknot/errcode.h"
+
+/*!
+ * \brief Returns error message for the given error code.
+ *
+ * \param code Error code.
+ *
+ * \return String containing the error message.
+ */
+const char *knot_strerror(int code);
+
+/*! @} */
diff --git a/src/libknot/internal/errcode.c b/src/libknot/internal/errcode.c
deleted file mode 100644
index d34875f295..0000000000
--- a/src/libknot/internal/errcode.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "libknot/internal/macros.h"
-#include "libknot/internal/errcode.h"
-
-#define ERR_ITEM(name) { name, KNOT_##name }
-
-typedef struct {
-	int errno_code;
-	int libknot_code;
-} err_table_t;
-
-/*!
- * \brief Errno to libknot error mapping table.
- */
-static const err_table_t errno_to_errcode[] = {
-	ERR_ITEM(ENOMEM),
-	ERR_ITEM(EINVAL),
-	ERR_ITEM(ENOTSUP),
-	ERR_ITEM(EBUSY),
-	ERR_ITEM(EAGAIN),
-	ERR_ITEM(EACCES),
-	ERR_ITEM(ECONNREFUSED),
-	ERR_ITEM(EISCONN),
-	ERR_ITEM(EADDRINUSE),
-	ERR_ITEM(ENOENT),
-	ERR_ITEM(EEXIST),
-	ERR_ITEM(ERANGE),
-	ERR_ITEM(EADDRNOTAVAIL),
-
-	/* Terminator - default value. */
-	{ 0, KNOT_ERROR }
-};
-
-int knot_map_errno_code(int code)
-{
-	if (code < 0) {
-		code = errno;
-	}
-
-	const err_table_t *err = errno_to_errcode;
-
-	while (err->errno_code != 0 && err->errno_code != code) {
-		err++;
-	}
-
-	return err->libknot_code;
-}
-
-int knot_map_errno(void)
-{
-	return knot_map_errno_code(-1);
-}
diff --git a/src/libknot/internal/errcode.h b/src/libknot/internal/errcode.h
deleted file mode 100644
index 8ca7d35ac7..0000000000
--- a/src/libknot/internal/errcode.h
+++ /dev/null
@@ -1,169 +0,0 @@
-/*!
-* \file errcode.h
-*
-* \author Lubos Slovak <lubos.slovak@nic.cz>
-* \author Marek Vavrusa <marek.vavrusa@nic.cz>
-*
-* \brief Error codes and function for getting error message.
-*
-* \addtogroup common_lib
-* @{
-*/
-/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#pragma once
-
-#include <errno.h>
-
-/*! \brief Error codes used in the library. */
-enum knot_error {
-	KNOT_EOK = 0,
-
-	/* Directly mapped error codes. */
-	KNOT_ENOMEM        = -ENOMEM,
-	KNOT_EINVAL        = -EINVAL,
-	KNOT_ENOTSUP       = -ENOTSUP,
-	KNOT_EBUSY         = -EBUSY,
-	KNOT_EAGAIN        = -EAGAIN,
-	KNOT_EACCES        = -EACCES,
-	KNOT_ECONNREFUSED  = -ECONNREFUSED,
-	KNOT_EISCONN       = -EISCONN,
-	KNOT_EADDRINUSE    = -EADDRINUSE,
-	KNOT_ENOENT        = -ENOENT,
-	KNOT_EEXIST        = -EEXIST,
-	KNOT_ERANGE        = -ERANGE,
-	KNOT_EADDRNOTAVAIL = -EADDRNOTAVAIL,
-
-	KNOT_ERROR_MIN = -1000,
-
-	/* General errors. */
-	KNOT_ERROR = KNOT_ERROR_MIN,
-	KNOT_ENOTRUNNING,
-	KNOT_EPARSEFAIL,
-	KNOT_ESEMCHECK,
-	KNOT_EEXPIRED,
-	KNOT_EUPTODATE,
-	KNOT_EFEWDATA,
-	KNOT_ESPACE,
-	KNOT_EMALF,
-	KNOT_ECRYPTO,
-	KNOT_ENSEC3PAR,
-	KNOT_ENSEC3CHAIN,
-	KNOT_EOUTOFZONE,
-	KNOT_EHASH,
-	KNOT_EZONEINVAL,
-	KNOT_EZONENOENT,
-	KNOT_ENOZONE,
-	KNOT_ENONODE,
-	KNOT_ENOMASTER,
-	KNOT_EDNAMEPTR,
-	KNOT_EPAYLOAD,
-	KNOT_EPREREQ,
-	KNOT_ETTL,
-	KNOT_ENOXFR,
-	KNOT_ENOIXFR,
-	KNOT_EXFRREFUSED,
-	KNOT_EDENIED,
-	KNOT_ECONN,
-	KNOT_ETIMEOUT,
-	KNOT_EIXFRSPACE,
-	KNOT_ECNAME,
-	KNOT_ENODIFF,
-	KNOT_EDSDIGESTLEN,
-	KNOT_ENOTSIG,
-	KNOT_ELIMIT,
-	KNOT_EWRITABLE,
-	KNOT_EOF,
-	KNOT_ESYSTEM,
-	KNOT_EFILE,
-
-	/* Control states. */
-	KNOT_CTL_STOP,
-	KNOT_CTL_ACCEPTED,
-	KNOT_CTL_ARG_REQ,
-
-	/* Network errors. */
-	KNOT_NET_EADDR,
-	KNOT_NET_ESOCKET,
-	KNOT_NET_ECONNECT,
-	KNOT_NET_ESEND,
-	KNOT_NET_ERECV,
-	KNOT_NET_ETIMEOUT,
-
-	/* Encoding errors. */
-	KNOT_BASE64_ESIZE,
-	KNOT_BASE64_ECHAR,
-	KNOT_BASE32HEX_ESIZE,
-	KNOT_BASE32HEX_ECHAR,
-
-	/* TSIG errors. */
-	KNOT_TSIG_EBADSIG,
-	KNOT_TSIG_EBADKEY,
-	KNOT_TSIG_EBADTIME,
-	KNOT_TSIG_EBADTRUNC,
-
-	/* Key parsing errors. */
-	KNOT_KEY_EPUBLIC_KEY_OPEN,
-	KNOT_KEY_EPRIVATE_KEY_OPEN,
-	KNOT_KEY_EPUBLIC_KEY_INVALID,
-
-	/* DNSSEC errors. */
-	KNOT_DNSSEC_EMISSINGKEYTYPE,
-	KNOT_DNSSEC_ENOKEY,
-
-	/* Yparser errors. */
-	KNOT_YP_ECHAR_TAB,
-	KNOT_YP_EINVAL_ITEM,
-	KNOT_YP_EINVAL_ID,
-	KNOT_YP_EINVAL_DATA,
-	KNOT_YP_EINVAL_INDENT,
-	KNOT_YP_ENOTSUP_DATA,
-	KNOT_YP_ENOTSUP_ID,
-	KNOT_YP_ENODATA,
-	KNOT_YP_ENOID,
-
-	/* Configuration errors. */
-	KNOT_CONF_EMPTY,
-	KNOT_CONF_EVERSION,
-	KNOT_CONF_EREDEFINE,
-	KNOT_CONF_ETXN,
-	KNOT_CONF_ENOTXN,
-	KNOT_CONF_EMANYTXN,
-
-	/* Processing error. */
-	KNOT_LAYER_ERROR,
-
-	KNOT_ERROR_MAX = -501
-};
-
-/*!
- * \brief Map POSIX errno code to Knot error code.
- *
- * \param code Errno code to transform (set -1 to use the current errno).
- *
- * \return Mapped errno or KNOT_ERROR if unknown.
- */
-int knot_map_errno_code(int code);
-
-/*!
- * \brief Get a POSIX errno mapped to Knot error code.
- *
- * \return Mapped errno or KNOT_ERROR if unknown.
- */
-int knot_map_errno(void);
-
-/*! @} */
diff --git a/src/libknot/internal/hhash.c b/src/libknot/internal/hhash.c
index ae297caa7a..2aee6a0c63 100644
--- a/src/libknot/internal/hhash.c
+++ b/src/libknot/internal/hhash.c
@@ -5,7 +5,7 @@
 #include "libknot/internal/macros.h"
 #include "libknot/internal/hhash.h"
 #include "libknot/internal/trie/murmurhash3.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 /* UCW array sorting defines. */
 static int universal_cmp(uint32_t k1, uint32_t k2, hhash_t *tbl);
diff --git a/src/libknot/internal/namedb/namedb_lmdb.c b/src/libknot/internal/namedb/namedb_lmdb.c
index 96a29b0dd6..266540f425 100644
--- a/src/libknot/internal/namedb/namedb_lmdb.c
+++ b/src/libknot/internal/namedb/namedb_lmdb.c
@@ -23,7 +23,7 @@
 
 #include "libknot/internal/macros.h"
 #include "libknot/internal/namedb/namedb_lmdb.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 #include <lmdb.h>
 
diff --git a/src/libknot/internal/namedb/namedb_trie.c b/src/libknot/internal/namedb/namedb_trie.c
index ebe1331bdc..1590ef9125 100644
--- a/src/libknot/internal/namedb/namedb_trie.c
+++ b/src/libknot/internal/namedb/namedb_trie.c
@@ -17,7 +17,7 @@
 #include <assert.h>
 
 #include "libknot/internal/macros.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 #include "libknot/internal/namedb/namedb_trie.h"
 #include "libknot/internal/trie/hat-trie.h"
 #include "libknot/internal/mempattern.h"
diff --git a/src/libknot/internal/trie/hat-trie.h b/src/libknot/internal/trie/hat-trie.h
index a3d0d7ed81..cc94d09ee6 100644
--- a/src/libknot/internal/trie/hat-trie.h
+++ b/src/libknot/internal/trie/hat-trie.h
@@ -25,7 +25,7 @@ extern "C" {
 #include <stdbool.h>
 #include "libknot/internal/mempattern.h"
 #include "libknot/internal/hhash.h"
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 
 /* Hat-trie defines. */
 #define TRIE_ZEROBUCKETS  0    /* Do not use hash buckets (pure trie). */
diff --git a/src/libknot/libknot.h b/src/libknot/libknot.h
index 642965c294..429d75484e 100644
--- a/src/libknot/libknot.h
+++ b/src/libknot/libknot.h
@@ -29,7 +29,7 @@
 #include "libknot/consts.h"
 #include "libknot/descriptor.h"
 #include "libknot/dname.h"
-#include "libknot/errcode.h"
+#include "libknot/error.h"
 #include "libknot/packet/compr.h"
 #include "libknot/packet/pkt.h"
 #include "libknot/packet/wire.h"
diff --git a/src/libknot/yparser/yparser.c b/src/libknot/yparser/yparser.c
index ec052c0ff0..3ad24a3dd6 100644
--- a/src/libknot/yparser/yparser.c
+++ b/src/libknot/yparser/yparser.c
@@ -23,7 +23,7 @@
 
 #include "libknot/internal/macros.h"
 #include "libknot/yparser/yparser.h"
-#include "libknot/libknot.h"
+#include "libknot/errcode.h"
 
 extern int _yp_start_state;
 extern int _yp_parse(yp_parser_t *parser);
diff --git a/src/libknot/yparser/ypbody.c b/src/libknot/yparser/ypbody.c
index d7db332df5..22ec7bbfec 100644
--- a/src/libknot/yparser/ypbody.c
+++ b/src/libknot/yparser/ypbody.c
@@ -18,7 +18,7 @@
 #include <string.h>
 
 #include "libknot/yparser/yparser.h"
-#include "libknot/libknot.h"
+#include "libknot/errcode.h"
 
 
 
diff --git a/src/libknot/yparser/ypbody.rl b/src/libknot/yparser/ypbody.rl
index 9ef1ac5c14..2d523bd0c9 100644
--- a/src/libknot/yparser/ypbody.rl
+++ b/src/libknot/yparser/ypbody.rl
@@ -17,7 +17,7 @@
 #include <string.h>
 
 #include "libknot/yparser/yparser.h"
-#include "libknot/libknot.h"
+#include "libknot/errcode.h"
 
 %%{
 	machine yparser;
diff --git a/src/libknot/yparser/ypformat.c b/src/libknot/yparser/ypformat.c
index 09c1f678a3..a66553f836 100644
--- a/src/libknot/yparser/ypformat.c
+++ b/src/libknot/yparser/ypformat.c
@@ -16,7 +16,7 @@
 
 #include "libknot/internal/macros.h"
 #include "libknot/yparser/yptrafo.h"
-#include "libknot/libknot.h"
+#include "libknot/errcode.h"
 
 static int format_item(
 	const yp_item_t *item,
diff --git a/src/libknot/yparser/ypscheme.c b/src/libknot/yparser/ypscheme.c
index ff2c7329f8..f79dfb1812 100644
--- a/src/libknot/yparser/ypscheme.c
+++ b/src/libknot/yparser/ypscheme.c
@@ -14,12 +14,13 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <assert.h>
 #include <stdlib.h>
 
 #include "libknot/internal/macros.h"
 #include "libknot/yparser/ypscheme.h"
 #include "libknot/yparser/yptrafo.h"
-#include "libknot/libknot.h"
+#include "libknot/errcode.h"
 
 /*! Initializes the referenced item. */
 static int set_ref_item(
diff --git a/src/libknot/yparser/yptrafo.c b/src/libknot/yparser/yptrafo.c
index 31da538347..bf5746c85e 100644
--- a/src/libknot/yparser/yptrafo.c
+++ b/src/libknot/yparser/yptrafo.c
@@ -22,7 +22,8 @@
 
 #include "libknot/yparser/yptrafo.h"
 #include "libknot/internal/macros.h"
-#include "libknot/libknot.h"
+#include "libknot/consts.h"
+#include "libknot/dname.h"
 #include "contrib/base64.h"
 #include "contrib/sockaddr.h"
 #include "contrib/wire_ctx.h"
diff --git a/tests/wire_ctx.c b/tests/wire_ctx.c
index bb974b5a6f..07a00a09cc 100644
--- a/tests/wire_ctx.c
+++ b/tests/wire_ctx.c
@@ -18,7 +18,7 @@
 #include <netinet/in.h>
 #include <stdio.h>
 
-#include "libknot/internal/errcode.h"
+#include "libknot/errcode.h"
 #include "contrib/wire_ctx.h"
 
 #define OK(wire) { \
-- 
GitLab