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