summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2012-01-17 16:35:07 -0500
committerNick Mathewson <nickm@torproject.org>2012-01-17 16:35:07 -0500
commit6e8c2a3e46e3578b0268d422b1753995d18fb734 (patch)
tree0f2952090d871b437021d2f6c6059e78234fa0f5 /src/common
parent1e923dd2fbdc0834b8fb3ba97b658f15a6d5f6b4 (diff)
downloadtor-6e8c2a3e46e3578b0268d422b1753995d18fb734.tar.gz
tor-6e8c2a3e46e3578b0268d422b1753995d18fb734.zip
Use SOCKET_OK macros in even more places
Add a TOR_INVALID_SOCKET macro to wrap -1/INVALID_SOCKET. Partial work for bug4533.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c14
-rw-r--r--src/common/compat.h8
2 files changed, 15 insertions, 7 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 3af43e80c9..7f8903733b 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1053,17 +1053,17 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
r = socketpair(family, type, protocol, fd);
if (r == 0) {
#if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC)
- if (fd[0] >= 0)
+ if (SOCKET_OK(fd[0]))
fcntl(fd[0], F_SETFD, FD_CLOEXEC);
- if (fd[1] >= 0)
+ if (SOCKET_OK(fd[1]))
fcntl(fd[1], F_SETFD, FD_CLOEXEC);
#endif
socket_accounting_lock();
- if (fd[0] >= 0) {
+ if (SOCKET_OK(fd[0])) {
++n_sockets_open;
mark_socket_open(fd[0]);
}
- if (fd[1] >= 0) {
+ if (SOCKET_OK(fd[1])) {
++n_sockets_open;
mark_socket_open(fd[1]);
}
@@ -1100,7 +1100,7 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
}
listener = tor_open_socket(AF_INET, type, 0);
- if (listener < 0)
+ if (!SOCKET_OK(listener))
return -tor_socket_errno(-1);
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
@@ -1113,7 +1113,7 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
goto tidy_up_and_fail;
connector = tor_open_socket(AF_INET, type, 0);
- if (connector < 0)
+ if (!SOCKET_OK(connector))
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
@@ -1128,7 +1128,7 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
size = sizeof(listen_addr);
acceptor = tor_accept_socket(listener,
(struct sockaddr *) &listen_addr, &size);
- if (acceptor < 0)
+ if (!SOCKET_OK(acceptor))
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
diff --git a/src/common/compat.h b/src/common/compat.h
index 2db8107a8e..1b79565c0b 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -399,11 +399,19 @@ typedef int socklen_t;
#endif
#ifdef MS_WINDOWS
+/* XXX Actually, this should arguably be SOCKET; we use intptr_t here so that
+ * any inadvertant checks for the socket being <= 0 or > 0 will probably
+ * still work. */
#define tor_socket_t intptr_t
#define SOCKET_OK(s) ((unsigned)(s) != INVALID_SOCKET)
+#define TOR_INVALID_SOCKET INVALID_SOCKET
#else
+/** Type used for a network socket. */
#define tor_socket_t int
+/** Macro: true iff 's' is a possible value for a valid initialized socket. */
#define SOCKET_OK(s) ((s) >= 0)
+/** Error/uninitialized value for a tor_socket_t. */
+#define TOR_INVALID_SOCKET (-1)
#endif
int tor_close_socket(tor_socket_t s);