Cygwin: net.cc: convert wsock_errmap into a simple array of error codes

Avoid linear searches for error codes by converting wsock_errmap
to a ordered array, indexed by WinSock error code (subtracted by
WSABASEERR.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2022-08-04 11:45:58 +02:00
parent a8c1dd2ce0
commit 70c7e8c1ba
1 changed files with 82 additions and 52 deletions

View File

@ -134,63 +134,93 @@ inet_makeaddr (int net, int lna)
return in; return in;
} }
static const errmap_t wsock_errmap[] = { static const int wsock_errmap[] =
{WSA_INVALID_HANDLE, "WSA_INVALID_HANDLE", EBADF}, {
{WSA_NOT_ENOUGH_MEMORY, "WSA_NOT_ENOUGH_MEMORY", ENOMEM}, 0, /* WSABASEERR (10000) */
{WSA_INVALID_PARAMETER, "WSA_INVALID_PARAMETER", EINVAL}, 0, /* 10001 */
{WSAEINTR, "WSAEINTR", EINTR}, 0, /* 10002 */
{WSAEWOULDBLOCK, "WSAEWOULDBLOCK", EWOULDBLOCK}, 0, /* 10003 */
{WSAEINPROGRESS, "WSAEINPROGRESS", EINPROGRESS}, EINTR, /* WSAEINTR */
{WSAEALREADY, "WSAEALREADY", EALREADY}, 0, /* 10005 */
{WSAENOTSOCK, "WSAENOTSOCK", ENOTSOCK}, 0, /* 10006 */
{WSAEDESTADDRREQ, "WSAEDESTADDRREQ", EDESTADDRREQ}, 0, /* 10007 */
{WSAEMSGSIZE, "WSAEMSGSIZE", EMSGSIZE}, 0, /* 10008 */
{WSAEPROTOTYPE, "WSAEPROTOTYPE", EPROTOTYPE}, EBADF, /* WSAEBADF */
{WSAENOPROTOOPT, "WSAENOPROTOOPT", ENOPROTOOPT}, 0, /* 10010 */
{WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT", EPROTONOSUPPORT}, 0, /* 10011 */
{WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT", ESOCKTNOSUPPORT}, 0, /* 10012 */
{WSAEOPNOTSUPP, "WSAEOPNOTSUPP", EOPNOTSUPP}, EACCES, /* WSAEACCES */
{WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT", EPFNOSUPPORT}, EFAULT, /* WSAEFAULT */
{WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT", EAFNOSUPPORT}, 0, /* 10015 */
{WSAEADDRINUSE, "WSAEADDRINUSE", EADDRINUSE}, 0, /* 10016 */
{WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL", EADDRNOTAVAIL}, 0, /* 10017 */
{WSAENETDOWN, "WSAENETDOWN", ENETDOWN}, 0, /* 10018 */
{WSAENETUNREACH, "WSAENETUNREACH", ENETUNREACH}, 0, /* 10019 */
{WSAENETRESET, "WSAENETRESET", ENETRESET}, 0, /* 10020 */
{WSAECONNABORTED, "WSAECONNABORTED", ECONNABORTED}, 0, /* 10021 */
{WSAECONNRESET, "WSAECONNRESET", ECONNRESET}, EINVAL, /* WSAEINVAL */
{WSAENOBUFS, "WSAENOBUFS", ENOBUFS}, 0, /* 10023 */
{WSAEISCONN, "WSAEISCONN", EISCONN}, EMFILE, /* WSAEMFILE */
{WSAENOTCONN, "WSAENOTCONN", ENOTCONN}, 0, /* 10025 */
{WSAESHUTDOWN, "WSAESHUTDOWN", ESHUTDOWN}, 0, /* 10026 */
{WSAETOOMANYREFS, "WSAETOOMANYREFS", ETOOMANYREFS}, 0, /* 10027 */
{WSAETIMEDOUT, "WSAETIMEDOUT", ETIMEDOUT}, 0, /* 10028 */
{WSAECONNREFUSED, "WSAECONNREFUSED", ECONNREFUSED}, 0, /* 10029 */
{WSAELOOP, "WSAELOOP", ELOOP}, 0, /* 10030 */
{WSAENAMETOOLONG, "WSAENAMETOOLONG", ENAMETOOLONG}, 0, /* 10031 */
{WSAEHOSTDOWN, "WSAEHOSTDOWN", EHOSTDOWN}, 0, /* 10032 */
{WSAEHOSTUNREACH, "WSAEHOSTUNREACH", EHOSTUNREACH}, 0, /* 10033 */
{WSAENOTEMPTY, "WSAENOTEMPTY", ENOTEMPTY}, 0, /* 10034 */
{WSAEPROCLIM, "WSAEPROCLIM", EPROCLIM}, EWOULDBLOCK, /* WSAEWOULDBLOCK */
{WSAEUSERS, "WSAEUSERS", EUSERS}, EINPROGRESS, /* WSAEINPROGRESS */
{WSAEDQUOT, "WSAEDQUOT", EDQUOT}, EALREADY, /* WSAEALREADY */
{WSAESTALE, "WSAESTALE", ESTALE}, ENOTSOCK, /* WSAENOTSOCK */
{WSAEREMOTE, "WSAEREMOTE", EREMOTE}, EDESTADDRREQ, /* WSAEDESTADDRREQ */
{WSAEINVAL, "WSAEINVAL", EINVAL}, EMSGSIZE, /* WSAEMSGSIZE */
{WSAEFAULT, "WSAEFAULT", EFAULT}, EPROTOTYPE, /* WSAEPROTOTYPE */
{WSAEBADF, "WSAEBADF", EBADF}, ENOPROTOOPT, /* WSAENOPROTOOPT */
{WSAEACCES, "WSAEACCES", EACCES}, EPROTONOSUPPORT, /* WSAEPROTONOSUPPORT */
{WSAEMFILE, "WSAEMFILE", EMFILE}, ESOCKTNOSUPPORT, /* WSAESOCKTNOSUPPORT */
{0, "NOERROR", 0}, EOPNOTSUPP, /* WSAEOPNOTSUPP */
{0, NULL, 0} EPFNOSUPPORT, /* WSAEPFNOSUPPORT */
EAFNOSUPPORT, /* WSAEAFNOSUPPORT */
EADDRINUSE, /* WSAEADDRINUSE */
EADDRNOTAVAIL, /* WSAEADDRNOTAVAIL */
ENETDOWN, /* WSAENETDOWN */
ENETUNREACH, /* WSAENETUNREACH */
ENETRESET, /* WSAENETRESET */
ECONNABORTED, /* WSAECONNABORTED */
ECONNRESET, /* WSAECONNRESET */
ENOBUFS, /* WSAENOBUFS */
EISCONN, /* WSAEISCONN */
ENOTCONN, /* WSAENOTCONN */
ESHUTDOWN, /* WSAESHUTDOWN */
ETOOMANYREFS, /* WSAETOOMANYREFS */
ETIMEDOUT, /* WSAETIMEDOUT */
ECONNREFUSED, /* WSAECONNREFUSED */
ELOOP, /* WSAELOOP */
ENAMETOOLONG, /* WSAENAMETOOLONG */
EHOSTDOWN, /* WSAEHOSTDOWN */
EHOSTUNREACH, /* WSAEHOSTUNREACH */
ENOTEMPTY, /* WSAENOTEMPTY */
EPROCLIM, /* WSAEPROCLIM */
EUSERS, /* WSAEUSERS */
EDQUOT, /* WSAEDQUOT */
ESTALE, /* WSAESTALE */
EREMOTE, /* WSAEREMOTE */
}; };
int int
find_winsock_errno (DWORD why) find_winsock_errno (DWORD why)
{ {
for (int i = 0; wsock_errmap[i].s != NULL; ++i) if (!why)
if (why == wsock_errmap[i].w) return 0;
return wsock_errmap[i].e; if (why < WSABASEERR)
geterrno_from_win_error (why, EACCES);
why -= WSABASEERR;
if (why < sizeof wsock_errmap / sizeof wsock_errmap[0])
return wsock_errmap[why];
return EACCES; return EACCES;
} }