1/*	$NetBSD: errno2result.c,v 1.2.6.1 2012/06/05 21:15:31 bouyer Exp $	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2002  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: errno2result.c,v 1.17 2008/09/12 04:46:25 marka Exp  */
21
22#include <config.h>
23
24#include <winsock2.h>
25#include "errno2result.h"
26#include <isc/result.h>
27#include <isc/strerror.h>
28#include <isc/util.h>
29
30/*
31 * Convert a POSIX errno value into an isc_result_t.  The
32 * list of supported errno values is not complete; new users
33 * of this function should add any expected errors that are
34 * not already there.
35 */
36isc_result_t
37isc__errno2resultx(int posixerrno, const char *file, int line) {
38	char strbuf[ISC_STRERRORSIZE];
39
40	switch (posixerrno) {
41	case ENOTDIR:
42	case WSAELOOP:
43	case WSAEINVAL:
44	case EINVAL:		/* XXX sometimes this is not for files */
45	case ENAMETOOLONG:
46	case WSAENAMETOOLONG:
47	case EBADF:
48	case WSAEBADF:
49		return (ISC_R_INVALIDFILE);
50	case ENOENT:
51		return (ISC_R_FILENOTFOUND);
52	case EACCES:
53	case WSAEACCES:
54	case EPERM:
55		return (ISC_R_NOPERM);
56	case EEXIST:
57		return (ISC_R_FILEEXISTS);
58	case EIO:
59		return (ISC_R_IOERROR);
60	case ENOMEM:
61		return (ISC_R_NOMEMORY);
62	case ENFILE:
63	case EMFILE:
64	case WSAEMFILE:
65		return (ISC_R_TOOMANYOPENFILES);
66	case ERROR_CANCELLED:
67		return (ISC_R_CANCELED);
68	case ERROR_CONNECTION_REFUSED:
69	case WSAECONNREFUSED:
70		return (ISC_R_CONNREFUSED);
71	case WSAENOTCONN:
72	case ERROR_CONNECTION_INVALID:
73		return (ISC_R_NOTCONNECTED);
74	case ERROR_HOST_UNREACHABLE:
75	case WSAEHOSTUNREACH:
76		return (ISC_R_HOSTUNREACH);
77	case ERROR_NETWORK_UNREACHABLE:
78	case WSAENETUNREACH:
79		return (ISC_R_NETUNREACH);
80	case ERROR_NO_NETWORK:
81		return (ISC_R_NETUNREACH);
82	case ERROR_PORT_UNREACHABLE:
83		return (ISC_R_HOSTUNREACH);
84	case ERROR_SEM_TIMEOUT:
85		return (ISC_R_TIMEDOUT);
86	case WSAECONNRESET:
87	case WSAENETRESET:
88	case WSAECONNABORTED:
89	case WSAEDISCON:
90	case ERROR_OPERATION_ABORTED:
91	case ERROR_CONNECTION_ABORTED:
92	case ERROR_REQUEST_ABORTED:
93		return (ISC_R_CONNECTIONRESET);
94	case WSAEADDRNOTAVAIL:
95		return (ISC_R_ADDRNOTAVAIL);
96	case ERROR_NETNAME_DELETED:
97	case WSAENETDOWN:
98		return (ISC_R_NETUNREACH);
99	case WSAEHOSTDOWN:
100		return (ISC_R_HOSTUNREACH);
101	case WSAENOBUFS:
102		return (ISC_R_NORESOURCES);
103	default:
104		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
105		UNEXPECTED_ERROR(file, line, "unable to convert errno "
106				 "to isc_result: %d: %s", posixerrno, strbuf);
107		/*
108		 * XXXDCL would be nice if perhaps this function could
109		 * return the system's error string, so the caller
110		 * might have something more descriptive than "unexpected
111		 * error" to log with.
112		 */
113		return (ISC_R_UNEXPECTED);
114	}
115}
116