fixed destination ip and port in proxyprotocol

This commit is contained in:
4O4-Not-F0und 2025-05-23 12:06:39 +08:00 committed by GitHub
parent ad1f5d68e9
commit f43ded9ce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -53,7 +53,7 @@ typedef char libpp_addr[108]; /* This is hardcoded in libproxyprotocol/proxy_pro
/* Fills *addr, *host and *serv with the connection information corresponding
* to fd. *host is the IP address as string and *serv is the service (port)
* */
static int get_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* serv)
static int get_peer_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* serv)
{
char serv_str[NI_MAXSERV];
int res;
@ -73,6 +73,33 @@ static int get_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* s
}
/* Fills *addr, *host and *serv with the LOCAL connection information corresponding
* to fd. *host is the IP address as string and *serv is the service (port)
* */
static int get_local_sock_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* serv)
{
char serv_str[NI_MAXSERV];
int res;
// addr->ai_addr should point to a suitable sockaddr
// addr->ai_addrlen should be initialized to its size
res = getsockname(fd, addr->ai_addr, &addr->ai_addrlen);
CHECK_RES_RETURN(res, "getsockname", -1);
res = getnameinfo(addr->ai_addr, addr->ai_addrlen,
(char*)host, sizeof(*host),
serv_str, sizeof(serv_str),
NI_NUMERICHOST | NI_NUMERICSERV);
if (res != 0) {
print_message(msg_system_error, "getnameinfo for local sock: %s\n", gai_strerror(res));
return -1;
}
*serv = atoi(serv_str);
return 0;
}
int pp_write_header(int pp_version, struct connection* cnx)
{
@ -89,14 +116,14 @@ int pp_write_header(int pp_version, struct connection* cnx)
addr.ai_addr = (struct sockaddr*)&ss;
addr.ai_addrlen = sizeof(ss);
res = get_info(cnx->q[0].fd,
res = get_peer_info(cnx->q[0].fd,
&addr,
&pp_info_in_v1.src_addr,
&pp_info_in_v1.src_port);
if (res == -1) return -1;
pp_info_in_v1.address_family = family_to_pp(addr.ai_addr->sa_family);
res = get_info(cnx->q[1].fd,
res = get_local_sock_info(cnx->q[0].fd,
&addr,
&pp_info_in_v1.dst_addr,
&pp_info_in_v1.dst_port
@ -117,4 +144,4 @@ int pp_write_header(int pp_version, struct connection* cnx)
return 0;
}
#endif /* HAVE_PROXYPROTOCOL */
#endif /* HAVE_PROXYPROTOCOL */