From 02b6159ac5cfad381ba773e0fa790ef4ec772475 Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Tue, 9 Apr 2013 19:46:21 +0000 Subject: [PATCH] applying patch: 1) Close ts->fdc if a new connect is received before the previous connect was accepted or rejected. Otherwise we will "leak" fds. 2) Include 0 in valid fd range 3) Don't try accept if ts->fdc < 0 4) Don't set tc->fdc = -1 (without closing) when connect fails, connect will be retried using alternative sockaddr in loop. --- src/tcp/tcp.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tcp/tcp.c b/src/tcp/tcp.c index a08f7a5..b726d00 100644 --- a/src/tcp/tcp.c +++ b/src/tcp/tcp.c @@ -497,6 +497,9 @@ static void tcp_conn_handler(int flags, void *arg) sa_init(&peer, AF_UNSPEC); + if (ts->fdc >= 0) + (void)close(ts->fdc); + ts->fdc = SOK_CAST accept(ts->fd, &peer.u.sa, &peer.len); if (-1 == ts->fdc) { @@ -605,7 +608,7 @@ int tcp_sock_alloc(struct tcp_sock **tsp, const struct sa *local, for (r = res; r; r = r->ai_next) { int fd = -1; - if (ts->fd > 0) + if (ts->fd >= 0) continue; fd = SOK_CAST socket(r->ai_family, SOCK_STREAM, IPPROTO_TCP); @@ -760,7 +763,7 @@ int tcp_accept(struct tcp_conn **tcp, struct tcp_sock *ts, tcp_estab_h *eh, struct tcp_conn *tc; int err; - if (!tcp || !ts) + if (!tcp || !ts || ts->fdc < 0) return EINVAL; tc = conn_alloc(eh, rh, ch, arg); @@ -796,7 +799,7 @@ void tcp_reject(struct tcp_sock *ts) if (!ts) return; - if (ts->fdc > 0) { + if (ts->fdc >= 0) { (void)close(ts->fdc); ts->fdc = -1; } @@ -1030,7 +1033,6 @@ int tcp_conn_connect(struct tcp_conn *tc, const struct sa *peer) goto again; if (EINPROGRESS != errno && EALREADY != errno) { - tc->fdc = -1; err = errno; DEBUG_INFO("connect: connect() %J: %m\n", peer, err); @@ -1256,7 +1258,7 @@ int tcp_conn_local_get(const struct tcp_conn *tc, struct sa *local) */ int tcp_conn_peer_get(const struct tcp_conn *tc, struct sa *peer) { - if (!tc) + if (!tc || !peer) return EINVAL; sa_init(peer, AF_UNSPEC);