    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 *      http://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 definiing the TFTP Acknowledgement
    025 * packet type.
    026 * <p>
    027 * Details regarding the TFTP protocol and the format of TFTP packets can
    028 * be found in RFC 783.  But the point of these classes is to keep you
    029 * from having to worry about the internals.  Additionally, only very
    030 * few people should have to care about any of the TFTPPacket classes
    031 * or derived classes.  Almost all users should only be concerned with the
    032 * {@link org.apache.commons.net.tftp.TFTPClient} class
    033 * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
    034 * and
    035 * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
    036 * methods.
    037 *
    038 *
    039 * @see TFTPPacket
    040 * @see TFTPPacketException
    041 * @see TFTP
    042 */
    043
    044public final class TFTPAckPacket extends TFTPPacket
    045{
    046    /** The block number being acknowledged by the packet. */
    047    int blockNumber;
    048
    049    /**
    050     * Creates an acknowledgment packet to be sent to a host at a given port
    051     * acknowledging receipt of a block.
    052     *
    053     * @param destination  The host to which the packet is going to be sent.
    054     * @param port  The port to which the packet is going to be sent.
    055     * @param blockNumber  The block number being acknowledged.
    056     */
    057    public TFTPAckPacket(final InetAddress destination, final int port, final int blockNumber)
    058    {
    059        super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
    060        this.blockNumber = blockNumber;
    061    }
    062
    063    /**
    064     * Creates an acknowledgement packet based from a received
    065     * datagram.  Assumes the datagram is at least length 4, else an
    066     * ArrayIndexOutOfBoundsException may be thrown.
    067     *
    068     * @param datagram  The datagram containing the received acknowledgement.
    069     * @throws TFTPPacketException  If the datagram isn't a valid TFTP
    070     *         acknowledgement packet.
    071     */
    072    TFTPAckPacket(final DatagramPacket datagram) throws TFTPPacketException
    073    {
    074        super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
    075              datagram.getPort());
    076        final byte[] data;
    077
    078        data = datagram.getData();
    079
    080        if (getType() != data[1]) {
    081            throw new TFTPPacketException("TFTP operator code does not match type.");
    082        }
    083
    084        this.blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
    085    }
    086
    087    /**
    088     * This is a method only available within the package for
    089     * implementing efficient datagram transport by elminating buffering.
    090     * It takes a datagram as an argument, and a byte buffer in which
    091     * to store the raw datagram data.  Inside the method, the data
    092     * is set as the datagram's data and the datagram returned.
    093     *
    094     * @param datagram  The datagram to create.
    095     * @param data The buffer to store the packet and to use in the datagram.
    096     * @return The datagram argument.
    097     */
    098    @Override
    099    DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data)
    100    {
    101        data[0] = 0;
    102        data[1] = (byte)type;
    103        data[2] = (byte)((blockNumber & 0xffff) >> 8);
    104        data[3] = (byte)(blockNumber & 0xff);
    105
    106        datagram.setAddress(address);
    107        datagram.setPort(port);
    108        datagram.setData(data);
    109        datagram.setLength(4);
    110
    111        return datagram;
    112    }
    113
    114
    115    /**
    116     * Creates a UDP datagram containing all the TFTP
    117     * acknowledgement packet data in the proper format.
    118     * This is a method exposed to the programmer in case he
    119     * wants to implement his own TFTP client instead of using
    120     * the {@link org.apache.commons.net.tftp.TFTPClient}
    121     * class.  Under normal circumstances, you should not have a need to call this
    122     * method.
    123     *
    124     * @return A UDP datagram containing the TFTP acknowledgement packet.
    125     */
    126    @Override
    127    public DatagramPacket newDatagram()
    128    {
    129        final byte[] data;
    130
    131        data = new byte[4];
    132        data[0] = 0;
    133        data[1] = (byte)type;
    134        data[2] = (byte)((blockNumber & 0xffff) >> 8);
    135        data[3] = (byte)(blockNumber & 0xff);
    136
    137        return new DatagramPacket(data, data.length, address, port);
    138    }
    139
    140
    141    /**
    142     * Returns the block number of the acknowledgement.
    143     *
    144     * @return The block number of the acknowledgement.
    145     */
    146    public int getBlockNumber()
    147    {
    148        return blockNumber;
    149    }
    150
    151
    152    /**
    153     * Sets the block number of the acknowledgement.
    154     *
    155     * @param blockNumber the number to set
    156     */
    157    public void setBlockNumber(final int blockNumber)
    158    {
    159        this.blockNumber = blockNumber;
    160    }
    161
    162    /**
    163     * For debugging
    164     * @since 3.6
    165     */
    166    @Override
    167    public String toString() {
    168        return super.toString() + " ACK " + blockNumber;
    169    }
    170}
    171