public void Request <T>(string ipDistant, string nomFichier, string nomFichierDest = null) where T : TFTP, new() { TFTP rq = new T { StrFichier = nomFichier, PointDistant = new IPEndPoint(IPAddress.Parse(ipDistant), 69) }; if (nomFichierDest == null) { rq.NomFichier = nomFichier; } else { rq.NomFichier = nomFichierDest; } Thread threadRq = new Thread(new ThreadStart(rq.Thread)); threadRq.Start(); Output.Text($"{typeof(T).Name} at IP: {ipDistant} Port: 69"); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("TFTP Client by Pascal Canuel and Justin-Roberge Lavoie"); Console.Write("Please select an option : "); Output.Text("tftp [<Host>] [{get | put}] <Source> [<Destination>]"); string input; string[] cmd; Creator cr = new Creator(); Console.ForegroundColor = ConsoleColor.Yellow; //input color while (true) { input = Console.ReadLine(); cmd = input.Split().Where(x => x != string.Empty).ToArray(); if ((cmd.Length == 4 || cmd.Length == 5) && cmd[0].ToUpper() == "TFTP") //beaucoup de validation a effectuer dans un certain ordre. Remplacer par RULE PATTERN { Match resultIP = Regex.Match(cmd[1], @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"); Match resultFile = Regex.Match(cmd[3], @"^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF|txt|png|mp4)$"); //à optimiser if (resultIP.Success && resultFile.Success) { if (cmd[2].ToUpper() == "GET") { if (cmd.Length == 4) { cr.Request <RRQ>(cmd[1], cmd[3]); } else { Match resultFileDest = Regex.Match(cmd[4], @"^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF|txt|png|mp4)$"); if (resultFileDest.Success) { cr.Request <RRQ>(cmd[1], cmd[3], cmd[4]); } else { Output.Text("Destination incorrect"); } } } else if (cmd[2].ToUpper() == "PUT") { if (cmd.Length == 4) { cr.Request <WRQ>(cmd[1], cmd[3]); } else { Match resultFileDest = Regex.Match(cmd[4], @"^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF|txt|png|mp4)$"); if (resultFileDest.Success) { cr.Request <WRQ>(cmd[1], cmd[3], cmd[4]); } else { Output.Text("Destination incorrect"); } } } else { Output.Text("Commande invalide"); } } } else { Output.Text("Format invalide"); } } }
public override void Thread() { FileStream fs; int nOctetsLus = 0, nTimeOuts = 0, nErreurACK = 0; try { WRQrequest(); m_socket.SendTo(m_tamponEnvoi, 2 + m_nomFichier.Length + 1 + 8 + 1, SocketFlags.None, m_PointDistant); if (!File.Exists(FullPath)) { Output.Text("File not found"); SendError(CodeErreur.FileNotFound); } else { //WRQ fs = File.Open(FullPath, FileMode.Open, FileAccess.Read, FileShare.Read); Send(); do { nOctetsLus = fs.Read(m_tamponEnvoi, 4, 512); do { if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead)) { nTimeOuts++; } else { m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant); if (!Receive(m_tamponReception)) { m_lire = false; nErreurACK++; } else { m_lire = true; m_socket.SendTo(m_tamponEnvoi, nOctetsLus + 4, SocketFlags.None, m_PointDistant); Send(); if (nOctetsLus < 512) { if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead)) { nTimeOuts++; } else { m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant); if (!Receive(m_tamponReception)) { m_lire = false; nErreurACK++; } else { m_lire = true; } } } } } }while (m_lire == false && nTimeOuts < 10 && nErreurACK < 3); }while (nOctetsLus == 512 && nTimeOuts < 10 && nErreurACK < 3); Output.Text($" WRQ closed from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {m_strFichier}"); fs.Close(); m_socket.Close(); } } catch (SocketException se) { Output.Text(se.Message); } }
public override void Thread() { FileStream fs; int nOctetsLus = 0, nTimeOuts = 0, nErreurACK = 0; try { RRQrequest(); m_socket.SendTo(m_tamponEnvoi, 2 + m_strFichier.Length + 1 + 8 + 1, SocketFlags.None, m_PointDistant); Send(); if (File.Exists(FullPath)) { Output.Text("File already exist"); SendError(CodeErreur.FileExisting); } else { fs = File.Open(FullPath, FileMode.CreateNew, FileAccess.Write, FileShare.Write); do { do { if (m_lire = !m_socket.Poll(5000000, SelectMode.SelectRead)) { nTimeOuts++; } else { nOctetsLus = m_socket.ReceiveFrom(m_tamponReception, ref m_PointDistant); if (Receive(m_tamponReception)) { m_lire = false; nErreurACK++; //TODO réenvoyer trame } else { m_lire = true; fs.Write(m_tamponReception, 4, nOctetsLus - 4); m_socket.SendTo(m_tamponEnvoi, 4, SocketFlags.None, m_PointDistant); Send(); } } }while (m_lire == false && nTimeOuts < 10 && nErreurACK < 3); }while (nOctetsLus == 516 && nTimeOuts < 10 && nErreurACK < 3); Output.Text($" RRQ closed from IP: {((IPEndPoint)PointDistant).Address} Port: {((IPEndPoint)PointDistant).Port} ---> {m_strFichier}"); fs.Close(); m_socket.Close(); } } catch (SocketException se) { Output.Text(se.Message); } }