<commit_before>/*
 *  Copyright (C) 2008 Pingtel Corp., certain elements licensed under a Contributor Agreement.
 *  Contributors retain copyright to elements licensed under a Contributor Agreement.
 *  Licensed to the User under the LGPL license.
 *
 */
package org.sipfoundry.sipxbridge;

import gov.nist.javax.sip.TransactionExt;
import gov.nist.javax.sip.clientauthutils.UserCredentials;
import gov.nist.javax.sip.header.ims.PPreferredIdentityHeader;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.sip.ClientTransaction;
import javax.sip.SipProvider;
import javax.sip.address.SipURI;
import javax.sip.header.FromHeader;
import javax.sip.message.Request;

import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.xbill.DNS.TextParseException;

/**
 * Keeps a mapping of account ID to ItspAccountInfo and a mapping of account ID to sip pbx account
 * info.
 * 
 * @author <NAME>
 * 
 */
public class AccountManagerImpl implements gov.nist.javax.sip.clientauthutils.AccountManager {

    private static Logger logger = Logger.getLogger(AccountManagerImpl.class);

    private HashSet<ItspAccountInfo> itspAccounts = new HashSet<ItspAccountInfo>();

 
    /*
     * The reverse name lookup map
     */
    private ConcurrentMap<String, String> addressToDomainNameMap = new ConcurrentHashMap<String, String>();

    private BridgeConfiguration bridgeConfiguration;

    private Hashtable<String, Boolean> alarmTable = new Hashtable<String, Boolean>();

    public AccountManagerImpl() {

    }

    // //////////////////////////////////////////////////////////////////
    // Package local methods.
    // //////////////////////////////////////////////////////////////////
    /**
     * @return the bridgeConfiguration
     */
    BridgeConfiguration getBridgeConfiguration() {
        return bridgeConfiguration;
    }


    /**
     * Start the failure timout timers for each of the accounts we manage.
     */
    void startAuthenticationFailureTimers() {

        for (ItspAccountInfo itspAccountInfo : this.getItspAccounts()) {
            itspAccountInfo.startFailureCounterScanner();
        }
    }

   

    /**
     * Get the default outbound ITSP account for outbound calls.
     */
    ItspAccountInfo getDefaultAccount() {
        return itspAccounts.iterator().next();
    }

    /**
     * Get the outbound ITSP account for a specific outbund SipURI.
     */
    ItspAccountInfo getAccount(Request request) {

        SipURI sipUri = (SipURI) request.getRequestURI();
        logger.debug("getItspAccount: fetching account for " + sipUri);
        ItspAccountInfo accountFound = null;
        try {

            for (ItspAccountInfo accountInfo : itspAccounts) {
                if (accountInfo.getProxyDomain() != null
                        && sipUri.getHost().endsWith(accountInfo.getProxyDomain())) {
                    if (accountInfo.getCallerId() == null) {
                        accountFound = accountInfo;
                        return accountInfo;
                    } else {
                        String callerId = accountInfo.getCallerId();
                        FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);

                        String userStr = ((SipURI) fromHeader.getAddress().getURI()).getUser();
                        String domainStr = ((SipURI) fromHeader.getAddress().getURI()).getHost();
                        if (userStr.equals("anonymous") && domainStr.equals("invalid")) {
                            PPreferredIdentityHeader pai = (PPreferredIdentityHeader) request
                                    .getHeader(PPreferredIdentityHeader.NAME);
                            if (pai == null) {
                                logger.warn("Anonymous call without P-Preferred-Identity ");
                                // BUGBUG - this is really a mistake we should reject
                                // the call if the PAI header is missing
                                accountFound = accountInfo;
                                return accountInfo;
                            }
                            userStr = ((SipURI) pai.getAddress().getURI()).getUser();
                        }
                        if (callerId.startsWith(userStr)) {
                            accountFound = accountInfo;
                            return accountInfo;
                        }

                    }
                }
            }
            // Fallback -- cannot find calling line id.
            for (ItspAccountInfo accountInfo : itspAccounts) {
                logger.warn("Could not match user part of inbound request URI");
                if (accountInfo.getProxyDomain() != null) {
                    if (sipUri.getHost().endsWith(accountInfo.getProxyDomain())) {
                        accountFound = accountInfo;
                        return accountInfo;
                    }
                }
            }
           /*
             * logger.error( Gateway.ACCOUNT_NOT_FOUND_ALARM_ID + " uri = " + sipUri ); try {
             * 
             * if (alarmTable.get(sipUri.getHost()) == null) { alarmTable.put(sipUri.getHost(),
             * true); Gateway.getAlarmClient().raiseAlarm(Gateway.ACCOUNT_NOT_FOUND_ALARM_ID,
             * sipUri.getHost()); } } catch (XmlRpcException e) { logger.error("Could not send
             * alarm " + Gateway.ACCOUNT_NOT_FOUND_ALARM_ID + " uri = " + sipUri); }
             */
            
            /*
             * If an account is not found return an account record with the
             * domain set to the outbound request domain. The INVITE will be
             * forwarded. If the other side does not like the INVITE it an
             * complain about it. See issue XX-5623 
             */
            accountFound = new ItspAccountInfo();
            accountFound.setProxyDomain(sipUri.getHost());
            accountFound.setGlobalAddressingUsed(true);
            return null;
        } finally {
            logger.debug("getItspAccount: returning " + accountFound);
        }
    }
<commit_msg>XX-5623 : Calls to destination like "<EMAIL>" were itspDomain.org is a working ITSP account in sipxbridge will fail. Add ITSP port and transport.<commit_after>/*
 *  Copyright (C) 2008 Pingtel Corp., certain elements licensed under a Contributor Agreement.
 *  Contributors retain copyright to elements licensed under a Contributor Agreement.
 *  Licensed to the User under the LGPL license.
 *
 */
