Skip to content

Commit

Permalink
Binding to a specific NIC is implicitly implied when an IP address is…
Browse files Browse the repository at this point in the history
… specified instead of a hostname (#1596); should fix issue #1581

* Binding to a specific NIC is implicitly implied when an IP address is specified instead of a hostname; Removed the redundant flag BindToSpecifiedAddress.
  • Loading branch information
mrsuciu committed Nov 17, 2021
1 parent 0baea07 commit 4137cfd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
28 changes: 24 additions & 4 deletions Stack/Opc.Ua.Bindings.Https/Stack/Https/HttpsTransportListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,31 @@ public void Start()
#else
httpsOptions.SslProtocols = SslProtocols.None;
#endif
m_hostBuilder.UseKestrel(options => {
options.ListenAnyIP(m_uri.Port, listenOptions => {
listenOptions.UseHttps(httpsOptions);
bool bindToSpecifiedAddress = true;
UriHostNameType hostType = Uri.CheckHostName(m_uri.Host);
if (hostType == UriHostNameType.Dns || hostType == UriHostNameType.Unknown || hostType == UriHostNameType.Basic)
{
bindToSpecifiedAddress = false;
}

if (bindToSpecifiedAddress)
{
IPAddress ipAddress = IPAddress.Parse(m_uri.Host);
m_hostBuilder.UseKestrel(options => {
options.Listen(ipAddress, m_uri.Port, listenOptions => {
listenOptions.UseHttps(httpsOptions);
});
});
});
}
else
{
m_hostBuilder.UseKestrel(options => {
options.ListenAnyIP(m_uri.Port, listenOptions => {
listenOptions.UseHttps(httpsOptions);
});
});
}


m_hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
m_hostBuilder.UseStartup<Startup>();
Expand Down
3 changes: 1 addition & 2 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public abstract class TcpServiceHost : ITransportListenerFactory
}

serverBase.CreateServiceHostEndpoint(uri.Uri, listenerEndpoints, endpointConfiguration, listener,
configuration.CertificateValidator.GetChannelValidator()
);
configuration.CertificateValidator.GetChannelValidator());

endpoints.AddRange(listenerEndpoints);
}
Expand Down
59 changes: 37 additions & 22 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,23 @@ public void Start()
port = Utils.UaTcpDefaultPort;
}

// create IPv4 socket.
bool bindToSpecifiedAddress = true;
UriHostNameType hostType = Uri.CheckHostName(m_uri.Host);
if (hostType == UriHostNameType.Dns || hostType == UriHostNameType.Unknown || hostType == UriHostNameType.Basic)
{
bindToSpecifiedAddress = false;
}

IPAddress ipAddress = IPAddress.Any;
if (bindToSpecifiedAddress)
{
ipAddress = IPAddress.Parse(m_uri.Host);
}

// create IPv4 or IPv6 socket.
try
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);
IPEndPoint endpoint = new IPEndPoint(ipAddress, port);
m_listeningSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnAccept;
Expand All @@ -311,32 +324,34 @@ public void Start()
Utils.Trace("failed to create IPv4 listening socket: " + ex.Message);
}

// create IPv6 socket
try
if (ipAddress == IPAddress.Any)
{
IPEndPoint endpointIPv6 = new IPEndPoint(IPAddress.IPv6Any, port);
m_listeningSocketIPv6 = new Socket(endpointIPv6.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnAccept;
args.UserToken = m_listeningSocketIPv6;
m_listeningSocketIPv6.Bind(endpointIPv6);
m_listeningSocketIPv6.Listen(Int32.MaxValue);
if (!m_listeningSocketIPv6.AcceptAsync(args))
// create IPv6 socket
try
{
OnAccept(null, args);
IPEndPoint endpointIPv6 = new IPEndPoint(IPAddress.IPv6Any, port);
m_listeningSocketIPv6 = new Socket(endpointIPv6.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnAccept;
args.UserToken = m_listeningSocketIPv6;
m_listeningSocketIPv6.Bind(endpointIPv6);
m_listeningSocketIPv6.Listen(Int32.MaxValue);
if (!m_listeningSocketIPv6.AcceptAsync(args))
{
OnAccept(null, args);
}
}
}
catch (Exception ex)
{
// no IPv6 support
if (m_listeningSocketIPv6 != null)
catch (Exception ex)
{
m_listeningSocketIPv6.Dispose();
m_listeningSocketIPv6 = null;
// no IPv6 support
if (m_listeningSocketIPv6 != null)
{
m_listeningSocketIPv6.Dispose();
m_listeningSocketIPv6 = null;
}
Utils.Trace("failed to create IPv6 listening socket: " + ex.Message);
}
Utils.Trace("failed to create IPv6 listening socket: " + ex.Message);
}

if (m_listeningSocketIPv6 == null && m_listeningSocket == null)
{
throw ServiceResultException.Create(
Expand Down

0 comments on commit 4137cfd

Please sign in to comment.