summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2018-11-16 19:09:22 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2018-11-16 19:11:18 +0100
commit442f25843bfde8fb835f0306f04d6da878bc5bca (patch)
tree8c14cb911b0652461a75e6b4a010c09918b369d8
parent470abd47f759193e181bcb566d6d87bb75659efc (diff)
afb-socket: Fix address reuseflounder_6.0.3flounder/6.0.36.0.3
Change-Id: I3ae51611b92762a4f09b1f6cca6ff27853cd9f95 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-socket.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/afb-socket.c b/src/afb-socket.c
index 77b5edf8..13b465d6 100644
--- a/src/afb-socket.c
+++ b/src/afb-socket.c
@@ -158,7 +158,7 @@ static int open_unix(const char *spec, int server)
*
* @return the file descriptor number of the socket or -1 in case of error
*/
-static int open_tcp(const char *spec, int server)
+static int open_tcp(const char *spec, int server, int reuseaddr)
{
int rc, fd;
const char *service, *host, *tail;
@@ -190,6 +190,10 @@ static int open_tcp(const char *spec, int server)
fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
if (fd >= 0) {
if (server) {
+ if (reuseaddr) {
+ rc = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
+ }
rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
} else {
rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
@@ -262,7 +266,7 @@ static struct entry *get_entry(const char *uri, int *offset)
*/
static int open_uri(const char *uri, int server)
{
- int fd, rc, offset;
+ int fd, offset;
struct entry *e;
const char *api;
@@ -281,7 +285,7 @@ static int open_uri(const char *uri, int server)
fd = open_unix(uri, server);
break;
case Type_Inet:
- fd = open_tcp(uri, server);
+ fd = open_tcp(uri, server, !e->noreuseaddr);
break;
case Type_Systemd:
if (server)
@@ -303,10 +307,6 @@ static int open_uri(const char *uri, int server)
fcntl(fd, F_SETFD, FD_CLOEXEC);
fcntl(fd, F_SETFL, O_NONBLOCK);
if (server) {
- if (!e->noreuseaddr) {
- rc = 1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
- }
if (!e->nolisten)
listen(fd, BACKLOG);
}