Skip to content

Commit

Permalink
fix(edge): UnixDomainSocketEndPoint is available in .NET 2.1 and grea…
Browse files Browse the repository at this point in the history
…ter (#1816)

* UnixDomainSocketEndPoint has been standard since 2.1. Allowing later versions to use the correct class

The edge client HSM provider uses UnixDomainSockets (UDS) for communication. Before .NET 2.1 to implement a Unix Socket you had to create your own class to do so. Since 2.1 there has been a native UnixDomainSocketEndPoint class in the runtime.

In 2.1 and 3.1 there is no issue. However in 5.0 there are some changes to the way the Socket class handles the native UnixDomainSocketEndPoint class. I didn't dig down extremely deep, but I suspect it's due to the way the endpoint handles the SocketAddress and the string manipulation there seeing as how there is a specific implementation for Windows and for Unix.
  • Loading branch information
jamdavi authored and vinagesh committed Mar 22, 2021
1 parent 03afec2 commit 19f2b30
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
using var stream = new HttpBufferedStream(new NetworkStream(socket, true));

byte[] requestBytes = HttpRequestResponseSerializer.SerializeRequest(request);

#if NET451 || NET472 || NETSTANDARD2_0
await stream.WriteAsync(requestBytes, 0, requestBytes.Length, cancellationToken).ConfigureAwait(false);
#else
Expand All @@ -42,10 +43,25 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

private async Task<Socket> GetConnectedSocketAsync()
{
var endpoint = new UnixDomainSocketEndPoint(_providerUri.LocalPath);
Socket socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
await socket.ConnectAsync(endpoint).ConfigureAwait(false);

// The Edge Agent uses unix sockets for communication with the modules deployed in docker for HSM.
// For netstandard 2.0 there was no implementation for a Unix Domain Socket (UDS) so we used a version
// that was part of a test that was reused in a number of libraries on the internet.
//
// https://github.com/dotnet/corefx/blob/12b51c6bf153cc237b251a4e264d5e7c0ee84a33/src/System.IO.Pipes/src/System/Net/Sockets/UnixDomainSocketEndPoint.cs
// https://github.com/dotnet/corefx/blob/12b51c6bf153cc237b251a4e264d5e7c0ee84a33/src/System.Net.Sockets/tests/FunctionalTests/UnixDomainSocketTest.cs#L248
//
// Since then the UnixDomainSocketEndpoint has been added to the dotnet framework and there has been considerable work
// around unix sockets in the BCL. For older versions of the framework we will continue to use the existing class since it works
// fine. For netcore 2.1 and greater as well as .NET 5.0 and greater we'll use the native framework version.

#if NET451 || NET472 || NETSTANDARD2_0
var endpoint = new Microsoft.Azure.Devices.Client.HsmAuthentication.Transport.UnixDomainSocketEndPoint(_providerUri.LocalPath);
#else
var endpoint = new System.Net.Sockets.UnixDomainSocketEndPoint(_providerUri.LocalPath);
#endif
await socket.ConnectAsync(endpoint).ConfigureAwait(false);
return socket;
}
}
Expand Down

0 comments on commit 19f2b30

Please sign in to comment.