use of javax.sip.header.ViaHeader in project load-balancer by RestComm.
the class Shootist method processInvite.
public void processInvite(Request request, ServerTransaction stx) {
try {
inviteRequest = request;
Response response = messageFactory.createResponse(180, request);
contactHeader = headerFactory.createContactHeader(addressFactory.createAddress("sip:here@" + localIPAddress + ":" + localPort));
response.addHeader(contactHeader);
dialog = stx.getDialog();
// check removing incorrect patching last via https://github.com/RestComm/load-balancer/issues/97
FromHeader fromHeader = (FromHeader) response.getHeader(From.NAME);
URI currUri = fromHeader.getAddress().getURI();
String fromUser = null;
if (currUri.isSipURI()) {
fromUser = ((SipURI) currUri).getUser();
if (fromUser.equals("senderToNexmo")) {
ViaHeader lastViaHeader = null;
ListIterator<ViaHeader> it = response.getHeaders(ViaHeader.NAME);
while (it.hasNext()) lastViaHeader = it.next();
lastViaHeader.setReceived("127.0.0.2");
lastViaHeader.setParameter("rport", "1111");
}
}
stx.sendResponse(response);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response = messageFactory.createResponse(200, request);
contactHeader = headerFactory.createContactHeader(addressFactory.createAddress("sip:here@" + localIPAddress + ":" + localPort));
response.addHeader(contactHeader);
dialog = stx.getDialog();
stx.sendResponse(response);
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of javax.sip.header.ViaHeader 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.ViaHeader in project load-balancer by RestComm.
the class SIPBalancerForwarder method getSenderNode.
// private Node getTransactionSourceNode(Response response) {
// ViaHeader viaHeader = ((ViaHeader)response.getHeader(ViaHeader.NAME));
// String host = viaHeader.getHost();
// String transport = viaHeader.getTransport();
// if(transport == null) transport = ListeningPoint.UDP;
// transport = transport.toLowerCase();
// int port = viaHeader.getPort();
// if(extraServerAddresses != null) {
// for(int q=0; q<extraServerAddresses.length; q++) {
// if(extraServerAddresses[q].equals(host) && extraServerPorts[q] == port) {
// return ExtraServerNode.extraServerNode;
// }
// }
// }
// Node node = getNodeDeadOrAlive(host, port, transport);
// if(node != null) {
// return node;
// }
// return null;
// }
private Node getSenderNode(Response response) {
SIPResponse resp = (SIPResponse) response;
String host = resp.getRemoteAddress().getHostAddress();
ViaHeader viaHeader = ((ViaHeader) response.getHeader(ViaHeader.NAME));
String currentTransport = viaHeader.getTransport();
String transport = null;
if (balancerRunner.balancerContext.internalTransport != null) {
if (logger.isDebugEnabled()) {
logger.debug("Set internal transport for getting sender Node: " + balancerRunner.balancerContext.internalTransport);
}
transport = balancerRunner.balancerContext.internalTransport;
} else if (balancerRunner.balancerContext.terminateTLSTraffic) {
if (currentTransport.equalsIgnoreCase(ListeningPoint.TLS))
transport = ListeningPoint.TCP;
else if (currentTransport.equalsIgnoreCase(ListeningPointExt.WSS))
transport = ListeningPointExt.WS;
else
transport = currentTransport;
} else {
transport = currentTransport;
}
if (transport == null)
transport = ListeningPoint.UDP;
int port = resp.getRemotePort();
if (extraServerAddresses != null) {
for (int q = 0; q < extraServerAddresses.length; q++) {
if (extraServerAddresses[q].equals(host) && extraServerPorts[q] == port) {
return ExtraServerNode.extraServerNode;
}
}
}
Node node = getNodeDeadOrAlive(host, port, transport);
if (node != null) {
return node;
}
return null;
}
use of javax.sip.header.ViaHeader in project load-balancer by RestComm.
the class SIPBalancerForwarder method isViaHeaderFromServer.
private boolean isViaHeaderFromServer(Request request) {
ViaHeader viaHeader = ((ViaHeader) request.getHeader(ViaHeader.NAME));
String host = viaHeader.getHost();
String transport = viaHeader.getTransport();
if (balancerRunner.balancerContext.internalTransport != null) {
if (logger.isDebugEnabled()) {
logger.debug("Set internal transport for Via checking (is from server): " + balancerRunner.balancerContext.internalTransport);
}
transport = balancerRunner.balancerContext.internalTransport;
} else if (balancerRunner.balancerContext.terminateTLSTraffic) {
if (transport.equalsIgnoreCase(ListeningPoint.TLS))
transport = ListeningPoint.TCP;
if (transport.equalsIgnoreCase(ListeningPointExt.WSS))
transport = ListeningPointExt.WS;
}
if (transport == null)
transport = ListeningPoint.UDP;
int port = viaHeader.getPort();
if (extraServerAddresses != null) {
for (int q = 0; q < extraServerAddresses.length; q++) {
if (extraServerAddresses[q].equals(host) && extraServerPorts[q] == port) {
return true;
}
}
}
if (getAliveNodeAnyVersion(host, port, transport) != null) {
return true;
}
return false;
}
use of javax.sip.header.ViaHeader 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;
}
Aggregations