Search in sources :

Example 1 with ClientTransaction

use of javax.sip.ClientTransaction in project Openfire by igniterealtime.

the class SimpleSession method sendRequest.

/**
	 * Sends a request with the specified request and transport.
	 * @param request   The request packet.
	 * @param transport The transport protocol used.
	 * @param dialog    The dialog for a persistent transaction.
	 *                  Leave it <code>null</code> if no dialog is associated with this request.
     * @throws javax.sip.SipException Unable to communicate.
	 */
@SuppressWarnings("unchecked")
private void sendRequest(Request request, String transport, Dialog dialog) throws SipException {
    for (Iterator sipProviders = sipStack.getSipProviders(); sipProviders.hasNext(); ) {
        SipProvider provider = (SipProvider) sipProviders.next();
        if (provider.getListeningPoint(transport) != null) {
            Log.debug("Sending packet:  \n" + request.toString() + "\n========\n");
            ClientTransaction transaction = provider.getNewClientTransaction(request);
            if (dialog != null)
                dialog.sendRequest(transaction);
            else
                transaction.sendRequest();
            return;
        }
    }
    Log.debug("SimpleSession(" + this.jid.getNode() + "):  No SipProvider found for that transport!");
}
Also used : ClientTransaction(javax.sip.ClientTransaction) Iterator(java.util.Iterator) SipProvider(javax.sip.SipProvider)

Example 2 with ClientTransaction

use of javax.sip.ClientTransaction in project Openfire by igniterealtime.

the class SipSecurityManager method handleChallenge.

/**
     * Uses securityAuthority to determinie a set of valid user credentials for
     * the specified Response (Challenge) and appends it to the challenged
     * request so that it could be retransmitted.
     * <p/>
     * Fredrik Wickstrom reported that dialog cseq counters are not incremented
     * when resending requests. He later uncovered additional problems and
     * proposed a way to fix them (his proposition was taken into account).
     *
     * @param challenge             the 401/407 challenge response
     * @param challengedTransaction the transaction established by the challenged request
     * @return a transaction containing a reoriginated request with the
     *         necessary authorization header.
     * @throws SipSecurityException
     */
public ClientTransaction handleChallenge(Response challenge, ClientTransaction challengedTransaction) throws SipSecurityException, SipException, InvalidArgumentException, ParseException {
    try {
        String branchID = challengedTransaction.getBranchId();
        Request challengedRequest = challengedTransaction.getRequest();
        Request reoriginatedRequest = (Request) challengedRequest.clone();
        ListIterator authHeaders = null;
        if (challenge == null || reoriginatedRequest == null)
            throw new NullPointerException("A null argument was passed to handle challenge.");
        if (challenge.getStatusCode() == Response.UNAUTHORIZED)
            authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
        else if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED)
            authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
        if (authHeaders == null)
            throw new SecurityException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
        // Remove all authorization headers from the request (we'll re-add
        // them
        // from cache)
        reoriginatedRequest.removeHeader(AuthorizationHeader.NAME);
        reoriginatedRequest.removeHeader(ProxyAuthorizationHeader.NAME);
        // rfc 3261 says that the cseq header should be augmented for the
        // new
        // request. do it here so that the new dialog (created together with
        // the new client transaction) takes it into account.
        // Bug report - Fredrik Wickstrom
        CSeqHeader cSeq = (CSeqHeader) reoriginatedRequest.getHeader((CSeqHeader.NAME));
        cSeq.setSequenceNumber(cSeq.getSequenceNumber() + 1);
        ClientTransaction retryTran = transactionCreator.getNewClientTransaction(reoriginatedRequest);
        WWWAuthenticateHeader authHeader = null;
        CredentialsCacheEntry ccEntry = null;
        while (authHeaders.hasNext()) {
            authHeader = (WWWAuthenticateHeader) authHeaders.next();
            String realm = authHeader.getRealm();
            // Check whether we have cached credentials for authHeader's
            // realm
            // make sure that if such credentials exist they get removed.
            // The
            // challenge means that there's something wrong with them.
            ccEntry = cachedCredentials.remove(realm);
            // Try to guess user name and facilitate user
            UserCredentials defaultCredentials = new UserCredentials();
            FromHeader from = (FromHeader) reoriginatedRequest.getHeader(FromHeader.NAME);
            URI uri = from.getAddress().getURI();
            if (uri.isSipURI()) {
                Log.debug("handleChallenge", SIPConfig.getAuthUserName());
                String user = SIPConfig.getAuthUserName() != null ? SIPConfig.getAuthUserName() : ((SipURI) uri).getUser();
                defaultCredentials.setAuthUserName(user == null ? SIPConfig.getUserName() : user);
            }
            boolean ccEntryHasSeenTran = false;
            if (ccEntry != null)
                ccEntryHasSeenTran = ccEntry.processResponse(branchID);
            // get a new pass
            if (// we don't have credentials for the
            ccEntry == null || // specified realm
            ((!authHeader.isStale() && ccEntryHasSeenTran))) {
                if (ccEntry == null) {
                    ccEntry = new CredentialsCacheEntry();
                    ccEntry.userCredentials = defaultCredentials;
                }
                // put the returned user name in the properties file
                // so that it appears as a default one next time user is
                // prompted for pass
                SIPConfig.setUserName(ccEntry.userCredentials.getUserName());
            } else // encode and send what we have
            if (ccEntry != null && (!ccEntryHasSeenTran || authHeader.isStale())) {
            }
            // if user canceled or sth else went wrong
            if (ccEntry.userCredentials == null)
                throw new SecurityException("Unable to authenticate with realm " + realm);
            AuthorizationHeader authorization = this.getAuthorization(reoriginatedRequest.getMethod(), reoriginatedRequest.getRequestURI().toString(), reoriginatedRequest.getContent() == null ? "" : reoriginatedRequest.getContent().toString(), authHeader, ccEntry.userCredentials);
            ccEntry.processRequest(retryTran.getBranchId());
            cachedCredentials.cacheEntry(realm, ccEntry);
            reoriginatedRequest.addHeader(authorization);
            // if there was trouble with the user - make sure we fix it
            if (uri.isSipURI()) {
                ((SipURI) uri).setUser(ccEntry.userCredentials.getUserName());
                Address add = from.getAddress();
                add.setURI(uri);
                from.setAddress(add);
                reoriginatedRequest.setHeader(from);
                if (challengedRequest.getMethod().equals(Request.REGISTER)) {
                    ToHeader to = (ToHeader) reoriginatedRequest.getHeader(ToHeader.NAME);
                    add.setURI(uri);
                    to.setAddress(add);
                    reoriginatedRequest.setHeader(to);
                }
                // very ugly but very necessary
                sipManCallback.setCurrentlyUsedURI(uri.toString());
                Log.debug("URI: " + uri.toString());
            }
        // if this is a register - fix to as well
        }
        return retryTran;
    } catch (Exception e) {
        Log.debug("ERRO REG: " + e.toString());
        return null;
    }
}
Also used : Address(javax.sip.address.Address) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ListIterator(java.util.ListIterator) SipURI(javax.sip.address.SipURI) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) SipException(javax.sip.SipException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException)

Example 3 with ClientTransaction

use of javax.sip.ClientTransaction in project XobotOS by xamarin.

the class SipHelper method handleChallenge.

public ClientTransaction handleChallenge(ResponseEvent responseEvent, AccountManager accountManager) throws SipException {
    AuthenticationHelper authenticationHelper = ((SipStackExt) mSipStack).getAuthenticationHelper(accountManager, mHeaderFactory);
    ClientTransaction tid = responseEvent.getClientTransaction();
    ClientTransaction ct = authenticationHelper.handleChallenge(responseEvent.getResponse(), tid, mSipProvider, 5);
    if (DEBUG)
        Log.d(TAG, "send request with challenge response: " + ct.getRequest());
    ct.sendRequest();
    return ct;
}
Also used : AuthenticationHelper(gov.nist.javax.sip.clientauthutils.AuthenticationHelper) SipStackExt(gov.nist.javax.sip.SipStackExt) ClientTransaction(javax.sip.ClientTransaction)

Example 4 with ClientTransaction

use of javax.sip.ClientTransaction in project XobotOS by xamarin.

the class SipHelper method sendOptions.

public ClientTransaction sendOptions(SipProfile caller, SipProfile callee, String tag) throws SipException {
    try {
        Request request = (caller == callee) ? createRequest(Request.OPTIONS, caller, tag) : createRequest(Request.OPTIONS, caller, callee, tag);
        ClientTransaction clientTransaction = mSipProvider.getNewClientTransaction(request);
        clientTransaction.sendRequest();
        return clientTransaction;
    } catch (Exception e) {
        throw new SipException("sendOptions()", e);
    }
}
Also used : ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) SipException(javax.sip.SipException) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) PeerUnavailableException(javax.sip.PeerUnavailableException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException)

Example 5 with ClientTransaction

use of javax.sip.ClientTransaction in project XobotOS by xamarin.

the class SipHelper method sendRegister.

public ClientTransaction sendRegister(SipProfile userProfile, String tag, int expiry) throws SipException {
    try {
        Request request = createRequest(Request.REGISTER, userProfile, tag);
        if (expiry == 0) {
            // remove all previous registrations by wildcard
            // rfc3261#section-10.2.2
            request.addHeader(createWildcardContactHeader());
        } else {
            request.addHeader(createContactHeader(userProfile));
        }
        request.addHeader(mHeaderFactory.createExpiresHeader(expiry));
        ClientTransaction clientTransaction = mSipProvider.getNewClientTransaction(request);
        clientTransaction.sendRequest();
        return clientTransaction;
    } catch (ParseException e) {
        throw new SipException("sendRegister()", e);
    }
}
Also used : ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ParseException(java.text.ParseException) SipException(javax.sip.SipException)

Aggregations

ClientTransaction (javax.sip.ClientTransaction)15 Request (javax.sip.message.Request)9 SipException (javax.sip.SipException)8 ParseException (java.text.ParseException)7 InvalidArgumentException (javax.sip.InvalidArgumentException)5 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)4 SipURI (javax.sip.address.SipURI)4 SIPRequest (gov.nist.javax.sip.message.SIPRequest)3 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)3 Hop (javax.sip.address.Hop)3 URI (javax.sip.address.URI)3 SipUri (gov.nist.javax.sip.address.SipUri)2 Route (gov.nist.javax.sip.header.Route)2 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)2 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)2 IOException (java.io.IOException)2 ListIterator (java.util.ListIterator)2 ListeningPoint (javax.sip.ListeningPoint)2 ServerTransaction (javax.sip.ServerTransaction)2 SipProvider (javax.sip.SipProvider)2