Receive hello packet.

This commit is contained in:
2019-10-12 19:53:12 +01:00
parent 7f5a775941
commit 36d273b7af
2 changed files with 58 additions and 4 deletions

View File

@@ -3,10 +3,10 @@ using System.Runtime.InteropServices;
namespace DiscImageChef.Devices.Remote namespace DiscImageChef.Devices.Remote
{ {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DicPacketHeader public struct DicPacketHeader
{ {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public string id; public byte[] id;
public uint len; public uint len;
public byte version; public byte version;
@@ -17,7 +17,7 @@ namespace DiscImageChef.Devices.Remote
} }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct DicPacketHello public struct DicPacketHello
{ {
public DicPacketHeader hdr; public DicPacketHeader hdr;

View File

@@ -30,14 +30,19 @@
// Copyright © 2011-2019 Natalia Portillo // Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
// TODO: Fix errors returned
using System; using System;
using System.Collections.Generic; 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.Structs; using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console; using DiscImageChef.Console;
using DiscImageChef.Devices.Remote;
using DiscImageChef.Helpers;
using Mono.Options; using Mono.Options;
namespace DiscImageChef.Commands namespace DiscImageChef.Commands
@@ -109,6 +114,55 @@ namespace DiscImageChef.Commands
try try
{ {
socket.Connect(ipEndPoint); 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, exiting...");
return (int) Errno.EIO;
}
var hdr = Marshal.ByteArrayToStructureLittleEndian<DicPacketHeader>(hdrBuf);
if (Encoding.ASCII.GetString(hdr.id) != Consts.PacketId)
{
DicConsole.ErrorWriteLine("Received data is not a DIC Remote Packet, exiting...");
return (int) Errno.EINVAL;
}
if (hdr.packetType != DicPacketType.Hello)
{
DicConsole.ErrorWriteLine("Expected Hello Packet, got packet type {0}, exiting...", hdr.packetType);
return (int) Errno.EINVAL;
}
if (hdr.version != Consts.PacketVersion)
{
DicConsole.ErrorWriteLine("Unrecognized packet version, exiting...");
return (int) Errno.EINVAL;
}
var 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, exiting...");
return (int) Errno.EIO;
}
var serverHello = Marshal.ByteArrayToStructureLittleEndian<DicPacketHello>(buf);
DicConsole.WriteLine("Server application: {0} {1}", serverHello.application, serverHello.version);
DicConsole.WriteLine("Server operating system: {0} {1} ({2})", serverHello.sysname, serverHello.release,
serverHello.machine);
DicConsole.WriteLine("Server maximum protocol: {0}", serverHello.maxProtocol);
socket.Shutdown(SocketShutdown.Both); socket.Shutdown(SocketShutdown.Both);
socket.Close(); socket.Close();
} }