View Javadoc

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    *      http://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  
23  /***
24   * An abstract class derived from TFTPPacket definiing a TFTP Request
25   * packet type.  It is subclassed by the
26   * {@link org.apache.commons.net.tftp.TFTPReadRequestPacket}
27   *   and
28   * {@link org.apache.commons.net.tftp.TFTPWriteRequestPacket}
29   *  classes.
30   * <p>
31   * Details regarding the TFTP protocol and the format of TFTP packets can
32   * be found in RFC 783.  But the point of these classes is to keep you
33   * from having to worry about the internals.  Additionally, only very
34   * few people should have to care about any of the TFTPPacket classes
35   * or derived classes.  Almost all users should only be concerned with the
36   * {@link org.apache.commons.net.tftp.TFTPClient} class
37   * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
38   * and
39   * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
40   * methods.
41   * <p>
42   * <p>
43   * @author Daniel F. Savarese
44   * @see TFTPPacket
45   * @see TFTPReadRequestPacket
46   * @see TFTPWriteRequestPacket
47   * @see TFTPPacketException
48   * @see TFTP
49   ***/
50  
51  public abstract class TFTPRequestPacket extends TFTPPacket
52  {
53      /***
54       * An array containing the string names of the transfer modes and indexed
55       * by the transfer mode constants.
56       ***/
57      static final String[] _modeStrings = { "netascii", "octet" };
58  
59      /***
60       * A null terminated byte array representation of the ascii names of the
61       * transfer mode constants.  This is convenient for creating the TFTP
62       * request packets.
63       ***/
64      static final byte[] _modeBytes[] = {
65                                             { (byte)'n', (byte)'e', (byte)'t', (byte)'a', (byte)'s', (byte)'c',
66                                               (byte)'i', (byte)'i', 0 },
67                                             { (byte)'o', (byte)'c', (byte)'t', (byte)'e', (byte)'t', 0 }
68                                         };
69  
70      /*** The transfer mode of the request. ***/
71      int _mode;
72  
73      /*** The filename of the request. ***/
74      String _filename;
75  
76      /***
77       * Creates a request packet of a given type to be sent to a host at a
78       * given port with a filename and transfer mode request.
79       * <p>
80       * @param destination  The host to which the packet is going to be sent.
81       * @param port  The port to which the packet is going to be sent.
82       * @param type The type of the request (either TFTPPacket.READ_REQUEST or
83       *             TFTPPacket.WRITE_REQUEST).
84       * @param filename The requested filename.
85       * @param mode The requested transfer mode.  This should be on of the TFTP
86       *        class MODE constants (e.g., TFTP.NETASCII_MODE).
87       ***/
88      TFTPRequestPacket(InetAddress destination, int port,
89                        int type, String filename, int mode)
90      {
91          super(type, destination, port);
92  
93          _filename = filename;
94          _mode = mode;
95      }
96  
97      /***
98       * Creates a request packet of a given type based on a received
99       * datagram.  Assumes the datagram is at least length 4, else an
100      * ArrayIndexOutOfBoundsException may be thrown.
101      * <p>
102      * @param type The type of the request (either TFTPPacket.READ_REQUEST or
103      *             TFTPPacket.WRITE_REQUEST).
104      * @param datagram  The datagram containing the received request.
105      * @throws TFTPPacketException  If the datagram isn't a valid TFTP
106      *         request packet of the appropriate type.
107      ***/
108     TFTPRequestPacket(int type, DatagramPacket datagram)
109     throws TFTPPacketException
110     {
111         super(type, datagram.getAddress(), datagram.getPort());
112 
113         byte[] data;
114         int index, length;
115         String mode;
116         StringBuffer buffer;
117 
118         data = datagram.getData();
119 
120         if (getType() != data[1])
121             throw new TFTPPacketException("TFTP operator code does not match type.");
122 
123         buffer = new StringBuffer();
124 
125         index = 2;
126         length = datagram.getLength();
127 
128         while (index < length && data[index] != 0)
129         {
130             buffer.append((char)data[index]);
131             ++index;
132         }
133 
134         _filename = buffer.toString();
135 
136         if (index >= length)
137             throw new TFTPPacketException("Bad filename and mode format.");
138 
139         buffer.setLength(0);
140         ++index; // need to advance beyond the end of string marker
141         while (index < length && data[index] != 0)
142         {
143             buffer.append((char)data[index]);
144             ++index;
145         }
146 
147         mode = buffer.toString().toLowerCase(java.util.Locale.ENGLISH);
148         length = _modeStrings.length;
149 
150         for (index = 0; index < length; index++)
151         {
152             if (mode.equals(_modeStrings[index]))
153             {
154                 _mode = index;
155                 break;
156             }
157         }
158 
159         if (index >= length)
160         {
161             throw new TFTPPacketException("Unrecognized TFTP transfer mode: " + mode);
162             // May just want to default to binary mode instead of throwing
163             // exception.
164             //_mode = TFTP.OCTET_MODE;
165         }
166     }
167 
168 
169     /***
170      * This is a method only available within the package for
171      * implementing efficient datagram transport by elminating buffering.
172      * It takes a datagram as an argument, and a byte buffer in which
173      * to store the raw datagram data.  Inside the method, the data
174      * is set as the datagram's data and the datagram returned.
175      * <p>
176      * @param datagram  The datagram to create.
177      * @param data The buffer to store the packet and to use in the datagram.
178      * @return The datagram argument.
179      ***/
180     @Override
181     final DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
182     {
183         int fileLength, modeLength;
184 
185         fileLength = _filename.length();
186         modeLength = _modeBytes[_mode].length;
187 
188         data[0] = 0;
189         data[1] = (byte)_type;
190         System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength);
191         data[fileLength + 2] = 0;
192         System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3,
193                          modeLength);
194 
195         datagram.setAddress(_address);
196         datagram.setPort(_port);
197         datagram.setData(data);
198         datagram.setLength(fileLength + modeLength + 3);
199         
200         return datagram;
201     }
202 
203     /***
204      * Creates a UDP datagram containing all the TFTP
205      * request packet data in the proper format.
206      * This is a method exposed to the programmer in case he
207      * wants to implement his own TFTP client instead of using
208      * the {@link org.apache.commons.net.tftp.TFTPClient}
209      * class.  Under normal circumstances, you should not have a need to call
210      * this method.
211      * <p>
212      * @return A UDP datagram containing the TFTP request packet.
213      ***/
214     @Override
215     public final DatagramPacket newDatagram()
216     {
217         int fileLength, modeLength;
218         byte[] data;
219 
220         fileLength = _filename.length();
221         modeLength = _modeBytes[_mode].length;
222 
223         data = new byte[fileLength + modeLength + 4];
224         data[0] = 0;
225         data[1] = (byte)_type;
226         System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength);
227         data[fileLength + 2] = 0;
228         System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3,
229                          modeLength);
230 
231         return new DatagramPacket(data, data.length, _address, _port);
232     }
233 
234     /***
235      * Returns the transfer mode of the request.
236      * <p>
237      * @return The transfer mode of the request.
238      ***/
239     public final int getMode()
240     {
241         return _mode;
242     }
243 
244     /***
245      * Returns the requested filename.
246      * <p>
247      * @return The requested filename.
248      ***/
249     public final String getFilename()
250     {
251         return _filename;
252     }
253 }