This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-jspf.git
commit fbdb1ead415da18538c0e3685b4774125fbc9283 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 17:38:08 2026 +0100 [PERF] a/mx/ip4/ip6 mechanisms: compare masked addresses as numbers AMechanism.checkAddressList and IP4Mechanism.checkSPF decided a match by formatting both masked addresses into Strings (StringBuffer, concatenation, Long.toHexString and toUpperCase per IPv6 group) and comparing them, the client side rebuilt for every candidate. About 28% of the hostile mx check, and most of IP4Mechanism.checkSPF, which also parsed the client address again for every ip4 and ip6 term. IPAddr.matchesMasked compares the masked groups as numbers, giving the same result as the Strings: false across IPv4 and IPv6, the IPv4 octets read as unsigned bytes. The ip4 and ip6 mechanisms compare the network with the client address parsed once per check by SPFSession, under the mask of the network. --- .../java/org/apache/james/jspf/core/IPAddr.java | 29 +++++++++++++++++++++ .../org/apache/james/jspf/core/SPFSession.java | 15 +++++++++++ .../org/apache/james/jspf/terms/AMechanism.java | 3 +-- .../org/apache/james/jspf/terms/IP4Mechanism.java | 8 +++--- .../org/apache/james/jspf/core/IPAddrTest.java | 30 ++++++++++++++++++++++ 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/core/IPAddr.java b/resolver/src/main/java/org/apache/james/jspf/core/IPAddr.java index f2ab6d6..f48f18a 100644 --- a/resolver/src/main/java/org/apache/james/jspf/core/IPAddr.java +++ b/resolver/src/main/java/org/apache/james/jspf/core/IPAddr.java @@ -299,6 +299,35 @@ public class IPAddr { return getIPAddress(maskedAddress(address, mask)); } + /** + * Compares the addresses as numbers, under the mask of this address: the same result as + * comparing the getMaskedIPAddress Strings when both have the mask of this one, without + * formatting them. + * + * @param other + * the address to compare + * @return true when other is of the same family (IPv4 or IPv6) and its masked address is the + * masked address of this one + */ + public boolean matchesMasked(IPAddr other) { + if (ipLength != other.ipLength) { + return false; + } + for (int i = 0; i < ipRun; i++) { + int thisPart = address[i] & mask[i]; + int otherPart = other.address[i] & mask[i]; + if (ipLength == 4) { + // the octets of an IPv4 address are kept as signed bytes, as get8BitAddress reads them + thisPart &= MASK8; + otherPart &= MASK8; + } + if (thisPart != otherPart) { + return false; + } + } + return true; + } + /** * Return the NibbleFormat of the IPAddr * diff --git a/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java b/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java index a88147b..43a02b7 100644 --- a/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java +++ b/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java @@ -89,6 +89,8 @@ public class SPFSession implements MacroData { private String ipAddress = ""; // also used for (i)<sending-host> + private IPAddr parsedIpAddress; + private String mailFrom = ""; // (s)<responsible-sender> private String hostName = ""; // (h)<sender-domain> @@ -273,6 +275,19 @@ public class SPFSession implements MacroData { public String getIpAddress() { return ipAddress; } + + /** + * The client address, parsed once for the check: callers must not change its mask. + * + * @return the ipAddress, parsed + * @throws PermErrorException when it is not a valid address + */ + public IPAddr getParsedIpAddress() throws PermErrorException { + if (parsedIpAddress == null) { + parsedIpAddress = IPAddr.getAddress(ipAddress); + } + return parsedIpAddress; + } /** * @see org.apache.james.jspf.core.MacroData#getMacroIpAddress() diff --git a/resolver/src/main/java/org/apache/james/jspf/terms/AMechanism.java b/resolver/src/main/java/org/apache/james/jspf/terms/AMechanism.java index a32bbe8..c68f7d4 100644 --- a/resolver/src/main/java/org/apache/james/jspf/terms/AMechanism.java +++ b/resolver/src/main/java/org/apache/james/jspf/terms/AMechanism.java @@ -160,8 +160,7 @@ public class AMechanism extends GenericMechanism implements SPFCheckerDNSRespons // set the mask in the address. // TODO should we use cidr from the parameters or the input checkAddress cidr? IPAddr ipAddr = IPAddr.getAddress(ip, checkAddress.getMaskLength()); - if (checkAddress.getMaskedIPAddress().equals( - ipAddr.getMaskedIPAddress())) { + if (checkAddress.matchesMasked(ipAddr)) { return true; } } diff --git a/resolver/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java b/resolver/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java index e777cdb..b052777 100644 --- a/resolver/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java +++ b/resolver/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java @@ -45,12 +45,10 @@ public class IP4Mechanism extends GenericMechanism { * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) */ public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException { - IPAddr originalIP; + // the client address under the mask of the network, compared as numbers + boolean matches = getIp().matchesMasked(spfData.getParsedIpAddress()); - originalIP = IPAddr.getAddress(spfData.getIpAddress(), getIp() - .getMaskLength()); - - spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.valueOf(getIp().getMaskedIPAddress().equals(originalIP.getMaskedIPAddress()))); + spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.valueOf(matches)); return null; } diff --git a/resolver/src/test/java/org/apache/james/jspf/core/IPAddrTest.java b/resolver/src/test/java/org/apache/james/jspf/core/IPAddrTest.java index 2cc9b3f..c8b83a8 100644 --- a/resolver/src/test/java/org/apache/james/jspf/core/IPAddrTest.java +++ b/resolver/src/test/java/org/apache/james/jspf/core/IPAddrTest.java @@ -190,4 +190,34 @@ public class IPAddrTest extends TestCase { } } + private static String randomAddress(java.util.Random random) { + switch (random.nextInt(3)) { + case 0: + // few values, so that the addresses often share a prefix + return random.nextInt(3) + "." + random.nextInt(3) + "." + (random.nextInt(2) * 255) + "." + random.nextInt(256); + case 1: + return "2001:db8:" + Integer.toHexString(random.nextInt(3)) + "::" + Integer.toHexString(random.nextInt(65536)); + default: + return "::ffff:" + random.nextInt(2) + ".2.3." + random.nextInt(256); + } + } + + public void testMatchesMaskedIsTheComparisonOfTheMaskedAddresses() throws PermErrorException { + java.util.Random random = new java.util.Random(42); + int matches = 0; + for (int n = 0; n < 50000; n++) { + int maskLength = random.nextInt(130); + IPAddr first = random.nextBoolean() ? IPAddr.getAddress(randomAddress(random), maskLength) + : IPAddr.getIP6Address(randomAddress(random), maskLength); + IPAddr second = IPAddr.getAddress(randomAddress(random), maskLength); + + boolean expected = first.getMaskedIPAddress().equals(second.getMaskedIPAddress()); + assertEquals(first + " " + second + "/" + maskLength, expected, first.matchesMasked(second)); + if (expected) { + matches++; + } + } + assertTrue(matches > 1000); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
