Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.*;
- import java.nio.ByteBuffer;
- public class TFTPServer
- {
- public static final int TFTPPORT = 4970;
- public static final int BUFSIZE = 516;
- public static final String READDIR = "/home/username/read/"; //custom address at your PC
- public static final String WRITEDIR = "/home/username/write/"; //custom address at your PC
- // OP codes
- public static final int OP_RRQ = 1;
- public static final int OP_WRQ = 2;
- public static final int OP_DAT = 3;
- public static final int OP_ACK = 4;
- public static final int OP_ERR = 5;
- public static void main(String[] args) {
- if (args.length > 0)
- {
- System.err.printf("usage: java %s\n", TFTPServer.class.getCanonicalName());
- System.exit(1);
- }
- //Starting the server
- try
- {
- TFTPServer server= new TFTPServer();
- server.start();
- }
- catch (SocketException e)
- {e.printStackTrace();}
- }
- private void start() throws SocketException
- {
- byte[] buf= new byte[BUFSIZE];
- // Create socket
- DatagramSocket socket= new DatagramSocket(null);
- // Create local bind point
- SocketAddress localBindPoint= new InetSocketAddress(TFTPPORT);
- socket.bind(localBindPoint);
- System.out.printf("Listening at port %d for new requests\n", TFTPPORT);
- // Loop to handle client requests
- while (true)
- {
- final InetSocketAddress clientAddress = receiveFrom(socket, buf);
- // If clientAddress is null, an error occurred in receiveFrom()
- if (clientAddress == null)
- continue;
- final StringBuffer requestedFile= new StringBuffer();
- final int reqtype = ParseRQ(buf, requestedFile);
- new Thread()
- {
- public void run()
- {
- try
- {
- DatagramSocket sendSocket= new DatagramSocket(0);
- // Connect to client
- sendSocket.connect(clientAddress);
- System.out.printf("%s request for %s from %s using port %d\n",
- (reqtype == OP_RRQ)?"Read":"Write",
- clientAddress.getHostName(), clientAddress.getPort());
- // Read request
- if (reqtype == OP_RRQ)
- {
- requestedFile.insert(0, READDIR);
- HandleRQ(sendSocket, requestedFile.toString(), OP_RRQ);
- }
- // Write request
- else
- {
- requestedFile.insert(0, WRITEDIR);
- HandleRQ(sendSocket,requestedFile.toString(),OP_WRQ);
- }
- sendSocket.close();
- }
- catch (SocketException e)
- {e.printStackTrace();}
- }
- }.start();
- }
- }
- /**
- * Reads the first block of data, i.e., the request for an action (read or write).
- * @param socket (socket to read from)
- * @param buf (where to store the read data)
- * @return socketAddress (the socket address of the client)
- */
- private InetSocketAddress receiveFrom(DatagramSocket socket, byte[] buf)
- {
- // Create datagram packet
- DatagramPacket receivePacket= new DatagramPacket(buf, buf.length);
- // Receive packet
- try {
- socket.receive(receivePacket);
- }
- catch(IOException e)
- {
- System.out.println("Error: "+e);
- }
- // Get client address and port from the packet
- InetSocketAddress socketAddress= new InetSocketAddress(receivePacket.getAddress(), receivePacket.getPort());
- return socketAddress;
- }
- /**
- * Parses the request in buf to retrieve the type of request and requestedFile
- *
- * @param buf (received request)
- * @param requestedFile (name of file to read/write)
- * @return opcode (request type: RRQ or WRQ)
- */
- private int ParseRQ(byte[] buf, StringBuffer requestedFile)
- {
- // See "TFTP Formats" in TFTP specification for the RRQ/WRQ request contents
- // byte[] buf;
- ByteBuffer wrap= ByteBuffer.wrap(buf);
- short opcode = wrap.getShort();
- String file= requestedFile.toString();
- file=file.substring(2, requestedFile.length());
- String[] parts=file.split("0");
- String fileName=(parts[0]);
- String Mode=parts[1];
- System.out.println("Opcode is: "+ opcode);
- System.out.println("Requested file: "+ fileName);
- System.out.println("Transfer mode is: "+ Mode);
- return opcode;
- }
- /**
- * Handles RRQ and WRQ requests
- *
- * @param sendSocket (socket used to send/receive packets)
- * @param requestedFile (name of file to read/write)
- * @param opcode (RRQ or WRQ)
- */
- private void HandleRQ(DatagramSocket sendSocket, String requestedFile, int opcode)
- {
- if(opcode == OP_RRQ)
- {
- // See "TFTP Formats" in TFTP specification for the DATA and ACK packet contents
- FileInputStream in=null;
- try {
- in = new FileInputStream(requestedFile);
- }
- catch(FileNotFoundException f)
- {
- System.out.println("ERROR, FILE NOT FOUND"+ f);
- send_ERR(sendSocket, OP_ERR);
- }
- try {
- byte[] buf = new byte[512];
- int length = in.read(buf);
- }
- catch(IOException e)
- {
- System.out.println("ERROR READING FILE"+ e);
- }
- boolean result = send_DATA_receive_ACK(DatagramSocket sendSocket, );
- }
- else if (opcode == OP_WRQ)
- {
- boolean result = receive_DATA_send_ACK(params);
- }
- else
- {
- System.err.println("Invalid request. Sending an error packet.");
- // See "TFTP Formats" in TFTP specification for the ERROR packet contents
- send_ERR(params);
- return;
- }
- }
- /**
- To be implemented
- */
- private boolean send_DATA_receive_ACK(DatagramSocket sendSocket, )
- {
- return true;}
- private boolean receive_DATA_send_ACK(params)
- {
- byte[] buf;
- short shortVal = OP_DATA;
- ByteBuffer wrap = ByteBuffer.wrap(buf);
- short opcode = wrap.putShort(shortVal);
- return true;}
- private void send_ERR(DatagramSocket socket, int opcode)
- {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement