📄 sipclient.java
字号:
// sendQuickResponse( curmessage, new SipStatus( 481 ) );// return;// } if( fwmode ) { if (SipClient.DEBUG) System.out.println( "SipClient: Forwarding call." ); if( fwbody != null ) { sendQuickResponse( curmessage, new SipStatus( 302 ), fwbody, new MimeContentType( "text/plain" ) ); } else { sendQuickResponse( curmessage, new SipStatus( 302 ) ); } return; } if( busymode ) { if (SipClient.DEBUG) System.out.println( "SipClient: We're busy.\n" ); if( busybody != null ) { sendQuickResponse( curmessage, new SipStatus( 486 ), busybody, new MimeContentType( "text/plain" ) ); } else { sendQuickResponse( curmessage, new SipStatus( 486 ) ); } return; } if( curmessage.getMethod() == Sip.BadMethod ) { if (SipClient.DEBUG) System.out.println( "SipClient: I don't recognize that method... Returning a 501.\n" ); sendQuickResponse( curmessage, new SipStatus( 501 ) ); return; } if( curmessage.hasHeader( SipHeader.Accept ) ) { if( acceptHeaderBad( curmessage.getHeaderData( SipHeader.Accept ).toLowerCase() ) ) { // ** LOOK AT System.err.println("We got a bad Accept header!!!"); sendQuickResponse( curmessage, new SipStatus( 406 ) ); return; } } if( ( curmessage.hasHeader( SipHeader.Require ) ) && ( curmessage.getHeaderData( SipHeader.Require ) != null ) ) { if (SipClient.DEBUG) System.out.println( "SipClient: This messages says it requires " + curmessage.getHeaderData( SipHeader.Require ) + ", returning 420." ); sendQuickResponse( curmessage, new SipStatus( 420 ) ); return; } // Create a new call and pass it the message if (SipClient.DEBUG) System.out.println( "SipClient: Searching for a user" ); // Search for a user SipUser curuser = null; for ( ListIterator liu = users.listIterator(); liu.hasNext(); ) { curuser = (SipUser)liu.next(); if( touri.equals( curuser.getUri() ) ) break; } if( curuser == null ) { if( defusermode ) { curuser = defuser; if (SipClient.DEBUG) System.out.println( "SipClient: Using default user" ); } else { if (SipClient.DEBUG) System.out.println( "SipClient: No user found" ); sendQuickResponse( curmessage, new SipStatus( 404 ) ); return; } } if (SipClient.DEBUG) System.out.println( "SipClient: Creating new call for user " + curuser.getUri().nameAddr() ); SipCall newcall = new SipCall( curuser, curmessage.getHeaderData( SipHeader.Call_ID ) ); SipCallMember member = newcall.incomingMessage( curmessage ); // Signal that we have an incoming call incomingCall( newcall, member ); } /** * Check to see if what was sent in Accept: can be read here */ private boolean acceptHeaderBad( String acceptHeader ) { String [] acceptibleHeaders = { "application/sdp", "text/plain" }; for ( int i = 0; i < acceptibleHeaders.length; i++ ) { if ( acceptibleHeaders[ i ].equals( acceptHeader ) ) { return false; } } return true; } /** * sendQuickResponse */ private void sendQuickResponse( SipMessage origmessage, SipStatus status ) { sendQuickResponse( origmessage, status, null, null ); } /** * sendQuickResponse */ private void sendQuickResponse( SipMessage origmessage, SipStatus status, String body, MimeContentType bodytype ) { SipMessage msg = new SipMessage(); SipVia topvia; String sendaddr; // Use via to tell us where to send it and how topvia = origmessage.getViaList().getTopmostVia(); MessageSocket outsocket = MessageSocketFactory.getMessageSocket( topvia.getTransport() ); // if transport is bad, no use sending if ( outsocket == null ) return; msg.setType( SipMessage.Response ); msg.setStatus( status ); // Copy via list exactly msg.setViaList( origmessage.getViaList() ); msg.insertHeader( SipHeader.From, origmessage.getHeaderData( SipHeader.From ) ); msg.insertHeader( SipHeader.To, origmessage.getHeaderData( SipHeader.To ) ); msg.insertHeader( SipHeader.CSeq, origmessage.getHeaderData( SipHeader.CSeq ) ); msg.insertHeader( SipHeader.Call_ID, origmessage.getHeaderData( SipHeader.Call_ID ) ); if ( origmessage.hasHeader( SipHeader.Require ) ) { msg.insertHeader( SipHeader.Unsupported, origmessage.getHeaderData( SipHeader.Require ) ); } if ( ( status.getCode() >= 300 ) && ( status.getCode() < 400 ) ) { msg.getContactList().addToHead( forwarduri ); } if ( bodytype != null ) { msg.insertHeader( SipHeader.Content_Type, bodytype.type() ); } msg.setBody( body ); // Calculate content length msg.insertHeader( SipHeader.Content_Length, String.valueOf( msg.getContentLength() ) ); // Advertise shamelessly msg.insertHeader( SipHeader.User_Agent, getUserAgent() ); // maddr, received, sentby if ( topvia.hasMaddrParam() ) { if (SipClient.DEBUG) System.out.println("SipClient: Using address from maddr via parameter"); sendaddr = topvia.getMaddrParam(); } else if( topvia.hasReceivedParam() ) { if (SipClient.DEBUG) System.out.println("SipClient: Using address from received via parameter"); sendaddr = topvia.getReceivedParam(); } else { sendaddr = topvia.getHostname(); } // See if we need to use a proxy ProxyLookup pl = ProxyServer.getProxyLookup(); SipUri sendtoUri = null; try { sendtoUri = pl.getConnectionUri( sendaddr ); } catch ( SipProxyException spe ) { System.err.println( "Unable to send message because: " + spe.toString() ); return; } // Announce where we're sending if (SipClient.DEBUG) System.out.println("SipClient: Really ending to " + sendtoUri.getHostname() + " port " + topvia.getPortNumber() ); outsocket.connect( sendtoUri.getHostname(), topvia.getPortNumber() ); // Announce that we're sending if (SipClient.DEBUG) System.out.println("SipClient: Sending:"); if (SipClient.DEBUG) System.out.println(msg.message()); String logmessage; logmessage = ": Sending message:\n" + msg.message() + "\n: End sent message"; loggerfd.println(logmessage); // Send it outsocket.send( msg.message(), msg.message().length() ); // outsocket.close(); } /** * addUser * @param user */ void addUser(SipUser user) { if( !users.contains( user ) ) { users.add( user ); } } /** * deleteUser * @param user */ private void deleteUser(SipUser user) { users.remove( Collections.binarySearch(users, user) ); } /** * addCall * @param call */ void addCall(SipCall call) { if( !calls.contains( call ) ) { calls.add( call ); } callListUpdated(); } /** * deleteCall * @param call */ private void deleteCall(SipCall call) { calls.remove( call ); callListUpdated(); } /** * sendRequest * @param msg * @param contact */ void sendRequest(SipMessage msg, boolean contact) { // Create a Via tag and add it to the message at the top of the list SipVia regvia = new SipVia(); // *** LOOK AT **** - pass in param? regvia.setTransport( SipVia.UDP ); regvia.setHostname( Sip.getLocalAddress() ); regvia.setPortNumber( listener.getPortNumber() ); msg.getViaList().insertTopmostVia( regvia ); // Calculate content length msg.insertHeader( SipHeader.Content_Length, String.valueOf( msg.getContentLength() ) ); // Advertise shamelessly msg.insertHeader( SipHeader.User_Agent, getUserAgent() ); // Indicate what we accept for methods that solicit responses if( msg.getMethod() != Sip.ACK && msg.getMethod() != Sip.CANCEL ) { // msg.insertHeader( SipHeader.Accept, "application/sdp, text/plain" ); msg.insertHeader( SipHeader.Accept, "text/plain" ); } // Set max-forwards if( maxforwards != 0 ) { msg.insertHeader( SipHeader.Max_Forwards, String.valueOf( maxforwards ) ); } // Via hiding mode if( hidemode != DontHideVia ) { if( hidemode == HideHop ) msg.insertHeader( SipHeader.Hide, "hop" ); else msg.insertHeader( SipHeader.Hide, "route" ); } // If this request requires the contact header, add it if( contact ) { msg.getContactList().addToHead( contacturi ); } // Retransmission timestamp msg.setTimestamp(); msg.setTimeTick( 500 ); // T1 msg.incrSendCount(); // Announce that we're sending a message // Send the message MessageSocket sendsocket = MessageSocketFactory.getMessageSocket( MessageSocket.UDP ); // Choose destination int connResult = 0; if( useproxy ) { connResult = sendsocket.connect( proxy, proxyport ); } else { // send to the host/port in the request uri String sendtoaddr; if( msg.getRequestUri().hasMaddrParam() ) { sendtoaddr = msg.getRequestUri().getMaddrParam(); } else { sendtoaddr = msg.getRequestUri().getHostname(); } // 10/15/01 Check sendtoaddr to see if it's really a host, or if we send // to a proxy ProxyLookup pl = null; if ( ( pl = ProxyServer.getProxyLookup() ) == null ) return; SipUri sendtoUri = null; try { sendtoUri = pl.getConnectionUri( sendtoaddr ); } catch ( SipProxyException spe ) { System.err.println( "Unable to send message because " + spe.toString() ); return; } InetAddress sendInetAddress = null; try { sendInetAddress = InetAddress.getByName( sendtoUri.getHostname() ); } catch ( UnknownHostException uhe ) { System.err.println( "Unable to connect to host: " + sendtoUri.getHostname() + " because " + uhe ); return; } if ( sendInetAddress.isMulticastAddress() ) { // make sendsocket a multicast socket sendsocket = MessageSocketFactory.getMessageSocket( MessageSocket.MCAST ); } if (SipClient.DEBUG) System.out.println( "SipClient: Really sending to " + sendtoUri.getHostname() + ":" + msg.getRequestUri().getPortNumber() ); connResult = sendsocket.connect( sendInetAddress, msg.getRequestUri().getPortNumber() ); } if ( connResult < 0 ) { if (SipClient.DEBUG) System.err.println( "Error connecting to remote host" ); } else { if (SipClient.DEBUG) System.out.println( "SipClient: Sending:\n" + msg.message() ); sendsocket.send( msg.message(), msg.message().length() ); } } /** * sendResponse * @param msg * @param contact */ public void sendResponse(SipMessage msg, boolean contact) { SipVia topvia; String sendaddr; // Use via to tell us where to send it and how topvia = msg.getViaList().getTopmostVia(); MessageSocket outsocket = MessageSocketFactory.getMessageSocket( topvia.getTransport() ); // If transport was bad, no use sending if( outsocket == null ) return; // Calculate content length msg.insertHeader( SipHeader.Content_Length, String.valueOf( msg.getContentLength() ) ); // Advertise shamelessly msg.insertHeader( SipHeader.User_Agent, getUserAgent() ); // If this request requires the contact header, add it if( contact ) { msg.getContactList().addToHead( contacturi ); } // Indicate what methods we allow if this is an answer to an OPTIONS if( msg.getHeaderData( SipHeader.CSeq ).indexOf( "OPTIONS" ) > 0 ) { msg.insertHeader( SipHeader.Allow, "INVITE, OPTIONS, ACK, BYE, MESSAGE, CANCEL" ); } // maddr, received, sentby if( topvia.hasMaddrParam() ) { if (SipClient.DEBUG) System.out.println( "SipClient: Using address from maddr via parameter\n" ); sendaddr = topvia.getMaddrParam(); } else if( topvia.hasReceivedParam() ) { if (SipClient.DEBUG) System.out.println( "SipClient: Using address from received via parameter\n" ); sendaddr = topvia.getReceivedParam(); } else { sendaddr = topvia.getHostname(); } // Figure out if this sendtoaddr should really go to a proxy ProxyLookup pl = null; if ( ( pl = ProxyServer.getProxyLookup() ) == null ) return; SipUri sendtoUri = null; try { sendtoUri = pl.getConnectionUri( sendaddr ); } catch ( SipProxyException spe ) { System.err.println( "Unable to send message because: " + spe.toString() ); return; } // Announce where we're sending if (SipClient.DEBUG) System.out.println( "SipClient: Really sending to " + sendaddr + " port " + topvia.getPortNumber() ); if( !outsocket.setHostname( sendtoUri.getHostname() ) ) { return; } if ( outsocket.connect( sendtoUri.getHostname(), topvia.getPortNumber() ) < 0 ) return; // Announce what we're sending if (SipClient.DEBUG) System.out.println( "SipClient: Sending:\n" + msg.message() ); String logmessage; logmessage = ": Sending message:\n" + msg.message() + "\n: End sent message"; loggerfd.println(logmessage); // Send it outsocket.send( msg.message(), msg.message().length() ); // outsocket.close(); } /** * sendRaw * @param msg */ void sendRaw(SipMessage msg) { // Announce that we're sending if (SipClient.DEBUG) System.out.println( "SipClient: Sending:" ); if (SipClient.DEBUG) System.out.println( msg.message() ); String logmessage; logmessage = ": Sending message:\n" + msg.message() + "\n: End sent message"; loggerfd.println(logmessage); // UDPMessageSocket sendsocket; MessageSocket sendsocket = MessageSocketFactory.getMessageSocket( SipVia.UDP ); // Choose destination if( ( msg.getType() != SipMessage.Response ) && useproxy ) { if( !sendsocket.setHostname( proxy ) ) { return; } sendsocket.connect( (String)null, proxyport ); } else { // Send to whatever is in the request uri String sendtoaddr; if( msg.getRequestUri().hasMaddrParam() ) { sendtoaddr = msg.getRequestUri().getMaddrParam(); } else { sendtoaddr = msg.getRequestUri().getHostname(); } // See if we need to use proxy ProxyLookup pl = ProxyServer.getProxyLookup(); SipUri sendtoUri = null; try { sendtoUri = pl.getConnectionUri( sendtoaddr ); } catch ( SipProxyException spe ) { System.err.println( "Unable to send message because: " + spe.toString() ); return; } sendsocket.connect( sendtoUri.getHostname(), msg.getRequestUri().getPortNumber() ); } sendsocket.send( msg.message(), msg.message().length() ); } /** * callTypeUpdated */ void callTypeUpdated() { callListUpdated(); } /** * Check if incoming messages are available */ boolean haveMessages() { return mQueue.hasMessage(); } /** * Test method * */ public static void main (String[] args) { // try initializing ... replaced this with SipTester SipUri uri = new SipUri( "sip:srjones@mitre.org" ); SipClient client = SipClient.getSipClient(); SipUser user = null; client.setDefaultUserMode( true ); client.setExplicitProxyMode( false ); String laddr = Sip.getLocalAddress(); client.setCallForward( false ); client.setCallForwardUri( null ); client.setCallForwardMessage( null ); client.setMaxForwards( 0 ); client.setBusyMessage( null ); client.setHideViaMode( SipClient.HideHop ); user = new SipUser( client, uri ); if ( DEBUG ) System.out.println( "SipClient: initialization complete" ); // create a REGISTER message to a server String serverurl = "sip:129.83.9.75"; SipRegister reg = user.addServer( serverurl ); reg.requestRegister( "Steve", "nopass" ); for (;;) {} //System.exit(0); } // class variables public static final int DontHideVia = 0; public static final int HideHop = 1; public static final int HideRoute = 2; static final boolean DEBUG = true; private ArrayList users = new ArrayList(); private ArrayList calls = new ArrayList(); private String proxy; private int proxyport; private boolean useproxy; private SipUri contacturi; private boolean fwmode; private SipUri forwarduri; private String fwbody; private boolean busymode; private String busybody; private int maxforwards; private int hidemode; private boolean defusermode; private SipUser defuser; private MessageSocket listener; private PrintWriter loggerfd; private MessageQueue mQueue; // for handling call events private CallListener callListener = null; protected static SipClient _instance = null;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -