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. * TFTPPacket is an abstract class encapsulating the functionality common
  21. * to the 5 types of TFTP packets. It also provides a static factory
  22. * method that will create the correct TFTP packet instance from a
  23. * datagram. This relieves the programmer from having to figure out what
  24. * kind of TFTP packet is contained in a datagram and create it himself.
  25. * <p>
  26. * Details regarding the TFTP protocol and the format of TFTP packets can
  27. * be found in RFC 783. But the point of these classes is to keep you
  28. * from having to worry about the internals. Additionally, only very
  29. * few people should have to care about any of the TFTPPacket classes
  30. * or derived classes. Almost all users should only be concerned with the
  31. * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
  32. * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
  33. * and
  34. * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
  35. * methods.
  36. * <p>
  37. * <p>
  38. * @author Daniel F. Savarese
  39. * @see TFTPPacketException
  40. * @see TFTP
  41. ***/
  42. public abstract class TFTPPacket
  43. {
  44. /***
  45. * The minimum size of a packet. This is 4 bytes. It is enough
  46. * to store the opcode and blocknumber or other required data
  47. * depending on the packet type.
  48. ***/
  49. static final int MIN_PACKET_SIZE = 4;
  50. /***
  51. * This is the actual TFTP spec
  52. * identifier and is equal to 1.
  53. * Identifier returned by <a href="#getType">getType()</a>
  54. * indicating a read request packet.
  55. ***/
  56. public static final int READ_REQUEST = 1;
  57. /***
  58. * This is the actual TFTP spec
  59. * identifier and is equal to 2.
  60. * Identifier returned by <a href="#getType">getType()</a>
  61. * indicating a write request packet.
  62. ***/
  63. public static final int WRITE_REQUEST = 2;
  64. /***
  65. * This is the actual TFTP spec
  66. * identifier and is equal to 3.
  67. * Identifier returned by <a href="#getType">getType()</a>
  68. * indicating a data packet.
  69. ***/
  70. public static final int DATA = 3;
  71. /***
  72. * This is the actual TFTP spec
  73. * identifier and is equal to 4.
  74. * Identifier returned by <a href="#getType">getType()</a>
  75. * indicating an acknowledgement packet.
  76. ***/
  77. public static final int ACKNOWLEDGEMENT = 4;
  78. /***
  79. * This is the actual TFTP spec
  80. * identifier and is equal to 5.
  81. * Identifier returned by <a href="#getType">getType()</a>
  82. * indicating an error packet.
  83. ***/
  84. public static final int ERROR = 5;
  85. /***
  86. * The TFTP data packet maximum segment size in bytes. This is 512
  87. * and is useful for those familiar with the TFTP protocol who want
  88. * to use the <a href="org.apache.commons.net.tftp.TFTP.html#_top_">TFTP</a>
  89. * class methods to implement their own TFTP servers or clients.
  90. ***/
  91. public static final int SEGMENT_SIZE = 512;
  92. /*** The type of packet. ***/
  93. int _type;
  94. /*** The port the packet came from or is going to. ***/
  95. int _port;
  96. /*** The host the packet is going to be sent or where it came from. ***/
  97. InetAddress _address;
  98. /***
  99. * When you receive a datagram that you expect to be a TFTP packet, you use
  100. * this factory method to create the proper TFTPPacket object
  101. * encapsulating the data contained in that datagram. This method is the
  102. * only way you can instantiate a TFTPPacket derived class from a
  103. * datagram.
  104. * <p>
  105. * @param datagram The datagram containing a TFTP packet.
  106. * @return The TFTPPacket object corresponding to the datagram.
  107. * @exception TFTPPacketException If the datagram does not contain a valid
  108. * TFTP packet.
  109. ***/
  110. public final static TFTPPacket newTFTPPacket(DatagramPacket datagram)
  111. throws TFTPPacketException
  112. {
  113. byte[] data;
  114. TFTPPacket packet = null;
  115. if (datagram.getLength() < MIN_PACKET_SIZE)
  116. throw new TFTPPacketException(
  117. "Bad packet. Datagram data length is too short.");
  118. data = datagram.getData();
  119. switch (data[1])
  120. {
  121. case READ_REQUEST:
  122. packet = new TFTPReadRequestPacket(datagram);
  123. break;
  124. case WRITE_REQUEST:
  125. packet = new TFTPWriteRequestPacket(datagram);
  126. break;
  127. case DATA:
  128. packet = new TFTPDataPacket(datagram);
  129. break;
  130. case ACKNOWLEDGEMENT:
  131. packet = new TFTPAckPacket(datagram);
  132. break;
  133. case ERROR:
  134. packet = new TFTPErrorPacket(datagram);
  135. break;
  136. default:
  137. throw new TFTPPacketException(
  138. "Bad packet. Invalid TFTP operator code.");
  139. }
  140. return packet;
  141. }
  142. /***
  143. * This constructor is not visible outside of the package. It is used
  144. * by subclasses within the package to initialize base data.
  145. * <p>
  146. * @param type The type of the packet.
  147. * @param address The host the packet came from or is going to be sent.
  148. * @param port The port the packet came from or is going to be sent.
  149. **/
  150. TFTPPacket(int type, InetAddress address, int port)
  151. {
  152. _type = type;
  153. _address = address;
  154. _port = port;
  155. }
  156. /***
  157. * This is an abstract method only available within the package for
  158. * implementing efficient datagram transport by elminating buffering.
  159. * It takes a datagram as an argument, and a byte buffer in which
  160. * to store the raw datagram data. Inside the method, the data
  161. * should be set as the datagram's data and the datagram returned.
  162. * <p>
  163. * @param datagram The datagram to create.
  164. * @param data The buffer to store the packet and to use in the datagram.
  165. * @return The datagram argument.
  166. ***/
  167. abstract DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data);
  168. /***
  169. * Creates a UDP datagram containing all the TFTP packet
  170. * data in the proper format.
  171. * This is an abstract method, exposed to the programmer in case he
  172. * wants to implement his own TFTP client instead of using
  173. * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
  174. * class.
  175. * Under normal circumstances, you should not have a need to call this
  176. * method.
  177. * <p>
  178. * @return A UDP datagram containing the TFTP packet.
  179. ***/
  180. public abstract DatagramPacket newDatagram();
  181. /***
  182. * Returns the type of the packet.
  183. * <p>
  184. * @return The type of the packet.
  185. ***/
  186. public final int getType()
  187. {
  188. return _type;
  189. }
  190. /***
  191. * Returns the address of the host where the packet is going to be sent
  192. * or where it came from.
  193. * <p>
  194. * @return The type of the packet.
  195. ***/
  196. public final InetAddress getAddress()
  197. {
  198. return _address;
  199. }
  200. /***
  201. * Returns the port where the packet is going to be sent
  202. * or where it came from.
  203. * <p>
  204. * @return The port where the packet came from or where it is going.
  205. ***/
  206. public final int getPort()
  207. {
  208. return _port;
  209. }
  210. /*** Sets the port where the packet is going to be sent. ***/
  211. public final void setPort(int port)
  212. {
  213. _port = port;
  214. }
  215. /*** Sets the host address where the packet is going to be sent. ***/
  216. public final void setAddress(InetAddress address)
  217. {
  218. _address = address;
  219. }
  220. }