/*
 * Copyright (c) 2009, syuu
 * Licensed under the NicoCache License.
 */

package com.dokukino.genkidama;
/*
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Enumeration;

import net.sbbi.upnp.impls.InternetGatewayDevice;

import orca.cod.Util;
*/
public class LocalAddressLocator {
/*
	InternetGatewayDevice igd;
	private InetAddress netmask;

	public LocalAddressLocator(InternetGatewayDevice igd, InetAddress netmask) {
		this.igd = igd;
		this.netmask = netmask;
	}

	public LocalAddressLocator(InternetGatewayDevice igd, String netmask) throws UnknownHostException {
		this(igd, InetAddress.getByName(netmask));
	}

	public InetAddress locate() throws Exception {
		InetAddress addr = InetAddress.getLocalHost();
		if(isDefaultRouteAddr(addr, netmask))
			return addr;
		Enumeration<NetworkInterface> netIfEnum = NetworkInterface.getNetworkInterfaces();
		while(netIfEnum.hasMoreElements()) {
			NetworkInterface netIf = netIfEnum.nextElement();
			Enumeration<InetAddress> ifAddrEnum = netIf.getInetAddresses();
			while(ifAddrEnum.hasMoreElements()) {
				InetAddress ifAddr = ifAddrEnum.nextElement();
				if (isDefaultRouteAddr(ifAddr, netmask))
					return ifAddr;
			}
		}
		throw new UnableToLocateLocalAddressException();
	}

	private boolean isDefaultRouteAddr(InetAddress ifAddr, InetAddress mask) throws Exception {
		URL url = igd.getIGDRootDevice().getPresentationURL();
		InetAddress routerAddr = InetAddress.getByName(url.getHost());
		return Util.sameSubnet(routerAddr, ifAddr, mask);
	}
*/
}
