code cleanup

This commit is contained in:
Yves Rutschle 2025-03-29 17:34:25 +01:00
parent 2f111b6b8d
commit 416a82fcc6
4 changed files with 40 additions and 18 deletions

View File

@ -498,7 +498,7 @@ void connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking
cnx->q[1].fd = fd;
if (cnx->proto->proxyprotocol_is_present) {
int res = pp_write_header(cnx->proto->proxyprotocol, cnx);
pp_write_header(cnx->proto->proxyprotocol, cnx);
/* If pp_write_header() fails, it already logs a message and there is
* nothing much we can do. The server side will probably close the
* connection */

View File

@ -44,6 +44,32 @@ static int family_to_pp(int af_family)
}
}
typedef char libpp_addr[108];
/* 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)
{
char serv_str[NI_MAXSERV];
int res;
res = getpeername(fd, addr->ai_addr, &addr->ai_addrlen);
CHECK_RES_RETURN(res, "getpeername", -1);
res = getnameinfo(addr->ai_addr, addr->ai_addrlen,
(char*)host, sizeof(*host),
serv_str, sizeof(serv_str),
NI_NUMERICHOST | NI_NUMERICSERV );
CHECK_RES_RETURN(res, "getnameinfo", -1);
*serv = atoi(serv_str);
return 0;
}
int pp_write_header(int pp_version, struct connection* cnx)
{
pp_info_t pp_info_in_v1 = {
@ -54,28 +80,24 @@ int pp_write_header(int pp_version, struct connection* cnx)
struct sockaddr_storage ss;
struct addrinfo addr;
char host[NI_MAXHOST], serv[NI_MAXSERV];
int res;
addr.ai_addr = (struct sockaddr*)&ss;
addr.ai_addrlen = sizeof(ss);
res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
res = getnameinfo(addr.ai_addr, addr.ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV );
memcpy(pp_info_in_v1.src_addr, host, sizeof(pp_info_in_v1.src_addr));
pp_info_in_v1.src_port = atoi(serv);
res = get_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 = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
res = getnameinfo(addr.ai_addr, addr.ai_addrlen,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV );
memcpy(pp_info_in_v1.dst_addr, host, sizeof(pp_info_in_v1.dst_addr));
pp_info_in_v1.dst_port = atoi(serv);
res = get_info(cnx->q[1].fd,
&addr,
&pp_info_in_v1.dst_addr,
&pp_info_in_v1.dst_port
);
if (res == -1) return -1;
uint8_t *pp1_hdr = pp_create_hdr(pp_version, &pp_info_in_v1, &pp1_hdr_len, &error);

View File

@ -45,7 +45,7 @@ protocols:
(
{ name: "ssh"; host: "localhost"; port: "9000"; fork: true; transparent: true; resolve_on_forward: true; },
{ name: "socks5"; host: "localhost"; port: "9001"; },
{ name: "http"; host: "www.lemonde.fr"; port: "80"; proxyprotocol: 1; },
{ name: "http"; host: "localhost"; port: "80"; proxyprotocol: 1; },
{ name: "tinc"; host: "localhost"; port: "9003"; },
{ name: "openvpn"; host: "localhost"; port: "9004"; },
{ name: "xmpp"; host: "localhost"; port: "9009"; },

View File

@ -1,5 +1,5 @@
#ifndef VERSION_H
#define VERSION_H
#define VERSION "v2.1.4-37-g951b708-dirty"
#define VERSION "v2.1.4-39-g2f111b6-dirty"
#endif