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