summaryrefslogtreecommitdiff
path: root/ext/libnatpmp/fr/free
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-11-30 13:10:22 -0800
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-11-30 13:10:22 -0800
commit67685213309a3e4aaa76a2f3173af5dd0ffa3eec (patch)
tree7b5745bfebac22cf83ef834b73d751b3467a07de /ext/libnatpmp/fr/free
parent6c0de526e864716a6c11d05108d4034257a19d5c (diff)
downloadinfinitytier-67685213309a3e4aaa76a2f3173af5dd0ffa3eec.tar.gz
infinitytier-67685213309a3e4aaa76a2f3173af5dd0ffa3eec.zip
Ton of uPnP work and adding NAT-PMP support, still testing...
Diffstat (limited to 'ext/libnatpmp/fr/free')
-rw-r--r--ext/libnatpmp/fr/free/miniupnp/libnatpmp/LibraryExtractor.java238
-rw-r--r--ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmp.java50
-rw-r--r--ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmpResponse.java28
-rw-r--r--ext/libnatpmp/fr/free/miniupnp/libnatpmp/URLUtils.java81
4 files changed, 397 insertions, 0 deletions
diff --git a/ext/libnatpmp/fr/free/miniupnp/libnatpmp/LibraryExtractor.java b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/LibraryExtractor.java
new file mode 100644
index 00000000..5491d940
--- /dev/null
+++ b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/LibraryExtractor.java
@@ -0,0 +1,238 @@
+package fr.free.miniupnp.libnatpmp;
+
+/** I (Leah X Schmidt) copied this code from jnaerator, because
+JNAerator's extractor requires you to buy into the whole JNA
+concept.
+
+JNAErator is
+Copyright (c) 2009 Olivier Chafik, All Rights Reserved
+
+JNAerator is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+JNAerator is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with JNAerator. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+public class LibraryExtractor {
+
+ private static boolean libPathSet = false;
+
+ public static String getLibraryPath(String libraryName, boolean extractAllLibraries, Class<?> cl) {
+ try {
+ String customPath = System.getProperty("library." + libraryName);
+ if (customPath == null)
+ customPath = System.getenv(libraryName.toUpperCase() + "_LIBRARY");
+ if (customPath != null) {
+ File f = new File(customPath);
+ if (!f.exists())
+ System.err.println("Library file '" + customPath + "' does not exist !");
+ else
+ return f.getAbsolutePath();
+ }
+ //ClassLoader cl = LibraryExtractor.class.getClassLoader();
+ String prefix = "(?i)" + (isWindows() ? "" : "lib") + libraryName + "[^A-Za-z_].*";
+ String libsuffix = "(?i).*\\.(so|dll|dylib|jnilib)";
+ //String othersuffix = "(?i).*\\.(pdb)";
+
+ URL sourceURL = null;
+ List<URL> otherURLs = new ArrayList<URL>();
+
+
+ String arch = getCurrentOSAndArchString();
+ //System.out.println("libURL = " + libURL);
+ List<URL> list = URLUtils.listFiles(URLUtils.getResource(cl, "libraries/" + arch)),
+ noArchList = URLUtils.listFiles(URLUtils.getResource(cl, "libraries/noarch"));
+
+ Set<String> names = new HashSet<String>();
+ for (URL url : list) {
+ String name = getFileName(url);
+ names.add(name);
+ }
+ for (URL url : noArchList) {
+ String name = getFileName(url);
+ if (names.add(name))
+ list.add(url);
+ }
+
+ for (File f : new File(".").listFiles())
+ if (f.isFile())
+ list.add(f.toURI().toURL());
+
+ for (URL url : list) {
+ String name = getFileName(url);
+ boolean pref = name.matches(prefix), suff = name.matches(libsuffix);
+ if (pref && suff)
+ sourceURL = url;
+ else //if (suff || fileName.matches(othersuffix))
+ otherURLs.add(url);
+ }
+ List<File> files = new ArrayList<File>();
+ if (extractAllLibraries) {
+ for (URL url : otherURLs)
+ files.add(extract(url));
+ }
+
+ if (System.getProperty("javawebstart.version") != null) {
+ if (isWindows()) {
+ //File f = new File("c:\\Windows\\" + (Platform.is64Bit() ? "SysWOW64\\" : "System32\\") + libraryName + ".dll");
+ File f = new File("c:\\Windows\\" + "System32\\" + libraryName + ".dll");
+ if (f.exists())
+ return f.toString();
+ } else if (isMac()) {
+ File f = new File("/System/Library/Frameworks/" + libraryName + ".framework/" + libraryName);
+ if (f.exists())
+ return f.toString();
+ }
+ }
+
+ if (sourceURL == null)
+ return libraryName;
+ else {
+ File file = extract(sourceURL);
+ files.add(file);
+
+ int lastSize;
+ do {
+ lastSize = files.size();
+ for (Iterator<File> it = files.iterator(); it.hasNext();) {
+ File f = it.next();
+ if (!f.getName().matches(libsuffix))
+ continue;
+
+ try {
+ System.load(f.toString());
+ it.remove();
+ } catch (Throwable ex) {
+ System.err.println("Loading " + f.getName() + " failed (" + ex + ")");
+ }
+ }
+ } while (files.size() < lastSize);
+
+ return file.getCanonicalPath();
+ }
+ } catch (Throwable ex) {
+ System.err.println("ERROR: Failed to extract library " + libraryName);
+ ex.printStackTrace();
+ return libraryName;
+ }
+ }
+
+ public static final boolean isWindows() {
+ String osName = System.getProperty("os.name");
+ return osName.startsWith("Windows");
+ }
+
+ public static final boolean isMac() {
+ String osName = System.getProperty("os.name");
+ return osName.startsWith("Mac") || osName.startsWith("Darwin");
+ }
+
+ //this code is from JNA, but JNA has a fallback to some native
+ //stuff in case this doesn't work. Since sun.arch.data.model is
+ //defined for Sun and IBM, this should work nearly everywhere.
+ public static final boolean is64Bit() {
+ String model = System.getProperty("sun.arch.data.model",
+ System.getProperty("com.ibm.vm.bitmode"));
+ if (model != null) {
+ return "64".equals(model);
+ }
+ String arch = System.getProperty("os.arch").toLowerCase();
+ if ("x86_64".equals(arch)
+ || "ia64".equals(arch)
+ || "ppc64".equals(arch)
+ || "sparcv9".equals(arch)
+ || "amd64".equals(arch)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public static String getCurrentOSAndArchString() {
+ String os = System.getProperty("os.name"), arch = System.getProperty("os.arch");
+ if (os.equals("Mac OS X")) {
+ os = "darwin";
+ arch = "fat";
+ } else if (os.startsWith("Windows")) {
+ return "win" + (is64Bit() ? "64" : "32");
+ } else if (os.matches("SunOS|Solaris"))
+ os = "solaris";
+ return os + "-" + arch;
+ }
+
+ private static File extract(URL url) throws IOException {
+ File localFile;
+ if ("file".equals(url.getProtocol()))
+ localFile = new File(URLDecoder.decode(url.getFile(), "UTF-8"));
+ else {
+ File f = new File(System.getProperty("user.home"));
+ f = new File(f, ".jnaerator");
+ f = new File(f, "extractedLibraries");
+ if (!f.exists())
+ f.mkdirs();
+
+ if (!libPathSet) {
+ String path = System.getProperty("java.library.path");
+ if (path == null) {
+ System.setProperty("java.library.path", f.toString());
+ } else {
+ System.setProperty("java.library.path", path + ":" + f);
+ }
+
+ libPathSet = true;
+ }
+ localFile = new File(f, new File(url.getFile()).getName());
+ URLConnection c = url.openConnection();
+ if (localFile.exists() && localFile.lastModified() > c.getLastModified()) {
+ c.getInputStream().close();
+ } else {
+ System.out.println("Extracting " + url);
+ InputStream in = c.getInputStream();
+ OutputStream out = new FileOutputStream(localFile);
+ int len;
+ byte[] b = new byte[1024];
+ while ((len = in.read(b)) > 0)
+ out.write(b, 0, len);
+ out.close();
+ in.close();
+ }
+ }
+ return localFile;
+ }
+
+ private static String getFileName(URL url) {
+ return new File(url.getFile()).getName();
+ }
+
+ public static void main(String[] args) {
+ System.out.println(getCurrentOSAndArchString());
+ }
+} \ No newline at end of file
diff --git a/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmp.java b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmp.java
new file mode 100644
index 00000000..2f1ddd3d
--- /dev/null
+++ b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmp.java
@@ -0,0 +1,50 @@
+package fr.free.miniupnp.libnatpmp;
+
+import java.nio.ByteBuffer;
+
+
+public class NatPmp {
+ private static final String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("jninatpmp", true, NatPmp.class);
+
+ static {
+ String s = JNA_LIBRARY_NAME;
+ startup();
+ }
+
+ public ByteBuffer natpmp;
+
+ public NatPmp() {
+ init(0, 0);
+ }
+
+ public NatPmp(int forcedgw) {
+ init(1, forcedgw);
+ }
+
+ /** Cleans up the native resources used by this object.
+ Attempting to use the object after this has been called
+ will lead to crashes.*/
+ public void dispose() {
+ free();
+ }
+
+
+ protected void finalize() {
+ if (natpmp != null)
+ free();
+ }
+
+ private native void init(int forcegw, int forcedgw);
+ private native void free();
+
+ private static native void startup();
+
+ public native int sendPublicAddressRequest();
+ public native int sendNewPortMappingRequest(int protocol, int privateport, int publicport, int lifetime);
+
+ //returns a number of milliseconds, in accordance with Java convention
+ public native long getNatPmpRequestTimeout();
+
+ public native int readNatPmpResponseOrRetry(NatPmpResponse response);
+
+}
diff --git a/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmpResponse.java b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmpResponse.java
new file mode 100644
index 00000000..35c87eab
--- /dev/null
+++ b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/NatPmpResponse.java
@@ -0,0 +1,28 @@
+package fr.free.miniupnp.libnatpmp;
+
+public class NatPmpResponse {
+
+ public static final int TYPE_PUBLICADDRESS=0;
+ public static final int TYPE_UDPPORTMAPPING=1;
+ public static final int TYPE_TCPPORTMAPPING=2;
+
+ /** see TYPE_* constants */
+ public short type;
+ /** NAT-PMP response code */
+ public short resultcode;
+ /** milliseconds since start of epoch */
+ public long epoch;
+
+ /** only defined if type == 0*/
+ public int addr;
+
+ /** only defined if type != 0 */
+ public int privateport;
+
+ /** only defined if type != 0 */
+ public int mappedpublicport;
+
+ /** only defined if type != 0 */
+ public long lifetime; //milliseconds
+
+} \ No newline at end of file
diff --git a/ext/libnatpmp/fr/free/miniupnp/libnatpmp/URLUtils.java b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/URLUtils.java
new file mode 100644
index 00000000..5b419ab3
--- /dev/null
+++ b/ext/libnatpmp/fr/free/miniupnp/libnatpmp/URLUtils.java
@@ -0,0 +1,81 @@
+package fr.free.miniupnp.libnatpmp;
+
+/** I (Leah X Schmidt) copied this code from jnaerator, because
+JNAerator's extractor requires you to buy into the whole JNA
+concept.
+
+JNAErator is
+Copyright (c) 2009 Olivier Chafik, All Rights Reserved
+
+JNAerator is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+JNAerator is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with JNAerator. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+public class URLUtils {
+
+ public static URL getResource(Class<?> cl, String path) throws IOException {
+ String clp = cl.getName().replace('.', '/') + ".class";
+ URL clu = cl.getClassLoader().getResource(clp);
+ String s = clu.toString();
+ if (s.endsWith(clp))
+ return new URL(s.substring(0, s.length() - clp.length()) + path);
+
+ if (s.startsWith("jar:")) {
+ String[] ss = s.split("!");
+ return new URL(ss[1] + "!/" + path);
+ }
+ return null;
+ }
+
+ public static List<URL> listFiles(URL directory) throws IOException {
+ List<URL> ret = new ArrayList<URL>();
+ String s = directory.toString();
+ if (s.startsWith("jar:")) {
+ String[] ss = s.substring("jar:".length()).split("!");
+ String path = ss[1];
+ URL target = new URL(ss[0]);
+ InputStream tin = target.openStream();
+ try {
+ JarInputStream jin = new JarInputStream(tin);
+ JarEntry je;
+ while ((je = jin.getNextJarEntry()) != null) {
+ String p = "/" + je.getName();
+ if (p.startsWith(path) && p.indexOf('/', path.length() + 1) < 0)
+
+ ret.add(new URL("jar:" + target + "!" + p));
+ }
+ } finally {
+ tin.close();
+ }
+ } else if (s.startsWith("file:")) {
+ File f = new File(directory.getFile());
+ File[] ffs = f.listFiles();
+ if (ffs != null)
+ for (File ff : ffs)
+ ret.add(ff.toURI().toURL());
+ } else
+ throw new IOException("Cannot list contents of " + directory);
+
+ return ret;
+ }
+} \ No newline at end of file