MINOR: listener: automatically set the port when creating listeners
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.
(cherry picked from commit 07400c56bb0ab81cfd2e295c790b699839fea0a3)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/listener.c b/src/listener.c
index 7f038c4..7689ceb 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -614,7 +614,11 @@
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);
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index e8e6c76..7c7670a 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -725,7 +725,6 @@
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 @@
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++;
}
diff --git a/src/proto_udp.c b/src/proto_udp.c
index e38ab0d..436f2a0 100644
--- a/src/proto_udp.c
+++ b/src/proto_udp.c
@@ -152,7 +152,6 @@
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 @@
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++;
}