    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   * TFTPPacket is an abstract class encapsulating the functionality common
    62   * to the 5 types of TFTP packets.  It also provides a static factory
    63   * method that will create the correct TFTP packet instance from a
    64   * datagram.  This relieves the programmer from having to figure out what
    65   * kind of TFTP packet is contained in a datagram and create it himself.
    66   * <p>
    67   * Details regarding the TFTP protocol and the format of TFTP packets can
    68   * be found in RFC 783.  But the point of these classes is to keep you
    69   * from having to worry about the internals.  Additionally, only very
    70   * few people should have to care about any of the TFTPPacket classes
    71   * or derived classes.  Almost all users should only be concerned with the
    72   * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
    73   * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
    74   * and
    75   * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
    76   * methods.
    77   * <p>
    78   * <p>
    79   * @author Daniel F. Savarese
    80   * @see TFTPPacketException
    81   * @see TFTP
    82   ***/
    83
    84  public abstract class TFTPPacket
    85  {
    86      /****
    87       * The minimum size of a packet.  This is 4 bytes.  It is enough
    88       * to store the opcode and blocknumber or other required data
    89       * depending on the packet type.
    90       ***/
    91      static final int MIN_PACKET_SIZE = 4;
    92
    93      /****
    94       * This is the actual TFTP spec
    95       * identifier and is equal to 1.
    96       * Identifier returned by <a href="#getType">getType()</a>
    97       * indicating a read request packet.
    98       ***/
    99      public static final int READ_REQUEST = 1;
    100
    101     /****
    102      * This is the actual TFTP spec
    103      * identifier and is equal to 2.
    104      * Identifier returned by <a href="#getType">getType()</a>
    105      * indicating a write request packet.
    106      ***/
    107     public static final int WRITE_REQUEST = 2;
    108
    109     /****
    110      * This is the actual TFTP spec
    111      * identifier and is equal to 3.
    112      * Identifier returned by <a href="#getType">getType()</a>
    113      * indicating a data packet.
    114      ***/
    115     public static final int DATA = 3;
    116
    117     /****
    118      * This is the actual TFTP spec
    119      * identifier and is equal to 4.
    120      * Identifier returned by <a href="#getType">getType()</a>
    121      * indicating an acknowledgement packet.
    122      ***/
    123     public static final int ACKNOWLEDGEMENT = 4;
    124
    125     /****
    126      * This is the actual TFTP spec
    127      * identifier and is equal to 5.
    128      * Identifier returned by <a href="#getType">getType()</a>
    129      * indicating an error packet.
    130      ***/
    131     public static final int ERROR = 5;
    132
    133     /****
    134      * The TFTP data packet maximum segment size in bytes.  This is 512
    135      * and is useful for those familiar with the TFTP protocol who want
    136      * to use the <a href="org.apache.commons.net.tftp.TFTP.html#_top_">TFTP</a>
    137      * class methods to implement their own TFTP servers or clients.
    138      ***/
    139     public static final int SEGMENT_SIZE = 512;
    140
    141     /**** The type of packet. ***/
    142     int _type;
    143
    144     /**** The port the packet came from or is going to. ***/
    145     int _port;
    146
    147     /**** The host the packet is going to be sent or where it came from. ***/
    148     InetAddress _address;
    149
    150     /****
    151      * When you receive a datagram that you expect to be a TFTP packet, you use
    152      * this factory method to create the proper TFTPPacket object
    153      * encapsulating the data contained in that datagram.  This method is the
    154      * only way you can instantiate a TFTPPacket derived class from a
    155      * datagram.
    156      * <p>
    157      * @param datagram  The datagram containing a TFTP packet.
    158      * @return The TFTPPacket object corresponding to the datagram.
    159      * @exception TFTPPacketException  If the datagram does not contain a valid
    160      *             TFTP packet.
    161      ***/
    162     public final static TFTPPacket newTFTPPacket(DatagramPacket datagram)
    163     throws TFTPPacketException
    164     {
    165         byte[] data;
    166         TFTPPacket packet = null;
    167
    168         if (datagram.getLength() < MIN_PACKET_SIZE)
    169             throw new TFTPPacketException(
    170                 "Bad packet. Datagram data length is too short.");
    171
    172         data = datagram.getData();
    173
    174         switch (data[1])
    175         {
    176         case READ_REQUEST:
    177             packet = new TFTPReadRequestPacket(datagram);
    178             break;
    179         case WRITE_REQUEST:
    180             packet = new TFTPWriteRequestPacket(datagram);
    181             break;
    182         case DATA:
    183             packet = new TFTPDataPacket(datagram);
    184             break;
    185         case ACKNOWLEDGEMENT:
    186             packet = new TFTPAckPacket(datagram);
    187             break;
    188         case ERROR:
    189             packet = new TFTPErrorPacket(datagram);
    190             break;
    191         default:
    192             throw new TFTPPacketException(
    193                 "Bad packet.  Invalid TFTP operator code.");
    194         }
    195
    196         return packet;
    197     }
    198
    199     /****
    200      * This constructor is not visible outside of the package.  It is used
    201      * by subclasses within the package to initialize base data.
    202      * <p>
    203      * @param type The type of the packet.
    204      * @param address The host the packet came from or is going to be sent.
    205      * @param port The port the packet came from or is going to be sent.
    206      **/
    207     TFTPPacket(int type, InetAddress address, int port)
    208     {
    209         _type = type;
    210         _address = address;
    211         _port = port;
    212     }
    213
    214     /****
    215      * This is an abstract method only available within the package for
    216      * implementing efficient datagram transport by elminating buffering.
    217      * It takes a datagram as an argument, and a byte buffer in which
    218      * to store the raw datagram data.  Inside the method, the data
    219      * should be set as the datagram's data and the datagram returned.
    220      * <p>
    221      * @param datagram  The datagram to create.
    222      * @param data The buffer to store the packet and to use in the datagram.
    223      * @return The datagram argument.
    224      ***/
    225     abstract DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data);
    226
    227     /****
    228      * Creates a UDP datagram containing all the TFTP packet
    229      * data in the proper format.
    230      * This is an abstract method, exposed to the programmer in case he
    231      * wants to implement his own TFTP client instead of using
    232      * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
    233      * class.
    234      * Under normal circumstances, you should not have a need to call this
    235      * method.
    236      * <p>
    237      * @return A UDP datagram containing the TFTP packet.
    238      ***/
    239     public abstract DatagramPacket newDatagram();
    240
    241     /****
    242      * Returns the type of the packet.
    243      * <p>
    244      * @return The type of the packet.
    245      ***/
    246     public final int getType()
    247     {
    248         return _type;
    249     }
    250
    251     /****
    252      * Returns the address of the host where the packet is going to be sent
    253      * or where it came from.
    254      * <p>
    255      * @return The type of the packet.
    256      ***/
    257     public final InetAddress getAddress()
    258     {
    259         return _address;
    260     }
    261
    262     /****
    263      * Returns the port where the packet is going to be sent
    264      * or where it came from.
    265      * <p>
    266      * @return The port where the packet came from or where it is going.
    267      ***/
    268     public final int getPort()
    269     {
    270         return _port;
    271     }
    272
    273     /**** Sets the port where the packet is going to be sent. ***/
    274     public final void setPort(int port)
    275     {
    276         _port = port;
    277     }
    278
    279     /**** Sets the host address where the packet is going to be sent. ***/
    280     public final void setAddress(InetAddress address)
    281     {
    282         _address = address;
    283     }
    284 }

This page was automatically generated by Maven