Example #1
0
        private void ProcessReadRequest(IPEndPoint remoteEndPoint, TftpPacket requestPacket)
        {
            if (String.IsNullOrEmpty(requestPacket.FileName))
            {
                return; // TODO
            }

            if (requestPacket.Mode != TftpMode.Octet)
            {
                SendError(TftpError.IllegalOperation, "Transfer mode not supported", remoteEndPoint);
                return;
            }

            var fileName = requestPacket.FileName.Replace('/', '\\');

            if ('\\' == fileName[0])
            {
                fileName = fileName.Substring(1);
            }
            fileName = Path.Combine(_rootDirectory, fileName);
            Console.WriteLine("Local file name: '{0}'", fileName);

            _fileInfo = new FileInfo(fileName);

            if (!_fileInfo.Exists)
            {
                SendError(TftpError.FileNotFound, "File not found", remoteEndPoint);
                return;
            }

            var responsePacket = new TftpPacket(TftpPacketType.OptionAcknowledgment);

            if (0 == requestPacket.TSize)
            {
                responsePacket.Options.Add("tsize", (Int32)_fileInfo.Length);
            }

            _blockSize = 512;
            if (requestPacket.BlockSize > 0)
            {
                _blockSize = requestPacket.BlockSize;
                responsePacket.Options.Add("blksize", _blockSize);
            }

            _fileBlock = new byte[_blockSize + 4];

            _timeout = -1; // TODO: implement
            if (requestPacket.Timeout > 0)
            {
                _timeout = requestPacket.Timeout;
                responsePacket.Options.Add("timeout", _timeout);
            }

            SendData(responsePacket.ToArray(), remoteEndPoint);
        }