/*
- * Copyright (C) 2015-2018 "IoT.bzh"
+ * Copyright (C) 2015-2019 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#include "afb-fdev.h"
#include "afb-socket.h"
-#include "afb-systemd.h"
+
+#include "systemd.h"
#include "fdev.h"
#include "verbose.h"
*
* @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;
struct addrinfo hint, *rai, *iai;
/* scan the uri */
- tail = strchr(spec, '/');
+ tail = strchrnul(spec, '/');
service = strchr(spec, ':');
if (tail == NULL || service == NULL || tail < service) {
errno = EINVAL;
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);
errno = EAFNOSUPPORT;
return -1;
#else
- return afb_systemd_fds_for(spec);
+ return systemd_fds_for(spec);
#endif
}
*/
static int open_uri(const char *uri, int server)
{
- int fd, rc, offset;
+ int fd, offset;
struct entry *e;
const char *api;
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)
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);
}