Skip to content
Snippets Groups Projects
Commit b9c98746 authored by Ondřej Surý's avatar Ondřej Surý
Browse files

Move knot_map_errno_internal to libknot-int

parent a7399911
No related branches found
No related tags found
1 merge request!350Separate yparser
......@@ -33,6 +33,7 @@ nobase_libknot_int_la_HEADERS = \
libknot/internal/base64.h \
libknot/internal/binsearch.h \
libknot/internal/endian.h \
libknot/internal/errcode.h \
libknot/internal/getline.h \
libknot/internal/heap.h \
libknot/internal/hhash.h \
......@@ -65,6 +66,7 @@ nobase_libknot_yparser_la_HEADERS = \
libknot_int_la_SOURCES = \
libknot/internal/base32hex.c \
libknot/internal/base64.c \
libknot/internal/errcode.c \
libknot/internal/getline.c \
libknot/internal/heap.c \
libknot/internal/hhash.c \
......
......@@ -29,6 +29,7 @@
#include "libknot/internal/strlcpy.h"
#include "libknot/internal/mem.h"
#include "libknot/internal/macros.h"
#include "libknot/errcode.h"
#include "knot/conf/conf.h"
#include "knot/conf/extra.h"
#include "knot/ctl/remote.h"
......
......@@ -22,6 +22,7 @@
#endif
#include "libknot/errcode.h"
#include "libknot/internal/errcode.h"
#include "libknot/internal/macros.h"
#include "dnssec/error.h"
......@@ -185,24 +186,13 @@ const char *knot_strerror(int code)
}
_public_
int knot_map_errno_internal(int fallback, int arg0, ...)
int knot_map_errno(int arg0, ...)
{
/* Iterate all variable-length arguments. */
va_list ap;
va_start(ap, arg0);
/* KNOT_ERROR serves as a sentinel. */
for (int c = arg0; c != 0; c = va_arg(ap, int)) {
/* Error code matches with mapped. */
if (c == errno) {
/* Return negative value of the code. */
va_end(ap);
return -abs(c);
}
}
int ret = knot_map_errno_internal(KNOT_ERROR, arg0, ap);
va_end(ap);
/* Fallback error code. */
return KNOT_ERROR;
return ret;
}
......@@ -28,114 +28,7 @@ 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_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,
/* 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_EINVAL_ITEM,
KNOT_YP_EINVAL_DATA,
KNOT_YP_ENOTSUP_DATA,
KNOT_YP_ENOTSUP_ID,
KNOT_YP_ENODATA,
KNOT_YP_ENOID,
/* Processing error. */
KNOT_LAYER_ERROR,
KNOT_ERROR_MAX = -501
};
#include <libknot/internal/errcode.h>
/*!
* \brief Returns error message for the given error code.
......@@ -146,23 +39,11 @@ enum knot_error {
*/
const char *knot_strerror(int code);
/*!
* \brief Get a POSIX errno mapped to Knot error code.
*
* \internal
*
* \param fallback Falback error code.
* \param arg0... Error codes allowed for lookup, list must be terminated by 0.
*
* \return Mapped errno or fallback error code.
*/
int knot_map_errno_internal(int fallback, int arg0, ...);
/*!
* \brief Map POSIX errno to Knot error code.
*
* KNOT_ERROR is used as a fallback error, the list is terminated implicitly.
*/
#define knot_map_errno(errors...) knot_map_errno_internal(KNOT_ERROR, errors, 0)
int knot_map_errno(int arg0, ...);
/*! @} */
/* 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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_LMDB
#include <lmdb.h>
#endif
#include "libknot/internal/errcode.h"
#include "libknot/internal/macros.h"
_public_
int knot_map_errno_internal(int fallback, int arg0, ...)
{
/* Iterate all variable-length arguments. */
va_list ap;
va_start(ap, arg0);
/* KNOT_ERROR serves as a sentinel. */
for (int c = arg0; c != 0; c = va_arg(ap, int)) {
/* Error code matches with mapped. */
if (c == errno) {
/* Return negative value of the code. */
va_end(ap);
return -abs(c);
}
}
va_end(ap);
/* Fallback error code. */
return KNOT_ERROR;
}
/*!
* \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_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,
/* 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_EINVAL_ITEM,
KNOT_YP_EINVAL_DATA,
KNOT_YP_ENOTSUP_DATA,
KNOT_YP_ENOTSUP_ID,
KNOT_YP_ENODATA,
KNOT_YP_ENOID,
/* Processing error. */
KNOT_LAYER_ERROR,
KNOT_ERROR_MAX = -501
};
/*!
* \brief Get a POSIX errno mapped to Knot error code.
*
* \internal
*
* \param fallback Falback error code.
* \param arg0... Error codes allowed for lookup, list must be terminated by 0.
*
* \return Mapped errno or fallback error code.
*/
int knot_map_errno_internal(int fallback, int arg0, ...);
/*! @} */
......@@ -34,14 +34,14 @@
#include <assert.h>
#include "libknot/internal/net.h"
#include "libknot/errcode.h"
#include "libknot/internal/errcode.h"
static int socket_create(int family, int type, int proto)
{
/* Create socket. */
int ret = socket(family, type, proto);
if (ret < 0) {
return knot_map_errno(EACCES, EINVAL, ENOMEM);
return knot_map_errno_internal(KNOT_ERROR, EACCES, EINVAL, ENOMEM);
}
return ret;
......@@ -135,7 +135,7 @@ int net_bound_socket(int type, const struct sockaddr_storage *ss,
const struct sockaddr *sa = (const struct sockaddr *)ss;
int ret = bind(socket, sa, sockaddr_len(sa));
if (ret < 0) {
ret = knot_map_errno(EADDRINUSE, EADDRNOTAVAIL, EINVAL, EACCES, ENOMEM);
ret = knot_map_errno_internal(KNOT_ERROR, EADDRINUSE, EADDRNOTAVAIL, EINVAL, EACCES, ENOMEM);
close(socket);
return ret;
}
......@@ -176,7 +176,7 @@ int net_connected_socket(int type, const struct sockaddr_storage *dst_addr,
int ret = connect(socket, sa, sockaddr_len(sa));
if (ret != 0 && errno != EINPROGRESS) {
close(socket);
return knot_map_errno(EACCES, EADDRINUSE, EAGAIN,
return knot_map_errno_internal(KNOT_ERROR, EACCES, EADDRINUSE, EAGAIN,
ECONNREFUSED, EISCONN);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment