diff --git a/FTPClient/FTPClient/TftpClient.cs b/FTPClient/FTPClient/TftpClient.cs index 8b225b995800cbff2de4995b1bae83b6af93774a..4d77849b50961e6cd7dcf5333be7dcfccbc6013e 100644 --- a/FTPClient/FTPClient/TftpClient.cs +++ b/FTPClient/FTPClient/TftpClient.cs @@ -11,10 +11,12 @@ namespace FTPClient public static Socket socket; public static IPEndPoint ipEndp; public static EndPoint endpoint; + public static string status; + private static int OpCode; public static NetworkStream ns; public static StreamReader sr; public static StreamWriter sw; - public static string status; + /// <summary> /// Enum for packet types @@ -38,7 +40,7 @@ namespace FTPClient } /// <summary> - /// Initialize socket and send out a write request + /// Initialize socket and send out a read request /// </summary> public void Initialize() { @@ -50,7 +52,7 @@ namespace FTPClient string filename = "README"; byte[] packet = new byte[4 + filename.Length + mode.Length]; packet[1] = (byte)PACKET_TYPE.RRQ; - + OpCode = (int)PACKET_TYPE.RRQ; int index = 2; foreach(char letter in filename) { @@ -66,6 +68,33 @@ namespace FTPClient } socket.SendTo(packet, endpoint); + handleReadRequest(); + } + + private void handleReadRequest() + { + byte[] buffer = new byte[256]; + socket.ReceiveFrom(buffer, ref endpoint); + int opCode = (int)(buffer[1] << 8 | buffer[0]); + if(!CheckOpCode(opCode)) return; + + + } + + private static bool CheckOpCode(int op) + { + if (!op.Equals(OpCode)) + { + SendError("OpCode is incorrect!"); + return false; + } + return true; + } + + private static void SendError(string error) + { + Console.WriteLine(error); + // TODO: Send error message } /// <summary> diff --git a/TFTPServer/TFTPServer/Server.cs b/TFTPServer/TFTPServer/Server.cs index c8f0da64ddb2503f1e656f3aaeb847271d7e9bb1..066a191e66b017076e4f8355c280045e8d86b3d5 100644 --- a/TFTPServer/TFTPServer/Server.cs +++ b/TFTPServer/TFTPServer/Server.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Net; using System.Net.Sockets; using System.Text; @@ -12,6 +13,8 @@ namespace TFTPServer public static IPEndPoint iep; public static List<EndPoint> clients; public static int port = 69; + private static int OpCode = 2; + private const string FILE_DIR = @"C:\temp"; /// <summary> /// Enum for packet types @@ -25,7 +28,19 @@ namespace TFTPServer ERROR } - private static void ReadRequest(byte[] buffer) + enum ERROR_CODE + { + Not_defined, + File_not_found, + Access_violation, + Disk_full, + Illegal_TFTP_operation, + Unknown_transfer_ID, + File_already_exists, + No_such_user + } + + private static void ReadRequest(byte[] buffer, EndPoint remote) { string filename = string.Empty; int index = 2; @@ -35,6 +50,37 @@ namespace TFTPServer index += 1; } while (buffer[index] != 0); Console.WriteLine($"Read request for file : {filename}"); + + string file_path = Path.Combine(FILE_DIR, filename); + if (!String.IsNullOrEmpty(filename) && File.Exists(file_path)) { + SendData(); + } else + { + string message = "File not found."; + SendError(remote, (int)ERROR_CODE.File_not_found, message); + } + } + + private static void SendData() + { + // TODO: Send file data + } + + private static void SendError(EndPoint remote, int error_code, string message) + { + byte[] messageBytes = Encoding.ASCII.GetBytes(message); + byte[] packet = new byte[4 + messageBytes.Length + 1]; + packet[0] = (byte)OpCode; + packet[1] = (byte)(OpCode >> 8); + packet[2] = (byte)error_code; + packet[3] = (byte)(error_code >> 8); + + int index = 4; + for(int i = 0; i < messageBytes.Length; i++) + { + packet[index] = messageBytes[i]; + } + socket.SendTo(packet, remote); } private static void WriteRequest(byte[] buffer) @@ -85,7 +131,7 @@ namespace TFTPServer int opcode = buffer[0] + buffer[1]; switch (opcode) { case (int)PACKET_TYPE.RRQ: - ReadRequest(buffer); + ReadRequest(buffer, remote); break; case (int)PACKET_TYPE.WRQ: WriteRequest(buffer); @@ -102,29 +148,6 @@ namespace TFTPServer default: break; } - - String rec_string = Encoding.ASCII.GetString(buffer, 0, received); - char[] delim = { ';' }; - String[] palat = rec_string.Split(delim, 2); - if (palat.Length < 2) - { - Console.WriteLine("Kirjoita viesti muodossa nimi;teksti"); - } - else - { - if (!clients.Contains(remote)) - { - clients.Add(remote); - Console.WriteLine("Uusi asiakas: [{0}:{1}]", ((IPEndPoint)remote).Address, ((IPEndPoint)remote).Port); - } - Console.WriteLine("{0}: {1}", palat[0], palat[1]); - foreach (EndPoint client in clients) - { - //String kaikille = palat[0] + ": " + palat[1]; - // lähetä rec_string - socket.SendTo(Encoding.ASCII.GetBytes(rec_string), client); - } - } } Console.ReadKey();