    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 Error
    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 TFTPErrorPacket extends TFTPPacket
    045    {
    046        /*** The undefined error code according to RFC 783, value 0. ***/
    047        public static final int UNDEFINED = 0;
    048
    049        /*** The file not found error code according to RFC 783, value 1. ***/
    050        public static final int FILE_NOT_FOUND = 1;
    051
    052        /*** The access violation error code according to RFC 783, value 2. ***/
    053        public static final int ACCESS_VIOLATION = 2;
    054
    055        /*** The disk full error code according to RFC 783, value 3. ***/
    056        public static final int OUT_OF_SPACE = 3;
    057
    058        /***
    059         * The illegal TFTP operation error code according to RFC 783, value 4.
    060         ***/
    061        public static final int ILLEGAL_OPERATION = 4;
    062
    063        /*** The unknown transfer id error code according to RFC 783, value 5. ***/
    064        public static final int UNKNOWN_TID = 5;
    065
    066        /*** The file already exists error code according to RFC 783, value 6. ***/
    067        public static final int FILE_EXISTS = 6;
    068
    069        /*** The no such user error code according to RFC 783, value 7. ***/
    070        public static final int NO_SUCH_USER = 7;
    071
    072        /*** The error code of this packet. ***/
    073        int _error;
    074
    075        /*** The error message of this packet. ***/
    076        String _message;
    077
    078        /***
    079         * Creates an error packet to be sent to a host at a given port
    080         * with an error code and error message.
    081         * <p>
    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 error The error code of the packet.
    085         * @param message The error message of the packet.
    086         ***/
    087        public TFTPErrorPacket(InetAddress destination, int port,
    088                               int error, String message)
    089        {
    090            super(TFTPPacket.ERROR, destination, port);
    091
    092            _error = error;
    093            _message = message;
    094        }
    095
    096        /***
    097         * Creates an error packet based from a received
    098         * datagram.  Assumes the datagram is at least length 4, else an
    099         * ArrayIndexOutOfBoundsException may be thrown.
    100         * <p>
    101         * @param datagram  The datagram containing the received error.
    102         * @throws TFTPPacketException  If the datagram isn't a valid TFTP
    103         *         error packet.
    104         ***/
    105        TFTPErrorPacket(DatagramPacket datagram) throws TFTPPacketException
    106        {
    107            super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort());
    108            int index, length;
    109            byte[] data;
    110            StringBuilder buffer;
    111
    112            data = datagram.getData();
    113            length = datagram.getLength();
    114
    115            if (getType() != data[1])
    116                throw new TFTPPacketException("TFTP operator code does not match type.");
    117
    118            _error = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
    119
    120            if (length < 5)
    121                throw new TFTPPacketException("Bad error packet. No message.");
    122
    123            index = 4;
    124            buffer = new StringBuilder();
    125
    126            while (index < length && data[index] != 0)
    127            {
    128                buffer.append((char)data[index]);
    129                ++index;
    130            }
    131
    132            _message = buffer.toString();
    133        }
    134
    135        /***
    136         * This is a method only available within the package for
    137         * implementing efficient datagram transport by elminating buffering.
    138         * It takes a datagram as an argument, and a byte buffer in which
    139         * to store the raw datagram data.  Inside the method, the data
    140         * is set as the datagram's data and the datagram returned.
    141         * <p>
    142         * @param datagram  The datagram to create.
    143         * @param data The buffer to store the packet and to use in the datagram.
    144         * @return The datagram argument.
    145         ***/
    146        @Override
    147        DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
    148        {
    149            int length;
    150
    151            length = _message.length();
    152
    153            data[0] = 0;
    154            data[1] = (byte)_type;
    155            data[2] = (byte)((_error & 0xffff) >> 8);
    156            data[3] = (byte)(_error & 0xff);
    157
    158            System.arraycopy(_message.getBytes(), 0, data, 4, length);
    159
    160            data[length + 4] = 0;
    161
    162            datagram.setAddress(_address);
    163            datagram.setPort(_port);
    164            datagram.setData(data);
    165            datagram.setLength(length + 4);
    166
    167            return datagram;
    168        }
    169
    170
    171        /***
    172         * Creates a UDP datagram containing all the TFTP
    173         * error packet data in the proper format.
    174         * This is a method exposed to the programmer in case he
    175         * wants to implement his own TFTP client instead of using
    176         * the {@link org.apache.commons.net.tftp.TFTPClient}
    177         * class.
    178         * Under normal circumstances, you should not have a need to call this
    179         * method.
    180         * <p>
    181         * @return A UDP datagram containing the TFTP error packet.
    182         ***/
    183        @Override
    184        public DatagramPacket newDatagram()
    185        {
    186            byte[] data;
    187            int length;
    188
    189            length = _message.length();
    190
    191            data = new byte[length + 5];
    192            data[0] = 0;
    193            data[1] = (byte)_type;
    194            data[2] = (byte)((_error & 0xffff) >> 8);
    195            data[3] = (byte)(_error & 0xff);
    196
    197            System.arraycopy(_message.getBytes(), 0, data, 4, length);
    198
    199            data[length + 4] = 0;
    200
    201            return new DatagramPacket(data, data.length, _address, _port);
    202        }
    203
    204
    205        /***
    206         * Returns the error code of the packet.
    207         * <p>
    208         * @return The error code of the packet.
    209         ***/
    210        public int getError()
    211        {
    212            return _error;
    213        }
    214
    215
    216        /***
    217         * Returns the error message of the packet.
    218         * <p>
    219         * @return The error message of the packet.
    220         ***/
    221        public String getMessage()
    222        {
    223            return _message;
    224        }
    225    }