]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: listener: automatically set the port when creating listeners
authorWilly Tarreau <w@1wt.eu>
Fri, 4 Dec 2020 13:49:11 +0000 (14:49 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 4 Dec 2020 14:08:00 +0000 (15:08 +0100)
In create_listeners() we iterate over a port range and call the
protocol's ->add() function to add a new listener on the specified
port. Only tcp4/tcp6/udp4/udp6 support a port, the other ones ignore
it. Now that we can rely on the address family to properly set the
port, better do it this way directly from create_listeners() and
remove the family-specific case from the protocol layer.

src/listener.c
src/proto_tcp.c
src/proto_udp.c

index c9f0c2de6ac70645dbd8ec48690ac5cdc9fe56b3..a8c5460a912a0f955874b4ea87a4a8cbe9896ee3 100644 (file)
@@ -614,7 +614,11 @@ int create_listeners(struct bind_conf *bc, const struct sockaddr_storage *ss,
                l->rx.owner = l;
                l->rx.iocb = proto->default_iocb;
                l->rx.fd = fd;
+
                memcpy(&l->rx.addr, ss, sizeof(*ss));
+               if (proto->fam.set_port)
+                       proto->fam.set_port(&l->rx.addr, port);
+
                MT_LIST_INIT(&l->wait_queue);
                listener_set_state(l, LI_INIT);
 
index e8e6c766c919fab2dd0a47d3e59c724338be48c4..7c7670a4e98d6ec52588c6929996ba01dc2ee97a 100644 (file)
@@ -725,7 +725,6 @@ static void tcpv4_add_listener(struct listener *listener, int port)
                return;
        listener_set_state(listener, LI_ASSIGNED);
        listener->rx.proto = &proto_tcpv4;
-       ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
        LIST_ADDQ(&proto_tcpv4.receivers, &listener->rx.proto_list);
        proto_tcpv4.nb_receivers++;
 }
@@ -743,7 +742,6 @@ static void tcpv6_add_listener(struct listener *listener, int port)
                return;
        listener_set_state(listener, LI_ASSIGNED);
        listener->rx.proto = &proto_tcpv6;
-       ((struct sockaddr_in6 *)(&listener->rx.addr))->sin6_port = htons(port);
        LIST_ADDQ(&proto_tcpv6.receivers, &listener->rx.proto_list);
        proto_tcpv6.nb_receivers++;
 }
index e38ab0d114f0294588b21e8698faabadcf31a253..436f2a01a2540a53611810323d3d21b76e1b61e5 100644 (file)
@@ -152,7 +152,6 @@ static void udp4_add_listener(struct listener *listener, int port)
                return;
        listener_set_state(listener, LI_ASSIGNED);
        listener->rx.proto = &proto_udp4;
-       ((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
        LIST_ADDQ(&proto_udp4.receivers, &listener->rx.proto_list);
        proto_udp4.nb_receivers++;
 }
@@ -167,7 +166,6 @@ static void udp6_add_listener(struct listener *listener, int port)
                return;
        listener_set_state(listener, LI_ASSIGNED);
        listener->rx.proto = &proto_udp6;
-       ((struct sockaddr_in6 *)(&listener->rx.addr))->sin6_port = htons(port);
        LIST_ADDQ(&proto_udp6.receivers, &listener->rx.proto_list);
        proto_udp6.nb_receivers++;
 }