Skip to content
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

NET-727 enable accessing options map for TFTP request packet #318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class TFTPErrorPacket extends TFTPPacket {
/** 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. */
public static final int INVALID_OPTIONS_VALUE = 8;

/** The error code of this packet. */
private final int error;

Expand Down
81 changes: 75 additions & 6 deletions src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
* An abstract class derived from TFTPPacket definiing a TFTP Request packet type. It is subclassed by the
Expand Down Expand Up @@ -56,6 +59,9 @@ public abstract class TFTPRequestPacket extends TFTPPacket {
/** The file name of the request. */
private final String fileName;

/** The option values */
private final Map<String, String> options = new HashMap<>();

/**
* Creates a request packet of a given type to be sent to a host at a given port with a file name and transfer mode request.
*
Expand Down Expand Up @@ -113,24 +119,50 @@ public abstract class TFTPRequestPacket extends TFTPPacket {
}

final String modeString = buffer.toString().toLowerCase(java.util.Locale.ENGLISH);
length = modeStrings.length;
final int modeStringsLength = modeStrings.length;

int mode = 0;
for (index = 0; index < length; index++) {
if (modeString.equals(modeStrings[index])) {
mode = index;
int modeIndex;
for (modeIndex = 0; modeIndex < modeStringsLength; modeIndex++) {
if (modeString.equals(modeStrings[modeIndex])) {
mode = modeIndex;
break;
}
}

this.mode = mode;

if (index >= length) {
if (modeIndex >= modeStringsLength) {
throw new TFTPPacketException("Unrecognized TFTP transfer mode: " + modeString);
// May just want to default to binary mode instead of throwing
// exception.
// _mode = TFTP.OCTET_MODE;
}

++index;
while (index < length) {
int start = index;
for (; data[index] != 0; ++index) {
if (index >= length) {
throw new TFTPPacketException("Invalid option format");
}
}

String option = new String(data, start, index - start, StandardCharsets.US_ASCII);

++index;
start = index;
for (; data[index] != 0; ++index) {
if (index >= length) {
throw new TFTPPacketException("Invalid option format");
}
}

String octets = new String(data, start, index - start, StandardCharsets.US_ASCII);

this.options.put(option, octets);
++index;
}
}

/**
Expand All @@ -151,6 +183,16 @@ public final int getMode() {
return mode;
}

/**
* Returns the options extensions of the request as a map.
* The keys are the option names and the values are the option values.
*
* @return The options extensions of the request as a map.
*/
public final Map<String, String> getOptions() {
return options;
}

/**
* Creates a UDP datagram containing all the TFTP request packet data in the proper format. This is a method exposed to the programmer in case he wants to
* implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
Expand All @@ -167,13 +209,22 @@ public final DatagramPacket newDatagram() {
fileLength = fileName.length();
modeLength = modeBytes[mode].length;

data = new byte[fileLength + modeLength + 4];
int optionsLength = 0;
for (Map.Entry<String, String> entry : options.entrySet()) {
optionsLength += entry.getKey().length() + 1 + entry.getValue().length() + 1;
}

data = new byte[fileLength + modeLength + 3 + optionsLength];
data[0] = 0;
data[1] = (byte) type;
System.arraycopy(fileName.getBytes(Charset.defaultCharset()), 0, data, 2, fileLength);
data[fileLength + 2] = 0;
System.arraycopy(modeBytes[mode], 0, data, fileLength + 3, modeLength);

if (optionsLength > 0) {
handleOptions(data, fileLength, modeLength);
}

return new DatagramPacket(data, data.length, address, port);
}

Expand All @@ -199,11 +250,29 @@ final DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] dat
data[fileLength + 2] = 0;
System.arraycopy(modeBytes[mode], 0, data, fileLength + 3, modeLength);

handleOptions(data, fileLength, modeLength);

datagram.setAddress(address);
datagram.setPort(port);
datagram.setData(data);
datagram.setLength(fileLength + modeLength + 3);

return datagram;
}

private void handleOptions(byte[] data, int fileLength, int modeLength) {
int index = fileLength + modeLength + 2;
for (Map.Entry<String, String> entry : options.entrySet()) {
data[index] = 0;
String key = entry.getKey();
String value = entry.getValue();

System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, data, ++index, key.length());
index += key.length();
data[index++] = 0;

System.arraycopy(value.getBytes(StandardCharsets.US_ASCII), 0, data, index, value.length());
index += value.length();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.net.tftp;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

/**
* Tests {@link TFTPRequestPacket}.
*/
class TFTPRequestPacketTest {

@Test
public void testGetOptions() throws UnknownHostException, TFTPPacketException {
DatagramPacket datagramPacket = getDatagramPacket();
TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
assertNotNull(requestPacket.toString());
Map<String, String> options = requestPacket.getOptions();
assertEquals(1, options.size());
assertEquals("1024", options.get("blksize"));
}

@Test
public void testNewDatagram() throws TFTPPacketException, UnknownHostException {
DatagramPacket datagramPacket = getDatagramPacket();

TFTPReadRequestPacket requestPacket = new TFTPReadRequestPacket(datagramPacket);
DatagramPacket newDatagram = requestPacket.newDatagram();

assertNotNull(newDatagram);
assertEquals(datagramPacket.getAddress(), newDatagram.getAddress());
assertEquals(datagramPacket.getPort(), newDatagram.getPort());
assertEquals(datagramPacket.getLength(), newDatagram.getLength());
assertArrayEquals(datagramPacket.getData(), newDatagram.getData());

byte[] data = new byte[datagramPacket.getLength()];
DatagramPacket newDatagram2 = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
requestPacket.newDatagram(newDatagram2, data);

assertEquals(datagramPacket.getAddress(), newDatagram2.getAddress());
assertEquals(datagramPacket.getPort(), newDatagram2.getPort());
assertArrayEquals(datagramPacket.getData(), data);
}

private static DatagramPacket getDatagramPacket() throws UnknownHostException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write(0);
byteStream.write(1);

try {
byteStream.write("fileName".getBytes(StandardCharsets.US_ASCII));
byteStream.write(0);
byteStream.write("octet".getBytes(StandardCharsets.US_ASCII));
byteStream.write(0);

byteStream.write("blksize".getBytes(StandardCharsets.US_ASCII));
byteStream.write(0);
byteStream.write("1024".getBytes(StandardCharsets.US_ASCII));
byteStream.write(0);
} catch (IOException e) {
throw new RuntimeException("Error creating TFTP request packet", e);
}

byte[] data = byteStream.toByteArray();
return new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 0);
}
}