    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
    018    package org.apache.commons.net.tftp;
    019
    020    import java.net.DatagramPacket;
    021    import 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     * <p>
    038     * <p>
    039     * @see TFTPPacket
    040     * @see TFTPPacketException
    041     * @see TFTP
    042     ***/
    043
    044    public 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         * <p>
    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(InetAddress destination, int port, int blockNumber)
    058        {
    059            super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
    060            _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         * <p>
    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(DatagramPacket datagram) throws TFTPPacketException
    073        {
    074            super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
    075                  datagram.getPort());
    076            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            _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
    084        }
    085
    086        /***
    087         * This is a method only available within the package for
    088         * implementing efficient datagram transport by elminating buffering.
    089         * It takes a datagram as an argument, and a byte buffer in which
    090         * to store the raw datagram data.  Inside the method, the data
    091         * is set as the datagram's data and the datagram returned.
    092         * <p>
    093         * @param datagram  The datagram to create.
    094         * @param data The buffer to store the packet and to use in the datagram.
    095         * @return The datagram argument.
    096         ***/
    097        @Override
    098        DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
    099        {
    100            data[0] = 0;
    101            data[1] = (byte)_type;
    102            data[2] = (byte)((_blockNumber & 0xffff) >> 8);
    103            data[3] = (byte)(_blockNumber & 0xff);
    104
    105            datagram.setAddress(_address);
    106            datagram.setPort(_port);
    107            datagram.setData(data);
    108            datagram.setLength(4);
    109
    110            return datagram;
    111        }
    112
    113
    114        /***
    115         * Creates a UDP datagram containing all the TFTP
    116         * acknowledgement packet data in the proper format.
    117         * This is a method exposed to the programmer in case he
    118         * wants to implement his own TFTP client instead of using
    119         * the {@link org.apache.commons.net.tftp.TFTPClient}
    120         * class.  Under normal circumstances, you should not have a need to call this
    121         * method.
    122         * <p>
    123         * @return A UDP datagram containing the TFTP acknowledgement packet.
    124         ***/
    125        @Override
    126        public DatagramPacket newDatagram()
    127        {
    128            byte[] data;
    129
    130            data = new byte[4];
    131            data[0] = 0;
    132            data[1] = (byte)_type;
    133            data[2] = (byte)((_blockNumber & 0xffff) >> 8);
    134            data[3] = (byte)(_blockNumber & 0xff);
    135
    136            return new DatagramPacket(data, data.length, _address, _port);
    137        }
    138
    139
    140        /***
    141         * Returns the block number of the acknowledgement.
    142         * <p>
    143         * @return The block number of the acknowledgement.
    144         ***/
    145        public int getBlockNumber()
    146        {
    147            return _blockNumber;
    148        }
    149
    150
    151        /*** Sets the block number of the acknowledgement.  ***/
    152        public void setBlockNumber(int blockNumber)
    153        {
    154            _blockNumber = blockNumber;
    155        }
    156    }
    157