r15324@catbus: nickm | 2007-09-24 12:22:21 -0400

New evutil.h macros to manipulate winsock errors.  Use them in http.c and in evutil_socketpair().


svn:r451
This commit is contained in:
Nick Mathewson 2007-09-24 16:26:11 +00:00
parent 3c1a6a68d2
commit fe4829776b
3 changed files with 22 additions and 8 deletions

View File

@ -77,15 +77,17 @@ evutil_socketpair(int family, int type, int protocol, int fd[2])
|| family != AF_UNIX
#endif
) {
return -WSAEAFNOSUPPORT;
SET_SOCKET_ERROR(WSAEAFNOSUPPORT);
return -1;
}
if (!fd) {
return -EINVAL;
SET_SOCKET_ERROR(WSAEINVAL);
return -1;
}
listener = socket(AF_INET, type, 0);
if (listener < 0)
return -errno;
return -1;
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@ -134,14 +136,16 @@ evutil_socketpair(int family, int type, int protocol, int fd[2])
saved_errno = WSAECONNABORTED;
tidy_up_and_fail:
if (saved_errno < 0)
saved_errno = errno;
saved_errno = WSAGetLastError();
if (listener != -1)
EVUTIL_CLOSESOCKET(listener);
if (connector != -1)
EVUTIL_CLOSESOCKET(connector);
if (acceptor != -1)
EVUTIL_CLOSESOCKET(acceptor);
return -saved_errno;
SET_SOCKET_ERROR(saved_errno);
return -1;
#endif
}
@ -157,7 +161,7 @@ evutil_make_socket_nonblocking(int fd)
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
event_warn("fcntl(O_NONBLOCK)");
return -1;
}
}
#endif
return 0;
}

View File

@ -46,6 +46,16 @@ int evutil_make_socket_nonblocking(int sock);
#define EVUTIL_CLOSESOCKET(s) close(s)
#endif
#ifdef WIN32
#define EVUTIL_SOCKET_ERROR() WSAGetLastError()
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { WSASetLastError(errcode); } while (0)
#else
#define EVUTIL_SOCKET_ERROR() (errno)
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { errno = (errcode); } while (0)
#endif
#ifdef __cplusplus
}
#endif

4
http.c
View File

@ -2356,9 +2356,9 @@ bind_socket_ai(struct addrinfo *ai)
return (fd);
out:
serrno = errno;
serrno = EVUTIL_SOCKET_ERROR();
EVUTIL_CLOSESOCKET(fd);
errno = serrno;
EVUTIL_SET_SOCKET_ERROR(serrno);
return (-1);
}