Skip to content

[NET-727] Enable accessing options map for TFTP request packet and allow using blksize option#331

Merged
garydgregory merged 6 commits intoapache:masterfrom
vimukthiD:RFC-2348-blksize
Mar 5, 2025
Merged

[NET-727] Enable accessing options map for TFTP request packet and allow using blksize option#331
garydgregory merged 6 commits intoapache:masterfrom
vimukthiD:RFC-2348-blksize

Conversation

@vimukthiD
Copy link
Copy Markdown
Contributor

relates to existing https://issues.apache.org/jira/browse/NET-727

This changes includes the mapping of options map in TFTPRequestPacket (according to RFC-2347) and allowing to use blksize (according to the RFC-2348) when doing a TFTP buffered send/receive.

Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @vimukthiD
Thank you for your PR!
Please rebase on git master to pick up the latest and address the comments I've scattered throughout.
TY!

private int packetSize = TFTPPacket.SEGMENT_SIZE + 4;

/** Internal state to track if the send/receive buffers are initialized */
private boolean buffersInityialized = false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix spelling of buffersInityialized.

public final void resetBuffersToSize(int packetSize) {
// the packet size should be between 8 - 65464 (inclusively) then we add 4 for the header
this.packetSize = (Math.min(Math.max(packetSize, 8), 65464) + 4);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra vertical whitespace.

}

/**
* Returns the buffer size of the buffered used by {@link #bufferedSend bufferedSend() } and {@link #bufferedReceive bufferedReceive() }.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc: A getter "Gets..."

/** The no such user error code according to RFC 783, value 7. */
public static final int NO_SUCH_USER = 7;

/** The invalid options error code according to RFC 2347, value 8. */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New public and protected elements should have a Javadoc @SInCE tag. In this case, for 3.12.0.

byte[] key = entry.getKey().getBytes(StandardCharsets.US_ASCII);
System.arraycopy(key, 0, data, offset, key.length);
offset += key.length;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for blank lines, if there is something to call out, then write an inline // comment.

public static final int ERROR = 5;

/**
* This is the actual TFTP spec identifier and is equal to 6.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to "This is", just say what it is 'TFTP spec identifier {@value}.'

}

/**
* Returns the options extensions of the request as a map.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A getter "Gets...".

@vimukthiD
Copy link
Copy Markdown
Contributor Author

Hello @garydgregory ,
thank you very much for checking the changes and sorry about the silly mistakes.
I've done the changes and updated the pull request.
Please let me know if I've missed anything.
Thank you !

Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vimukthiD

Thank you for updating your pull-request. You'll more comments to address in this round.

To verify your code coverage, run:

mvn clean site -Dcommons.jacoco.haltOnFailure=false -Pjacoco

and inspect the page:

target/site/jacoco/org.apache.commons.net.tftp/index.html#dn-b

Try to get the new code the highest possible coverage if you can.

TY!

private DatagramPacket sendDatagram;

/** Internal packet size, which is the data octet size plus 4 (for TFTP header octets) */
private int packetSize = TFTPPacket.SEGMENT_SIZE + 4;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't duplicate the expression that defines PACKET_SIZE, reuse that constant.

/**
* The invalid options error code according to RFC 2347, value 8.
* @since 3.12.0
* */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix formatting.

for (Map.Entry<String, String> entry : options.entrySet()) {
optionsLength += entry.getKey().length() + 1 + entry.getValue().length() + 1;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra vertical whitespace.

int offset = 0;
data[offset++] = 0;
data[offset++] = (byte) type;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra vertical whitespace.

}

private int writeOptionsData(byte[] data, int offset) {
// all the options would get written to the data byte array in following format
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this inline comment in a Javadoc comment using <pre> tags, keep it simple, you don't need "~~"s...

}

/**
* Gets the buffer size of the buffered used by {@link #bufferedSend bufferedSend() } and {@link #bufferedReceive bufferedReceive() }.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify clutter: {@link #bufferedSend bufferedSend() } -> {@link #bufferedSend(TFTPPacket)} and so on.

* @return current buffer size
* @since 3.12.0
*/
public int getBufferSize() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This method is not tested or called, remove it or add a unit test for it.
  • Rename to match the instance variable.

import java.util.HashMap;
import java.util.Map;

/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is not tested. Remove it or create a unit test.

for (Map.Entry<String, String> entry : options.entrySet()) {
optionsLength += entry.getKey().length() + 1 + entry.getValue().length() + 1;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra vertical whitespace.

throw new TFTPPacketException("Invalid option format");
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra vertical whitespace.

@garydgregory
Copy link
Copy Markdown
Member

Hello @vimukthiD
Make sure to rebase on git master which will pick up the latest Checlstyle rules that automatically tell you some of the comments I made above when you run the default Maven build (mvn by itself) or explicitly run Checkstyle (mvn checkstyle:check, which is faster than a whole build).

@vimukthiD
Copy link
Copy Markdown
Contributor Author

Hello @garydgregory
thank you very much for checking the changes again and sorry about the delay with the changes.
I've added the fixes you've mentioned and some minor tests.
Please let me know if I've missed anything.
Thank you !

@vimukthiD vimukthiD requested a review from garydgregory March 4, 2025 13:25
Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @vimukthiD

Thank you for your updates.

The class TFTPOptionsAckPacket is not used in main. Why is it needed?

TY!

@vimukthiD
Copy link
Copy Markdown
Contributor Author

Hello @garydgregory,

Oh yeah, sorry about that. At the moment it's not used, but was planning on implementing RFC-2347 which would require the TFTPOptionsAckPacket. Maybe it would be better to remove it from this changes and add it along its usages.
Let me know what you think.

Thank you!

@garydgregory
Copy link
Copy Markdown
Member

Hello @vimukthiD
If it's not used, then remove it and save that work on your end for another PR in the future.

@vimukthiD
Copy link
Copy Markdown
Contributor Author

Hello @garydgregory ,

Sorry for the trouble. I've removed the TFTPOptionsAckPacket

Thank you !

@garydgregory garydgregory merged commit 0a85ec8 into apache:master Mar 5, 2025
12 checks passed
@garydgregory garydgregory changed the title NET-727 enable accessing options map for TFTP request packet and allow using blksize option [NET-727] Enable accessing options map for TFTP request packet and allow using blksize option Mar 5, 2025
asfgit pushed a commit that referenced this pull request Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants