List of usage examples for org.apache.commons.net.tftp TFTPDataPacket MAX_DATA_LENGTH
int MAX_DATA_LENGTH
To view the source code for org.apache.commons.net.tftp TFTPDataPacket MAX_DATA_LENGTH.
Click Source Link
From source file:org.jnode.net.ipv4.tftp.TFTPServer.java
private void processRequest(TFTPPacket packet) throws IOException { if (log.isDebugEnabled()) log.debug("Received packet: " + packet.getAddress() + ':' + packet.getPort()); final int type = packet.getType(); switch (type) { case TFTPPacket.WRITE_REQUEST: if (clientAddress == null) { TFTPWriteRequestPacket wreqPacket = (TFTPWriteRequestPacket) packet; File file = new File(".", wreqPacket.getFilename()); log.info("Request to write file " + wreqPacket.getFilename() + " (" + file.getAbsolutePath() + ") received from " + packet.getAddress() + ':' + packet.getPort()); fileOut = new FileOutputStream(file); blockNumber = 0;//from w w w. j av a2 s . c o m bufferedSend(new TFTPAckPacket(packet.getAddress(), packet.getPort(), blockNumber)); clientAddress = packet.getAddress(); clientPort = packet.getPort(); } break; case TFTPPacket.DATA: if (packet.getAddress().equals(clientAddress) && packet.getPort() == clientPort) { TFTPDataPacket dataPacket = (TFTPDataPacket) packet; // if client sent next block if (dataPacket.getBlockNumber() == blockNumber + 1) { fileOut.write(dataPacket.getData(), dataPacket.getDataOffset(), dataPacket.getDataLength()); // send acknowledgement bufferedSend( new TFTPAckPacket(packet.getAddress(), packet.getPort(), dataPacket.getBlockNumber())); blockNumber++; // if last block then end of transfer if (dataPacket.getDataLength() < TFTPDataPacket.MAX_DATA_LENGTH) { clientAddress = null; clientPort = 0; fileOut.close(); } } } break; case TFTPPacket.READ_REQUEST: if (clientAddress == null) { TFTPReadRequestPacket rreqPacket = (TFTPReadRequestPacket) packet; try { File file = new File(".", rreqPacket.getFilename()); log.info("Request to read file " + rreqPacket.getFilename() + " (" + file.getAbsolutePath() + ") received from " + packet.getAddress() + ':' + packet.getPort()); fileIn = new FileInputStream(file); blockNumber = 1; byte[] data = new byte[TFTPDataPacket.MAX_DATA_LENGTH]; final int bytesRead = fileIn.read(data); bufferedSend(new TFTPDataPacket(packet.getAddress(), packet.getPort(), blockNumber, data, 0, bytesRead)); // if more blocks to send if (bytesRead == TFTPDataPacket.MAX_DATA_LENGTH) { clientAddress = packet.getAddress(); clientPort = packet.getPort(); } else { fileIn.close(); } } catch (FileNotFoundException ex) { bufferedSend(new TFTPErrorPacket(packet.getAddress(), packet.getPort(), TFTPErrorPacket.FILE_NOT_FOUND, ex.getMessage())); } } break; case TFTPPacket.ACKNOWLEDGEMENT: if (packet.getAddress().equals(clientAddress) && packet.getPort() == clientPort) { TFTPAckPacket ackPacket = (TFTPAckPacket) packet; // if client acknowledged last block if (ackPacket.getBlockNumber() == blockNumber) { // send next block byte[] data = new byte[TFTPDataPacket.MAX_DATA_LENGTH]; final int bytesRead = fileIn.read(data); blockNumber++; bufferedSend(new TFTPDataPacket(packet.getAddress(), packet.getPort(), blockNumber, data, 0, bytesRead)); // if last block then end of transfer if (bytesRead < TFTPDataPacket.MAX_DATA_LENGTH) { clientAddress = null; clientPort = 0; fileIn.close(); } } } break; } }
From source file:org.mobicents.slee.resource.tftp.TFTPTransfer.java
private void handleRead(TFTPReadRequestPacket trrp) throws Exception { long totalBytesSent = 0; try {//from w ww.ja v a2s .c om if (mode_ == ServerMode.PUT_ONLY) { transferTftp_.bufferedSend(new TFTPErrorPacket(trrp.getAddress(), trrp.getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, "Read not allowed by server.")); return; } if (trc.isFineEnabled()) trc.fine("READ request received, get cracking"); fireEvent(trrp); suspend(0); // TODO: do we really need to wait forever? if (trrp.getMode() == TFTP.NETASCII_MODE) { is_ = new ToNetASCIIInputStream(is_); } byte[] temp = new byte[TFTPDataPacket.MAX_DATA_LENGTH]; TFTPPacket answer; int block = 1; boolean sendNext = true; int readLength = TFTPDataPacket.MAX_DATA_LENGTH; TFTPDataPacket lastSentData = null; // We are reading a file, so when we read less than the // requested bytes, we know that we are at the end of the file. while (readLength == TFTPDataPacket.MAX_DATA_LENGTH && !shutdownTransfer) { if (sendNext) { readLength = is_.read(temp); if (readLength == -1) { readLength = 0; } lastSentData = new TFTPDataPacket(trrp.getAddress(), trrp.getPort(), block, temp, 0, readLength); transferTftp_.bufferedSend(lastSentData); totalBytesSent += readLength; } answer = null; int timeoutCount = 0; while (!shutdownTransfer && (answer == null || !answer.getAddress().equals(trrp.getAddress()) || answer.getPort() != trrp.getPort())) { // listen for an answer. if (answer != null) { // The answer that we got didn't come from the // expected source, fire back an error, and continue // listening. trc.warning("TFTP Server ignoring message from unexpected source."); transferTftp_.bufferedSend(new TFTPErrorPacket(answer.getAddress(), answer.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); } try { answer = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { if (timeoutCount >= maxTimeoutRetries_) { throw e; } // didn't get an ack for this data. need to resend // it. timeoutCount++; transferTftp_.bufferedSend(lastSentData); continue; } } if (answer == null || !(answer instanceof TFTPAckPacket)) { if (!shutdownTransfer) { trc.severe("Unexpected response from tftp client during transfer (" + answer + "). Transfer aborted."); } break; } else { // once we get here, we know we have an answer packet // from the correct host. TFTPAckPacket ack = (TFTPAckPacket) answer; if (ack.getBlockNumber() != block) { /* * The origional tftp spec would have called on us to resend the * previous data here, however, that causes the SAS Syndrome. * http://www.faqs.org/rfcs/rfc1123.html section 4.2.3.1 The modified * spec says that we ignore a duplicate ack. If the packet was really * lost, we will time out on receive, and resend the previous data at * that point. */ sendNext = false; } else { // send the next block block++; if (block > 65535) { // wrap the block number block = 0; } sendNext = true; } } } } finally { if (trc.isFineEnabled()) trc.fine("Bytes sent = " + totalBytesSent); try { if (is_ != null) is_.close(); if (sbbOs != null) sbbOs.close(); } catch (IOException e) { // noop } } }
From source file:org.mobicents.slee.resource.tftp.TFTPTransfer.java
private void handleWrite(TFTPWriteRequestPacket twrp) throws Exception { long totalBytesReceived = 0; try {/* w w w . ja va 2 s .c o m*/ if (mode_ == ServerMode.GET_ONLY) { transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), twrp.getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, "Write not allowed by server.")); return; } if (trc.isFineEnabled()) trc.fine("WRITE request received, get cracking"); fireEvent(twrp); suspend(0); if (twrp.getMode() == TFTP.NETASCII_MODE) { os_ = new FromNetASCIIOutputStream(os_); } TFTPAckPacket lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0); transferTftp_.bufferedSend(lastSentAck); int lastBlock = 0; while (true) { // get the response - ensure it is from the right place. TFTPPacket dataPacket = null; int timeoutCount = 0; while (!shutdownTransfer && (dataPacket == null || !dataPacket.getAddress().equals(twrp.getAddress()) || dataPacket.getPort() != twrp.getPort())) { // listen for an answer. if (dataPacket != null) { // The data that we got didn't come from the // expected source, fire back an error, and continue // listening. trc.warning("TFTP Server ignoring message from unexpected source."); transferTftp_.bufferedSend(new TFTPErrorPacket(dataPacket.getAddress(), dataPacket.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); } try { dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { if (timeoutCount >= maxTimeoutRetries_) { throw e; } // It didn't get our ack. Resend it. transferTftp_.bufferedSend(lastSentAck); timeoutCount++; continue; } } if (dataPacket != null && dataPacket instanceof TFTPWriteRequestPacket) { // it must have missed our initial ack. Send another. lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0); transferTftp_.bufferedSend(lastSentAck); } else if (dataPacket == null || !(dataPacket instanceof TFTPDataPacket)) { if (!shutdownTransfer) { trc.severe("Unexpected response from tftp client during transfer (" + dataPacket + "). Transfer aborted."); } break; } else { int block = ((TFTPDataPacket) dataPacket).getBlockNumber(); byte[] data = ((TFTPDataPacket) dataPacket).getData(); int dataLength = ((TFTPDataPacket) dataPacket).getDataLength(); int dataOffset = ((TFTPDataPacket) dataPacket).getDataOffset(); if (block > lastBlock || (lastBlock == 65535 && block == 0)) { // it might resend a data block if it missed our ack // - don't rewrite the block. os_.write(data, dataOffset, dataLength); lastBlock = block; fireEvent(dataPacket); totalBytesReceived += dataLength; } lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), block); transferTftp_.bufferedSend(lastSentAck); if (dataLength < TFTPDataPacket.MAX_DATA_LENGTH) { // end of stream signal - The tranfer is complete. if (trc.isFineEnabled()) trc.fine("Bytes received = " + totalBytesReceived); os_.close(); // But my ack may be lost - so listen to see if I // need to resend the ack. for (int i = 0; i < maxTimeoutRetries_; i++) { try { dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { // this is the expected route - the client // shouldn't be sending any more packets. break; } if (dataPacket != null && (!dataPacket.getAddress().equals(twrp.getAddress()) || dataPacket.getPort() != twrp.getPort())) { // make sure it was from the right client... transferTftp_.bufferedSend( new TFTPErrorPacket(dataPacket.getAddress(), dataPacket.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); } else { // This means they sent us the last // datapacket again, must have missed our // ack. resend it. transferTftp_.bufferedSend(lastSentAck); } } // all done. break; } } } } finally { if (sbbIs != null) sbbIs.close(); if (os_ != null) os_.close(); } }