Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ internal sealed class RemoteSessionHyperVSocketServer : IDisposable

#endregion

#region Helpers

/// <summary>
/// Gets the partition-specific Hyper-V VM ID that the session server listens on.
/// </summary>
/// <param name="loopbackMode">
/// When <see langword="true"/>, communication stays within the same partition
/// (HV_GUID_LOOPBACK); otherwise it targets the parent/host partition (HV_GUID_PARENT).
/// </param>
/// <returns>The VM ID used to bind the listening socket.</returns>
internal static Guid GetListenerVmId(bool loopbackMode)
{
Guid loopbackId = new Guid("e0e16197-dd56-4a10-9195-5ee7a155a838"); // HV_GUID_LOOPBACK
Guid parentId = new Guid("a42e7cda-d03f-480c-9cc2-a4de20abb878"); // HV_GUID_PARENT
return loopbackMode ? loopbackId : parentId;
}

#endregion

#region Constructors

public RemoteSessionHyperVSocketServer(bool LoopbackMode)
Expand All @@ -188,9 +207,7 @@ public RemoteSessionHyperVSocketServer(bool LoopbackMode)
try
{
Guid serviceId = new Guid("a5201c21-2770-4c11-a68e-f182edb29220"); // HV_GUID_VM_SESSION_SERVICE_ID_2
Guid loopbackId = new Guid("e0e16197-dd56-4a10-9195-5ee7a155a838"); // HV_GUID_LOOPBACK
Guid parentId = new Guid("a42e7cda-d03f-480c-9cc2-a4de20abb878"); // HV_GUID_PARENT
Guid vmId = LoopbackMode ? loopbackId : parentId;
Guid vmId = GetListenerVmId(LoopbackMode);
HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(HyperVSocketEndPoint.AF_HYPERV, vmId, serviceId);

Socket listenSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType)1);
Expand Down Expand Up @@ -250,7 +267,11 @@ public RemoteSessionHyperVSocketServer(bool LoopbackMode, string token, DateTime
try
{
Guid serviceId = new Guid("a5201c21-2770-4c11-a68e-f182edb29220"); // HV_GUID_VM_SESSION_SERVICE_ID_2
HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(HyperVSocketEndPoint.AF_HYPERV, Guid.Empty, serviceId);

// Bind to a partition-specific VM ID (host parent, or local loopback), consistent
// with the V1 server constructor above.
Guid vmId = GetListenerVmId(LoopbackMode);
HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(HyperVSocketEndPoint.AF_HYPERV, vmId, serviceId);

Socket listenSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType)1);
listenSocket.Bind(endpoint);
Expand Down
12 changes: 12 additions & 0 deletions test/xUnit/csharp/test_RemoteHyperV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,5 +802,17 @@ public async Task ValidateFailsWhenTokensMismatch(string token, string expectedT

await serverTask;
}

[SkippableTheory]
[InlineData(false, "a42e7cda-d03f-480c-9cc2-a4de20abb878")] // HV_GUID_PARENT (host partition)
[InlineData(true, "e0e16197-dd56-4a10-9195-5ee7a155a838")] // HV_GUID_LOOPBACK (same partition)
public void ServerListenerUsesExpectedPartitionVmId(bool loopbackMode, string expectedVmId)
{
// The session server must listen on a specific partition (host parent, or local
// loopback) depending on the requested mode.
Guid vmId = System.Management.Automation.Remoting.RemoteSessionHyperVSocketServer.GetListenerVmId(loopbackMode);

Assert.Equal(new Guid(expectedVmId), vmId);
}
}
}
Loading