Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix explicit 0.0.0.0 address binding #712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions QuickFIXn/ThreadedSocketAcceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,19 @@ private AcceptorSocketDescriptor GetAcceptorSocketDescriptor(Dictionary dict)
IPEndPoint socketEndPoint;
if (dict.Has(SessionSettings.SOCKET_ACCEPT_HOST))
{
string host = dict.GetString(SessionSettings.SOCKET_ACCEPT_HOST);
IPAddress[] addrs = Dns.GetHostAddresses(host);
socketEndPoint = new IPEndPoint(addrs[0], port);
// Set hostname (if it is not already configured)
socketSettings.ServerCommonName = socketSettings.ServerCommonName ?? host;
string host = dict.GetString(SessionSettings.SOCKET_ACCEPT_HOST);

if (IsAnyIpAddress(host))
{
socketEndPoint = new IPEndPoint(IPAddress.Any, port);
}
else
{
IPAddress[] addrs = Dns.GetHostAddresses(host);
socketEndPoint = new IPEndPoint(addrs[0], port);
// Set hostname (if it is not already configured)
socketSettings.ServerCommonName ??= host;
}
}
else
{
Expand All @@ -102,7 +110,6 @@ private AcceptorSocketDescriptor GetAcceptorSocketDescriptor(Dictionary dict)

socketSettings.Configure(dict);


if (!_socketDescriptorForAddress.TryGetValue(socketEndPoint, out var descriptor))
{
descriptor = new AcceptorSocketDescriptor(socketEndPoint, socketSettings, dict);
Expand All @@ -112,6 +119,18 @@ private AcceptorSocketDescriptor GetAcceptorSocketDescriptor(Dictionary dict)
return descriptor;
}

/// <summary>
/// Checks if the host address is the "any" address in either IPv4 (0.0.0.0) or IPv6 (::)
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
private static bool IsAnyIpAddress(string host)
{
return IPAddress.TryParse(host, out IPAddress? address) &&
(address.Equals(IPAddress.Any) ||
address.Equals(IPAddress.IPv6Any));
}

/// <summary>
/// Create session, either at start-up or as an ad-hoc operation
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ What's New
* #815 - update broken/neglected example apps & docs (gbirchmeier)
* #764 - fix positive UTC offset parsing in DateTimeConverter (Rob-Hague)
* #766 - use ordinal string operations (Rob-Hague)
* #767 - Avoid string conversions in FieldMap.Get{FieldType} where possible (Rob-Hague)
* #767 - avoid string conversions in FieldMap.Get{FieldType} where possible (Rob-Hague)
* #785 - use correct SocketError "Shutdown" code when socket is deliberately shutdown (oclancy)
* #711 - fix explicit 0.0.0.0 address binding (bohdanstefaniuk)

### v1.11.2:
* same as v1.11.1, but I fixed the readme in the pushed nuget packages
Expand Down
Loading