]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: connection: add conn_get_src() and conn_get_dst()
authorWilly Tarreau <w@1wt.eu>
Wed, 17 Jul 2019 08:48:33 +0000 (10:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 19 Jul 2019 11:50:09 +0000 (13:50 +0200)
These functions currently are the same as conn_get_from_addr() and
conn_get_to_addr() respectively except that they return a status for
the operation that the caller can test.

include/proto/connection.h

index 9b4ac51d335a4f48e1ecb231fb3e34efeec58cf5..4c896daee6ba5c345fa512327f4179c00be3bd1f 100644 (file)
@@ -661,6 +661,46 @@ static inline void conn_get_to_addr(struct connection *conn)
        conn->flags |= CO_FL_ADDR_TO_SET;
 }
 
+/* Retrieves the connection's original source address. Returns non-zero on
+ * success or zero on failure. The operation is only performed once and the
+ * address is stored in the connection for future use.
+ */
+static inline int conn_get_src(struct connection *conn)
+{
+       if (conn->flags & CO_FL_ADDR_FROM_SET)
+               return 1;
+
+       if (!conn_ctrl_ready(conn) || !conn->ctrl->get_src)
+               return 0;
+
+       if (conn->ctrl->get_src(conn->handle.fd, (struct sockaddr *)&conn->addr.from,
+                               sizeof(conn->addr.from),
+                               obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
+               return 0;
+       conn->flags |= CO_FL_ADDR_FROM_SET;
+       return 1;
+}
+
+/* Retrieves the connection's original destination address. Returns non-zero on
+ * success or zero on failure. The operation is only performed once and the
+ * address is stored in the connection for future use.
+ */
+static inline int conn_get_dst(struct connection *conn)
+{
+       if (conn->flags & CO_FL_ADDR_TO_SET)
+               return 1;
+
+       if (!conn_ctrl_ready(conn) || !conn->ctrl->get_dst)
+               return 0;
+
+       if (conn->ctrl->get_dst(conn->handle.fd, (struct sockaddr *)&conn->addr.to,
+                               sizeof(conn->addr.to),
+                               obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
+               return 0;
+       conn->flags |= CO_FL_ADDR_TO_SET;
+       return 1;
+}
+
 /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
  * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). The connection is tested and if
  * it is null, nothing is done.