From fdaeb608465e77e6ab6792406374775b44a1a004 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 6 Jan 2014 13:09:15 +0100 Subject: [PATCH] when using systemd socket activation the tcp-port option is optional. Moreover the UDP and TCP ports are "discovered" from the provided file descriptors. --- src/config.c | 2 +- src/main.c | 58 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/config.c b/src/config.c index 0ba4d350..2a4e0fae 100644 --- a/src/config.c +++ b/src/config.c @@ -50,7 +50,7 @@ static struct cfg_options available_options[] = { { .name = "auth", .type = OPTION_MULTI_LINE, .mandatory = 1 }, { .name = "route", .type = OPTION_MULTI_LINE, .mandatory = 0 }, { .name = "listen-host", .type = OPTION_STRING, .mandatory = 0 }, - { .name = "tcp-port", .type = OPTION_NUMERIC, .mandatory = 1 }, + { .name = "tcp-port", .type = OPTION_NUMERIC, .mandatory = 0 }, { .name = "udp-port", .type = OPTION_NUMERIC, .mandatory = 0 }, { .name = "keepalive", .type = OPTION_NUMERIC, .mandatory = 0 }, { .name = "dpd", .type = OPTION_NUMERIC, .mandatory = 0 }, diff --git a/src/main.c b/src/main.c index 5bad3356..00e7f815 100644 --- a/src/main.c +++ b/src/main.c @@ -81,7 +81,6 @@ static void add_listener(struct listen_list_st *list, struct sockaddr* addr, socklen_t addr_len) { struct listener_st *tmp; - int ret; tmp = calloc(1, sizeof(struct listener_st)); tmp->fd = fd; @@ -89,16 +88,8 @@ static void add_listener(struct listen_list_st *list, tmp->socktype = socktype; tmp->protocol = protocol; - if (addr == NULL) { - ret = getsockname(fd, (struct sockaddr*)&tmp->addr, &tmp->addr_len); - if (ret == -1) { - perror("getsockname failed"); - tmp->addr_len = 0; - } - } else { - tmp->addr_len = addr_len; - memcpy(&tmp->addr, addr, addr_len); - } + tmp->addr_len = addr_len; + memcpy(&tmp->addr, addr, addr_len); list_add(&list->head, &(tmp->list)); list->total++; @@ -209,42 +200,66 @@ listen_ports(struct cfg_st* config, struct listen_list_st *list, const char *nod { struct addrinfo hints, *res; char portname[6]; - int ret; + int ret, fds; list_head_init(&list->head); list->total = 0; #ifdef HAVE_LIBSYSTEMD_DAEMON /* Support for systemd socket-activatable service */ - if ((ret=sd_listen_fds(0)) > 0) { + if ((fds=sd_listen_fds(0)) > 0) { /* if we get our fds from systemd */ unsigned i; int family, type, fd; + struct sockaddr_storage tmp_sock; + socklen_t tmp_sock_len; - for (i=0;iport = ntohs(((struct sockaddr_in*)&tmp_sock)->sin_port); + else + config->port = ntohs(((struct sockaddr_in6*)&tmp_sock)->sin6_port); + } else if (type == SOCK_DGRAM) { + if (family == AF_INET) + config->udp_port = ntohs(((struct sockaddr_in*)&tmp_sock)->sin_port); + else + config->udp_port = ntohs(((struct sockaddr_in6*)&tmp_sock)->sin6_port); + } + + add_listener(list, fd, family, type, 0, (struct sockaddr*)&tmp_sock, tmp_sock_len); } if (list->total == 0) { @@ -259,6 +274,11 @@ listen_ports(struct cfg_st* config, struct listen_list_st *list, const char *nod } #endif + if (config->port == 0) { + fprintf(stderr, "tcp-port option is mandatory!\n"); + return -1; + } + snprintf(portname, sizeof(portname), "%d", config->port); memset(&hints, 0, sizeof(hints));