Exemplo n.º 1
0
        public int PutFile(string filename)
        {
            byte[] txBuf = new byte[516];
            int    txCount;
            string s = System.IO.File.ReadAllText(filename);

            System.IO.StringReader sr = new System.IO.StringReader(s);
            short         blockNumber;
            ASCIIEncoding ae = new ASCIIEncoding();

            sendTftpPacket(Client.OP_CODE.WRQ, filename, null, -1);//send WRQ
            char[] chunk      = new char[512];
            int    dataLength = sr.ReadBlock(chunk, 0, 512);

            do
            {
                txCount = recvTftpPacket(ref txBuf);
                OP_CODE op = (OP_CODE)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 0));
                System.Diagnostics.Debug.Assert(op == OP_CODE.ACK);
                blockNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 2));
                //s += new string(ae.GetChars(rxBuf, sizeof(short) * 2, rxCount - 4 >= 4 ? rxCount - 4 : 0));
                byte[] data = new byte[dataLength];
                int    chrCount, byteCount;
                bool   completed;
                ae.GetEncoder().Convert(chunk, 0, dataLength, data, 0, data.Length, true, out chrCount, out byteCount, out completed);

                sendTftpPacket(Client.OP_CODE.DATA, string.Empty, data, ++blockNumber);
            } while ((dataLength = sr.ReadBlock(chunk, 0, 512)) == 512);
            //sendTftpPacket(Client.OP_CODE.ACK, string.Empty, null, blockNumber);//final ACK
            return(txCount);
        }
Exemplo n.º 2
0
        public int PutBinaryFile(string filename)
        {
            byte[]        txBuf = new byte[516];
            byte[]        data;
            int           txCount;
            FileStream    fs = new FileStream(filename, FileMode.Open);
            BinaryReader  br = new BinaryReader(fs);
            short         blockNumber;
            ASCIIEncoding ae = new ASCIIEncoding();

            sendTftpPacket(Client.OP_CODE.WRQ, Path.GetFileName(filename), null, -1);//send WRQ
            //char[] chunk = new char[512];
            //int dataLength = sr.ReadBlock(chunk, 0, 512);
            do
            {
                txCount = recvTftpPacket(ref txBuf);
                OP_CODE op = (OP_CODE)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 0));
                System.Diagnostics.Debug.Assert(op == OP_CODE.ACK);
                blockNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(txBuf, 2));
                //s += new string(ae.GetChars(rxBuf, sizeof(short) * 2, rxCount - 4 >= 4 ? rxCount - 4 : 0));
                data = br.ReadBytes(512);
                //int chrCount, byteCount;
                //bool completed;
                //ae.GetEncoder().Convert(chunk, 0, dataLength, data, 0, data.Length, true, out chrCount, out byteCount, out completed);

                sendTftpPacket(Client.OP_CODE.DATA, string.Empty, data, ++blockNumber);
            } while (data.Length == 512);
            //sendTftpPacket(Client.OP_CODE.ACK, string.Empty, null, blockNumber);//final ACK
            return(txCount);
        }
Exemplo n.º 3
0
        private void sendMessage(OP_CODE op_code, object obj)
        {
            switch (op_code)
            {
            case OP_CODE.HELLO:
                break;

            case OP_CODE.RECEIVED:
                break;

            case OP_CODE.REQUEST:
                break;

            case OP_CODE.POSITION:
                break;

            case OP_CODE.SHOOT:
                break;

            case OP_CODE.DEAD:
                break;

            default:
                break;
            }
            throw new NotImplementedException();
        }
Exemplo n.º 4
0
        public string GetFile(string filename)
        {
            byte[]        rxBuf = new byte[516];
            int           rxCount;
            string        s = "";
            short         blockNumber;
            ASCIIEncoding ae = new ASCIIEncoding();

            sendTftpPacket(Client.OP_CODE.RRQ, filename, null, -1);//send RRQ

            do
            {
                rxCount = recvTftpPacket(ref rxBuf);
                OP_CODE op = (OP_CODE)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rxBuf, 0));
                blockNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rxBuf, 2));
                s          += new string(ae.GetChars(rxBuf, sizeof(short) * 2, rxCount - 4 >= 4 ? rxCount - 4 : 0));;
                sendTftpPacket(Client.OP_CODE.ACK, string.Empty, null, blockNumber);
            } while (rxCount >= 516);
            //sendTftpPacket(Client.OP_CODE.ACK, string.Empty, null, blockNumber);//final ACK
            return(s);
        }
