internal RemoteSessionHyperVSocketClient( Guid vmId, bool isFirstConnection, bool isContainer = false) { Guid serviceId; _syncObject = new object(); if (isFirstConnection) { // HV_GUID_VM_SESSION_SERVICE_ID serviceId = new Guid("999e53d4-3d5c-4c3e-8779-bed06ec056e1"); } else { // HV_GUID_VM_SESSION_SERVICE_ID_2 serviceId = new Guid("a5201c21-2770-4c11-a68e-f182edb29220"); } EndPoint = new HyperVSocketEndPoint(HyperVSocketEndPoint.AF_HYPERV, vmId, serviceId); HyperVSocket = new Socket(EndPoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType) 1); // // We need to call SetSocketOption() in order to set up Hyper-V socket connection between container host and Hyper-V container. // Here is the scenario: the Hyper-V container is inside a utility vm, which is inside the container host // if (isContainer) { var value = new byte[sizeof(uint)]; value[0] = 1; try { HyperVSocket.SetSocketOption((System.Net.Sockets.SocketOptionLevel)HV_PROTOCOL_RAW, (System.Net.Sockets.SocketOptionName)HVSOCKET_CONTAINER_PASSTHRU, (byte[])value); } catch { throw new PSDirectException( PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.RemoteSessionHyperVSocketClientConstructorSetSocketOptionFailure)); } } }
public override bool Equals(object obj) { HyperVSocketEndPoint endpoint = (HyperVSocketEndPoint)obj; if (endpoint == null) { return(false); } if ((_addressFamily == endpoint.AddressFamily) && (_vmId == endpoint.VmId) && (_serviceId == endpoint.ServiceId)) { return(true); } return(false); }
public override EndPoint Create(SocketAddress SockAddr) { if (SockAddr == null || SockAddr.Family != AF_HYPERV || SockAddr.Size != 34) { return(null); } HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(SockAddr.Family, Guid.Empty, Guid.Empty); string sockAddress = SockAddr.ToString(); endpoint.VmId = new Guid(sockAddress.Substring(4, 16)); endpoint.ServiceId = new Guid(sockAddress.Substring(20, 16)); return(endpoint); }
public override EndPoint Create(SocketAddress SockAddr) { if (SockAddr == null || SockAddr.Family != AF_HYPERV || SockAddr.Size != 34) { return null; } HyperVSocketEndPoint endpoint = new HyperVSocketEndPoint(SockAddr.Family, Guid.Empty, Guid.Empty); string sockAddress = SockAddr.ToString(); endpoint.VmId = new Guid(sockAddress.Substring(4, 16)); endpoint.ServiceId = new Guid(sockAddress.Substring(20, 16)); return endpoint; }
public RemoteSessionHyperVSocketServer(bool LoopbackMode) { // TODO: uncomment below code when .NET supports Hyper-V socket duplication /* * NamedPipeClientStream clientPipeStream; * byte[] buffer = new byte[1000]; * int bytesRead; */ _syncObject = new object(); Exception ex = null; try { // TODO: uncomment below code when .NET supports Hyper-V socket duplication /* * if (!LoopbackMode) * { * // * // Create named pipe client. * // * using (clientPipeStream = new NamedPipeClientStream(".", * "PS_VMSession", * PipeDirection.InOut, * PipeOptions.None, * TokenImpersonationLevel.None)) * { * // * // Connect to named pipe server. * // * clientPipeStream.Connect(10*1000); * * // * // Read LPWSAPROTOCOL_INFO. * // * bytesRead = clientPipeStream.Read(buffer, 0, 1000); * } * } * * // * // Create duplicate socket. * // * byte[] protocolInfo = new byte[bytesRead]; * Array.Copy(buffer, protocolInfo, bytesRead); * * SocketInformation sockInfo = new SocketInformation(); * sockInfo.ProtocolInformation = protocolInfo; * sockInfo.Options = SocketInformationOptions.Connected; * * socket = new Socket(sockInfo); * if (socket == null) * { * Dbg.Assert(false, "Unexpected error in RemoteSessionHyperVSocketServer."); * * tracer.WriteMessage("RemoteSessionHyperVSocketServer", "RemoteSessionHyperVSocketServer", Guid.Empty, * "Unexpected error in constructor: {0}", "socket duplication failure"); * } */ // TODO: remove below 6 lines of code when .NET supports Hyper-V socket duplication 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); Socket listenSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType) 1); listenSocket.Bind(endpoint); listenSocket.Listen(1); HyperVSocket = listenSocket.Accept(); Stream = new NetworkStream(HyperVSocket, true); // Create reader/writer streams. TextReader = new StreamReader(Stream); TextWriter = new StreamWriter(Stream); TextWriter.AutoFlush = true; // // listenSocket is not closed when it goes out of scope here. Sometimes it is // closed later in this thread, while other times it is not closed at all. This will // cause problem when we set up a second PowerShell Direct session. Let's // explicitly close listenSocket here for safe. // if (listenSocket != null) { try { listenSocket.Dispose(); } catch (ObjectDisposedException) { } } } catch (Exception e) { ex = e; } if (ex != null) { Dbg.Fail("Unexpected error in RemoteSessionHyperVSocketServer."); // Unexpected error. string errorMessage = !string.IsNullOrEmpty(ex.Message) ? ex.Message : string.Empty; _tracer.WriteMessage("RemoteSessionHyperVSocketServer", "RemoteSessionHyperVSocketServer", Guid.Empty, "Unexpected error in constructor: {0}", errorMessage); throw new PSInvalidOperationException( PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.RemoteSessionHyperVSocketServerConstructorFailure), ex, nameof(PSRemotingErrorId.RemoteSessionHyperVSocketServerConstructorFailure), ErrorCategory.InvalidOperation, null); } }
public RemoteSessionHyperVSocketServer(bool LoopbackMode) { // TODO: uncomment below code when .NET supports Hyper-V socket duplication /* NamedPipeClientStream clientPipeStream; byte[] buffer = new byte[1000]; int bytesRead; */ _syncObject = new object(); Exception ex = null; try { // TODO: uncomment below code when .NET supports Hyper-V socket duplication /* if (!LoopbackMode) { // // Create named pipe client. // using (clientPipeStream = new NamedPipeClientStream(".", "PS_VMSession", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.None)) { // // Connect to named pipe server. // clientPipeStream.Connect(10*1000); // // Read LPWSAPROTOCOL_INFO. // bytesRead = clientPipeStream.Read(buffer, 0, 1000); } } // // Create duplicate socket. // byte[] protocolInfo = new byte[bytesRead]; Array.Copy(buffer, protocolInfo, bytesRead); SocketInformation sockInfo = new SocketInformation(); sockInfo.ProtocolInformation = protocolInfo; sockInfo.Options = SocketInformationOptions.Connected; socket = new Socket(sockInfo); if (socket == null) { Dbg.Assert(false, "Unexpected error in RemoteSessionHyperVSocketServer."); tracer.WriteMessage("RemoteSessionHyperVSocketServer", "RemoteSessionHyperVSocketServer", Guid.Empty, "Unexpected error in constructor: {0}", "socket duplication failure"); } */ // TODO: remove below 6 lines of code when .NET supports Hyper-V socket duplication 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); Socket listenSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType)1); listenSocket.Bind(endpoint); listenSocket.Listen(1); HyperVSocket = listenSocket.Accept(); Stream = new NetworkStream(HyperVSocket, true); // Create reader/writer streams. TextReader = new StreamReader(Stream); TextWriter = new StreamWriter(Stream); TextWriter.AutoFlush = true; // // listenSocket is not closed when it goes out of scope here. Sometimes it is // closed later in this thread, while other times it is not closed at all. This will // cause problem when we set up a second PowerShell Direct session. Let's // explicitly close listenSocket here for safe. // if (listenSocket != null) { try { listenSocket.Dispose(); } catch (ObjectDisposedException) { } } } catch (Exception e) { CommandProcessorBase.CheckForSevereException(e); ex = e; } if (ex != null) { Dbg.Assert(false, "Unexpected error in RemoteSessionHyperVSocketServer."); // Unexpected error. string errorMessage = !string.IsNullOrEmpty(ex.Message) ? ex.Message : string.Empty; _tracer.WriteMessage("RemoteSessionHyperVSocketServer", "RemoteSessionHyperVSocketServer", Guid.Empty, "Unexpected error in constructor: {0}", errorMessage); throw new PSInvalidOperationException( PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.RemoteSessionHyperVSocketServerConstructorFailure), ex, PSRemotingErrorId.RemoteSessionHyperVSocketServerConstructorFailure.ToString(), ErrorCategory.InvalidOperation, null); } }
internal RemoteSessionHyperVSocketClient( Guid vmId, bool isFirstConnection, bool isContainer = false) { Guid serviceId; _syncObject = new object(); if (isFirstConnection) { // HV_GUID_VM_SESSION_SERVICE_ID serviceId = new Guid("999e53d4-3d5c-4c3e-8779-bed06ec056e1"); } else { // HV_GUID_VM_SESSION_SERVICE_ID_2 serviceId = new Guid("a5201c21-2770-4c11-a68e-f182edb29220"); } EndPoint = new HyperVSocketEndPoint(HyperVSocketEndPoint.AF_HYPERV, vmId, serviceId); HyperVSocket = new Socket(EndPoint.AddressFamily, SocketType.Stream, (System.Net.Sockets.ProtocolType)1); // // We need to call SetSocketOption() in order to set up Hyper-V socket connection between container host and Hyper-V container. // Here is the scenario: the Hyper-V container is inside a utility vm, which is inside the container host // if (isContainer) { var value = new byte[sizeof(uint)]; value[0] = 1; try { HyperVSocket.SetSocketOption((System.Net.Sockets.SocketOptionLevel)HV_PROTOCOL_RAW, (System.Net.Sockets.SocketOptionName)HVSOCKET_CONTAINER_PASSTHRU, (byte[])value); } catch { throw new PSDirectException( PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.RemoteSessionHyperVSocketClientConstructorSetSocketOptionFailure)); } } }