TFTPPacket.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. /**
  21.  * TFTPPacket is an abstract class encapsulating the functionality common to the 5 types of TFTP packets. It also provides a static factory method that will
  22.  * create the correct TFTP packet instance from a datagram. This relieves the programmer from having to figure out what kind of TFTP packet is contained in a
  23.  * datagram and create it himself.
  24.  * <p>
  25.  * 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
  26.  * 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
  27.  * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
  28.  * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
  29.  *
  30.  *
  31.  * @see TFTPPacketException
  32.  * @see TFTP
  33.  */

  34. public abstract class TFTPPacket {
  35.     /**
  36.      * The minimum size of a packet. This is 4 bytes. It is enough to store the opcode and blocknumber or other required data depending on the packet type.
  37.      */
  38.     static final int MIN_PACKET_SIZE = 4;

  39.     /**
  40.      * This is the actual TFTP spec identifier and is equal to 1. Identifier returned by {@link #getType getType()} indicating a read request packet.
  41.      */
  42.     public static final int READ_REQUEST = 1;

  43.     /**
  44.      * This is the actual TFTP spec identifier and is equal to 2. Identifier returned by {@link #getType getType()} indicating a write request packet.
  45.      */
  46.     public static final int WRITE_REQUEST = 2;

  47.     /**
  48.      * This is the actual TFTP spec identifier and is equal to 3. Identifier returned by {@link #getType getType()} indicating a data packet.
  49.      */
  50.     public static final int DATA = 3;

  51.     /**
  52.      * This is the actual TFTP spec identifier and is equal to 4. Identifier returned by {@link #getType getType()} indicating an acknowledgement packet.
  53.      */
  54.     public static final int ACKNOWLEDGEMENT = 4;

  55.     /**
  56.      * This is the actual TFTP spec identifier and is equal to 5. Identifier returned by {@link #getType getType()} indicating an error packet.
  57.      */
  58.     public static final int ERROR = 5;

  59.     /**
  60.      * The TFTP data packet maximum segment size in bytes. This is 512 and is useful for those familiar with the TFTP protocol who want to use the
  61.      * {@link org.apache.commons.net.tftp.TFTP} class methods to implement their own TFTP servers or clients.
  62.      */
  63.     public static final int SEGMENT_SIZE = 512;

  64.     /**
  65.      * When you receive a datagram that you expect to be a TFTP packet, you use this factory method to create the proper TFTPPacket object encapsulating the
  66.      * data contained in that datagram. This method is the only way you can instantiate a TFTPPacket derived class from a datagram.
  67.      *
  68.      * @param datagram The datagram containing a TFTP packet.
  69.      * @return The TFTPPacket object corresponding to the datagram.
  70.      * @throws TFTPPacketException If the datagram does not contain a valid TFTP packet.
  71.      */
  72.     public static final TFTPPacket newTFTPPacket(final DatagramPacket datagram) throws TFTPPacketException {
  73.         final byte[] data;
  74.         TFTPPacket packet;

  75.         if (datagram.getLength() < MIN_PACKET_SIZE) {
  76.             throw new TFTPPacketException("Bad packet. Datagram data length is too short.");
  77.         }

  78.         data = datagram.getData();

  79.         switch (data[1]) {
  80.         case READ_REQUEST:
  81.             packet = new TFTPReadRequestPacket(datagram);
  82.             break;
  83.         case WRITE_REQUEST:
  84.             packet = new TFTPWriteRequestPacket(datagram);
  85.             break;
  86.         case DATA:
  87.             packet = new TFTPDataPacket(datagram);
  88.             break;
  89.         case ACKNOWLEDGEMENT:
  90.             packet = new TFTPAckPacket(datagram);
  91.             break;
  92.         case ERROR:
  93.             packet = new TFTPErrorPacket(datagram);
  94.             break;
  95.         default:
  96.             throw new TFTPPacketException("Bad packet.  Invalid TFTP operator code.");
  97.         }

  98.         return packet;
  99.     }

  100.     /** The type of packet. */
  101.     int type;

  102.     /** The port the packet came from or is going to. */
  103.     int port;

  104.     /** The host the packet is going to be sent or where it came from. */
  105.     InetAddress address;

  106.     /**
  107.      * This constructor is not visible outside the package. It is used by subclasses within the package to initialize base data.
  108.      *
  109.      * @param type    The type of the packet.
  110.      * @param address The host the packet came from or is going to be sent.
  111.      * @param port    The port the packet came from or is going to be sent.
  112.      **/
  113.     TFTPPacket(final int type, final InetAddress address, final int port) {
  114.         this.type = type;
  115.         this.address = address;
  116.         this.port = port;
  117.     }

  118.     /**
  119.      * Returns the address of the host where the packet is going to be sent or where it came from.
  120.      *
  121.      * @return The type of the packet.
  122.      */
  123.     public final InetAddress getAddress() {
  124.         return address;
  125.     }

  126.     /**
  127.      * Returns the port where the packet is going to be sent or where it came from.
  128.      *
  129.      * @return The port where the packet came from or where it is going.
  130.      */
  131.     public final int getPort() {
  132.         return port;
  133.     }

  134.     /**
  135.      * Returns the type of the packet.
  136.      *
  137.      * @return The type of the packet.
  138.      */
  139.     public final int getType() {
  140.         return type;
  141.     }

  142.     /**
  143.      * Creates a UDP datagram containing all the TFTP packet data in the proper format. This is an abstract method, exposed to the programmer in case he wants
  144.      * to implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not
  145.      * have a need to call this method.
  146.      *
  147.      * @return A UDP datagram containing the TFTP packet.
  148.      */
  149.     public abstract DatagramPacket newDatagram();

  150.     /**
  151.      * This is an abstract method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram
  152.      * as an argument, and a byte buffer in which to store the raw datagram data. Inside the method, the data should be set as the datagram's data and the
  153.      * datagram returned.
  154.      *
  155.      * @param datagram The datagram to create.
  156.      * @param data     The buffer to store the packet and to use in the datagram.
  157.      * @return The datagram argument.
  158.      */
  159.     abstract DatagramPacket newDatagram(DatagramPacket datagram, byte[] data);

  160.     /**
  161.      * Sets the host address where the packet is going to be sent.
  162.      *
  163.      * @param address the address to set
  164.      */
  165.     public final void setAddress(final InetAddress address) {
  166.         this.address = address;
  167.     }

  168.     /**
  169.      * Sets the port where the packet is going to be sent.
  170.      *
  171.      * @param port the port to set
  172.      */
  173.     public final void setPort(final int port) {
  174.         this.port = port;
  175.     }

  176.     /**
  177.      * For debugging
  178.      *
  179.      * @since 3.6
  180.      */
  181.     @Override
  182.     public String toString() {
  183.         return address + " " + port + " " + type;
  184.     }
  185. }