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

Ensure endpoints are returned when alternate url port does not match #2511

Merged
merged 3 commits into from
Feb 17, 2024
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
4 changes: 2 additions & 2 deletions Libraries/Opc.Ua.Client/CoreClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static EndpointDescription SelectEndpoint(
EndpointDescription endpoint = endpoints[ii];

// check for a match on the URL scheme.
if (endpoint.EndpointUrl.StartsWith(url.Scheme))
if (endpoint.EndpointUrl.StartsWith(url.Scheme, StringComparison.Ordinal))
{
// check if security was requested.
if (useSecurity)
Expand Down Expand Up @@ -297,7 +297,7 @@ public static EndpointDescription SelectEndpoint(
// pick the first available endpoint by default.
if (selectedEndpoint == null && endpoints.Count > 0)
{
selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme) == true);
selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme, StringComparison.Ordinal) == true);
}

// return the selected endpoint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

using System;
using System.Collections.Generic;

namespace Opc.Ua
{
Expand Down
127 changes: 83 additions & 44 deletions Stack/Opc.Ua.Core/Stack/Server/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Logging;
using Opc.Ua.Bindings;
using System.Net.Sockets;

namespace Opc.Ua
{
Expand Down Expand Up @@ -934,7 +936,28 @@ protected string NormalizeHostname(string hostname)
}

// substitute the computer name for any local IP if an IP is used by client.
IPAddress[] addresses = Utils.GetHostAddresses(Utils.GetHostName());
IPAddress[] addresses = Array.Empty<IPAddress>();
try
{
addresses = Utils.GetHostAddresses(computerName);
}
catch (SocketException e)
{
Utils.LogWarning(e, "Unable to get host addresses for hostname {0}.", hostname);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have been computerName


if (addresses.Length == 0)
{
string fullName = Dns.GetHostName();
try
{
addresses = Utils.GetHostAddresses(fullName);
}
catch (SocketException e)
{
Utils.LogError(e, "Unable to get host addresses for DNS hostname {0}.", fullName);
}
}

for (int ii = 0; ii < addresses.Length; ii++)
{
Expand All @@ -955,7 +978,7 @@ protected string NormalizeHostname(string hostname)
{
entry = Dns.GetHostEntry(computerName);
}
catch (System.Net.Sockets.SocketException e)
catch (SocketException e)
{
Utils.LogError(e, "Unable to check aliases for hostname {0}.", computerName);
}
Expand Down Expand Up @@ -1023,7 +1046,10 @@ protected IList<BaseAddress> FilterByEndpointUrl(Uri endpointUrl, IList<BaseAddr
{
if (alternateUrl.DnsSafeHost == endpointUrl.DnsSafeHost)
{
accessibleAddresses.Add(new BaseAddress() { Url = alternateUrl, ProfileUri = baseAddress.ProfileUri, DiscoveryUrl = alternateUrl });
if (!accessibleAddresses.Any(item => item.Url == alternateUrl))
{
accessibleAddresses.Add(new BaseAddress() { Url = alternateUrl, ProfileUri = baseAddress.ProfileUri, DiscoveryUrl = alternateUrl });
}
break;
}
}
Expand Down Expand Up @@ -1133,59 +1159,72 @@ protected EndpointDescriptionCollection TranslateEndpointDescriptions(
{
EndpointDescriptionCollection translations = new EndpointDescriptionCollection();

// process endpoints
foreach (EndpointDescription endpoint in endpoints)
bool matchPort = false;
do
{
UriBuilder endpointUrl = new UriBuilder(endpoint.EndpointUrl);
// first round with port match
matchPort = !matchPort;

// find matching base address.
foreach (BaseAddress baseAddress in baseAddresses)
// process endpoints
foreach (EndpointDescription endpoint in endpoints)
{
bool translateHttpsEndpoint = false;
if (endpoint.TransportProfileUri == Profiles.HttpsBinaryTransport && baseAddress.ProfileUri == Profiles.HttpsBinaryTransport)
{
translateHttpsEndpoint = true;
}
UriBuilder endpointUrl = new UriBuilder(endpoint.EndpointUrl);

if (endpoint.TransportProfileUri != baseAddress.ProfileUri && !translateHttpsEndpoint)
// find matching base address.
foreach (BaseAddress baseAddress in baseAddresses)
{
continue;
}
bool translateHttpsEndpoint = false;
if (endpoint.TransportProfileUri == Profiles.HttpsBinaryTransport && baseAddress.ProfileUri == Profiles.HttpsBinaryTransport)
{
translateHttpsEndpoint = true;
}

if ((endpointUrl.Scheme != baseAddress.Url.Scheme) || (endpointUrl.Port != baseAddress.Url.Port))
{
continue;
}
if (endpoint.TransportProfileUri != baseAddress.ProfileUri && !translateHttpsEndpoint)
{
continue;
}

EndpointDescription translation = new EndpointDescription();
if (endpointUrl.Scheme != baseAddress.Url.Scheme)
{
continue;
}

translation.EndpointUrl = baseAddress.Url.ToString();
// try to match port in the first round, skip in the second round
if (matchPort && endpointUrl.Port != baseAddress.Url.Port)
{
continue;
}

if (endpointUrl.Path.StartsWith(baseAddress.Url.PathAndQuery, StringComparison.Ordinal) &&
endpointUrl.Path.Length > baseAddress.Url.PathAndQuery.Length)
{
string suffix = endpointUrl.Path.Substring(baseAddress.Url.PathAndQuery.Length);
translation.EndpointUrl += suffix;
}
EndpointDescription translation = new EndpointDescription();

translation.ProxyUrl = endpoint.ProxyUrl;
translation.SecurityLevel = endpoint.SecurityLevel;
translation.SecurityMode = endpoint.SecurityMode;
translation.SecurityPolicyUri = endpoint.SecurityPolicyUri;
translation.ServerCertificate = endpoint.ServerCertificate;
translation.TransportProfileUri = endpoint.TransportProfileUri;
translation.UserIdentityTokens = endpoint.UserIdentityTokens;
translation.Server = application;

if (!translations.Exists(match =>
match.EndpointUrl.Equals(translation.EndpointUrl, StringComparison.Ordinal) &&
match.SecurityMode == translation.SecurityMode &&
match.SecurityPolicyUri.Equals(translation.SecurityPolicyUri, StringComparison.Ordinal)))
{
translations.Add(translation);
translation.EndpointUrl = baseAddress.Url.ToString();

if (endpointUrl.Path.StartsWith(baseAddress.Url.PathAndQuery, StringComparison.Ordinal) &&
endpointUrl.Path.Length > baseAddress.Url.PathAndQuery.Length)
{
string suffix = endpointUrl.Path.Substring(baseAddress.Url.PathAndQuery.Length);
translation.EndpointUrl += suffix;
}

translation.ProxyUrl = endpoint.ProxyUrl;
translation.SecurityLevel = endpoint.SecurityLevel;
translation.SecurityMode = endpoint.SecurityMode;
translation.SecurityPolicyUri = endpoint.SecurityPolicyUri;
translation.ServerCertificate = endpoint.ServerCertificate;
translation.TransportProfileUri = endpoint.TransportProfileUri;
translation.UserIdentityTokens = endpoint.UserIdentityTokens;
translation.Server = application;

if (!translations.Exists(match =>
match.EndpointUrl.Equals(translation.EndpointUrl, StringComparison.Ordinal) &&
match.SecurityMode == translation.SecurityMode &&
match.SecurityPolicyUri.Equals(translation.SecurityPolicyUri, StringComparison.Ordinal)))
{
translations.Add(translation);
}
}
}
}
} while (matchPort && translations.Count == 0);

translations.Sort((ep1, ep2) => string.Compare(ep1.EndpointUrl, ep2.EndpointUrl, StringComparison.Ordinal));

Expand Down
Loading
Loading