aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-19 23:01:07 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-19 23:01:07 +0000
commit9479dd376889c9d2cfb25bae8bd8b0a570118b3f (patch)
tree38b2451c138ed53a6bccecf9794e64d38af7721c /src/common
parentd1a195d3cb5201ef66d3aa3b71fecde7f2083ff2 (diff)
downloadtor-9479dd376889c9d2cfb25bae8bd8b0a570118b3f.tar
tor-9479dd376889c9d2cfb25bae8bd8b0a570118b3f.tar.gz
r18226@catbus: nickm | 2008-02-19 18:01:01 -0500
Brown-paper-bag time. We were failing to count all the sockets from accept(). svn:r13595
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c55
-rw-r--r--src/common/compat.h1
2 files changed, 39 insertions, 17 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 691a3f6b4..aa84b7a96 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -546,6 +546,29 @@ tor_close_socket(int s)
return r;
}
+#ifdef DEBUG_SOCKET_COUNTING
+static INLINE void
+mark_socket_open(int s)
+{
+ if (s > max_socket) {
+ if (max_socket == -1) {
+ open_sockets = bitarray_init_zero(s+128);
+ max_socket = s+128;
+ } else {
+ open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
+ max_socket = s+128;
+ }
+ }
+ if (bitarray_is_set(open_sockets, s)) {
+ log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
+ "gave it to me!", s);
+ }
+ bitarray_set(open_sockets, s);
+}
+#else
+#define mark_socket_open(s) STMT_NIL
+#endif
+
/** As socket(), but counts the number of open sockets. */
int
tor_open_socket(int domain, int type, int protocol)
@@ -553,22 +576,19 @@ tor_open_socket(int domain, int type, int protocol)
int s = socket(domain, type, protocol);
if (s >= 0) {
++n_sockets_open;
-#ifdef DEBUG_SOCKET_COUNTING
- if (s > max_socket) {
- if (max_socket == -1) {
- open_sockets = bitarray_init_zero(s+128);
- max_socket = s+128;
- } else {
- open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
- max_socket = s+128;
- }
- }
- if (bitarray_is_set(open_sockets, s)) {
- log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
- "gave it to me!", s);
- }
- bitarray_set(open_sockets, s);
-#endif
+ mark_socket_open(s);
+ }
+ return s;
+}
+
+/** As socket(), but counts the number of open sockets. */
+int
+tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len)
+{
+ int s = accept(sockfd, addr, len);
+ if (s >= 0) {
+ ++n_sockets_open;
+ mark_socket_open(s);
}
return s;
}
@@ -676,7 +696,8 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
goto tidy_up_and_fail;
size = sizeof(listen_addr);
- acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
+ acceptor = tor_accept_socket(listener,
+ (struct sockaddr *) &listen_addr, &size);
if (acceptor < 0)
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
diff --git a/src/common/compat.h b/src/common/compat.h
index c705e047d..7be7fddcd 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -257,6 +257,7 @@ int touch_file(const char *fname);
int tor_close_socket(int s);
int tor_open_socket(int domain, int type, int protocol);
+int tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len);
int get_n_open_sockets(void);
#ifdef USE_BSOCKETS