public DNSLookupContinuation onDNSResponse()

in resolver/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java [124:198]


	public DNSLookupContinuation onDNSResponse(DNSResponse response, SPFSession spfSession)
            throws PermErrorException, TempErrorException, NoneException, NeutralException {
        
        List<String> domainList = (List<String>) spfSession.getAttribute(ATTRIBUTE_DOMAIN_LIST);
        try {
            if (domainList == null) {
            
                domainList = response.getResponse();
                
                // No PTR records found
                if (domainList == null) {
                    spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
                    return null;
                }
        
                // check if the maximum lookup count is reached
                if (dnsService.getRecordLimit() > 0 && domainList.size() > dnsService.getRecordLimit()) {
                    // Truncate the PTR list to getRecordLimit.
                    // See #ptr-limit rfc4408 test
                    domainList = domainList.subList(0, dnsService.getRecordLimit()-1);
                    // throw new PermErrorException("Maximum PTR lookup count reached");
                }
                
                spfSession.setAttribute(ATTRIBUTE_DOMAIN_LIST, domainList);
                
            } else {

                String compareDomain = (String) spfSession.getAttribute(ATTRIBUTE_CURRENT_DOMAIN);
                String host = (String) spfSession.getAttribute(ATTRIBUTE_EXPANDED_HOST);
    
                List<String> aList = response.getResponse();
    

                if (aList != null) {
                    for (int j = 0; j < aList.size(); j++) {
                        // Added the IPAddr parsing/toString to have matching in IPV6 multiple ways to 
                        if (IPAddr.getAddress((String) aList.get(j)).getIPAddress().equals(IPAddr.getAddress(spfSession.getIpAddress()).getIPAddress())) {
                            
                            if (compareDomain.equals(host)
                                    || compareDomain.endsWith("." + host)) {
                                spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.TRUE);
                                return null;
                            }
                        }
                    }
                }
            
            }
        } catch (TimeoutException e) {
            throw new TempErrorException("Timeout querying the dns server");
        }
        

        if (domainList.size() > 0) {
            String currentDomain = (String) domainList.remove(0);
    
            DNSRequest dnsRequest;
            // check if the connecting ip is ip6. If so lookup AAAA record
            if (IPAddr.isIPV6(spfSession.getIpAddress())) {
                // Get aaaa record for this
                dnsRequest = new DNSRequest(currentDomain, DNSRequest.AAAA);
            } else {
                // Get a record for this
                dnsRequest = new DNSRequest(currentDomain, DNSRequest.A);
            }
            
            spfSession.setAttribute(ATTRIBUTE_CURRENT_DOMAIN, currentDomain);
            
            return new DNSLookupContinuation(dnsRequest, PTRMechanism.this);
        } else {
            spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
            return null;
        }

    }