Skip to content

Commit

Permalink
Merge pull request #712 from bohdanstefaniuk/fix-any-ip-address-binding
Browse files Browse the repository at this point in the history
Fix explicit 0.0.0.0 address binding
  • Loading branch information
gbirchmeier authored Jan 25, 2024
2 parents 07a94c5 + 04554ec commit b5af015
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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

0 comments on commit b5af015

Please sign in to comment.