Mercurial > hg > Members > riono > TreeVNC_ja_comment
changeset 208:f1d7cb4a1868
compute netmask and netaddress
author | oc |
---|---|
date | Wed, 02 Jul 2014 18:52:29 +0900 |
parents | b31903e5b02d |
children | 7b106b4a0c9f |
files | src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java |
diffstat | 2 files changed, 60 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java Wed Jul 02 18:14:20 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java Wed Jul 02 18:52:29 2014 +0900 @@ -2,6 +2,7 @@ import java.io.IOException; +import java.net.InetAddress; import java.util.LinkedList; public class TreeManagement { @@ -10,6 +11,8 @@ boolean runflag = false; private final int treebranch = 2; private boolean showTreeNode = false; + private byte[] netmask; + private byte[] netaddress; public TreeManagement(String hostName, int vncport, boolean showTree) { TreeVNCNode me = new TreeVNCNode(hostName, vncport,"localhost"); @@ -193,4 +196,18 @@ return treebranch; } + public void setNetMask(byte[] netmask,byte[] netaddress) { + this.netmask = netmask; + this.netaddress = netaddress; + } + + public boolean onTheSameNetwork(InetAddress adr ) { + byte [] byteadr = adr.getAddress(); + if (byteadr.length != netmask.length) return false; + for(int i=0; i < netmask.length; i++) { + if ((netmask[i] & byteadr[i])!=netaddress[i]) return false; + } + return true; + } + } \ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java Wed Jul 02 18:14:20 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java Wed Jul 02 18:52:29 2014 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.treevnc; import java.net.InetAddress; +import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketException; @@ -23,20 +24,49 @@ } public void getNetworkInterfaces() throws SocketException { - for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { - NetworkInterface ni; - ni = e.nextElement(); - if (ni.isUp() && ni.supportsMulticast() && !ni.isLoopback()) { - String hostName = ni.getInetAddresses().nextElement().getHostAddress(); - TreeManagement treeManager = new TreeManagement(hostName, ConnectionParams.DEFAULT_VNC_ROOT, myRfb.getViewer().getShowTree()); - treeManager.getList().getFirst().setPort(myRfb.getAcceptPort()); - addNetworkInterface(ni, treeManager); - System.out.println("Interfaces :" + ni.getName()); - } + for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { + NetworkInterface ni = e.nextElement(); + if (ni.isUp() && ni.supportsMulticast() && !ni.isLoopback()) { + for (InterfaceAddress ipaddress : ni.getInterfaceAddresses()) { + byte [] netmask = getNetMask(ipaddress); + String hostName = ipaddress.getAddress().getHostName(); + TreeManagement treeManager = new TreeManagement(hostName, ConnectionParams.DEFAULT_VNC_ROOT, myRfb.getViewer().getShowTree()); + treeManager.getList().getFirst().setPort(myRfb.getAcceptPort()); + byte[] netaddr = ipaddress.getAddress().getAddress(); + for(int i=0;i<netaddr.length;i++) { + netaddr[i] &= netmask[i]; + } + treeManager.setNetMask(netmask,netaddr); + addNetworkInterface(ni, treeManager); + System.out.println("Interfaces :" + ni.getName()); + } + } } } - NetworkInterface getInterface(Socket vncSocket) { + private byte[] getNetMask(InterfaceAddress ipaddress) { + short prefix = ipaddress.getNetworkPrefixLength(); + byte[] netmask = ipaddress.getAddress().getAddress(); + for(int i = 0; i < netmask.length; i++) { + int bitpos = i * 8; + if (bitpos > prefix) { + netmask[i] = 0; + } else if (bitpos+8 > prefix) { + // ex. bitpos = 0, prefix = 2 + // bitpos+8-prefix + // 1100 0000 + int bytemask = 0x80; + for(int j = 1; j < bitpos+8-prefix; j++) { + bytemask >>= 1; + bytemask |= 0x80; + } + netmask[i] = (byte)bytemask; + } + } + return netmask ; + } + + NetworkInterface getInterface(Socket vncSocket) { InetAddress address = vncSocket.getInetAddress(); try { return NetworkInterface.getByInetAddress(address); @@ -57,4 +87,6 @@ return interfaces.get(intf); } + + }