From 416a82fcc6415a7415d6e5bd743abf967e4f47db Mon Sep 17 00:00:00 2001 From: Yves Rutschle Date: Sat, 29 Mar 2025 17:34:25 +0100 Subject: [PATCH] code cleanup --- common.c | 2 +- proxyprotocol.c | 52 +++++++++++++++++++++++++++++++++++-------------- test.cfg | 2 +- version.h | 2 +- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/common.c b/common.c index 680e755..f22f732 100644 --- a/common.c +++ b/common.c @@ -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 */ diff --git a/proxyprotocol.c b/proxyprotocol.c index 549d3dc..b5033ca 100644 --- a/proxyprotocol.c +++ b/proxyprotocol.c @@ -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); diff --git a/test.cfg b/test.cfg index 314b420..4c4342c 100644 --- a/test.cfg +++ b/test.cfg @@ -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"; }, diff --git a/version.h b/version.h index 5eadcd4..11c3bc4 100644 --- a/version.h +++ b/version.h @@ -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