Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/org/tinyradius/attribute/IntegerAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public void setAttributeValue(String value) {
* Check attribute length.
* @see org.tinyradius.attribute.RadiusAttribute#readAttribute(byte[], int, int)
*/
public void readAttribute(byte[] data, int offset, int length)
public void readAttribute(byte[] data, int offset, int attrType, int attrLen, int attrTypeSize, int attrLenSize)
throws RadiusException {
if (length != 6)
if (attrLen != 4 + attrTypeSize + attrLenSize)
throw new RadiusException("integer attribute: expected 4 bytes data");
super.readAttribute(data, offset, length);
super.readAttribute(data, offset, attrType, attrLen, attrTypeSize, attrLenSize);
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/tinyradius/attribute/IpAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public void setIpAsLong(long ip) {
* Check attribute length.
* @see org.tinyradius.attribute.RadiusAttribute#readAttribute(byte[], int, int)
*/
public void readAttribute(byte[] data, int offset, int length)
public void readAttribute(byte[] data, int offset, int attrType, int attrLen, int attrTypeSize, int attrLenSize)
throws RadiusException {
if (length != 6)
if (attrLen != 4 + attrTypeSize + attrLenSize)
throw new RadiusException("IP attribute: expected 4 bytes data");
super.readAttribute(data, offset, length);
super.readAttribute(data, offset, attrType, attrLen, attrTypeSize, attrLenSize);
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/tinyradius/attribute/Ipv6Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public void setAttributeValue(String value) {
* Check attribute length.
* @see org.tinyradius.attribute.RadiusAttribute#readAttribute(byte[], int, int)
*/
public void readAttribute(byte[] data, int offset, int length)
public void readAttribute(byte[] data, int offset, int attrType, int attrLen, int attrTypeSize, int attrLenSize)
throws RadiusException {
if (length != 18)
if (attrLen != 16 + attrTypeSize + attrLenSize)
throw new RadiusException("IP attribute: expected 16 bytes data");
super.readAttribute(data, offset, length);
super.readAttribute(data, offset, attrType, attrLen, attrTypeSize, attrLenSize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public void setAttributeValue(String value) {
* Check attribute length.
* @see org.tinyradius.attribute.RadiusAttribute#readAttribute(byte[], int, int)
*/
public void readAttribute(byte[] data, int offset, int length)
public void readAttribute(byte[] data, int offset, int attrType, int attrLen, int attrTypeSize, int attrLenSize)
throws RadiusException {
if (length > 20 || length < 4)
if (attrLen > 20 + attrTypeSize + attrLenSize || attrLen < 4 + attrTypeSize + attrLenSize)
throw new RadiusException("IPv6 prefix attribute: expected 4-20 bytes data");
super.readAttribute(data, offset, length);
super.readAttribute(data, offset, attrType, attrLen, attrTypeSize, attrLenSize);
}

}
44 changes: 42 additions & 2 deletions src/main/java/org/tinyradius/attribute/RadiusAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public int getAttributeType() {
* Sets the type of this Radius attribute.
*
* @param attributeType
* type code, 0-255
* type code, 0-65535
*/
public void setAttributeType(int attributeType) {
if (attributeType < 0 || attributeType > 255)
if (attributeType < 0 || attributeType > 65535)
throw new IllegalArgumentException("attribute type invalid: " + attributeType);
this.attributeType = attributeType;
}
Expand Down Expand Up @@ -165,6 +165,24 @@ public byte[] writeAttribute() {
return attr;
}

/**
* Returns this attribute encoded as a byte array.
*
* @return attribute
*/
public byte[] writeAttribute(int attrTypeSize, int attrLenSize) {
if (getAttributeType() == -1)
throw new IllegalArgumentException("attribute type not set");
if (attributeData == null)
throw new NullPointerException("attribute data not set");

byte[] attr = new byte[attrTypeSize + attrLenSize + attributeData.length];
System.arraycopy(toByteArray(getAttributeType(), attrTypeSize), 0, attr, 0, attrTypeSize);
System.arraycopy(toByteArray(attrTypeSize + attrLenSize + attributeData.length, attrLenSize), 0, attr, attrTypeSize, attrLenSize);
System.arraycopy(attributeData, 0, attr, attrTypeSize + attrLenSize, attributeData.length);
return attr;
}

/**
* Reads in this attribute from the passed byte array.
*
Expand All @@ -181,6 +199,15 @@ public void readAttribute(byte[] data, int offset, int length) throws RadiusExce
setAttributeData(attrData);
}

public void readAttribute(byte[] data, int offset, int attrType, int attrLen, int attrTypeSize, int attrLenSize) throws RadiusException {
if (attrLen < 2)
throw new RadiusException("attribute length too small: " + attrLen);
byte[] attrData = new byte[attrLen - attrTypeSize - attrLenSize];
System.arraycopy(data, offset + attrTypeSize + attrLenSize, attrData, 0, attrLen - attrTypeSize - attrLenSize);
setAttributeType(attrType);
setAttributeData(attrData);
}

/**
* String representation for debugging purposes.
*
Expand Down Expand Up @@ -275,6 +302,19 @@ public static RadiusAttribute createRadiusAttribute(int attributeType) {
return createRadiusAttribute(dictionary, -1, attributeType);
}

private static byte[] toByteArray(int value, int len) {
switch (len) {
case 1:
return new byte[] {(byte) value};
case 2:
return new byte[] {(byte) (value >> 8), (byte) value};
case 4:
return new byte[] {(byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value};
default:
throw new IllegalArgumentException("integer can only be [1,2,4] bytes in size");
}
}

/**
* Dictionary to look up attribute names.
*/
Expand Down
77 changes: 47 additions & 30 deletions src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* $Id: VendorSpecificAttribute.java,v 1.7 2005/11/22 10:18:38 wuttke Exp $
* Created on 10.04.2005
*
*
* @author Matthias Wuttke
* @version $Revision: 1.7 $
*/
Expand Down Expand Up @@ -37,7 +37,7 @@ public VendorSpecificAttribute() {

/**
* Constructs a new Vendor-Specific attribute to be sent.
*
*
* @param vendorId
* vendor ID of the sub-attributes
*/
Expand All @@ -48,7 +48,7 @@ public VendorSpecificAttribute(int vendorId) {

/**
* Sets the vendor ID of the child attributes.
*
*
* @param childVendorId
*/
public void setChildVendorId(int childVendorId) {
Expand All @@ -57,7 +57,7 @@ public void setChildVendorId(int childVendorId) {

/**
* Returns the vendor ID of the sub-attributes.
*
*
* @return vendor ID of sub attributes
*/
public int getChildVendorId() {
Expand All @@ -66,7 +66,7 @@ public int getChildVendorId() {

/**
* Also copies the new dictionary to sub-attributes.
*
*
* @param dictionary
* dictionary to set
* @see org.tinyradius.attribute.RadiusAttribute#setDictionary(org.tinyradius.dictionary.Dictionary)
Expand All @@ -81,7 +81,7 @@ public void setDictionary(Dictionary dictionary) {

/**
* Adds a sub-attribute to this attribute.
*
*
* @param attribute
* sub-attribute to add
*/
Expand All @@ -94,7 +94,7 @@ public void addSubAttribute(RadiusAttribute attribute) {

/**
* Adds a sub-attribute with the specified name to this attribute.
*
*
* @param name
* name of the sub-attribute
* @param value
Expand Down Expand Up @@ -123,7 +123,7 @@ public void addSubAttribute(String name, String value) {

/**
* Removes the specified sub-attribute from this attribute.
*
*
* @param attribute
* RadiusAttribute to remove
*/
Expand All @@ -134,7 +134,7 @@ public void removeSubAttribute(RadiusAttribute attribute) {

/**
* Returns the list of sub-attributes.
*
*
* @return List of RadiusAttribute objects
*/
public List getSubAttributes() {
Expand All @@ -143,7 +143,7 @@ public List getSubAttributes() {

/**
* Returns all sub-attributes of this attribut which have the given type.
*
*
* @param attributeType
* type of sub-attributes to get
* @return list of RadiusAttribute objects, does not return null
Expand All @@ -164,7 +164,7 @@ public List getSubAttributes(int attributeType) {
/**
* Returns a sub-attribute of the given type which may only occur once in
* this attribute.
*
*
* @param type
* sub-attribute type
* @return RadiusAttribute object or null if there is no such sub-attribute
Expand All @@ -184,7 +184,7 @@ else if (attrs.size() == 0)

/**
* Returns a single sub-attribute of the given type name.
*
*
* @param type
* attribute type name
* @return RadiusAttribute object or null if there is no such attribute
Expand All @@ -208,7 +208,7 @@ public RadiusAttribute getSubAttribute(String type) throws RadiusException {
/**
* Returns the value of the Radius attribute of the given type or null if
* there is no such attribute.
*
*
* @param type
* attribute type name
* @return value of the attribute as a string or null if there is no such
Expand All @@ -228,7 +228,7 @@ public String getSubAttributeValue(String type) throws RadiusException {

/**
* Renders this attribute as a byte array.
*
*
* @see org.tinyradius.attribute.RadiusAttribute#writeAttribute()
*/
public byte[] writeAttribute() {
Expand All @@ -243,7 +243,7 @@ public byte[] writeAttribute() {
try {
for (Iterator i = subAttributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute) i.next();
bos.write(a.writeAttribute());
bos.write(a.writeAttribute(getDictionary().getVendorTypeSize(getChildVendorId()), getDictionary().getVendorLengthSize(getChildVendorId())));
}
}
catch (IOException ioe) {
Expand All @@ -268,7 +268,7 @@ public byte[] writeAttribute() {
/**
* Reads a Vendor-Specific attribute and decodes the internal sub-attribute
* structure.
*
*
* @see org.tinyradius.attribute.RadiusAttribute#readAttribute(byte[], int, int)
*/
public void readAttribute(byte[] data, int offset, int length) throws RadiusException {
Expand All @@ -283,22 +283,21 @@ public void readAttribute(byte[] data, int offset, int length) throws RadiusExce
throw new RadiusException("not a Vendor-Specific attribute");

// read vendor ID and vendor data
/*
* int vendorId = (data[offset + 2] << 24 | data[offset + 3] << 16 |
* data[offset + 4] << 8 | ((int)data[offset + 5] & 0x000000ff));
*/
int vendorId = (unsignedByteToInt(data[offset + 2]) << 24 | unsignedByteToInt(data[offset + 3]) << 16
| unsignedByteToInt(data[offset + 4]) << 8 | unsignedByteToInt(data[offset + 5]));
int vendorId = unsignedByteToInt(data, offset + 2, 4);
setChildVendorId(vendorId);

// validate sub-attribute structure
int pos = 0;
int count = 0;

int vendorTypeSize = getDictionary().getVendorTypeSize(vendorId);
int vendorLengthSize = getDictionary().getVendorLengthSize(vendorId);

while (pos < vsaLen) {
if (pos + 1 >= vsaLen)
throw new RadiusException("Vendor-Specific attribute malformed");
// int vsaSubType = data[(offset + 6) + pos] & 0x0ff;
int vsaSubLen = data[(offset + 6) + pos + 1] & 0x0ff;
//int vsaSubType = unsignedByteToInt(data, (offset + 6) + pos, vendorTypeSize);
int vsaSubLen = unsignedByteToInt(data, (offset + 6) + pos + vendorTypeSize, vendorLengthSize);
pos += vsaSubLen;
count++;
}
Expand All @@ -308,22 +307,40 @@ public void readAttribute(byte[] data, int offset, int length) throws RadiusExce
subAttributes = new ArrayList(count);
pos = 0;
while (pos < vsaLen) {
int subtype = data[(offset + 6) + pos] & 0x0ff;
int sublength = data[(offset + 6) + pos + 1] & 0x0ff;
RadiusAttribute a = createRadiusAttribute(getDictionary(), vendorId, subtype);
a.readAttribute(data, (offset + 6) + pos, sublength);
int subType = unsignedByteToInt(data, (offset + 6) + pos, vendorTypeSize);
int subLength = unsignedByteToInt(data, (offset + 6) + pos + vendorTypeSize, vendorLengthSize);

RadiusAttribute a = createRadiusAttribute(getDictionary(), vendorId, subType);
a.readAttribute(data, (offset + 6) + pos, subType, subLength, vendorTypeSize, vendorLengthSize);
subAttributes.add(a);
pos += sublength;
pos += subLength;
}
}

private static int unsignedByteToInt(byte b) {
return b & 0xFF;
}

private static int unsignedByteToInt(byte[] bytes, int offset, int len) {
switch (len) {
case 1:
return bytes[offset] & 0xFF;
case 2:
return ((bytes[offset] & 0xFF) << 8 ) |
((bytes[offset + 1] & 0xFF) << 0 );
case 4:
return ((bytes[offset] & 0xFF) << 24) |
((bytes[offset + 1] & 0xFF) << 16) |
((bytes[offset + 2] & 0xFF) << 8 ) |
((bytes[offset + 3] & 0xFF) << 0 );
default:
throw new IllegalArgumentException("integer can only be [1,2,4] bytes in size");
}
}

/**
* Returns a string representation for debugging.
*
*
* @see org.tinyradius.attribute.RadiusAttribute#toString()
*/
public String toString() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tinyradius/dictionary/AttributeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public int getTypeCode() {
* Sets the Radius type code for this attribute type.
*
* @param code
* type code, 1-255
* type code, 1-65535
*/
public void setTypeCode(int code) {
if (code < 1 || code > 255)
if (code < 1 || code > 65535)
throw new IllegalArgumentException("code out of bounds");
this.typeCode = code;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/tinyradius/dictionary/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,21 @@ public interface Dictionary {
* @return vendor ID or -1
*/
public int getVendorId(String vendorName);

/**
* Retrieves vendor type size in bytes
* vendor code.
* @param vendorId vendor number
* @return vendor name or null
*/
public int getVendorTypeSize(int vendorId);

/**
* Retrieves vendor length size in bytes
* vendor code.
* @param vendorId vendor number
* @return vendor name or null
*/
public int getVendorLengthSize(int vendorId);

}
Loading