⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dnsproxylookup.java

📁 Java SIP
💻 JAVA
字号:
/* * Copyright (c) 2001 The MITRE Corporation <srjones@mitre.org> * * * 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. * *//** * Class to provide a means to lookup DNS SRV records * */package org.mitre.jsip.proxy;import java.util.*;import javax.naming.*;import javax.naming.directory.*;import org.mitre.jsip.*;public class DNSProxyLookup implements ProxyLookup {    private String domainName;    private String dnsServer = "linus.mitre.org";    private Hashtable uriCache = new Hashtable();    DNSProxyLookup( String domainName, String dnsServer ) {	this.domainName = domainName;	this.dnsServer = dnsServer;    }    public SipUri getProxyUri( ) throws SipProxyException {	if ( uriCache.contains( domainName ) ) {	    return (SipUri) uriCache.get( domainName );	}	Hashtable env = new Hashtable();	env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsFactoryContext" );	env.put( "java.naming.provider.url", "dns://" + dnsServer + "/" + domainName );	String proxyName = null;	try {	    DirContext ictx = new InitialDirContext( env );	    	    String sipName = "_sip." + domainName;	    Attributes attr = ictx.getAttributes( sipName, new String [] { "SRV" } ); 		    //	String proxyName = attr.get( "A" ); // ??????? should be a hostname	    proxyName = "stars.mitre.org";	} catch ( NamingException ne ) {	    throw new SipProxyException( ne.toString() );	}	SipUri returnUri = new SipUri( "sip:" + proxyName );	uriCache.put( domainName, returnUri );	return returnUri;    }    public SipUri getConnectionUri( String host ) throws SipProxyException {	// check the cache first to see if we've already done this lookup	if ( uriCache.contains( host ) ) {	    return (SipUri) uriCache.get( host );	}	// now check DNS. If this host has an SOA record, treat it as a domain.	// if not, treat it as a host 	Attributes attrs = null;	DirContext ictx = null;	try {	    ictx = getDirContext( host );	    attrs = ictx.getAttributes( "", new String [] {"SOA"} );	} catch ( NamingException nex ) {	    throw new SipProxyException( nex.toString() );	}	String sipHost = null;	if ( attrs.size() > 0 ) {	    // this appears to be a domain. Try to contact its SIP proxy	    String sipName = "_sip";	    try {		Attributes attr = ictx.getAttributes( sipName, new String[]{ "SRV" } ); 				if ( attr.size() == 0 ) { // no proxy server found		    throw new SipProxyException( "No SIP proxy found in DNS tables" );		}				Attribute at = attr.get( "SRV" );		sipHost = (String) at.get();	    } catch ( NamingException ne ) {		throw new SipProxyException( ne.toString() );	    }	} else {	    // believe this is a host. Pass uri back as such	    sipHost = host;	}	SipUri returnUri = new SipUri( sipHost );	uriCache.put( host, returnUri );	return returnUri;    }	    protected void testDNSLookup(String hostname) throws NamingException     {	this.domainName = domainName;	Hashtable env = new Hashtable();	env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" );	env.put( "java.naming.provider.url", "dns://" + dnsServer + "/" + domainName );	DirContext ictx = new InitialDirContext( env );		Attributes attr = ictx.getAttributes( hostname, new String [] {"SOA"} );	//	Attributes attr = ictx.getAttributes( hostname );	NamingEnumeration ne = attr.getAll();		for ( ; ne.hasMore(); ) {	    Attribute at = (Attribute)ne.next();	    System.out.print( "Found attribute: " + at.getID() + " with value of " );	    String atVal = (String)at.get();	    System.out.println( atVal );	}    }    private DirContext getDirContext( String dname ) throws NamingException {	Hashtable env = new Hashtable();	env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" );	env.put( "java.naming.provider.url", "dns://" + dnsServer + "/" + dname );	return new InitialDirContext( env );    }    public static void main( String [] argc ) {	DNSProxyLookup dpl = new DNSProxyLookup( argc[0], "linus.mitre.org" );	// just test	try {	    dpl.testDNSLookup( argc[1] );	} catch ( NamingException nex ) {	    System.err.println( "Got a problem: " + nex );	}	System.exit(0);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -