public void Send(IPEndPoint target, TFTPPacket packet) { var bytessend = this.socket.SendTo(packet.Data, packet.Offset, SocketFlags.None, target); this.OnDataSend(bytessend, target, this.type); }
internal void Readfile(IPEndPoint client) { var readedBytes = 0L; var done = false; if (this.fs == null) this.fs = new FileStream(Clients[client.Address].FileName, FileMode.Open, FileAccess.Read, FileShare.Read, Settings.ReadBuffer); if (this.bs == null) this.bs = new BufferedStream(this.fs, Settings.ReadBuffer); // Align the last Block if (Clients[client.Address].TransferSize <= Clients[client.Address].BlockSize) { Clients[client.Address].BlockSize = (int)Clients[client.Address].TransferSize; done = true; } var chunk = new byte[Clients[client.Address].BlockSize]; this.bs.Seek(Clients[client.Address].BytesRead, SeekOrigin.Begin); readedBytes = this.bs.Read(chunk, 0, chunk.Length); Clients[client.Address].BytesRead += readedBytes; Clients[client.Address].TransferSize -= readedBytes; var response = new TFTPPacket(4 + chunk.Length, TFTPOPCodes.DAT, client); if (Clients[client.Address].Blocks == 0) Clients[client.Address].Blocks += 1; response.Block = Convert.ToInt16(Clients[client.Address].Blocks); Array.Copy(chunk, 0, response.Data, response.Offset, chunk.Length); response.Offset += chunk.Length; this.Send(ref response); if (Clients.ContainsKey(client.Address) && done) Clients[client.Address].Stage = TFTPStage.Done; }
internal void Handle_Option_request(long tsize, int blksize, IPEndPoint client) { if (!Clients.ContainsKey(client.Address)) return; Clients[client.Address].Stage = TFTPStage.Handshake; var tmpbuffer = new byte[100]; var offset = 0; var blksizeopt = Exts.StringToByte("blksize"); Array.Copy(blksizeopt, 0, tmpbuffer, offset, blksizeopt.Length); offset += blksizeopt.Length + 1; var blksize_value = Exts.StringToByte(blksize.ToString()); Array.Copy(blksize_value, 0, tmpbuffer, offset, blksize_value.Length); offset += blksize_value.Length + 1; var tsizeOpt = Exts.StringToByte("tsize"); Array.Copy(tsizeOpt, 0, tmpbuffer, offset, tsizeOpt.Length); offset += tsizeOpt.Length + 1; var tsize_value = Exts.StringToByte(tsize.ToString()); Array.Copy(tsize_value, 0, tmpbuffer, offset, tsize_value.Length); offset += tsize_value.Length + 1; var packet = new TFTPPacket(2 + offset, TFTPOPCodes.OCK, client); Array.Copy(tmpbuffer, 0, packet.Data, packet.Offset, offset); packet.Offset += offset; Array.Clear(tmpbuffer, 0, tmpbuffer.Length); this.Send(ref packet); }
internal void ExtractOptions(ref TFTPPacket data) { var parts = Exts.ToParts(data.Data, "\0".ToCharArray()); for (var i = 0; i < parts.Length; i++) { if (i == 0) { if (!Options.ContainsKey("file")) Options.Add("file", parts[i]); else Options["file"] = parts[i]; } if (i == 1) { if (!Options.ContainsKey("mode")) Options.Add("mode", parts[i]); else Options["mode"] = parts[i]; this.SetMode(TFTPMode.Octet, data.Source); } if (parts[i] == "blksize") { if (!Options.ContainsKey(parts[i])) Options.Add(parts[i], parts[i + 1]); else Options[parts[i]] = parts[i + 1]; if (Clients.ContainsKey(data.Source.Address)) Clients[data.Source.Address].BlockSize = int.Parse(Options["blksize"]); } if (parts[i] == "tsize") { if (!Options.ContainsKey(parts[i])) Options.Add(parts[i], parts[i + 1]); else Options[parts[i]] = parts[i + 1]; } if (parts[i] == "windowsize") { if (!Options.ContainsKey(parts[i])) Options.Add(parts[i], parts[i + 1]); else Options[parts[i]] = parts[i + 1]; } } }
internal override void DataReceived(object sender, DataReceivedEventArgs e) { lock (this) { var request = new TFTPPacket(e.Data.Length, TFTPOPCodes.UNK, e.RemoteEndpoint); request.Data = e.Data; request.Type = SocketType.TFTP; switch (request.OPCode) { case TFTPOPCodes.RRQ: var rrq_thread = new Thread(new ParameterizedThreadStart(Handle_RRQ_Request)); rrq_thread.Start(request); break; case TFTPOPCodes.ERR: this.Handle_Error_Request(request.ErrorCode, request.ErrorMessage, request.Source, true); break; case TFTPOPCodes.ACK: if (!Clients.ContainsKey(request.Source.Address)) return; var ack_thread = new Thread(new ParameterizedThreadStart(Handle_ACK_Request)); ack_thread.Start(request); break; default: this.Handle_Error_Request(TFTPErrorCode.IllegalOperation, "Unknown OPCode: {0}".F(request.OPCode), request.Source); break; } } }
public void Send(ref TFTPPacket packet) { this.socket.Send(packet.Source, packet); }
public void Handle_Error_Request(TFTPErrorCode error, string message, IPEndPoint client, bool clientError = false) { if (!Clients.ContainsKey(client.Address)) return; Clients[client.Address].Stage = TFTPStage.Error; if (!clientError) { var response = new TFTPPacket(5 + message.Length, TFTPOPCodes.ERR, client); response.ErrorCode = error; response.ErrorMessage = message; this.Send(ref response); } Errorhandler.Report(LogTypes.Error, "[TFTP] {0}: {1}".F(error, message)); if (this.fs != null) this.fs.Close(); if (this.bs != null) this.bs.Close(); Clients.Remove(client.Address); }
public static sbyte GetTFTPOPCode(TFTPPacket packet) => GetTFTPOPCode(packet.Data);