Use VSA_getsockname() - avoid sockaddr_storage

avoids an second getsockname() in vbp_write_proxy_v1()
avoids a memcpy() in VTCP_my_suckaddr()
parent 32ea9c59
...@@ -251,20 +251,22 @@ vbp_write_proxy_v1(struct vbp_target *vt, int *sock) ...@@ -251,20 +251,22 @@ vbp_write_proxy_v1(struct vbp_target *vt, int *sock)
char buf[105]; /* maximum size for a TCP6 PROXY line with null char */ char buf[105]; /* maximum size for a TCP6 PROXY line with null char */
char addr[VTCP_ADDRBUFSIZE]; char addr[VTCP_ADDRBUFSIZE];
char port[VTCP_PORTBUFSIZE]; char port[VTCP_PORTBUFSIZE];
struct sockaddr_storage ss; char vsabuf[vsa_suckaddr_len];
struct suckaddr *sua;
int proto;
struct vsb vsb; struct vsb vsb;
socklen_t l;
VTCP_myname(*sock, addr, sizeof addr, port, sizeof port); sua = VSA_getsockname(*sock, vsabuf, sizeof vsabuf);
AN(sua);
VTCP_name(sua, addr, sizeof addr, port, sizeof port);
AN(VSB_init(&vsb, buf, sizeof buf)); AN(VSB_init(&vsb, buf, sizeof buf));
l = sizeof ss; proto = VSA_Get_Proto(sua);
AZ(getsockname(*sock, (void *)&ss, &l)); if (proto == AF_INET || proto == AF_INET6)
if (ss.ss_family == AF_INET || ss.ss_family == AF_INET6) {
VSB_printf(&vsb, "PROXY %s %s %s %s %s\r\n", VSB_printf(&vsb, "PROXY %s %s %s %s %s\r\n",
ss.ss_family == AF_INET ? "TCP4" : "TCP6", proto == AF_INET ? "TCP4" : "TCP6",
addr, addr, port, port); addr, addr, port, port);
} else else
VSB_cat(&vsb, "PROXY UNKNOWN\r\n"); VSB_cat(&vsb, "PROXY UNKNOWN\r\n");
AZ(VSB_finish(&vsb)); AZ(VSB_finish(&vsb));
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include "vdef.h" #include "vdef.h"
#include "vas.h" #include "vas.h"
...@@ -105,12 +106,8 @@ VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen, ...@@ -105,12 +106,8 @@ VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen,
struct suckaddr * struct suckaddr *
VTCP_my_suckaddr(int sock) VTCP_my_suckaddr(int sock)
{ {
struct sockaddr_storage addr_s; return (VSA_getsockname(sock,
socklen_t l; malloc(vsa_suckaddr_len), vsa_suckaddr_len));
l = sizeof addr_s;
AZ(getsockname(sock, (void *)&addr_s, &l));
return (VSA_Malloc(&addr_s, l));
} }
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
...@@ -118,12 +115,10 @@ VTCP_my_suckaddr(int sock) ...@@ -118,12 +115,10 @@ VTCP_my_suckaddr(int sock)
void void
VTCP_myname(int sock, char *abuf, unsigned alen, char *pbuf, unsigned plen) VTCP_myname(int sock, char *abuf, unsigned alen, char *pbuf, unsigned plen)
{ {
struct sockaddr_storage addr_s; char buf[vsa_suckaddr_len];
socklen_t l;
l = sizeof addr_s; VTCP_name(VSA_getsockname(sock, buf, sizeof buf),
AZ(getsockname(sock, (void *)&addr_s, &l)); abuf, alen, pbuf, plen);
vtcp_sa_to_ascii(&addr_s, l, abuf, alen, pbuf, plen);
} }
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment