Beispiel #1
0
 /**
  * Gets the packet.
  *
  * @return the packet
  * @throws InterruptedIOException the interrupted io exception
  * @throws SocketException the socket exception
  * @throws IOException Signals that an I/O exception has occurred.
  * @throws TFTPPacketException the tFTP packet exception
  */
 private TFTPPacket getPacket()
     throws InterruptedIOException, SocketException, IOException, TFTPPacketException {
   long maxTime = System.currentTimeMillis() + receiveTimeout * 1000;
   while (true) {
     TFTPPacket packet = tftp.bufferedReceive();
     if (packet.getAddress().equals(requestPacket.getAddress())
         && packet.getPort() == requestPacket.getPort()) {
       return packet;
     }
     if (System.currentTimeMillis() > maxTime) {
       throw new SocketTimeoutException();
     }
   }
 }
Beispiel #2
0
 /**
  * Match.
  *
  * @param packet the packet
  * @return true, if successful
  */
 public boolean match(TFTPWriteRequestPacket packet) {
   if (this.status != Status.PREPARED) {
     return false;
   }
   try {
     NetworkAddress address = NetworkAddress.getNetworkAddress(packet.getAddress(), 0);
     if (!address.equals(source)) {
       return false;
     }
   } catch (UnknownHostException e) {
     return false;
   }
   if (packet.getFilename() == null) {
     return false;
   }
   String file1 = fileName.replaceFirst("^/", "");
   String file2 = packet.getFilename().replaceFirst("^/", "");
   if (!file1.equals(file2)) {
     return false;
   }
   return true;
 }
Beispiel #3
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Thread#run()
  */
 @Override
 public void run() {
   while (requestPacket == null) {
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
       status = Status.ERROR;
       return;
     }
     if (checkTimeout()) {
       return;
     }
   }
   status = Status.RUNNING;
   tftp = new TFTP();
   tftp.beginBufferedOps();
   tftp.setDefaultTimeout(receiveTimeout * 1000);
   try {
     tftp.open();
   } catch (SocketException e) {
     status = Status.ERROR;
     return;
   }
   TFTPPacket packet = requestPacket;
   TFTPAckPacket ackPacket = new TFTPAckPacket(packet.getAddress(), packet.getPort(), 0);
   while (packet instanceof TFTPWriteRequestPacket) {
     try {
       tftp.bufferedSend(ackPacket);
       packet = this.getPacket();
     } catch (Exception e) {
     }
     if (checkTimeout()) {
       return;
     }
   }
   int lastBlock = 0;
   while (packet instanceof TFTPDataPacket) {
     TFTPDataPacket dataPacket = (TFTPDataPacket) packet;
     int block = dataPacket.getBlockNumber();
     if (block > lastBlock || (lastBlock == 65535 && block == 0)) {
       try {
         buffer.write(
             dataPacket.getData(), dataPacket.getDataOffset(), dataPacket.getDataLength());
       } catch (IOException e) {
         status = Status.ERROR;
         return;
       }
     }
     ackPacket = new TFTPAckPacket(requestPacket.getAddress(), requestPacket.getPort(), block);
     try {
       tftp.bufferedSend(ackPacket);
     } catch (IOException e) {
     }
     if (dataPacket.getDataLength() < TFTPDataPacket.MAX_DATA_LENGTH) {
       this.status = Status.FINISHED;
       for (int i = 0; i < 3; i++) {
         try {
           packet = this.getPacket();
           tftp.bufferedSend(ackPacket);
         } catch (Exception e) {
           return;
         }
       }
     }
     try {
       packet = this.getPacket();
     } catch (Exception e) {
     }
     if (checkTimeout()) {
       return;
     }
   }
   status = Status.ERROR;
   tftp.close();
 }