src/main/java/org/tinyradius/packet/RadiusPacket.java
protected static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, String sharedSecret, RadiusPacket request)
        throws IOException, RadiusException {
        // check shared secret
        if (sharedSecret == null || sharedSecret.length() == 0) {
            throw new RuntimeException("no shared secret has been set");
        }

        // check request authenticator
        if (request != null && request.getAuthenticator() == null) {
            throw new RuntimeException("request authenticator not set");
        }

        // read and check header
        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);
        rp.authenticator = authenticator;

        // load attributes
        pos = 0;
        while (pos < attributeData.length) {
            int attributeType = attributeData[pos] & 0x0ff;
            int attributeLength = attributeData[pos + 1] & 0x0ff;
            RadiusAttribute a = RadiusAttribute.createRadiusAttribute(dictionary, -1, attributeType);
            a.readAttribute(attributeData, pos, attributeLength);
            rp.addAttribute(a);
            pos += attributeLength;
        }

        // request packet?
        if (request == null) {
            // decode attributes
            rp.decodeRequestAttributes(sharedSecret);
            rp.checkRequestAuthenticator(sharedSecret, length, attributeData);
        }
        else {
            // response packet: check authenticator
            rp.checkResponseAuthenticator(sharedSecret, length, attributeData, request.getAuthenticator());
        }

        return rp;
    }
/**
     * Reads a Radius packet from the given input stream and
     * creates an appropiate RadiusPacket descendant object.
     * Reads in all attributes and returns the object.
     * Decodes the encrypted fields and attributes of the packet.
     *
     * @param dictionary   dictionary to use for attributes
     * @param sharedSecret shared secret to be used to decode this packet
     * @param request      Radius request packet if this is a response packet to be
     *                     decoded, null if this is a request packet to be decoded
     * @return new RadiusPacket object
     * @throws IOException     if an IO error occurred
     * @throws RadiusException if the Radius packet is malformed
     */
Reads a Radius packet from the given input stream and
creates an appropiate RadiusPacket descendant object.
Reads in all attributes and returns the object.
Decodes the encrypted fields and attributes of the packet.


Reads a Radius packet from the given input stream and creates an appropiate RadiusPacket descendant object . Reads in all attributes and returns the object . Decodes the encrypted fields and attributes of the packet .
protected static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, String sharedSecret, RadiusPacket request)
        throws IOException, RadiusException {
        if (sharedSecret == null || sharedSecret.length() == 0) {
            throw new RuntimeException("no shared secret has been set");
        }
        if (request != null && request.getAuthenticator() == null) {
            throw new RuntimeException("request authenticator not set");
        }
        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)");
        }
        byte[] authenticator = new byte[16];
        byte[] attributeData = new byte[length - RADIUS_HEADER_LENGTH];
        in.read(authenticator);
        in.read(attributeData);
        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");
        }
        RadiusPacket rp = createRadiusPacket(type);
        rp.setPacketType(type);
        rp.setPacketIdentifier(identifier);
        rp.authenticator = authenticator;
        pos = 0;
        while (pos < attributeData.length) {
            int attributeType = attributeData[pos] & 0x0ff;
            int attributeLength = attributeData[pos + 1] & 0x0ff;
            RadiusAttribute a = RadiusAttribute.createRadiusAttribute(dictionary, -1, attributeType);
            a.readAttribute(attributeData, pos, attributeLength);
            rp.addAttribute(a);
            pos += attributeLength;
        }
        if (request == null) {
            rp.decodeRequestAttributes(sharedSecret);
            rp.checkRequestAuthenticator(sharedSecret, length, attributeData);
        }
        else {
            rp.checkResponseAuthenticator(sharedSecret, length, attributeData, request.getAuthenticator());
        }
        return rp;
    }
protected static RadiusPacket decodePacket ( Dictionary dictionary , InputStream in , String sharedSecret , RadiusPacket request ) throws IOException , RadiusException { if ( sharedSecret == null || sharedSecret . length ( ) == 0 ) { throw new RuntimeException ( "no shared secret has been set" ) ; } if ( request != null && request . getAuthenticator ( ) == null ) { throw new RuntimeException ( "request authenticator not set" ) ; } 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)" ) ; } byte [ ] authenticator = new byte [ 16 ] ; byte [ ] attributeData = new byte [ length - RADIUS_HEADER_LENGTH ] ; in . read ( authenticator ) ; in . read ( attributeData ) ; 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" ) ; } RadiusPacket rp = createRadiusPacket ( type ) ; rp . setPacketType ( type ) ; rp . setPacketIdentifier ( identifier ) ; rp . authenticator = authenticator ; pos = 0 ; while ( pos < attributeData . length ) { int attributeType = attributeData [ pos ] & 0x0ff ; int attributeLength = attributeData [ pos + 1 ] & 0x0ff ; RadiusAttribute a = RadiusAttribute . createRadiusAttribute ( dictionary , - 1 , attributeType ) ; a . readAttribute ( attributeData , pos , attributeLength ) ; rp . addAttribute ( a ) ; pos += attributeLength ; } if ( request == null ) { rp . decodeRequestAttributes ( sharedSecret ) ; rp . checkRequestAuthenticator ( sharedSecret , length , attributeData ) ; } else { rp . checkResponseAuthenticator ( sharedSecret , length , attributeData , request . getAuthenticator ( ) ) ; } return rp ; }
Reads a Radius packet from the given input stream and
 creates an appropiate RadiusPacket descendant object.
Reads a Radius packet from the given input stream and creates an appropiate RadiusPacket descendant object .
// check shared secret
 // check request authenticator
 // read and check header
 // read rest of packet
 // check and count attributes
 // create RadiusPacket object; set properties
 // load attributes
 // request packet?
 // decode attributes
 // response packet: check authenticator
