Advertisement
kiah

Untitled

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