📄 radiuspacket.java
字号:
if (attributeType < 1 || attributeType > 255)
throw new IllegalArgumentException("attribute type out of bounds");
LinkedList result = new LinkedList();
for (Iterator i = attributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute)i.next();
if (attributeType == a.getAttributeType())
result.add(a);
}
return result;
}
/**
* 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.
* @param vendorId vendor ID
* @param attributeType attribute type code
* @return list of RadiusAttribute objects, never null
*/
public List getAttributes(int vendorId, int attributeType) {
if (vendorId == -1)
return getAttributes(attributeType);
LinkedList result = new LinkedList();
List vsas = getVendorAttributes(vendorId);
for (Iterator i = vsas.iterator(); i.hasNext();) {
VendorSpecificAttribute vsa = (VendorSpecificAttribute)i.next();
List sas = vsa.getSubAttributes();
for (Iterator j = sas.iterator(); j.hasNext();) {
RadiusAttribute attr = (RadiusAttribute)j.next();
if (attr.getAttributeType() == attributeType &&
attr.getVendorId() == vendorId)
result.add(attr);
}
}
return result;
}
/**
* Returns a list of all attributes belonging to this Radius
* packet.
* @return List of RadiusAttribute objects
*/
public List getAttributes() {
return attributes;
}
/**
* Returns a Radius attribute of the given type which may only occur once
* in the Radius packet.
* @param type attribute type
* @return RadiusAttribute object or null if there is no such attribute
* @throws RuntimeException if there are multiple occurences of the
* requested attribute type
*/
public RadiusAttribute getAttribute(int type) {
List attrs = getAttributes(type);
if (attrs.size() > 1)
throw new RuntimeException("multiple attributes of requested type " + type);
else if (attrs.size() == 0)
return null;
else
return (RadiusAttribute)attrs.get(0);
}
/**
* Returns a Radius attribute of the given type and vendor ID
* which may only occur once in the Radius packet.
* @param vendorId vendor ID
* @param type attribute type
* @return RadiusAttribute object or null if there is no such attribute
* @throws RuntimeException if there are multiple occurences of the
* requested attribute type
*/
public RadiusAttribute getAttribute(int vendorId, int type) {
if (vendorId == -1)
return getAttribute(type);
List attrs = getAttributes(vendorId, type);
if (attrs.size() > 1)
throw new RuntimeException("multiple attributes of requested type " + type);
else if (attrs.size() == 0)
return null;
else
return (RadiusAttribute)attrs.get(0);
}
/**
* Returns a single Radius attribute of the given type name.
* Also returns sub-attributes.
* @param type attribute type name
* @return RadiusAttribute object or null if there is no such attribute
* @throws RuntimeException if the attribute occurs multiple times
*/
public RadiusAttribute getAttribute(String type) {
if (type == null || type.length() == 0)
throw new IllegalArgumentException("type name is empty");
AttributeType t = dictionary.getAttributeTypeByName(type);
if (t == null)
throw new IllegalArgumentException("unknown attribute type name '" + type + "'");
return getAttribute(t.getVendorId(), t.getTypeCode());
}
/**
* Returns the value of the Radius attribute of the given type or
* null if there is no such attribute.
* Also returns sub-attributes.
* @param type attribute type name
* @return value of the attribute as a string or null if there
* is no such attribute
* @throws IllegalArgumentException if the type name is unknown
* @throws RuntimeException attribute occurs multiple times
*/
public String getAttributeValue(String type) {
RadiusAttribute attr = getAttribute(type);
if (attr == null)
return null;
else
return attr.getAttributeValue();
}
/**
* Returns the Vendor-Specific attribute(s) for the given vendor ID.
* @param vendorId vendor ID of the attribute(s)
* @return List with VendorSpecificAttribute objects, never null
*/
public List getVendorAttributes(int vendorId) {
LinkedList result = new LinkedList();
for (Iterator i = attributes.iterator(); i.hasNext();) {
RadiusAttribute a = (RadiusAttribute)i.next();
if (a instanceof VendorSpecificAttribute) {
VendorSpecificAttribute vsa = (VendorSpecificAttribute)a;
if (vsa.getChildVendorId() == vendorId)
result.add(vsa);
}
}
return result;
}
/**
* Returns the Vendor-Specific attribute for the given vendor ID.
* If there is more than one Vendor-Specific
* attribute with the given vendor ID, the first attribute found is
* returned. If there is no such attribute, null is returned.
* @param vendorId vendor ID of the attribute
* @return the attribute or null if there is no such attribute
* @deprecated use getVendorAttributes(int)
* @see #getVendorAttributes(int)
*/
public VendorSpecificAttribute getVendorAttribute(int vendorId) {
for (Iterator i = getAttributes(VendorSpecificAttribute.VENDOR_SPECIFIC).iterator(); i.hasNext();) {
VendorSpecificAttribute vsa = (VendorSpecificAttribute)i.next();
if (vsa.getChildVendorId() == vendorId)
return vsa;
}
return null;
}
/**
* Encodes this Radius packet and sends it to the specified output
* stream.
* @param out output stream to use
* @param sharedSecret shared secret to be used to encode this packet
* @exception IOException communication error
*/
public void encodeRequestPacket(OutputStream out, String sharedSecret)
throws IOException {
encodePacket(out, sharedSecret, null);
}
/**
* Encodes this Radius response packet and sends it to the specified output
* stream.
* @param out output stream to use
* @param sharedSecret shared secret to be used to encode this packet
* @param request Radius request packet
* @exception IOException communication error
*/
public void encodeResponsePacket(OutputStream out, String sharedSecret, RadiusPacket request)
throws IOException {
if (request == null)
throw new NullPointerException("request cannot be null");
encodePacket(out, sharedSecret, request);
}
/**
* Reads a Radius request 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 sharedSecret shared secret to be used to decode this packet
* @return new RadiusPacket object
* @exception IOException IO error
* @exception RadiusException malformed packet
*/
public static RadiusPacket decodeRequestPacket(InputStream in, String sharedSecret)
throws IOException, RadiusException {
return decodePacket(DefaultDictionary.getDefaultDictionary(), in, sharedSecret, null);
}
/**
* Reads a Radius response packet from the given input stream and
* creates an appropiate RadiusPacket descendant object.
* Reads in all attributes and returns the object.
* Checks the packet authenticator.
* @param sharedSecret shared secret to be used to decode this packet
* @param request Radius request packet
* @return new RadiusPacket object
* @exception IOException IO error
* @exception RadiusException malformed packet
*/
public static RadiusPacket decodeResponsePacket(InputStream in, String sharedSecret, RadiusPacket request)
throws IOException, RadiusException {
if (request == null)
throw new NullPointerException("request may not be null");
return decodePacket(DefaultDictionary.getDefaultDictionary(), in, sharedSecret, request);
}
/**
* Reads a Radius request 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 in InputStream to read packet from
* @param sharedSecret shared secret to be used to decode this packet
* @return new RadiusPacket object
* @exception IOException IO error
* @exception RadiusException malformed packet
*/
public static RadiusPacket decodeRequestPacket(Dictionary dictionary, InputStream in, String sharedSecret)
throws IOException, RadiusException {
return decodePacket(dictionary, in, sharedSecret, null);
}
/**
* Reads a Radius response packet from the given input stream and
* creates an appropiate RadiusPacket descendant object.
* Reads in all attributes and returns the object.
* Checks the packet authenticator.
* @param dictionary dictionary to use for attributes
* @param in InputStream to read packet from
* @param sharedSecret shared secret to be used to decode this packet
* @param request Radius request packet
* @return new RadiusPacket object
* @exception IOException IO error
* @exception RadiusException malformed packet
*/
public static RadiusPacket decodeResponsePacket(Dictionary dictionary, InputStream in, String sharedSecret, RadiusPacket request)
throws IOException, RadiusException {
if (request == null)
throw new NullPointerException("request may not be null");
return decodePacket(dictionary, in, sharedSecret, request);
}
/**
* Retrieves the next packet identifier to use and increments the static
* storage.
* @return the next packet identifier to use
*/
public static synchronized int getNextPacketIdentifier(){
nextPacketId++;
if (nextPacketId > 255)
nextPacketId = 0;
return nextPacketId;
}
/**
* Creates a RadiusPacket object. Depending on the passed type, the
* appropiate successor is chosen. Sets the type, but does not touch
* the packet identifier.
* @param type packet type
* @return RadiusPacket object
*/
public static RadiusPacket createRadiusPacket(final int type) {
RadiusPacket rp;
switch (type) {
case ACCESS_REQUEST:
rp = new AccessRequest();
break;
case ACCOUNTING_REQUEST:
rp = new AccountingRequest();
break;
case ACCESS_ACCEPT:
case ACCESS_REJECT:
case ACCOUNTING_RESPONSE:
default:
rp = new RadiusPacket();
}
rp.setPacketType(type);
return rp;
}
/**
* String representation of this packet, for debugging purposes.
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer s = new StringBuffer();
s.append(getPacketTypeName());
s.append(", ID ");
s.append(packetIdentifier);
for (Iterator i = attributes.iterator(); i.hasNext();) {
RadiusAttribute attr = (RadiusAttribute)i.next();
s.append("\n");
s.append(attr.toString());
}
return s.toString();
}
/**
* 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 by the method
* createAuthenticator() and will return null if no authenticator
* has been created yet.
* @return authenticator, 16 bytes
*/
public byte[] getAuthenticator() {
return authenticator;
}
/**
* Sets the authenticator to be used for this Radius packet.
* This method should seldomly be used.
* Authenticators are created and managed by this class internally.
* @param authenticator authenticator
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -