001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.tftp;
019
020import java.net.DatagramPacket;
021import java.net.InetAddress;
022
023/**
024 * A final class derived from TFTPPacket defining the TFTP Data packet type.
025 * <p>
026 * Details regarding the TFTP protocol and the format of TFTP packets can be found in RFC 783. But the point of these classes is to keep you from having to
027 * worry about the internals. Additionally, only very few people should have to care about any of the TFTPPacket classes or derived classes. Almost all users
028 * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
029 * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
030 * </p>
031 *
032 * @see TFTPPacket
033 * @see TFTPPacketException
034 * @see TFTP
035 */
036
037public final class TFTPDataPacket extends TFTPPacket {
038
039    /** The maximum number of bytes in a TFTP data packet (512) */
040    public static final int MAX_DATA_LENGTH = 512;
041
042    /** The minimum number of bytes in a TFTP data packet (0) */
043    public static final int MIN_DATA_LENGTH = 0;
044
045    /** The block number of the packet. */
046    int blockNumber;
047
048    /** The length of the data. */
049    private int length;
050
051    /** The offset into the _data array at which the data begins. */
052    private int offset;
053
054    /** The data stored in the packet. */
055    private byte[] data;
056
057    /**
058     * Creates a data packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
059     *
060     * @param datagram The datagram containing the received data.
061     * @throws TFTPPacketException If the datagram isn't a valid TFTP data packet.
062     */
063    TFTPDataPacket(final DatagramPacket datagram) throws TFTPPacketException {
064        super(DATA, datagram.getAddress(), datagram.getPort());
065
066        this.data = datagram.getData();
067        this.offset = 4;
068
069        if (getType() != this.data[1]) {
070            throw new TFTPPacketException("TFTP operator code does not match type.");
071        }
072
073        this.blockNumber = (this.data[2] & 0xff) << 8 | this.data[3] & 0xff;
074
075        this.length = datagram.getLength() - 4;
076    }
077
078    /**
079     * Creates a data packet to be sent to a host at a given port with a given block number. The actual data to be sent is passed as an array, an offset, and a
080     * length. The offset is the offset into the byte array where the data starts. The length is the length of the data.
081     *
082     * @param destination The host to which the packet is going to be sent.
083     * @param port        The port to which the packet is going to be sent.
084     * @param blockNumber The block number of the data.
085     * @param data        The byte array containing the data.
086     */
087    public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data) {
088        this(destination, port, blockNumber, data, 0, data.length);
089    }
090
091    /**
092     * Creates a data packet to be sent to a host at a given port with a given block number. The actual data to be sent is passed as an array, an offset, and a
093     * length. The offset is the offset into the byte array where the data starts. The length is the length of the data.
094     *
095     * @param destination The host to which the packet is going to be sent.
096     * @param port        The port to which the packet is going to be sent.
097     * @param blockNumber The block number of the data.
098     * @param data        The byte array containing the data.
099     * @param offset      The offset into the array where the data starts.
100     * @param length      The length of the data.
101     */
102    public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data, final int offset, final int length) {
103        super(DATA, destination, port);
104        this.blockNumber = blockNumber;
105        this.data = data;
106        this.offset = offset;
107        this.length = length;
108    }
109
110    /**
111     * Gets the block number of the data packet.
112     *
113     * @return The block number of the data packet.
114     */
115    public int getBlockNumber() {
116        return blockNumber;
117    }
118
119    /**
120     * Gets the byte array containing the packet data.
121     *
122     * @return The byte array containing the packet data.
123     */
124    public byte[] getData() {
125        return data;
126    }
127
128    /**
129     * Gets the length of the data part of the data packet.
130     *
131     * @return The length of the data part of the data packet.
132     */
133    public int getDataLength() {
134        return length;
135    }
136
137    /**
138     * Gets the offset into the byte array where the packet data actually starts.
139     *
140     * @return The offset into the byte array where the packet data actually starts.
141     */
142    public int getDataOffset() {
143        return offset;
144    }
145
146    /**
147     * Creates a UDP datagram containing all the TFTP data packet data in the proper format. This is a method exposed to the programmer in case he wants to
148     * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
149     * a need to call this method.
150     *
151     * @return A UDP datagram containing the TFTP data packet.
152     */
153    @Override
154    public DatagramPacket newDatagram() {
155        final byte[] data;
156
157        data = new byte[length + 4];
158        data[0] = 0;
159        data[1] = (byte) type;
160        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
161        data[3] = (byte) (blockNumber & 0xff);
162
163        System.arraycopy(this.data, offset, data, 4, length);
164
165        return new DatagramPacket(data, length + 4, address, port);
166    }
167
168    /**
169     * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
170     * argument, and a byte buffer in which to store the raw datagram data. Inside the method, the data is set as the datagram's data and the datagram returned.
171     *
172     * @param datagram The datagram to create.
173     * @param data     The buffer to store the packet and to use in the datagram.
174     * @return The datagram argument.
175     */
176    @Override
177    DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
178        data[0] = 0;
179        data[1] = (byte) type;
180        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
181        data[3] = (byte) (blockNumber & 0xff);
182
183        // Doublecheck we're not the same
184        if (data != this.data) {
185            System.arraycopy(this.data, offset, data, 4, length);
186        }
187
188        datagram.setAddress(address);
189        datagram.setPort(port);
190        datagram.setData(data);
191        datagram.setLength(length + 4);
192
193        return datagram;
194    }
195
196    /**
197     * Sets the block number of the data packet.
198     *
199     * @param blockNumber the number to set
200     */
201    public void setBlockNumber(final int blockNumber) {
202        this.blockNumber = blockNumber;
203    }
204
205    /**
206     * Sets the data for the data packet.
207     *
208     * @param data   The byte array containing the data.
209     * @param offset The offset into the array where the data starts.
210     * @param length The length of the data.
211     */
212    public void setData(final byte[] data, final int offset, final int length) {
213        this.data = data;
214        this.offset = offset;
215        this.length = length;
216    }
217
218    /**
219     * For debugging
220     *
221     * @since 3.6
222     */
223    @Override
224    public String toString() {
225        return super.toString() + " DATA " + blockNumber + " " + length;
226    }
227}