0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000-2002, 2004, 2005, 2007, 2011-2013, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id$ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence#include <config.h>
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include <isc/result.h>
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews#include <isc/strerror.h>
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews#include <isc/util.h>
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include "errno2result.h"
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * Convert a POSIX errno value into an isc_result_t. The
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * list of supported errno values is not complete; new users
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * of this function should add any expected errors that are
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * not already there.
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrenceisc_result_t
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Huntisc___errno2result(int posixerrno, isc_boolean_t dolog,
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt const char *file, unsigned int line)
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt{
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews char strbuf[ISC_STRERRORSIZE];
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence switch (posixerrno) {
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case ENOTDIR:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case ELOOP:
1ab0c9558f167cac2fc6a486547334d56930b81eDavid Lawrence case EINVAL: /* XXX sometimes this is not for files */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case ENAMETOOLONG:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case EBADF:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_INVALIDFILE);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case ENOENT:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_FILENOTFOUND);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case EACCES:
1ab0c9558f167cac2fc6a486547334d56930b81eDavid Lawrence case EPERM:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_NOPERM);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case EEXIST:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_FILEEXISTS);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case EIO:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_IOERROR);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case ENOMEM:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_NOMEMORY);
a09e70c94769dcb03c47b89667b591950a29bcb7Automatic Updater case ENFILE:
fcf28b759c65388e8875f2dbcf8eb5fa786b29ecAndreas Gustafsson case EMFILE:
fcf28b759c65388e8875f2dbcf8eb5fa786b29ecAndreas Gustafsson return (ISC_R_TOOMANYOPENFILES);
31707708c585c53b61ca1edb2e224e6bb1b985a5Evan Hunt#ifdef EOVERFLOW
31707708c585c53b61ca1edb2e224e6bb1b985a5Evan Hunt case EOVERFLOW:
31707708c585c53b61ca1edb2e224e6bb1b985a5Evan Hunt return (ISC_R_RANGE);
31707708c585c53b61ca1edb2e224e6bb1b985a5Evan Hunt#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case EPIPE:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ECONNRESET
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ECONNRESET:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ECONNABORTED
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ECONNABORTED:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_CONNECTIONRESET);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ENOTCONN
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ENOTCONN:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_NOTCONNECTED);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ETIMEDOUT
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ETIMEDOUT:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_TIMEDOUT);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ENOBUFS
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ENOBUFS:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_NORESOURCES);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef EAFNOSUPPORT
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case EAFNOSUPPORT:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_FAMILYNOSUPPORT);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ENETDOWN
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ENETDOWN:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_NETDOWN);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef EHOSTDOWN
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case EHOSTDOWN:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_HOSTDOWN);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef ENETUNREACH
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case ENETUNREACH:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_NETUNREACH);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef EHOSTUNREACH
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case EHOSTUNREACH:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_HOSTUNREACH);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#ifdef EADDRINUSE
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case EADDRINUSE:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson return (ISC_R_ADDRINUSE);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson#endif
c00a1eb423623442aff428336bb55590f79013bbMark Andrews case EADDRNOTAVAIL:
c00a1eb423623442aff428336bb55590f79013bbMark Andrews return (ISC_R_ADDRNOTAVAIL);
ac7098295643c4ba83383e2faf44cd13bee9b485Mark Andrews case ECONNREFUSED:
ac7098295643c4ba83383e2faf44cd13bee9b485Mark Andrews return (ISC_R_CONNREFUSED);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence default:
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt if (dolog) {
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt isc__strerror(posixerrno, strbuf, sizeof(strbuf));
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt UNEXPECTED_ERROR(file, line, "unable to convert errno "
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt "to isc_result: %d: %s",
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt posixerrno, strbuf);
f5898cf3484b1588d5239faa4062a2b2b606ce91Evan Hunt }
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence /*
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * XXXDCL would be nice if perhaps this function could
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * return the system's error string, so the caller
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * might have something more descriptive than "unexpected
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * error" to log with.
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence return (ISC_R_UNEXPECTED);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence }
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence}