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 || family != AF_UNIX
#endif #endif
) { ) {
return -WSAEAFNOSUPPORT; SET_SOCKET_ERROR(WSAEAFNOSUPPORT);
return -1;
} }
if (!fd) { if (!fd) {
return -EINVAL; SET_SOCKET_ERROR(WSAEINVAL);
return -1;
} }
listener = socket(AF_INET, type, 0); listener = socket(AF_INET, type, 0);
if (listener < 0) if (listener < 0)
return -errno; return -1;
memset(&listen_addr, 0, sizeof(listen_addr)); memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET; listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 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; saved_errno = WSAECONNABORTED;
tidy_up_and_fail: tidy_up_and_fail:
if (saved_errno < 0) if (saved_errno < 0)
saved_errno = errno; saved_errno = WSAGetLastError();
if (listener != -1) if (listener != -1)
EVUTIL_CLOSESOCKET(listener); EVUTIL_CLOSESOCKET(listener);
if (connector != -1) if (connector != -1)
EVUTIL_CLOSESOCKET(connector); EVUTIL_CLOSESOCKET(connector);
if (acceptor != -1) if (acceptor != -1)
EVUTIL_CLOSESOCKET(acceptor); EVUTIL_CLOSESOCKET(acceptor);
return -saved_errno;
SET_SOCKET_ERROR(saved_errno);
return -1;
#endif #endif
} }
@ -157,7 +161,7 @@ evutil_make_socket_nonblocking(int fd)
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
event_warn("fcntl(O_NONBLOCK)"); event_warn("fcntl(O_NONBLOCK)");
return -1; return -1;
} }
#endif #endif
return 0; return 0;
} }

View File

@ -46,6 +46,16 @@ int evutil_make_socket_nonblocking(int sock);
#define EVUTIL_CLOSESOCKET(s) close(s) #define EVUTIL_CLOSESOCKET(s) close(s)
#endif #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 #ifdef __cplusplus
} }
#endif #endif

4
http.c
View File

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