Files
Aaru/DiscImageChef.Devices/Remote/Remote.cs

377 lines
13 KiB
C#
Raw Normal View History

2019-10-12 22:51:50 +01:00
using System;
2019-10-12 23:44:51 +01:00
using System.Collections.Generic;
2019-10-12 22:51:50 +01:00
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
2019-10-12 23:44:51 +01:00
using System.Runtime.InteropServices;
2019-10-14 01:02:25 +01:00
using DiscImageChef.CommonTypes.Enums;
2019-10-12 22:51:50 +01:00
using DiscImageChef.CommonTypes.Interop;
using DiscImageChef.Console;
2019-10-13 23:31:56 +01:00
using DiscImageChef.Decoders.ATA;
2019-10-12 23:44:51 +01:00
using Marshal = DiscImageChef.Helpers.Marshal;
2019-10-12 22:51:50 +01:00
using Version = DiscImageChef.CommonTypes.Interop.Version;
namespace DiscImageChef.Devices.Remote
{
2019-10-12 23:12:39 +01:00
public class Remote : IDisposable
2019-10-12 22:51:50 +01:00
{
private readonly string _host;
2019-10-13 20:54:10 +01:00
private readonly Socket _socket;
2019-10-12 22:51:50 +01:00
public Remote(string host)
{
_host = host;
2019-10-12 22:51:50 +01:00
var ipHostEntry = Dns.GetHostEntry(host);
var ipAddress = ipHostEntry.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
if (ipAddress is null)
{
DicConsole.ErrorWriteLine("Host not found");
throw new SocketException(11001);
}
var ipEndPoint = new IPEndPoint(ipAddress, 6666);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(ipEndPoint);
DicConsole.WriteLine("Connected to {0}", host);
var hdrBuf = new byte[Marshal.SizeOf<DicPacketHeader>()];
var len = _socket.Receive(hdrBuf, hdrBuf.Length, SocketFlags.Peek);
if (len < hdrBuf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
throw new IOException();
}
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (hdr.id != Consts.PacketId)
{
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet...");
throw new ArgumentException();
}
2019-10-13 20:54:10 +01:00
byte[] buf;
2019-10-12 22:51:50 +01:00
if (hdr.packetType != DicPacketType.Hello)
{
2019-10-13 20:54:10 +01:00
if (hdr.packetType != DicPacketType.Nop)
{
DicConsole.ErrorWriteLine("Expected Hello Packet, got packet type {0}...", hdr.packetType);
throw new ArgumentException();
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
throw new IOException();
}
var nop = Marshal.ByteArrayToStructureLittleEndian<DicPacketNop>(buf);
DicConsole.ErrorWriteLine($"{nop.reason}");
2019-10-12 22:51:50 +01:00
throw new ArgumentException();
}
if (hdr.version != Consts.PacketVersion)
{
DicConsole.ErrorWriteLine("Unrecognized packet version...");
throw new ArgumentException();
}
2019-10-13 20:54:10 +01:00
buf = new byte[hdr.len];
2019-10-12 22:51:50 +01:00
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
throw new IOException();
}
var serverHello = Marshal.ByteArrayToStructureLittleEndian<DicPacketHello>(buf);
ServerApplication = serverHello.application;
ServerVersion = serverHello.version;
ServerOperatingSystem = serverHello.sysname;
ServerOperatingSystemVersion = serverHello.release;
ServerArchitecture = serverHello.machine;
ServerProtocolVersion = serverHello.maxProtocol;
var clientHello = new DicPacketHello
{
application = "DiscImageChef",
version = Version.GetVersion(),
maxProtocol = Consts.MaxProtocol,
sysname = DetectOS.GetPlatformName(
DetectOS.GetRealPlatformID(), DetectOS.GetVersion()),
release = DetectOS.GetVersion(),
2019-10-12 23:44:51 +01:00
machine = RuntimeInformation.ProcessArchitecture.ToString(),
2019-10-12 22:51:50 +01:00
hdr = new DicPacketHeader
{
id = Consts.PacketId,
len = (uint) Marshal.SizeOf<DicPacketHello>(),
version = Consts.PacketVersion,
packetType = DicPacketType.Hello
}
};
buf = Marshal.StructureToByteArrayLittleEndian(clientHello);
len = _socket.Send(buf, SocketFlags.None);
if (len >= buf.Length) return;
DicConsole.ErrorWriteLine("Could not write to the network...");
throw new IOException();
}
public string ServerApplication { get; }
public string ServerVersion { get; }
public string ServerOperatingSystem { get; }
public string ServerOperatingSystemVersion { get; }
public string ServerArchitecture { get; }
public int ServerProtocolVersion { get; }
2019-10-12 23:12:39 +01:00
public void Dispose()
{
Disconnect();
}
2019-10-12 22:51:50 +01:00
public void Disconnect()
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
2019-10-12 23:12:39 +01:00
public DeviceInfo[] ListDevices()
{
2019-10-12 23:23:33 +01:00
var cmdPkt = new DicPacketCommandListDevices
{
hdr = new DicPacketHeader
{
id = Consts.PacketId,
len = (uint) Marshal.SizeOf<DicPacketCommandListDevices>(),
version = Consts.PacketVersion,
packetType = DicPacketType.CommandListDevices
}
};
var buf = Marshal.StructureToByteArrayLittleEndian(cmdPkt);
var len = _socket.Send(buf, SocketFlags.None);
2019-10-12 23:27:59 +01:00
if (len != buf.Length)
2019-10-12 23:23:33 +01:00
{
DicConsole.ErrorWriteLine("Could not write to the network...");
return new DeviceInfo[0];
}
2019-10-12 23:44:51 +01:00
var hdrBuf = new byte[Marshal.SizeOf<DicPacketHeader>()];
len = _socket.Receive(hdrBuf, hdrBuf.Length, SocketFlags.Peek);
if (len < hdrBuf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return new DeviceInfo[0];
}
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (hdr.id != Consts.PacketId)
{
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet...");
return new DeviceInfo[0];
}
if (hdr.packetType != DicPacketType.ResponseListDevices)
{
2019-10-13 20:54:10 +01:00
if (hdr.packetType != DicPacketType.Nop)
{
DicConsole.ErrorWriteLine("Expected List Devices Response Packet, got packet type {0}...",
hdr.packetType);
return new DeviceInfo[0];
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return new DeviceInfo[0];
}
var nop = Marshal.ByteArrayToStructureLittleEndian<DicPacketNop>(buf);
DicConsole.ErrorWriteLine($"{nop.reason}");
2019-10-12 23:44:51 +01:00
return new DeviceInfo[0];
}
if (hdr.version != Consts.PacketVersion)
{
DicConsole.ErrorWriteLine("Unrecognized packet version...");
return new DeviceInfo[0];
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return new DeviceInfo[0];
}
var response = Marshal.ByteArrayToStructureLittleEndian<DicPacketResponseListDevices>(buf);
var devices = new List<DeviceInfo>();
var offset = Marshal.SizeOf<DicPacketResponseListDevices>();
var devInfoLen = Marshal.SizeOf<DeviceInfo>();
for (ushort i = 0; i < response.devices; i++)
{
var dev = Marshal.ByteArrayToStructureLittleEndian<DeviceInfo>(buf, offset, devInfoLen);
dev.Path = dev.Path[0] == '/' ? $"dic://{_host}{dev.Path}" : $"dic://{_host}/{dev.Path}";
devices.Add(dev);
2019-10-12 23:44:51 +01:00
offset += devInfoLen;
}
return devices.ToArray();
2019-10-12 23:12:39 +01:00
}
2019-10-13 22:29:09 +01:00
public bool Open(string devicePath, out int LastError)
{
LastError = 0;
var cmdPkt = new DicPacketCommandOpenDevice
{
hdr = new DicPacketHeader
{
id = Consts.PacketId,
len = (uint) Marshal.SizeOf<DicPacketCommandOpenDevice>(),
version = Consts.PacketVersion,
packetType = DicPacketType.CommandOpen
},
device_path = devicePath
};
var buf = Marshal.StructureToByteArrayLittleEndian(cmdPkt);
var len = _socket.Send(buf, SocketFlags.None);
if (len != buf.Length)
{
DicConsole.ErrorWriteLine("Could not write to the network...");
LastError = -1;
return false;
}
var hdrBuf = new byte[Marshal.SizeOf<DicPacketHeader>()];
len = _socket.Receive(hdrBuf, hdrBuf.Length, SocketFlags.Peek);
if (len < hdrBuf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
LastError = -1;
return false;
}
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (hdr.id != Consts.PacketId)
{
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet...");
LastError = -1;
return false;
}
if (hdr.packetType != DicPacketType.Nop)
{
DicConsole.ErrorWriteLine("Expected List Devices Response Packet, got packet type {0}...",
hdr.packetType);
LastError = -1;
return false;
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
LastError = -1;
return false;
}
var nop = Marshal.ByteArrayToStructureLittleEndian<DicPacketNop>(buf);
switch (nop.reasonCode)
{
case DicNopReason.OpenOk:
return true;
case DicNopReason.NotImplemented:
throw new NotImplementedException($"{nop.reason}");
}
DicConsole.ErrorWriteLine($"{nop.reason}");
LastError = nop.errno;
return false;
}
2019-10-13 23:31:56 +01:00
public int SendScsiCommand(byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout,
ScsiDirection direction, out double duration, out bool sense)
{
throw new NotImplementedException("Remote SCSI commands not yet implemented...");
}
public int SendAtaCommand(AtaRegistersChs registers, out AtaErrorRegistersChs errorRegisters,
AtaProtocol protocol, AtaTransferRegister transferRegister,
ref byte[] buffer,
uint timeout, bool transferBlocks,
out double duration, out bool sense)
{
throw new NotImplementedException("Remote CHS ATA commands not yet implemented...");
}
public int SendAtaCommand(AtaRegistersLba28 registers, out AtaErrorRegistersLba28 errorRegisters,
AtaProtocol protocol, AtaTransferRegister transferRegister,
ref byte[] buffer,
uint timeout, bool transferBlocks,
out double duration, out bool sense)
{
throw new NotImplementedException("Remote 28-bit ATA commands not yet implemented...");
}
public int SendAtaCommand(AtaRegistersLba48 registers, out AtaErrorRegistersLba48 errorRegisters,
AtaProtocol protocol, AtaTransferRegister transferRegister,
ref byte[] buffer,
uint timeout, bool transferBlocks,
out double duration, out bool sense)
{
throw new NotImplementedException("Remote 48-bit ATA commands not yet implemented...");
}
public int SendMmcCommand(MmcCommands command, bool write, bool isApplication, MmcFlags flags,
uint argument,
uint blockSize, uint blocks, ref byte[] buffer, out uint[] response,
out double duration, out bool sense, uint timeout = 0)
{
throw new NotImplementedException("Remote SDHCI commands not yet implemented...");
}
2019-10-14 01:02:25 +01:00
public DeviceType GetDeviceType()
{
throw new NotImplementedException("Getting remote device type not yet implemented...");
}
2019-10-12 22:51:50 +01:00
}
}