001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.net.tftp;
019
020 import java.net.DatagramPacket;
021 import java.net.InetAddress;
022
023 /***
024 * A final class derived from TFTPPacket definiing the TFTP Acknowledgement
025 * packet type.
026 * <p>
027 * Details regarding the TFTP protocol and the format of TFTP packets can
028 * be found in RFC 783. But the point of these classes is to keep you
029 * from having to worry about the internals. Additionally, only very
030 * few people should have to care about any of the TFTPPacket classes
031 * or derived classes. Almost all users should only be concerned with the
032 * {@link org.apache.commons.net.tftp.TFTPClient} class
033 * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
034 * and
035 * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
036 * methods.
037 * <p>
038 * <p>
039 * @see TFTPPacket
040 * @see TFTPPacketException
041 * @see TFTP
042 ***/
043
044 public final class TFTPAckPacket extends TFTPPacket
045 {
046 /*** The block number being acknowledged by the packet. ***/
047 int _blockNumber;
048
049 /***
050 * Creates an acknowledgment packet to be sent to a host at a given port
051 * acknowledging receipt of a block.
052 * <p>
053 * @param destination The host to which the packet is going to be sent.
054 * @param port The port to which the packet is going to be sent.
055 * @param blockNumber The block number being acknowledged.
056 ***/
057 public TFTPAckPacket(InetAddress destination, int port, int blockNumber)
058 {
059 super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
060 _blockNumber = blockNumber;
061 }
062
063 /***
064 * Creates an acknowledgement packet based from a received
065 * datagram. Assumes the datagram is at least length 4, else an
066 * ArrayIndexOutOfBoundsException may be thrown.
067 * <p>
068 * @param datagram The datagram containing the received acknowledgement.
069 * @throws TFTPPacketException If the datagram isn't a valid TFTP
070 * acknowledgement packet.
071 ***/
072 TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException
073 {
074 super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
075 datagram.getPort());
076 byte[] data;
077
078 data = datagram.getData();
079
080 if (getType() != data[1]) {
081 throw new TFTPPacketException("TFTP operator code does not match type.");
082 }
083
084 _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
085 }
086
087 /***
088 * This is a method only available within the package for
089 * implementing efficient datagram transport by elminating buffering.
090 * It takes a datagram as an argument, and a byte buffer in which
091 * to store the raw datagram data. Inside the method, the data
092 * is set as the datagram's data and the datagram returned.
093 * <p>
094 * @param datagram The datagram to create.
095 * @param data The buffer to store the packet and to use in the datagram.
096 * @return The datagram argument.
097 ***/
098 @Override
099 DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
100 {
101 data[0] = 0;
102 data[1] = (byte)_type;
103 data[2] = (byte)((_blockNumber & 0xffff) >> 8);
104 data[3] = (byte)(_blockNumber & 0xff);
105
106 datagram.setAddress(_address);
107 datagram.setPort(_port);
108 datagram.setData(data);
109 datagram.setLength(4);
110
111 return datagram;
112 }
113
114
115 /***
116 * Creates a UDP datagram containing all the TFTP
117 * acknowledgement packet data in the proper format.
118 * This is a method exposed to the programmer in case he
119 * wants to implement his own TFTP client instead of using
120 * the {@link org.apache.commons.net.tftp.TFTPClient}
121 * class. Under normal circumstances, you should not have a need to call this
122 * method.
123 * <p>
124 * @return A UDP datagram containing the TFTP acknowledgement packet.
125 ***/
126 @Override
127 public DatagramPacket newDatagram()
128 {
129 byte[] data;
130
131 data = new byte[4];
132 data[0] = 0;
133 data[1] = (byte)_type;
134 data[2] = (byte)((_blockNumber & 0xffff) >> 8);
135 data[3] = (byte)(_blockNumber & 0xff);
136
137 return new DatagramPacket(data, data.length, _address, _port);
138 }
139
140
141 /***
142 * Returns the block number of the acknowledgement.
143 * <p>
144 * @return The block number of the acknowledgement.
145 ***/
146 public int getBlockNumber()
147 {
148 return _blockNumber;
149 }
150
151
152 /*** Sets the block number of the acknowledgement. ***/
153 public void setBlockNumber(int blockNumber)
154 {
155 _blockNumber = blockNumber;
156 }
157 }
158