Skip to content

Allow access control and auditing requirements to be specified when creating named pipe server #8

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 43 additions & 2 deletions src/protobuf-net.GrpcLite/ConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
#if NET6_0_OR_GREATER
using System.Runtime.Versioning;
#endif
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
@@ -48,8 +51,45 @@ public static Func<CancellationToken, ValueTask<ConnectionState<Stream>>> Connec
/// </summary>
public static Func<CancellationToken, ValueTask<ConnectionState<Stream>>> ListenNamedPipe(string pipeName, ILogger? logger = null) => async cancellationToken =>
{
var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte,
var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte,
PipeOptions.Asynchronous | PipeOptions.WriteThrough);

return await WaitForClientConnection(pipeName, pipe, logger, cancellationToken);
};

#if !NET5_0 && !NETCOREAPP3_1
/// <summary>
/// Listen (as a server) to a named-pipe.
/// </summary>
#if NET6_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
public static Func<CancellationToken, ValueTask<ConnectionState<Stream>>> ListenNamedPipe(string pipeName, PipeSecurity pipeSecurity, ILogger? logger = null) => async cancellationToken =>
{
#if NETFRAMEWORK
var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte,
PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0, pipeSecurity);

#elif NETSTANDARD2_1

var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0);

pipe.SetAccessControl(pipeSecurity);
#else

var pipe = NamedPipeServerStreamAcl.Create(pipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 0, 0, pipeSecurity);

#endif

return await WaitForClientConnection(pipeName, pipe, logger, cancellationToken);
};
#endif

private static async Task<ConnectionState<Stream>> WaitForClientConnection(string pipeName, NamedPipeServerStream pipe, ILogger? logger, CancellationToken cancellationToken)
{
try
{
logger.Debug(pipeName, static (state, _) => $"waiting for connection... {state}");
@@ -65,7 +105,8 @@ public static Func<CancellationToken, ValueTask<ConnectionState<Stream>>> Listen
pipe.SafeDispose();
throw;
}
};
}


/// <summary>
/// Connect (as a client) to a socket server.
1 change: 1 addition & 0 deletions src/protobuf-net.GrpcLite/protobuf-net.GrpcLite.csproj
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
<!-- has important fix for netfx ROS bug -->
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.IO.Pipes.AccessControl" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net462'">