View Javadoc
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  
18  package org.apache.commons.net.tftp;
19  
20  import java.net.DatagramPacket;
21  import java.net.InetAddress;
22  
23  /**
24   * A final class derived from TFTPPacket definiing the TFTP Error
25   * packet type.
26   * <p>
27   * Details regarding the TFTP protocol and the format of TFTP packets can
28   * be found in RFC 783.  But the point of these classes is to keep you
29   * from having to worry about the internals.  Additionally, only very
30   * few people should have to care about any of the TFTPPacket classes
31   * or derived classes.  Almost all users should only be concerned with the
32   * {@link org.apache.commons.net.tftp.TFTPClient} class
33   * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
34   * and
35   * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
36   * methods.
37   *
38   *
39   * @see TFTPPacket
40   * @see TFTPPacketException
41   * @see TFTP
42   */
43  
44  public final class TFTPErrorPacket extends TFTPPacket
45  {
46      /** The undefined error code according to RFC 783, value 0. */
47      public static final int UNDEFINED = 0;
48  
49      /** The file not found error code according to RFC 783, value 1. */
50      public static final int FILE_NOT_FOUND = 1;
51  
52      /** The access violation error code according to RFC 783, value 2. */
53      public static final int ACCESS_VIOLATION = 2;
54  
55      /** The disk full error code according to RFC 783, value 3. */
56      public static final int OUT_OF_SPACE = 3;
57  
58      /**
59       * The illegal TFTP operation error code according to RFC 783, value 4.
60       */
61      public static final int ILLEGAL_OPERATION = 4;
62  
63      /** The unknown transfer id error code according to RFC 783, value 5. */
64      public static final int UNKNOWN_TID = 5;
65  
66      /** The file already exists error code according to RFC 783, value 6. */
67      public static final int FILE_EXISTS = 6;
68  
69      /** The no such user error code according to RFC 783, value 7. */
70      public static final int NO_SUCH_USER = 7;
71  
72      /** The error code of this packet. */
73      private final int error;
74  
75      /** The error message of this packet. */
76      private final String message;
77  
78      /**
79       * Creates an error packet to be sent to a host at a given port
80       * with an error code and error message.
81       *
82       * @param destination  The host to which the packet is going to be sent.
83       * @param port  The port to which the packet is going to be sent.
84       * @param error The error code of the packet.
85       * @param message The error message of the packet.
86       */
87      public TFTPErrorPacket(final InetAddress destination, final int port,
88                             final int error, final String message)
89      {
90          super(TFTPPacket.ERROR, destination, port);
91  
92          this.error = error;
93          this.message = message;
94      }
95  
96      /**
97       * Creates an error packet based from a received
98       * datagram.  Assumes the datagram is at least length 4, else an
99       * ArrayIndexOutOfBoundsException may be thrown.
100      *
101      * @param datagram  The datagram containing the received error.
102      * @throws TFTPPacketException  If the datagram isn't a valid TFTP
103      *         error packet.
104      */
105     TFTPErrorPacket(final DatagramPacket datagram) throws TFTPPacketException
106     {
107         super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort());
108         int index;
109         final int length;
110         final byte[] data;
111         final StringBuilder buffer;
112 
113         data = datagram.getData();
114         length = datagram.getLength();
115 
116         if (getType() != data[1]) {
117             throw new TFTPPacketException("TFTP operator code does not match type.");
118         }
119 
120         error = (data[2] & 0xff) << 8 | data[3] & 0xff;
121 
122         if (length < 5) {
123             throw new TFTPPacketException("Bad error packet. No message.");
124         }
125 
126         index = 4;
127         buffer = new StringBuilder();
128 
129         while (index < length && data[index] != 0)
130         {
131             buffer.append((char)data[index]);
132             ++index;
133         }
134 
135         message = buffer.toString();
136     }
137 
138     /**
139      * This is a method only available within the package for
140      * implementing efficient datagram transport by elminating buffering.
141      * It takes a datagram as an argument, and a byte buffer in which
142      * to store the raw datagram data.  Inside the method, the data
143      * is set as the datagram's data and the datagram returned.
144      *
145      * @param datagram  The datagram to create.
146      * @param data The buffer to store the packet and to use in the datagram.
147      * @return The datagram argument.
148      */
149     @Override
150     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data)
151     {
152         final int length;
153 
154         length = message.length();
155 
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(), 0, data, 4, length);
162 
163         data[length + 4] = 0;
164 
165         datagram.setAddress(address);
166         datagram.setPort(port);
167         datagram.setData(data);
168         datagram.setLength(length + 4);
169 
170         return datagram;
171     }
172 
173 
174     /**
175      * Creates a UDP datagram containing all the TFTP
176      * error packet data in the proper format.
177      * This is a method exposed to the programmer in case he
178      * wants to implement his own TFTP client instead of using
179      * the {@link org.apache.commons.net.tftp.TFTPClient}
180      * class.
181      * Under normal circumstances, you should not have a need to call this
182      * method.
183      *
184      * @return A UDP datagram containing the TFTP error packet.
185      */
186     @Override
187     public DatagramPacket newDatagram()
188     {
189         final byte[] data;
190         final int length;
191 
192         length = message.length();
193 
194         data = new byte[length + 5];
195         data[0] = 0;
196         data[1] = (byte)type;
197         data[2] = (byte)((error & 0xffff) >> 8);
198         data[3] = (byte)(error & 0xff);
199 
200         System.arraycopy(message.getBytes(), 0, data, 4, length);
201 
202         data[length + 4] = 0;
203 
204         return new DatagramPacket(data, data.length, address, port);
205     }
206 
207 
208     /**
209      * Returns the error code of the packet.
210      *
211      * @return The error code of the packet.
212      */
213     public int getError()
214     {
215         return error;
216     }
217 
218 
219     /**
220      * Returns the error message of the packet.
221      *
222      * @return The error message of the packet.
223      */
224     public String getMessage()
225     {
226         return message;
227     }
228 
229     /**
230      * For debugging
231      * @since 3.6
232      */
233     @Override
234     public String toString() {
235         return super.toString() + " ERR " + error + " " + message;
236     }
237 }