kiah

Untitled

Mar 1st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.nio.ByteBuffer;
  5.  
  6. public class TFTPServer
  7. {
  8. public static final int TFTPPORT = 4970;
  9. public static final int BUFSIZE = 516;
  10. public static final String READDIR = "/home/username/read/"; //custom address at your PC
  11. public static final String WRITEDIR = "/home/username/write/"; //custom address at your PC
  12. // OP codes
  13. public static final int OP_RRQ = 1;
  14. public static final int OP_WRQ = 2;
  15. public static final int OP_DAT = 3;
  16. public static final int OP_ACK = 4;
  17. public static final int OP_ERR = 5;
  18.  
  19. public static void main(String[] args) {
  20. if (args.length > 0)
  21. {
  22. System.err.printf("usage: java %s\n", TFTPServer.class.getCanonicalName());
  23. System.exit(1);
  24. }
  25. //Starting the server
  26. try
  27. {
  28. TFTPServer server= new TFTPServer();
  29. server.start();
  30. }
  31. catch (SocketException e)
  32. {e.printStackTrace();}
  33. }
  34.  
  35. private void start() throws SocketException
  36. {
  37. byte[] buf= new byte[BUFSIZE];
  38.  
  39. // Create socket
  40. DatagramSocket socket= new DatagramSocket(null);
  41.  
  42. // Create local bind point
  43. SocketAddress localBindPoint= new InetSocketAddress(TFTPPORT);
  44. socket.bind(localBindPoint);
  45.  
  46. System.out.printf("Listening at port %d for new requests\n", TFTPPORT);
  47.  
  48. // Loop to handle client requests
  49. while (true)
  50. {
  51.  
  52. final InetSocketAddress clientAddress = receiveFrom(socket, buf);
  53.  
  54. // If clientAddress is null, an error occurred in receiveFrom()
  55. if (clientAddress == null)
  56. continue;
  57.  
  58. final StringBuffer requestedFile= new StringBuffer();
  59. final int reqtype = ParseRQ(buf, requestedFile);
  60.  
  61. new Thread()
  62. {
  63. public void run()
  64. {
  65. try
  66. {
  67. DatagramSocket sendSocket= new DatagramSocket(0);
  68.  
  69. // Connect to client
  70. sendSocket.connect(clientAddress);
  71.  
  72. System.out.printf("%s request for %s from %s using port %d\n",
  73. (reqtype == OP_RRQ)?"Read":"Write",
  74. clientAddress.getHostName(), clientAddress.getPort());
  75.  
  76. // Read request
  77. if (reqtype == OP_RRQ)
  78. {
  79. requestedFile.insert(0, READDIR);
  80. HandleRQ(sendSocket, requestedFile.toString(), OP_RRQ);
  81. }
  82. // Write request
  83. else
  84. {
  85. requestedFile.insert(0, WRITEDIR);
  86. HandleRQ(sendSocket,requestedFile.toString(),OP_WRQ);
  87. }
  88. sendSocket.close();
  89. }
  90. catch (SocketException e)
  91. {e.printStackTrace();}
  92. }
  93. }.start();
  94. }
  95. }
  96.  
  97. /**
  98. * Reads the first block of data, i.e., the request for an action (read or write).
  99. * @param socket (socket to read from)
  100. * @param buf (where to store the read data)
  101. * @return socketAddress (the socket address of the client)
  102. */
  103. private InetSocketAddress receiveFrom(DatagramSocket socket, byte[] buf)
  104. {
  105. // Create datagram packet
  106. DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
  107.  
  108. // Receive packet
  109. try {
  110. socket.receive(receivePacket);
  111. }
  112. catch(IOException e)
  113. {
  114. System.out.println("Error: "+e);
  115. }
  116.  
  117. // Get client address and port from the packet
  118. InetSocketAddress socketAddress= new InetSocketAddress(receivePacket.getAddress(), receivePacket.getPort());
  119. return socketAddress;
  120. }
  121.  
  122. /**
  123. * Parses the request in buf to retrieve the type of request and requestedFile
  124. *
  125. * @param buf (received request)
  126. * @param requestedFile (name of file to read/write)
  127. * @return opcode (request type: RRQ or WRQ)
  128. */
  129. private int ParseRQ(byte[] buf, StringBuffer requestedFile)
  130. {
  131. // See "TFTP Formats" in TFTP specification for the RRQ/WRQ request contents
  132. // byte[] buf;
  133. ByteBuffer wrap= ByteBuffer.wrap(buf);
  134. short opcode = wrap.getShort();
  135. String file= requestedFile.toString();
  136.  
  137. file=file.substring(2, requestedFile.length());
  138. String[] parts=file.split("0");
  139. String fileName=(parts[0]);
  140.  
  141. String Mode=parts[1];
  142. System.out.println("Opcode is: "+ opcode);
  143. System.out.println("Requested file: "+ fileName);
  144. System.out.println("Transfer mode is: "+ Mode);
  145. return opcode;
  146. }
  147.  
  148. /**
  149. * Handles RRQ and WRQ requests
  150. *
  151. * @param sendSocket (socket used to send/receive packets)
  152. * @param requestedFile (name of file to read/write)
  153. * @param opcode (RRQ or WRQ)
  154. */
  155. private void HandleRQ(DatagramSocket sendSocket, String requestedFile, int opcode)
  156. {
  157. System.out.println("Requested file: "+requestedFile);
  158. File file=new File(requestedFile);
  159. byte[] buf = new byte[512];
  160. if(opcode == OP_RRQ)
  161. {
  162. // See "TFTP Formats" in TFTP specification for the DATA and ACK packet contents
  163.  
  164. FileInputStream in=null;
  165.  
  166. try {
  167. in = new FileInputStream(requestedFile);
  168.  
  169. int length = in.read(buf);
  170.  
  171.  
  172. short blockNum = 1;
  173. ByteBuffer buffer = ByteBuffer.allocate(BUFSIZE);
  174. buffer.putInt(OP_DAT);
  175. buffer.putShort(blockNum);
  176. buffer.put(buf, 0, length);
  177. DatagramPacket sender = new DatagramPacket(buffer.array(), 4 + length);
  178. System.out.println("Sending...");
  179.  
  180. boolean result = send_DATA_receive_ACK(sendSocket, sender, blockNum++ );
  181. if(result)
  182. {
  183. System.out.println("Successfully sent. BlockNum is: "+ blockNum);
  184. }
  185. else{
  186. System.out.println("ERROR: Connection lost");
  187. send_ERR(sendSocket, opcode, "Connection to server lost");
  188. }
  189. }
  190.  
  191.  
  192.  
  193. catch(FileNotFoundException f)
  194. {
  195. System.out.println("ERROR, FILE NOT FOUND" + f);
  196. send_ERR(sendSocket, OP_ERR, "File not found");
  197. }
  198. catch(IOException e)
  199. {
  200. System.out.println("ERROR READING FILE" + e);
  201. }
  202.  
  203.  
  204. }
  205. else if (opcode == OP_WRQ)
  206. {
  207. if(file.exists())
  208. {
  209. System.out.println("Error, file already exists");
  210. send_ERR(sendSocket, OP_ERR, "Error: file already exists");
  211. }
  212. else{
  213. try{
  214. FileOutputStream out=new FileOutputStream(file);
  215. }
  216. catch(FileNotFoundException f){
  217. System.out.println("ERROR, file could not be created" + f);
  218. send_ERR(sendSocket, OP_ERR, "ERROR: file could not be created");
  219. }
  220. short blockNum=0;
  221. ByteBuffer buffer = ByteBuffer.allocate(BUFSIZE);
  222. buffer.putInt(OP_ACK);
  223. buffer.putShort(blockNum);
  224. DatagramPacket ackP= new DatagramPacket(buffer.array(), 4);
  225.  
  226. boolean result = receive_DATA_send_ACK(sendSocket, ackP, blockNum );
  227. }}
  228. else
  229. {
  230. System.err.println("Invalid request. Sending an error packet.");
  231. // See "TFTP Formats" in TFTP specification for the ERROR packet contents
  232. send_ERR(sendSocket, opcode, "Invalid request. Sending an error packet.");
  233. return;
  234. }
  235. }
  236.  
  237.  
  238. /**
  239. To be implemented
  240. */
  241. private boolean send_DATA_receive_ACK(DatagramSocket sendSocket, DatagramPacket sender, short blockNum)
  242. {
  243.  
  244.  
  245.  
  246. return true;}
  247.  
  248. private boolean receive_DATA_send_ACK(DatagramSocket sendSocket, DatagramPacket ackP, short blockNum)
  249. {
  250. byte[] buf;
  251. short shortVal = OP_DAT;
  252. ByteBuffer wrap = ByteBuffer.wrap(buf);
  253. short opcode = wrap.putShort(shortVal);
  254.  
  255.  
  256. return true;}
  257.  
  258. private void send_ERR(DatagramSocket socket, int opcode, String msg)
  259. {}
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment