1 22 package org.objectweb.petals.util; 23 24 import java.io.DataInputStream ; 25 import java.io.IOException ; 26 import java.net.InetAddress ; 27 import java.net.NetworkInterface ; 28 import java.net.SocketException ; 29 import java.util.Enumeration ; 30 31 37 public class NetworkUtil { 38 39 40 public static String IPV4_LOOPBACK_ADDRESS = "127.0.0.1"; 41 42 45 private NetworkUtil() { 46 } 47 48 55 public static String getIPv4Address() throws SocketException { 56 Enumeration <NetworkInterface > e = NetworkInterface 57 .getNetworkInterfaces(); 58 59 while (e.hasMoreElements()) { 60 NetworkInterface netItf = e.nextElement(); 61 Enumeration <InetAddress > addresses = netItf.getInetAddresses(); 62 63 while (addresses.hasMoreElements()) { 64 InetAddress ip = addresses.nextElement(); 65 if (!ip.isLoopbackAddress() && isIPv4(ip.getHostAddress())) { 66 return ip.getHostAddress(); 67 } 68 } 69 } 70 return NetworkUtil.IPV4_LOOPBACK_ADDRESS; 71 } 72 73 80 protected static String getAddressFromSystemCall() { 81 String host = IPV4_LOOPBACK_ADDRESS; 82 String hostIP = ""; 83 String os = System.getProperty("os.name").toLowerCase(); 84 85 String command = "/sbin/ifconfig"; 86 87 if (os.indexOf("windows") != -1) { 88 command = "ipconfig"; 90 91 } else if (os.indexOf("linux") != -1) { 92 command = "/sbin/ifconfig"; 94 95 } else if (os.indexOf("mac") != -1) { 96 command = "ifconfig"; 98 } 99 100 try { 102 Process process = Runtime.getRuntime().exec(command); 103 try { 104 process.waitFor(); 105 } catch (InterruptedException e) { 106 } 108 DataInputStream dis = new DataInputStream (process.getInputStream()); 109 byte[] content = new byte[dis.available()]; 110 dis.readFully(content); 111 String output = new String (content); 112 113 if (os.indexOf("windows") != -1 && output.length() > 45) { 115 hostIP = getAddressFromWindowsSystemCallResult(output); 116 } else if (os.indexOf("linux") != -1) { 117 hostIP = getAddressFromLinuxSystemCallResult(output); 118 } else if (os.indexOf("mac") != -1) { 119 hostIP = getAddressFromMacSystemCallResult(output); 120 } else { 121 } 123 124 } catch (IOException e) { 125 } 127 128 if (hostIP.charAt(0) >= '0' && hostIP.charAt(0) <= '9') { 132 host = hostIP; 133 } 134 135 return host.trim(); 136 } 137 138 143 private static String getAddressFromWindowsSystemCallResult(String output) { 144 String hostIP = ""; 145 if (output.indexOf("IP") > -1) { 149 hostIP = output.substring(output.indexOf(": ", output.indexOf("IP", 150 output.indexOf("IP") + 10)) + 2, output.indexOf(": ", output 151 .indexOf("IP", output.indexOf("IP") + 10)) + 15); 152 153 } 154 return hostIP; 155 } 156 157 163 private static String getAddressFromMacSystemCallResult(String output) { 164 return ""; 165 } 166 167 172 private static String getAddressFromLinuxSystemCallResult(String output) { 173 String hostIP = ""; 174 if (output.indexOf("lo") > -1) { 176 output = output.substring(0, output.indexOf("lo")) 178 + output.substring(output.indexOf("\n", output.indexOf(")", 179 output.indexOf("lo"))), output.length()); 180 } 181 if (output.indexOf("inet addr:") > -1) { 182 hostIP = output.substring(output.indexOf("inet addr:") + 10, output 184 .indexOf(" ", output.indexOf("inet addr:") + 10)); 185 } else if (output.indexOf("inet adr:") > -1) { 186 hostIP = output.substring(output.indexOf("inet adr:") + 9, output 188 .indexOf(" ", output.indexOf("inet adr:") + 10)); 189 } 190 return hostIP; 191 } 192 193 198 public static boolean isIPv4(String hostAddress) { 199 return hostAddress.split("[.]").length == 4; 200 } 201 202 } 203 | Popular Tags |