package org.sipfoundry.sipxbridge;

import gov.nist.javax.sip.TransactionExt;
import gov.nist.javax.sip.clientauthutils.UserCredentials;
import gov.nist.javax.sip.header.ims.PPreferredIdentityHeader;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.sip.ClientTransaction;
import javax.sip.SipProvider;
import javax.sip.address.SipURI;
import javax.sip.header.FromHeader;
import javax.sip.message.Request;

import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.xbill.DNS.TextParseException;

/**
 * Keeps a mapping of account ID to ItspAccountInfo and a mapping of account ID to sip pbx account
 * info.
 * 
 * @author <NAME>
 * 
 */
public class AccountManagerImpl implements gov.nist.javax.sip.clientauthutils.AccountManager {

    private static Logger logger = Logger.getLogger(AccountManagerImpl.class);

    private HashSet<ItspAccountInfo> itspAccounts = new HashSet<ItspAccountInfo>();

 
    /*
     * The reverse name lookup map
     */
    private ConcurrentMap<String, String> addressToDomainNameMap = new ConcurrentHashMap<String, String>();

    private BridgeConfiguration bridgeConfiguration;

    private Hashtable<String, Boolean> alarmTable = new Hashtable<String, Boolean>();

    public AccountManagerImpl() {

    }

    // //////////////////////////////////////////////////////////////////
    // Package local methods.
    // //////////////////////////////////////////////////////////////////
    /**
     * @return the bridgeConfiguration
     */
    BridgeConfiguration getBridgeConfiguration() {
        return bridgeConfiguration;
    }


    /**
     * Start the failure timout timers for each of the accounts we manage.
     */
    void startAuthenticationFailureTimers() {

        for (ItspAccountInfo itspAccountInfo : this.getItspAccounts()) {
            itspAccountInfo.startFailureCounterScanner();
        }
    }

   

    /**
     * Get the default outbound ITSP account for outbound calls.
     */
    ItspAccountInfo getDefaultAccount() {
        return itspAccounts.iterator().next();
    }

    /**
     * Get the outbound ITSP account for a specific outbund SipURI.
     */
    ItspAccountInfo getAccount(Request request) {

        SipURI sipUri = (SipURI) request.getRequestURI();
        logger.debug("getItspAccount: fetching account for " + sipUri);
        ItspAccountInfo accountFound = null;
        try {

            for (ItspAccountInfo accountInfo : itspAccounts) {
                if (accountInfo.getProxyDomain() != null
                        && sipUri.getHost().endsWith(accountInfo.getProxyDomain())) {
                    if (accountInfo.getCallerId() == null) {
                        accountFound = accountInfo;
                        return accountInfo;
                    } else {
                        String callerId = accountInfo.getCallerId();
                        FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);

                        String userStr = ((SipURI) fromHeader.getAddress().getURI()).getUser();
                        String domainStr = ((SipURI) fromHeader.getAddress().getURI()).getHost();
                        if (userStr.equals("anonymous") && domainStr.equals("invalid")) {
                            PPreferredIdentityHeader pai = (PPreferredIdentityHeader) request
                                    .getHeader(PPreferredIdentityHeader.NAME);
                            if (pai == null) {
                                logger.warn("Anonymous call without P-Preferred-Identity ");
                                // BUGBUG - this is really a mistake we should reject
                                // the call if the PAI header is missing
                                accountFound = accountInfo;
                                return accountInfo;
                            }
                            userStr = ((SipURI) pai.getAddress().getURI()).getUser();
                        }
                        if (callerId.startsWith(userStr)) {
                            accountFound = accountInfo;
                            return accountInfo;
                        }

                    }
                }
            }
            // Fallback -- cannot find calling line id.
            for (ItspAccountInfo accountInfo : itspAccounts) {
                logger.warn("Could not match user part of inbound request URI");
                if (accountInfo.getProxyDomain() != null) {
                    if (sipUri.getHost().endsWith(accountInfo.getProxyDomain())) {
                        accountFound = accountInfo;
                        return accountInfo;
                    }
                }
            }
           /*
             * logger.error( Gateway.ACCOUNT_NOT_FOUND_ALARM_ID + " uri = " + sipUri ); try {
             * 
             * if (alarmTable.get(sipUri.getHost()) == null) { alarmTable.put(sipUri.getHost(),
             * true); Gateway.getAlarmClient().raiseAlarm(Gateway.ACCOUNT_NOT_FOUND_ALARM_ID,
             * sipUri.getHost()); } } catch (XmlRpcException e) { logger.error("Could not send
             * alarm " + Gateway.ACCOUNT_NOT_FOUND_ALARM_ID + " uri = " + sipUri); }
             */
            
            /*
             * If an account is not found return an account record with the
             * domain set to the outbound request domain. The INVITE will be
             * forwarded. If the other side does not like the INVITE it an
             * complain about it. See issue XX-5623 
             */
            accountFound = new ItspAccountInfo();
            accountFound.setProxyDomain(sipUri.getHost());
            accountFound.setUserName(sipUri.getUser());
            accountFound.setOutboundProxyPort(sipUri.getPort());
            accountFound.setOutboundTransport(sipUri.getTransportParam());
            accountFound.setGlobalAddressingUsed(true);
            return null;
        } finally {
            logger.debug("getItspAccount: returning " + accountFound);
        }
    }

{'fraction_non_alphanumeric': 0.10763996288277142, 'fraction_numerical': 0.0017012063099288586, 'mean_word_length': 1.8213350785340314, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3632546', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3210, 'n_words': 776}