Skip to content

Commit 332119f

Browse files
Improve host resolution at FtpService to align with previous behavior. (#948)
(cherry picked from commit d04b027)
1 parent 524d23d commit 332119f

File tree

1 file changed

+23
-10
lines changed
  • dotnet/src/dotnetframework/GxClasses/Domain

1 file changed

+23
-10
lines changed

dotnet/src/dotnetframework/GxClasses/Domain/GxFtp.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using System.Linq;
99
using GeneXus.Encryption;
1010
using GeneXus.Configuration;
11+
using log4net;
1112

1213
namespace GeneXus.Utils
1314
{
1415
[SecuritySafeCritical]
1516
public class FtpService
1617
{
18+
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(FtpService));
1719

1820
private Socket _DataSocket;
1921
private Socket _ControlSocket;
@@ -511,8 +513,7 @@ private void OpenDataConnection()
511513
}
512514
_DataSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
513515

514-
IPHostEntry localHostEntry = Dns.GetHostEntry(Dns.GetHostName());
515-
IPAddress hostAddress = localHostEntry.AddressList.First(a => (a.AddressFamily == _DataSocket.AddressFamily)); ;
516+
IPAddress hostAddress = GetIpAddress(Dns.GetHostName());
516517
IPEndPoint epListener = new IPEndPoint(hostAddress, 0);
517518
_DataSocket.Bind(epListener);
518519

@@ -565,8 +566,7 @@ private void OpenPassiveDataConnection()
565566
{
566567
ProcessProtocolViolationError("Error in creating Data Socket");
567568
}
568-
IPHostEntry serverHostEntry = Dns.GetHostEntry(_RequestUri.Host);
569-
IPAddress hostAddress = serverHostEntry.AddressList.First(a => (a.AddressFamily == _DataSocket.AddressFamily)); ; ;
569+
IPAddress hostAddress = GetIpAddress(_RequestUri.Host);
570570
IPEndPoint serverEndPoint = new IPEndPoint(hostAddress, Port);
571571

572572
try
@@ -631,10 +631,8 @@ private void OpenControlConnection(Uri uriToConnect)
631631
clientEndPoint = _ControlSocket.LocalEndPoint;
632632
try
633633
{
634-
IPHostEntry serverHostEntry = Dns.GetHostEntry(Host);
635-
IPAddress ipAddresses = serverHostEntry.AddressList.First(a => (a.AddressFamily == _ControlSocket.AddressFamily));
634+
IPAddress ipAddresses = GetIpAddress(Host);
636635
IPEndPoint serverEndPoint = new IPEndPoint(ipAddresses, Port);
637-
638636
try
639637
{
640638
if (GXUtil.IsWindowsPlatform)
@@ -679,6 +677,21 @@ private void OpenControlConnection(Uri uriToConnect)
679677
}
680678
}
681679

680+
private IPAddress GetIpAddress(string host)
681+
{
682+
IPHostEntry serverHostEntry = Dns.GetHostEntry(host);
683+
IPAddress ipAddresses = serverHostEntry.AddressList.FirstOrDefault();
684+
GXLogging.Debug(log, $"GetHostEntry({host}) AddressList length: ", serverHostEntry.AddressList.Length.ToString());
685+
if (ipAddresses == null)
686+
{
687+
serverHostEntry = Dns.GetHostByName(host);
688+
ipAddresses = serverHostEntry.AddressList.FirstOrDefault();
689+
GXLogging.Debug(log, $"GetHostByName({host}) AddressList length: ", serverHostEntry.AddressList.Length.ToString());
690+
}
691+
GXLogging.Debug(log, "HostAddress ", ipAddresses.ToString());
692+
return ipAddresses;
693+
}
694+
682695
void CloseDataConnection()
683696
{
684697
if(_DataSocket != null)
@@ -976,19 +989,19 @@ internal void ProcessApplicationError( string s)
976989
{
977990
this._status = 1;
978991
this._statusDescription = s;
979-
992+
GXLogging.Error(log, "ProcessApplicationError " + s);
980993
}
981994
internal void ProcessApplicationError( string s, Exception e)
982995
{
983996
this._status = 1;
984997
this._statusDescription = s + e.Message;
985-
998+
GXLogging.Error(log, "ProcessApplicationError " + s, e);
986999
}
9871000
internal void ProcessProtocolViolationError( string s)
9881001
{
9891002
this._status = 1;
9901003
this._statusDescription = new ProtocolViolationException(s).ToString();
991-
1004+
GXLogging.Warn(log, "ProcessProtocolViolationError ", this._statusDescription);
9921005
}
9931006
internal string ComposeExceptionMessage(ResponseDescription resp_desc, string log)
9941007
{

0 commit comments

Comments
 (0)