]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: backend: switch to conn_get_{src,dst}() for port and address mapping
authorWilly Tarreau <w@1wt.eu>
Wed, 17 Jul 2019 09:27:38 +0000 (11:27 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 19 Jul 2019 11:50:09 +0000 (13:50 +0200)
The backend connect code uses conn_get_{from,to}_addr to forward addresses
in transparent mode and to map server ports, without really checking if the
operation succeeds. In preparation of future changes, let's switch to
conn_get_{src,dst}() and integrate status check for possible failures.

include/proto/stream_interface.h
src/backend.c

index 6727921ebce0c2585bda03ea7384096181ae66b5..e0f699184c4874f316f620f583bdf9f5d0a52c40 100644 (file)
@@ -526,7 +526,7 @@ static inline int si_connect(struct stream_interface *si, struct connection *con
 
        /* needs src ip/port for logging */
        if (si->flags & SI_FL_SRC_ADDR)
-               conn_get_from_addr(conn);
+               conn_get_src(conn);
 
        return ret;
 }
index 169481c4615df3edb3e4757009e3e51fc1cc9c09..dc0c2985cfed5104f4874871c71d0a7b48e931cf 100644 (file)
@@ -678,12 +678,12 @@ int assign_server(struct stream *s)
                        switch (s->be->lbprm.algo & BE_LB_PARM) {
                        case BE_LB_HASH_SRC:
                                conn = objt_conn(strm_orig(s));
-                               if (conn && conn->addr.from.ss_family == AF_INET) {
+                               if (conn && conn_get_src(conn) && conn->addr.from.ss_family == AF_INET) {
                                        srv = get_server_sh(s->be,
                                                            (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
                                                            4, prev_srv);
                                }
-                               else if (conn && conn->addr.from.ss_family == AF_INET6) {
+                               else if (conn && conn_get_src(conn) && conn->addr.from.ss_family == AF_INET6) {
                                        srv = get_server_sh(s->be,
                                                            (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
                                                            16, prev_srv);
@@ -840,9 +840,9 @@ int assign_server_address(struct stream *s, struct connection *srv_conn)
                         * locally on multiple addresses at once. Nothing is done
                         * for AF_UNIX addresses.
                         */
-                       conn_get_to_addr(cli_conn);
-
-                       if (cli_conn->addr.to.ss_family == AF_INET) {
+                       if (!conn_get_dst(cli_conn)) {
+                               /* do nothing if we can't retrieve the address */
+                       } else if (cli_conn->addr.to.ss_family == AF_INET) {
                                ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
                        } else if (cli_conn->addr.to.ss_family == AF_INET6) {
                                ((struct sockaddr_in6 *)&srv_conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
@@ -854,14 +854,14 @@ int assign_server_address(struct stream *s, struct connection *srv_conn)
                if ((__objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
                        int base_port;
 
-                       conn_get_to_addr(cli_conn);
-
-                       /* First, retrieve the port from the incoming connection */
-                       base_port = get_host_port(&cli_conn->addr.to);
+                       if (conn_get_dst(cli_conn)) {
+                               /* First, retrieve the port from the incoming connection */
+                               base_port = get_host_port(&cli_conn->addr.to);
 
-                       /* Second, assign the outgoing connection's port */
-                       base_port += get_host_port(&srv_conn->addr.to);
-                       set_host_port(&srv_conn->addr.to, base_port);
+                               /* Second, assign the outgoing connection's port */
+                               base_port += get_host_port(&srv_conn->addr.to);
+                               set_host_port(&srv_conn->addr.to, base_port);
+                       }
                }
        }
        else if (s->be->options & PR_O_DISPATCH) {
@@ -870,9 +870,8 @@ int assign_server_address(struct stream *s, struct connection *srv_conn)
        }
        else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
                /* in transparent mode, use the original dest addr if no dispatch specified */
-               conn_get_to_addr(cli_conn);
-
-               if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
+               if (conn_get_dst(cli_conn) &&
+                   (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6))
                        srv_conn->addr.to = cli_conn->addr.to;
        }
        else if (s->be->options & PR_O_HTTP_PROXY) {
@@ -1046,7 +1045,7 @@ static void assign_tproxy_address(struct stream *s)
        case CO_SRC_TPROXY_CIP:
                /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
                cli_conn = objt_conn(strm_orig(s));
-               if (cli_conn)
+               if (cli_conn && conn_get_src(cli_conn))
                        srv_conn->addr.from = cli_conn->addr.from;
                else
                        memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
@@ -1474,7 +1473,7 @@ int connect_server(struct stream *s)
                        srv_conn->flags |= CO_FL_SEND_PROXY;
                        srv_conn->send_proxy_ofs = 1; /* must compute size */
                        if (cli_conn)
-                               conn_get_to_addr(cli_conn);
+                               conn_get_dst(cli_conn);
                }
 
                assign_tproxy_address(s);