]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: protocols: register the ->add function and stop calling them directly
authorWilly Tarreau <w@1wt.eu>
Fri, 15 Sep 2017 05:55:51 +0000 (07:55 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 15 Sep 2017 09:49:52 +0000 (11:49 +0200)
cfgparse has no business directly calling each individual protocol's 'add'
function to create a listener. Now that they're all registered, better
perform a protocol lookup on the family and have a standard ->add method
for all of them.

include/proto/proto_tcp.h
include/proto/proto_uxst.h
include/types/protocol.h
src/cfgparse.c
src/proto_tcp.c
src/proto_uxst.c

index 94a84153b168b44d9f7c391050af9f576d847830..5241e7cafb8f571171cd7d9a4e765ae4ab560a82 100644 (file)
@@ -28,8 +28,6 @@
 #include <proto/stick_table.h>
 
 int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote);
-void tcpv4_add_listener(struct listener *listener, int port);
-void tcpv6_add_listener(struct listener *listener, int port);
 int tcp_pause_listener(struct listener *l);
 int tcp_connect_server(struct connection *conn, int data, int delack);
 int tcp_connect_probe(struct connection *conn);
index 63de24f53dcaf898018635e4e3f04c1452832910..11aed981bbacf35e9907a863d52a8be30c91353a 100644 (file)
@@ -26,7 +26,6 @@
 #include <types/stream.h>
 #include <types/task.h>
 
-void uxst_add_listener(struct listener *listener, int port);
 int uxst_pause_listener(struct listener *l);
 int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
 int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
index 6a4986ff4a09e2411935602e488081f172637281..9480c186907fe1114b8df736209f5dbd537f8f1e 100644 (file)
@@ -61,6 +61,7 @@ struct protocol {
        int (*get_dst)(int fd, struct sockaddr *, socklen_t, int dir); /* syscall used to retrieve dst addr */
        int (*drain)(int fd);                           /* indicates whether we can safely close the fd */
        int (*pause)(struct listener *l);               /* temporarily pause this listener for a soft restart */
+       void (*add)(struct listener *l, int port);      /* add a listener for this protocol and port */
 
        struct list listeners;                          /* list of listeners using this protocol */
        int nb_listeners;                               /* number of listeners */
index 9752b81e29f6a4aecbaf4c46772d46b0669cd16e..759d407598df86654e90651e60f533fa7ab1885f 100644 (file)
@@ -72,8 +72,6 @@
 #include <proto/listener.h>
 #include <proto/log.h>
 #include <proto/protocol.h>
-#include <proto/proto_tcp.h>
-#include <proto/proto_uxst.h>
 #include <proto/proto_http.h>
 #include <proto/proxy.h>
 #include <proto/peers.h>
@@ -288,6 +286,13 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
                memcpy(&ss, ss2, sizeof(ss));
 
                for (; port <= end; port++) {
+                       struct protocol *proto = protocol_by_family(ss.ss_family);
+
+                       if (!proto) {
+                               memprintf(err, "unsupported protocol family %d for address '%s'.\n", ss.ss_family, str);
+                               goto fail;
+                       }
+
                        l = calloc(1, sizeof(*l));
                        l->obj_type = OBJ_TYPE_LISTENER;
                        LIST_ADDQ(&curproxy->conf.listeners, &l->by_fe);
@@ -298,15 +303,7 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
                        memcpy(&l->addr, &ss, sizeof(ss));
                        l->state = LI_INIT;
 
-                       if (ss.ss_family == AF_INET) {
-                               tcpv4_add_listener(l, port);
-                       }
-                       else if (ss.ss_family == AF_INET6) {
-                               tcpv6_add_listener(l, port);
-                       }
-                       else {
-                               uxst_add_listener(l, port);
-                       }
+                       proto->add(l, port);
 
                        jobs++;
                        listeners++;
index 6b1505a3a5ff2ac949eb2fc4dfc5a964a01f8759..0fad867a9288c7ea1eaea498fdf8967cfacff3e1 100644 (file)
@@ -63,6 +63,8 @@
 
 static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
 static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen);
+static void tcpv4_add_listener(struct listener *listener, int port);
+static void tcpv6_add_listener(struct listener *listener, int port);
 
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct protocol proto_tcpv4 = {
@@ -83,6 +85,7 @@ static struct protocol proto_tcpv4 = {
        .get_dst = tcp_get_dst,
        .drain = tcp_drain,
        .pause = tcp_pause_listener,
+       .add = tcpv4_add_listener,
        .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners),
        .nb_listeners = 0,
 };
@@ -106,6 +109,7 @@ static struct protocol proto_tcpv6 = {
        .get_dst = tcp_get_dst,
        .drain = tcp_drain,
        .pause = tcp_pause_listener,
+       .add = tcpv6_add_listener,
        .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners),
        .nb_listeners = 0,
 };
@@ -1141,7 +1145,7 @@ static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
  * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
  * The number of listeners for the protocol is updated.
  */
-void tcpv4_add_listener(struct listener *listener, int port)
+static void tcpv4_add_listener(struct listener *listener, int port)
 {
        if (listener->state != LI_INIT)
                return;
@@ -1156,7 +1160,7 @@ void tcpv4_add_listener(struct listener *listener, int port)
  * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
  * The number of listeners for the protocol is updated.
  */
-void tcpv6_add_listener(struct listener *listener, int port)
+static void tcpv6_add_listener(struct listener *listener, int port)
 {
        if (listener->state != LI_INIT)
                return;
index 767cd16fd35bf3332f06ad05a639fcb54349fb59..ae6e3388c3ac9b56c5f227115c1a0ffcde484a67 100644 (file)
@@ -49,6 +49,7 @@ static int uxst_bind_listener(struct listener *listener, char *errmsg, int errle
 static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
 static int uxst_unbind_listeners(struct protocol *proto);
 static int uxst_connect_server(struct connection *conn, int data, int delack);
+static void uxst_add_listener(struct listener *listener, int port);
 
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct protocol proto_unix = {
@@ -69,6 +70,7 @@ static struct protocol proto_unix = {
        .get_src = uxst_get_src,
        .get_dst = uxst_get_dst,
        .pause = uxst_pause_listener,
+       .add = uxst_add_listener,
        .listeners = LIST_HEAD_INIT(proto_unix.listeners),
        .nb_listeners = 0,
 };
@@ -369,7 +371,7 @@ static int uxst_unbind_listener(struct listener *listener)
  * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
  * The number of listeners for the protocol is updated.
  */
-void uxst_add_listener(struct listener *listener, int port)
+static void uxst_add_listener(struct listener *listener, int port)
 {
        if (listener->state != LI_INIT)
                return;