Understanding JDK 1.6 on Ubuntu Linux IPv6 output handling -
i'm running ubuntu 10.10 , hosts configuration following:
root@maxim-desktop:~# cat /etc/hosts 192.168.20.20 maxim-desktop # added networkmanager 192.168.10.20 maxim-lp 127.0.0.1 localhost.localdomain localhost ::1 maxim-desktop localhost6.localdomain6 localhost6 # following lines desirable ipv6 capable hosts ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts root@maxim-desktop:~# cat /etc/resolv.conf # generated networkmanager nameserver 192.168.20.1 root@maxim-desktop:~# cat /etc/hostname maxim-desktop root@maxim-desktop:~# hostname maxim-desktop root@maxim-desktop:~# hostname --fqdn root@maxim-desktop:~# ifconfig eth0 link encap:ethernet hwaddr 00:1c:c0:f2:ba:89 inet addr:192.168.20.20 bcast:192.168.0.255 mask:255.255.255.0 inet6 addr: fe80::21c:c0ff:fef2:ba89/64 scope:link broadcast running multicast mtu:1500 metric:1 rx packets:9023854 errors:0 dropped:0 overruns:0 frame:0 tx packets:8532803 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:4839992361 (4.8 gb) tx bytes:1067998152 (1.0 gb) interrupt:20 memory:d3300000-d3320000 lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host loopback running mtu:16436 metric:1 rx packets:12333251 errors:0 dropped:0 overruns:0 frame:0 tx packets:12333251 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:3216352432 (3.2 gb) tx bytes:3216352432 (3.2 gb)
i'm trying reach same result within java code.
sadly, following code doesn't seem cut through.
//copyright (c) 2011, maxim veksler //all rights reserved. // //redistribution , use in source , binary forms, or without //modification, permitted provided following conditions met: // * redistributions of source code must retain above copyright // notice, list of conditions , following disclaimer. // * redistributions in binary form must reproduce above copyright // notice, list of conditions , following disclaimer in // documentation and/or other materials provided distribution. // * neither name of <organization> nor // names of contributors may used endorse or promote products // derived software without specific prior written permission. // //this software provided copyright holders , contributors "as is" , //any express or implied warranties, including, not limited to, implied //warranties of merchantability , fitness particular purpose //disclaimed. in no event shall <copyright holder> liable //direct, indirect, incidental, special, exemplary, or consequential damages //(including, not limited to, procurement of substitute goods or services; //loss of use, data, or profits; or business interruption) caused , //on theory of liability, whether in contract, strict liability, or tort //(including negligence or otherwise) arising in way out of use of //software, if advised of possibility of such damage. import java.net.inet4address; import java.net.inet6address; import java.net.inetaddress; import java.net.networkinterface; import java.net.socketexception; import java.util.enumeration; import org.slf4j.logger; import org.slf4j.loggerfactory; /** * hostname , ip address info, based on jdk6 networkinterface * * @author maxim veksler <maxim@vekslers.org> */ public class networkutil { private static logger log = loggerfactory.getlogger(networkutil.class); public static void main(string[] args) { system.out.println("mac: " + networkutil.getmachinemac()); system.out.println("hostname: " + networkutil.getmachinehostname()); system.out.println("ip: " + networkutil.getmachineipaddress()); system.out.println("hostname ipv6: " + networkutil.getmachineipv6hostname()); system.out.println("ip ipv6: " + networkutil.getmachineipv6address()); } /** * mac address of remote ip (if on local lan). * @param hostnameorip target ip or hostname (if have dns configured). * * @return mac address if ip in local lan, null if not. */ public static string getremotehostmac(string hostnameorip) { try { inetaddress address = inetaddress.getbyname(hostnameorip); networkinterface networkinterface = networkinterface.getbyinetaddress(address); return obtainmacfromaddress(networkinterface); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain mac address address " + hostnameorip, e); } } // means had failure. return null; } /** * machine address of machine running on. * @return 08-00-27-dc-4a-9e or null if can't obtain mac */ public static string getmachinemac() { try { return obtainmacfromaddress(getnonloopbacknetworkinterface()); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain mac address localhost", e); } } return null; } /** * machine hostname, based on ipv4 configurations. * * @return string representing fqdn or null if can't find hostname */ public static string getmachinehostname() { try { networkinterface networkinterface = getnonloopbacknetworkinterface(); inet4address address = getinet4address(networkinterface); if(address != null) return address.getcanonicalhostname(); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain machinehostname", e); } } return null; } /** * machine hostname, based on ipv6 configurations. * @return string representing fqdn or null if can't find hostname */ public static string getmachineipv6hostname() { try { networkinterface networkinterface = getnonloopbacknetworkinterface(); inet6address address = getinet6address(networkinterface); if(address != null) return address.getcanonicalhostname(); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain ipv6hostname", e); } } return null; } /** * machine ip, based on ipv4 configurations. * * @return string representing ip or null if can't find configured interface */ public static string getmachineipaddress() { try { networkinterface networkinterface = getnonloopbacknetworkinterface(); inet4address address = getinet4address(networkinterface); if(address != null) return address.gethostaddress(); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain machineipaddress", e); } } return null; } /** * machine ip, based on ipv6 configurations. * * @return string representing ip or null if can't find configured interface */ public static string getmachineipv6address() { try { networkinterface networkinterface = getnonloopbacknetworkinterface(); inet6address address = getinet6address(networkinterface); if(address != null) return address.gethostaddress(); } catch (exception e) { if(log.isdebugenabled()) { log.debug("failed obtain machineipv6address", e); } } return null; } /* * ######################## * helper private functions */ private static string obtainmacfromaddress(networkinterface networkinterface) throws socketexception { if(networkinterface != null) { byte[] mac = networkinterface.gethardwareaddress(); if(mac == null) throw new error("failed obtain mac address interface: " + networkinterface.getdisplayname()); stringbuilder stringbuilder = new stringbuilder(17); /* * extract each array of mac address , convert hexa * following format 08-00-27-dc-4a-9e. */ (int = 0; < mac.length; i++) { stringbuilder.append(string.format("%02x%s", mac[i], (i < mac.length - 1) ? "-" : "")); } return stringbuilder.tostring(); } return null; } private static inet4address getinet4address(networkinterface networkinterface) { if(networkinterface != null) { enumeration<inetaddress> nicaddresses = networkinterface.getinetaddresses(); while(nicaddresses.hasmoreelements()) { inetaddress address = nicaddresses.nextelement(); if(address instanceof inet4address) return (inet4address)address; } } return null; } private static inet6address getinet6address(networkinterface networkinterface) { if(networkinterface != null) { enumeration<inetaddress> nicaddresses = networkinterface.getinetaddresses(); while(nicaddresses.hasmoreelements()) { inetaddress address = nicaddresses.nextelement(); if(address instanceof inet6address) return (inet6address)address; } } return null; } private static networkinterface getnonloopbacknetworkinterface() throws socketexception { // need iterate on nic's machine has because stupid ubuntu not assign // mac address default loopback interface... enumeration<networkinterface> b = networkinterface.getnetworkinterfaces(); while(b.hasmoreelements()) { networkinterface networkinterface = b.nextelement(); enumeration<inetaddress> inetaddresses = networkinterface.getinetaddresses(); while(inetaddresses.hasmoreelements()) { inetaddress address = inetaddresses.nextelement(); if(!address.isloopbackaddress()) return networkinterface; } } // means haven't found non loopback interfaces. bummer, return empty handed. return null; } }
the following printed on machine:
mac: 00-1c-c0-f2-ba-89 hostname: maxim-desktop ip: 192.168.20.20 hostname ipv6: fe80:0:0:0:21c:c0ff:fef2:ba89%2 ip ipv6: fe80:0:0:0:21c:c0ff:fef2:ba89%2
- as can see hostname output ipv4 & ipv6 not same, wonder why should this?
- also, meaning of
%2
get's attached ipv6 related queries - bug in code or jvm / linux kernel issue?
thank you,
maxim.
if @ code, here's does:
- find non-loopback network interface (ie. in case, eth0)
- query ip address of interface (in case, 192.168.20.20 or fe80:0:0:0:21c:c0ff:fef2:ba89%2)
- resolve ip address. can see
hosts
file, 192.168.20.20 correctly resolvesmaxim-desktop
, there no hostname corresponding ipv6 address.
ipv6 addresses beginning
fe80
(or, precise, addresses infe80::/10
network) link-local addresses, ,%2
denotes network interface system should use.
Comments
Post a Comment