private ArrayList getLocalViaHeaders() throws IOException { /* * We can't keep a cached copy because the callers * of this method change the viaHeaders. In particular * a branch may be added which causes INVITES to fail. */ if (viaHeaders != null) { return viaHeaders; } ListeningPoint lp = sipProvider.getListeningPoint(); viaHeaders = new ArrayList(); try { String addr = lp.getIPAddress(); ViaHeader viaHeader = headerFactory.createViaHeader(addr, lp.getPort(), lp.getTransport(), null); viaHeader.setRPort(); viaHeaders.add(viaHeader); return viaHeaders; } catch (ParseException e) { throw new IOException( "A ParseException occurred while creating Via Headers! " + e.getMessage()); } catch (InvalidArgumentException e) { throw new IOException( "Unable to create a via header for port " + lp.getPort() + " " + e.getMessage()); } }
/** * parse the String message * * @return SIPHeader (AcceptEncoding object) * @throws ParseException if the message does not respect the spec. */ public SIPHeader parse() throws ParseException { AcceptEncodingList acceptEncodingList = new AcceptEncodingList(); if (debug) dbg_enter("AcceptEncodingParser.parse"); try { headerName(TokenTypes.ACCEPT_ENCODING); // empty body is fine for this header. if (lexer.lookAhead(0) == '\n') { AcceptEncoding acceptEncoding = new AcceptEncoding(); acceptEncodingList.add(acceptEncoding); } else { while (lexer.lookAhead(0) != '\n') { AcceptEncoding acceptEncoding = new AcceptEncoding(); if (lexer.lookAhead(0) != ';') { // Content-Coding: lexer.match(TokenTypes.ID); Token value = lexer.getNextToken(); acceptEncoding.setEncoding(value.getTokenValue()); } while (lexer.lookAhead(0) == ';') { this.lexer.match(';'); this.lexer.SPorHT(); this.lexer.match('q'); this.lexer.SPorHT(); this.lexer.match('='); this.lexer.SPorHT(); lexer.match(TokenTypes.ID); Token value = lexer.getNextToken(); try { float qv = Float.parseFloat(value.getTokenValue()); acceptEncoding.setQValue(qv); } catch (NumberFormatException ex) { throw createParseException(ex.getMessage()); } catch (InvalidArgumentException ex) { throw createParseException(ex.getMessage()); } this.lexer.SPorHT(); } acceptEncodingList.add(acceptEncoding); if (lexer.lookAhead(0) == ',') { this.lexer.match(','); this.lexer.SPorHT(); } } } return acceptEncodingList; } finally { if (debug) dbg_leave("AcceptEncodingParser.parse"); } }
private MaxForwardsHeader getMaxForwardsHeader() throws IOException { if (maxForwardsHeader != null) { return maxForwardsHeader; } try { maxForwardsHeader = headerFactory.createMaxForwardsHeader(MAX_FORWARDS); return maxForwardsHeader; } catch (InvalidArgumentException e) { throw new IOException( "A problem occurred while creating MaxForwardsHeader " + e.getMessage()); } }
private void sendInviteOK() { try { if (inviteTid.getState() != TransactionState.COMPLETED) { System.out.println("shootme: Dialog state before 486: " + inviteTid.getDialog().getState()); inviteTid.sendResponse(okResponse); System.out.println("shootme: Dialog state after 486: " + inviteTid.getDialog().getState()); } } catch (SipException ex) { ex.printStackTrace(); } catch (InvalidArgumentException ex) { ex.printStackTrace(); } }
/* * Begin Third-Party Call Control. */ public void initiateCall() throws IOException { try { try { busyTreatment = new TreatmentManager("busy.au", 0); } catch (IOException e) { Logger.println("Invalid busy treatment: " + e.getMessage()); } Logger.writeFile("Call " + cp + ": Begin SIP third party call"); setState(CallState.INVITED); InetSocketAddress isa = callHandler.getReceiveAddress(); if (isa == null) { throw new IOException("can't get receiver socket!"); } // send INVITE to the CallParticipant clientTransaction = sipUtil.sendInvite(cp, isa); if (clientTransaction == null) { Logger.error("Error placing call: " + cp); setState(CallState.ENDED, "Reason='Error placing call'"); throw new IOException("Error placing call: " + cp); } CallIdHeader callIdHeader = (CallIdHeader) clientTransaction.getRequest().getHeader(CallIdHeader.NAME); sipCallId = callIdHeader.getCallId(); sipServerCallback = SipServer.getSipServerCallback(); sipServerCallback.addSipListener(sipCallId, this); } catch (java.text.ParseException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } catch (InvalidArgumentException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } catch (SipException e) { Logger.println("Call " + cp + " Error placing call " + cp + ": " + e.getMessage()); setState(CallState.ENDED, "Reason='Error placing call " + cp + " " + e.getMessage() + "'"); throw new IOException("Error placing call " + cp + " " + e.getMessage()); } }
public void processRequest(RequestEvent requestEvent) { Request request = requestEvent.getRequest(); ServerTransaction serverTransactionId = requestEvent.getServerTransaction(); System.out.println( "\n\nRequest " + request.getMethod() + " received at " + sipStack.getStackName() + " with server transaction id " + serverTransactionId); if (request.getMethod().equals(Request.INVITE)) { processInvite(requestEvent, serverTransactionId); } else if (request.getMethod().equals(Request.ACK)) { processAck(requestEvent, serverTransactionId); } else if (request.getMethod().equals(Request.BYE)) { processBye(requestEvent, serverTransactionId); } else if (request.getMethod().equals(Request.CANCEL)) { processCancel(requestEvent, serverTransactionId); } else { try { serverTransactionId.sendResponse(messageFactory.createResponse(202, request)); // send one back SipProvider prov = (SipProvider) requestEvent.getSource(); Request refer = requestEvent.getDialog().createRequest("REFER"); requestEvent.getDialog().sendRequest(prov.getNewClientTransaction(refer)); } 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(); } } }
public void unregister() throws IOException { if (!isRegistered) { return; } cancelPendingRegistrations(); isRegistered = false; if (this.registerRequest == null) { Log.info("Couldn't find the initial register request"); throw new IOException("Couldn't find the initial register request"); } Request unregisterRequest = (Request) registerRequest.clone(); try { unregisterRequest.getExpires().setExpires(0); CSeqHeader cSeqHeader = (CSeqHeader) unregisterRequest.getHeader(CSeqHeader.NAME); // [issue 1] - increment registration cseq number // reported by - Roberto Tealdi <*****@*****.**> cSeqHeader.setSequenceNumber(cSeqHeader.getSequenceNumber() + 1); } catch (InvalidArgumentException e) { Log.info("Unable to set Expires Header " + e.getMessage()); return; } ClientTransaction unregisterTransaction = null; try { unregisterTransaction = sipProvider.getNewClientTransaction(unregisterRequest); } catch (TransactionUnavailableException e) { throw new IOException("Unable to create a unregister transaction " + e.getMessage()); } try { unregisterTransaction.sendRequest(); } catch (SipException e) { Log.info("Faied to send unregister request " + e.getMessage()); return; } }
private void register() throws IOException { Log.info("Registering with " + registrar); FromHeader fromHeader = getFromHeader(); Address fromAddress = fromHeader.getAddress(); // Request URI SipURI requestURI = null; try { requestURI = addressFactory.createSipURI(null, registrar); } catch (ParseException e) { throw new IOException("Bad registrar address:" + registrar + " " + e.getMessage()); } // requestURI.setPort(registrarPort); try { requestURI.setTransportParam(sipProvider.getListeningPoint().getTransport()); } catch (ParseException e) { throw new IOException( sipProvider.getListeningPoint().getTransport() + " is not a valid transport! " + e.getMessage()); } CallIdHeader callIdHeader = sipProvider.getNewCallId(); CSeqHeader cSeqHeader = null; try { cSeqHeader = headerFactory.createCSeqHeader(1, Request.REGISTER); } catch (ParseException e) { // Should never happen throw new IOException("Corrupt Sip Stack " + e.getMessage()); } catch (InvalidArgumentException e) { // Should never happen throw new IOException("The application is corrupt "); } ToHeader toHeader = null; try { String proxyWorkAround = System.getProperty("com.sun.mc.softphone.REGISTRAR_WORKAROUND"); if (proxyWorkAround != null && proxyWorkAround.toUpperCase().equals("TRUE")) { SipURI toURI = (SipURI) (requestURI.clone()); toURI.setUser(System.getProperty("user.name")); toHeader = headerFactory.createToHeader(addressFactory.createAddress(toURI), null); } else { toHeader = headerFactory.createToHeader(fromAddress, null); } } catch (ParseException e) { throw new IOException( "Could not create a To header for address:" + fromHeader.getAddress() + " " + e.getMessage()); } ArrayList viaHeaders = getLocalViaHeaders(); MaxForwardsHeader maxForwardsHeader = getMaxForwardsHeader(); Request request = null; try { request = messageFactory.createRequest( requestURI, Request.REGISTER, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader); } catch (ParseException e) { throw new IOException("Could not create the register request! " + e.getMessage()); } ExpiresHeader expHeader = null; for (int retry = 0; retry < 2; retry++) { try { expHeader = headerFactory.createExpiresHeader(expires); } catch (InvalidArgumentException e) { if (retry == 0) { continue; } throw new IOException( "Invalid registrations expiration parameter - " + expires + " " + e.getMessage()); } } request.addHeader(expHeader); ContactHeader contactHeader = getRegistrationContactHeader(); request.addHeader(contactHeader); try { SipURI routeURI = (SipURI) addressFactory.createURI("sip:" + proxyCredentials.getProxy() + ";lr"); RouteHeader routeHeader = headerFactory.createRouteHeader(addressFactory.createAddress(routeURI)); request.addHeader(routeHeader); } catch (Exception e) { Log.error("Creating registration route error ", e); } ClientTransaction regTrans = null; try { regTrans = sipProvider.getNewClientTransaction(request); } catch (TransactionUnavailableException e) { throw new IOException( "Could not create a register transaction!\n" + "Check that the Registrar address is correct! " + e.getMessage()); } try { sipCallId = callIdHeader.getCallId(); sipServerCallback.addSipListener(sipCallId, this); registerRequest = request; regTrans.sendRequest(); Log.debug("Sent register request " + registerRequest); if (expires > 0) { scheduleReRegistration(); } } catch (Exception e) { throw new IOException("Could not send out the register request! " + e.getMessage()); } this.registerRequest = request; }
/** * Process a request from a distant contact * * @param requestEvent the <tt>RequestEvent</tt> containing the newly received request. * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't * be offered to other processors registered for the same method; <tt>false</tt>, otherwise */ @Override public boolean processRequest(RequestEvent requestEvent) { synchronized (messageProcessors) { for (SipMessageProcessor listener : messageProcessors) if (!listener.processMessage(requestEvent)) return true; } // get the content String content = null; Request req = requestEvent.getRequest(); try { content = new String(req.getRawContent(), getCharset(req)); } catch (UnsupportedEncodingException ex) { if (logger.isDebugEnabled()) logger.debug("failed to convert the message charset"); content = new String(requestEvent.getRequest().getRawContent()); } // who sent this request ? FromHeader fromHeader = (FromHeader) requestEvent.getRequest().getHeader(FromHeader.NAME); if (fromHeader == null) { logger.error("received a request without a from header"); return false; } Contact from = opSetPersPresence.resolveContactID(fromHeader.getAddress().getURI().toString()); ContentTypeHeader ctheader = (ContentTypeHeader) req.getHeader(ContentTypeHeader.NAME); String ctype = null; String cencoding = null; if (ctheader == null) { ctype = DEFAULT_MIME_TYPE; } else { ctype = ctheader.getContentType() + "/" + ctheader.getContentSubType(); cencoding = ctheader.getParameter("charset"); } if (cencoding == null) cencoding = DEFAULT_MIME_ENCODING; Message newMessage = createMessage(content, ctype, cencoding, null); if (from == null) { if (logger.isDebugEnabled()) logger.debug( "received a message from an unknown contact: " + fromHeader.getAddress().getURI().toString()); // create the volatile contact from = opSetPersPresence.createVolatileContact(fromHeader.getAddress().getURI().toString()); } // answer ok try { Response ok = sipProvider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest()); SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(ok); } catch (ParseException exc) { logger.error("failed to build the response", exc); } catch (SipException exc) { logger.error("failed to send the response : " + exc.getMessage(), exc); } catch (InvalidArgumentException exc) { if (logger.isDebugEnabled()) logger.debug("Invalid argument for createResponse : " + exc.getMessage(), exc); } // fire an event MessageReceivedEvent msgReceivedEvt = new MessageReceivedEvent(newMessage, from, System.currentTimeMillis()); fireMessageEvent(msgReceivedEvt); return true; }
/** * Attach JAIN-SIP <tt>SipProvider</tt> and <tt>ListeningPoint</tt> to the stack either for clear * communications or TLS. Clear UDP and TCP <tt>ListeningPoint</tt>s are not handled separately as * the former is a fallback for the latter (depending on the size of the data transmitted). Both * <tt>ListeningPoint</tt>s must be bound to the same address and port in order for the related * <tt>SipProvider</tt> to be created. If a UDP or TCP <tt>ListeningPoint</tt> cannot bind, retry * for both on another port. * * @param preferredPort which port to try first to bind. * @param retries how many times should we try to find a free port to bind * @param secure whether to create the TLS SipProvider. or the clear UDP/TCP one. * @throws TransportNotSupportedException in case we try to create a provider for a transport not * currently supported by jain-sip * @throws InvalidArgumentException if we try binding to an illegal port (which we won't) * @throws ObjectInUseException if another <tt>SipProvider</tt> is already associated with this * <tt>ListeningPoint</tt>. * @throws TransportAlreadySupportedException if there is already a ListeningPoint associated to * this <tt>SipProvider</tt> with the same transport of the <tt>ListeningPoint</tt>. * @throws TooManyListenersException if we try to add a new <tt>SipListener</tt> with a * <tt>SipProvider</tt> when one was already registered. */ private void createProvider(int preferredPort, int retries, boolean secure) throws TransportNotSupportedException, InvalidArgumentException, ObjectInUseException, TransportAlreadySupportedException, TooManyListenersException { String context = (secure ? "TLS: " : "clear UDP/TCP: "); if (retries < 0) { // very unlikely to happen with the default 50 retries logger.error(context + "couldn't find free ports to listen on."); return; } ListeningPoint tlsLP = null; ListeningPoint udpLP = null; ListeningPoint tcpLP = null; try { if (secure) { tlsLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TLS); if (logger.isTraceEnabled()) logger.trace("TLS secure ListeningPoint has been created."); this.secureJainSipProvider = this.stack.createSipProvider(tlsLP); this.secureJainSipProvider.addSipListener(this); } else { udpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.UDP); tcpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TCP); if (logger.isTraceEnabled()) logger.trace("UDP and TCP clear ListeningPoints have " + "been created."); this.clearJainSipProvider = this.stack.createSipProvider(udpLP); this.clearJainSipProvider.addListeningPoint(tcpLP); this.clearJainSipProvider.addSipListener(this); } if (logger.isTraceEnabled()) logger.trace(context + "SipProvider has been created."); } catch (InvalidArgumentException ex) { // makes sure we didn't leave an open listener // as both UDP and TCP listener have to bind to the same port if (tlsLP != null) this.stack.deleteListeningPoint(tlsLP); if (udpLP != null) this.stack.deleteListeningPoint(udpLP); if (tcpLP != null) this.stack.deleteListeningPoint(tcpLP); // FIXME: "Address already in use" is not working // as ex.getMessage() displays in the locale language in SC // (getMessage() is always supposed to be English though) // this should be a temporary workaround // if (ex.getMessage().indexOf("Address already in use") != -1) // another software is probably using the port if (ex.getCause() instanceof java.io.IOException) { if (logger.isDebugEnabled()) logger.debug("Port " + preferredPort + " seems in use for either TCP or UDP."); // tries again on a new random port int currentlyTriedPort = NetworkUtils.getRandomPortNumber(); if (logger.isDebugEnabled()) logger.debug("Retrying bind on port " + currentlyTriedPort); this.createProvider(currentlyTriedPort, retries - 1, secure); } else throw ex; } }
/** * parse the String message * * @return SIPHeader (RetryAfter object) * @throws SIPParseException if the message does not respect the spec. */ public SIPHeader parse() throws ParseException { if (debug) dbg_enter("RetryAfterParser.parse"); RetryAfter retryAfter = new RetryAfter(); try { headerName(TokenTypes.RETRY_AFTER); // mandatory delatseconds: String value = lexer.number(); try { int ds = Integer.parseInt(value); retryAfter.setRetryAfter(ds); } catch (NumberFormatException ex) { throw createParseException(ex.getMessage()); } catch (InvalidArgumentException ex) { throw createParseException(ex.getMessage()); } this.lexer.SPorHT(); if (lexer.lookAhead(0) == '(') { String comment = this.lexer.comment(); retryAfter.setComment(comment); } this.lexer.SPorHT(); while (lexer.lookAhead(0) == ';') { this.lexer.match(';'); this.lexer.SPorHT(); lexer.match(TokenTypes.ID); Token token = lexer.getNextToken(); value = token.getTokenValue(); if (value.equalsIgnoreCase("duration")) { this.lexer.match('='); this.lexer.SPorHT(); value = lexer.number(); try { int duration = Integer.parseInt(value); retryAfter.setDuration(duration); } catch (NumberFormatException ex) { throw createParseException(ex.getMessage()); } catch (InvalidArgumentException ex) { throw createParseException(ex.getMessage()); } } else { this.lexer.SPorHT(); this.lexer.match('='); this.lexer.SPorHT(); lexer.match(TokenTypes.ID); Token secondToken = lexer.getNextToken(); String secondValue = secondToken.getTokenValue(); retryAfter.setParameter(value, secondValue); } this.lexer.SPorHT(); } } finally { if (debug) dbg_leave("RetryAfterParser.parse"); } return retryAfter; }