forked from robinrodricks/FluentFTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Locally bind to specific address via new LocalIpAddress property
- Loading branch information
1 parent
8c3cbc6
commit 04c0371
Showing
6 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpExamples | ||
{ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
using FluentFTP; | ||
|
||
public static class LocalIpAddress | ||
{ | ||
public static void LocaIpAddressExample() | ||
{ | ||
// IP addresses for current host inside myprivatedomain | ||
var localIpAddresses = new[] | ||
{ | ||
IPAddress.Parse("10.244.191.143"), | ||
IPAddress.Parse("fcec:177:cfbd:6555:8f8c::1") | ||
}; | ||
|
||
foreach (var localIpAddress in localIpAddresses) | ||
{ | ||
// let's say that ftp.myprivatedomain has ipv4 and ipv5 addresses | ||
using (var f = new FtpClient("ftp.myprivatedomain", "test", "test") | ||
{ | ||
InternetProtocolVersions = localIpAddress.AddressFamily == AddressFamily.InterNetworkV6 ? FtpIpVersion.IPv6 : FtpIpVersion.IPv4, | ||
|
||
// Equivalent to lftp's ftp:port-ipv[4|6] and net:socket-bind-ipv[4|6] (see http://manpages.org/lftp) | ||
LocalIpAddress = localIpAddress | ||
}) | ||
{ | ||
f.Connect(); | ||
|
||
Console.WriteLine($"Connected to {f.RemoteEndPoint} from {f.LocalEndPoint}"); | ||
foreach (var file in f.GetListing()) | ||
{ | ||
Console.Out.WriteLine(file); | ||
} | ||
|
||
f.Disconnect(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="LocalPorts.cs" company=""> | ||
// | ||
// </copyright> | ||
// <summary> | ||
// The local ports. | ||
// </summary> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace FluentFTP.Helpers | ||
{ | ||
#region Usings | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// The local ports. | ||
/// </summary> | ||
public static class LocalPorts | ||
{ | ||
/// <summary> | ||
/// The r. | ||
/// </summary> | ||
internal static readonly Random R = new Random(); | ||
|
||
/// <summary> | ||
/// Get available. | ||
/// </summary> | ||
/// <param name="localIpAddress"> | ||
/// The local ip address. | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="int"/>. | ||
/// </returns> | ||
public static int GetRandomAvailable(IPAddress localIpAddress) | ||
{ | ||
lock (R) | ||
{ | ||
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); | ||
var tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners(); | ||
var inUsePorts = new HashSet<int>( | ||
tcpConnInfoArray.Where(ipEndPoint => localIpAddress.Equals(ipEndPoint.Address)) | ||
.Select(ipEndPoint => ipEndPoint.Port)); | ||
int localPort; | ||
do | ||
{ | ||
localPort = 1025 + R.Next(32000); | ||
} | ||
while (inUsePorts.Contains(localPort)); | ||
|
||
return localPort; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters