Send hello packet.

This commit is contained in:
2019-10-12 22:13:33 +01:00
parent 36d273b7af
commit 083517b8ed
4 changed files with 136 additions and 86 deletions

View File

@@ -2,7 +2,7 @@ namespace DiscImageChef.Devices.Remote
{ {
public class Consts public class Consts
{ {
public const string PacketId = "DICPACKT"; public const ulong PacketId = 0x6873678065677584; // "DICPACKT"
public const int PacketVersion = 1; public const int PacketVersion = 1;
public const int MaxProtocol = 1; public const int MaxProtocol = 1;
} }

View File

@@ -5,8 +5,7 @@ namespace DiscImageChef.Devices.Remote
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DicPacketHeader public struct DicPacketHeader
{ {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public ulong id;
public byte[] id;
public uint len; public uint len;
public byte version; public byte version;

View File

@@ -46,7 +46,10 @@ namespace DiscImageChef.Helpers
/// <typeparam name="T">The type whose size is to be returned.</typeparam> /// <typeparam name="T">The type whose size is to be returned.</typeparam>
/// <returns>The size, in bytes, of the type that is specified by the <see cref="T" /> generic type parameter.</returns> /// <returns>The size, in bytes, of the type that is specified by the <see cref="T" /> generic type parameter.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SizeOf<T>() => System.Runtime.InteropServices.Marshal.SizeOf<T>(); public static int SizeOf<T>()
{
return System.Runtime.InteropServices.Marshal.SizeOf<T>();
}
/// <summary> /// <summary>
/// Marshal little-endian binary data to a structure /// Marshal little-endian binary data to a structure
@@ -57,9 +60,9 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes) where T : struct public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes) where T : struct
{ {
GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T str = var str =
(T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
ptr.Free(); ptr.Free();
return str; return str;
} }
@@ -88,11 +91,11 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
{ {
GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
object str = object str =
(T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
ptr.Free(); ptr.Free();
return (T)SwapStructureMembersEndian(str); return (T) SwapStructureMembersEndian(str);
} }
/// <summary> /// <summary>
@@ -120,11 +123,11 @@ namespace DiscImageChef.Helpers
public static T ByteArrayToStructurePdpEndian<T>(byte[] bytes) where T : struct public static T ByteArrayToStructurePdpEndian<T>(byte[] bytes) where T : struct
{ {
{ {
GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
object str = object str =
(T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
ptr.Free(); ptr.Free();
return (T)SwapStructureMembersEndianPdp(str); return (T) SwapStructureMembersEndianPdp(str);
} }
} }
@@ -151,8 +154,10 @@ namespace DiscImageChef.Helpers
/// <typeparam name="T">Type of the structure to marshal</typeparam> /// <typeparam name="T">Type of the structure to marshal</typeparam>
/// <returns>The binary data marshalled in a structure with the specified type</returns> /// <returns>The binary data marshalled in a structure with the specified type</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct => public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct
MemoryMarshal.Read<T>(bytes); {
return MemoryMarshal.Read<T>(bytes);
}
/// <summary> /// <summary>
/// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method
@@ -165,8 +170,10 @@ namespace DiscImageChef.Helpers
/// <returns>The binary data marshalled in a structure with the specified type</returns> /// <returns>The binary data marshalled in a structure with the specified type</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) public static T SpanToStructureLittleEndian<T>(ReadOnlySpan<byte> bytes, int start, int length)
where T : struct => where T : struct
MemoryMarshal.Read<T>(bytes.Slice(start, length)); {
return MemoryMarshal.Read<T>(bytes.Slice(start, length));
}
/// <summary> /// <summary>
/// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will
@@ -178,8 +185,8 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes) where T : struct public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes) where T : struct
{ {
T str = SpanToStructureLittleEndian<T>(bytes); var str = SpanToStructureLittleEndian<T>(bytes);
return (T)SwapStructureMembersEndian(str); return (T) SwapStructureMembersEndian(str);
} }
/// <summary> /// <summary>
@@ -194,8 +201,8 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct public static T SpanToStructureBigEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
{ {
T str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length)); var str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
return (T)SwapStructureMembersEndian(str); return (T) SwapStructureMembersEndian(str);
} }
/// <summary> /// <summary>
@@ -209,7 +216,7 @@ namespace DiscImageChef.Helpers
public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes) where T : struct public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes) where T : struct
{ {
object str = SpanToStructureLittleEndian<T>(bytes); object str = SpanToStructureLittleEndian<T>(bytes);
return (T)SwapStructureMembersEndianPdp(str); return (T) SwapStructureMembersEndianPdp(str);
} }
/// <summary> /// <summary>
@@ -223,7 +230,7 @@ namespace DiscImageChef.Helpers
public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct public static T SpanToStructurePdpEndian<T>(ReadOnlySpan<byte> bytes, int start, int length) where T : struct
{ {
object str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length)); object str = SpanToStructureLittleEndian<T>(bytes.Slice(start, length));
return (T)SwapStructureMembersEndianPdp(str); return (T) SwapStructureMembersEndianPdp(str);
} }
/// <summary> /// <summary>
@@ -240,10 +247,10 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T MarshalStructure<T>(byte[] bytes) where T : struct public static T MarshalStructure<T>(byte[] bytes) where T : struct
{ {
if(!(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is MarshallingPropertiesAttribute if (!(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is MarshallingPropertiesAttribute
properties)) return ByteArrayToStructureLittleEndian<T>(bytes); properties)) return ByteArrayToStructureLittleEndian<T>(bytes);
switch(properties.Endian) switch (properties.Endian)
{ {
case BitEndian.Little: case BitEndian.Little:
return properties.HasReferences return properties.HasReferences
@@ -274,76 +281,77 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object SwapStructureMembersEndian(object str) public static object SwapStructureMembersEndian(object str)
{ {
Type t = str.GetType(); var t = str.GetType();
FieldInfo[] fieldInfo = t.GetFields(); var fieldInfo = t.GetFields();
foreach(FieldInfo fi in fieldInfo) foreach (var fi in fieldInfo)
if(fi.FieldType == typeof(short)) if (fi.FieldType == typeof(short))
{ {
short x = (short)fi.GetValue(str); var x = (short) fi.GetValue(str);
fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF))); fi.SetValue(str, (short) ((x << 8) | ((x >> 8) & 0xFF)));
} }
else if(fi.FieldType == typeof(int)) else if (fi.FieldType == typeof(int))
{ {
int x = (int)fi.GetValue(str); var x = (int) fi.GetValue(str);
x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF)); x = (int) (((x << 8) & 0xFF00FF00) | (((uint) x >> 8) & 0xFF00FF));
fi.SetValue(str, (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF))); fi.SetValue(str, (int) (((uint) x << 16) | (((uint) x >> 16) & 0xFFFF)));
} }
else if(fi.FieldType == typeof(long)) else if (fi.FieldType == typeof(long))
{ {
long x = (long)fi.GetValue(str); var x = (long) fi.GetValue(str);
x = ((x & 0x00000000FFFFFFFF) << 32) | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x00000000FFFFFFFF) << 32) | (long) (((ulong) x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x0000FFFF0000FFFF) << 16) | (long) (((ulong) x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8); x = ((x & 0x00FF00FF00FF00FF) << 8) | (long) (((ulong) x & 0xFF00FF00FF00FF00) >> 8);
fi.SetValue(str, x); fi.SetValue(str, x);
} }
else if(fi.FieldType == typeof(ushort)) else if (fi.FieldType == typeof(ushort))
{ {
ushort x = (ushort)fi.GetValue(str); var x = (ushort) fi.GetValue(str);
fi.SetValue(str, (ushort)((x << 8) | (x >> 8))); fi.SetValue(str, (ushort) ((x << 8) | (x >> 8)));
} }
else if(fi.FieldType == typeof(uint)) else if (fi.FieldType == typeof(uint))
{ {
uint x = (uint)fi.GetValue(str); var x = (uint) fi.GetValue(str);
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
fi.SetValue(str, (x << 16) | (x >> 16)); fi.SetValue(str, (x << 16) | (x >> 16));
} }
else if(fi.FieldType == typeof(ulong)) else if (fi.FieldType == typeof(ulong))
{ {
ulong x = (ulong)fi.GetValue(str); var x = (ulong) fi.GetValue(str);
x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
fi.SetValue(str, x); fi.SetValue(str, x);
} }
else if(fi.FieldType == typeof(float)) else if (fi.FieldType == typeof(float))
{ {
float flt = (float)fi.GetValue(str); var flt = (float) fi.GetValue(str);
byte[] flt_b = BitConverter.GetBytes(flt); var flt_b = BitConverter.GetBytes(flt);
fi.SetValue(str, BitConverter.ToSingle(new[] {flt_b[3], flt_b[2], flt_b[1], flt_b[0]}, 0)); fi.SetValue(str, BitConverter.ToSingle(new[] {flt_b[3], flt_b[2], flt_b[1], flt_b[0]}, 0));
} }
else if(fi.FieldType == typeof(double)) else if (fi.FieldType == typeof(double))
{ {
double dbl = (double)fi.GetValue(str); var dbl = (double) fi.GetValue(str);
byte[] dbl_b = BitConverter.GetBytes(dbl); var dbl_b = BitConverter.GetBytes(dbl);
fi.SetValue(str, fi.SetValue(str,
BitConverter BitConverter
.ToDouble(new[] {dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0]}, .ToDouble(
new[] {dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0]},
0)); 0));
} }
else if(fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte)) else if (fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte))
{ {
// Do nothing, can't byteswap them! // Do nothing, can't byteswap them!
} }
else if(fi.FieldType == typeof(Guid)) else if (fi.FieldType == typeof(Guid))
{ {
// TODO: Swap GUID // TODO: Swap GUID
} }
// TODO: Swap arrays and enums // TODO: Swap arrays and enums
else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) else if (fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray)
{ {
object obj = fi.GetValue(str); var obj = fi.GetValue(str);
object strc = SwapStructureMembersEndian(obj); var strc = SwapStructureMembersEndian(obj);
fi.SetValue(str, strc); fi.SetValue(str, strc);
} }
@@ -353,34 +361,50 @@ namespace DiscImageChef.Helpers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object SwapStructureMembersEndianPdp(object str) public static object SwapStructureMembersEndianPdp(object str)
{ {
Type t = str.GetType(); var t = str.GetType();
FieldInfo[] fieldInfo = t.GetFields(); var fieldInfo = t.GetFields();
foreach(FieldInfo fi in fieldInfo) foreach (var fi in fieldInfo)
if(fi.FieldType == typeof(short) || fi.FieldType == typeof(long) || fi.FieldType == typeof(ushort) || if (fi.FieldType == typeof(short) || fi.FieldType == typeof(long) || fi.FieldType == typeof(ushort) ||
fi.FieldType == typeof(ulong) || fi.FieldType == typeof(float) || fi.FieldType == typeof(double) || fi.FieldType == typeof(ulong) || fi.FieldType == typeof(float) || fi.FieldType == typeof(double) ||
fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte) || fi.FieldType == typeof(Guid)) fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte) || fi.FieldType == typeof(Guid))
{ {
// Do nothing // Do nothing
} }
else if(fi.FieldType == typeof(int)) else if (fi.FieldType == typeof(int))
{ {
int x = (int)fi.GetValue(str); var x = (int) fi.GetValue(str);
fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
} }
else if(fi.FieldType == typeof(uint)) else if (fi.FieldType == typeof(uint))
{ {
uint x = (uint)fi.GetValue(str); var x = (uint) fi.GetValue(str);
fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
} }
// TODO: Swap arrays and enums // TODO: Swap arrays and enums
else if(fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) else if (fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray)
{ {
object obj = fi.GetValue(str); var obj = fi.GetValue(str);
object strc = SwapStructureMembersEndianPdp(obj); var strc = SwapStructureMembersEndianPdp(obj);
fi.SetValue(str, strc); fi.SetValue(str, strc);
} }
return str; return str;
} }
/// <summary>
/// Marshal a structure to little-endian binary data
/// </summary>
/// <param name="bytes">Byte array containing the binary data</param>
/// <typeparam name="T">Type of the structure to marshal</typeparam>
/// <returns>The binary data marshalled in a structure with the specified type</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct
{
var buf = new byte[SizeOf<T>()];
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false);
ptr.Free();
return buf;
}
} }
} }

View File

@@ -37,8 +37,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
using DiscImageChef.CommonTypes.Enums; using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Interop;
using DiscImageChef.CommonTypes.Structs; using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console; using DiscImageChef.Console;
using DiscImageChef.Devices.Remote; using DiscImageChef.Devices.Remote;
@@ -129,7 +129,7 @@ namespace DiscImageChef.Commands
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf); var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (Encoding.ASCII.GetString(hdr.id) != Consts.PacketId) if (hdr.id != Consts.PacketId)
{ {
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet, exiting..."); DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet, exiting...");
return (int) Errno.EINVAL; return (int) Errno.EINVAL;
@@ -163,6 +163,33 @@ namespace DiscImageChef.Commands
serverHello.machine); serverHello.machine);
DicConsole.WriteLine("Server maximum protocol: {0}", serverHello.maxProtocol); DicConsole.WriteLine("Server maximum protocol: {0}", serverHello.maxProtocol);
var clientHello = new DicPacketHello
{
application = MainClass.AssemblyTitle,
version = MainClass.AssemblyVersion?.InformationalVersion,
maxProtocol = Consts.MaxProtocol,
sysname = DetectOS.GetPlatformName(
DetectOS.GetRealPlatformID(), DetectOS.GetVersion()),
release = DetectOS.GetVersion(),
machine = "", // TODO: Get architecture
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)
{
DicConsole.ErrorWriteLine("Could not write to the network, exiting...");
return (int) Errno.EIO;
}
socket.Shutdown(SocketShutdown.Both); socket.Shutdown(SocketShutdown.Both);
socket.Close(); socket.Close();
} }