TFTPErrorPacket.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.tftp;

  18. import java.net.DatagramPacket;
  19. import java.net.InetAddress;
  20. import java.nio.charset.Charset;

  21. /**
  22.  * A final class derived from TFTPPacket defining the TFTP Error packet type.
  23.  * <p>
  24.  * 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
  25.  * 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
  26.  * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
  27.  * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
  28.  *
  29.  *
  30.  * @see TFTPPacket
  31.  * @see TFTPPacketException
  32.  * @see TFTP
  33.  */

  34. public final class TFTPErrorPacket extends TFTPPacket {
  35.     /** The undefined error code according to RFC 783, value 0. */
  36.     public static final int UNDEFINED = 0;

  37.     /** The file not found error code according to RFC 783, value 1. */
  38.     public static final int FILE_NOT_FOUND = 1;

  39.     /** The access violation error code according to RFC 783, value 2. */
  40.     public static final int ACCESS_VIOLATION = 2;

  41.     /** The disk full error code according to RFC 783, value 3. */
  42.     public static final int OUT_OF_SPACE = 3;

  43.     /**
  44.      * The illegal TFTP operation error code according to RFC 783, value 4.
  45.      */
  46.     public static final int ILLEGAL_OPERATION = 4;

  47.     /** The unknown transfer id error code according to RFC 783, value 5. */
  48.     public static final int UNKNOWN_TID = 5;

  49.     /** The file already exists error code according to RFC 783, value 6. */
  50.     public static final int FILE_EXISTS = 6;

  51.     /** The no such user error code according to RFC 783, value 7. */
  52.     public static final int NO_SUCH_USER = 7;

  53.     /** The error code of this packet. */
  54.     private final int error;

  55.     /** The error message of this packet. */
  56.     private final String message;

  57.     /**
  58.      * Creates an error packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
  59.      *
  60.      * @param datagram The datagram containing the received error.
  61.      * @throws TFTPPacketException If the datagram isn't a valid TFTP error packet.
  62.      */
  63.     TFTPErrorPacket(final DatagramPacket datagram) throws TFTPPacketException {
  64.         super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort());
  65.         int index;
  66.         final int length;
  67.         final byte[] data;
  68.         final StringBuilder buffer;

  69.         data = datagram.getData();
  70.         length = datagram.getLength();

  71.         if (getType() != data[1]) {
  72.             throw new TFTPPacketException("TFTP operator code does not match type.");
  73.         }

  74.         error = (data[2] & 0xff) << 8 | data[3] & 0xff;

  75.         if (length < 5) {
  76.             throw new TFTPPacketException("Bad error packet. No message.");
  77.         }

  78.         index = 4;
  79.         buffer = new StringBuilder();

  80.         while (index < length && data[index] != 0) {
  81.             buffer.append((char) data[index]);
  82.             ++index;
  83.         }

  84.         message = buffer.toString();
  85.     }

  86.     /**
  87.      * Creates an error packet to be sent to a host at a given port with an error code and error message.
  88.      *
  89.      * @param destination The host to which the packet is going to be sent.
  90.      * @param port        The port to which the packet is going to be sent.
  91.      * @param error       The error code of the packet.
  92.      * @param message     The error message of the packet.
  93.      */
  94.     public TFTPErrorPacket(final InetAddress destination, final int port, final int error, final String message) {
  95.         super(TFTPPacket.ERROR, destination, port);

  96.         this.error = error;
  97.         this.message = message;
  98.     }

  99.     /**
  100.      * Returns the error code of the packet.
  101.      *
  102.      * @return The error code of the packet.
  103.      */
  104.     public int getError() {
  105.         return error;
  106.     }

  107.     /**
  108.      * Returns the error message of the packet.
  109.      *
  110.      * @return The error message of the packet.
  111.      */
  112.     public String getMessage() {
  113.         return message;
  114.     }

  115.     /**
  116.      * 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
  117.      * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
  118.      * a need to call this method.
  119.      *
  120.      * @return A UDP datagram containing the TFTP error packet.
  121.      */
  122.     @Override
  123.     public DatagramPacket newDatagram() {
  124.         final byte[] data;
  125.         final int length;

  126.         length = message.length();

  127.         data = new byte[length + 5];
  128.         data[0] = 0;
  129.         data[1] = (byte) type;
  130.         data[2] = (byte) ((error & 0xffff) >> 8);
  131.         data[3] = (byte) (error & 0xff);

  132.         System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length);

  133.         data[length + 4] = 0;

  134.         return new DatagramPacket(data, data.length, address, port);
  135.     }

  136.     /**
  137.      * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
  138.      * 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.
  139.      *
  140.      * @param datagram The datagram to create.
  141.      * @param data     The buffer to store the packet and to use in the datagram.
  142.      * @return The datagram argument.
  143.      */
  144.     @Override
  145.     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
  146.         final int length;

  147.         length = message.length();

  148.         data[0] = 0;
  149.         data[1] = (byte) type;
  150.         data[2] = (byte) ((error & 0xffff) >> 8);
  151.         data[3] = (byte) (error & 0xff);

  152.         System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length);

  153.         data[length + 4] = 0;

  154.         datagram.setAddress(address);
  155.         datagram.setPort(port);
  156.         datagram.setData(data);
  157.         datagram.setLength(length + 4);

  158.         return datagram;
  159.     }

  160.     /**
  161.      * For debugging
  162.      *
  163.      * @since 3.6
  164.      */
  165.     @Override
  166.     public String toString() {
  167.         return super.toString() + " ERR " + error + " " + message;
  168.     }
  169. }