Skip to content

Commit 725cb81

Browse files
committed
CORE: SOCKETAPI/Windows: using native error query
1 parent b3c7dce commit 725cb81

File tree

2 files changed

+24
-75
lines changed

2 files changed

+24
-75
lines changed

xemu/emutools_socketapi.c

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -44,81 +44,30 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
4444
#endif
4545

4646
#ifdef XEMU_ARCH_WIN
47-
// FIXME: maybe migrate this to Windows' FormatMessage() at some point?
48-
const char *xemusock_strerror ( int err )
47+
const char *xemusock_strerror ( const int err )
4948
{
50-
switch (err) {
51-
case 0: return "Success (0)";
52-
case WSA_INVALID_HANDLE: return "Invalid handle (INVALID_HANDLE)";
53-
case WSA_NOT_ENOUGH_MEMORY: return "Not enough memory (NOT_ENOUGH_MEMORY)";
54-
case WSA_INVALID_PARAMETER: return "Invalid parameter (INVALID_PARAMETER)";
55-
case WSA_OPERATION_ABORTED: return "Operation aborted (OPERATION_ABORTED)";
56-
case WSA_IO_INCOMPLETE: return "IO incomplete (IO_INCOMPLETE)";
57-
case WSA_IO_PENDING: return "IO pending (IO_PENDING)";
58-
case WSAEINTR: return "Interrupted (EINTR)";
59-
case WSAEBADF: return "Invalid handle (EBADF)";
60-
case WSAEACCES: return "Permission denied (EACCES)";
61-
case WSAEFAULT: return "Bad address (EFAULT)";
62-
case WSAEINVAL: return "Invaild argument (EINVAL)";
63-
case WSAEMFILE: return "Too many files (EMFILE)";
64-
case WSAEWOULDBLOCK: return "Resource temporarily unavailable (EWOULDBLOCK)";
65-
case WSAEINPROGRESS: return "Operation now in progress (EINPROGRESS)";
66-
case WSAEALREADY: return "Operation already in progress (EALREADY)";
67-
case WSAENOTSOCK: return "Socket operation on nonsocket (ENOTSOCK)";
68-
case WSAEDESTADDRREQ: return "Destination address required (EDESTADDRREQ)";
69-
case WSAEMSGSIZE: return "Message too long (EMSGSIZE)";
70-
case WSAEPROTOTYPE: return "Protocol wrong type for socket (EPROTOTYPE)";
71-
case WSAENOPROTOOPT: return "Bad protocol option (ENOPROTOOPT)";
72-
case WSAEPROTONOSUPPORT: return "Protocol not supported (EPROTONOSUPPORT)";
73-
case WSAESOCKTNOSUPPORT: return "Socket type not supported (ESOCKTNOSUPPORT)";
74-
case WSAEOPNOTSUPP: return "Operation not supported (EOPNOTSUPP)";
75-
case WSAEPFNOSUPPORT: return "Protocol family not supported (EPFNOSUPPORT)";
76-
case WSAEAFNOSUPPORT: return "Address family not supported by protocol family (EAFNOSUPPORT)";
77-
case WSAEADDRINUSE: return "Address already in use (EADDRINUSE)";
78-
case WSAEADDRNOTAVAIL: return "Cannot assign requested address (EADDRNOTAVAIL)";
79-
case WSAENETDOWN: return "Network is down (ENETDOWN)";
80-
case WSAENETUNREACH: return "Network is unreachable (ENETUNREACH)";
81-
case WSAENETRESET: return "Network dropped connection on reset (ENETRESET)";
82-
case WSAECONNABORTED: return "Software caused connection abort (ECONNABORTED)";
83-
case WSAECONNRESET: return "Connection reset by peer (ECONNRESET)";
84-
case WSAENOBUFS: return "No buffer space available (ENOBUFS)";
85-
case WSAEISCONN: return "Socket is already connected (EISCONN)";
86-
case WSAENOTCONN: return "Socket is not connected (ENOTCONN)";
87-
case WSAESHUTDOWN: return "Cannot send after socket shutdown (ESHUTDOWN)";
88-
case WSAETOOMANYREFS: return "Too many references (ETOOMANYREFS)";
89-
case WSAETIMEDOUT: return "Connection timed out (ETIMEDOUT)";
90-
case WSAECONNREFUSED: return "Connection refused (ECONNREFUSED)";
91-
case WSAELOOP: return "Cannot translate name (ELOOP)";
92-
case WSAENAMETOOLONG: return "Name too long (ENAMETOOLONG)";
93-
case WSAEHOSTDOWN: return "Host is down (EHOSTDOWN)";
94-
case WSAEHOSTUNREACH: return "No route to host (EHOSTUNREACH)";
95-
case WSAENOTEMPTY: return "Directory not empty (ENOTEMPTY)";
96-
case WSAEPROCLIM: return "Too many processes (EPROCLIM)";
97-
case WSAEUSERS: return "User quota exceeded (EUSERS)";
98-
case WSAEDQUOT: return "Disk quota exceeded (EDQUOT)";
99-
case WSAESTALE: return "Stale file handle reference (ESTALE)";
100-
case WSAEREMOTE: return "Item is remote (EREMOTE)";
101-
case WSASYSNOTREADY: return "Network subsystem is unavailable (SYSNOTREADY)";
102-
case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range (VERNOTSUPPORTED)";
103-
case WSANOTINITIALISED: return "Successful WSAStartup not yet performed (NOTINITIALISED)";
104-
case WSAEDISCON: return "Graceful shutdown in progress (EDISCON)";
105-
case WSAENOMORE: return "No more results (ENOMORE)";
106-
case WSAECANCELLED: return "Call has been canceled (WSAECANCELLED)";
107-
case WSAEINVALIDPROCTABLE: return "Procedure call table is invalid (EINVALIDPROCTABLE)";
108-
case WSAEINVALIDPROVIDER: return "Service provider is invalid (EINVALIDPROVIDER)";
109-
case WSAEPROVIDERFAILEDINIT: return "Service provider failed to initialize (EPROVIDERFAILEDINIT)";
110-
case WSASYSCALLFAILURE: return "System call failure (SYSCALLFAILURE)";
111-
case WSASERVICE_NOT_FOUND: return "Service not found (SERVICE_NOT_FOUND)";
112-
case WSATYPE_NOT_FOUND: return "Class type not found (TYPE_NOT_FOUND)";
113-
case WSA_E_NO_MORE: return "No more results (E_NO_MORE)";
114-
case WSA_E_CANCELLED: return "Call was canceled (E_CANCELLED)";
115-
case WSAEREFUSED: return "Database query was refused (EREFUSED)";
116-
case WSAHOST_NOT_FOUND: return "Host not found (HOST_NOT_FOUND)";
117-
case WSATRY_AGAIN: return "Nonauthoritative host not found (TRY_AGAIN)";
118-
case WSANO_RECOVERY: return "This is a nonrecoverable error (NO_RECOVERY)";
119-
case WSANO_DATA: return "Valid name, no data record of requested type (NO_DATA)";
120-
default: return "Unknown Winsock error";
121-
}
49+
wchar_t wbuf[512];
50+
static char buf[512];
51+
const DWORD len = FormatMessageW(
52+
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
53+
NULL,
54+
err,
55+
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // English, if possible
56+
wbuf,
57+
sizeof(wbuf) / sizeof(wbuf[0]),
58+
NULL
59+
);
60+
if (!len)
61+
goto unknown;
62+
const int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, sizeof(buf), NULL, NULL);
63+
if (!utf8_len)
64+
goto unknown;
65+
for (char *p = buf + strlen(buf); --p >= buf && (*p <= 32); )
66+
*p = '\0';
67+
return buf;
68+
unknown:
69+
snprintf(buf, sizeof buf, "Unknown WSA error %d", err);
70+
return buf;
12271
}
12372
#endif
12473

xemu/emutools_socketapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
3333
# define XS_INVALID_SOCKET INVALID_SOCKET
3434
# define XS_SOCKET_ERROR SOCKET_ERROR
3535
# define SHUT_RDWR SD_BOTH
36-
extern const char *xemusock_strerror ( int err );
36+
extern const char *xemusock_strerror ( const int err );
3737
#else
3838
# include <arpa/inet.h>
3939
# include <sys/socket.h>

0 commit comments

Comments
 (0)