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   * <p>
38   * <p>
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      int _error;
74  
75      /*** The error message of this packet. ***/
76      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       * <p>
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(InetAddress destination, int port,
88                             int error, String message)
89      {
90          super(TFTPPacket.ERROR, destination, port);
91  
92          _error = error;
93          _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      * <p>
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(DatagramPacket datagram) throws TFTPPacketException
106     {
107         super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort());
108         int index, length;
109         byte[] data;
110         StringBuilder buffer;
111 
112         data = datagram.getData();
113         length = datagram.getLength();
114 
115         if (getType() != data[1]) {
116             throw new TFTPPacketException("TFTP operator code does not match type.");
117         }
118 
119         _error = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
120 
121         if (length < 5) {
122             throw new TFTPPacketException("Bad error packet. No message.");
123         }
124 
125         index = 4;
126         buffer = new StringBuilder();
127 
128         while (index < length && data[index] != 0)
129         {
130             buffer.append((char)data[index]);
131             ++index;
132         }
133 
134         _message = buffer.toString();
135     }
136 
137     /***
138      * This is a method only available within the package for
139      * implementing efficient datagram transport by elminating buffering.
140      * It takes a datagram as an argument, and a byte buffer in which
141      * to store the raw datagram data.  Inside the method, the data
142      * is set as the datagram's data and the datagram returned.
143      * <p>
144      * @param datagram  The datagram to create.
145      * @param data The buffer to store the packet and to use in the datagram.
146      * @return The datagram argument.
147      ***/
148     @Override
149     DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
150     {
151         int length;
152 
153         length = _message.length();
154 
155         data[0] = 0;
156         data[1] = (byte)_type;
157         data[2] = (byte)((_error & 0xffff) >> 8);
158         data[3] = (byte)(_error & 0xff);
159 
160         System.arraycopy(_message.getBytes(), 0, data, 4, length);
161 
162         data[length + 4] = 0;
163 
164         datagram.setAddress(_address);
165         datagram.setPort(_port);
166         datagram.setData(data);
167         datagram.setLength(length + 4);
168 
169         return datagram;
170     }
171 
172 
173     /***
174      * Creates a UDP datagram containing all the TFTP
175      * error packet data in the proper format.
176      * This is a method exposed to the programmer in case he
177      * wants to implement his own TFTP client instead of using
178      * the {@link org.apache.commons.net.tftp.TFTPClient}
179      * class.
180      * Under normal circumstances, you should not have a need to call this
181      * method.
182      * <p>
183      * @return A UDP datagram containing the TFTP error packet.
184      ***/
185     @Override
186     public DatagramPacket newDatagram()
187     {
188         byte[] data;
189         int length;
190 
191         length = _message.length();
192 
193         data = new byte[length + 5];
194         data[0] = 0;
195         data[1] = (byte)_type;
196         data[2] = (byte)((_error & 0xffff) >> 8);
197         data[3] = (byte)(_error & 0xff);
198 
199         System.arraycopy(_message.getBytes(), 0, data, 4, length);
200 
201         data[length + 4] = 0;
202 
203         return new DatagramPacket(data, data.length, _address, _port);
204     }
205 
206 
207     /***
208      * Returns the error code of the packet.
209      * <p>
210      * @return The error code of the packet.
211      ***/
212     public int getError()
213     {
214         return _error;
215     }
216 
217 
218     /***
219      * Returns the error message of the packet.
220      * <p>
221      * @return The error message of the packet.
222      ***/
223     public String getMessage()
224     {
225         return _message;
226     }
227 }