From 4b3bc5fc441d87e7b40c86ad8dbc7ba325c16ff6 Mon Sep 17 00:00:00 2001 From: Sebastiaan Lokhorst Date: Thu, 29 Sep 2016 14:06:29 +0200 Subject: [PATCH] Reuse socket when it's still open. Properly close socket when binding/listening fails. --- stream.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stream.c b/stream.c index 181a1e97..02ac78c0 100644 --- a/stream.c +++ b/stream.c @@ -741,16 +741,19 @@ int http_bindsock(int port, int local, int ipv6_localhost) addr_str = "any address"; sin.sin6_addr = in6addr_any; } - int no = 0; - setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&no, sizeof(no)); + int no = 0, yes = 1; + setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)); + setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); if (bind(sd, &sin, sizeof(sin)) != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, "%s: error binding on %s port %d", addr_str, port); + close(sd); return -1; } if (listen(sd, DEF_MAXWEBQUEUE) != 0) { MOTION_LOG(CRT, TYPE_STREAM, SHOW_ERRNO, "%s: error listening"); + close(sd); return -1; }