List of usage examples for java.net InetAddress isSiteLocalAddress
public boolean isSiteLocalAddress()
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * @since 3.1/*from ww w.j a v a 2s. c o m*/ */ protected void _parsePassiveModeReply(String reply) throws MalformedServerReplyException { java.util.regex.Matcher m = __PARMS_PAT.matcher(reply); if (!m.find()) { throw new MalformedServerReplyException( "Could not parse passive host information.\nServer Reply: " + reply); } __passiveHost = m.group(1).replace(',', '.'); // Fix up to look like IP address try { int oct1 = Integer.parseInt(m.group(2)); int oct2 = Integer.parseInt(m.group(3)); __passivePort = (oct1 << 8) | oct2; } catch (NumberFormatException e) { throw new MalformedServerReplyException( "Could not parse passive port information.\nServer Reply: " + reply); } if (__passiveNatWorkaround) { try { InetAddress host = InetAddress.getByName(__passiveHost); // reply is a local address, but target is not - assume NAT box changed the PASV reply if (host.isSiteLocalAddress()) { InetAddress remote = getRemoteAddress(); if (!remote.isSiteLocalAddress()) { String hostAddress = remote.getHostAddress(); fireReplyReceived(0, "[Replacing site local address " + __passiveHost + " with " + hostAddress + "]\n"); __passiveHost = hostAddress; } } } catch (UnknownHostException e) { // Should not happen as we are passing in an IP address throw new MalformedServerReplyException( "Could not parse passive host information.\nServer Reply: " + reply); } } }
From source file:org.restcomm.connect.telephony.CallManager.java
public void bye(final Object message) throws IOException { final ActorRef self = self(); final SipServletRequest request = (SipServletRequest) message; final SipApplicationSession application = request.getApplicationSession(); // if this response is coming from a client that is in a p2p session with another registered client // we will just proxy the response SipSession linkedB2BUASession = B2BUAHelper.getLinkedSession(request); if (linkedB2BUASession != null) { if (logger.isInfoEnabled()) { logger.info(String.format("B2BUA: Got BYE request: \n %s", request)); }/*from ww w . j ava 2 s . c om*/ //Prepare the BYE request to the linked session request.getSession().setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, request); SipServletRequest clonedBye = linkedB2BUASession.createRequest("BYE"); linkedB2BUASession.setAttribute(B2BUAHelper.B2BUA_LAST_REQUEST, clonedBye); if (patchForNatB2BUASessions) { // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307 SipURI toInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.TO_INET_URI); SipURI fromInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.FROM_INET_URI); InetAddress byeRURI = null; try { byeRURI = InetAddress.getByName(((SipURI) clonedBye.getRequestURI()).getHost()); } catch (UnknownHostException e) { } boolean isBehindLB = false; final String initialIpBeforeLB = request.getHeader("X-Sip-Balancer-InitialRemoteAddr"); String initialPortBeforeLB = request.getHeader("X-Sip-Balancer-InitialRemotePort"); if (initialIpBeforeLB != null) { if (initialPortBeforeLB == null) initialPortBeforeLB = "5060"; if (logger.isDebugEnabled()) { logger.debug( "We are behind load balancer, checking if the request URI needs to be patched"); } isBehindLB = true; } if (logger.isDebugEnabled()) { logger.debug("toInetUri: " + toInetUri + " fromInetUri: " + fromInetUri + " byeRURI: " + byeRURI + " initialIpBeforeLB: " + initialIpBeforeLB + " initialPortBeforeLB: " + initialPortBeforeLB); } if (toInetUri != null && byeRURI == null) { if (logger.isInfoEnabled()) { logger.info("Using the real To inet ip address of the sip client " + toInetUri.toString() + " as a request uri of the CloneBye request"); } clonedBye.setRequestURI(toInetUri); } else if (toInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress() || byeRURI.isLoopbackAddress())) { if (logger.isInfoEnabled()) { logger.info("Using the real To inet ip address of the sip client " + toInetUri.toString() + " as a request uri of the CloneBye request"); } clonedBye.setRequestURI(toInetUri); } else if (fromInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress() || byeRURI.isLoopbackAddress())) { if (isBehindLB) { // https://github.com/RestComm/Restcomm-Connect/issues/1357 boolean patchRURI = isLBPatchRURI(clonedBye, initialIpBeforeLB, initialPortBeforeLB); if (patchRURI) { if (logger.isDebugEnabled()) { logger.debug( "We are behind load balancer, but Using the real ip address of the sip client " + fromInetUri.toString() + " as a request uri of the CloneBye request"); } clonedBye.setRequestURI(fromInetUri); } } else { if (logger.isInfoEnabled()) { logger.info("Using the real From inet ip address of the sip client " + fromInetUri.toString() + " as a request uri of the CloneBye request"); } clonedBye.setRequestURI(fromInetUri); } } else if (toInetUri == null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress() || byeRURI.isLoopbackAddress())) { if (logger.isInfoEnabled()) { logger.info( "Public IP toInetUri from SipSession is null, will check LB headers from last Response"); } if (isBehindLB) { String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB; SipURI uri = sipFactory.createSipURI(null, realIP); boolean patchRURI = isLBPatchRURI(clonedBye, initialIpBeforeLB, initialPortBeforeLB); if (patchRURI) { if (logger.isDebugEnabled()) { logger.debug("We are behind load balancer, will use: " + initialIpBeforeLB + ":" + initialPortBeforeLB + " for the cloned BYE message"); } clonedBye.setRequestURI(uri); } if (logger.isInfoEnabled()) { logger.info("We are behind load balancer, will use Initial Remote Address " + initialIpBeforeLB + ":" + initialPortBeforeLB + " for the cloned BYE request"); } } else { if (logger.isInfoEnabled()) { logger.info("LB Headers are also null"); } } } } B2BUAHelper.updateCDR(request, CallStateChanged.State.COMPLETED); //Prepare 200 OK for received BYE SipServletResponse okay = request.createResponse(Response.OK); okay.send(); //Send the Cloned BYE if (logger.isInfoEnabled()) { logger.info(String.format("B2BUA: Will send out Cloned BYE request: \n %s", clonedBye)); } clonedBye.send(); } else { final ActorRef call = (ActorRef) application.getAttribute(Call.class.getName()); if (call != null) call.tell(request, self); } }
From source file:org.mobicents.servlet.restcomm.telephony.Call.java
private void sendBye(Hangup hangup) throws IOException, TransitionNotFoundException, TransitionFailedException, TransitionRollbackException {// w w w . j a va 2s. c om final SipSession session = invite.getSession(); String sessionState = session.getState().name(); if (sessionState == SipSession.State.INITIAL.name() || (sessionState == SipSession.State.EARLY.name() && isInbound())) { final SipServletResponse resp = invite.createResponse(Response.SERVER_INTERNAL_ERROR); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { resp.addHeader("Reason", hangup.getMessage()); } addCustomHeaders(resp); resp.send(); fsm.transition(hangup, completed); return; } if (sessionState == SipSession.State.EARLY.name()) { final SipServletRequest cancel = invite.createCancel(); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { cancel.addHeader("Reason", hangup.getMessage()); } addCustomHeaders(cancel); cancel.send(); fsm.transition(hangup, completed); return; } else { final SipServletRequest bye = session.createRequest("BYE"); addCustomHeaders(bye); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { bye.addHeader("Reason", hangup.getMessage()); } SipURI realInetUri = (SipURI) session.getAttribute("realInetUri"); InetAddress byeRURI = InetAddress.getByName(((SipURI) bye.getRequestURI()).getHost()); // INVITE sip:+12055305520@107.21.247.251 SIP/2.0 // Record-Route: <sip:10.154.28.245:5065;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0> // Record-Route: <sip:10.154.28.245:5060;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0> // Record-Route: <sip:67.231.8.195;lr=on;ftag=gK0043eb81> // Record-Route: <sip:67.231.4.204;r2=on;lr=on;ftag=gK0043eb81> // Record-Route: <sip:192.168.6.219;r2=on;lr=on;ftag=gK0043eb81> // Accept: application/sdp // Allow: INVITE,ACK,CANCEL,BYE // Via: SIP/2.0/UDP 10.154.28.245:5065;branch=z9hG4bK1cdb.193075b2.058724zsd_0 // Via: SIP/2.0/UDP 10.154.28.245:5060;branch=z9hG4bK1cdb.193075b2.058724_0 // Via: SIP/2.0/UDP 67.231.8.195;branch=z9hG4bK1cdb.193075b2.0 // Via: SIP/2.0/UDP 67.231.4.204;branch=z9hG4bK1cdb.f9127375.0 // Via: SIP/2.0/UDP 192.168.16.114:5060;branch=z9hG4bK00B6ff7ff87ed50497f // From: <sip:+1302109762259@192.168.16.114>;tag=gK0043eb81 // To: <sip:12055305520@192.168.6.219> // Call-ID: 587241765_133360558@192.168.16.114 // CSeq: 393447729 INVITE // Max-Forwards: 67 // Contact: <sip:+1302109762259@192.168.16.114:5060> // Diversion: <sip:+112055305520@192.168.16.114:5060>;privacy=off;screen=no; reason=unknown; counter=1 // Supported: replaces // Content-Disposition: session;handling=required // Content-Type: application/sdp // Remote-Party-ID: <sip:+1302109762259@192.168.16.114:5060>;privacy=off;screen=no // X-Sip-Balancer-InitialRemoteAddr: 67.231.8.195 // X-Sip-Balancer-InitialRemotePort: 5060 // Route: <sip:10.13.169.214:5080;transport=udp;lr> // Content-Length: 340 ListIterator<String> recordRouteList = invite.getHeaders(RecordRouteHeader.NAME); if (invite.getHeader("X-Sip-Balancer-InitialRemoteAddr") != null) { if (logger.isInfoEnabled()) { logger.info( "We are behind LoadBalancer and will remove the first two RecordRoutes since they are the LB node"); } recordRouteList.next(); recordRouteList.remove(); recordRouteList.next(); recordRouteList.remove(); } if (recordRouteList.hasNext()) { if (logger.isInfoEnabled()) { logger.info("Record Route is set, wont change the Request URI"); } } else { if (logger.isInfoEnabled()) { logger.info("Checking RURI, realInetUri: " + realInetUri + " byeRURI: " + byeRURI); } if (logger.isDebugEnabled()) { logger.debug("byeRURI.isSiteLocalAddress(): " + byeRURI.isSiteLocalAddress()); logger.debug("byeRURI.isAnyLocalAddress(): " + byeRURI.isAnyLocalAddress()); logger.debug("byeRURI.isLoopbackAddress(): " + byeRURI.isLoopbackAddress()); } if (realInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress() || byeRURI.isLoopbackAddress())) { if (logger.isInfoEnabled()) { logger.info("Using the real ip address of the sip client " + realInetUri.toString() + " as a request uri of the BYE request"); } bye.setRequestURI(realInetUri); } } if (logger.isInfoEnabled()) { logger.info("Will sent out BYE to: " + bye.getRequestURI()); } bye.send(); } }
From source file:org.restcomm.connect.telephony.CallManager.java
private void ack(SipServletRequest request) throws IOException { SipServletResponse response = B2BUAHelper.getLinkedResponse(request); // if this is an ACK that belongs to a B2BUA session, then we proxy it to the other client if (response != null) { SipServletRequest ack = response.createAck(); // if (!ack.getHeaders("Route").hasNext() && patchForNatB2BUASessions) { if (patchForNatB2BUASessions) { InetAddress ackRURI = null; try { ackRURI = InetAddress.getByName(((SipURI) ack.getRequestURI()).getHost()); } catch (UnknownHostException e) { }//w w w . ja va 2 s . c om boolean isBehindLB = false; final String initialIpBeforeLB = response.getHeader("X-Sip-Balancer-InitialRemoteAddr"); String initialPortBeforeLB = response.getHeader("X-Sip-Balancer-InitialRemotePort"); if (initialIpBeforeLB != null) { if (initialPortBeforeLB == null) initialPortBeforeLB = "5060"; if (logger.isDebugEnabled()) { logger.debug( "We are behind load balancer, checking if the request URI needs to be patched"); } isBehindLB = true; } // Issue #307: https://telestax.atlassian.net/browse/RESTCOMM-307 SipURI toInetUri = (SipURI) request.getSession().getAttribute(B2BUAHelper.TO_INET_URI); if (toInetUri != null && ackRURI == null) { if (isBehindLB) { // https://github.com/RestComm/Restcomm-Connect/issues/1357 boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB); if (patchRURI) { if (logger.isDebugEnabled()) { logger.debug( "We are behind load balancer, but Using the real ip address of the sip client " + toInetUri.toString() + " as a request uri of the ACK request"); } ack.setRequestURI(toInetUri); } else { // https://github.com/RestComm/Restcomm-Connect/issues/1357 if (logger.isDebugEnabled()) { logger.debug( "removing the toInetUri to avoid the other subsequent requests using it " + toInetUri.toString()); } request.getSession().removeAttribute(B2BUAHelper.TO_INET_URI); } } else { if (logger.isInfoEnabled()) { logger.info("Using the real ip address of the sip client " + toInetUri.toString() + " as a request uri of the ACK request"); } ack.setRequestURI(toInetUri); } } else if (toInetUri != null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress() || ackRURI.isLoopbackAddress())) { if (isBehindLB) { // https://github.com/RestComm/Restcomm-Connect/issues/1357 boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB); if (patchRURI) { if (logger.isDebugEnabled()) { logger.debug( "We are behind load balancer, but Using the real ip address of the sip client " + toInetUri.toString() + " as a request uri of the ACK request"); } ack.setRequestURI(toInetUri); } else { // https://github.com/RestComm/Restcomm-Connect/issues/1357 if (logger.isDebugEnabled()) { logger.debug( "removing the toInetUri to avoid the other subsequent requests using it " + toInetUri.toString()); } request.getSession().removeAttribute(B2BUAHelper.TO_INET_URI); } } else { if (logger.isInfoEnabled()) { logger.info("Using the real ip address of the sip client " + toInetUri.toString() + " as a request uri of the ACK request"); } ack.setRequestURI(toInetUri); } } else if (toInetUri == null && (ackRURI.isSiteLocalAddress() || ackRURI.isAnyLocalAddress() || ackRURI.isLoopbackAddress())) { if (logger.isInfoEnabled()) { logger.info( "Public IP toInetUri from SipSession is null, will check LB headers from last Response"); } if (isBehindLB) { String realIP = initialIpBeforeLB + ":" + initialPortBeforeLB; SipURI uri = sipFactory.createSipURI(null, realIP); boolean patchRURI = isLBPatchRURI(ack, initialIpBeforeLB, initialPortBeforeLB); if (patchRURI) { if (logger.isDebugEnabled()) { logger.debug("We are behind load balancer, will use Initial Remote Address " + initialIpBeforeLB + ":" + initialPortBeforeLB + " for the ACK request"); } ack.setRequestURI(uri); } } else { if (logger.isInfoEnabled()) { logger.info("LB Headers are also null"); } } } } ack.send(); SipApplicationSession sipApplicationSession = request.getApplicationSession(); // Defaulting the sip application session to 1h sipApplicationSession.setExpires(60); } else { if (logger.isInfoEnabled()) { logger.info("Linked Response couldn't be found for ACK request"); } final ActorRef call = (ActorRef) request.getApplicationSession().getAttribute(Call.class.getName()); if (call != null) { if (logger.isInfoEnabled()) { logger.info("Will send ACK to call actor: " + call.path()); } call.tell(request, self()); } } // else { // SipSession sipSession = request.getSession(); // SipApplicationSession sipAppSession = request.getApplicationSession(); // if(sipSession.getInvalidateWhenReady()){ // logger.info("Invalidating sipSession: "+sipSession.getId()); // sipSession.invalidate(); // } // if(sipAppSession.getInvalidateWhenReady()){ // logger.info("Invalidating sipAppSession: "+sipAppSession.getId()); // sipAppSession.invalidate(); // } // } }
From source file:org.restcomm.connect.telephony.Call.java
private void sendBye(Hangup hangup) throws IOException, TransitionNotFoundException, TransitionFailedException, TransitionRollbackException {// w ww. j a va 2 s . c o m final SipSession session = invite.getSession(); final String sessionState = session.getState().name(); if (sessionState == SipSession.State.TERMINATED.name()) { if (logger.isInfoEnabled()) { logger.info("SipSession already TERMINATED, will not send BYE"); } return; } else { if (logger.isInfoEnabled()) { logger.info("About to send BYE, session state: " + sessionState); } } if (sessionState == SipSession.State.INITIAL.name() || (sessionState == SipSession.State.EARLY.name() && isInbound())) { int sipResponse = (enable200OkDelay && hangup.getSipResponse() != null) ? hangup.getSipResponse() : Response.SERVER_INTERNAL_ERROR; final SipServletResponse resp = invite.createResponse(sipResponse); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { resp.addHeader("Reason", hangup.getMessage()); } addCustomHeaders(resp); resp.send(); fsm.transition(hangup, completed); return; } if (sessionState == SipSession.State.EARLY.name()) { final SipServletRequest cancel = invite.createCancel(); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { cancel.addHeader("Reason", hangup.getMessage()); } addCustomHeaders(cancel); cancel.send(); external = CallStateChanged.State.CANCELED; fsm.transition(hangup, completed); return; } else { final SipServletRequest bye = session.createRequest("BYE"); addCustomHeaders(bye); if (hangup.getMessage() != null && !hangup.getMessage().equals("")) { bye.addHeader("Reason", hangup.getMessage()); } SipURI realInetUri = (SipURI) session.getAttribute("realInetUri"); InetAddress byeRURI = InetAddress.getByName(((SipURI) bye.getRequestURI()).getHost()); // INVITE sip:+12055305520@107.21.247.251 SIP/2.0 // Record-Route: <sip:10.154.28.245:5065;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0> // Record-Route: <sip:10.154.28.245:5060;transport=udp;lr;node_host=10.13.169.214;node_port=5080;version=0> // Record-Route: <sip:67.231.8.195;lr=on;ftag=gK0043eb81> // Record-Route: <sip:67.231.4.204;r2=on;lr=on;ftag=gK0043eb81> // Record-Route: <sip:192.168.6.219;r2=on;lr=on;ftag=gK0043eb81> // Accept: application/sdp // Allow: INVITE,ACK,CANCEL,BYE // Via: SIP/2.0/UDP 10.154.28.245:5065;branch=z9hG4bK1cdb.193075b2.058724zsd_0 // Via: SIP/2.0/UDP 10.154.28.245:5060;branch=z9hG4bK1cdb.193075b2.058724_0 // Via: SIP/2.0/UDP 67.231.8.195;branch=z9hG4bK1cdb.193075b2.0 // Via: SIP/2.0/UDP 67.231.4.204;branch=z9hG4bK1cdb.f9127375.0 // Via: SIP/2.0/UDP 192.168.16.114:5060;branch=z9hG4bK00B6ff7ff87ed50497f // From: <sip:+1302109762259@192.168.16.114>;tag=gK0043eb81 // To: <sip:12055305520@192.168.6.219> // Call-ID: 587241765_133360558@192.168.16.114 // CSeq: 393447729 INVITE // Max-Forwards: 67 // Contact: <sip:+1302109762259@192.168.16.114:5060> // Diversion: <sip:+112055305520@192.168.16.114:5060>;privacy=off;screen=no; reason=unknown; counter=1 // Supported: replaces // Content-Disposition: session;handling=required // Content-Type: application/sdp // Remote-Party-ID: <sip:+1302109762259@192.168.16.114:5060>;privacy=off;screen=no // X-Sip-Balancer-InitialRemoteAddr: 67.231.8.195 // X-Sip-Balancer-InitialRemotePort: 5060 // Route: <sip:10.13.169.214:5080;transport=udp;lr> // Content-Length: 340 ListIterator<String> recordRouteList = invite.getHeaders(RecordRouteHeader.NAME); if (invite.getHeader("X-Sip-Balancer-InitialRemoteAddr") != null) { if (logger.isInfoEnabled()) { logger.info( "We are behind LoadBalancer and will remove the first two RecordRoutes since they are the LB node"); } recordRouteList.next(); recordRouteList.remove(); recordRouteList.next(); recordRouteList.remove(); } if (recordRouteList.hasNext()) { if (logger.isInfoEnabled()) { logger.info("Record Route is set, wont change the Request URI"); } } else { if (logger.isInfoEnabled()) { logger.info("Checking RURI, realInetUri: " + realInetUri + " byeRURI: " + byeRURI); } if (logger.isDebugEnabled()) { logger.debug("byeRURI.isSiteLocalAddress(): " + byeRURI.isSiteLocalAddress()); logger.debug("byeRURI.isAnyLocalAddress(): " + byeRURI.isAnyLocalAddress()); logger.debug("byeRURI.isLoopbackAddress(): " + byeRURI.isLoopbackAddress()); } if (realInetUri != null && (byeRURI.isSiteLocalAddress() || byeRURI.isAnyLocalAddress() || byeRURI.isLoopbackAddress())) { if (logger.isInfoEnabled()) { logger.info("real ip address of the sip client " + realInetUri.toString() + " is not null, checking if the request URI needs to be patched"); } boolean patchRURI = true; try { // https://github.com/RestComm/Restcomm-Connect/issues/1336 checking if the initial IP and Port behind LB is part of the route set or not ListIterator<? extends Address> routes = bye.getAddressHeaders(RouteHeader.NAME); while (routes.hasNext() && patchRURI) { SipURI route = (SipURI) routes.next().getURI(); String routeHost = route.getHost(); int routePort = route.getPort(); if (routePort < 0) { routePort = 5060; } if (logger.isDebugEnabled()) { logger.debug("Checking if route " + routeHost + ":" + routePort + " is matching ip and port of realNetURI " + realInetUri.getHost() + ":" + realInetUri.getPort() + " for the BYE request"); } if (routeHost.equalsIgnoreCase(realInetUri.getHost()) && routePort == realInetUri.getPort()) { if (logger.isDebugEnabled()) { logger.debug("route " + route + " is matching ip and port of realNetURI " + realInetUri.getHost() + ":" + realInetUri.getPort() + " for the BYE request, so not patching the Request-URI"); } patchRURI = false; } } } catch (ServletParseException e) { logger.error("Impossible to parse the route set from the BYE " + bye, e); } if (patchRURI) { if (logger.isInfoEnabled()) { logger.info("Using the real ip address of the sip client " + realInetUri.toString() + " as a request uri of the BYE request"); } bye.setRequestURI(realInetUri); } } } if (logger.isInfoEnabled()) { logger.info("Will sent out BYE to: " + bye.getRequestURI()); } try { bye.send(); sentBye = true; } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Exception during Send Bye: " + e.toString()); } } } }