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 Acknowledgement 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 TFTPAckPacket extends TFTPPacket {
038
039    /** The block number being acknowledged by the packet. */
040    int blockNumber;
041
042    /**
043     * Creates an acknowledgement packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may
044     * be thrown.
045     *
046     * @param datagram The datagram containing the received acknowledgement.
047     * @throws TFTPPacketException If the datagram isn't a valid TFTP acknowledgement packet.
048     */
049    TFTPAckPacket(final DatagramPacket datagram) throws TFTPPacketException {
050        super(ACKNOWLEDGEMENT, datagram.getAddress(), datagram.getPort());
051        final byte[] data;
052
053        data = datagram.getData();
054
055        if (getType() != data[1]) {
056            throw new TFTPPacketException("TFTP operator code does not match type.");
057        }
058
059        this.blockNumber = (data[2] & 0xff) << 8 | data[3] & 0xff;
060    }
061
062    /**
063     * Creates an acknowledgment packet to be sent to a host at a given port acknowledging receipt of a block.
064     *
065     * @param destination The host to which the packet is going to be sent.
066     * @param port        The port to which the packet is going to be sent.
067     * @param blockNumber The block number being acknowledged.
068     */
069    public TFTPAckPacket(final InetAddress destination, final int port, final int blockNumber) {
070        super(ACKNOWLEDGEMENT, destination, port);
071        this.blockNumber = blockNumber;
072    }
073
074    /**
075     * Gets the block number of the acknowledgement.
076     *
077     * @return The block number of the acknowledgement.
078     */
079    public int getBlockNumber() {
080        return blockNumber;
081    }
082
083    /**
084     * Creates a UDP datagram containing all the TFTP acknowledgement packet data in the proper format. This is a method exposed to the programmer in case he
085     * wants to implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should
086     * not have a need to call this method.
087     *
088     * @return A UDP datagram containing the TFTP acknowledgement packet.
089     */
090    @Override
091    public DatagramPacket newDatagram() {
092        final byte[] data;
093
094        data = new byte[4];
095        data[0] = 0;
096        data[1] = (byte) type;
097        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
098        data[3] = (byte) (blockNumber & 0xff);
099
100        return new DatagramPacket(data, data.length, address, port);
101    }
102
103    /**
104     * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
105     * 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.
106     *
107     * @param datagram The datagram to create.
108     * @param data     The buffer to store the packet and to use in the datagram.
109     * @return The datagram argument.
110     */
111    @Override
112    DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
113        data[0] = 0;
114        data[1] = (byte) type;
115        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
116        data[3] = (byte) (blockNumber & 0xff);
117
118        datagram.setAddress(address);
119        datagram.setPort(port);
120        datagram.setData(data);
121        datagram.setLength(4);
122
123        return datagram;
124    }
125
126    /**
127     * Sets the block number of the acknowledgement.
128     *
129     * @param blockNumber the number to set
130     */
131    public void setBlockNumber(final int blockNumber) {
132        this.blockNumber = blockNumber;
133    }
134
135    /**
136     * For debugging
137     *
138     * @since 3.6
139     */
140    @Override
141    public String toString() {
142        return super.toString() + " ACK " + blockNumber;
143    }
144}