xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/netmgr/uverr2result.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /*	$NetBSD: uverr2result.c,v 1.1 2024/02/18 20:57:55 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include <stdbool.h>
17 #include <uv.h>
18 
19 #include <isc/result.h>
20 #include <isc/strerr.h>
21 #include <isc/string.h>
22 #include <isc/util.h>
23 
24 #include "netmgr-int.h"
25 
26 /*%
27  * Convert a libuv error value into an isc_result_t.  The
28  * list of supported error values is not complete; new users
29  * of this function should add any expected errors that are
30  * not already there.
31  */
32 isc_result_t
33 isc___nm_uverr2result(int uverr, bool dolog, const char *file,
34 		      unsigned int line, const char *func) {
35 	switch (uverr) {
36 	case 0:
37 		return (ISC_R_SUCCESS);
38 	case UV_ENOTDIR:
39 	case UV_ELOOP:
40 	case UV_EINVAL: /* XXX sometimes this is not for files */
41 	case UV_ENAMETOOLONG:
42 	case UV_EBADF:
43 		return (ISC_R_INVALIDFILE);
44 	case UV_ENOENT:
45 		return (ISC_R_FILENOTFOUND);
46 	case UV_EAGAIN:
47 		return (ISC_R_NOCONN);
48 	case UV_EACCES:
49 	case UV_EPERM:
50 		return (ISC_R_NOPERM);
51 	case UV_EEXIST:
52 		return (ISC_R_FILEEXISTS);
53 	case UV_EIO:
54 		return (ISC_R_IOERROR);
55 	case UV_ENOMEM:
56 		return (ISC_R_NOMEMORY);
57 	case UV_ENFILE:
58 	case UV_EMFILE:
59 		return (ISC_R_TOOMANYOPENFILES);
60 	case UV_ENOSPC:
61 		return (ISC_R_DISCFULL);
62 	case UV_EPIPE:
63 	case UV_ECONNRESET:
64 	case UV_ECONNABORTED:
65 		return (ISC_R_CONNECTIONRESET);
66 	case UV_ENOTCONN:
67 		return (ISC_R_NOTCONNECTED);
68 	case UV_ETIMEDOUT:
69 		return (ISC_R_TIMEDOUT);
70 	case UV_ENOBUFS:
71 		return (ISC_R_NORESOURCES);
72 	case UV_EAFNOSUPPORT:
73 		return (ISC_R_FAMILYNOSUPPORT);
74 	case UV_ENETDOWN:
75 		return (ISC_R_NETDOWN);
76 	case UV_EHOSTDOWN:
77 		return (ISC_R_HOSTDOWN);
78 	case UV_ENETUNREACH:
79 		return (ISC_R_NETUNREACH);
80 	case UV_EHOSTUNREACH:
81 		return (ISC_R_HOSTUNREACH);
82 	case UV_EADDRINUSE:
83 		return (ISC_R_ADDRINUSE);
84 	case UV_EADDRNOTAVAIL:
85 		return (ISC_R_ADDRNOTAVAIL);
86 	case UV_ECONNREFUSED:
87 		return (ISC_R_CONNREFUSED);
88 	case UV_ECANCELED:
89 		return (ISC_R_CANCELED);
90 	case UV_EOF:
91 		return (ISC_R_EOF);
92 	case UV_EMSGSIZE:
93 		return (ISC_R_MAXSIZE);
94 	case UV_ENOTSUP:
95 		return (ISC_R_FAMILYNOSUPPORT);
96 	default:
97 		if (dolog) {
98 			UNEXPECTED_ERROR(
99 				file, line,
100 				"unable to convert libuv "
101 				"error code in %s to isc_result: %d: %s",
102 				func, uverr, uv_strerror(uverr));
103 		}
104 		return (ISC_R_UNEXPECTED);
105 	}
106 }
107