001/** 002 * 003 * Copyright 2003-2005 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingle.nat; 018 019import org.jivesoftware.smack.SmackException.NotConnectedException; 020import org.jivesoftware.smack.XMPPException; 021import org.jivesoftware.smackx.jingle.JingleSession; 022 023import java.net.InetAddress; 024import java.net.NetworkInterface; 025import java.net.SocketException; 026import java.util.Enumeration; 027 028/** 029 * Basic Resolver takes all IP addresses of the interfaces and uses the 030 * first non-loopback address. 031 * A very simple and easy to use resolver. 032 */ 033public class BasicResolver extends TransportResolver { 034 035 /** 036 * Constructor. 037 */ 038 public BasicResolver() { 039 super(); 040 } 041 042 /** 043 * Resolve the IP address. 044 * <p/> 045 * The BasicResolver takes the IP addresses of the interfaces and uses the 046 * first non-loopback, non-linklocal and non-sitelocal address. 047 * @throws NotConnectedException 048 */ 049 public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException { 050 051 setResolveInit(); 052 053 clearCandidates(); 054 055 Enumeration<NetworkInterface> ifaces = null; 056 057 try { 058 ifaces = NetworkInterface.getNetworkInterfaces(); 059 } catch (SocketException e) { 060 e.printStackTrace(); 061 } 062 063 while (ifaces.hasMoreElements()) { 064 065 NetworkInterface iface = ifaces.nextElement(); 066 Enumeration<InetAddress> iaddresses = iface.getInetAddresses(); 067 068 while (iaddresses.hasMoreElements()) { 069 InetAddress iaddress = iaddresses.nextElement(); 070 if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) { 071 TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort()); 072 tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName()); 073 addCandidate(tr); 074 setResolveEnd(); 075 return; 076 } 077 } 078 } 079 080 try { 081 ifaces = NetworkInterface.getNetworkInterfaces(); 082 } catch (SocketException e) { 083 e.printStackTrace(); 084 } 085 086 while (ifaces.hasMoreElements()) { 087 088 NetworkInterface iface = ifaces.nextElement(); 089 Enumeration<InetAddress> iaddresses = iface.getInetAddresses(); 090 091 while (iaddresses.hasMoreElements()) { 092 InetAddress iaddress = iaddresses.nextElement(); 093 if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) { 094 TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort()); 095 tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName()); 096 addCandidate(tr); 097 setResolveEnd(); 098 return; 099 } 100 } 101 } 102 103 try { 104 TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort()); 105 tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName()); 106 addCandidate(tr); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 setResolveEnd(); 111 112 } 113 114 public void initialize() throws XMPPException { 115 setInitialized(); 116 } 117 118 public void cancel() throws XMPPException { 119 // Nothing to do here 120 } 121}