Commit 1089beae authored by rswindell's avatar rswindell
Browse files

Define and use socket_errno and socket_strerror() to include helpful error

descriptions in bind failure error messages in a cross-platform
(i.e. WinSock-compatible) manner.
parent 2b7963b4
......@@ -361,7 +361,7 @@ int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen
break;
if(lprintf!=NULL)
lprintf(i<retries ? LOG_WARNING:LOG_CRIT
,"%04d !ERROR %d binding %s socket%s", s, ERROR_VALUE, prot, port_str);
,"%04d !ERROR %d (%s) binding %s socket%s", s, ERROR_VALUE, socket_strerror(socket_errno), prot, port_str);
if(i<retries) {
if(lprintf!=NULL)
lprintf(LOG_WARNING,"%04d Will retry in %u seconds (%u of %u)"
......@@ -496,3 +496,20 @@ BOOL inet_addrmatch(union xp_sockaddr* addr1, union xp_sockaddr* addr2)
}
return FALSE;
}
#if defined(_WINSOCKAPI_)
/* Return the current socket error description (for Windows), like strerror() does for errno */
DLLEXPORT const char* socket_strerror(int error_number)
{
static char msg[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // dwFlags
NULL, // lpSource
error_number, // dwMessageId
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // dwLanguageId
msg,
sizeof(msg),
NULL);
return msg;
}
#endif
......@@ -185,7 +185,8 @@ union xp_sockaddr {
typedef uint32_t in_addr_t;
static int wsa_error;
#define ERROR_VALUE ((wsa_error=WSAGetLastError())>0 ? wsa_error-WSABASEERR : wsa_error)
#define ERROR_VALUE ((wsa_error=WSAGetLastError())>0 ? wsa_error-WSABASEERR : wsa_error)
#define socket_errno WSAGetLastError()
#define sendsocket(s,b,l) send(s,b,l,0)
/* For getaddrinfo() */
......@@ -210,6 +211,8 @@ static int wsa_error;
#define closesocket close
#define ioctlsocket ioctl
#define ERROR_VALUE errno
#define socket_errno errno
#define socket_strerror strerror
#define sendsocket write /* FreeBSD send() is broken */
#ifdef __WATCOMC__
......@@ -238,6 +241,10 @@ DLLEXPORT uint16_t inet_addrport(union xp_sockaddr *addr);
DLLEXPORT void inet_setaddrport(union xp_sockaddr *addr, uint16_t port);
DLLEXPORT BOOL inet_addrmatch(union xp_sockaddr* addr1, union xp_sockaddr* addr2);
#if defined(_WINSOCKAPI_)
DLLEXPORT const char* socket_strerror(int);
#endif
#ifdef __cplusplus
}
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment