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 /** The undefined error code according to RFC 783, value 0. */ 40 public static final int UNDEFINED = 0; 41 42 /** The file not found error code according to RFC 783, value 1. */ 43 public static final int FILE_NOT_FOUND = 1; 44 45 /** The access violation error code according to RFC 783, value 2. */ 46 public static final int ACCESS_VIOLATION = 2; 47 48 /** The disk full error code according to RFC 783, value 3. */ 49 public static final int OUT_OF_SPACE = 3; 50 51 /** 52 * The illegal TFTP operation error code according to RFC 783, value 4. 53 */ 54 public static final int ILLEGAL_OPERATION = 4; 55 56 /** The unknown transfer id error code according to RFC 783, value 5. */ 57 public static final int UNKNOWN_TID = 5; 58 59 /** The file already exists error code according to RFC 783, value 6. */ 60 public static final int FILE_EXISTS = 6; 61 62 /** The no such user error code according to RFC 783, value 7. */ 63 public static final int NO_SUCH_USER = 7; 64 65 /** 66 * The invalid options error code according to RFC 2347, value 8. 67 * 68 * @since 3.12.0 69 */ 70 public static final int INVALID_OPTIONS_VALUE = 8; 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 based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown. 80 * 81 * @param datagram The datagram containing the received error. 82 * @throws TFTPPacketException If the datagram isn't a valid TFTP error packet. 83 */ 84 TFTPErrorPacket(final DatagramPacket datagram) throws TFTPPacketException { 85 super(ERROR, datagram.getAddress(), datagram.getPort()); 86 int index; 87 final int length; 88 final byte[] data; 89 final StringBuilder buffer; 90 91 data = datagram.getData(); 92 length = datagram.getLength(); 93 94 if (getType() != data[1]) { 95 throw new TFTPPacketException("TFTP operator code does not match type."); 96 } 97 98 error = (data[2] & 0xff) << 8 | data[3] & 0xff; 99 100 if (length < 5) { 101 throw new TFTPPacketException("Bad error packet. No message."); 102 } 103 104 index = 4; 105 buffer = new StringBuilder(); 106 107 while (index < length && data[index] != 0) { 108 buffer.append((char) data[index]); 109 ++index; 110 } 111 112 message = buffer.toString(); 113 } 114 115 /** 116 * Creates an error packet to be sent to a host at a given port with an error code and error message. 117 * 118 * @param destination The host to which the packet is going to be sent. 119 * @param port The port to which the packet is going to be sent. 120 * @param error The error code of the packet. 121 * @param message The error message of the packet. 122 */ 123 public TFTPErrorPacket(final InetAddress destination, final int port, final int error, final String message) { 124 super(ERROR, destination, port); 125 126 this.error = error; 127 this.message = message; 128 } 129 130 /** 131 * Gets the error code of the packet. 132 * 133 * @return The error code of the packet. 134 */ 135 public int getError() { 136 return error; 137 } 138 139 /** 140 * Gets the error message of the packet. 141 * 142 * @return The error message of the packet. 143 */ 144 public String getMessage() { 145 return message; 146 } 147 148 /** 149 * 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 150 * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have 151 * a need to call this method. 152 * 153 * @return A UDP datagram containing the TFTP error packet. 154 */ 155 @Override 156 public DatagramPacket newDatagram() { 157 final byte[] data; 158 final int length; 159 160 length = message.length(); 161 162 data = new byte[length + 5]; 163 data[0] = 0; 164 data[1] = (byte) type; 165 data[2] = (byte) ((error & 0xffff) >> 8); 166 data[3] = (byte) (error & 0xff); 167 168 System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length); 169 170 data[length + 4] = 0; 171 172 return new DatagramPacket(data, data.length, address, port); 173 } 174 175 /** 176 * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an 177 * 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. 178 * 179 * @param datagram The datagram to create. 180 * @param data The buffer to store the packet and to use in the datagram. 181 * @return The datagram argument. 182 */ 183 @Override 184 DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) { 185 final int length; 186 187 length = message.length(); 188 189 data[0] = 0; 190 data[1] = (byte) type; 191 data[2] = (byte) ((error & 0xffff) >> 8); 192 data[3] = (byte) (error & 0xff); 193 194 System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length); 195 196 data[length + 4] = 0; 197 198 datagram.setAddress(address); 199 datagram.setPort(port); 200 datagram.setData(data); 201 datagram.setLength(length + 4); 202 203 return datagram; 204 } 205 206 /** 207 * For debugging 208 * 209 * @since 3.6 210 */ 211 @Override 212 public String toString() { 213 return super.toString() + " ERR " + error + " " + message; 214 } 215 }