public static Packet parse(DatagramPacket packet) throws IllegalArgumentException, AssertionError {
        Packet rad;
        try {
            ByteBuffer bb = ByteBuffer.wrap(packet.getData());
            int packetCode = bb.get();
            int packetIdentifier = bb.get();
            int packetLength = bb.getShort();

            if(packetLength != packet.getLength())
                throw new AssertionError("Invalid package length. Discarding package.");
            byte[] packetData = new byte[AUTH_FIELDLENGTH];
            bb.get(packetData, 0, AUTH_FIELDLENGTH);

            rad = new Packet(packetCode, packetIdentifier, packetData);

            while(bb.position() < packetLength) {
                int attrCode = bb.get();
                int attrLength = bb.get();
                int attrLengthWithoutHeader = attrLength-Attribute.HEADER_SIZE;

                byte[] attrData = new byte[attrLengthWithoutHeader];
                bb.get(attrData, 0, attrLengthWithoutHeader);
                rad.addAttribute(attrCode, attrLength, attrData);
            }

        } catch (AssertionError ex) {
            System.out.println(ex.getMessage());
            rad = null;
        }

        return rad;
    }