1 16 package org.apache.commons.net.tftp; 17 18 import java.net.DatagramPacket ; 19 import java.net.InetAddress ; 20 21 44 45 public abstract class TFTPPacket 46 { 47 52 static final int MIN_PACKET_SIZE = 4; 53 54 60 public static final int READ_REQUEST = 1; 61 62 68 public static final int WRITE_REQUEST = 2; 69 70 76 public static final int DATA = 3; 77 78 84 public static final int ACKNOWLEDGEMENT = 4; 85 86 92 public static final int ERROR = 5; 93 94 100 public static final int SEGMENT_SIZE = 512; 101 102 103 int _type; 104 105 106 int _port; 107 108 109 InetAddress _address; 110 111 123 public final static TFTPPacket newTFTPPacket(DatagramPacket datagram) 124 throws TFTPPacketException 125 { 126 byte[] data; 127 TFTPPacket packet = null; 128 129 if (datagram.getLength() < MIN_PACKET_SIZE) 130 throw new TFTPPacketException( 131 "Bad packet. Datagram data length is too short."); 132 133 data = datagram.getData(); 134 135 switch (data[1]) 136 { 137 case READ_REQUEST: 138 packet = new TFTPReadRequestPacket(datagram); 139 break; 140 case WRITE_REQUEST: 141 packet = new TFTPWriteRequestPacket(datagram); 142 break; 143 case DATA: 144 packet = new TFTPDataPacket(datagram); 145 break; 146 case ACKNOWLEDGEMENT: 147 packet = new TFTPAckPacket(datagram); 148 break; 149 case ERROR: 150 packet = new TFTPErrorPacket(datagram); 151 break; 152 default: 153 throw new TFTPPacketException( 154 "Bad packet. Invalid TFTP operator code."); 155 } 156 157 return packet; 158 } 159 160 168 TFTPPacket(int type, InetAddress address, int port) 169 { 170 _type = type; 171 _address = address; 172 _port = port; 173 } 174 175 186 abstract DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data); 187 188 200 public abstract DatagramPacket newDatagram(); 201 202 207 public final int getType() 208 { 209 return _type; 210 } 211 212 218 public final InetAddress getAddress() 219 { 220 return _address; 221 } 222 223 229 public final int getPort() 230 { 231 return _port; 232 } 233 234 235 public final void setPort(int port) 236 { 237 _port = port; 238 } 239 240 241 public final void setAddress(InetAddress address) 242 { 243 _address = address; 244 } 245 } 246 | Popular Tags |