Exemplo n.º 5
0
        public void sendTftpPacket(OP_CODE op, string fname, byte[] data, short blockNumber)
        {
            byte[] buf      = null;
            byte[] mode     = null;
            byte[] txBuffer = null;
            byte[] b_op     = null;

            if (fname.Length > 0)
            {
                buf = new byte[fname.ToCharArray().Length + 1];
                for (int i = 0; i < buf.Length - 1; i++)
                {
                    buf[i] = (byte)ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, BitConverter.GetBytes(fname[i])).GetValue(0);
                }
                buf[buf.Length - 1] = (byte)'\0';
                mode = new byte["octet\0".Length];
                for (int i = 0; i < mode.Length - 1; i++)
                {
                    mode[i] = (byte)ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, BitConverter.GetBytes("octet\0"[i])).GetValue(0);
                }
            }
            switch (op)
            {
            case OP_CODE.RRQ:
                txBuffer = new byte[buf.Length + 2 + mode.Length];
                b_op     = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.RRQ));
                Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                Buffer.BlockCopy(buf, 0, txBuffer, b_op.Length, buf.Length);
                Buffer.BlockCopy(mode, 0, txBuffer, b_op.Length + buf.Length, mode.Length);
                s.Send(txBuffer);
                break;

            case OP_CODE.WRQ:
                txBuffer = new byte[buf.Length + 2 + mode.Length];
                b_op     = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.WRQ));
                Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                Buffer.BlockCopy(buf, 0, txBuffer, b_op.Length, buf.Length);
                Buffer.BlockCopy(mode, 0, txBuffer, b_op.Length + buf.Length, mode.Length);
                s.Send(txBuffer);
                break;

            case OP_CODE.CD:
                break;

            case OP_CODE.LIST:
                break;

            case OP_CODE.ACK:
                if (blockNumber >= 0)
                {
                    txBuffer = new byte[4];
                    b_op     = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.ACK));
                    Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                    byte[] blockAck = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber));
                    Buffer.BlockCopy(blockAck, 0, txBuffer, b_op.Length, sizeof(short));
                    s.Send(txBuffer);
                }
                break;

            case OP_CODE.CLOSE:

                txBuffer = new byte[4];
                b_op     = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.ACK));
                Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                //byte[] closeBuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber));
                //Buffer.BlockCopy(closeBuf, 0, txBuffer, b_op.Length, sizeof(short));
                txBuffer[2] = 0x55; txBuffer[3] = 0x55;
                s.Send(txBuffer);
                break;

            case OP_CODE.DATA:
                txBuffer = new byte[4 + data.Length];
                b_op     = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.DATA));
                Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber)), 0, txBuffer, 2, 2);
                Buffer.BlockCopy(data, 0, txBuffer, 4, data.Length);
                s.Send(txBuffer);
                break;

            case OP_CODE.ERROR:
                break;

            default:
                break;
            }
        }
        private FileTransfer.FileSegmentAssembler GetOrCreateAssembler(NetworkTcpSession tcpSession, bool fileTransferIsClientToServer, Guid fileId, OP_CODE smb2Command)
        {
            string uniqueFileId = GetUniqueGuid(tcpSession, fileId);

            FileTransfer.FileSegmentAssembler assembler = null;
            if (!this.fileSegmentAssemblerList.ContainsKey(uniqueFileId))
            {
                if (this.fileIdFilenameMap.ContainsKey(uniqueFileId))
                {
                    assembler = new FileTransfer.FileSegmentAssembler(this.fileOutputDirectory, tcpSession, fileTransferIsClientToServer, this.fileIdFilenameMap[uniqueFileId], uniqueFileId, base.MainPacketHandler.FileStreamAssemblerList, this.fileSegmentAssemblerList, FileTransfer.FileStreamTypes.SMB2, "SMB2 " + Enum.GetName(typeof(OP_CODE), smb2Command) + " " + fileId.ToString() + " \"" + this.fileIdFilenameMap[uniqueFileId] + "\"", null);
                    this.fileSegmentAssemblerList.Add(uniqueFileId, assembler);
                }
            }
            else
            {
                assembler = this.fileSegmentAssemblerList[uniqueFileId];
            }
            return(assembler);
        }
