Cleanup error checking logic in bind_peer() (#412)

Thanks for the cleanup!
This commit is contained in:
Latchezar Tzvetkoff 2023-11-15 23:02:21 +02:00 committed by GitHub
parent 90a55b6f9d
commit 7499c26e9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

2
.gitignore vendored
View File

@ -3,8 +3,10 @@
*.o *.o
cscope.* cscope.*
echosrv echosrv
libsslh.a
sslh-fork sslh-fork
sslh-select sslh-select
sslh-ev
sslh.8.gz sslh.8.gz
tags tags
version.h version.h

View File

@ -110,7 +110,7 @@ int make_listen_tfo(int s)
return setsockopt(s, SOL_SOCKET, TCP_FASTOPEN, (char*)&qlen, sizeof(qlen)); return setsockopt(s, SOL_SOCKET, TCP_FASTOPEN, (char*)&qlen, sizeof(qlen));
} }
/* Starts listening on a single address /* Starts listening on a single address
* Returns a socket filehandle, or dies with message in case of major error */ * Returns a socket filehandle, or dies with message in case of major error */
int listen_single_addr(struct addrinfo* addr, int keepalive, int udp) int listen_single_addr(struct addrinfo* addr, int keepalive, int udp)
{ {
@ -262,7 +262,7 @@ int bind_peer(int fd, int fd_from)
/* if the destination is the same machine, there's no need to do bind */ /* if the destination is the same machine, there's no need to do bind */
if (is_same_machine(&from)) if (is_same_machine(&from))
return 0; return 0;
#ifndef IP_BINDANY /* use IP_TRANSPARENT */ #ifndef IP_BINDANY /* use IP_TRANSPARENT */
res = setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &trans, sizeof(trans)); res = setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &trans, sizeof(trans));
CHECK_RES_DIE(res, "setsockopt IP_TRANSPARENT"); CHECK_RES_DIE(res, "setsockopt IP_TRANSPARENT");
@ -278,10 +278,13 @@ int bind_peer(int fd, int fd_from)
} }
#endif /* IP_TRANSPARENT / IP_BINDANY */ #endif /* IP_TRANSPARENT / IP_BINDANY */
res = bind(fd, from.ai_addr, from.ai_addrlen); res = bind(fd, from.ai_addr, from.ai_addrlen);
if (res == -1 && errno != EADDRINUSE) { if (res == -1) {
CHECK_RES_RETURN(res, "bind", res); if (errno != EADDRINUSE) {
} print_message(msg_system_error, "%s:%d:%s:%d:%s\n", __FILE__, __LINE__,
else if (res == -1 ) { "bind", errno, strerror(errno));
return res;
}
/* /*
* If there is more than one transparent mode proxy going on, such as * If there is more than one transparent mode proxy going on, such as
* using sslh as the target of stunnel also in transparent mode, then * using sslh as the target of stunnel also in transparent mode, then
@ -291,9 +294,7 @@ int bind_peer(int fd, int fd_from)
* have changed, but most people won't care. * have changed, but most people won't care.
* Also note that stunnel uses the same logic for the same situation. * Also note that stunnel uses the same logic for the same situation.
*/ */
struct sockaddr_in *sin; ((struct sockaddr_in *)from.ai_addr)->sin_port = 0;
sin = from.ai_addr;
sin->sin_port = 0; /* auto-pick an unused high port */
res = bind(fd, from.ai_addr, from.ai_addrlen); res = bind(fd, from.ai_addr, from.ai_addrlen);
CHECK_RES_RETURN(res, "bind", res); CHECK_RES_RETURN(res, "bind", res);
} }
@ -866,4 +867,3 @@ void write_pid_file(const char* pidfile)
exit(3); exit(3);
} }
} }