1 16 package org.apache.commons.net.tftp; 17 18 import java.net.DatagramPacket ; 19 import java.net.InetAddress ; 20 21 48 49 public abstract class TFTPRequestPacket extends TFTPPacket 50 { 51 55 static final String [] _modeStrings = { "netascii", "octet" }; 56 57 62 static final byte[] _modeBytes[] = { 63 { (byte)'n', (byte)'e', (byte)'t', (byte)'a', (byte)'s', (byte)'c', 64 (byte)'i', (byte)'i', 0 }, 65 { (byte)'o', (byte)'c', (byte)'t', (byte)'e', (byte)'t', 0 } 66 }; 67 68 69 int _mode; 70 71 72 String _filename; 73 74 86 TFTPRequestPacket(InetAddress destination, int port, 87 int type, String filename, int mode) 88 { 89 super(type, destination, port); 90 91 _filename = filename; 92 _mode = mode; 93 } 94 95 106 TFTPRequestPacket(int type, DatagramPacket datagram) 107 throws TFTPPacketException 108 { 109 super(type, datagram.getAddress(), datagram.getPort()); 110 111 byte[] data; 112 int index, length; 113 String mode; 114 StringBuffer buffer; 115 116 data = datagram.getData(); 117 118 if (getType() != data[1]) 119 throw new TFTPPacketException("TFTP operator code does not match type."); 120 121 buffer = new StringBuffer (); 122 123 index = 2; 124 length = datagram.getLength(); 125 126 while (index < length && data[index] != 0) 127 { 128 buffer.append((char)data[index]); 129 ++index; 130 } 131 132 _filename = buffer.toString(); 133 134 if (index >= length) 135 throw new TFTPPacketException("Bad filename and mode format."); 136 137 buffer.setLength(0); 138 ++index; while (index < length && data[index] != 0) 140 { 141 buffer.append((char)data[index]); 142 ++index; 143 } 144 145 mode = buffer.toString().toLowerCase(); 146 length = _modeStrings.length; 147 148 for (index = 0; index < length; index++) 149 { 150 if (mode.equals(_modeStrings[index])) 151 { 152 _mode = index; 153 break; 154 } 155 } 156 157 if (index >= length) 158 { 159 throw new TFTPPacketException("Unrecognized TFTP transfer mode: " + mode); 160 } 164 } 165 166 167 178 final DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) 179 { 180 int fileLength, modeLength; 181 182 fileLength = _filename.length(); 183 modeLength = _modeBytes[_mode].length; 184 185 data[0] = 0; 186 data[1] = (byte)_type; 187 System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength); 188 data[fileLength + 2] = 0; 189 System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3, 190 modeLength); 191 192 datagram.setAddress(_address); 193 datagram.setPort(_port); 194 datagram.setData(data); 195 datagram.setLength(fileLength + modeLength + 3); 196 197 return datagram; 198 } 199 200 211 public final DatagramPacket newDatagram() 212 { 213 int fileLength, modeLength; 214 byte[] data; 215 216 fileLength = _filename.length(); 217 modeLength = _modeBytes[_mode].length; 218 219 data = new byte[fileLength + modeLength + 4]; 220 data[0] = 0; 221 data[1] = (byte)_type; 222 System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength); 223 data[fileLength + 2] = 0; 224 System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3, 225 modeLength); 226 227 return new DatagramPacket (data, data.length, _address, _port); 228 } 229 230 235 public final int getMode() 236 { 237 return _mode; 238 } 239 240 245 public final String getFilename() 246 { 247 return _filename; 248 } 249 } 250 | Popular Tags |