1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2024-09-18 01:33:00 +03:00

Bind outgoing connection to a specific IP address (avoid illegal access)

This commit is contained in:
hiura 2023-09-12 10:20:51 +09:00
parent c2fe874865
commit 643cbbbf88

View File

@ -13681,6 +13681,14 @@ char *PrintError(int ErrorCode)
Message = "Cannot assign requested address."; Message = "Cannot assign requested address.";
break; break;
case WSAEISCONN:
Message = "Socket is already connected."; // Added on AUG.10, 2023
break;
case WSAEINVAL:
Message = "Invalid argument."; // Added on AUG.10, 2023
break;
default: default:
Message = ""; Message = "";
break; break;
@ -13906,16 +13914,20 @@ SOCKET BindConnectTimeoutIPv4(IP* localIP, UINT localport, IP* ip, UINT port, UI
if (s != INVALID_SOCKET) { if (s != INVALID_SOCKET) {
int ier; int ier;
IP tmpIP; IP tmpIP;
IP *tmpIP2;
if (localIP == BIND_LOCALIP_NULL) { if (localIP == BIND_LOCALIP_NULL) {
StrToIP(&tmpIP, "0.0.0.0"); // A NULL address for the argument "localIP" is treated as if "0::0" in IPV4 was specified. StrToIP(&tmpIP, "0.0.0.0"); // A NULL address for the argument "localIP" is treated as if "0.0.0.0" in IPV4 was specified.
localIP = &tmpIP; tmpIP2 = &tmpIP;
}
else {
tmpIP2 = localIP;
} }
if ((IsZeroIP(localIP) == false) || (localport != 0)) { if ((IsZeroIP(tmpIP2) == false) || (localport != 0)) {
// Bind the socket // Bind the socket
if (bind_sock(s, localIP, localport) != 0) { if (bind_sock(s, tmpIP2, localport) != 0) {
#ifdef OS_WIN32 #ifdef OS_WIN32
ier = WSAGetLastError(); ier = WSAGetLastError();
Debug("IPv4 bind() failed with error: %d %s\n", ier, PrintError(ier)); Debug("IPv4 bind() failed with error: %d %s\n", ier, PrintError(ier));
@ -14635,16 +14647,20 @@ void BindConnectThreadForIPv6(THREAD* thread, void* param)
if (s != INVALID_SOCKET){ if (s != INVALID_SOCKET){
int ier; int ier;
IP tmpIP; IP tmpIP;
IP *tmpIP2;
if (p->LocalIP == BIND_LOCALIP_NULL) { if (p->LocalIP == BIND_LOCALIP_NULL) {
StrToIP(&tmpIP, "0::0"); // A NULL address for the argument "p->LocalIP" is treated as if "0::0" in IPV6 was specified. StrToIP(&tmpIP, "0::0"); // A NULL address for the argument "p->LocalIP" is treated as if "0::0" in IPV6 was specified.
p->LocalIP = &tmpIP; tmpIP2 = &tmpIP;
}
else {
tmpIP2 = p->LocalIP;
} }
if ((IsZeroIP(p->LocalIP) == false) || (p->LocalPort != 0)){ if ((IsZeroIP(tmpIP2) == false) || (p->LocalPort != 0)){
// Bind the socket // Bind the socket
if (bind_sock(s, p->LocalIP, p->LocalPort) != 0) { if (bind_sock(s, tmpIP2, p->LocalPort) != 0) {
#ifdef OS_WIN32 #ifdef OS_WIN32
ier = WSAGetLastError(); ier = WSAGetLastError();
Debug("IPv6 bind() failed with error: %d %s\n", ier, PrintError(ier)); Debug("IPv6 bind() failed with error: %d %s\n", ier, PrintError(ier));