use of javax.sip.header.CSeqHeader in project XobotOS by xamarin.
the class AuthenticationHelperImpl method handleChallenge.
/*
* (non-Javadoc)
*
* @see gov.nist.javax.sip.clientauthutils.AuthenticationHelper#handleChallenge(javax.sip.message.Response,
* javax.sip.ClientTransaction, javax.sip.SipProvider)
*/
public ClientTransaction handleChallenge(Response challenge, ClientTransaction challengedTransaction, SipProvider transactionCreator, int cacheTime) throws SipException, NullPointerException {
try {
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logDebug("handleChallenge: " + challenge);
}
SIPRequest challengedRequest = ((SIPRequest) challengedTransaction.getRequest());
Request reoriginatedRequest = null;
/*
* If the challenged request is part of a Dialog and the
* Dialog is confirmed the re-originated request should be
* generated as an in-Dialog request.
*/
if (challengedRequest.getToTag() != null || challengedTransaction.getDialog() == null || challengedTransaction.getDialog().getState() != DialogState.CONFIRMED) {
reoriginatedRequest = (Request) challengedRequest.clone();
} else {
/*
* Re-originate the request by consulting the dialog. In particular
* the route set could change between the original request and the
* in-dialog challenge.
*/
reoriginatedRequest = challengedTransaction.getDialog().createRequest(challengedRequest.getMethod());
Iterator<String> headerNames = challengedRequest.getHeaderNames();
while (headerNames.hasNext()) {
String headerName = headerNames.next();
if (reoriginatedRequest.getHeader(headerName) != null) {
ListIterator<Header> iterator = reoriginatedRequest.getHeaders(headerName);
while (iterator.hasNext()) {
reoriginatedRequest.addHeader(iterator.next());
}
}
}
}
// remove the branch id so that we could use the request in a new
// transaction
removeBranchID(reoriginatedRequest);
if (challenge == null || reoriginatedRequest == null) {
throw new NullPointerException("A null argument was passed to handle challenge.");
}
ListIterator authHeaders = null;
if (challenge.getStatusCode() == Response.UNAUTHORIZED) {
authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
} else if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED) {
authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
} else {
throw new IllegalArgumentException("Unexpected status code ");
}
if (authHeaders == null) {
throw new IllegalArgumentException("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));
try {
cSeq.setSeqNumber(cSeq.getSeqNumber() + 1l);
} catch (InvalidArgumentException ex) {
throw new SipException("Invalid CSeq -- could not increment : " + cSeq.getSeqNumber());
}
/* Resolve this to the next hop based on the previous lookup. If we are not using
* lose routing (RFC2543) then just attach hop as a maddr param.
*/
if (challengedRequest.getRouteHeaders() == null) {
Hop hop = ((SIPClientTransaction) challengedTransaction).getNextHop();
SipURI sipUri = (SipURI) reoriginatedRequest.getRequestURI();
// BEGIN android-added
if (!hop.getHost().equalsIgnoreCase(sipUri.getHost()) && !hop.equals(sipStack.getRouter(challengedRequest).getOutboundProxy()))
// END android-added
sipUri.setMAddrParam(hop.getHost());
if (hop.getPort() != -1)
sipUri.setPort(hop.getPort());
}
ClientTransaction retryTran = transactionCreator.getNewClientTransaction(reoriginatedRequest);
WWWAuthenticateHeader authHeader = null;
SipURI requestUri = (SipURI) challengedTransaction.getRequest().getRequestURI();
while (authHeaders.hasNext()) {
authHeader = (WWWAuthenticateHeader) authHeaders.next();
String realm = authHeader.getRealm();
AuthorizationHeader authorization = null;
String sipDomain;
if (this.accountManager instanceof SecureAccountManager) {
UserCredentialHash credHash = ((SecureAccountManager) this.accountManager).getCredentialHash(challengedTransaction, realm);
URI uri = reoriginatedRequest.getRequestURI();
sipDomain = credHash.getSipDomain();
authorization = this.getAuthorization(reoriginatedRequest.getMethod(), uri.toString(), (reoriginatedRequest.getContent() == null) ? "" : new String(reoriginatedRequest.getRawContent()), authHeader, credHash);
} else {
UserCredentials userCreds = ((AccountManager) this.accountManager).getCredentials(challengedTransaction, realm);
sipDomain = userCreds.getSipDomain();
if (userCreds == null)
throw new SipException("Cannot find user creds for the given user name and realm");
// we haven't yet authenticated this realm since we were
// started.
authorization = this.getAuthorization(reoriginatedRequest.getMethod(), reoriginatedRequest.getRequestURI().toString(), (reoriginatedRequest.getContent() == null) ? "" : new String(reoriginatedRequest.getRawContent()), authHeader, userCreds);
}
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logDebug("Created authorization header: " + authorization.toString());
if (cacheTime != 0)
cachedCredentials.cacheAuthorizationHeader(sipDomain, authorization, cacheTime);
reoriginatedRequest.addHeader(authorization);
}
if (sipStack.isLoggingEnabled()) {
sipStack.getStackLogger().logDebug("Returning authorization transaction." + retryTran);
}
return retryTran;
} catch (SipException ex) {
throw ex;
} catch (Exception ex) {
sipStack.getStackLogger().logError("Unexpected exception ", ex);
throw new SipException("Unexpected exception ", ex);
}
}
use of javax.sip.header.CSeqHeader in project XobotOS by xamarin.
the class SipHelper method createRequest.
private Request createRequest(String requestType, SipProfile caller, SipProfile callee, String tag) throws ParseException, SipException {
FromHeader fromHeader = createFromHeader(caller, tag);
ToHeader toHeader = createToHeader(callee);
SipURI requestURI = callee.getUri();
List<ViaHeader> viaHeaders = createViaHeaders();
CallIdHeader callIdHeader = createCallIdHeader();
CSeqHeader cSeqHeader = createCSeqHeader(requestType);
MaxForwardsHeader maxForwards = createMaxForwardsHeader();
Request request = mMessageFactory.createRequest(requestURI, requestType, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
request.addHeader(createContactHeader(caller));
return request;
}
use of javax.sip.header.CSeqHeader in project load-balancer by RestComm.
the class Shootist method sendInitialInvite.
public void sendInitialInvite() {
try {
if (!started)
start();
String fromName = "BigGuy";
String fromSipAddress = "here.com";
String fromDisplayName = "The Master Blaster";
String toSipAddress = "there.com";
String toUser = "LittleGuy";
String toDisplayName = "The Little Blister";
// create >From Header
SipURI fromAddress = addressFactory.createSipURI(fromName, fromSipAddress);
Address fromNameAddress = addressFactory.createAddress(fromAddress);
fromNameAddress.setDisplayName(fromDisplayName);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "12345");
// create To Header
SipURI toAddress = addressFactory.createSipURI(toUser, toSipAddress);
Address toNameAddress = addressFactory.createAddress(toAddress);
toNameAddress.setDisplayName(toDisplayName);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
// create Request URI
SipURI requestURI = addressFactory.createSipURI(toUser, peerHostPort);
// Create ViaHeaders
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
String ipAddress = udpListeningPoint.getIPAddress();
ViaHeader viaHeader = headerFactory.createViaHeader(ipAddress, sipProvider.getListeningPoint(transport).getPort(), transport, null);
// add via headers
viaHeaders.add(viaHeader);
// Create ContentTypeHeader
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
// Create a new CallId header
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// Create a new Cseq header
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);
// Create a new MaxForwardsHeader
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
// Create the request.
Request request = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
// Create contact headers
String host = "127.0.0.1";
SipURI contactUrl = addressFactory.createSipURI(fromName, host);
contactUrl.setPort(udpListeningPoint.getPort());
contactUrl.setLrParam();
// Create the contact name address.
SipURI contactURI = addressFactory.createSipURI(fromName, host);
contactURI.setPort(sipProvider.getListeningPoint(transport).getPort());
Address contactAddress = addressFactory.createAddress(contactURI);
// Add the contact address.
contactAddress.setDisplayName(fromName);
contactHeader = headerFactory.createContactHeader(contactAddress);
request.addHeader(contactHeader);
// You can add extension headers of your own making
// to the outgoing SIP request.
// Add the extension header.
Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
request.addHeader(extensionHeader);
String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 129.6.55.78\r\n" + "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
byte[] contents = sdpData.getBytes();
request.setContent(contents, contentTypeHeader);
// You can add as many extension headers as you
// want.
extensionHeader = headerFactory.createHeader("My-Other-Header", "my new header value ");
request.addHeader(extensionHeader);
Header callInfoHeader = headerFactory.createHeader("Call-Info", "<http://www.antd.nist.gov>");
request.addHeader(callInfoHeader);
// Create the client transaction.
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
dialog = inviteTid.getDialog();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
fail("Unxpected exception ");
}
}
use of javax.sip.header.CSeqHeader in project load-balancer by RestComm.
the class WorstCaseUdpTestAffinityAlgorithm method processAssignedExternalRequest.
public Node processAssignedExternalRequest(Request request, Node assignedNode) {
Boolean isIpV6 = LbUtils.isValidInet6Address(assignedNode.getIp());
// if((y++)%2==0) if(request.getHeader("CSeq").toString().contains("1")) return assignedNode;
String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
long cseq = cs.getSeqNumber();
if (callIdMap.get(callId) != null) {
assignedNode = callIdMap.get(callId);
}
ViaHeader via = (ViaHeader) request.getHeader(Via.NAME);
String transport = via.getTransport().toLowerCase();
RouteHeader route = (RouteHeader) request.getHeader(RouteHeader.NAME);
SipURI uri = null;
if (route != null) {
uri = (SipURI) route.getAddress().getURI();
} else {
uri = (SipURI) request.getRequestURI();
}
try {
Node node;
if (!request.getMethod().equalsIgnoreCase("ACK")) {
// Gvag: new transaction should go to a new node
// getNodeA(callId+cseq);
Node newNode = nextAvailableNode(isIpV6);
if (newNode == null) {
// for(Node currNode:invocationContext.nodes) {
for (Node currNode : invocationContext.sipNodeMap(isIpV6).values()) {
if (!currNode.equals(assignedNode)) {
newNode = currNode;
}
}
}
node = newNode;
} else
node = assignedNode;
uri.setHost(node.getIp());
if (balancerContext.internalTransport != null) {
transport = balancerContext.internalTransport.toLowerCase();
} else if (balancerContext.terminateTLSTraffic)
if (transport.equalsIgnoreCase(ListeningPoint.TLS))
transport = ListeningPoint.TCP.toLowerCase();
else if (transport.equalsIgnoreCase(ListeningPointExt.WSS))
transport = ListeningPointExt.WS.toLowerCase();
Integer port = Integer.parseInt(node.getProperties().get(transport + "Port"));
uri.setPort(port);
callIdMap.put(callId, node);
setNodeA(callId + cseq, node);
if (request.getRequestURI().isSipURI()) {
SipURI ruri = (SipURI) request.getRequestURI();
String rurihostid = ruri.getHost() + ruri.getPort();
String originalhostid = assignedNode.getIp() + assignedNode.getProperties().get(transport + "Port");
if (rurihostid.equals(originalhostid)) {
ruri.setPort(port);
ruri.setHost(node.getIp());
}
}
return node;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return assignedNode;
}
use of javax.sip.header.CSeqHeader in project load-balancer by RestComm.
the class WorstCaseUdpTestAffinityAlgorithm method processExternalRequest.
public Node processExternalRequest(Request request, Boolean isIpV6) {
String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
Node node;
CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
long cseq = cs.getSeqNumber();
node = callIdMap.get(callId);
callIdTimestamps.put(callId, System.currentTimeMillis());
if (node == null) {
//
node = nextAvailableNode(isIpV6);
if (node == null)
return null;
callIdMap.put(callId, node);
if (logger.isDebugEnabled()) {
logger.debug("No node found in the affinity map. It is null. We select new node: " + node);
}
} else {
// if(!invocationContext.nodes.contains(node)) { // If the assigned node is now dead
if (!invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
// If the assigned node is now dead
node = selectNewNode(node, callId);
} else {
// .. and we just leave it like that
if (logger.isDebugEnabled()) {
logger.debug("The assigned node in the affinity map is still alive: " + node);
}
if (!request.getMethod().equals("ACK")) {
// for(Node n:invocationContext.nodes) {
for (Node n : invocationContext.sipNodeMap(isIpV6).values()) {
if (!n.equals(node))
node = n;
break;
}
}
}
}
setNodeA(callId + cseq, node);
callIdMap.put(callId, node);
// }
return node;
}
Aggregations