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 * https://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 import java.nio.charset.Charset;
23
24 /**
25 * A final class derived from TFTPPacket defining the TFTP Error packet type.
26 * <p>
27 * 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
28 * 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
29 * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
30 * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
31 * </p>
32 *
33 * @see TFTPPacket
34 * @see TFTPPacketException
35 * @see TFTP
36 */
37
38 public final class TFTPErrorPacket extends TFTPPacket {
39
40 /** The undefined error code according to RFC 783, value 0. */
41 public static final int UNDEFINED = 0;
42
43 /** The file not found error code according to RFC 783, value 1. */
44 public static final int FILE_NOT_FOUND = 1;
45
46 /** The access violation error code according to RFC 783, value 2. */
47 public static final int ACCESS_VIOLATION = 2;
48
49 /** The disk full error code according to RFC 783, value 3. */
50 public static final int OUT_OF_SPACE = 3;
51
52 /**
53 * The illegal TFTP operation error code according to RFC 783, value 4.
54 */
55 public static final int ILLEGAL_OPERATION = 4;
56
57 /** The unknown transfer id error code according to RFC 783, value 5. */
58 public static final int UNKNOWN_TID = 5;
59
60 /** The file already exists error code according to RFC 783, value 6. */
61 public static final int FILE_EXISTS = 6;
62
63 /** The no such user error code according to RFC 783, value 7. */
64 public static final int NO_SUCH_USER = 7;
65
66 /**
67 * The invalid options error code according to RFC 2347, value 8.
68 *
69 * @since 3.12.0
70 */
71 public static final int INVALID_OPTIONS_VALUE = 8;
72
73 /** The error code of this packet. */
74 private final int error;
75
76 /** The error message of this packet. */
77 private final String message;
78
79 /**
80 * Creates an error packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
81 *
82 * @param datagram The datagram containing the received error.
83 * @throws TFTPPacketException If the datagram isn't a valid TFTP error packet.
84 */
85 TFTPErrorPacket(final DatagramPacket datagram) throws TFTPPacketException {
86 super(ERROR, datagram.getAddress(), datagram.getPort());
87 int index;
88 final int length;
89 final byte[] data;
90 final StringBuilder buffer;
91
92 data = datagram.getData();
93 length = datagram.getLength();
94
95 if (getType() != data[1]) {
96 throw new TFTPPacketException("TFTP operator code does not match type.");
97 }
98
99 error = (data[2] & 0xff) << 8 | data[3] & 0xff;
100
101 if (length < 5) {
102 throw new TFTPPacketException("Bad error packet. No message.");
103 }
104
105 index = 4;
106 buffer = new StringBuilder();
107
108 while (index < length && data[index] != 0) {
109 buffer.append((char) data[index]);
110 ++index;
111 }
112
113 message = buffer.toString();
114 }
115
116 /**
117 * Creates an error packet to be sent to a host at a given port with an error code and error message.
118 *
119 * @param destination The host to which the packet is going to be sent.
120 * @param port The port to which the packet is going to be sent.
121 * @param error The error code of the packet.
122 * @param message The error message of the packet.
123 */
124 public TFTPErrorPacket(final InetAddress destination, final int port, final int error, final String message) {
125 super(ERROR, destination, port);
126
127 this.error = error;
128 this.message = message;
129 }
130
131 /**
132 * Gets the error code of the packet.
133 *
134 * @return The error code of the packet.
135 */
136 public int getError() {
137 return error;
138 }
139
140 /**
141 * Gets the error message of the packet.
142 *
143 * @return The error message of the packet.
144 */
145 public String getMessage() {
146 return message;
147 }
148
149 /**
150 * Creates a UDP datagram containing all the TFTP error packet data in the proper format. This is a method exposed to the programmer in case he wants to
151 * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
152 * a need to call this method.
153 *
154 * @return A UDP datagram containing the TFTP error packet.
155 */
156 @Override
157 public DatagramPacket newDatagram() {
158 final byte[] data;
159 final int length;
160
161 length = message.length();
162
163 data = new byte[length + 5];
164 data[0] = 0;
165 data[1] = (byte) type;
166 data[2] = (byte) ((error & 0xffff) >> 8);
167 data[3] = (byte) (error & 0xff);
168
169 System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length);
170
171 data[length + 4] = 0;
172
173 return new DatagramPacket(data, data.length, address, port);
174 }
175
176 /**
177 * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
178 * argument, and a byte buffer in which to store the raw datagram data. Inside the method, the data is set as the datagram's data and the datagram returned.
179 *
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 @Override
185 DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
186 final 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(Charset.defaultCharset()), 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 * For debugging
209 *
210 * @since 3.6
211 */
212 @Override
213 public String toString() {
214 return super.toString() + " ERR " + error + " " + message;
215 }
216 }