android: Add helper to parse IP addresses from strings
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.
This commit is contained in:
parent
8e7ad9ace8
commit
7028e9d31e
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue