#include <sys/sendfile.h>
#endif
-#undef DEBUG
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
static guint32 startup_count=0;
+static guint32 in_cleanup = 0;
static void socket_close (gpointer handle, gpointer data);
int ret;
struct _WapiHandle_socket *socket_handle = (struct _WapiHandle_socket *)data;
-#ifdef DEBUG
- g_message ("%s: closing socket handle %p", __func__, handle);
-#endif
+ DEBUG ("%s: closing socket handle %p", __func__, handle);
- if (startup_count == 0) {
+ if (startup_count == 0 && !in_cleanup) {
WSASetLastError (WSANOTINITIALISED);
return;
}
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: close error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: close error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
- WSASetLastError (errnum);
+ if (!in_cleanup)
+ WSASetLastError (errnum);
}
- socket_handle->saved_error = 0;
+ if (!in_cleanup)
+ socket_handle->saved_error = 0;
}
int WSAStartup(guint32 requested, WapiWSAData *data)
data->wVersion = requested < data->wHighVersion? requested:
data->wHighVersion;
-#ifdef DEBUG
- g_message ("%s: high version 0x%x", __func__, data->wHighVersion);
-#endif
+ DEBUG ("%s: high version 0x%x", __func__, data->wHighVersion);
strncpy (data->szDescription, "WAPI", WSADESCRIPTION_LEN);
strncpy (data->szSystemStatus, "groovy", WSASYS_STATUS_LEN);
int WSACleanup(void)
{
-#ifdef DEBUG
- g_message ("%s: cleaning up", __func__);
-#endif
+ DEBUG ("%s: cleaning up", __func__);
if (--startup_count) {
/* Do nothing */
return(0);
}
+ in_cleanup = 1;
_wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
+ in_cleanup = 0;
return(0);
}
if (new_fd == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: accept error: %s", __func__, strerror(errno));
-#endif
+ DEBUG ("%s: accept error: %s", __func__, strerror(errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
}
if (new_fd >= _wapi_fd_reserve) {
-#ifdef DEBUG
- g_message ("%s: File descriptor is too big", __func__);
-#endif
+ DEBUG ("%s: File descriptor is too big", __func__);
WSASetLastError (WSASYSCALLFAILURE);
return(INVALID_SOCKET);
}
-#ifdef DEBUG
- g_message ("%s: returning newly accepted socket handle %p with",
+ DEBUG ("%s: returning newly accepted socket handle %p with",
__func__, new_handle);
-#endif
return(new_fd);
}
ret = bind (fd, my_addr, addrlen);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: bind error: %s", __func__, strerror(errno));
-#endif
+ DEBUG ("%s: bind error: %s", __func__, strerror(errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
errnum = errno;
if (errno != EINTR) {
-#ifdef DEBUG
- g_message ("%s: connect error: %s", __func__,
+ DEBUG ("%s: connect error: %s", __func__,
strerror (errnum));
-#endif
errnum = errno_to_WSA (errnum, __func__);
if (errnum == WSAEINPROGRESS)
(gpointer *)&socket_handle);
if (ok == FALSE) {
/* ECONNRESET means the socket was closed by another thread */
- if (errnum != WSAECONNRESET)
+ /* Async close on mac raises ECONNABORTED. */
+ if (errnum != WSAECONNRESET && errnum != WSAENETDOWN)
g_warning ("%s: error looking up socket handle %p (error %d)", __func__, handle, errnum);
} else {
socket_handle->saved_error = errnum;
if (errno != EINTR) {
errnum = errno_to_WSA (errno, __func__);
-#ifdef DEBUG
- g_message ("%s: connect poll error: %s",
+ DEBUG ("%s: connect poll error: %s",
__func__, strerror (errno));
-#endif
WSASetLastError (errnum);
return(SOCKET_ERROR);
&len) == -1) {
errnum = errno_to_WSA (errno, __func__);
-#ifdef DEBUG
- g_message ("%s: connect getsockopt error: %s",
+ DEBUG ("%s: connect getsockopt error: %s",
__func__, strerror (errno));
-#endif
WSASetLastError (errnum);
return(SOCKET_ERROR);
socket_handle->saved_error = errnum;
}
-#ifdef DEBUG
- g_message ("%s: connect getsockopt returned error: %s",
+ DEBUG ("%s: connect getsockopt returned error: %s",
__func__, strerror (so_error));
-#endif
WSASetLastError (errnum);
return(SOCKET_ERROR);
ret = getpeername (fd, name, namelen);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: getpeername error: %s", __func__,
+ DEBUG ("%s: getpeername error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
ret = getsockname (fd, name, namelen);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: getsockname error: %s", __func__,
+ DEBUG ("%s: getsockname error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
ret = getsockopt (fd, level, optname, tmp_val, optlen);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: getsockopt error: %s", __func__,
+ DEBUG ("%s: getsockopt error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
ret = listen (fd, backlog);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: listen error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: listen error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: recv error: %s", __func__, strerror(errno));
-#endif
+ DEBUG ("%s: recv error: %s", __func__, strerror(errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: recvmsg error: %s", __func__, strerror(errno));
-#endif
+ DEBUG ("%s: recvmsg error: %s", __func__, strerror(errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: send error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: send error: %s", __func__, strerror (errno));
#ifdef O_NONBLOCK
/* At least linux returns EAGAIN/EWOULDBLOCK when the timeout has been set on
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: send error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: send error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: sendmsg error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: sendmsg error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
ret = setsockopt (fd, level, optname, tmp_val, optlen);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: setsockopt error: %s", __func__,
+ DEBUG ("%s: setsockopt error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
ret = shutdown (fd, how);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: shutdown error: %s", __func__,
+ DEBUG ("%s: shutdown error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (fd == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: socket error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: socket error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
}
if (fd >= _wapi_fd_reserve) {
-#ifdef DEBUG
- g_message ("%s: File descriptor is too big (%d >= %d)",
+ DEBUG ("%s: File descriptor is too big (%d >= %d)",
__func__, fd, _wapi_fd_reserve);
-#endif
WSASetLastError (WSASYSCALLFAILURE);
close (fd);
if (ret == -1) {
int errnum = errno;
-#ifdef DEBUG
- g_message ("%s: Error setting SO_REUSEADDR", __func__);
-#endif
+ DEBUG ("%s: Error setting SO_REUSEADDR", __func__);
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
return(INVALID_SOCKET);
}
-#ifdef DEBUG
- g_message ("%s: returning socket handle %p", __func__, handle);
-#endif
+ DEBUG ("%s: returning socket handle %p", __func__, handle);
return(fd);
}
he = gethostbyname (hostname);
if (he == NULL) {
-#ifdef DEBUG
- g_message ("%s: gethostbyname error: %s", __func__,
+ DEBUG ("%s: gethostbyname error: %s", __func__,
strerror (h_errno));
-#endif
switch(h_errno) {
case HOST_NOT_FOUND:
if (newsock == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: socket error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: socket error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: dup2 error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: dup2 error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
guint32 flags, guint32 reserved)
{
-#ifdef DEBUG
- g_message ("%s: called on socket %d!", __func__, fd);
-#endif
+ DEBUG ("%s: called on socket %d!", __func__, fd);
if (reserved != 0) {
WSASetLastError (WSAEINVAL);
ret = ioctl (fd, command, buffer);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message("%s: WSAIoctl error: %s", __func__,
+ DEBUG("%s: WSAIoctl error: %s", __func__,
strerror (errno));
-#endif
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: ioctl error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: ioctl error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);
if (ret == -1) {
gint errnum = errno;
-#ifdef DEBUG
- g_message ("%s: select error: %s", __func__, strerror (errno));
-#endif
+ DEBUG ("%s: select error: %s", __func__, strerror (errno));
errnum = errno_to_WSA (errnum, __func__);
WSASetLastError (errnum);