use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class SIPBalancerForwarder method getLoopbackUri.
public SipURI getLoopbackUri(Request request, boolean isIpv6) {
if (logger.isDebugEnabled())
logger.debug("Check request for loop. Request: " + request);
SipURI uri = null;
RouteHeader route = (RouteHeader) request.getHeader(RouteHeader.NAME);
if (route != null) {
if (route.getAddress().getURI().isSipURI()) {
uri = (SipURI) route.getAddress().getURI();
}
} else {
if (request.getRequestURI().isSipURI()) {
uri = (SipURI) request.getRequestURI();
}
}
if (uri.getHost().matches(".*[a-zA-Z]+.*")) {
if (logger.isDebugEnabled())
logger.debug("We are going to patch URI because it has domain name instead of IP : " + uri);
try {
uri.setHost(InetAddress.getByName(uri.getHost()).getHostAddress());
} catch (UnknownHostException | ParseException e) {
e.printStackTrace();
}
}
if (uri != null) {
if (!isIpv6) {
if (uri.getHost().equals(balancerRunner.balancerContext.externalHost) || uri.getHost().equals(balancerRunner.balancerContext.publicIP) || (balancerRunner.balancerContext.externalIpLoadBalancerAddresses != null && balancerRunner.balancerContext.externalIpLoadBalancerAddresses.contains(uri.getHost()))) {
int port = uri.getPort();
for (int externalPort : balancerRunner.balancerContext.externalPorts) if (port == externalPort)
return uri;
}
if (uri.getHost().equals(balancerRunner.balancerContext.internalHost) || uri.getHost().equals(balancerRunner.balancerContext.publicIP) || (balancerRunner.balancerContext.internalIpLoadBalancerAddresses != null && balancerRunner.balancerContext.internalIpLoadBalancerAddresses.contains(uri.getHost()))) {
int port = uri.getPort();
for (int internalPort : balancerRunner.balancerContext.internalPorts) if (port == internalPort)
return uri;
}
} else {
if (uri.getHost().equals(balancerRunner.balancerContext.externalIpv6Host) || uri.getHost().equals(balancerRunner.balancerContext.publicIPv6) || (balancerRunner.balancerContext.externalIpv6LoadBalancerAddresses != null && balancerRunner.balancerContext.externalIpv6LoadBalancerAddresses.contains(uri.getHost()))) {
int port = uri.getPort();
for (int externalIpv6Port : balancerRunner.balancerContext.externalIpv6Ports) if (port == externalIpv6Port)
return uri;
}
if (uri.getHost().equals(balancerRunner.balancerContext.internalIpv6Host) || uri.getHost().equals(balancerRunner.balancerContext.publicIPv6) || (balancerRunner.balancerContext.internalIpv6LoadBalancerAddresses != null && balancerRunner.balancerContext.internalIpv6LoadBalancerAddresses.contains(uri.getHost()))) {
int port = uri.getPort();
for (int internalIpv6Port : balancerRunner.balancerContext.internalIpv6Ports) if (port == internalIpv6Port)
return uri;
}
}
}
return null;
}
use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class SIPBalancerForwarder method createRecordRouteHeaders.
private void createRecordRouteHeaders(ArrayList<ListeningPoint> listeningPoints, RecordRouteHeader[] activeHeaders) throws ParseException {
for (ListeningPoint listeningPoint : listeningPoints) {
SipURI internalLocalUri = balancerRunner.balancerContext.addressFactory.createSipURI(null, listeningPoint.getIPAddress());
internalLocalUri.setPort(listeningPoint.getPort());
internalLocalUri.setTransportParam(listeningPoint.getTransport());
// See RFC 3261 19.1.1 for lr parameter
internalLocalUri.setLrParam();
Address internalLocalAddress = balancerRunner.balancerContext.addressFactory.createAddress(internalLocalUri);
internalLocalAddress.setURI(internalLocalUri);
if (logger.isDebugEnabled()) {
logger.debug("adding internal Record Router Header :" + internalLocalAddress);
}
activeHeaders[getIndexByTransport(listeningPoint.getTransport())] = balancerRunner.balancerContext.headerFactory.createRecordRouteHeader(internalLocalAddress);
}
}
use of javax.sip.address.SipURI in project load-balancer by RestComm.
the class SIPBalancerForwarder method stampRecordRoute.
private RecordRouteHeader stampRecordRoute(RecordRouteHeader rrh, RouteHeaderHints hints, String transport) {
SipURI uri = (SipURI) rrh.getAddress().getURI();
try {
if (hints.serverAssignedNode != null) {
if (logger.isDebugEnabled()) {
logger.debug("About to stamp RecordRoute for hints:\n" + hints.serverAssignedNode.toString() + "\n");
}
uri.setParameter(ROUTE_PARAM_NODE_HOST, hints.serverAssignedNode.getIp());
uri.setParameter(ROUTE_PARAM_NODE_PORT, hints.serverAssignedNode.getProperties().get(transport.toLowerCase() + "Port").toString());
uri.setParameter(ROUTE_PARAM_NODE_VERSION, hints.version);
} else {
if (logger.isDebugEnabled()) {
logger.debug("No serverAssignedNode could be found, not stamping the record route\n");
}
}
} catch (ParseException e) {
logger.warn("Problem adding rrh", e);
}
return rrh;
}
use of javax.sip.address.SipURI 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.address.SipURI in project load-balancer by RestComm.
the class TestSipListener method processSubscribe.
/**
* Process the invite request.
*/
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
try {
logger.info("notifier: got an Subscribe sending OK");
logger.info("notifier: " + request);
logger.info("notifier : dialog = " + requestEvent.getDialog());
EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
// this.gotSubscribeRequest = true;
// Always create a ServerTransaction, best as early as possible in the code
Response response = null;
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
// Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
boolean isInitial = requestEvent.getDialog() == null;
if (isInitial) {
// JvB: need random tags to test forking
String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
response = protocolObjects.messageFactory.createResponse(202, request);
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
// Sanity check: to header should not ahve a tag. Else the dialog
// should have matched
// Application is supposed to set.
toHeader.setTag(toTag);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
} else {
response = protocolObjects.messageFactory.createResponse(202, request);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
}
// Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
else
address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
response.addHeader(contactHeader);
// Expires header is mandatory in 2xx responses to SUBSCRIBE
ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
if (expires == null) {
// rather short
expires = protocolObjects.headerFactory.createExpiresHeader(30);
}
response.addHeader(expires);
/*
* JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2:
* "[...] a NOTIFY message is always sent immediately after any 200-
* class response to a SUBSCRIBE request"
*
* Do this before creating the NOTIFY request below
*/
st.sendResponse(response);
/*
* NOTIFY requests MUST contain a "Subscription-State" header with a
* value of "active", "pending", or "terminated". The "active" value
* indicates that the subscription has been accepted and has been
* authorized (in most cases; see section 5.2.). The "pending" value
* indicates that the subscription has been received, but that
* policy information is insufficient to accept or deny the
* subscription at this time. The "terminated" value indicates that
* the subscription is not active.
*/
if (sendNotify) {
Request notifyRequest = dialog.createRequest("NOTIFY");
// Mark the contact header, to check that the remote contact is updated
// ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
// Initial state is pending, second time we assume terminated (Expires==0)
SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
allSubscriptionStates.add(sstate.getState().toLowerCase());
// Need a reason for terminated
if (sstate.getState().equalsIgnoreCase("terminated")) {
sstate.setReasonCode("deactivated");
}
notifyRequest.addHeader(sstate);
notifyRequest.setHeader(eventHeader);
notifyRequest.setHeader(contactHeader);
// notifyRequest.setHeader(routeHeader);
ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
Thread.sleep(timeToWaitBetweenSubsNotify);
}
// Let the other side know that the tx is pending acceptance
//
dialog.sendRequest(ct);
logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
logger.info("Dialog " + dialog);
logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
if (expires.getExpires() != 0) {
Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
myEventSource.start();
}
}
} catch (Throwable ex) {
logger.info(ex.getMessage(), ex);
}
}
Aggregations