?? sip.java
字號:
/* * This file was derived from libdissipate, an open source SIP library. The original file * was modified on 1/23/2001. Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * This class is a container for items of global interest to a Sip stack. It * also holds a string containing the best-known local address to be placed in * a Via or Contact header. * * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com * * @author S.R.Jones * @version 1 */package org.mitre.jsip;import org.mitre.jsip.util.Base64;import java.security.MessageDigest;public class Sip{ /** * Sip */ public Sip() { } /** * getMethodString * @param m * @return String */ public static String getMethodString(int m) { switch ( m ) { case INVITE: return "INVITE"; case ACK: return "ACK"; case BYE: return "BYE"; case OPTIONS: return "OPTIONS"; case CANCEL: return "CANCEL"; case REGISTER: return "REGISTER"; case MESSAGE: return "MESSAGE"; case SUBSCRIBE: return "SUBSCRIBE"; case NOTIFY: return "NOTIFY"; case INFO: return "INFO"; case BadMethod: return "BAD"; }; return null; } /** * matchMethod * @param m * @return Method */ public static int matchMethod(String m) { if ( m.compareTo( getMethodString( INVITE ) ) == 0 ) { return INVITE; } if ( m.compareTo( getMethodString( ACK ) ) == 0 ) { return ACK; } if ( m.compareTo( getMethodString( BYE ) ) == 0 ) { return BYE; } if ( m.compareTo( getMethodString( OPTIONS ) ) == 0 ) { return OPTIONS; } if ( m.compareTo( getMethodString( CANCEL ) ) == 0 ) { return CANCEL; } if ( m.compareTo( getMethodString( REGISTER ) ) == 0 ) { return REGISTER; } if ( m.compareTo( getMethodString( MESSAGE ) ) == 0 ) { return MESSAGE; } if ( m.compareTo( getMethodString( SUBSCRIBE ) ) == 0 ) { return SUBSCRIBE; } if ( m.compareTo( getMethodString( NOTIFY ) ) == 0 ) { return NOTIFY; } if ( m.compareTo( getMethodString( INFO ) ) == 0 ) { return INFO; } if ( m.compareTo( getMethodString( MESSAGE ) ) == 0 ) { return MESSAGE; } return BadMethod; } /** * getLocalAddress * @return String */ public static String getLocalAddress() { if( dissipate_ouraddress == null ) { // see if it's set dissipate_ouraddress = System.getProperty("sip.dissipate.address", SipUtil.getLocalFqdn()); } return dissipate_ouraddress; } /** * Get the port we will be listening to * * @return a sip port number */ public static int getLocalPort() { if( dissipate_ourport == 0 ) { // check to see if it's set as a system property, else set to default String portnum = System.getProperty("sip.dissipate.port", "5060"); try { dissipate_ourport = Integer.parseInt(portnum); } catch (NumberFormatException nfe) { System.err.println("Error setting port to desired value: " + portnum + ". Going with default value (5060)"); dissipate_ourport = 5060; } } return dissipate_ourport; } /** * setLocalAddress * @param localaddr */ public static void setLocalAddress(String localaddr) { dissipate_ouraddress = localaddr; } /** * getDigestResponse * @param user * @param password * @param method * @param requri * @param authstr * @return String */ public static String getDigestResponse(String user, String password, String method, String requri, String authstr) { String realm = ""; String nonce = ""; String opaque = ""; String algorithm = ""; String qop = ""; String digest; String cnonce; String noncecount; // static final char p = authstr.latin1(); String pAuthStr = authstr; int ptr = 0; // HASHHEX HA1; // HASHHEX HA2 = ""; // HASHHEX response; String response = ""; int i = 0; while( ptr < pAuthStr.length() ) { while( ( pAuthStr.charAt(ptr) == ' ' ) || ( pAuthStr.charAt(ptr) == ',' ) || ( pAuthStr.charAt(ptr) == '\t') ) ptr++; i = 0; if( pAuthStr.substring( ptr, ptr+7 ).equals("realm=\"") ) { ptr += 7; i = pAuthStr.indexOf('\"', ptr); realm = pAuthStr.substring(ptr, i - 1 ); } else if ( pAuthStr.substring( ptr, ptr+7).equals("nonce=\"") ) { ptr += 7; i = pAuthStr.indexOf('\"', ptr); nonce = pAuthStr.substring(ptr, i - 1); } else if ( pAuthStr.substring( ptr, ptr+8).equals("opaque=\"") ) { ptr += 8; i = pAuthStr.indexOf('\"', ptr); opaque = pAuthStr.substring(ptr, i - 1); } else if ( pAuthStr.substring( ptr, ptr+10).equals("algorith=\"") ) { ptr += 10; i = pAuthStr.indexOf('\"', ptr); algorithm = pAuthStr.substring( ptr, i - 1 ); } else if ( pAuthStr.substring( ptr, ptr+11).equals("algorithm=\"") ) { ptr += 11; i = pAuthStr.indexOf('\"', ptr); algorithm = pAuthStr.substring( ptr, i - 1 ); } else if ( pAuthStr.substring( ptr, ptr+5).equals("qop=\"") ) { ptr += 5; i = pAuthStr.indexOf('\"', ptr); qop = pAuthStr.substring( ptr, i - 1 ); } if( i >= 0 ) ptr += i; else ptr++; } digest = "Digest username=\""; digest += user; digest += "\", "; digest += "realm=\""; digest += realm; digest += "\", "; digest += "nonce=\""; digest += nonce; digest += "\", "; digest += "uri=\""; digest += requri; digest += "\", "; cnonce = "abcdefghi"; noncecount = "00000001"; // *** LOOK AT **** need to calculate response //DigestCalcHA1( "MD5", user.latin1(), realm.latin1(), password.latin1(), // nonce.latin1(), cnonce.latin1(), HA1 ); //DigestCalcResponse( HA1, nonce.latin1(), noncecount.latin1(), // cnonce.latin1(), qop.latin1(), method.latin1(), requri.latin1(), HA2, response ); String toDigest = user + ":" + realm + ":" + password; byte[] digestbuffer = null; try { MessageDigest md = MessageDigest.getInstance( "MD5" ); md.update( toDigest.getBytes() ); digestbuffer = md.digest(); } catch ( Exception e ) { System.err.println( "Error creating digest request: " + e ); return null; } digest += "qop=\"auth\", "; digest += "cnonce=\""; digest += cnonce; digest += "\", "; digest += "nc="; digest += noncecount; digest += ", "; digest += "response=\""; digest += response; digest += "\""; if( opaque != null ) { digest += ", opaque=\""; digest += opaque; digest += "\""; } System.out.println( "SipProtocol: Digest calculated." ); return digest; } /** * getBasicResponse * @param user * @param password * @return String */ public static String getBasicResponse(String user, String password) { String basic; String userpass = ""; basic = "Basic "; userpass += user; userpass += ":"; userpass += password; basic += Base64.encode( userpass ); return basic; } private static String dissipate_ouraddress ; private static int dissipate_ourport; // class variables public static final int INVITE = 0; public static final int ACK = 1; public static final int BYE = 2; public static final int OPTIONS = 3; public static final int CANCEL = 4; public static final int REGISTER = 5; public static final int MESSAGE = 6; public static final int SUBSCRIBE = 7; public static final int NOTIFY = 8; public static final int INFO = 9; public static final int BadMethod = 10; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -