changeset 735:2486fa6490c6

add Io.my_ips()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jun 2016 18:24:28 -0600
parents e44e98fe9de8
children 2113294ccc6d
files core/src/luan/modules/Io.luan core/src/luan/modules/IoLuan.java
diffstat 2 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/Io.luan	Tue Jun 14 17:54:12 2016 -0600
+++ b/core/src/luan/modules/Io.luan	Tue Jun 14 18:24:28 2016 -0600
@@ -5,6 +5,7 @@
 local M = {}
 
 M.ip = IoLuan.ip
+M.my_ips = IoLuan.my_ips
 M.read_console_line = IoLuan.read_console_line
 M.schemes = IoLuan.newSchemes()
 M.uri = IoLuan.uri
--- a/core/src/luan/modules/IoLuan.java	Tue Jun 14 17:54:12 2016 -0600
+++ b/core/src/luan/modules/IoLuan.java	Tue Jun 14 18:24:28 2016 -0600
@@ -25,8 +25,11 @@
 import java.net.Socket;
 import java.net.ServerSocket;
 import java.net.InetAddress;
+import java.net.Inet4Address;
+import java.net.NetworkInterface;
 import java.net.MalformedURLException;
 import java.net.UnknownHostException;
+import java.util.Enumeration;
 import java.util.Map;
 import java.util.zip.ZipOutputStream;
 import java.util.zip.ZipEntry;
@@ -746,6 +749,19 @@
 		}
 	}
 
+	public static LuanTable my_ips() throws IOException {
+		LuanTable tbl = new LuanTable();
+		for( Enumeration<NetworkInterface> e1 = NetworkInterface.getNetworkInterfaces(); e1.hasMoreElements(); ) {
+			NetworkInterface ni = e1.nextElement();
+			for( Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements(); ) {
+				InetAddress ia = e2.nextElement();
+				if( ia instanceof Inet4Address )
+					tbl.rawPut(ia.getHostAddress(),true);
+			}
+		}
+		return tbl;
+	}
+
 
 	// files maps zip name to uri
 	public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException {