private void SendError(TftpError errorCode, String errorMessage, IPEndPoint remoteEndPoint) { Console.WriteLine("Sending error {0} '{1}'", errorCode, errorMessage); using (var writer = new TftpPacketWriter(errorMessage.Length + 5)) { writer.WriteUInt16((UInt16)TftpPacketType.Error); writer.WriteUInt16((UInt16)errorCode); writer.WriteString(errorMessage); SendData(writer.ToArray(), remoteEndPoint); } }
// NOTE: TFTP packets use network byte order (big-endian). This does not appear to be mentioned in the RFC. public static ITftpPacket Parse(byte[] bytes) { int index = 0; TftpOperation op = (TftpOperation)EndianBitConverter.Big.ToUInt16(bytes, index); index += 2; string fileName; TftpMode mode; ushort blockNumber; switch (op) { case TftpOperation.ReadRequest: fileName = ParseNetascii(bytes, ref index); mode = ParseMode(bytes, ref index); return(new RrqPacket(fileName, mode)); case TftpOperation.WriteRequest: fileName = ParseNetascii(bytes, ref index); mode = ParseMode(bytes, ref index); return(new WrqPacket(fileName, mode)); case TftpOperation.Data: blockNumber = EndianBitConverter.Big.ToUInt16(bytes, index); index += 2; byte[] data = new byte[bytes.Length - index]; Buffer.BlockCopy(bytes, index, data, 0, data.Length); return(new DataPacket(blockNumber, data)); case TftpOperation.Ack: blockNumber = EndianBitConverter.Big.ToUInt16(bytes, index); return(new AckPacket(blockNumber)); case TftpOperation.Error: TftpError error = (TftpError)EndianBitConverter.Big.ToUInt16(bytes, index); index += 2; string message = ParseNetascii(bytes, ref index); return(new ErrorPacket(error, message)); default: throw new Exception("Operation not recognized."); } }
public ErrorPacket(TftpError error, string message) { Error = error; Message = message; }