Exemplo n.º 7
0
        public void sendTftpPacket(OP_CODE op,string fname, byte[] data, short blockNumber)
        {
            byte[] buf = null;
            byte[] mode = null;
            byte[] txBuffer = null;
            byte[] b_op = null;

            if (fname.Length>0)
            {
                buf = new byte[fname.ToCharArray().Length + 1];
                for (int i = 0; i < buf.Length - 1; i++)
                {
                    buf[i] = (byte)ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, BitConverter.GetBytes(fname[i])).GetValue(0);
                }
                buf[buf.Length - 1] = (byte)'\0';
                mode = new byte["octet\0".Length];
                for (int i = 0; i < mode.Length - 1; i++)
                {
                    mode[i] = (byte)ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, BitConverter.GetBytes("octet\0"[i])).GetValue(0);
                }
            }
            switch (op)
            {
                case OP_CODE.RRQ:
                    txBuffer = new byte[buf.Length+2+mode.Length];
                    b_op = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.RRQ));
                    Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                    Buffer.BlockCopy(buf, 0, txBuffer, b_op.Length, buf.Length);
                    Buffer.BlockCopy(mode, 0, txBuffer, b_op.Length + buf.Length, mode.Length);
                    s.Send(txBuffer);
                    break;
                case OP_CODE.WRQ:
                    txBuffer = new byte[buf.Length + 2 + mode.Length];
                    b_op = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.WRQ));
                    Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                    Buffer.BlockCopy(buf, 0, txBuffer, b_op.Length, buf.Length);
                    Buffer.BlockCopy(mode, 0, txBuffer, b_op.Length + buf.Length, mode.Length);
                    s.Send(txBuffer);
                    break;
                case OP_CODE.CD:
                    break;
                case OP_CODE.LIST:
                    break;
                case OP_CODE.ACK:
                    if (blockNumber>=0)
                    {
                        txBuffer = new byte[4];
                        b_op = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.ACK));
                        Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                        byte[] blockAck = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber));
                        Buffer.BlockCopy(blockAck, 0, txBuffer, b_op.Length, sizeof(short));
                        s.Send(txBuffer);
                    }
                    break;
                case OP_CODE.CLOSE:

                        txBuffer = new byte[4];
                        b_op = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.ACK));
                        Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                        //byte[] closeBuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber));
                        //Buffer.BlockCopy(closeBuf, 0, txBuffer, b_op.Length, sizeof(short));
                        txBuffer[2] = 0x55; txBuffer[3] = 0x55;
                        s.Send(txBuffer);
                    break;
                case OP_CODE.DATA:
                    txBuffer = new byte[4+data.Length];
                    b_op = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)OP_CODE.DATA));
                    Buffer.BlockCopy(b_op, 0, txBuffer, 0, b_op.Length);
                    Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(blockNumber)), 0, txBuffer, 2, 2);
                    Buffer.BlockCopy(data, 0, txBuffer, 4, data.Length);
                    s.Send(txBuffer);
                    break;
                case OP_CODE.ERROR:
                    break;
                default:
                    break;
            }
        }
Exemplo n.º 8
0
 public override string ToString()
 {
     return(OP_CODE.ToString() + " [" + Variable1?.Name + " " + Variable1?.Value?.ToString() + " ]" + " [" + Variable2?.Name + " " + Variable2?.Value?.ToString() + " ]" + " [" + Variable3?.Name + " " + Variable3?.Value?.ToString() + " ]");
 }