Skip to content

Commit 07e142c

Browse files
committed
SOCK4: search all host addresses for the first IPv4 address.
1 parent cfd4125 commit 07e142c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Renci.SshNet/Session.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ internal void SendMessage(Message message)
954954
hash = _clientMac.ComputeHash(packetData);
955955
}
956956

957-
// Encrypt packet data
957+
// Encrypt packet data
958958
if (_clientCipher != null)
959959
{
960960
packetData = _clientCipher.Encrypt(packetData, packetDataOffset, (packetData.Length - packetDataOffset));
@@ -1952,9 +1952,9 @@ private void MessageListener()
19521952
break;
19531953
}
19541954
#elif FEATURE_SOCKET_POLL
1955-
// when Socket.Select(IList, IList, IList, Int32) is not available or is buggy, we use
1956-
// Socket.Poll(Int, SelectMode) to block until either data is available or the socket
1957-
// is closed
1955+
// when Socket.Select(IList, IList, IList, Int32) is not available or is buggy, we use
1956+
// Socket.Poll(Int, SelectMode) to block until either data is available or the socket
1957+
// is closed
19581958
_socket.Poll(-1, SelectMode.SelectRead);
19591959

19601960
if (!_socket.IsConnected())
@@ -2271,14 +2271,16 @@ private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port
22712271

22722272
private static byte[] GetSocks4DestinationAddress(string hostname)
22732273
{
2274-
var ip = DnsAbstraction.GetHostAddresses(hostname)[0];
2274+
var addresses = DnsAbstraction.GetHostAddresses(hostname);
22752275

2276-
if (ip.AddressFamily != AddressFamily.InterNetwork)
2276+
for (var i = 0; i < addresses.Length; i++)
22772277
{
2278-
throw new ProxyException("SOCKS4 only supports IPv4.");
2278+
var address = addresses[i];
2279+
if (address.AddressFamily == AddressFamily.InterNetwork)
2280+
return address.GetAddressBytes();
22792281
}
22802282

2281-
return ip.GetAddressBytes();
2283+
throw new ProxyException(string.Format("SOCKS4 only supports IPv4. No such address found for '{0}'.", hostname));
22822284
}
22832285

22842286
private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
@@ -2442,7 +2444,7 @@ private void Reset()
24422444
private static SshConnectionException CreateConnectionAbortedByServerException()
24432445
{
24442446
return new SshConnectionException("An established connection was aborted by the server.",
2445-
DisconnectReason.ConnectionLost);
2447+
DisconnectReason.ConnectionLost);
24462448
}
24472449

24482450
#region IDisposable implementation

0 commit comments

Comments
 (0)