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