4

I have an incoming FTP request. I would like to get the IP Address of the FTP server mentioned in the incoming FTP request. I have to validate this against a list of whitelisted FTP servers.

Any help will be well appreciated..

My code is as follows:

try
{
    IPHostEntry host;
    string localIP = "?";
    host = Dns.GetHostEntry(uri);
    foreach (IPAddress ip in host.AddressList)
    {
        // we are only interested in IPV4 Addresses
        if (ip.AddressFamily == AddressFamily.InterNetwork) 
        {
            localIP = ip.ToString();
        }
    }

    return localIP;
}
catch (Exception exception)
{
    throw;
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
Gagan
  • 5,416
  • 13
  • 58
  • 86
  • well most of the code that I was using was from stackoverflow.. these were the links that i followed http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c http://blogs.x2line.com/al/archive/2008/08/29/3544.aspx – Gagan Aug 10 '11 at 20:38
  • Please can you clarify / edit so I know exactly what you need? – m.edmondson Aug 10 '11 at 20:42
  • Are you servicing a FTP request or making one? Code you posted is not at all relevant. – YetAnotherUser Aug 10 '11 at 20:47
  • I am making a FTPRequest. The code that I have is how I try to get the IP Address given a FTP URL. FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath)); – Gagan Aug 10 '11 at 20:50
  • Then this code should work - what you are doing is filtering the address to just IPv4 addresses which might be filtering your actual server. – YetAnotherUser Aug 10 '11 at 20:56
  • Nope.. I am pretty sure that the server is IPv4. I am pretty sure , because I have a test FTP server on my own machine.. and then I am using the FTP address of my local machine as an input for testing purposes... – Gagan Aug 10 '11 at 20:59
  • the url is a regular ftp of the format ftp://ftpserver – Gagan Aug 11 '11 at 01:24

1 Answers1

5

Ok here is my hack..

private string GetFTPAddress(string uri)
{
    try
    {
       // IPHostEntry host;
        string localIP = null;
        var entries = uri.Split('/');
        var host = Dns.GetHostAddresses(entries[2]);
        foreach (IPAddress ip in host)
        {
            // we are only interested in IPV4 Addresses
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
            }
        }

        return localIP;
    }
    catch (Exception exception)
    {
        throw;
    }
}
Abel
  • 56,041
  • 24
  • 146
  • 247
Gagan
  • 5,416
  • 13
  • 58
  • 86