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.jingleold.nat;
018
019import org.jivesoftware.smack.SmackException.NotConnectedException;
020import org.jivesoftware.smack.XMPPException;
021import org.jivesoftware.smackx.jingleold.JingleSession;
022
023import java.net.InetAddress;
024import java.net.NetworkInterface;
025import java.net.SocketException;
026import java.util.Enumeration;
027import java.util.logging.Level;
028import java.util.logging.Logger;
029
030/**
031 * Basic Resolver takes all IP addresses of the interfaces and uses the
032 * first non-loopback address.
033 * A very simple and easy to use resolver.
034 */
035public class BasicResolver extends TransportResolver {
036    private static final Logger LOGGER = Logger.getLogger(BasicResolver.class.getName());
037
038    /**
039     * Constructor.
040     */
041    public BasicResolver() {
042        super();
043    }
044
045    /**
046     * Resolve the IP address.
047     * <p/>
048     * The BasicResolver takes the IP addresses of the interfaces and uses the
049     * first non-loopback, non-linklocal and non-sitelocal address.
050     * @throws NotConnectedException 
051     * @throws InterruptedException 
052     */
053    @Override
054    public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
055
056        setResolveInit();
057
058        clearCandidates();
059
060        Enumeration<NetworkInterface> ifaces = null;
061
062        try {
063            ifaces = NetworkInterface.getNetworkInterfaces();
064        } catch (SocketException e) {
065            LOGGER.log(Level.WARNING, "exception", e);
066        }
067
068        while (ifaces.hasMoreElements()) {
069
070            NetworkInterface iface = ifaces.nextElement();
071            Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
072
073            while (iaddresses.hasMoreElements()) {
074                InetAddress iaddress = iaddresses.nextElement();
075                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
076                    TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
077                    tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
078                    addCandidate(tr);
079                    setResolveEnd();
080                    return;
081                }
082            }
083        }
084
085        try {
086            ifaces = NetworkInterface.getNetworkInterfaces();
087        } catch (SocketException e) {
088            LOGGER.log(Level.WARNING, "exception", e);
089        }
090
091        while (ifaces.hasMoreElements()) {
092
093            NetworkInterface iface = ifaces.nextElement();
094            Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
095
096            while (iaddresses.hasMoreElements()) {
097                InetAddress iaddress = iaddresses.nextElement();
098                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
099                    TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
100                    tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
101                    addCandidate(tr);
102                    setResolveEnd();
103                    return;
104                }
105            }
106        }
107
108        try {
109            TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort());
110            tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName());
111            addCandidate(tr);
112        } catch (Exception e) {
113            LOGGER.log(Level.WARNING, "exception", e);
114        }
115        setResolveEnd();
116
117    }
118
119    @Override
120    public void initialize() throws XMPPException {
121        setInitialized();
122    }
123
124    @Override
125    public void cancel() throws XMPPException {
126        // Nothing to do here
127    }
128}