Add packet to check if remote is running as root.

This commit is contained in:
2019-10-26 17:39:50 +01:00
parent ad053da2a6
commit 49297dc54a
5 changed files with 138 additions and 40 deletions

View File

@@ -140,6 +140,71 @@ namespace DiscImageChef.Devices.Remote
public string ServerArchitecture { get; }
public int ServerProtocolVersion { get; }
public bool IsRoot
{
get
{
var cmdPkt = new DicPacketCmdAmIRoot
{
hdr = new DicPacketHeader
{
remote_id = Consts.RemoteId, packet_id = Consts.PacketId,
len = (uint) Marshal.SizeOf<DicPacketCmdAmIRoot>(),
version = Consts.PacketVersion,
packetType = DicPacketType.CommandAmIRoot
}
};
var buf = Marshal.StructureToByteArrayLittleEndian(cmdPkt);
var len = _socket.Send(buf, SocketFlags.None);
if (len != buf.Length)
{
DicConsole.ErrorWriteLine("Could not write to the network...");
return false;
}
var hdrBuf = new byte[Marshal.SizeOf<DicPacketHeader>()];
len = Receive(_socket, hdrBuf, hdrBuf.Length, SocketFlags.Peek);
if (len < hdrBuf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return false;
}
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (hdr.remote_id != Consts.RemoteId || hdr.packet_id != Consts.PacketId)
{
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet...");
return false;
}
if (hdr.packetType != DicPacketType.ResponseAmIRoot)
{
DicConsole.ErrorWriteLine("Expected Am I Root? Response Packet, got packet type {0}...",
hdr.packetType);
return false;
}
buf = new byte[hdr.len];
len = Receive(_socket, buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return false;
}
var res = Marshal.ByteArrayToStructureLittleEndian<DicPacketResAmIRoot>(buf);
return res.am_i_root;
}
}
public void Dispose()
{
Disconnect();