コード例 #1
0
        public bool Open(string devicePath, out int lastError)
        {
            lastError = 0;

            var cmdPkt = new AaruPacketCommandOpenDevice
            {
                hdr = new AaruPacketHeader
                {
                    remote_id  = Consts.RemoteId, packet_id = Consts.PacketId,
                    len        = (uint)Marshal.SizeOf <AaruPacketCommandOpenDevice>(), version = Consts.PacketVersion,
                    packetType = AaruPacketType.CommandOpen
                },
                device_path = devicePath
            };

            byte[] buf = Marshal.StructureToByteArrayLittleEndian(cmdPkt);

            int len = _socket.Send(buf, SocketFlags.None);

            if (len != buf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not write to the network...");
                lastError = -1;

                return(false);
            }

            byte[] hdrBuf = new byte[Marshal.SizeOf <AaruPacketHeader>()];

            len = Receive(_socket, hdrBuf, hdrBuf.Length, SocketFlags.Peek);

            if (len < hdrBuf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");
                lastError = -1;

                return(false);
            }

            AaruPacketHeader hdr = Marshal.ByteArrayToStructureLittleEndian <AaruPacketHeader>(hdrBuf);

            if (hdr.remote_id != Consts.RemoteId ||
                hdr.packet_id != Consts.PacketId)
            {
                AaruConsole.ErrorWriteLine("Received data is not an Aaru Remote Packet...");
                lastError = -1;

                return(false);
            }

            if (hdr.packetType != AaruPacketType.Nop)
            {
                AaruConsole.ErrorWriteLine("Expected List Devices Response Packet, got packet type {0}...",
                                           hdr.packetType);

                lastError = -1;

                return(false);
            }

            buf = new byte[hdr.len];
            len = Receive(_socket, buf, buf.Length, SocketFlags.None);

            if (len < buf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");
                lastError = -1;

                return(false);
            }

            AaruPacketNop nop = Marshal.ByteArrayToStructureLittleEndian <AaruPacketNop>(buf);

            switch (nop.reasonCode)
            {
            case AaruNopReason.OpenOk:         return(true);

            case AaruNopReason.NotImplemented: throw new NotImplementedException($"{nop.reason}");
            }

            AaruConsole.ErrorWriteLine($"{nop.reason}");
            lastError = nop.errno;

            return(false);
        }
コード例 #2
0
        public Remote(string host)
        {
            _host = host;
            IPHostEntry ipHostEntry = Dns.GetHostEntry(host);

            IPAddress ipAddress =
                ipHostEntry.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);

            if (ipAddress is null)
            {
                AaruConsole.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);

            AaruConsole.WriteLine("Connected to {0}", host);

            byte[] hdrBuf = new byte[Marshal.SizeOf <AaruPacketHeader>()];

            int len = Receive(_socket, hdrBuf, hdrBuf.Length, SocketFlags.Peek);

            if (len < hdrBuf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");

                throw new IOException();
            }

            AaruPacketHeader hdr = Marshal.ByteArrayToStructureLittleEndian <AaruPacketHeader>(hdrBuf);

            if (hdr.remote_id != Consts.RemoteId ||
                hdr.packet_id != Consts.PacketId)
            {
                AaruConsole.ErrorWriteLine("Received data is not an Aaru Remote Packet...");

                throw new ArgumentException();
            }

            byte[] buf;

            if (hdr.packetType != AaruPacketType.Hello)
            {
                if (hdr.packetType != AaruPacketType.Nop)
                {
                    AaruConsole.ErrorWriteLine("Expected Hello Packet, got packet type {0}...", hdr.packetType);

                    throw new ArgumentException();
                }

                buf = new byte[hdr.len];
                len = Receive(_socket, buf, buf.Length, SocketFlags.None);

                if (len < buf.Length)
                {
                    AaruConsole.ErrorWriteLine("Could not read from the network...");

                    throw new IOException();
                }

                AaruPacketNop nop = Marshal.ByteArrayToStructureLittleEndian <AaruPacketNop>(buf);

                AaruConsole.ErrorWriteLine($"{nop.reason}");

                throw new ArgumentException();
            }

            if (hdr.version != Consts.PacketVersion)
            {
                AaruConsole.ErrorWriteLine("Unrecognized packet version...");

                throw new ArgumentException();
            }

            buf = new byte[hdr.len];
            len = Receive(_socket, buf, buf.Length, SocketFlags.None);

            if (len < buf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");

                throw new IOException();
            }

            AaruPacketHello serverHello = Marshal.ByteArrayToStructureLittleEndian <AaruPacketHello>(buf);

            ServerApplication            = serverHello.application;
            ServerVersion                = serverHello.version;
            ServerOperatingSystem        = serverHello.sysname;
            ServerOperatingSystemVersion = serverHello.release;
            ServerArchitecture           = serverHello.machine;
            ServerProtocolVersion        = serverHello.maxProtocol;

            var clientHello = new AaruPacketHello
            {
                application = "Aaru", version = Version.GetVersion(), maxProtocol = Consts.MaxProtocol,
                sysname     = DetectOS.GetPlatformName(DetectOS.GetRealPlatformID(), DetectOS.GetVersion()),
                release     = DetectOS.GetVersion(), machine = RuntimeInformation.ProcessArchitecture.ToString(), hdr =
                    new AaruPacketHeader
                {
                    remote_id  = Consts.RemoteId, packet_id = Consts.PacketId,
                    len        = (uint)Marshal.SizeOf <AaruPacketHello>(), version = Consts.PacketVersion,
                    packetType = AaruPacketType.Hello
                }
            };

            buf = Marshal.StructureToByteArrayLittleEndian(clientHello);

            len = _socket.Send(buf, SocketFlags.None);

            if (len >= buf.Length)
            {
                return;
            }

            AaruConsole.ErrorWriteLine("Could not write to the network...");

            throw new IOException();
        }
