changeset 103:0c0da17b72b2

add IpV6.java
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 25 Oct 2011 01:22:47 +0900
parents accf5a6e1176
children 5241bf573f69
files src/myVncClient/IpV6.java
diffstat 1 files changed, 78 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myVncClient/IpV6.java	Tue Oct 25 01:22:47 2011 +0900
@@ -0,0 +1,78 @@
+package myVncClient;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.net.Inet6Address;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class IpV6 {
+	private HashMap<NetworkInterface, ArrayList<InetAddress>> interfaceMap;
+
+	public IpV6() {
+		interfaceMap = new HashMap<NetworkInterface, ArrayList<InetAddress>>();
+	}
+
+	public void getInterface() {
+		interfaceMap.clear();
+		try {
+			Enumeration<NetworkInterface> iList = NetworkInterface
+					.getNetworkInterfaces();
+			if (iList == null) {
+				System.out.println("notfound interface");
+			} else {
+				while (iList.hasMoreElements()) {
+					NetworkInterface iface = iList.nextElement();
+					Enumeration<InetAddress> addrList = iface
+							.getInetAddresses();
+					if (!addrList.hasMoreElements())
+						continue;
+					ArrayList<InetAddress> iaddress = new ArrayList<InetAddress>();
+					while (addrList.hasMoreElements())
+						iaddress.add(addrList.nextElement());
+					interfaceMap.put(iface, iaddress);
+				}
+			}
+		} catch (SocketException se) {
+			System.out.println("Error getting network interfaces: "
+					+ se.getMessage());
+		}
+	}
+
+	public boolean match(String name) {
+		Pattern pattern = Pattern.compile("lo.*");
+		Matcher matcher = pattern.matcher(name);
+		return matcher.matches();
+	}
+
+	public String getV6() {
+		boolean flag = false;
+		String name = null;
+		for (NetworkInterface n : interfaceMap.keySet()) {
+			if (!(match(n.getName()))) {
+				for (InetAddress a : interfaceMap.get(n)) {
+					if (a instanceof Inet6Address && !(flag)) {
+						System.out.println(a.getHostAddress());
+						flag = true;
+						name = a.getHostAddress();
+					}
+				}
+			}
+		}
+		return name;
+	}
+
+	public HashMap<NetworkInterface, ArrayList<InetAddress>> getInetfaceMap() {
+		return interfaceMap;
+	}
+
+	public void setInterfaceMap(
+			HashMap<NetworkInterface, ArrayList<InetAddress>> interfaceMap) {
+		this.interfaceMap = interfaceMap;
+	}
+
+}