int type = in.read() & 0x0ff;
int identifier = in.read() & 0x0ff;
int length = (in.read() & 0x0ff) << 8 | (in.read() & 0x0ff);
if (request != null && request.getPacketIdentifier() != identifier)
throw new RadiusException("bad packet: invalid packet identifier (request: " + request.getPacketIdentifier() + ", response: " + identifier);
if (length < RADIUS_HEADER_LENGTH)
throw new RadiusException("bad packet: packet too short (" + length + " bytes)");
if (length > MAX_PACKET_LENGTH)
throw new RadiusException("bad packet: packet too long (" + length + " bytes)");
// read rest of packet
byte[] authenticator = new byte[16];
byte[] attributeData = new byte[length - RADIUS_HEADER_LENGTH];
in.read(authenticator);
in.read(attributeData);
// check and count attributes
int pos = 0;
int attributeCount = 0;
while (pos < attributeData.length) {
if (pos + 1 >= attributeData.length)
throw new RadiusException("bad packet: attribute length mismatch");
int attributeLength = attributeData[pos + 1] & 0x0ff;
if (attributeLength < 2)
throw new RadiusException("bad packet: invalid attribute length");
pos += attributeLength;
attributeCount++;
}
if (pos != attributeData.length)
throw new RadiusException("bad packet: attribute length mismatch");
// create RadiusPacket object; set properties
RadiusPacket rp = createRadiusPacket(type);
rp.setPacketType(type);
rp.setPacketIdentifier(identifier);