MINOR: connection: make sockaddr_alloc() take the address to be copied
Roughly half of the calls to sockadr_alloc() are made to copy an already
known address. Let's optionally pass it in argument so that the function
can handle the copy at the same time, this slightly simplifies its usage.
diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h
index b99df4d..b735a6e 100644
--- a/include/haproxy/connection.h
+++ b/include/haproxy/connection.h
@@ -357,9 +357,11 @@
* returns it. If <sap> is NULL, the address is always allocated and returned.
* if <sap> is non-null, an address will only be allocated if it points to a
* non-null pointer. In this case the allocated address will be assigned there.
- * In both situations the new pointer is returned.
+ * If <orig> is non-null and <len> positive, the address in <sa> will be copied
+ * into the allocated address. In both situations the new pointer is returned.
*/
-static inline struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap)
+static inline struct sockaddr_storage *
+sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len)
{
struct sockaddr_storage *sa;
@@ -367,6 +369,8 @@
return *sap;
sa = pool_alloc(pool_head_sockaddr);
+ if (sa && orig && len > 0)
+ memcpy(sa, orig, len);
if (sap)
*sap = sa;
return sa;
@@ -535,7 +539,7 @@
if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_src)
return 0;
- if (!sockaddr_alloc(&conn->src))
+ if (!sockaddr_alloc(&conn->src, NULL, 0))
return 0;
if (conn->ctrl->fam->get_src(conn->handle.fd, (struct sockaddr *)conn->src,
@@ -558,7 +562,7 @@
if (!conn_ctrl_ready(conn) || !conn->ctrl->fam->get_dst)
return 0;
- if (!sockaddr_alloc(&conn->dst))
+ if (!sockaddr_alloc(&conn->dst, NULL, 0))
return 0;
if (conn->ctrl->fam->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst,
diff --git a/src/backend.c b/src/backend.c
index 99f179d..789bece 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -845,7 +845,7 @@
DPRINTF(stderr,"assign_server_address : s=%p\n",s);
- if (!sockaddr_alloc(&s->target_addr))
+ if (!sockaddr_alloc(&s->target_addr, NULL, 0))
return SRV_STATUS_INTERNAL;
if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
@@ -1056,7 +1056,7 @@
else
return;
- if (!sockaddr_alloc(&srv_conn->src))
+ if (!sockaddr_alloc(&srv_conn->src, NULL, 0))
return;
switch (src->opts & CO_SRC_TPROXY_MASK) {
@@ -1411,7 +1411,7 @@
}
}
- if (!srv_conn || !sockaddr_alloc(&srv_conn->dst)) {
+ if (!srv_conn || !sockaddr_alloc(&srv_conn->dst, 0, 0)) {
if (srv_conn)
conn_free(srv_conn);
return SF_ERR_RESOURCE;
diff --git a/src/connection.c b/src/connection.c
index 1274112..dc54b9d 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -501,7 +501,7 @@
if (!conn_ctrl_ready(conn))
goto fail;
- if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
+ if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
goto fail;
if (!fd_recv_ready(conn->handle.fd))
@@ -871,7 +871,7 @@
if (!conn_ctrl_ready(conn))
goto fail;
- if (!sockaddr_alloc(&conn->src) || !sockaddr_alloc(&conn->dst))
+ if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
goto fail;
if (!fd_recv_ready(conn->handle.fd))
diff --git a/src/hlua.c b/src/hlua.c
index e7cac65..9616a75 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -2564,11 +2564,10 @@
si = appctx->owner;
s = si_strm(si);
- if (!sockaddr_alloc(&s->target_addr)) {
+ if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
xref_unlock(&socket->xref, peer);
WILL_LJMP(luaL_error(L, "connect: internal error"));
}
- *s->target_addr = *addr;
s->flags |= SF_ADDR_SET;
hlua = hlua_gethlua(L);
diff --git a/src/http_ana.c b/src/http_ana.c
index 142a0bf..870682f 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -754,7 +754,7 @@
struct htx_sl *sl;
struct ist uri, path;
- if (!sockaddr_alloc(&s->target_addr)) {
+ if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
if (!(s->flags & SF_ERR_MASK))
s->flags |= SF_ERR_RESOURCE;
goto return_int_err;
diff --git a/src/peers.c b/src/peers.c
index 1b05dbd..18df3cd 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -2574,9 +2574,8 @@
/* initiate an outgoing connection */
s->target = peer_session_target(peer, s);
- if (!sockaddr_alloc(&s->target_addr))
+ if (!sockaddr_alloc(&s->target_addr, &peer->addr, sizeof(peer->addr)))
goto out_free_strm;
- *s->target_addr = peer->addr;
s->flags = SF_ASSIGNED|SF_ADDR_SET;
s->si[1].flags |= SI_FL_NOLINGER;
diff --git a/src/session.c b/src/session.c
index da271cf..f6e4fe4 100644
--- a/src/session.c
+++ b/src/session.c
@@ -147,11 +147,10 @@
if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL))
goto out_close;
- if (!sockaddr_alloc(&cli_conn->src))
+ if (!sockaddr_alloc(&cli_conn->src, addr, sizeof(*addr)))
goto out_free_conn;
cli_conn->handle.fd = cfd;
- *cli_conn->src = *addr;
cli_conn->flags |= CO_FL_ADDR_FROM_SET;
cli_conn->proxy_netns = l->rx.settings->netns;
diff --git a/src/sink.c b/src/sink.c
index 64bac61..b87ea28 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -649,9 +649,8 @@
s->target = &sft->srv->obj_type;
- if (!sockaddr_alloc(&s->target_addr))
+ if (!sockaddr_alloc(&s->target_addr, &sft->srv->addr, sizeof(sft->srv->addr)))
goto out_free_strm;
- *s->target_addr = sft->srv->addr;
s->flags = SF_ASSIGNED|SF_ADDR_SET;
s->si[1].flags |= SI_FL_NOLINGER;
diff --git a/src/tcpcheck.c b/src/tcpcheck.c
index b9ef380..3142e78 100644
--- a/src/tcpcheck.c
+++ b/src/tcpcheck.c
@@ -1028,7 +1028,7 @@
check->wait_list.events = 0;
/* no client address */
- if (!sockaddr_alloc(&conn->dst)) {
+ if (!sockaddr_alloc(&conn->dst, NULL, 0)) {
status = SF_ERR_RESOURCE;
goto fail_check;
}