- Dynamic network change fixes and improvements.
This commit is contained in:
@ -375,11 +375,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instance,
|
||||
jstring javaPath, jstring javaIpV6Addr, jboolean javaPreferIpV6) {
|
||||
jstring javaPath, jstring javaIpV4Addr, jstring javaIpV6Addr, jboolean javaPreferIpV6) {
|
||||
|
||||
LOGD("starting baresip\n");
|
||||
|
||||
struct sa temp_sa;
|
||||
const char *ipv4_addr = (*env)->GetStringUTFChars(env, javaIpV4Addr, 0);
|
||||
const char *ipv6_addr = (*env)->GetStringUTFChars(env, javaIpV6Addr, 0);
|
||||
char start_error[64] = "";
|
||||
|
||||
@ -422,13 +423,19 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (strlen(ipv4_addr) > 0) {
|
||||
sa_set_str(&temp_sa, ipv4_addr, 0);
|
||||
net_set_address(baresip_network(), &temp_sa);
|
||||
}
|
||||
|
||||
if (strlen(ipv6_addr) > 0) {
|
||||
sa_set_str(&temp_sa, ipv6_addr, 0);
|
||||
net_set_address(baresip_network(), &temp_sa);
|
||||
if ((bool)(javaPreferIpV6 == JNI_TRUE))
|
||||
net_set_af(baresip_network(), AF_INET6);
|
||||
net_set_af(baresip_network(), javaPreferIpV6 == JNI_TRUE ? AF_INET6 : AF_INET);
|
||||
}
|
||||
|
||||
net_debug_log();
|
||||
|
||||
play_set_path(baresip_player(), path);
|
||||
|
||||
err = ua_init("baresip v" BARESIP_VERSION " (" ARCH "/" OS ")",
|
||||
@ -484,7 +491,6 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
|
||||
goto out;
|
||||
}
|
||||
|
||||
net_debug_log();
|
||||
for (le = list_head(uag_list()); le != NULL; le = le->next)
|
||||
ua_print_status_log(le->data);
|
||||
|
||||
@ -492,8 +498,8 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
|
||||
err = re_main(signal_handler);
|
||||
|
||||
out:
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, javaIpV6Addr, ipv6_addr);
|
||||
(*env)->ReleaseStringUTFChars(env, javaIpV4Addr, ipv4_addr);
|
||||
(*env)->ReleaseStringUTFChars(env, javaIpV6Addr, ipv6_addr);
|
||||
|
||||
if (err) {
|
||||
LOGE("stopping UAs due to error: (%d)\n", err);
|
||||
@ -1417,6 +1423,18 @@ Java_com_tutpro_baresip_Api_net_1set_1address(JNIEnv *env, jobject thiz, jstring
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_tutpro_baresip_Api_net_1unset_1address(JNIEnv *env, jobject thiz, jboolean javaIpV6) {
|
||||
int res = 0;
|
||||
struct sa temp_sa;
|
||||
LOGD("unsetting %s address\n", javaIpV6 == JNI_TRUE ? "IPv6" : "IpV4");
|
||||
if (javaIpV6 == JNI_TRUE)
|
||||
sa_init(&temp_sa, AF_INET6);
|
||||
else
|
||||
sa_init(&temp_sa, AF_INET);
|
||||
net_set_address(baresip_network(), &temp_sa);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_tutpro_baresip_Api_net_1set_1af(JNIEnv *env, jobject thiz, jboolean javaIpV6) {
|
||||
int af = javaIpV6 == JNI_TRUE ? AF_INET6 : AF_INET;
|
||||
|
||||
@ -33,6 +33,7 @@ object Api {
|
||||
external fun log_level_set(level: Int)
|
||||
external fun net_use_nameserver(servers: String): Int
|
||||
external fun net_set_address(ip_addr: String): Int
|
||||
external fun net_unset_address(ipv6: Boolean)
|
||||
external fun net_set_af(ipv6: Boolean): Boolean
|
||||
external fun net_force_change()
|
||||
external fun net_debug()
|
||||
|
||||
@ -79,19 +79,33 @@ class BaresipService: Service() {
|
||||
|
||||
override fun onAvailable(network: Network) {
|
||||
super.onAvailable(network)
|
||||
Log.i(LOG_TAG, "Network '${cm.getLinkProperties(network)}' is available")
|
||||
dnsServers = cm.getLinkProperties(network).dnsServers
|
||||
linkAddresses = cm.getLinkProperties(network).linkAddresses
|
||||
val linkProperties = cm.getLinkProperties(network)
|
||||
Log.i(LOG_TAG, "Network $network is available: '$linkProperties'")
|
||||
dnsServers = linkProperties.dnsServers
|
||||
linkAddresses = linkProperties.linkAddresses
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
super.onLost(network)
|
||||
Log.d(LOG_TAG, "Network '$network' is lost")
|
||||
val allNetworks = cm.allNetworks
|
||||
if (allNetworks.size > 0) {
|
||||
Log.d(LOG_TAG, "Network ${allNetworks[0]} is available")
|
||||
val linkProperties = cm.getLinkProperties(allNetworks[0])
|
||||
if (dynDns) {
|
||||
dnsServers = linkProperties.dnsServers
|
||||
if (Config.updateDnsServers(dnsServers) != 0)
|
||||
Log.w(LOG_TAG, "Failed to update DNS servers '$dnsServers'")
|
||||
}
|
||||
linkAddresses = linkProperties.linkAddresses
|
||||
Utils.updateLinkAddresses()
|
||||
UserAgent.register()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
|
||||
super.onLinkPropertiesChanged(network, linkProperties)
|
||||
Log.e(LOG_TAG, "Network link properties changed: $linkProperties")
|
||||
Log.e(LOG_TAG, "Network $network link properties changed: $linkProperties")
|
||||
if (!isServiceRunning)
|
||||
return
|
||||
if (dynDns) {
|
||||
@ -99,28 +113,8 @@ class BaresipService: Service() {
|
||||
if (Config.updateDnsServers(dnsServers) != 0)
|
||||
Log.w(LOG_TAG, "Failed to update DNS servers '$dnsServers'")
|
||||
}
|
||||
val ipV6Addr = Utils.findIpV6Address(linkProperties.linkAddresses)
|
||||
if (ipV6Addr != "") {
|
||||
Log.w(LOG_TAG, "Found IPv6 address '$ipV6Addr'")
|
||||
if (Api.net_set_address(ipV6Addr) != 0) {
|
||||
Log.w(LOG_TAG, "Failed to update net address '$ipV6Addr'")
|
||||
} else {
|
||||
if (preferIpV6)
|
||||
Api.net_set_af(true)
|
||||
}
|
||||
}
|
||||
val ipV4Addr = Utils.findIpV4Address(linkProperties.linkAddresses)
|
||||
if (ipV4Addr != "") {
|
||||
Log.w(LOG_TAG, "Found IPv4 address '$ipV4Addr'")
|
||||
if (Api.net_set_address(ipV4Addr) != 0) {
|
||||
Log.w(LOG_TAG, "Failed to update net address '$ipV4Addr'")
|
||||
} else {
|
||||
if (!preferIpV6 || (ipV6Addr == ""))
|
||||
Api.net_set_af(false)
|
||||
}
|
||||
}
|
||||
Api.net_force_change()
|
||||
Api.net_debug()
|
||||
linkAddresses = linkProperties.linkAddresses
|
||||
Utils.updateLinkAddresses()
|
||||
// UserAgent.register()
|
||||
}
|
||||
}
|
||||
@ -267,6 +261,7 @@ class BaresipService: Service() {
|
||||
Message.restore()
|
||||
|
||||
Thread(Runnable { baresipStart(filesPath,
|
||||
Utils.findIpV4Address(linkAddresses),
|
||||
Utils.findIpV6Address(linkAddresses),
|
||||
preferIpV6) }).start()
|
||||
|
||||
@ -1016,7 +1011,7 @@ class BaresipService: Service() {
|
||||
isServiceClean = true
|
||||
}
|
||||
|
||||
external fun baresipStart(path: String, ipV6Addr: String, preferIpV6: Boolean)
|
||||
external fun baresipStart(path: String, ipV4Addr: String, ipV6Addr: String, preferIpV6: Boolean)
|
||||
external fun baresipStop(force: Boolean)
|
||||
|
||||
companion object {
|
||||
|
||||
@ -220,28 +220,7 @@ class ConfigActivity : AppCompatActivity() {
|
||||
if (oldPreferIPv6 != preferIPv6String) {
|
||||
Config.replaceVariable("prefer_ipv6", preferIPv6String)
|
||||
BaresipService.preferIpV6 = preferIPv6String == "yes"
|
||||
if (preferIPv6String == "yes") {
|
||||
val ipV6Addr = Utils.findIpV6Address(BaresipService.linkAddresses)
|
||||
if (ipV6Addr != "") {
|
||||
if (Api.net_set_address(ipV6Addr) != 0) {
|
||||
Log.w("Baresip", "Failed to update net address '$ipV6Addr'")
|
||||
} else {
|
||||
Api.net_set_af(true)
|
||||
Api.net_force_change()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val ipV4Addr = Utils.findIpV4Address(BaresipService.linkAddresses)
|
||||
if (ipV4Addr != "") {
|
||||
if (Api.net_set_address(ipV4Addr) != 0) {
|
||||
Log.w("Baresip", "Failed to update net address '$ipV4Addr'")
|
||||
} else {
|
||||
Api.net_set_af(false)
|
||||
Api.net_force_change()
|
||||
}
|
||||
}
|
||||
}
|
||||
Api.net_debug()
|
||||
Utils.updateLinkAddresses()
|
||||
save = true
|
||||
}
|
||||
|
||||
|
||||
@ -244,6 +244,36 @@ object Utils {
|
||||
return ""
|
||||
}
|
||||
|
||||
fun updateLinkAddresses() {
|
||||
val ipV6Addr = findIpV6Address(BaresipService.linkAddresses)
|
||||
if (ipV6Addr != "") {
|
||||
Log.d("Baresip", "Found IPv6 address '$ipV6Addr'")
|
||||
if (Api.net_set_address(ipV6Addr) != 0) {
|
||||
Log.w("Baresip", "Failed to update net address '$ipV6Addr'")
|
||||
} else {
|
||||
Api.net_set_af(BaresipService.preferIpV6)
|
||||
}
|
||||
} else {
|
||||
Api.net_unset_address(true)
|
||||
Api.net_set_af(false)
|
||||
}
|
||||
val ipV4Addr = findIpV4Address(BaresipService.linkAddresses)
|
||||
if (ipV4Addr != "") {
|
||||
Log.w("Baresip", "Found IPv4 address '$ipV4Addr'")
|
||||
if (Api.net_set_address(ipV4Addr) != 0) {
|
||||
Log.w("Baresip", "Failed to update net address '$ipV4Addr'")
|
||||
} else {
|
||||
if (!BaresipService.preferIpV6 || (ipV6Addr == ""))
|
||||
Api.net_set_af(false)
|
||||
}
|
||||
} else {
|
||||
Api.net_unset_address(false)
|
||||
Api.net_set_af(false)
|
||||
}
|
||||
Api.net_force_change()
|
||||
Api.net_debug()
|
||||
}
|
||||
|
||||
fun implode(list: List<String>, sep: String): String {
|
||||
var res = ""
|
||||
for (s in list) {
|
||||
|
||||
Reference in New Issue
Block a user