-
Notifications
You must be signed in to change notification settings - Fork 612
NMS-16237: add support for RFC2348 "TFTP blksize" to the tftp implementation #8184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dino2gnt
wants to merge
6
commits into
release-35.x
Choose a base branch
from
dcy/nms-16237
base: release-35.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
197b8a8
NMS-16237: add support for RFC2348 "TFTP blksize" to the tftp server …
dino2gnt c235110
Fix test
dino2gnt 17583af
Update features/device-config/tftp/src/main/java/org/opennms/features…
dino2gnt 754040e
formatting
dino2gnt abc6976
Check state of previously opened socket before opening a new socket o…
dino2gnt c413f49
also remove generated filenamesuffix token
dino2gnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,18 +22,21 @@ | |
| package org.opennms.features.deviceconfig.tftp.impl; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.net.DatagramPacket; | ||
| import java.net.DatagramSocket; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketException; | ||
| import java.net.SocketTimeoutException; | ||
| import java.net.UnknownHostException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.commons.net.io.FromNetASCIIOutputStream; | ||
|
|
@@ -60,6 +63,7 @@ | |
| // - received files are not stored on disk but in memory and are handed off to registered listeners | ||
| // - the maximum file size to receive can be configured | ||
| // - requesting files is not supported | ||
| // - added support for RFC2348 blksize | ||
|
|
||
| public class TftpServerImpl implements TftpServer, Runnable, AutoCloseable { | ||
|
|
||
|
|
@@ -92,6 +96,8 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| try { | ||
| int lastBlock = 0; | ||
| final String fileName = twrp.getFilename(); | ||
| int negotiatedBlksize = TFTPDataPacket.MAX_DATA_LENGTH; | ||
| Map<String,String> twrpOptions = twrp.getOptions(); | ||
|
|
||
| try { | ||
| if (twrp.getMode() == TFTP.NETASCII_MODE) { | ||
|
|
@@ -104,9 +110,58 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| .getPort(), TFTPErrorPacket.UNDEFINED, e.getMessage())); | ||
| return; | ||
| } | ||
|
|
||
| TFTPAckPacket lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0); | ||
| sendData(transferTftp_, lastSentAck); // send the data | ||
|
|
||
| // Handle RFC2348 TFTP blksize | ||
| if (twrpOptions.containsKey("blksize")) { | ||
| LOG.debug("Received blksize option from client '{}', attempting negotiation", twrp.getAddress()); | ||
| try { | ||
| negotiatedBlksize = Integer.parseInt(twrpOptions.get("blksize")); | ||
| if (negotiatedBlksize < 8 || negotiatedBlksize > 65464) { | ||
| LOG.error("Received invalid blksize '{}' option from client {}", negotiatedBlksize, twrp.getAddress()); | ||
| transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), | ||
| twrp.getPort(), TFTPErrorPacket.INVALID_OPTIONS_VALUE, | ||
| "Invalid blksize")); | ||
| return; | ||
| } else { | ||
| LOG.debug("Negotiating tftp blksize '{}' with client '{}'", negotiatedBlksize, twrp.getAddress()); | ||
| // Create an OAck response packet - commons doesn't provide a method for this yet. | ||
| DatagramPacket oackPacket = createBlksizeOAckPacket(negotiatedBlksize, twrp.getAddress(), twrp.getPort()); | ||
|
|
||
| try { | ||
| int localPort = transferTftp_.getLocalPort(); | ||
| transferTftp_.close(); | ||
| while (true) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think we should while(true) loops. Let me try this differently.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't feel good about it either. |
||
| if (!transferTftp_.isOpen()) { // wait until we know the socket is not open | ||
| break; | ||
| } | ||
| } | ||
| DatagramSocket socket = new DatagramSocket(localPort); | ||
| socket.send(oackPacket); | ||
| socket.close(); | ||
| while (true) { | ||
| if (socket.isClosed()) { // wait until we know the socket is closed | ||
| break; | ||
| } | ||
| } | ||
| transferTftp_.open(localPort); // every day I'm shufflin' | ||
| } catch (Exception e) { | ||
| LOG.debug("Error sending OACK packet in response to 'blksize={}',", negotiatedBlksize, e); | ||
| return; | ||
| } | ||
| transferTftp_.resetBuffersToSize(negotiatedBlksize); | ||
| LOG.debug("Successfully negotiated blksize '{}' with client '{}'", negotiatedBlksize, twrp.getAddress()); | ||
| } | ||
| } catch (final Exception e) { | ||
| LOG.error("Received invalid blksize '{}' option from client {}:", negotiatedBlksize, twrp.getAddress(), e); | ||
| transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), | ||
| twrp.getPort(), TFTPErrorPacket.INVALID_OPTIONS_VALUE, | ||
| "Invalid blksize")); | ||
| return; | ||
| } | ||
| } else { | ||
| sendData(transferTftp_, lastSentAck); // send the ack | ||
| } | ||
|
|
||
| while (true) { | ||
| // get the response - ensure it is from the right place. | ||
|
|
@@ -134,11 +189,12 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| dataPacket = transferTftp_.bufferedReceive(); | ||
| } catch (final SocketTimeoutException e) { | ||
| statistics.incErrors(); | ||
| LOG.error("did not receive data packet", e); | ||
| LOG.error("did not receive data packet from client '{}': ", twrp.getAddress(), e); | ||
| if (timeoutCount >= maxTimeoutRetries_) { | ||
| throw e; | ||
| } | ||
| // It didn't get our ack. Resend it. | ||
| LOG.debug("Resending missed ack to '{}'", twrp.getAddress()); | ||
| transferTftp_.bufferedSend(lastSentAck); | ||
| timeoutCount++; | ||
| continue; | ||
|
|
@@ -147,6 +203,7 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
|
|
||
| if (dataPacket instanceof TFTPWriteRequestPacket) { | ||
| // it must have missed our initial ack. Send another. | ||
| LOG.debug("Resending WRQ ack to '{}'", twrp.getAddress()); | ||
| lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0); | ||
| transferTftp_.bufferedSend(lastSentAck); | ||
| } else if (dataPacket == null || !(dataPacket instanceof TFTPDataPacket)) { | ||
|
|
@@ -161,6 +218,7 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| final byte[] data = ((TFTPDataPacket) dataPacket).getData(); | ||
| final int dataLength = ((TFTPDataPacket) dataPacket).getDataLength(); | ||
| final int dataOffset = ((TFTPDataPacket) dataPacket).getDataOffset(); | ||
| LOG.debug("Processing block {} from client '{}'", block, twrp.getAddress()); | ||
|
|
||
| if (block > lastBlock || lastBlock == 65535 && block == 0) { | ||
| // it might resend a data block if it missed our ack | ||
|
|
@@ -184,8 +242,9 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| } | ||
|
|
||
| lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), block); | ||
| LOG.debug("Sending Ack for block {} from client '{}'", block, twrp.getAddress()); | ||
| sendData(transferTftp_, lastSentAck); // send the data | ||
| if (dataLength < TFTPDataPacket.MAX_DATA_LENGTH) { | ||
| if (dataLength < negotiatedBlksize) { | ||
| // end of stream signal - The transfer is complete. | ||
| bos.close(); | ||
|
|
||
|
|
@@ -195,7 +254,7 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
|
|
||
| List<TftpFileReceiver> rs; | ||
| synchronized (receivers) { | ||
| rs = new ArrayList(receivers); | ||
| rs = new ArrayList<>(receivers); | ||
| } | ||
| rs.forEach(r -> { | ||
| r.onFileReceived(twrp.getAddress(), fileName, content); | ||
|
|
@@ -209,6 +268,7 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| } catch (final SocketTimeoutException e) { | ||
| // this is the expected route - the client | ||
| // shouldn't be sending any more packets. | ||
| LOG.debug("tftp transfer from client '{}' complete.", twrp.getAddress()); | ||
| break; | ||
| } | ||
|
|
||
|
|
@@ -236,6 +296,7 @@ private void handleWrite(final TFTPWriteRequestPacket twrp) throws IOException, | |
| } | ||
|
|
||
| // all done. | ||
| LOG.debug("Timing out transfer from client '{}'", twrp.getAddress()); | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -266,6 +327,7 @@ public void run() { | |
| .getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, | ||
| "Read not allowed by server.")); | ||
| } else if (tftpPacket_ instanceof TFTPWriteRequestPacket) { | ||
| LOG.debug("Received write request from client '{}'", tftpPacket_.getAddress()); | ||
| handleWrite((TFTPWriteRequestPacket) tftpPacket_); | ||
| } else { | ||
| statistics.incWarnings(); | ||
|
|
@@ -599,4 +661,19 @@ public TftpStatistics getStatistics() { | |
| public TftpStatistics getAndResetStatistics() { | ||
| return statistics.cloneAndReset(); | ||
| } | ||
|
|
||
| private DatagramPacket createBlksizeOAckPacket(int size, InetAddress addr, int port) throws IOException { | ||
| // build the oack packet - commons doesn't provide a method for this yet... | ||
| ByteArrayOutputStream oack = new ByteArrayOutputStream(); | ||
| oack.write(0); | ||
| oack.write(TFTPPacket.OACK); | ||
| oack.write("blksize".getBytes(StandardCharsets.US_ASCII)); | ||
| oack.write(0); | ||
| oack.write(String.valueOf(size).getBytes(StandardCharsets.US_ASCII)); | ||
| oack.write(0); | ||
| //make it a byte array | ||
| byte[] oackData = oack.toByteArray(); | ||
|
|
||
| return new DatagramPacket(oackData, oackData.length, addr, port); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.