コード例 #3
0
        public DeviceInfo[] ListDevices()
        {
            var cmdPkt = new AaruPacketCommandListDevices
            {
                hdr = new AaruPacketHeader
                {
                    remote_id  = Consts.RemoteId, packet_id = Consts.PacketId,
                    len        = (uint)Marshal.SizeOf <AaruPacketCommandListDevices>(), version = Consts.PacketVersion,
                    packetType = AaruPacketType.CommandListDevices
                }
            };

            byte[] buf = Marshal.StructureToByteArrayLittleEndian(cmdPkt);

            int len = _socket.Send(buf, SocketFlags.None);

            if (len != buf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not write to the network...");

                return(new DeviceInfo[0]);
            }

            byte[] hdrBuf = new byte[Marshal.SizeOf <AaruPacketHeader>()];

            len = Receive(_socket, hdrBuf, hdrBuf.Length, SocketFlags.Peek);

            if (len < hdrBuf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");

                return(new DeviceInfo[0]);
            }

            AaruPacketHeader hdr = Marshal.ByteArrayToStructureLittleEndian <AaruPacketHeader>(hdrBuf);

            if (hdr.remote_id != Consts.RemoteId ||
                hdr.packet_id != Consts.PacketId)
            {
                AaruConsole.ErrorWriteLine("Received data is not an Aaru Remote Packet...");

                return(new DeviceInfo[0]);
            }

            if (hdr.packetType != AaruPacketType.ResponseListDevices)
            {
                if (hdr.packetType != AaruPacketType.Nop)
                {
                    AaruConsole.ErrorWriteLine("Expected List Devices Response Packet, got packet type {0}...",
                                               hdr.packetType);

                    return(new DeviceInfo[0]);
                }

                buf = new byte[hdr.len];
                len = Receive(_socket, buf, buf.Length, SocketFlags.None);

                if (len < buf.Length)
                {
                    AaruConsole.ErrorWriteLine("Could not read from the network...");

                    return(new DeviceInfo[0]);
                }

                AaruPacketNop nop = Marshal.ByteArrayToStructureLittleEndian <AaruPacketNop>(buf);

                AaruConsole.ErrorWriteLine($"{nop.reason}");

                return(new DeviceInfo[0]);
            }

            if (hdr.version != Consts.PacketVersion)
            {
                AaruConsole.ErrorWriteLine("Unrecognized packet version...");

                return(new DeviceInfo[0]);
            }

            buf = new byte[hdr.len];
            len = Receive(_socket, buf, buf.Length, SocketFlags.None);

            if (len < buf.Length)
            {
                AaruConsole.ErrorWriteLine("Could not read from the network...");

                return(new DeviceInfo[0]);
            }

            AaruPacketResponseListDevices response =
                Marshal.ByteArrayToStructureLittleEndian <AaruPacketResponseListDevices>(buf);

            List <DeviceInfo> devices = new List <DeviceInfo>();
            int offset     = Marshal.SizeOf <AaruPacketResponseListDevices>();
            int devInfoLen = Marshal.SizeOf <DeviceInfo>();

            for (ushort i = 0; i < response.devices; i++)
            {
                DeviceInfo dev = Marshal.ByteArrayToStructureLittleEndian <DeviceInfo>(buf, offset, devInfoLen);
                dev.Path = dev.Path[0] == '/' ? $"aaru://{_host}{dev.Path}" : $"aaru://{_host}/{dev.Path}";
                devices.Add(dev);
                offset += devInfoLen;
            }

            return(devices.ToArray());
        }