    View Javadoc
    1   package org.apache.commons.net.tftp;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.net.DatagramPacket;
    58  import java.net.InetAddress;
    59
    60  /****
    61   * A final class derived from TFTPPacket definiing the TFTP Error
    62   * packet type.
    63   * <p>
    64   * Details regarding the TFTP protocol and the format of TFTP packets can
    65   * be found in RFC 783.  But the point of these classes is to keep you
    66   * from having to worry about the internals.  Additionally, only very
    67   * few people should have to care about any of the TFTPPacket classes
    68   * or derived classes.  Almost all users should only be concerned with the
    69   * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
    70   * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
    71   * and
    72   * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
    73   * methods.
    74   * <p>
    75   * <p>
    76   * @author Daniel F. Savarese
    77   * @see TFTPPacket
    78   * @see TFTPPacketException
    79   * @see TFTP
    80   ***/
    81
    82  public final class TFTPErrorPacket extends TFTPPacket
    83  {
    84      /**** The undefined error code according to RFC 783, value 0. ***/
    85      public static final int UNDEFINED = 0;
    86
    87      /**** The file not found error code according to RFC 783, value 1. ***/
    88      public static final int FILE_NOT_FOUND = 1;
    89
    90      /**** The access violation error code according to RFC 783, value 2. ***/
    91      public static final int ACCESS_VIOLATION = 2;
    92
    93      /**** The disk full error code according to RFC 783, value 3. ***/
    94      public static final int OUT_OF_SPACE = 3;
    95
    96      /****
    97       * The illegal TFTP operation error code according to RFC 783, value 4.
    98       ***/
    99      public static final int ILLEGAL_OPERATION = 4;
    100
    101     /**** The unknown transfer id error code according to RFC 783, value 5. ***/
    102     public static final int UNKNOWN_TID = 5;
    103
    104     /**** The file already exists error code according to RFC 783, value 6. ***/
    105     public static final int FILE_EXISTS = 6;
    106
    107     /**** The no such user error code according to RFC 783, value 7. ***/
    108     public static final int NO_SUCH_USER = 7;
    109
    110     /**** The error code of this packet. ***/
    111     int _error;
    112
    113     /**** The error message of this packet. ***/
    114     String _message;
    115
    116     /****
    117      * Creates an error packet to be sent to a host at a given port
    118      * with an error code and error message.
    119      * <p>
    120      * @param destination  The host to which the packet is going to be sent.
    121      * @param port  The port to which the packet is going to be sent.
    122      * @param error The error code of the packet.
    123      * @param message The error message of the packet.
    124      ***/
    125     public TFTPErrorPacket(InetAddress destination, int port,
    126                            int error, String message)
    127     {
    128         super(TFTPPacket.ERROR, destination, port);
    129
    130         _error = error;
    131         _message = message;
    132     }
    133
    134     /****
    135      * Creates an error packet based from a received
    136      * datagram.  Assumes the datagram is at least length 4, else an
    137      * ArrayIndexOutOfBoundsException may be thrown.
    138      * <p>
    139      * @param datagram  The datagram containing the received error.
    140      * @throws TFTPPacketException  If the datagram isn't a valid TFTP
    141      *         error packet.
    142      ***/
    143     TFTPErrorPacket(DatagramPacket datagram) throws TFTPPacketException
    144     {
    145         super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort());
    146         int index, length;
    147         byte[] data;
    148         StringBuffer buffer;
    149
    150         data = datagram.getData();
    151         length = datagram.getLength();
    152
    153         if (getType() != data[1])
    154             throw new TFTPPacketException("TFTP operator code does not match type.");
    155
    156         _error = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
    157
    158         if (length < 5)
    159             throw new TFTPPacketException("Bad error packet. No message.");
    160
    161         index = 4;
    162         buffer = new StringBuffer();
    163
    164         while (index < length && data[index] != 0)
    165         {
    166             buffer.append((char)data[index]);
    167             ++index;
    168         }
    169
    170         _message = buffer.toString();
    171     }
    172
    173     /****
    174      * This is a method only available within the package for
    175      * implementing efficient datagram transport by elminating buffering.
    176      * It takes a datagram as an argument, and a byte buffer in which
    177      * to store the raw datagram data.  Inside the method, the data
    178      * is set as the datagram's data and the datagram returned.
    179      * <p>
    180      * @param datagram  The datagram to create.
    181      * @param data The buffer to store the packet and to use in the datagram.
    182      * @return The datagram argument.
    183      ***/
    184     DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
    185     {
    186         int length;
    187
    188         length = _message.length();
    189
    190         data[0] = 0;
    191         data[1] = (byte)_type;
    192         data[2] = (byte)((_error & 0xffff) >> 8);
    193         data[3] = (byte)(_error & 0xff);
    194
    195         System.arraycopy(_message.getBytes(), 0, data, 4, length);
    196
    197         data[length + 4] = 0;
    198
    199         datagram.setAddress(_address);
    200         datagram.setPort(_port);
    201         datagram.setData(data);
    202         datagram.setLength(length + 4);
    203
    204         return datagram;
    205     }
    206
    207
    208     /****
    209      * Creates a UDP datagram containing all the TFTP
    210      * error packet data in the proper format.
    211      * This is a method exposed to the programmer in case he
    212      * wants to implement his own TFTP client instead of using
    213      * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
    214      * class.
    215      * Under normal circumstances, you should not have a need to call this
    216      * method.
    217      * <p>
    218      * @return A UDP datagram containing the TFTP error packet.
    219      ***/
    220     public DatagramPacket newDatagram()
    221     {
    222         byte[] data;
    223         int length;
    224
    225         length = _message.length();
    226
    227         data = new byte[length + 5];
    228         data[0] = 0;
    229         data[1] = (byte)_type;
    230         data[2] = (byte)((_error & 0xffff) >> 8);
    231         data[3] = (byte)(_error & 0xff);
    232
    233         System.arraycopy(_message.getBytes(), 0, data, 4, length);
    234
    235         data[length + 4] = 0;
    236
    237         return new DatagramPacket(data, data.length, _address, _port);
    238     }
    239
    240
    241     /****
    242      * Returns the error code of the packet.
    243      * <p>
    244      * @return The error code of the packet.
    245      ***/
    246     public int getError()
    247     {
    248         return _error;
    249     }
    250
    251
    252     /****
    253      * Returns the error message of the packet.
    254      * <p>
    255      * @return The error message of the packet.
    256      ***/
    257     public String getMessage()
    258     {
    259         return _message;
    260     }
    261 }

This page was automatically generated by Maven