From 75e16f0967845a4bcf4b20ed555eae806ceb60b4 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Tue, 26 May 2020 15:34:16 -0500 Subject: [PATCH 01/21] Change dictionary definition for Delegated-IPv6-Prefix --- src/main/resources/org/tinyradius/dictionary/default_dictionary | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/org/tinyradius/dictionary/default_dictionary b/src/main/resources/org/tinyradius/dictionary/default_dictionary index 38b3ac6d..a2dc098d 100644 --- a/src/main/resources/org/tinyradius/dictionary/default_dictionary +++ b/src/main/resources/org/tinyradius/dictionary/default_dictionary @@ -81,7 +81,7 @@ ATTRIBUTE Login-IPv6-Host 98 octets ATTRIBUTE Framed-IPv6-Route 99 string ATTRIBUTE Framed-IPv6-Pool 100 string ATTRIBUTE Error-Cause 101 integer -ATTRIBUTE Delegated-Ipv6-Prefix 123 ipv6addr +ATTRIBUTE Delegated-IPv6-Prefix 123 ipv6prefix ATTRIBUTE Framed-IPv6-Address 168 ipv6addr ATTRIBUTE DNS-Server-IPv6-Address 169 ipv6addr ATTRIBUTE Route-IPv6-Information 170 octets From 328c8cc2dd0602431c47e736e7fb4b097c0d5cb8 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Tue, 26 May 2020 15:36:38 -0500 Subject: [PATCH 02/21] Fix parsing of Ipv6PrefixAttribute to allow for prefixes smaller than 16 bytes per RFCs. --- .../attribute/Ipv6PrefixAttribute.java | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/Ipv6PrefixAttribute.java b/src/main/java/org/tinyradius/attribute/Ipv6PrefixAttribute.java index ba8a4a8a..5762382e 100644 --- a/src/main/java/org/tinyradius/attribute/Ipv6PrefixAttribute.java +++ b/src/main/java/org/tinyradius/attribute/Ipv6PrefixAttribute.java @@ -4,13 +4,14 @@ */ package org.tinyradius.attribute; -import java.util.Arrays; -import java.util.StringTokenizer; -import java.net.Inet6Address; -import java.net.UnknownHostException; - +import inet.ipaddr.AddressStringException; +import inet.ipaddr.AddressValueException; +import inet.ipaddr.IPAddressString; +import inet.ipaddr.ipv6.IPv6Address; import org.tinyradius.util.RadiusException; +import java.util.Arrays; + /** * This class represents a Radius attribute for an IPv6 prefix. */ @@ -40,14 +41,19 @@ public Ipv6PrefixAttribute(int type, String value) { */ public String getAttributeValue() { final byte[] data = getAttributeData(); - if (data == null || data.length != 18) - throw new RuntimeException("ip attribute: expected 18 bytes attribute data"); + if (data == null) throw new RuntimeException("ipv6 prefix attribute: expected 2-18 bytes attribute data and got null."); + if (data.length < 2 || data.length > 18) + throw new RuntimeException("ipv6 prefix attribute: expected 2-18 bytes attribute data and got " + data.length); try { - final int prefix = (data[1] & 0xff); - final Inet6Address addr = (Inet6Address)Inet6Address.getByAddress(null, Arrays.copyOfRange(data,2,data.length)); - - return addr.getHostAddress() + "/" + prefix; - } catch (UnknownHostException e) { + final int prefixSize = (data[1] & 0xff); + byte[] prefix = Arrays.copyOfRange(data,2,data.length); + if(prefix.length < 16) { + // Pad w/ trailing 0's if length not 128 bits (IPv6Address will pad w/ leading 0's if less than 128 bits) + prefix = Arrays.copyOf(prefix, 16); + } + final IPv6Address ipv6prefix = new IPv6Address(prefix, prefixSize); + return ipv6prefix.toString(); + } catch (AddressValueException e) { throw new IllegalArgumentException("bad IPv6 prefix", e); } @@ -64,21 +70,24 @@ public void setAttributeValue(String value) { if (value == null || value.length() < 3) throw new IllegalArgumentException("bad IPv6 address : " + value); try { - final byte[] data = new byte[18]; - data[0] = 0; -//TODO better checking - final int slashPos = value.indexOf("/"); - data[1] = (byte)(Integer.valueOf(value.substring(slashPos+1)) & 0xff); - - final Inet6Address addr = (Inet6Address)Inet6Address.getByName(value.substring(0,slashPos)); - - byte[] ipData = addr.getAddress(); + IPAddressString ipAddressString = new IPAddressString(value); + if( !ipAddressString.isIPAddress() || !ipAddressString.isIPv6() ) + throw new IllegalArgumentException("bad IPv6 address : " + value); + + IPv6Address ipv6Prefix = ipAddressString.toAddress().toIPv6(); + + final byte[] data = new byte[18]; + data[0] = 0; + data[1] = (byte) (ipv6Prefix.getPrefixLength() & 0xff); + + byte[] ipData = ipv6Prefix.getNetworkSection().getBytes(); for (int i = 0; i < ipData.length; i++) { - data[i+2] = ipData[i]; + data[i + 2] = ipData[i]; } - + setAttributeData(data); - } catch (UnknownHostException e) { + + } catch (AddressStringException e) { throw new IllegalArgumentException("bad IPv6 address : " + value, e); } } @@ -90,8 +99,8 @@ public void setAttributeValue(String value) { */ public void readAttribute(byte[] data, int offset, int length) throws RadiusException { - if (length != 20) - throw new RadiusException("IP attribute: expected 18 bytes data"); + if (length > 20 || length < 4) + throw new RadiusException("IPv6 prefix attribute: expected 4-20 bytes data"); super.readAttribute(data, offset, length); } From 7155e86a29a0e6699f715228576d2aa440acd9f7 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Tue, 26 May 2020 15:37:53 -0500 Subject: [PATCH 03/21] Add seancfoley IPAddress library for better parsing of IPv6 addresses/prefixes. --- pom.xml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 302f9b86..68e4a2ae 100644 --- a/pom.xml +++ b/pom.xml @@ -32,8 +32,8 @@ UTF-8 - 6 - 6 + 8 + 8 @@ -49,6 +49,12 @@ test + + com.github.seancfoley + ipaddress + 5.3.1 + + From 98cc2f5ed9353c19455f395c2a008e9b59e92803 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 16:27:06 -0600 Subject: [PATCH 04/21] Add maven clean and compiler plugins --- pom.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pom.xml b/pom.xml index 01cb078a..63635800 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,22 @@ + + org.apache.maven.plugins + maven-clean-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + true + -Xlint:unchecked + + 8 + + From 5805989dc8f6f913c664e4a7cd6f8df10ffb41d1 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 16:27:28 -0600 Subject: [PATCH 05/21] Bump junit to 4.3.2 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 63635800..5046d9d8 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ junit junit - 4.13.1 + 4.13.2 test From 1b3092f80a690bd063b21dd8a5314ba7c616359e Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 17:06:59 -0600 Subject: [PATCH 06/21] Remove un-needed public declaration of interface methods --- .../java/org/tinyradius/dictionary/Dictionary.java | 10 +++++----- .../org/tinyradius/dictionary/WritableDictionary.java | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/tinyradius/dictionary/Dictionary.java b/src/main/java/org/tinyradius/dictionary/Dictionary.java index a3023020..94c8f1d7 100644 --- a/src/main/java/org/tinyradius/dictionary/Dictionary.java +++ b/src/main/java/org/tinyradius/dictionary/Dictionary.java @@ -19,7 +19,7 @@ public interface Dictionary { * @param typeName name of the attribute type * @return AttributeType object or null */ - public AttributeType getAttributeTypeByName(String typeName); + AttributeType getAttributeTypeByName(String typeName); /** * Retrieves an attribute type by type code. This method @@ -27,7 +27,7 @@ public interface Dictionary { * @param typeCode type code, 1-255 * @return AttributeType object or null */ - public AttributeType getAttributeTypeByCode(int typeCode); + AttributeType getAttributeTypeByCode(int typeCode); /** * Retrieves an attribute type for a vendor-specific @@ -36,7 +36,7 @@ public interface Dictionary { * @param typeCode type code, 1-255 * @return AttributeType object or null */ - public AttributeType getAttributeTypeByCode(int vendorId, int typeCode); + AttributeType getAttributeTypeByCode(int vendorId, int typeCode); /** * Retrieves the name of the vendor with the given @@ -44,7 +44,7 @@ public interface Dictionary { * @param vendorId vendor number * @return vendor name or null */ - public String getVendorName(int vendorId); + String getVendorName(int vendorId); /** * Retrieves the ID of the vendor with the given @@ -52,6 +52,6 @@ public interface Dictionary { * @param vendorName name of the vendor * @return vendor ID or -1 */ - public int getVendorId(String vendorName); + int getVendorId(String vendorName); } diff --git a/src/main/java/org/tinyradius/dictionary/WritableDictionary.java b/src/main/java/org/tinyradius/dictionary/WritableDictionary.java index 2d8bc5a4..93041f7f 100644 --- a/src/main/java/org/tinyradius/dictionary/WritableDictionary.java +++ b/src/main/java/org/tinyradius/dictionary/WritableDictionary.java @@ -18,12 +18,12 @@ public interface WritableDictionary * @param vendorId vendor ID * @param vendorName name of the vendor */ - public void addVendor(int vendorId, String vendorName); + void addVendor(int vendorId, String vendorName); /** * Adds an AttributeType object to the dictionary. * @param attributeType AttributeType object */ - public void addAttributeType(AttributeType attributeType); + void addAttributeType(AttributeType attributeType); } From a78ca06fb3f2a085656609a639abb699574a1a36 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 17:09:03 -0600 Subject: [PATCH 07/21] Convert C# style byte array to Java convention --- src/main/java/org/tinyradius/packet/AccessRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/tinyradius/packet/AccessRequest.java b/src/main/java/org/tinyradius/packet/AccessRequest.java index 043b0f77..418eb565 100644 --- a/src/main/java/org/tinyradius/packet/AccessRequest.java +++ b/src/main/java/org/tinyradius/packet/AccessRequest.java @@ -297,7 +297,7 @@ private byte[] encodePapPassword(final byte[] userPass, byte[] sharedSecret) { md5.update(encryptedPass, i - 16, 16); } - byte bn[] = md5.digest(); + byte[] bn = md5.digest(); // perform the XOR as specified by RFC 2865. for (int j = 0; j < 16; j++) @@ -330,7 +330,7 @@ private String decodePapPassword(byte[] encryptedPass, byte[] sharedSecret) thro md5.reset(); md5.update(sharedSecret); md5.update(i == 0 ? getAuthenticator() : lastBlock); - byte bn[] = md5.digest(); + byte[] bn = md5.digest(); System.arraycopy(encryptedPass, i, lastBlock, 0, 16); From 23c8cf68d012b3f3803aca1893438b4911692836 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 17:11:04 -0600 Subject: [PATCH 08/21] Use StandardCharsets.UTF_8 constant instead of hardcoded string value. --- .../org/tinyradius/attribute/StringAttribute.java | 14 +++----------- src/main/java/org/tinyradius/util/RadiusUtil.java | 14 +++----------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/StringAttribute.java b/src/main/java/org/tinyradius/attribute/StringAttribute.java index 21a00089..765505aa 100644 --- a/src/main/java/org/tinyradius/attribute/StringAttribute.java +++ b/src/main/java/org/tinyradius/attribute/StringAttribute.java @@ -6,7 +6,7 @@ */ package org.tinyradius.attribute; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * This class represents a Radius attribute which only @@ -36,11 +36,7 @@ public StringAttribute(int type, String value) { * @return a string */ public String getAttributeValue() { - try { - return new String(getAttributeData(), "UTF-8"); - } catch (UnsupportedEncodingException uee) { - return new String(getAttributeData()); - } + return new String(getAttributeData(), StandardCharsets.UTF_8); } /** @@ -50,11 +46,7 @@ public String getAttributeValue() { public void setAttributeValue(String value) { if (value == null) throw new NullPointerException("string value not set"); - try { - setAttributeData(value.getBytes("UTF-8")); - } catch (UnsupportedEncodingException uee) { - setAttributeData(value.getBytes()); - } + setAttributeData(value.getBytes(StandardCharsets.UTF_8)); } } diff --git a/src/main/java/org/tinyradius/util/RadiusUtil.java b/src/main/java/org/tinyradius/util/RadiusUtil.java index e8e2298b..0b80320f 100644 --- a/src/main/java/org/tinyradius/util/RadiusUtil.java +++ b/src/main/java/org/tinyradius/util/RadiusUtil.java @@ -6,7 +6,7 @@ */ package org.tinyradius.util; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * This class contains miscellaneous static utility functions. @@ -20,11 +20,7 @@ public class RadiusUtil { * @return UTF-8 byte array */ public static byte[] getUtf8Bytes(String str) { - try { - return str.getBytes("UTF-8"); - } catch (UnsupportedEncodingException uee) { - return str.getBytes(); - } + return str.getBytes(StandardCharsets.UTF_8); } /** @@ -34,11 +30,7 @@ public static byte[] getUtf8Bytes(String str) { * @return Java string */ public static String getStringFromUtf8(byte[] utf8) { - try { - return new String(utf8, "UTF-8"); - } catch (UnsupportedEncodingException uee) { - return new String(utf8); - } + return new String(utf8, StandardCharsets.UTF_8); } /** From 523512c8056cffbcd95c71cb0b538f79980c23fc Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 22:06:55 -0600 Subject: [PATCH 09/21] Set checks/types for raw maps, lists, sets, etc. --- .../attribute/VendorSpecificAttribute.java | 41 +++-- .../tinyradius/dictionary/AttributeType.java | 14 +- .../dictionary/MemoryDictionary.java | 32 ++-- .../org/tinyradius/packet/AccessRequest.java | 67 ++++---- .../org/tinyradius/packet/RadiusPacket.java | 148 ++++++++---------- .../org/tinyradius/proxy/RadiusProxy.java | 16 +- 6 files changed, 147 insertions(+), 171 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java b/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java index 5adf0767..ca729326 100644 --- a/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java +++ b/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java @@ -10,7 +10,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.tinyradius.dictionary.AttributeType; @@ -50,6 +49,7 @@ public VendorSpecificAttribute(int vendorId) { * Sets the vendor ID of the child attributes. * * @param childVendorId + * vendor ID of the child attributes */ public void setChildVendorId(int childVendorId) { this.childVendorId = childVendorId; @@ -73,8 +73,7 @@ public int getChildVendorId() { */ public void setDictionary(Dictionary dictionary) { super.setDictionary(dictionary); - for (Iterator i = subAttributes.iterator(); i.hasNext();) { - RadiusAttribute attr = (RadiusAttribute) i.next(); + for (RadiusAttribute attr : subAttributes) { attr.setDictionary(dictionary); } } @@ -87,7 +86,7 @@ public void setDictionary(Dictionary dictionary) { */ public void addSubAttribute(RadiusAttribute attribute) { if (attribute.getVendorId() != getChildVendorId()) - throw new IllegalArgumentException("sub attribut has incorrect vendor ID"); + throw new IllegalArgumentException("sub attribute has incorrect vendor ID"); subAttributes.add(attribute); } @@ -137,24 +136,23 @@ public void removeSubAttribute(RadiusAttribute attribute) { * * @return List of RadiusAttribute objects */ - public List getSubAttributes() { + public List getSubAttributes() { return subAttributes; } /** - * Returns all sub-attributes of this attribut which have the given type. + * Returns all sub-attributes of this attribute which have the given type. * * @param attributeType * type of sub-attributes to get * @return list of RadiusAttribute objects, does not return null */ - public List getSubAttributes(int attributeType) { + public List getSubAttributes(int attributeType) { if (attributeType < 1 || attributeType > 255) throw new IllegalArgumentException("sub-attribute type out of bounds"); - LinkedList result = new LinkedList(); - for (Iterator i = subAttributes.iterator(); i.hasNext();) { - RadiusAttribute a = (RadiusAttribute) i.next(); + LinkedList result = new LinkedList<>(); + for (RadiusAttribute a : subAttributes) { if (attributeType == a.getAttributeType()) result.add(a); } @@ -169,17 +167,17 @@ public List getSubAttributes(int attributeType) { * sub-attribute type * @return RadiusAttribute object or null if there is no such sub-attribute * @throws RuntimeException - * if there are multiple occurences of the + * if there are multiple occurrences of the * requested sub-attribute type */ public RadiusAttribute getSubAttribute(int type) { - List attrs = getSubAttributes(type); + List attrs = getSubAttributes(type); if (attrs.size() > 1) throw new RuntimeException("multiple sub-attributes of requested type " + type); else if (attrs.size() == 0) return null; else - return (RadiusAttribute) attrs.get(0); + return attrs.get(0); } /** @@ -188,11 +186,10 @@ else if (attrs.size() == 0) * @param type * attribute type name * @return RadiusAttribute object or null if there is no such attribute - * @throws RadiusException * @throws RuntimeException * if the attribute occurs multiple times */ - public RadiusAttribute getSubAttribute(String type) throws RadiusException { + public RadiusAttribute getSubAttribute(String type) { if (type == null || type.length() == 0) throw new IllegalArgumentException("type name is empty"); @@ -218,7 +215,7 @@ public RadiusAttribute getSubAttribute(String type) throws RadiusException { * @throws RuntimeException * attribute occurs multiple times */ - public String getSubAttributeValue(String type) throws RadiusException { + public String getSubAttributeValue(String type) { RadiusAttribute attr = getSubAttribute(type); if (attr == null) { return null; @@ -241,8 +238,7 @@ public byte[] writeAttribute() { // write sub-attributes try { - for (Iterator i = subAttributes.iterator(); i.hasNext();) { - RadiusAttribute a = (RadiusAttribute) i.next(); + for (RadiusAttribute a : subAttributes) { bos.write(a.writeAttribute()); } } @@ -305,7 +301,7 @@ public void readAttribute(byte[] data, int offset, int length) throws RadiusExce if (pos != vsaLen) throw new RadiusException("Vendor-Specific attribute malformed"); - subAttributes = new ArrayList(count); + subAttributes = new ArrayList<>(count); pos = 0; while (pos < vsaLen) { int subtype = data[(offset + 6) + pos] & 0x0ff; @@ -327,7 +323,7 @@ private static int unsignedByteToInt(byte b) { * @see org.tinyradius.attribute.RadiusAttribute#toString() */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append("Vendor-Specific: "); int vendorId = getChildVendorId(); String vendorName = getDictionary().getVendorName(vendorId); @@ -341,8 +337,7 @@ public String toString() { sb.append("vendor ID "); sb.append(vendorId); } - for (Iterator i = getSubAttributes().iterator(); i.hasNext();) { - RadiusAttribute attr = (RadiusAttribute) i.next(); + for (RadiusAttribute attr : getSubAttributes()) { sb.append("\n"); sb.append(attr.toString()); } @@ -352,7 +347,7 @@ public String toString() { /** * Sub attributes. Only set if isRawData == false. */ - private List subAttributes = new ArrayList(); + private List subAttributes = new ArrayList<>(); /** * Vendor ID of sub-attributes. diff --git a/src/main/java/org/tinyradius/dictionary/AttributeType.java b/src/main/java/org/tinyradius/dictionary/AttributeType.java index 5b62600e..b839702d 100644 --- a/src/main/java/org/tinyradius/dictionary/AttributeType.java +++ b/src/main/java/org/tinyradius/dictionary/AttributeType.java @@ -7,7 +7,6 @@ package org.tinyradius.dictionary; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import org.tinyradius.attribute.RadiusAttribute; @@ -145,7 +144,7 @@ public void setVendorId(int vendorId) { */ public String getEnumeration(int value) { if (enumeration != null) { - return (String) enumeration.get(new Integer(value)); + return enumeration.get(value); } return null; } @@ -163,10 +162,9 @@ public Integer getEnumeration(String value) { throw new IllegalArgumentException("value is empty"); if (enumeration == null) return null; - for (Iterator i = enumeration.entrySet().iterator(); i.hasNext();) { - Map.Entry e = (Map.Entry) i.next(); + for (Map.Entry e : enumeration.entrySet()) { if (e.getValue().equals(value)) - return (Integer) e.getKey(); + return e.getKey(); } return null; } @@ -183,8 +181,8 @@ public void addEnumerationValue(int num, String name) { if (name == null || name.length() == 0) throw new IllegalArgumentException("name is empty"); if (enumeration == null) - enumeration = new HashMap(); - enumeration.put(new Integer(num), name); + enumeration = new HashMap<>(); + enumeration.put(num, name); } /** @@ -205,6 +203,6 @@ public String toString() { private int typeCode; private String name; private Class attributeClass; - private Map enumeration = null; + private Map enumeration = null; } diff --git a/src/main/java/org/tinyradius/dictionary/MemoryDictionary.java b/src/main/java/org/tinyradius/dictionary/MemoryDictionary.java index aabc5f38..684bcb04 100644 --- a/src/main/java/org/tinyradius/dictionary/MemoryDictionary.java +++ b/src/main/java/org/tinyradius/dictionary/MemoryDictionary.java @@ -8,7 +8,6 @@ package org.tinyradius.dictionary; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; /** @@ -47,11 +46,11 @@ public AttributeType getAttributeTypeByCode(int typeCode) { * @see org.tinyradius.dictionary.Dictionary#getAttributeTypeByCode(int, int) */ public AttributeType getAttributeTypeByCode(int vendorCode, int typeCode) { - Map vendorAttributes = (Map) attributesByCode.get(new Integer(vendorCode)); + Map vendorAttributes = attributesByCode.get(vendorCode); if (vendorAttributes == null) { return null; } - return (AttributeType) vendorAttributes.get(new Integer(typeCode)); + return vendorAttributes.get(typeCode); } /** @@ -63,7 +62,7 @@ public AttributeType getAttributeTypeByCode(int vendorCode, int typeCode) { * @see org.tinyradius.dictionary.Dictionary#getAttributeTypeByName(java.lang.String) */ public AttributeType getAttributeTypeByName(String typeName) { - return (AttributeType) attributesByName.get(typeName); + return attributesByName.get(typeName); } /** @@ -76,10 +75,9 @@ public AttributeType getAttributeTypeByName(String typeName) { * @see org.tinyradius.dictionary.Dictionary#getVendorId(java.lang.String) */ public int getVendorId(String vendorName) { - for (Iterator i = vendorsByCode.entrySet().iterator(); i.hasNext();) { - Map.Entry e = (Map.Entry) i.next(); + for (Map.Entry e : vendorsByCode.entrySet()) { if (e.getValue().equals(vendorName)) - return ((Integer) e.getKey()).intValue(); + return e.getKey(); } return -1; } @@ -94,7 +92,7 @@ public int getVendorId(String vendorName) { * @see org.tinyradius.dictionary.Dictionary#getVendorName(int) */ public String getVendorName(int vendorId) { - return (String) vendorsByCode.get(new Integer(vendorId)); + return vendorsByCode.get(vendorId); } /** @@ -114,7 +112,7 @@ public void addVendor(int vendorId, String vendorName) { throw new IllegalArgumentException("duplicate vendor code"); if (vendorName == null || vendorName.length() == 0) throw new IllegalArgumentException("vendor name empty"); - vendorsByCode.put(new Integer(vendorId), vendorName); + vendorsByCode.put(vendorId, vendorName); } /** @@ -129,18 +127,14 @@ public void addAttributeType(AttributeType attributeType) { if (attributeType == null) throw new IllegalArgumentException("attribute type must not be null"); - Integer vendorId = new Integer(attributeType.getVendorId()); - Integer typeCode = new Integer(attributeType.getTypeCode()); + Integer vendorId = attributeType.getVendorId(); + Integer typeCode = attributeType.getTypeCode(); String attributeName = attributeType.getName(); if (attributesByName.containsKey(attributeName)) throw new IllegalArgumentException("duplicate attribute name: " + attributeName); - Map vendorAttributes = (Map) attributesByCode.get(vendorId); - if (vendorAttributes == null) { - vendorAttributes = new HashMap(); - attributesByCode.put(vendorId, vendorAttributes); - } + Map vendorAttributes = attributesByCode.computeIfAbsent(vendorId, k -> new HashMap<>()); if (vendorAttributes.containsKey(typeCode)) throw new IllegalArgumentException("duplicate type code: " + typeCode); @@ -148,8 +142,8 @@ public void addAttributeType(AttributeType attributeType) { vendorAttributes.put(typeCode, attributeType); } - private Map vendorsByCode = new HashMap(); // - private Map attributesByCode = new HashMap(); // > - private Map attributesByName = new HashMap(); // + private Map vendorsByCode = new HashMap<>(); // + private Map> attributesByCode = new HashMap<>(); // > + private Map attributesByName = new HashMap<>(); // } diff --git a/src/main/java/org/tinyradius/packet/AccessRequest.java b/src/main/java/org/tinyradius/packet/AccessRequest.java index 418eb565..57b4c9f4 100644 --- a/src/main/java/org/tinyradius/packet/AccessRequest.java +++ b/src/main/java/org/tinyradius/packet/AccessRequest.java @@ -45,7 +45,7 @@ public class AccessRequest extends RadiusPacket { */ public static final String AUTH_EAP = "eap"; - public static final Set AUTH_PROTOCOLS = new HashSet(Arrays.asList(AUTH_PAP, AUTH_CHAP, AUTH_MS_CHAP_V2, AUTH_EAP)); + public static final Set AUTH_PROTOCOLS = new HashSet<>(Arrays.asList(AUTH_PAP, AUTH_CHAP, AUTH_MS_CHAP_V2, AUTH_EAP)); /** * Constructs an empty Access-Request packet. @@ -56,11 +56,11 @@ public AccessRequest() { /** * Constructs an Access-Request packet, sets the - * code, identifier and adds an User-Name and an + * code, identifier and adds a User-Name and a * User-Password attribute (PAP). * * @param userName - * user name + * username * @param userPassword * user password */ @@ -74,7 +74,7 @@ public AccessRequest(String userName, String userPassword) { * Sets the User-Name attribute of this Access-Request. * * @param userName - * user name to set + * username to set */ public void setUserName(String userName) { if (userName == null) @@ -110,17 +110,17 @@ public String getUserPassword() { } /** - * Retrieves the user name from the User-Name attribute. + * Retrieves the username from the User-Name attribute. * - * @return user name + * @return username */ public String getUserName() { - List attrs = getAttributes(USER_NAME); - if (attrs.size() < 1 || attrs.size() > 1) + List attrs = getAttributes(USER_NAME); + if (attrs.size() != 1) throw new RuntimeException("exactly one User-Name attribute required"); - RadiusAttribute ra = (RadiusAttribute) attrs.get(0); - return ((StringAttribute) ra).getAttributeValue(); + RadiusAttribute ra = attrs.get(0); + return ra.getAttributeValue(); } /** @@ -152,6 +152,7 @@ public void setAuthProtocol(String authProtocol) { * and CHAP. * * @param plaintext + * Plain text password * @return true if the password is valid, false otherwise */ public boolean verifyPassword(String plaintext) throws RadiusException { @@ -219,24 +220,26 @@ protected void encodeRequestAttributes(String sharedSecret) { // ok for proxied packets whose CHAP password is already encrypted // throw new RuntimeException("no password set"); - if (getAuthProtocol().equals(AUTH_PAP)) { - byte[] pass = encodePapPassword(RadiusUtil.getUtf8Bytes(this.password), RadiusUtil.getUtf8Bytes(sharedSecret)); - removeAttributes(USER_PASSWORD); - addAttribute(new RadiusAttribute(USER_PASSWORD, pass)); - } - else if (getAuthProtocol().equals(AUTH_CHAP)) { - byte[] challenge = createChapChallenge(); - byte[] pass = encodeChapPassword(password, challenge); - removeAttributes(CHAP_PASSWORD); - removeAttributes(CHAP_CHALLENGE); - addAttribute(new RadiusAttribute(CHAP_PASSWORD, pass)); - addAttribute(new RadiusAttribute(CHAP_CHALLENGE, challenge)); - } - else if (getAuthProtocol().equals(AUTH_MS_CHAP_V2)) { - throw new RuntimeException("encoding not supported for " + AUTH_MS_CHAP_V2); - } - else if (getAuthProtocol().equals(AUTH_EAP)) { - throw new RuntimeException("encoding not supported for " + AUTH_EAP); + switch (getAuthProtocol()) { + case AUTH_PAP: { + byte[] pass = encodePapPassword(RadiusUtil.getUtf8Bytes(this.password), RadiusUtil.getUtf8Bytes(sharedSecret)); + removeAttributes(USER_PASSWORD); + addAttribute(new RadiusAttribute(USER_PASSWORD, pass)); + break; + } + case AUTH_CHAP: { + byte[] challenge = createChapChallenge(); + byte[] pass = encodeChapPassword(password, challenge); + removeAttributes(CHAP_PASSWORD); + removeAttributes(CHAP_CHALLENGE); + addAttribute(new RadiusAttribute(CHAP_PASSWORD, pass)); + addAttribute(new RadiusAttribute(CHAP_CHALLENGE, challenge)); + break; + } + case AUTH_MS_CHAP_V2: + throw new RuntimeException("encoding not supported for " + AUTH_MS_CHAP_V2); + case AUTH_EAP: + throw new RuntimeException("encoding not supported for " + AUTH_EAP); } } @@ -254,7 +257,7 @@ private byte[] encodePapPassword(final byte[] userPass, byte[] sharedSecret) { // to 128 bytes. If it isn't a multiple of 16 bytes fill it out with zeroes // to make it a multiple of 16 bytes. If it is greater than 128 bytes // truncate it at 128. - byte[] userPassBytes = null; + byte[] userPassBytes; if (userPass.length > 128) { userPassBytes = new byte[128]; System.arraycopy(userPass, 0, userPassBytes, 0, 128); @@ -264,7 +267,7 @@ private byte[] encodePapPassword(final byte[] userPass, byte[] sharedSecret) { } // declare the byte array to hold the final product - byte[] encryptedPass = null; + byte[] encryptedPass; if (userPassBytes.length < 128) { if (userPassBytes.length % 16 == 0) { // tt is already a multiple of 16 bytes @@ -437,7 +440,7 @@ private boolean verifyChapPassword(String plaintext) throws RadiusException { /** * Random generator */ - private static SecureRandom random = new SecureRandom(); + private static final SecureRandom random = new SecureRandom(); /** * Radius type code for Radius attribute User-Name @@ -478,6 +481,6 @@ private boolean verifyChapPassword(String plaintext) throws RadiusException { /** * Logger for logging information about malformed packets */ - private static Log logger = LogFactory.getLog(AccessRequest.class); + private static final Log logger = LogFactory.getLog(AccessRequest.class); } diff --git a/src/main/java/org/tinyradius/packet/RadiusPacket.java b/src/main/java/org/tinyradius/packet/RadiusPacket.java index e8f34af1..e128244e 100644 --- a/src/main/java/org/tinyradius/packet/RadiusPacket.java +++ b/src/main/java/org/tinyradius/packet/RadiusPacket.java @@ -17,7 +17,6 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.ArrayList; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.tinyradius.attribute.RadiusAttribute; @@ -80,7 +79,7 @@ public class RadiusPacket { * packet type */ public RadiusPacket(final int type) { - this(type, getNextPacketIdentifier(), new ArrayList()); + this(type, getNextPacketIdentifier(), new ArrayList<>()); } /** @@ -93,7 +92,7 @@ public RadiusPacket(final int type) { * packet identifier */ public RadiusPacket(final int type, final int identifier) { - this(type, identifier, new ArrayList()); + this(type, identifier, new ArrayList<>()); } /** @@ -107,7 +106,7 @@ public RadiusPacket(final int type, final int identifier) { * @param attributes * list of RadiusAttribute objects */ - public RadiusPacket(final int type, final int identifier, final List attributes) { + public RadiusPacket(final int type, final int identifier, final List attributes) { setPacketType(type); setPacketIdentifier(identifier); setAttributes(attributes); @@ -226,14 +225,13 @@ public void setPacketType(int type) { * @param attributes * list of RadiusAttribute objects */ - public void setAttributes(List attributes) { + public void setAttributes(List attributes) { if (attributes == null) throw new NullPointerException("attributes list is null"); - for (Iterator i = attributes.iterator(); i.hasNext();) { - Object element = i.next(); - if (!(element instanceof RadiusAttribute)) - throw new IllegalArgumentException("attribute not an instance of RadiusAttribute"); + for (RadiusAttribute radiusAttribute : attributes) { + if (radiusAttribute == null) + throw new NullPointerException("attributes list contains a null"); } this.attributes = attributes; @@ -241,7 +239,7 @@ public void setAttributes(List attributes) { /** * Adds a Radius attribute to this packet. Can also be used - * to add Vendor-Specific sub-attributes. If a attribute with + * to add Vendor-Specific sub-attributes. If an attribute with * a vendor code != -1 is passed in, a VendorSpecificAttribute * is created for the sub-attribute. * @@ -252,9 +250,9 @@ public void addAttribute(RadiusAttribute attribute) { if (attribute == null) throw new NullPointerException("attribute is null"); attribute.setDictionary(getDictionary()); - if (attribute.getVendorId() == -1) + if (attribute.getVendorId() == -1) { this.attributes.add(attribute); - else { + } else { VendorSpecificAttribute vsa = new VendorSpecificAttribute(attribute.getVendorId()); vsa.addSubAttribute(attribute); this.attributes.add(vsa); @@ -263,7 +261,7 @@ public void addAttribute(RadiusAttribute attribute) { /** * Adds a Radius attribute to this packet. - * Uses AttributeTypes to lookup the type code and converts + * Uses AttributeTypes to look up the type code and converts * the value. * Can also be used to add sub-attributes. * @@ -302,10 +300,9 @@ public void removeAttribute(RadiusAttribute attribute) { } else { // remove Vendor-Specific sub-attribute - List vsas = getVendorAttributes(attribute.getVendorId()); - for (Iterator i = vsas.iterator(); i.hasNext();) { - VendorSpecificAttribute vsa = (VendorSpecificAttribute) i.next(); - List sas = vsa.getSubAttributes(); + List vsas = getVendorAttributes(attribute.getVendorId()); + for (VendorSpecificAttribute vsa : vsas) { + List sas = vsa.getSubAttributes(); if (sas.contains(attribute)) { vsa.removeSubAttribute(attribute); if (sas.size() == 1) @@ -328,27 +325,22 @@ public void removeAttributes(int type) { if (type < 1 || type > 255) throw new IllegalArgumentException("attribute type out of bounds"); - Iterator i = attributes.iterator(); - while (i.hasNext()) { - RadiusAttribute attribute = (RadiusAttribute) i.next(); - if (attribute.getAttributeType() == type) - i.remove(); - } + attributes.removeIf(attribute -> attribute.getAttributeType() == type); } /** - * Removes the last occurence of the attribute of the given + * Removes the last occurrence of the attribute of the given * type from the packet. * * @param type * attribute type code */ public void removeLastAttribute(int type) { - List attrs = getAttributes(type); + List attrs = getAttributes(type); if (attrs == null || attrs.size() == 0) return; - RadiusAttribute lastAttribute = (RadiusAttribute) attrs.get(attrs.size() - 1); + RadiusAttribute lastAttribute = attrs.get(attrs.size() - 1); removeAttribute(lastAttribute); } @@ -367,16 +359,10 @@ public void removeAttributes(int vendorId, int typeCode) { return; } - 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() == typeCode && attr.getVendorId() == vendorId) - j.remove(); - } + List vsas = getVendorAttributes(vendorId); + for (VendorSpecificAttribute vsa : vsas) { + List sas = vsa.getSubAttributes(); + sas.removeIf(attr -> attr.getAttributeType() == typeCode && attr.getVendorId() == vendorId); if (sas.size() == 0) // removed the last sub-attribute // --> remove the whole Vendor-Specific attribute @@ -392,13 +378,12 @@ public void removeAttributes(int vendorId, int typeCode) { * type of attributes to get * @return list of RadiusAttribute objects, does not return null */ - public List getAttributes(int attributeType) { + public List getAttributes(int attributeType) { 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(); + LinkedList result = new LinkedList<>(); + for (RadiusAttribute a : attributes) { if (attributeType == a.getAttributeType()) result.add(a); } @@ -416,17 +401,15 @@ public List getAttributes(int attributeType) { * attribute type code * @return list of RadiusAttribute objects, never null */ - public List getAttributes(int vendorId, int attributeType) { + 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(); + LinkedList result = new LinkedList<>(); + List vsas = getVendorAttributes(vendorId); + for (VendorSpecificAttribute vsa : vsas) { + List sas = vsa.getSubAttributes(); + for (RadiusAttribute attr : sas) { if (attr.getAttributeType() == attributeType && attr.getVendorId() == vendorId) result.add(attr); } @@ -441,7 +424,7 @@ public List getAttributes(int vendorId, int attributeType) { * * @return List of RadiusAttribute objects */ - public List getAttributes() { + public List getAttributes() { return attributes; } @@ -453,17 +436,17 @@ public List getAttributes() { * attribute type * @return RadiusAttribute object or null if there is no such attribute * @throws RuntimeException - * if there are multiple occurences of the + * if there are multiple occurrences of the * requested attribute type */ public RadiusAttribute getAttribute(int type) { - List attrs = getAttributes(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); + return attrs.get(0); } /** @@ -476,20 +459,20 @@ else if (attrs.size() == 0) * attribute type * @return RadiusAttribute object or null if there is no such attribute * @throws RuntimeException - * if there are multiple occurences of the + * if there are multiple occurrences of the * requested attribute type */ public RadiusAttribute getAttribute(int vendorId, int type) { if (vendorId == -1) return getAttribute(type); - List attrs = getAttributes(vendorId, 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); + return attrs.get(0); } /** @@ -542,10 +525,9 @@ public String getAttributeValue(String type) { * 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(); + public List getVendorAttributes(int vendorId) { + LinkedList result = new LinkedList<>(); + for (RadiusAttribute a : attributes) { if (a instanceof VendorSpecificAttribute) { VendorSpecificAttribute vsa = (VendorSpecificAttribute) a; if (vsa.getChildVendorId() == vendorId) @@ -568,8 +550,8 @@ public List getVendorAttributes(int vendorId) { * @see #getVendorAttributes(int) */ public VendorSpecificAttribute getVendorAttribute(int vendorId) { - for (Iterator i = getAttributes(VendorSpecificAttribute.VENDOR_SPECIFIC).iterator(); i.hasNext();) { - VendorSpecificAttribute vsa = (VendorSpecificAttribute) i.next(); + for (RadiusAttribute radiusAttribute : getAttributes(VendorSpecificAttribute.VENDOR_SPECIFIC)) { + VendorSpecificAttribute vsa = (VendorSpecificAttribute) radiusAttribute; if (vsa.getChildVendorId() == vendorId) return vsa; } @@ -612,7 +594,7 @@ public void encodeResponsePacket(OutputStream out, String sharedSecret, RadiusPa /** * Reads a Radius request packet from the given input stream and - * creates an appropiate RadiusPacket descendant object. + * creates an appropriate RadiusPacket descendant object. * Reads in all attributes and returns the object. * Decodes the encrypted fields and attributes of the packet. * @@ -634,7 +616,7 @@ public static RadiusPacket decodeRequestPacket(InputStream in, String sharedSecr /** * Reads a Radius response packet from the given input stream and - * creates an appropiate RadiusPacket descendant object. + * creates an appropriate RadiusPacket descendant object. * Reads in all attributes and returns the object. * Checks the packet authenticator. * @@ -656,7 +638,7 @@ public static RadiusPacket decodeResponsePacket(InputStream in, String sharedSec /** * Reads a Radius request packet from the given input stream and - * creates an appropiate RadiusPacket descendant object. + * creates an appropriate RadiusPacket descendant object. * Reads in all attributes and returns the object. * Decodes the encrypted fields and attributes of the packet. * @@ -678,7 +660,7 @@ public static RadiusPacket decodeRequestPacket(Dictionary dictionary, InputStrea /** * Reads a Radius response packet from the given input stream and - * creates an appropiate RadiusPacket descendant object. + * creates an appropriate RadiusPacket descendant object. * Reads in all attributes and returns the object. * Checks the packet authenticator. * @@ -718,8 +700,7 @@ public static synchronized int getNextPacketIdentifier() { /** * Creates a RadiusPacket object. Depending on the passed type, an - * appropriate packet is created. Also sets the type, and the - * the packet identifier. + * appropriate packet is created. Also sets the type, and the packet identifier. * * @param type * packet type @@ -753,12 +734,11 @@ public static RadiusPacket createRadiusPacket(final int type) { * @see java.lang.Object#toString() */ public String toString() { - StringBuffer s = new StringBuffer(); + StringBuilder s = new StringBuilder(); s.append(getPacketTypeName()); s.append(", ID "); s.append(packetIdentifier); - for (Iterator i = attributes.iterator(); i.hasNext();) { - RadiusAttribute attr = (RadiusAttribute) i.next(); + for (RadiusAttribute attr : attributes) { s.append("\n"); s.append(attr.toString()); } @@ -781,7 +761,7 @@ public byte[] getAuthenticator() { /** * Sets the authenticator to be used for this Radius packet. - * This method should seldomly be used. + * This method should seldom be used. * Authenticators are created and managed by this class internally. * * @param authenticator @@ -811,8 +791,7 @@ public Dictionary getDictionary() { */ public void setDictionary(Dictionary dictionary) { this.dictionary = dictionary; - for (Iterator i = attributes.iterator(); i.hasNext();) { - RadiusAttribute attr = (RadiusAttribute) i.next(); + for (RadiusAttribute attr : attributes) { attr.setDictionary(dictionary); } } @@ -881,6 +860,8 @@ protected void encodePacket(OutputStream out, String sharedSecret, RadiusPacket * authenticator. * * @param sharedSecret + * shared secret that secures the communication + * with the other Radius server/client */ protected void encodeRequestAttributes(String sharedSecret) { } @@ -950,7 +931,7 @@ protected byte[] createResponseAuthenticator(String sharedSecret, int packetLeng /** * Reads a Radius packet from the given input stream and - * creates an appropiate RadiusPacket descendant object. + * creates an appropriate RadiusPacket descendant object. * Reads in all attributes and returns the object. * Decodes the encrypted fields and attributes of the packet. * @@ -1004,7 +985,6 @@ public static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, // 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"); @@ -1012,7 +992,6 @@ public static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, 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"); @@ -1051,7 +1030,7 @@ public static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, /** * Checks the request authenticator against the supplied shared secret. - * Overriden by AccountingRequest to handle special accounting request + * Overridden by AccountingRequest to handle special accounting request * authenticators. There is no way to check request authenticators for * authentication requests as they contain secret bytes. * @@ -1062,24 +1041,28 @@ public static RadiusPacket decodePacket(Dictionary dictionary, InputStream in, * @param attributes * request attribute data * @throws RadiusException + * RadiusException */ protected void checkRequestAuthenticator(String sharedSecret, int packetLength, byte[] attributes) throws RadiusException { } /** - * Can be overriden to decode encoded request attributes such as + * Can be overridden to decode encoded request attributes such as * User-Password. This method may use getAuthenticator() to get the * request authenticator. * * @param sharedSecret + * shared secret that secures the communication + * with the other Radius server/client * @throws RadiusException + * RadiusException */ protected void decodeRequestAttributes(String sharedSecret) throws RadiusException { } /** * This method checks the authenticator of this Radius packet. This method - * may be overriden to include special attributes in the authenticator check. + * may be overridden to include special attributes in the authenticator check. * * @param sharedSecret * shared secret to be used to encrypt the authenticator @@ -1125,8 +1108,7 @@ protected MessageDigest getMd5Digest() { */ protected byte[] getAttributeBytes() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(MAX_PACKET_LENGTH); - for (Iterator i = attributes.iterator(); i.hasNext();) { - RadiusAttribute a = (RadiusAttribute) i.next(); + for (RadiusAttribute a : attributes) { bos.write(a.writeAttribute()); } bos.flush(); @@ -1146,7 +1128,7 @@ protected byte[] getAttributeBytes() throws IOException { /** * Attributes for this packet. */ - private List attributes = new ArrayList(); + private List attributes = new ArrayList<>(); /** * MD5 digest. @@ -1171,6 +1153,6 @@ protected byte[] getAttributeBytes() throws IOException { /** * Random number generator. */ - private static SecureRandom random = new SecureRandom(); + private static final SecureRandom random = new SecureRandom(); } diff --git a/src/main/java/org/tinyradius/proxy/RadiusProxy.java b/src/main/java/org/tinyradius/proxy/RadiusProxy.java index 7e5d0261..65e82993 100644 --- a/src/main/java/org/tinyradius/proxy/RadiusProxy.java +++ b/src/main/java/org/tinyradius/proxy/RadiusProxy.java @@ -107,6 +107,7 @@ public void setProxyPort(int proxyPort) { * @param socketTimeout * socket timeout, >0 ms * @throws SocketException + * Socket Exception */ public void setSocketTimeout(int socketTimeout) throws SocketException { super.setSocketTimeout(socketTimeout); @@ -119,6 +120,7 @@ public void setSocketTimeout(int socketTimeout) throws SocketException { * * @return socket * @throws SocketException + * Socket Exception */ protected DatagramSocket getProxySocket() throws SocketException { if (proxySocket == null) { @@ -167,17 +169,18 @@ protected RadiusPacket handlePacket(InetSocketAddress localAddress, InetSocketAd * @param remote * the server the packet arrived from * @throws IOException + * IO Exception */ protected void proxyPacketReceived(RadiusPacket packet, InetSocketAddress remote) throws IOException, RadiusException { // retrieve my Proxy-State attribute (the last) - List proxyStates = packet.getAttributes(33); + List proxyStates = packet.getAttributes(33); if (proxyStates == null || proxyStates.size() == 0) throw new RadiusException("proxy packet without Proxy-State attribute"); - RadiusAttribute proxyState = (RadiusAttribute) proxyStates.get(proxyStates.size() - 1); + RadiusAttribute proxyState = proxyStates.get(proxyStates.size() - 1); // retrieve proxy connection from cache String state = new String(proxyState.getAttributeData()); - RadiusProxyConnection proxyConnection = (RadiusProxyConnection) proxyConnections.remove(state); + RadiusProxyConnection proxyConnection = proxyConnections.remove(state); if (proxyConnection == null) { logger.warn("received packet on proxy port without saved proxy connection - duplicate?"); return; @@ -214,9 +217,10 @@ protected void proxyPacketReceived(RadiusPacket packet, InetSocketAddress remote * * @param packet * the packet to proxy - * @param proxyCon + * @param proxyConnection * the RadiusProxyConnection for this packet * @throws IOException + * IO Exception */ protected void proxyPacket(RadiusPacket packet, RadiusProxyConnection proxyConnection) throws IOException { synchronized (RadiusProxy.class) { @@ -261,10 +265,10 @@ protected void proxyPacket(RadiusPacket packet, RadiusProxyConnection proxyConne * without a received response. * Key: Proxy Index (String), Value: RadiusProxyConnection */ - private Map proxyConnections = new HashMap(); + private Map proxyConnections = new HashMap<>(); private int proxyPort = 1814; private DatagramSocket proxySocket = null; - private static Log logger = LogFactory.getLog(RadiusProxy.class); + private static final Log logger = LogFactory.getLog(RadiusProxy.class); } From 439413c12d5597147260558006eb2e90a06fd3a3 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 22:42:13 -0600 Subject: [PATCH 10/21] Code cleanup for Java 8 compiler --- .../attribute/IntegerAttribute.java | 4 +- .../org/tinyradius/attribute/IpAttribute.java | 3 +- .../tinyradius/attribute/Ipv6Attribute.java | 1 - .../dictionary/DefaultDictionary.java | 4 +- .../tinyradius/packet/AccountingRequest.java | 26 +++++----- .../org/tinyradius/test/TestDictionary.java | 4 -- .../java/org/tinyradius/test/TestServer.java | 6 +-- .../org/tinyradius/util/RadiusClient.java | 16 +++--- .../org/tinyradius/util/RadiusServer.java | 50 ++++++++++--------- .../java/org/tinyradius/util/RadiusUtil.java | 6 +-- 10 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/IntegerAttribute.java b/src/main/java/org/tinyradius/attribute/IntegerAttribute.java index 5aeed728..fad7dda8 100644 --- a/src/main/java/org/tinyradius/attribute/IntegerAttribute.java +++ b/src/main/java/org/tinyradius/attribute/IntegerAttribute.java @@ -56,7 +56,7 @@ public String getAttributeValue() { return name; } // Radius uses only unsigned values.... - return Long.toString(((long)value & 0xffffffffl)); + return Long.toString(((long)value & 0xffffffffL)); } /** @@ -82,7 +82,7 @@ public void setAttributeValue(String value) { if (at != null) { Integer val = at.getEnumeration(value); if (val != null) { - setAttributeValue(val.intValue()); + setAttributeValue(val); return; } } diff --git a/src/main/java/org/tinyradius/attribute/IpAttribute.java b/src/main/java/org/tinyradius/attribute/IpAttribute.java index 5c0e812f..8b090041 100644 --- a/src/main/java/org/tinyradius/attribute/IpAttribute.java +++ b/src/main/java/org/tinyradius/attribute/IpAttribute.java @@ -48,7 +48,7 @@ public IpAttribute(int type, long ipNum) { * @see org.tinyradius.attribute.RadiusAttribute#getAttributeValue() */ public String getAttributeValue() { - StringBuffer ip = new StringBuffer(); + StringBuilder ip = new StringBuilder(); byte[] data = getAttributeData(); if (data == null || data.length != 4) throw new RuntimeException("ip attribute: expected 4 bytes attribute data"); @@ -107,6 +107,7 @@ public long getIpAsLong() { * Sets the IP number represented by this IpAttribute * as a 32 bit unsigned number. * @param ip + * ip address */ public void setIpAsLong(long ip) { byte[] data = new byte[4]; diff --git a/src/main/java/org/tinyradius/attribute/Ipv6Attribute.java b/src/main/java/org/tinyradius/attribute/Ipv6Attribute.java index dfe30f33..6afed965 100644 --- a/src/main/java/org/tinyradius/attribute/Ipv6Attribute.java +++ b/src/main/java/org/tinyradius/attribute/Ipv6Attribute.java @@ -4,7 +4,6 @@ */ package org.tinyradius.attribute; -import java.util.StringTokenizer; import java.net.Inet6Address; import java.net.UnknownHostException; diff --git a/src/main/java/org/tinyradius/dictionary/DefaultDictionary.java b/src/main/java/org/tinyradius/dictionary/DefaultDictionary.java index 0ff44873..ed44d524 100644 --- a/src/main/java/org/tinyradius/dictionary/DefaultDictionary.java +++ b/src/main/java/org/tinyradius/dictionary/DefaultDictionary.java @@ -37,9 +37,9 @@ private DefaultDictionary() { private static final String DICTIONARY_RESOURCE = "org/tinyradius/dictionary/default_dictionary"; private static DefaultDictionary instance = null; - /** + /* * Creates the singleton instance of this object - * and parses the classpath ressource. + * and parses the classpath resource. */ static { try { diff --git a/src/main/java/org/tinyradius/packet/AccountingRequest.java b/src/main/java/org/tinyradius/packet/AccountingRequest.java index 69791c04..6c9b349e 100644 --- a/src/main/java/org/tinyradius/packet/AccountingRequest.java +++ b/src/main/java/org/tinyradius/packet/AccountingRequest.java @@ -50,7 +50,7 @@ public class AccountingRequest extends RadiusPacket { * Constructs an Accounting-Request packet to be sent to a Radius server. * * @param userName - * user name + * username * @param acctStatusType * ACCT_STATUS_TYPE_* */ @@ -69,10 +69,10 @@ public AccountingRequest() { } /** - * Sets the User-Name attribute of this Accountnig-Request. + * Sets the User-Name attribute of this Accounting-Request. * * @param userName - * user name to set + * username to set */ public void setUserName(String userName) { if (userName == null) @@ -85,22 +85,23 @@ public void setUserName(String userName) { } /** - * Retrieves the user name from the User-Name attribute. + * Retrieves the username from the User-Name attribute. * - * @return user name + * @return username * @throws RadiusException + * Radius Exception */ public String getUserName() throws RadiusException { - List attrs = getAttributes(USER_NAME); - if (attrs.size() < 1 || attrs.size() > 1) + List attrs = getAttributes(USER_NAME); + if (attrs.size() != 1) throw new RuntimeException("exactly one User-Name attribute required"); - RadiusAttribute ra = (RadiusAttribute) attrs.get(0); - return ((StringAttribute) ra).getAttributeValue(); + RadiusAttribute ra = attrs.get(0); + return ra.getAttributeValue(); } /** - * Sets the Acct-Status-Type attribute of this Accountnig-Request. + * Sets the Acct-Status-Type attribute of this Accounting-Request. * * @param acctStatusType * ACCT_STATUS_TYPE_* to set @@ -113,10 +114,11 @@ public void setAcctStatusType(int acctStatusType) { } /** - * Retrieves the user name from the User-Name attribute. + * Retrieves the username from the User-Name attribute. * - * @return user name + * @return username * @throws RadiusException + * Radius Exception */ public int getAcctStatusType() throws RadiusException { RadiusAttribute ra = getAttribute(ACCT_STATUS_TYPE); diff --git a/src/main/java/org/tinyradius/test/TestDictionary.java b/src/main/java/org/tinyradius/test/TestDictionary.java index c92f73d9..3f230544 100644 --- a/src/main/java/org/tinyradius/test/TestDictionary.java +++ b/src/main/java/org/tinyradius/test/TestDictionary.java @@ -6,15 +6,11 @@ */ package org.tinyradius.test; -import java.io.FileInputStream; -import java.io.InputStream; - import org.tinyradius.attribute.IpAttribute; import org.tinyradius.attribute.Ipv6Attribute; import org.tinyradius.attribute.Ipv6PrefixAttribute; import org.tinyradius.dictionary.Dictionary; import org.tinyradius.dictionary.DefaultDictionary; -import org.tinyradius.dictionary.DictionaryParser; import org.tinyradius.packet.AccessRequest; /** diff --git a/src/main/java/org/tinyradius/test/TestServer.java b/src/main/java/org/tinyradius/test/TestServer.java index 4bf63492..bf66fbcc 100644 --- a/src/main/java/org/tinyradius/test/TestServer.java +++ b/src/main/java/org/tinyradius/test/TestServer.java @@ -7,7 +7,6 @@ */ package org.tinyradius.test; -import java.io.IOException; import java.net.InetSocketAddress; import org.tinyradius.packet.AccessRequest; import org.tinyradius.packet.RadiusPacket; @@ -22,9 +21,10 @@ public class TestServer { /** - * @throws IOException + * @throws Exception + * Exception */ - public static void main(String[] args) throws IOException, Exception { + public static void main(String[] args) throws Exception { RadiusServer server = new RadiusServer() { // Authorize localhost/testing123 public String getSharedSecret(InetSocketAddress client) { diff --git a/src/main/java/org/tinyradius/util/RadiusClient.java b/src/main/java/org/tinyradius/util/RadiusClient.java index 158668b5..bdf9ec8a 100644 --- a/src/main/java/org/tinyradius/util/RadiusClient.java +++ b/src/main/java/org/tinyradius/util/RadiusClient.java @@ -59,7 +59,7 @@ public RadiusClient(RadiusEndpoint client) { * Authenticates a user via PAP. * * @param userName - * user name + * username * @param password * password * @return true if authentication is successful, false otherwise @@ -77,7 +77,7 @@ public synchronized boolean authenticate(String userName, String password) throw * Authenticates a user. * * @param userName - * user name + * username * @param password * password * @param protocol @@ -251,10 +251,11 @@ public int getSocketTimeout() { * @param socketTimeout * timeout, ms, >0 * @throws SocketException + * Socket Exception */ public void setSocketTimeout(int socketTimeout) throws SocketException { if (socketTimeout < 1) - throw new IllegalArgumentException("socket tiemout must be positive"); + throw new IllegalArgumentException("socket timeout must be positive"); this.socketTimeout = socketTimeout; if (serverSocket != null) serverSocket.setSoTimeout(socketTimeout); @@ -368,6 +369,7 @@ public static RadiusPacket communicate(RadiusEndpoint remoteServer, RadiusPacket * * @return local socket * @throws SocketException + * Socket Exception */ protected DatagramSocket getSocket() throws SocketException { if (serverSocket == null || serverSocket.isClosed()) { @@ -378,7 +380,7 @@ protected DatagramSocket getSocket() throws SocketException { } /** - * Creates a datagram packet from a RadiusPacket to be send. + * Creates a datagram packet from a RadiusPacket to be sent. * * @param packet * RadiusPacket @@ -386,6 +388,7 @@ protected DatagramSocket getSocket() throws SocketException { * destination port number * @return new datagram packet * @throws IOException + * IO Exception */ protected DatagramPacket makeDatagramPacket(RadiusPacket packet, int port) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -393,8 +396,7 @@ protected DatagramPacket makeDatagramPacket(RadiusPacket packet, int port) throw byte[] data = bos.toByteArray(); InetAddress address = InetAddress.getByName(getHostName()); - DatagramPacket datagram = new DatagramPacket(data, data.length, address, port); - return datagram; + return new DatagramPacket(data, data.length, address, port); } /** @@ -419,6 +421,6 @@ protected RadiusPacket makeRadiusPacket(DatagramPacket packet, RadiusPacket requ private int retryCount = 3; private int socketTimeout = 3000; private String authProtocol = AccessRequest.AUTH_PAP; - private static Log logger = LogFactory.getLog(RadiusClient.class); + private static final Log logger = LogFactory.getLog(RadiusClient.class); } diff --git a/src/main/java/org/tinyradius/util/RadiusServer.java b/src/main/java/org/tinyradius/util/RadiusServer.java index d361e697..95d88a59 100644 --- a/src/main/java/org/tinyradius/util/RadiusServer.java +++ b/src/main/java/org/tinyradius/util/RadiusServer.java @@ -57,13 +57,13 @@ public abstract class RadiusServer { * passed IP address and the received packet data or null if the client * is not allowed at this server. * - * for compatiblity this standard implementation just call the getSharedSecret(InetSocketAddress) method - * and should be overrived when necessary + * for compatibility this standard implementation just call the getSharedSecret(InetSocketAddress) method + * and should be overridden when necessary * * @param client * IP address and port number of client * @param packet - * packet received from client, the packettype comes as RESERVED, + * packet received from client, the packet type comes as RESERVED, * because for some packets the secret is necessary for decoding * @return shared secret or null */ @@ -73,17 +73,17 @@ public String getSharedSecret(InetSocketAddress client, RadiusPacket packet) { /** * Returns the password of the passed user. Either this - * method or accessRequestReceived() should be overriden. + * method or accessRequestReceived() should be overridden. * * @param userName - * user name + * username * @return plain-text password or null if user unknown */ public abstract String getUserPassword(String userName); /** * Constructs an answer for an Access-Request packet. Either this - * method or isUserAuthenticated should be overriden. + * method or isUserAuthenticated should be overridden. * * @param accessRequest * Radius request packet @@ -107,7 +107,7 @@ public RadiusPacket accessRequestReceived(AccessRequest accessRequest, InetSocke /** * Constructs an answer for an Accounting-Request packet. This method - * should be overriden if accounting is supported. + * should be overridden if accounting is supported. * * @param accountingRequest * Radius request packet @@ -227,10 +227,11 @@ public int getSocketTimeout() { * @param socketTimeout * socket timeout, >0 ms * @throws SocketException + * Socket Exception */ public void setSocketTimeout(int socketTimeout) throws SocketException { if (socketTimeout < 1) - throw new IllegalArgumentException("socket tiemout must be positive"); + throw new IllegalArgumentException("socket timeout must be positive"); this.socketTimeout = socketTimeout; if (authSocket != null) authSocket.setSoTimeout(socketTimeout); @@ -329,9 +330,8 @@ public void setListenAddress(InetAddress listenAddress) { * response packet */ protected void copyProxyState(RadiusPacket request, RadiusPacket answer) { - List proxyStateAttrs = request.getAttributes(33); - for (Iterator i = proxyStateAttrs.iterator(); i.hasNext();) { - RadiusAttribute proxyStateAttr = (RadiusAttribute) i.next(); + List proxyStateAttrs = request.getAttributes(33); + for (RadiusAttribute proxyStateAttr : proxyStateAttrs) { answer.addAttribute(proxyStateAttr); } } @@ -341,8 +341,8 @@ protected void copyProxyState(RadiusPacket request, RadiusPacket answer) { * Returns when stop() is called. * * @throws SocketException - * @throws InterruptedException - * + * Socket Exception + * */ protected void listenAuth() throws SocketException { listen(getAuthSocket()); @@ -353,7 +353,7 @@ protected void listenAuth() throws SocketException { * Returns when stop() is called. * * @throws SocketException - * @throws InterruptedException + * Socket Exception */ protected void listenAcct() throws SocketException { listen(getAcctSocket()); @@ -392,12 +392,12 @@ protected void listen(final DatagramSocket s) { } else { executor.submit(new Runnable() { - + @Override public void run() { processRequest(s, packetIn); } - + }); } } @@ -472,9 +472,12 @@ protected void processRequest(final DatagramSocket s, final DatagramPacket packe * @param request * the packet * @param sharedSecret + * shared secret * @return response packet or null for no response * @throws RadiusException + * Radius Exception * @throws IOException + * IO Exception */ protected RadiusPacket handlePacket(InetSocketAddress localAddress, InetSocketAddress remoteAddress, RadiusPacket request, String sharedSecret) throws RadiusException, IOException { @@ -496,9 +499,8 @@ else if (localAddress.getPort() == getAcctPort()) { else logger.error("unknown Radius packet type: " + request.getPacketType()); } - else { - // ignore packet on unknown port - } + // ignore packet on unknown port + } else logger.info("ignore duplicate packet"); @@ -511,6 +513,7 @@ else if (localAddress.getPort() == getAcctPort()) { * * @return socket * @throws SocketException + * Socket Exception */ protected DatagramSocket getAuthSocket() throws SocketException { if (authSocket == null) { @@ -528,6 +531,7 @@ protected DatagramSocket getAuthSocket() throws SocketException { * * @return socket * @throws SocketException + * Socket Exception */ protected DatagramSocket getAcctSocket() throws SocketException { if (acctSocket == null) { @@ -541,7 +545,7 @@ protected DatagramSocket getAcctSocket() throws SocketException { } /** - * Creates a Radius response datagram packet from a RadiusPacket to be send. + * Creates a Radius response datagram packet from a RadiusPacket to be sent. * * @param packet * RadiusPacket @@ -555,6 +559,7 @@ protected DatagramSocket getAcctSocket() throws SocketException { * request packet * @return new datagram packet * @throws IOException + * IO Exception */ protected DatagramPacket makeDatagramPacket(RadiusPacket packet, String secret, InetAddress address, int port, RadiusPacket request) throws IOException { @@ -562,8 +567,7 @@ protected DatagramPacket makeDatagramPacket(RadiusPacket packet, String secret, packet.encodeResponsePacket(bos, secret, request); byte[] data = bos.toByteArray(); - DatagramPacket datagram = new DatagramPacket(data, data.length, address, port); - return datagram; + return new DatagramPacket(data, data.length, address, port); } /** @@ -637,6 +641,6 @@ protected boolean isPacketDuplicate(RadiusPacket packet, InetSocketAddress addre private long lastClean; private long duplicateInterval = 30000; // 30 s protected transient boolean closing = false; - private static Log logger = LogFactory.getLog(RadiusServer.class); + private static final Log logger = LogFactory.getLog(RadiusServer.class); } diff --git a/src/main/java/org/tinyradius/util/RadiusUtil.java b/src/main/java/org/tinyradius/util/RadiusUtil.java index 0b80320f..3e033e50 100644 --- a/src/main/java/org/tinyradius/util/RadiusUtil.java +++ b/src/main/java/org/tinyradius/util/RadiusUtil.java @@ -40,10 +40,10 @@ public static String getStringFromUtf8(byte[] utf8) { * @return hex string */ public static String getHexString(byte[] data) { - StringBuffer hex = new StringBuffer("0x"); + StringBuilder hex = new StringBuilder("0x"); if (data != null) - for (int i = 0; i < data.length; i++) { - String digit = Integer.toString(data[i] & 0x0ff, 16); + for (byte datum : data) { + String digit = Integer.toString(datum & 0x0ff, 16); if (digit.length() < 2) hex.append('0'); hex.append(digit); From f03e8efb2ead77a0b35e6e1549f1a890c463e982 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Wed, 22 Dec 2021 22:52:08 -0600 Subject: [PATCH 11/21] Additional code cleanup for Java 8 compiler --- src/main/java/org/tinyradius/packet/RadiusPacket.java | 1 + .../java/org/tinyradius/attribute/IntegerAttributeTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/tinyradius/packet/RadiusPacket.java b/src/main/java/org/tinyradius/packet/RadiusPacket.java index e128244e..82904483 100644 --- a/src/main/java/org/tinyradius/packet/RadiusPacket.java +++ b/src/main/java/org/tinyradius/packet/RadiusPacket.java @@ -549,6 +549,7 @@ public List getVendorAttributes(int vendorId) { * @deprecated use getVendorAttributes(int) * @see #getVendorAttributes(int) */ + @Deprecated public VendorSpecificAttribute getVendorAttribute(int vendorId) { for (RadiusAttribute radiusAttribute : getAttributes(VendorSpecificAttribute.VENDOR_SPECIFIC)) { VendorSpecificAttribute vsa = (VendorSpecificAttribute) radiusAttribute; diff --git a/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java b/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java index d8d59982..d95b375b 100644 --- a/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java +++ b/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java @@ -9,7 +9,7 @@ public class IntegerAttributeTest { @Test public void test() { final IntegerAttribute intAttr = new IntegerAttribute(27, 0); - final long bigValue = 0xffffffffl; // big value with highest bit set + final long bigValue = 0xffffffffL; // big value with highest bit set System.err.println((int)bigValue); System.err.println(bigValue); final String bigValueSt = Long.toString(bigValue); From e609db4ab137dd5c24be43e469b86e645ba54d76 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 26 Dec 2021 12:07:44 -0600 Subject: [PATCH 12/21] Convert raw Class type for AttributeType and add type checking --- .../org/tinyradius/attribute/RadiusAttribute.java | 2 +- .../java/org/tinyradius/dictionary/AttributeType.java | 10 +++++----- .../org/tinyradius/dictionary/DictionaryParser.java | 11 +++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/RadiusAttribute.java b/src/main/java/org/tinyradius/attribute/RadiusAttribute.java index 2059b79a..995fa4a5 100644 --- a/src/main/java/org/tinyradius/attribute/RadiusAttribute.java +++ b/src/main/java/org/tinyradius/attribute/RadiusAttribute.java @@ -234,7 +234,7 @@ public static RadiusAttribute createRadiusAttribute(Dictionary dictionary, int v AttributeType at = dictionary.getAttributeTypeByCode(vendorId, attributeType); if (at != null && at.getAttributeClass() != null) { try { - attribute = (RadiusAttribute) at.getAttributeClass().newInstance(); + attribute = at.getAttributeClass().newInstance(); } catch (Exception e) { // error instantiating class - should not occur diff --git a/src/main/java/org/tinyradius/dictionary/AttributeType.java b/src/main/java/org/tinyradius/dictionary/AttributeType.java index b839702d..4a70b662 100644 --- a/src/main/java/org/tinyradius/dictionary/AttributeType.java +++ b/src/main/java/org/tinyradius/dictionary/AttributeType.java @@ -26,7 +26,7 @@ public class AttributeType { * RadiusAttribute descendant who handles * attributes of this type */ - public AttributeType(int code, String name, Class type) { + public AttributeType(int code, String name, Class type) { setTypeCode(code); setName(name); setAttributeClass(type); @@ -44,7 +44,7 @@ public AttributeType(int code, String name, Class type) { * @param type * sub-attribute class */ - public AttributeType(int vendor, int code, String name, Class type) { + public AttributeType(int vendor, int code, String name, Class type) { setTypeCode(code); setName(name); setAttributeClass(type); @@ -99,7 +99,7 @@ public void setName(String name) { * * @return class */ - public Class getAttributeClass() { + public Class getAttributeClass() { return attributeClass; } @@ -107,7 +107,7 @@ public Class getAttributeClass() { * Sets the RadiusAttribute descendant class which represents * attributes of this type. */ - public void setAttributeClass(Class type) { + public void setAttributeClass(Class type) { if (type == null) throw new NullPointerException("type is null"); if (!RadiusAttribute.class.isAssignableFrom(type)) @@ -202,7 +202,7 @@ public String toString() { private int vendorId = -1; private int typeCode; private String name; - private Class attributeClass; + private Class attributeClass; private Map enumeration = null; } diff --git a/src/main/java/org/tinyradius/dictionary/DictionaryParser.java b/src/main/java/org/tinyradius/dictionary/DictionaryParser.java index 7062ea51..d0446dc0 100644 --- a/src/main/java/org/tinyradius/dictionary/DictionaryParser.java +++ b/src/main/java/org/tinyradius/dictionary/DictionaryParser.java @@ -102,7 +102,7 @@ private static void parseAttributeLine(WritableDictionary dictionary, StringToke String typeStr = tok.nextToken().trim(); // translate type to class - Class type; + Class type; if (code == VendorSpecificAttribute.VENDOR_SPECIFIC) type = VendorSpecificAttribute.class; else @@ -143,7 +143,7 @@ private static void parseVendorAttributeLine(WritableDictionary dictionary, Stri int code = Integer.parseInt(tok.nextToken().trim()); String typeStr = tok.nextToken().trim(); - Class type = getAttributeTypeClass(code, typeStr); + Class type = getAttributeTypeClass(code, typeStr); AttributeType at = new AttributeType(Integer.parseInt(vendor), code, name, type); dictionary.addAttributeType(at); } @@ -192,12 +192,11 @@ private static void includeDictionaryFile(WritableDictionary dictionary, StringT * string|octets|integer|date|ipaddr|ipv6addr|ipv6prefix * @return RadiusAttribute class or descendant */ - private static Class getAttributeTypeClass(int attributeType, String typeStr) { - Class type = RadiusAttribute.class; + private static Class getAttributeTypeClass(int attributeType, String typeStr) { + // Default type is RadiusAttribute + Class type = RadiusAttribute.class; if (typeStr.equalsIgnoreCase("string")) type = StringAttribute.class; - else if (typeStr.equalsIgnoreCase("octets")) - type = RadiusAttribute.class; else if (typeStr.equalsIgnoreCase("integer") || typeStr.equalsIgnoreCase("date")) type = IntegerAttribute.class; else if (typeStr.equalsIgnoreCase("ipaddr")) From 88009c93c205d46487f636f75378df0b985633c3 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 26 Dec 2021 12:10:32 -0600 Subject: [PATCH 13/21] Use auto resource management for try block of RadiusClient/Datagram socket --- src/main/java/org/tinyradius/util/RadiusClient.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/tinyradius/util/RadiusClient.java b/src/main/java/org/tinyradius/util/RadiusClient.java index bdf9ec8a..219e726e 100644 --- a/src/main/java/org/tinyradius/util/RadiusClient.java +++ b/src/main/java/org/tinyradius/util/RadiusClient.java @@ -313,8 +313,7 @@ public RadiusPacket communicate(RadiusPacket request, int port) throws IOExcepti DatagramPacket packetIn = new DatagramPacket(new byte[RadiusPacket.MAX_PACKET_LENGTH], RadiusPacket.MAX_PACKET_LENGTH); DatagramPacket packetOut = makeDatagramPacket(request, port); - DatagramSocket socket = getSocket(); - try { + try (DatagramSocket socket = getSocket()) { for (int i = 1; i <= getRetryCount(); i++) { try { socket.send(packetOut); @@ -337,8 +336,6 @@ public RadiusPacket communicate(RadiusPacket request, int port) throws IOExcepti // calculated again (call makeDatagramPacket) } } - }finally { - socket.close(); } return null; From 938463fd7dca7fa7d8ec73427e4997dd43e3f67c Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Mon, 27 Dec 2021 09:27:54 -0600 Subject: [PATCH 14/21] Use Java 11 with release 8 for compiling in CI --- .github/workflows/ci.yml | 6 +++--- pom.xml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6fb3461a..4a105d11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,10 +10,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Set up JDK 1.8 + - name: Set up JDK 11 uses: actions/setup-java@v2 with: - java-version: 8 - distribution: adopt + java-version: 11 + distribution: temurin - name: Build with Maven run: mvn -V -B package --file pom.xml diff --git a/pom.xml b/pom.xml index 5046d9d8..b0102062 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,7 @@ true -Xlint:unchecked - + 8 From 2d449256ad8a94f23699782e3dfca659a21c30a5 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Mon, 27 Dec 2021 16:35:27 -0600 Subject: [PATCH 15/21] Migrate to Junit 5 --- pom.xml | 12 +++++++++--- .../tinyradius/attribute/IntegerAttributeTest.java | 13 +++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index b0102062..691de592 100644 --- a/pom.xml +++ b/pom.xml @@ -43,9 +43,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter + 5.8.2 test @@ -88,6 +88,12 @@ 8 + + org.apache.maven.plugins + maven-surefire-plugin + + 2.22.2 + diff --git a/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java b/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java index d95b375b..fa31ae09 100644 --- a/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java +++ b/src/test/java/org/tinyradius/attribute/IntegerAttributeTest.java @@ -1,20 +1,21 @@ package org.tinyradius.attribute; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; -import org.junit.Test; public class IntegerAttributeTest { @Test public void test() { final IntegerAttribute intAttr = new IntegerAttribute(27, 0); - final long bigValue = 0xffffffffL; // big value with highest bit set - System.err.println((int)bigValue); - System.err.println(bigValue); + final long bigValue = 0xffffffffL; // big value with the highest bit set final String bigValueSt = Long.toString(bigValue); intAttr.setAttributeValue(bigValueSt); - assertEquals(bigValueSt, intAttr.getAttributeValue()); + //System.err.println(intAttr.getAttributeTypeObject().getName()); + Assertions.assertEquals(IntegerAttribute.class, intAttr.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("Session-Timeout", intAttr.getAttributeTypeObject().getName()); + Assertions.assertEquals(bigValueSt, intAttr.getAttributeValue()); } } From e01952fdb94ae7af4b995cb6c3a967a323c851de Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Mon, 27 Dec 2021 16:35:41 -0600 Subject: [PATCH 16/21] Add some unit tests for Attribute objects --- .../tinyradius/attribute/IpAttributeTest.java | 18 +++++++++++++++++ .../attribute/Ipv6AttributeTest.java | 17 ++++++++++++++++ .../attribute/Ipv6PrefixAttributeTest.java | 18 +++++++++++++++++ .../attribute/StringAttributeTest.java | 18 +++++++++++++++++ .../VendorSpecificAttributeTest.java | 20 +++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 src/test/java/org/tinyradius/attribute/IpAttributeTest.java create mode 100644 src/test/java/org/tinyradius/attribute/Ipv6AttributeTest.java create mode 100644 src/test/java/org/tinyradius/attribute/Ipv6PrefixAttributeTest.java create mode 100644 src/test/java/org/tinyradius/attribute/StringAttributeTest.java create mode 100644 src/test/java/org/tinyradius/attribute/VendorSpecificAttributeTest.java diff --git a/src/test/java/org/tinyradius/attribute/IpAttributeTest.java b/src/test/java/org/tinyradius/attribute/IpAttributeTest.java new file mode 100644 index 00000000..3bec57bb --- /dev/null +++ b/src/test/java/org/tinyradius/attribute/IpAttributeTest.java @@ -0,0 +1,18 @@ +package org.tinyradius.attribute; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class IpAttributeTest { + + @Test + public void test() { + final IpAttribute ipAttr = new IpAttribute(8,"192.168.1.2"); + //System.err.println(ipAttr.getAttributeTypeObject().getName()); + + Assertions.assertEquals(IpAttribute.class, ipAttr.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("Framed-IP-Address", ipAttr.getAttributeTypeObject().getName()); + Assertions.assertEquals(3232235778L, ipAttr.getIpAsLong()); + } + +} diff --git a/src/test/java/org/tinyradius/attribute/Ipv6AttributeTest.java b/src/test/java/org/tinyradius/attribute/Ipv6AttributeTest.java new file mode 100644 index 00000000..8a6907c2 --- /dev/null +++ b/src/test/java/org/tinyradius/attribute/Ipv6AttributeTest.java @@ -0,0 +1,17 @@ +package org.tinyradius.attribute; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class Ipv6AttributeTest { + + @Test + public void test() { + final String testIpv6Addr = "2001:db8:85a3:0:0:0:0:1234"; + final Ipv6Attribute ipv6Attr = new Ipv6Attribute(168, testIpv6Addr); + Assertions.assertEquals(Ipv6Attribute.class, ipv6Attr.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("Framed-IPv6-Address", ipv6Attr.getAttributeTypeObject().getName()); + Assertions.assertEquals(testIpv6Addr, ipv6Attr.getAttributeValue()); + } + +} diff --git a/src/test/java/org/tinyradius/attribute/Ipv6PrefixAttributeTest.java b/src/test/java/org/tinyradius/attribute/Ipv6PrefixAttributeTest.java new file mode 100644 index 00000000..896f5c67 --- /dev/null +++ b/src/test/java/org/tinyradius/attribute/Ipv6PrefixAttributeTest.java @@ -0,0 +1,18 @@ +package org.tinyradius.attribute; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class Ipv6PrefixAttributeTest { + + @Test + public void test() { + final String testIpv6Prefix = "2001:db8:85a3::/56"; + final Ipv6PrefixAttribute ipv6PrefixAttr = new Ipv6PrefixAttribute(123, testIpv6Prefix); + //System.err.println(ipv6Prefix); + Assertions.assertEquals(Ipv6PrefixAttribute.class, ipv6PrefixAttr.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("Delegated-IPv6-Prefix", ipv6PrefixAttr.getAttributeTypeObject().getName()); + Assertions.assertEquals(testIpv6Prefix, ipv6PrefixAttr.getAttributeValue()); + } + +} diff --git a/src/test/java/org/tinyradius/attribute/StringAttributeTest.java b/src/test/java/org/tinyradius/attribute/StringAttributeTest.java new file mode 100644 index 00000000..9c297820 --- /dev/null +++ b/src/test/java/org/tinyradius/attribute/StringAttributeTest.java @@ -0,0 +1,18 @@ +package org.tinyradius.attribute; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StringAttributeTest { + + @Test + public void test() { + final String username = "jdoe@example.com"; + final StringAttribute strAttr = new StringAttribute(1, username); + //System.err.println(strAttr); + Assertions.assertEquals(StringAttribute.class, strAttr.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("User-Name", strAttr.getAttributeTypeObject().getName()); + Assertions.assertEquals(username, strAttr.getAttributeValue()); + } + +} diff --git a/src/test/java/org/tinyradius/attribute/VendorSpecificAttributeTest.java b/src/test/java/org/tinyradius/attribute/VendorSpecificAttributeTest.java new file mode 100644 index 00000000..810b9bd7 --- /dev/null +++ b/src/test/java/org/tinyradius/attribute/VendorSpecificAttributeTest.java @@ -0,0 +1,20 @@ +package org.tinyradius.attribute; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class VendorSpecificAttributeTest { + + @Test + public void test() { + final String vsaName = "WISPr-Location-Name"; + final String vsaValue = "somewhere"; + VendorSpecificAttribute vsa = new VendorSpecificAttribute(14122); + vsa.addSubAttribute(vsaName, vsaValue); + //System.err.println(vsa); + Assertions.assertEquals(VendorSpecificAttribute.class, vsa.getAttributeTypeObject().getAttributeClass()); + Assertions.assertEquals("Vendor-Specific", vsa.getAttributeTypeObject().getName()); + Assertions.assertEquals(vsaValue, vsa.getSubAttributeValue(vsaName)); + } + +} From b48a42e2307909ca46d4c571f4f47419e7239d36 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Mon, 27 Dec 2021 16:37:13 -0600 Subject: [PATCH 17/21] Set version to 1.2.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 691de592..1921f77a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.tinyradius tinyradius - 1.1.1-SNAPSHOT + 1.2.0-SNAPSHOT jar TinyRadius Java Radius Library From 748e49d3fa7932f3af12234b7fc9ccbe136029de Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 16 Jan 2022 11:02:10 -0600 Subject: [PATCH 18/21] Swapped LinkedList for ArrayList and use getDeclaredConstructor per sonatype-lift. --- .../java/org/tinyradius/attribute/RadiusAttribute.java | 6 ++---- .../org/tinyradius/attribute/VendorSpecificAttribute.java | 3 +-- src/main/java/org/tinyradius/packet/RadiusPacket.java | 7 +++---- src/main/java/org/tinyradius/test/TestServer.java | 4 ++-- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/tinyradius/attribute/RadiusAttribute.java b/src/main/java/org/tinyradius/attribute/RadiusAttribute.java index 995fa4a5..3e4e2a9f 100644 --- a/src/main/java/org/tinyradius/attribute/RadiusAttribute.java +++ b/src/main/java/org/tinyradius/attribute/RadiusAttribute.java @@ -96,8 +96,6 @@ public void setAttributeValue(String value) { * Gets the value of this attribute as a string. * * @return value - * @exception RadiusException - * if the value is invalid */ public String getAttributeValue() { return RadiusUtil.getHexString(getAttributeData()); @@ -168,7 +166,7 @@ public byte[] writeAttribute() { /** * Reads in this attribute from the passed byte array. * - * @param data + * @param data Raw Attribute data */ public void readAttribute(byte[] data, int offset, int length) throws RadiusException { if (length < 2) @@ -234,7 +232,7 @@ public static RadiusAttribute createRadiusAttribute(Dictionary dictionary, int v AttributeType at = dictionary.getAttributeTypeByCode(vendorId, attributeType); if (at != null && at.getAttributeClass() != null) { try { - attribute = at.getAttributeClass().newInstance(); + attribute = at.getAttributeClass().getDeclaredConstructor().newInstance(); } catch (Exception e) { // error instantiating class - should not occur diff --git a/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java b/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java index ca729326..db80e8a5 100644 --- a/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java +++ b/src/main/java/org/tinyradius/attribute/VendorSpecificAttribute.java @@ -10,7 +10,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import org.tinyradius.dictionary.AttributeType; import org.tinyradius.dictionary.Dictionary; @@ -151,7 +150,7 @@ public List getSubAttributes(int attributeType) { if (attributeType < 1 || attributeType > 255) throw new IllegalArgumentException("sub-attribute type out of bounds"); - LinkedList result = new LinkedList<>(); + ArrayList result = new ArrayList<>(); for (RadiusAttribute a : subAttributes) { if (attributeType == a.getAttributeType()) result.add(a); diff --git a/src/main/java/org/tinyradius/packet/RadiusPacket.java b/src/main/java/org/tinyradius/packet/RadiusPacket.java index 82904483..64cd5fbb 100644 --- a/src/main/java/org/tinyradius/packet/RadiusPacket.java +++ b/src/main/java/org/tinyradius/packet/RadiusPacket.java @@ -17,7 +17,6 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import org.tinyradius.attribute.RadiusAttribute; import org.tinyradius.attribute.VendorSpecificAttribute; @@ -382,7 +381,7 @@ public List getAttributes(int attributeType) { if (attributeType < 1 || attributeType > 255) throw new IllegalArgumentException("attribute type out of bounds"); - LinkedList result = new LinkedList<>(); + ArrayList result = new ArrayList<>(); for (RadiusAttribute a : attributes) { if (attributeType == a.getAttributeType()) result.add(a); @@ -405,7 +404,7 @@ public List getAttributes(int vendorId, int attributeType) { if (vendorId == -1) return getAttributes(attributeType); - LinkedList result = new LinkedList<>(); + ArrayList result = new ArrayList<>(); List vsas = getVendorAttributes(vendorId); for (VendorSpecificAttribute vsa : vsas) { List sas = vsa.getSubAttributes(); @@ -526,7 +525,7 @@ public String getAttributeValue(String type) { * @return List with VendorSpecificAttribute objects, never null */ public List getVendorAttributes(int vendorId) { - LinkedList result = new LinkedList<>(); + ArrayList result = new ArrayList<>(); for (RadiusAttribute a : attributes) { if (a instanceof VendorSpecificAttribute) { VendorSpecificAttribute vsa = (VendorSpecificAttribute) a; diff --git a/src/main/java/org/tinyradius/test/TestServer.java b/src/main/java/org/tinyradius/test/TestServer.java index bf66fbcc..72f1b872 100644 --- a/src/main/java/org/tinyradius/test/TestServer.java +++ b/src/main/java/org/tinyradius/test/TestServer.java @@ -21,8 +21,8 @@ public class TestServer { /** - * @throws Exception - * Exception + * Test server which terminates after 30 s. + * @throws Exception Exception */ public static void main(String[] args) throws Exception { RadiusServer server = new RadiusServer() { From 6b6b0113c16111fe949dbb864217527ad42fa8d5 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 16 Jan 2022 11:06:13 -0600 Subject: [PATCH 19/21] Remove unused param attributeType from DictionaryParser per sonatype lift --- .../org/tinyradius/dictionary/DictionaryParser.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/tinyradius/dictionary/DictionaryParser.java b/src/main/java/org/tinyradius/dictionary/DictionaryParser.java index d0446dc0..efc8f4df 100644 --- a/src/main/java/org/tinyradius/dictionary/DictionaryParser.java +++ b/src/main/java/org/tinyradius/dictionary/DictionaryParser.java @@ -35,7 +35,7 @@ public class DictionaryParser { * @param source * input stream * @return dictionary object - * @throws IOException + * @throws IOException IOException */ public static Dictionary parseDictionary(InputStream source) throws IOException { WritableDictionary d = new MemoryDictionary(); @@ -106,7 +106,7 @@ private static void parseAttributeLine(WritableDictionary dictionary, StringToke if (code == VendorSpecificAttribute.VENDOR_SPECIFIC) type = VendorSpecificAttribute.class; else - type = getAttributeTypeClass(code, typeStr); + type = getAttributeTypeClass(typeStr); // create and cache object dictionary.addAttributeType(new AttributeType(code, name, type)); @@ -143,7 +143,7 @@ private static void parseVendorAttributeLine(WritableDictionary dictionary, Stri int code = Integer.parseInt(tok.nextToken().trim()); String typeStr = tok.nextToken().trim(); - Class type = getAttributeTypeClass(code, typeStr); + Class type = getAttributeTypeClass(typeStr); AttributeType at = new AttributeType(Integer.parseInt(vendor), code, name, type); dictionary.addAttributeType(at); } @@ -186,13 +186,11 @@ private static void includeDictionaryFile(WritableDictionary dictionary, StringT * Returns the RadiusAttribute descendant class for the given * attribute type. * - * @param attributeType - * * @param typeStr * string|octets|integer|date|ipaddr|ipv6addr|ipv6prefix * @return RadiusAttribute class or descendant */ - private static Class getAttributeTypeClass(int attributeType, String typeStr) { + private static Class getAttributeTypeClass(String typeStr) { // Default type is RadiusAttribute Class type = RadiusAttribute.class; if (typeStr.equalsIgnoreCase("string")) From 45a8f859bc15faadfc8c6c01c36b3d9de603218d Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 16 Jan 2022 11:37:58 -0600 Subject: [PATCH 20/21] Synchronize methods reading serverSocket in RadiusClient (sonatype lift warning for race conditions) --- src/main/java/org/tinyradius/util/RadiusClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/tinyradius/util/RadiusClient.java b/src/main/java/org/tinyradius/util/RadiusClient.java index 219e726e..abc44ebe 100644 --- a/src/main/java/org/tinyradius/util/RadiusClient.java +++ b/src/main/java/org/tinyradius/util/RadiusClient.java @@ -147,7 +147,7 @@ public synchronized RadiusPacket account(AccountingRequest request) throws IOExc /** * Closes the socket of this client. */ - public void close() { + public synchronized void close() { if (serverSocket != null) serverSocket.close(); } @@ -257,8 +257,10 @@ public void setSocketTimeout(int socketTimeout) throws SocketException { if (socketTimeout < 1) throw new IllegalArgumentException("socket timeout must be positive"); this.socketTimeout = socketTimeout; - if (serverSocket != null) - serverSocket.setSoTimeout(socketTimeout); + synchronized (this) { + if (serverSocket != null) + serverSocket.setSoTimeout(socketTimeout); + } } /** @@ -368,7 +370,7 @@ public static RadiusPacket communicate(RadiusEndpoint remoteServer, RadiusPacket * @throws SocketException * Socket Exception */ - protected DatagramSocket getSocket() throws SocketException { + protected synchronized DatagramSocket getSocket() throws SocketException { if (serverSocket == null || serverSocket.isClosed()) { serverSocket = new DatagramSocket(); serverSocket.setSoTimeout(getSocketTimeout()); From d24c740cadc6b0bde65518b43f836d7e09b9bec9 Mon Sep 17 00:00:00 2001 From: Rick Frey Date: Sun, 16 Jan 2022 12:26:00 -0600 Subject: [PATCH 21/21] Synchronize methods reading serverSocket in RadiusClient (sonatype lift warning for race conditions) --- src/main/java/org/tinyradius/util/RadiusClient.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/tinyradius/util/RadiusClient.java b/src/main/java/org/tinyradius/util/RadiusClient.java index abc44ebe..efb782e5 100644 --- a/src/main/java/org/tinyradius/util/RadiusClient.java +++ b/src/main/java/org/tinyradius/util/RadiusClient.java @@ -253,14 +253,12 @@ public int getSocketTimeout() { * @throws SocketException * Socket Exception */ - public void setSocketTimeout(int socketTimeout) throws SocketException { + public synchronized void setSocketTimeout(int socketTimeout) throws SocketException { if (socketTimeout < 1) throw new IllegalArgumentException("socket timeout must be positive"); this.socketTimeout = socketTimeout; - synchronized (this) { - if (serverSocket != null) - serverSocket.setSoTimeout(socketTimeout); - } + if (serverSocket != null) + serverSocket.setSoTimeout(socketTimeout); } /**