1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.tftp;
  17. import java.net.DatagramPacket;
  18. import java.net.InetAddress;
  19. /***
  20. * A final class derived from TFTPPacket definiing the TFTP Acknowledgement
  21. * packet type.
  22. * <p>
  23. * Details regarding the TFTP protocol and the format of TFTP packets can
  24. * be found in RFC 783. But the point of these classes is to keep you
  25. * from having to worry about the internals. Additionally, only very
  26. * few people should have to care about any of the TFTPPacket classes
  27. * or derived classes. Almost all users should only be concerned with the
  28. * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
  29. * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
  30. * and
  31. * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
  32. * methods.
  33. * <p>
  34. * <p>
  35. * @author Daniel F. Savarese
  36. * @see TFTPPacket
  37. * @see TFTPPacketException
  38. * @see TFTP
  39. ***/
  40. public final class TFTPAckPacket extends TFTPPacket
  41. {
  42. /*** The block number being acknowledged by the packet. ***/
  43. int _blockNumber;
  44. /***
  45. * Creates an acknowledgment packet to be sent to a host at a given port
  46. * acknowledging receipt of a block.
  47. * <p>
  48. * @param destination The host to which the packet is going to be sent.
  49. * @param port The port to which the packet is going to be sent.
  50. * @param blockNumber The block number being acknowledged.
  51. ***/
  52. public TFTPAckPacket(InetAddress destination, int port, int blockNumber)
  53. {
  54. super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
  55. _blockNumber = blockNumber;
  56. }
  57. /***
  58. * Creates an acknowledgement packet based from a received
  59. * datagram. Assumes the datagram is at least length 4, else an
  60. * ArrayIndexOutOfBoundsException may be thrown.
  61. * <p>
  62. * @param datagram The datagram containing the received acknowledgement.
  63. * @throws TFTPPacketException If the datagram isn't a valid TFTP
  64. * acknowledgement packet.
  65. ***/
  66. TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException
  67. {
  68. super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
  69. datagram.getPort());
  70. byte[] data;
  71. data = datagram.getData();
  72. if (getType() != data[1])
  73. throw new TFTPPacketException("TFTP operator code does not match type.");
  74. _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
  75. }
  76. /***
  77. * This is a method only available within the package for
  78. * implementing efficient datagram transport by elminating buffering.
  79. * It takes a datagram as an argument, and a byte buffer in which
  80. * to store the raw datagram data. Inside the method, the data
  81. * is set as the datagram's data and the datagram returned.
  82. * <p>
  83. * @param datagram The datagram to create.
  84. * @param data The buffer to store the packet and to use in the datagram.
  85. * @return The datagram argument.
  86. ***/
  87. DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
  88. {
  89. data[0] = 0;
  90. data[1] = (byte)_type;
  91. data[2] = (byte)((_blockNumber & 0xffff) >> 8);
  92. data[3] = (byte)(_blockNumber & 0xff);
  93. datagram.setAddress(_address);
  94. datagram.setPort(_port);
  95. datagram.setData(data);
  96. datagram.setLength(4);
  97. return datagram;
  98. }
  99. /***
  100. * Creates a UDP datagram containing all the TFTP
  101. * acknowledgement packet data in the proper format.
  102. * This is a method exposed to the programmer in case he
  103. * wants to implement his own TFTP client instead of using
  104. * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
  105. * class. Under normal circumstances, you should not have a need to call this
  106. * method.
  107. * <p>
  108. * @return A UDP datagram containing the TFTP acknowledgement packet.
  109. ***/
  110. public DatagramPacket newDatagram()
  111. {
  112. byte[] data;
  113. data = new byte[4];
  114. data[0] = 0;
  115. data[1] = (byte)_type;
  116. data[2] = (byte)((_blockNumber & 0xffff) >> 8);
  117. data[3] = (byte)(_blockNumber & 0xff);
  118. return new DatagramPacket(data, data.length, _address, _port);
  119. }
  120. /***
  121. * Returns the block number of the acknowledgement.
  122. * <p>
  123. * @return The block number of the acknowledgement.
  124. ***/
  125. public int getBlockNumber()
  126. {
  127. return _blockNumber;
  128. }
  129. /*** Sets the block number of the acknowledgement. ***/
  130. public void setBlockNumber(int blockNumber)
  131. {
  132. _blockNumber = blockNumber;
  133. }
  134. }