Package org.tinyradius.packet
Class RadiusPacket
- java.lang.Object
-
- org.tinyradius.packet.RadiusPacket
-
- Direct Known Subclasses:
AccessRequest
,AccountingRequest
public class RadiusPacket extends java.lang.Object
A generic Radius packet. Subclasses provide convenience methods for special packet types.
-
-
Field Summary
Fields Modifier and Type Field Description static int
HEADER_LENGTH
-
Constructor Summary
Constructors Constructor Description RadiusPacket(Dictionary dictionary, int type, int identifier)
Builds a Radius packet with the given type and identifier, without attributes, and with null authenticator.RadiusPacket(Dictionary dictionary, int type, int identifier, byte[] authenticator)
Builds a Radius packet with the given type and identifier and without attributes.RadiusPacket(Dictionary dictionary, int type, int identifier, byte[] authenticator, java.util.List<RadiusAttribute> attributes)
Builds a Radius packet with the given type, identifier and attributes.RadiusPacket(Dictionary dictionary, int type, int identifier, java.util.List<RadiusAttribute> attributes)
Builds a Radius packet with the given type and identifier and without attributes.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAttribute(java.lang.String typeName, java.lang.String value)
Adds a Radius attribute to this packet.void
addAttribute(RadiusAttribute attribute)
Adds a Radius attribute to this packet.protected byte[]
createHashedAuthenticator(java.lang.String sharedSecret, byte[] requestAuthenticator)
Creates an authenticator for a Radius response packet.RadiusPacket
encodeRequest(java.lang.String sharedSecret)
Encode request and generate authenticator.RadiusPacket
encodeResponse(java.lang.String sharedSecret, byte[] requestAuthenticator)
Encode and generate authenticator.RadiusAttribute
getAttribute(int type)
Returns a Radius attribute of the given type which may only occur once in the Radius packet.RadiusAttribute
getAttribute(int vendorId, int type)
Returns a Radius attribute of the given type and vendor ID which may only occur once in the Radius packet.RadiusAttribute
getAttribute(java.lang.String type)
Returns a single Radius attribute of the given type name.protected byte[]
getAttributeBytes()
Encodes the attributes of this Radius packet to a byte array.java.util.Map<java.lang.String,java.lang.String>
getAttributeMap()
java.util.List<RadiusAttribute>
getAttributes()
Returns a list of all attributes belonging to this Radius packet.java.util.List<RadiusAttribute>
getAttributes(int type)
Returns all attributes of this packet of the given type.java.util.List<RadiusAttribute>
getAttributes(int vendorId, int attributeType)
Returns all attributes of this packet that have got the given type and belong to the given vendor ID.java.lang.String
getAttributeValue(java.lang.String type)
Returns the value of the Radius attribute of the given type or null if there is no such attribute.byte[]
getAuthenticator()
Returns the authenticator for this Radius packet.Dictionary
getDictionary()
int
getIdentifier()
int
getType()
java.util.List<VendorSpecificAttribute>
getVendorAttributes(int vendorId)
Returns the Vendor-Specific attribute(s) for the given vendor ID.void
removeAttribute(RadiusAttribute attribute)
Removes the specified attribute from this packet.void
removeAttributes(int type)
Removes all attributes from this packet which have got the specified type.void
removeAttributes(int vendorId, int typeCode)
Removes all sub-attributes of the given vendor and type.void
removeLastAttribute(int type)
Removes the last occurrence of the attribute of the given type from the packet.java.lang.String
toString()
void
verify(java.lang.String sharedSecret, byte[] requestAuthenticator)
Checks the request authenticator against the supplied shared secret.
-
-
-
Field Detail
-
HEADER_LENGTH
public static final int HEADER_LENGTH
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RadiusPacket
public RadiusPacket(Dictionary dictionary, int type, int identifier)
Builds a Radius packet with the given type and identifier, without attributes, and with null authenticator.- Parameters:
dictionary
- custom dictionary to usetype
- packet typeidentifier
- packet identifier
-
RadiusPacket
public RadiusPacket(Dictionary dictionary, int type, int identifier, byte[] authenticator)
Builds a Radius packet with the given type and identifier and without attributes.- Parameters:
dictionary
- custom dictionary to usetype
- packet typeidentifier
- packet identifierauthenticator
- authenticator for packet, nullable
-
RadiusPacket
public RadiusPacket(Dictionary dictionary, int type, int identifier, java.util.List<RadiusAttribute> attributes)
Builds a Radius packet with the given type and identifier and without attributes.- Parameters:
dictionary
- custom dictionary to usetype
- packet typeidentifier
- packet identifierattributes
- list of attributes for packet
-
RadiusPacket
public RadiusPacket(Dictionary dictionary, int type, int identifier, byte[] authenticator, java.util.List<RadiusAttribute> attributes)
Builds a Radius packet with the given type, identifier and attributes.- Parameters:
dictionary
- custom dictionary to usetype
- packet typeidentifier
- packet identifierauthenticator
- can be null if creating manuallyattributes
- list of RadiusAttribute objects
-
-
Method Detail
-
getIdentifier
public int getIdentifier()
- Returns:
- Radius packet identifier
-
getType
public int getType()
- Returns:
- Radius packet type
-
addAttribute
public void addAttribute(RadiusAttribute attribute)
Adds a Radius attribute to this packet. Can also be used to add Vendor-Specific sub-attributes. If a attribute with a vendor code != -1 is passed in, a VendorSpecificAttribute is created for the sub-attribute.- Parameters:
attribute
- RadiusAttribute object
-
addAttribute
public void addAttribute(java.lang.String typeName, java.lang.String value)
Adds a Radius attribute to this packet. Uses AttributeTypes to lookup the type code and converts the value. Can also be used to add sub-attributes.- Parameters:
typeName
- name of the attribute, for example "NAS-Ip-Address", should NOT be 'Vendor-Specific'value
- value of the attribute, for example "127.0.0.1"- Throws:
java.lang.IllegalArgumentException
- if type name is unknown
-
removeAttribute
public void removeAttribute(RadiusAttribute attribute)
Removes the specified attribute from this packet.- Parameters:
attribute
- RadiusAttribute to remove
-
removeAttributes
public void removeAttributes(int type)
Removes all attributes from this packet which have got the specified type.- Parameters:
type
- attribute type to remove
-
removeLastAttribute
public void removeLastAttribute(int type)
Removes the last occurrence of the attribute of the given type from the packet.- Parameters:
type
- attribute type code
-
removeAttributes
public void removeAttributes(int vendorId, int typeCode)
Removes all sub-attributes of the given vendor and type.- Parameters:
vendorId
- vendor IDtypeCode
- attribute type code
-
getAttributes
public java.util.List<RadiusAttribute> getAttributes(int type)
Returns all attributes of this packet of the given type. Returns an empty list if there are no such attributes.- Parameters:
type
- type of attributes to get- Returns:
- list of RadiusAttribute objects, does not return null
-
getAttributes
public java.util.List<RadiusAttribute> getAttributes(int vendorId, int attributeType)
Returns all attributes of this packet that have got the given type and belong to the given vendor ID. Returns an empty list if there are no such attributes.- Parameters:
vendorId
- vendor IDattributeType
- attribute type code- Returns:
- list of RadiusAttribute objects, never null
-
getAttributes
public java.util.List<RadiusAttribute> getAttributes()
Returns a list of all attributes belonging to this Radius packet.- Returns:
- List of RadiusAttribute objects
-
getAttribute
public RadiusAttribute getAttribute(int type)
Returns a Radius attribute of the given type which may only occur once in the Radius packet.- Parameters:
type
- attribute type- Returns:
- RadiusAttribute object or null if there is no such attribute
- Throws:
java.lang.RuntimeException
- if there are multiple occurrences of the requested attribute type
-
getAttribute
public RadiusAttribute getAttribute(int vendorId, int type)
Returns a Radius attribute of the given type and vendor ID which may only occur once in the Radius packet.- Parameters:
vendorId
- vendor IDtype
- attribute type- Returns:
- RadiusAttribute object or null if there is no such attribute
- Throws:
java.lang.RuntimeException
- if there are multiple occurrences of the requested attribute type
-
getAttribute
public RadiusAttribute getAttribute(java.lang.String type)
Returns a single Radius attribute of the given type name. Also returns sub-attributes.- Parameters:
type
- attribute type name- Returns:
- RadiusAttribute object or null if there is no such attribute
- Throws:
java.lang.RuntimeException
- if the attribute occurs multiple times
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String type)
Returns the value of the Radius attribute of the given type or null if there is no such attribute. Also returns sub-attributes.- Parameters:
type
- attribute type name- Returns:
- value of the attribute as a string or null if there is no such attribute
- Throws:
java.lang.IllegalArgumentException
- if the type name is unknownjava.lang.RuntimeException
- attribute occurs multiple times
-
getVendorAttributes
public java.util.List<VendorSpecificAttribute> getVendorAttributes(int vendorId)
Returns the Vendor-Specific attribute(s) for the given vendor ID.- Parameters:
vendorId
- vendor ID of the attribute(s)- Returns:
- List with VendorSpecificAttribute objects, never null
-
encodeRequest
public RadiusPacket encodeRequest(java.lang.String sharedSecret)
Encode request and generate authenticator. Should be idempotent.Base implementation generates hashed authenticator.
- Parameters:
sharedSecret
- shared secret that secures the communication with the other Radius server/client- Returns:
- RadiusPacket with new authenticator and/or encoded attributes
-
encodeResponse
public RadiusPacket encodeResponse(java.lang.String sharedSecret, byte[] requestAuthenticator)
Encode and generate authenticator. Should be idempotent.Requires request authenticator to generator response authenticator.
- Parameters:
sharedSecret
- shared secret to be used to encode this packetrequestAuthenticator
- request packet authenticator- Returns:
- new RadiusPacket instance with same properties and valid authenticator
-
getAuthenticator
public byte[] getAuthenticator()
Returns the authenticator for this Radius packet. For a Radius packet read from a stream, this will return the authenticator sent by the server. For a new Radius packet to be sent, this will return the authenticator created and will return null if no authenticator has been created yet.- Returns:
- authenticator, 16 bytes
-
getDictionary
public Dictionary getDictionary()
- Returns:
- the dictionary this Radius packet uses.
-
createHashedAuthenticator
protected byte[] createHashedAuthenticator(java.lang.String sharedSecret, byte[] requestAuthenticator)
Creates an authenticator for a Radius response packet.- Parameters:
sharedSecret
- shared secretrequestAuthenticator
- request packet authenticator- Returns:
- new 16 byte response authenticator
-
verify
public void verify(java.lang.String sharedSecret, byte[] requestAuthenticator) throws RadiusException
Checks the request authenticator against the supplied shared secret.- Parameters:
sharedSecret
- shared secretrequestAuthenticator
- should be set to request authenticator if verifying response, otherwise set to 16 zero octets- Throws:
RadiusException
- if authenticator check fails
-
getAttributeBytes
protected byte[] getAttributeBytes()
Encodes the attributes of this Radius packet to a byte array.- Returns:
- byte array with encoded attributes
-
getAttributeMap
public java.util.Map<java.lang.String,java.lang.String> getAttributeMap()
- Returns:
- Map of attribute key-value
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-