]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Add helper to parse IP addresses from strings
authorTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 17:47:33 +0000 (18:47 +0100)
committerTobias Brunner <tobias@strongswan.org>
Tue, 5 Mar 2019 17:56:09 +0000 (18:56 +0100)
Using InetAddress.fromName() is not ideal as it might result in a DNS
resolution, which causes an exception if we do it from the main thread.

src/frontends/android/app/src/main/java/org/strongswan/android/utils/Utils.java
src/frontends/android/app/src/main/jni/libandroidbridge/charonservice.c

index f2e8e005828a5988dceb8976d432192b0de50e25..9dec3c0e3b063e834b11a9e24b8d14a450381616 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014-2017 Tobias Brunner
+ * Copyright (C) 2014-2019 Tobias Brunner
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -16,6 +16,9 @@
 package org.strongswan.android.utils;
 
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
 public class Utils
 {
        static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();
@@ -46,4 +49,29 @@ public class Utils
         * @return true if valid
         */
        public native static boolean isProposalValid(boolean ike, String proposal);
+
+       /**
+        * Parse an IP address without doing a name lookup
+        *
+        * @param address IP address string
+        * @return address bytes if valid
+        */
+       private native static byte[] parseInetAddressBytes(String address);
+
+       /**
+        * Parse an IP address without doing a name lookup (as compared to InetAddress.fromName())
+        *
+        * @param address IP address string
+        * @return address if valid
+        * @throws UnknownHostException if address is invalid
+        */
+       public static InetAddress parseInetAddress(String address) throws UnknownHostException
+       {
+               byte[] bytes = parseInetAddressBytes(address);
+               if (bytes == null)
+               {
+                       throw new UnknownHostException();
+               }
+               return InetAddress.getByAddress(bytes);
+       }
 }
index aa84a2b9c7df17d5655eb4b0e89d0d57ad285297..78abede585fb5ac8fe1d0816a5e09d8964f267e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2018 Tobias Brunner
+ * Copyright (C) 2012-2019 Tobias Brunner
  * Copyright (C) 2012 Giuliano Grassi
  * Copyright (C) 2012 Ralf Sager
  * HSR Hochschule fuer Technik Rapperswil
@@ -758,3 +758,34 @@ JNI_METHOD_P(org_strongswan_android_utils, Utils, isProposalValid, jboolean,
        library_deinit();
        return valid;
 }
+
+/**
+ * Utility function to parse an IP address from a string (static, so `this` is the class)
+ */
+JNI_METHOD_P(org_strongswan_android_utils, Utils, parseInetAddressBytes, jbyteArray,
+       jstring address)
+{
+       jbyteArray bytes;
+       host_t *host;
+       char *str;
+
+       dbg = dbg_android;
+
+       if (!library_init(NULL, "charon"))
+       {
+               library_deinit();
+               return NULL;
+       }
+       str = androidjni_convert_jstring(env, address);
+       host = host_create_from_string(str, 0);
+       if (!host)
+       {
+               free(str);
+               return NULL;
+       }
+       bytes = byte_array_from_chunk(env, host->get_address(host));
+       host->destroy(host);
+       free(str);
+       library_deinit();
+       return bytes;
+}