Reformat code
This commit is contained in:
parent
630e76198e
commit
bdffb7a457
@ -5,31 +5,33 @@ import java.security.MessageDigest;
|
||||
|
||||
|
||||
public class CheckSum {
|
||||
static String getChecksum(String fileName) {
|
||||
StringBuffer sb = new StringBuffer("");
|
||||
try {
|
||||
String datafile = fileName;
|
||||
//use SHA1 to calculate checksum
|
||||
MessageDigest md = MessageDigest.getInstance("SHA1");
|
||||
FileInputStream fis = new FileInputStream(datafile);
|
||||
byte[] dataBytes = new byte[1024];
|
||||
static String getChecksum(String fileName) {
|
||||
StringBuffer sb = new StringBuffer("");
|
||||
try {
|
||||
String datafile = fileName;
|
||||
//use SHA1 to calculate checksum
|
||||
MessageDigest md = MessageDigest.getInstance("SHA1");
|
||||
FileInputStream fis = new FileInputStream(datafile);
|
||||
byte[] dataBytes = new byte[1024];
|
||||
|
||||
int nread = 0;
|
||||
int nread = 0;
|
||||
|
||||
while ((nread = fis.read(dataBytes)) != -1) {
|
||||
md.update(dataBytes, 0, nread);
|
||||
}
|
||||
fis.close();
|
||||
while ((nread = fis.read(dataBytes)) != -1) {
|
||||
md.update(dataBytes, 0, nread);
|
||||
}
|
||||
fis.close();
|
||||
|
||||
byte[] mdbytes = md.digest();
|
||||
byte[] mdbytes = md.digest();
|
||||
|
||||
// convert the byte to hex format
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
// convert the byte to hex format
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
|
||||
} catch (Exception e){System.out.println("Generate Checksum Failed: "+e.getMessage());}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Generate Checksum Failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,12 @@ package ru.redguy.tftpserver;
|
||||
|
||||
public interface ErrorEvent {
|
||||
public void onPacketReceiveException(Exception exception);
|
||||
|
||||
public void onPacketReadException(Exception exception);
|
||||
|
||||
public void onPacketWriteException(Exception exception);
|
||||
|
||||
public void onClientReadException(Exception exception, TFTPread tftPread);
|
||||
|
||||
public void onClientWriteException(Exception exception, TFTPwrite tftPwrite);
|
||||
}
|
||||
|
@ -1,87 +1,88 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class TFTPServer {
|
||||
|
||||
private DatagramSocket socket;
|
||||
private Runner runner;
|
||||
private Thread thread;
|
||||
private ErrorEvent errorEvent;
|
||||
private DatagramSocket socket;
|
||||
private Runner runner;
|
||||
private Thread thread;
|
||||
private ErrorEvent errorEvent;
|
||||
|
||||
public void start() throws SocketException {
|
||||
start(69);
|
||||
}
|
||||
public void start() throws SocketException {
|
||||
start(69);
|
||||
}
|
||||
|
||||
public void start(int port) throws SocketException {
|
||||
socket = new DatagramSocket(port);
|
||||
runner = new Runner(socket,this);
|
||||
thread = new Thread(runner);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
public void start(int port) throws SocketException {
|
||||
socket = new DatagramSocket(port);
|
||||
runner = new Runner(socket, this);
|
||||
thread = new Thread(runner);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void start(String host, int port) throws UnknownHostException, SocketException {
|
||||
socket = new DatagramSocket(port,InetAddress.getByName(host));
|
||||
runner = new Runner(socket,this);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
public void start(String host, int port) throws UnknownHostException, SocketException {
|
||||
socket = new DatagramSocket(port, InetAddress.getByName(host));
|
||||
runner = new Runner(socket, this);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return socket.getLocalPort();
|
||||
}
|
||||
public int getPort() {
|
||||
return socket.getLocalPort();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
thread.interrupt();
|
||||
}
|
||||
public void stop() {
|
||||
thread.interrupt();
|
||||
}
|
||||
|
||||
public void onError(ErrorEvent event) {
|
||||
this.errorEvent = event;
|
||||
}
|
||||
public void onError(ErrorEvent event) {
|
||||
this.errorEvent = event;
|
||||
}
|
||||
|
||||
static class Runner implements Runnable {
|
||||
static class Runner implements Runnable {
|
||||
|
||||
DatagramSocket datagramSocket;
|
||||
TFTPServer server;
|
||||
boolean run = true;
|
||||
DatagramSocket datagramSocket;
|
||||
TFTPServer server;
|
||||
boolean run = true;
|
||||
|
||||
public Runner(DatagramSocket socket,TFTPServer server) {
|
||||
this.datagramSocket = socket;
|
||||
this.server = server;
|
||||
}
|
||||
public Runner(DatagramSocket socket, TFTPServer server) {
|
||||
this.datagramSocket = socket;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
}
|
||||
public void stop() {
|
||||
run = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (run) {
|
||||
TFTPpacket in = null;
|
||||
try {
|
||||
in = TFTPpacket.receive(datagramSocket);
|
||||
} catch (IOException e) {
|
||||
server.errorEvent.onPacketReceiveException(e);
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
while (run) {
|
||||
TFTPpacket in = null;
|
||||
try {
|
||||
in = TFTPpacket.receive(datagramSocket);
|
||||
} catch (IOException e) {
|
||||
server.errorEvent.onPacketReceiveException(e);
|
||||
}
|
||||
|
||||
if (in instanceof TFTPread) {
|
||||
try {
|
||||
TFTPserverRRQ r = new TFTPserverRRQ((TFTPread) in, server.errorEvent);
|
||||
} catch (TftpException e) {
|
||||
server.errorEvent.onPacketReadException(e);
|
||||
}
|
||||
}
|
||||
|
||||
else if (in instanceof TFTPwrite) {
|
||||
try {
|
||||
TFTPserverWRQ w = new TFTPserverWRQ((TFTPwrite) in, server.errorEvent);
|
||||
} catch (TftpException e) {
|
||||
server.errorEvent.onPacketWriteException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (in instanceof TFTPread) {
|
||||
try {
|
||||
TFTPserverRRQ r = new TFTPserverRRQ((TFTPread) in, server.errorEvent);
|
||||
} catch (TftpException e) {
|
||||
server.errorEvent.onPacketReadException(e);
|
||||
}
|
||||
} else if (in instanceof TFTPwrite) {
|
||||
try {
|
||||
TFTPserverWRQ w = new TFTPserverWRQ((TFTPwrite) in, server.errorEvent);
|
||||
} catch (TftpException e) {
|
||||
server.errorEvent.onPacketWriteException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,132 +1,138 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
class TftpException extends Exception {
|
||||
public TftpException() {
|
||||
super();
|
||||
}
|
||||
public TftpException(String s) {
|
||||
super(s);
|
||||
}
|
||||
public TftpException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TftpException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//GENERAL packet: define the packet structure, necessary members and methods//
|
||||
//of TFTP packet. To be extended by other specific packet(read, write, etc) //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
public class TFTPpacket {
|
||||
|
||||
// TFTP constants
|
||||
public static int tftpPort = 69;
|
||||
public static int maxTftpPakLen=516;
|
||||
public static int maxTftpData=512;
|
||||
// TFTP constants
|
||||
public static int tftpPort = 69;
|
||||
public static int maxTftpPakLen = 516;
|
||||
public static int maxTftpData = 512;
|
||||
|
||||
// Tftp opcodes
|
||||
protected static final short tftpRRQ=1;
|
||||
protected static final short tftpWRQ=2;
|
||||
protected static final short tftpDATA=3;
|
||||
protected static final short tftpACK=4;
|
||||
protected static final short tftpERROR=5;
|
||||
// Tftp opcodes
|
||||
protected static final short tftpRRQ = 1;
|
||||
protected static final short tftpWRQ = 2;
|
||||
protected static final short tftpDATA = 3;
|
||||
protected static final short tftpACK = 4;
|
||||
protected static final short tftpERROR = 5;
|
||||
|
||||
// Packet Offsets
|
||||
protected static final int opOffset=0;
|
||||
// Packet Offsets
|
||||
protected static final int opOffset = 0;
|
||||
|
||||
protected static final int fileOffset=2;
|
||||
protected static final int fileOffset = 2;
|
||||
|
||||
protected static final int blkOffset=2;
|
||||
protected static final int dataOffset=4;
|
||||
protected static final int blkOffset = 2;
|
||||
protected static final int dataOffset = 4;
|
||||
|
||||
protected static final int numOffset=2;
|
||||
protected static final int msgOffset=4;
|
||||
protected static final int numOffset = 2;
|
||||
protected static final int msgOffset = 4;
|
||||
|
||||
// The actual packet for UDP transfer
|
||||
protected byte [] message;
|
||||
protected int length;
|
||||
// The actual packet for UDP transfer
|
||||
protected byte[] message;
|
||||
protected int length;
|
||||
|
||||
// Address info (required for replies)
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
// Address info (required for replies)
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
|
||||
// Constructor
|
||||
public TFTPpacket() {
|
||||
message=new byte[maxTftpPakLen];
|
||||
length=maxTftpPakLen;
|
||||
}
|
||||
|
||||
// Methods to receive packet and convert it to yhe right type(data/ack/read/...)
|
||||
public static TFTPpacket receive(DatagramSocket sock) throws IOException {
|
||||
TFTPpacket in=new TFTPpacket(), retPak=new TFTPpacket();
|
||||
//receive data and put them into in.message
|
||||
DatagramPacket inPak = new DatagramPacket(in.message,in.length);
|
||||
sock.receive(inPak);
|
||||
|
||||
//Check the opcode in message, then cast the message into the corresponding type
|
||||
switch (in.get(0)) {
|
||||
case tftpRRQ:
|
||||
retPak=new TFTPread();
|
||||
break;
|
||||
case tftpWRQ:
|
||||
retPak=new TFTPwrite();
|
||||
break;
|
||||
case tftpDATA:
|
||||
retPak=new TFTPdata();
|
||||
break;
|
||||
case tftpACK:
|
||||
retPak=new TFTPack();
|
||||
break;
|
||||
case tftpERROR:
|
||||
retPak=new TFTPerror();
|
||||
break;
|
||||
// Constructor
|
||||
public TFTPpacket() {
|
||||
message = new byte[maxTftpPakLen];
|
||||
length = maxTftpPakLen;
|
||||
}
|
||||
retPak.message=in.message;
|
||||
retPak.length=inPak.getLength();
|
||||
retPak.host=inPak.getAddress();
|
||||
retPak.port=inPak.getPort();
|
||||
|
||||
return retPak;
|
||||
}
|
||||
|
||||
//Method to send packet
|
||||
public void send(InetAddress ip, int port, DatagramSocket s) throws IOException {
|
||||
s.send(new DatagramPacket(message,length,ip,port));
|
||||
}
|
||||
// Methods to receive packet and convert it to yhe right type(data/ack/read/...)
|
||||
public static TFTPpacket receive(DatagramSocket sock) throws IOException {
|
||||
TFTPpacket in = new TFTPpacket(), retPak = new TFTPpacket();
|
||||
//receive data and put them into in.message
|
||||
DatagramPacket inPak = new DatagramPacket(in.message, in.length);
|
||||
sock.receive(inPak);
|
||||
|
||||
// DatagramPacket like methods
|
||||
public InetAddress getAddress() {
|
||||
return host;
|
||||
}
|
||||
//Check the opcode in message, then cast the message into the corresponding type
|
||||
switch (in.get(0)) {
|
||||
case tftpRRQ:
|
||||
retPak = new TFTPread();
|
||||
break;
|
||||
case tftpWRQ:
|
||||
retPak = new TFTPwrite();
|
||||
break;
|
||||
case tftpDATA:
|
||||
retPak = new TFTPdata();
|
||||
break;
|
||||
case tftpACK:
|
||||
retPak = new TFTPack();
|
||||
break;
|
||||
case tftpERROR:
|
||||
retPak = new TFTPerror();
|
||||
break;
|
||||
}
|
||||
retPak.message = in.message;
|
||||
retPak.length = inPak.getLength();
|
||||
retPak.host = inPak.getAddress();
|
||||
retPak.port = inPak.getPort();
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
return retPak;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
//Method to send packet
|
||||
public void send(InetAddress ip, int port, DatagramSocket s) throws IOException {
|
||||
s.send(new DatagramPacket(message, length, ip, port));
|
||||
}
|
||||
|
||||
// Methods to put opcode, blkNum, error code into the byte array 'message'.
|
||||
protected void put(int at, short value) {
|
||||
message[at++] = (byte)(value >>> 8); // first byte
|
||||
message[at] = (byte)(value % 256); // last byte
|
||||
}
|
||||
// DatagramPacket like methods
|
||||
public InetAddress getAddress() {
|
||||
return host;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
//Put the filename and mode into the 'message' at 'at' follow by byte "del"
|
||||
protected void put(int at, String value, byte del) {
|
||||
value.getBytes(0, value.length(), message, at);
|
||||
message[at + value.length()] = del;
|
||||
}
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
protected int get(int at) {
|
||||
return (message[at] & 0xff) << 8 | message[at+1] & 0xff;
|
||||
}
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
protected String get (int at, byte del) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (message[at] != del) result.append((char)message[at++]);
|
||||
return result.toString();
|
||||
}
|
||||
// Methods to put opcode, blkNum, error code into the byte array 'message'.
|
||||
protected void put(int at, short value) {
|
||||
message[at++] = (byte) (value >>> 8); // first byte
|
||||
message[at] = (byte) (value % 256); // last byte
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
//Put the filename and mode into the 'message' at 'at' follow by byte "del"
|
||||
protected void put(int at, String value, byte del) {
|
||||
value.getBytes(0, value.length(), message, at);
|
||||
message[at + value.length()] = del;
|
||||
}
|
||||
|
||||
protected int get(int at) {
|
||||
return (message[at] & 0xff) << 8 | message[at + 1] & 0xff;
|
||||
}
|
||||
|
||||
protected String get(int at, byte del) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (message[at] != del) result.append((char) message[at++]);
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
@ -135,35 +141,37 @@ public class TFTPpacket {
|
||||
////////////////////////////////////////////////////////
|
||||
final class TFTPdata extends TFTPpacket {
|
||||
|
||||
// Constructors
|
||||
protected TFTPdata() {}
|
||||
public TFTPdata(int blockNumber, FileInputStream in) throws IOException {
|
||||
this.message = new byte[maxTftpPakLen];
|
||||
// manipulate message
|
||||
this.put(opOffset, tftpDATA);
|
||||
this.put(blkOffset, (short) blockNumber);
|
||||
// read the file into packet and calculate the entire length
|
||||
length = in.read(message, dataOffset, maxTftpData) + 4;
|
||||
}
|
||||
// Constructors
|
||||
protected TFTPdata() {
|
||||
}
|
||||
|
||||
// Accessors
|
||||
public TFTPdata(int blockNumber, FileInputStream in) throws IOException {
|
||||
this.message = new byte[maxTftpPakLen];
|
||||
// manipulate message
|
||||
this.put(opOffset, tftpDATA);
|
||||
this.put(blkOffset, (short) blockNumber);
|
||||
// read the file into packet and calculate the entire length
|
||||
length = in.read(message, dataOffset, maxTftpData) + 4;
|
||||
}
|
||||
|
||||
public int blockNumber() {
|
||||
return this.get(blkOffset);
|
||||
}
|
||||
// Accessors
|
||||
|
||||
/*
|
||||
* public void data(byte[] buffer) { buffer = new byte[length-4];
|
||||
*
|
||||
* for (int i=0; i<length-4; i++) buffer[i]=message[i+dataOffset]; }
|
||||
*/
|
||||
|
||||
// File output
|
||||
public int write(FileOutputStream out) throws IOException {
|
||||
out.write(message, dataOffset, length - 4);
|
||||
public int blockNumber() {
|
||||
return this.get(blkOffset);
|
||||
}
|
||||
|
||||
return (length - 4);
|
||||
}
|
||||
/*
|
||||
* public void data(byte[] buffer) { buffer = new byte[length-4];
|
||||
*
|
||||
* for (int i=0; i<length-4; i++) buffer[i]=message[i+dataOffset]; }
|
||||
*/
|
||||
|
||||
// File output
|
||||
public int write(FileOutputStream out) throws IOException {
|
||||
out.write(message, dataOffset, length - 4);
|
||||
|
||||
return (length - 4);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
@ -172,25 +180,27 @@ final class TFTPdata extends TFTPpacket {
|
||||
/////////////////////////////////////////////////////////
|
||||
class TFTPerror extends TFTPpacket {
|
||||
|
||||
// Constructors
|
||||
protected TFTPerror() {
|
||||
}
|
||||
//Generate error packet
|
||||
public TFTPerror(int number, String message) {
|
||||
length = 4 + message.length() + 1;
|
||||
this.message = new byte[length];
|
||||
put(opOffset, tftpERROR);
|
||||
put(numOffset, (short) number);
|
||||
put(msgOffset, message, (byte) 0);
|
||||
}
|
||||
// Constructors
|
||||
protected TFTPerror() {
|
||||
}
|
||||
|
||||
// Accessors
|
||||
public int number() {
|
||||
return this.get(numOffset);
|
||||
}
|
||||
public String message() {
|
||||
return this.get(msgOffset, (byte) 0);
|
||||
}
|
||||
//Generate error packet
|
||||
public TFTPerror(int number, String message) {
|
||||
length = 4 + message.length() + 1;
|
||||
this.message = new byte[length];
|
||||
put(opOffset, tftpERROR);
|
||||
put(numOffset, (short) number);
|
||||
put(msgOffset, message, (byte) 0);
|
||||
}
|
||||
|
||||
// Accessors
|
||||
public int number() {
|
||||
return this.get(numOffset);
|
||||
}
|
||||
|
||||
public String message() {
|
||||
return this.get(msgOffset, (byte) 0);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
@ -199,21 +209,22 @@ class TFTPerror extends TFTPpacket {
|
||||
/////////////////////////////////////////////////////////
|
||||
final class TFTPack extends TFTPpacket {
|
||||
|
||||
// Constructors
|
||||
protected TFTPack() {
|
||||
}
|
||||
//Generate ack packet
|
||||
public TFTPack(int blockNumber) {
|
||||
length = 4;
|
||||
this.message = new byte[length];
|
||||
put(opOffset, tftpACK);
|
||||
put(blkOffset, (short) blockNumber);
|
||||
}
|
||||
// Constructors
|
||||
protected TFTPack() {
|
||||
}
|
||||
|
||||
// Accessors
|
||||
public int blockNumber() {
|
||||
return this.get(blkOffset);
|
||||
}
|
||||
//Generate ack packet
|
||||
public TFTPack(int blockNumber) {
|
||||
length = 4;
|
||||
this.message = new byte[length];
|
||||
put(opOffset, tftpACK);
|
||||
put(blkOffset, (short) blockNumber);
|
||||
}
|
||||
|
||||
// Accessors
|
||||
public int blockNumber() {
|
||||
return this.get(blkOffset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,97 +1,105 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
|
||||
class TFTPserverRRQ extends Thread {
|
||||
|
||||
protected DatagramSocket sock;
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
protected FileInputStream source;
|
||||
protected TFTPpacket req;
|
||||
protected int timeoutLimit=5;
|
||||
protected String fileName;
|
||||
protected DatagramSocket sock;
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
protected FileInputStream source;
|
||||
protected TFTPpacket req;
|
||||
protected int timeoutLimit = 5;
|
||||
protected String fileName;
|
||||
|
||||
// initialize read request
|
||||
public TFTPserverRRQ(TFTPread request, ErrorEvent event) throws TftpException {
|
||||
try {
|
||||
req = request;
|
||||
//open new socket with random port num for tranfer
|
||||
sock = new DatagramSocket();
|
||||
sock.setSoTimeout(1000);
|
||||
fileName = request.fileName();
|
||||
// initialize read request
|
||||
public TFTPserverRRQ(TFTPread request, ErrorEvent event) throws TftpException {
|
||||
try {
|
||||
req = request;
|
||||
//open new socket with random port num for tranfer
|
||||
sock = new DatagramSocket();
|
||||
sock.setSoTimeout(1000);
|
||||
fileName = request.fileName();
|
||||
|
||||
host = request.getAddress();
|
||||
port = request.getPort();
|
||||
|
||||
//create file object in parent folder
|
||||
File srcFile = new File(fileName);
|
||||
/*System.out.println("procce checking");*/
|
||||
//check file
|
||||
if (srcFile.exists() && srcFile.isFile() && srcFile.canRead()) {
|
||||
source = new FileInputStream(srcFile);
|
||||
this.start(); //open new thread for transfer
|
||||
} else
|
||||
throw new TftpException("access violation");
|
||||
host = request.getAddress();
|
||||
port = request.getPort();
|
||||
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage()); // error code 1
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
//create file object in parent folder
|
||||
File srcFile = new File(fileName);
|
||||
/*System.out.println("procce checking");*/
|
||||
//check file
|
||||
if (srcFile.exists() && srcFile.isFile() && srcFile.canRead()) {
|
||||
source = new FileInputStream(srcFile);
|
||||
this.start(); //open new thread for transfer
|
||||
} else
|
||||
throw new TftpException("access violation");
|
||||
|
||||
event.onClientReadException(e,request);
|
||||
}
|
||||
}
|
||||
//everything is fine, open new thread to transfer file
|
||||
public void run() {
|
||||
int bytesRead = TFTPpacket.maxTftpPakLen;
|
||||
// handle read request
|
||||
if (req instanceof TFTPread) {
|
||||
try {
|
||||
for (int blkNum = 1; bytesRead == TFTPpacket.maxTftpPakLen; blkNum++) {
|
||||
TFTPdata outPak = new TFTPdata(blkNum, source);
|
||||
/*System.out.println("send block no. " + outPak.blockNumber()); */
|
||||
bytesRead = outPak.getLength();
|
||||
/*System.out.println("bytes sent: " + bytesRead);*/
|
||||
outPak.send(host, port, sock);
|
||||
/*System.out.println("current op code " + outPak.get(0)); */
|
||||
|
||||
//wait for the correct ack. if incorrect, retry up to 5 times
|
||||
while (timeoutLimit!=0) {
|
||||
try {
|
||||
TFTPpacket ack = TFTPpacket.receive(sock);
|
||||
if (!(ack instanceof TFTPack)){throw new Exception("Client failed");}
|
||||
TFTPack a = (TFTPack) ack;
|
||||
|
||||
if(a.blockNumber()!=blkNum){ //check ack
|
||||
throw new SocketTimeoutException("last packet lost, resend packet");}
|
||||
/*System.out.println("confirm blk num " + a.blockNumber()+" from "+a.getPort());*/
|
||||
break;
|
||||
}
|
||||
catch (SocketTimeoutException t) {//resend last packet
|
||||
System.out.println("Resent blk " + blkNum);
|
||||
timeoutLimit--;
|
||||
outPak.send(host, port, sock);
|
||||
}
|
||||
} // end of while
|
||||
if(timeoutLimit==0){throw new Exception("connection failed");}
|
||||
}
|
||||
System.out.println("Transfer completed.(Client " +host +")" );
|
||||
System.out.println("Filename: "+fileName + "\nSHA1 checksum: "+CheckSum.getChecksum(fileName)+"\n");
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage()); // error code 1
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
event.onClientReadException(e, request);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Client failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
//everything is fine, open new thread to transfer file
|
||||
public void run() {
|
||||
int bytesRead = TFTPpacket.maxTftpPakLen;
|
||||
// handle read request
|
||||
if (req instanceof TFTPread) {
|
||||
try {
|
||||
for (int blkNum = 1; bytesRead == TFTPpacket.maxTftpPakLen; blkNum++) {
|
||||
TFTPdata outPak = new TFTPdata(blkNum, source);
|
||||
/*System.out.println("send block no. " + outPak.blockNumber()); */
|
||||
bytesRead = outPak.getLength();
|
||||
/*System.out.println("bytes sent: " + bytesRead);*/
|
||||
outPak.send(host, port, sock);
|
||||
/*System.out.println("current op code " + outPak.get(0)); */
|
||||
|
||||
//wait for the correct ack. if incorrect, retry up to 5 times
|
||||
while (timeoutLimit != 0) {
|
||||
try {
|
||||
TFTPpacket ack = TFTPpacket.receive(sock);
|
||||
if (!(ack instanceof TFTPack)) {
|
||||
throw new Exception("Client failed");
|
||||
}
|
||||
TFTPack a = (TFTPack) ack;
|
||||
|
||||
if (a.blockNumber() != blkNum) { //check ack
|
||||
throw new SocketTimeoutException("last packet lost, resend packet");
|
||||
}
|
||||
/*System.out.println("confirm blk num " + a.blockNumber()+" from "+a.getPort());*/
|
||||
break;
|
||||
} catch (SocketTimeoutException t) {//resend last packet
|
||||
System.out.println("Resent blk " + blkNum);
|
||||
timeoutLimit--;
|
||||
outPak.send(host, port, sock);
|
||||
}
|
||||
} // end of while
|
||||
if (timeoutLimit == 0) {
|
||||
throw new Exception("connection failed");
|
||||
}
|
||||
}
|
||||
System.out.println("Transfer completed.(Client " + host + ")");
|
||||
System.out.println("Filename: " + fileName + "\nSHA1 checksum: " + CheckSum.getChecksum(fileName) + "\n");
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
System.out.println("Client failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,106 +1,111 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
|
||||
class TFTPserverWRQ extends Thread {
|
||||
|
||||
protected DatagramSocket sock;
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
protected FileOutputStream outFile;
|
||||
protected TFTPpacket req;
|
||||
protected int timeoutLimit = 5;
|
||||
//protected int testloss=0;
|
||||
protected File saveFile;
|
||||
protected String fileName;
|
||||
protected DatagramSocket sock;
|
||||
protected InetAddress host;
|
||||
protected int port;
|
||||
protected FileOutputStream outFile;
|
||||
protected TFTPpacket req;
|
||||
protected int timeoutLimit = 5;
|
||||
//protected int testloss=0;
|
||||
protected File saveFile;
|
||||
protected String fileName;
|
||||
|
||||
// Initialize read request
|
||||
public TFTPserverWRQ(TFTPwrite request, ErrorEvent event) throws TftpException {
|
||||
try {
|
||||
req = request;
|
||||
sock = new DatagramSocket(); // new port for transfer
|
||||
sock.setSoTimeout(1000);
|
||||
// Initialize read request
|
||||
public TFTPserverWRQ(TFTPwrite request, ErrorEvent event) throws TftpException {
|
||||
try {
|
||||
req = request;
|
||||
sock = new DatagramSocket(); // new port for transfer
|
||||
sock.setSoTimeout(1000);
|
||||
|
||||
host = request.getAddress();
|
||||
port = request.getPort();
|
||||
fileName = request.fileName();
|
||||
//create file object in parent folder
|
||||
saveFile = new File(fileName);
|
||||
host = request.getAddress();
|
||||
port = request.getPort();
|
||||
fileName = request.fileName();
|
||||
//create file object in parent folder
|
||||
saveFile = new File(fileName);
|
||||
|
||||
if (!saveFile.exists()) {
|
||||
outFile = new FileOutputStream(saveFile);
|
||||
TFTPack a = new TFTPack(0);
|
||||
a.send(host, port, sock); // send ack 0 at first, ready to
|
||||
// receive
|
||||
this.start();
|
||||
} else
|
||||
throw new TftpException("access violation, file exists");
|
||||
if (!saveFile.exists()) {
|
||||
outFile = new FileOutputStream(saveFile);
|
||||
TFTPack a = new TFTPack(0);
|
||||
a.send(host, port, sock); // send ack 0 at first, ready to
|
||||
// receive
|
||||
this.start();
|
||||
} else
|
||||
throw new TftpException("access violation, file exists");
|
||||
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage()); // error code 1
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage()); // error code 1
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
event.onClientWriteException(e,request);
|
||||
}
|
||||
}
|
||||
event.onClientWriteException(e, request);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
/*int bytesRead = TFTPpacket.maxTftpPakLen;*/
|
||||
// handle write request
|
||||
if (req instanceof TFTPwrite) {
|
||||
try {
|
||||
for (int blkNum = 1, bytesOut = 512; bytesOut == 512; blkNum++) {
|
||||
while (timeoutLimit != 0) {
|
||||
try {
|
||||
TFTPpacket inPak = TFTPpacket.receive(sock);
|
||||
//check packet type
|
||||
if (inPak instanceof TFTPerror) {
|
||||
TFTPerror p = (TFTPerror) inPak;
|
||||
throw new TftpException(p.message());
|
||||
} else if (inPak instanceof TFTPdata) {
|
||||
TFTPdata p = (TFTPdata) inPak;
|
||||
/*System.out.println("incoming data " + p.blockNumber());*/
|
||||
// check blk num
|
||||
if (/*testloss==20||*/p.blockNumber() != blkNum) { //expect to be the same
|
||||
//System.out.println("loss. testloss="+testloss+"timeoutLimit="+timeoutLimit);
|
||||
//testloss++;
|
||||
throw new SocketTimeoutException();
|
||||
}
|
||||
//write to the file and send ack
|
||||
bytesOut = p.write(outFile);
|
||||
TFTPack a = new TFTPack(blkNum);
|
||||
a.send(host, port, sock);
|
||||
//testloss++;
|
||||
break;
|
||||
}
|
||||
} catch (SocketTimeoutException t2) {
|
||||
System.out.println("Time out, resend ack");
|
||||
TFTPack a = new TFTPack(blkNum - 1);
|
||||
a.send(host, port, sock);
|
||||
timeoutLimit--;
|
||||
}
|
||||
}
|
||||
if(timeoutLimit==0){throw new Exception("Connection failed");}
|
||||
}
|
||||
outFile.close();
|
||||
System.out.println("Transfer completed.(Client " +host +")" );
|
||||
System.out.println("Filename: "+fileName + "\nSHA1 checksum: "+CheckSum.getChecksum(fileName)+"\n");
|
||||
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
public void run() {
|
||||
/*int bytesRead = TFTPpacket.maxTftpPakLen;*/
|
||||
// handle write request
|
||||
if (req instanceof TFTPwrite) {
|
||||
try {
|
||||
for (int blkNum = 1, bytesOut = 512; bytesOut == 512; blkNum++) {
|
||||
while (timeoutLimit != 0) {
|
||||
try {
|
||||
TFTPpacket inPak = TFTPpacket.receive(sock);
|
||||
//check packet type
|
||||
if (inPak instanceof TFTPerror) {
|
||||
TFTPerror p = (TFTPerror) inPak;
|
||||
throw new TftpException(p.message());
|
||||
} else if (inPak instanceof TFTPdata) {
|
||||
TFTPdata p = (TFTPdata) inPak;
|
||||
/*System.out.println("incoming data " + p.blockNumber());*/
|
||||
// check blk num
|
||||
if (/*testloss==20||*/p.blockNumber() != blkNum) { //expect to be the same
|
||||
//System.out.println("loss. testloss="+testloss+"timeoutLimit="+timeoutLimit);
|
||||
//testloss++;
|
||||
throw new SocketTimeoutException();
|
||||
}
|
||||
//write to the file and send ack
|
||||
bytesOut = p.write(outFile);
|
||||
TFTPack a = new TFTPack(blkNum);
|
||||
a.send(host, port, sock);
|
||||
//testloss++;
|
||||
break;
|
||||
}
|
||||
} catch (SocketTimeoutException t2) {
|
||||
System.out.println("Time out, resend ack");
|
||||
TFTPack a = new TFTPack(blkNum - 1);
|
||||
a.send(host, port, sock);
|
||||
timeoutLimit--;
|
||||
}
|
||||
}
|
||||
if (timeoutLimit == 0) {
|
||||
throw new Exception("Connection failed");
|
||||
}
|
||||
}
|
||||
outFile.close();
|
||||
System.out.println("Transfer completed.(Client " + host + ")");
|
||||
System.out.println("Filename: " + fileName + "\nSHA1 checksum: " + CheckSum.getChecksum(fileName) + "\n");
|
||||
|
||||
System.out.println("Client failed: " + e.getMessage());
|
||||
saveFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
try {
|
||||
ePak.send(host, port, sock);
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
System.out.println("Client failed: " + e.getMessage());
|
||||
saveFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package ru.redguy.tftpserver.datasource;
|
||||
|
||||
import ru.redguy.tftpserver.IDataSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package ru.redguy.tftpserver;
|
||||
package ru.redguy.tftpserver.datasource;
|
||||
|
||||
public interface IDataSource {
|
||||
public boolean isFileExists(String localPath);
|
Reference in New Issue
Block a user