Bind local IPEndPoint
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.4k
- Forks
- 993
- Avg merge
- 9d 21h
- Merged PRs (30d)
- 1
Description
I need to be able to specify the local IP address a connection binds to when establishing the sessions tcp connection.
Currently the operating system uses the outbound interface's IP Address to source a connect from. In multi-homed situations, the selected interface's IP address may not be allowed to connect to the target address.
I have tested a work around in the code in the Session classes SocketConnect method.
partial void SocketConnect(string host, int port)
{
...
...
var ep = new IPEndPoint(ipAddress, port);
_socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, socketBufferSize);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, socketBufferSize);
// Begin local binding
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
{
if (StaticBinding.BindToIPAddress != null)
{
_socket.Bind(new IPEndPoint(StaticBinding.BindToIPAddress, 0));
}
}
else if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
if (StaticBinding.BindToIPv6Address != null)
{
_socket.Bind(new IPEndPoint(StaticBinding.BindToIPv6Address, 0));
}
}
// End local binding
Log(string.Format("Initiating connect to '{0}:{1}'.", ConnectionInfo.Host, ConnectionInfo.Port));
...
...
public class StaticBinding
{
public static System.Net.IPAddress BindToIPAddress { get; set; }
public static System.Net.IPAddress BindToIPv6Address { get; set; }
}
I don't think using the static properties is necessarily the correct approach. It's just how I'm handling this workaround at the moment. Also, my use of separate IPv4 and IPv6 binding properties might be simplified to a single IP Address which has its AddressFamily set appropriately.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Session classes' SocketConnect method and review how connection options are currently represented. Define how a caller should provide a local IPv4 or IPv6 address, then verify that TCP connections bind to the requested address before connecting; the issue does not mention a specific test file.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100