Skip to content

Commit

Permalink
mobile: Fix c-ares initialization on Android (envoyproxy#35970)
Browse files Browse the repository at this point in the history
This PR fixes an issue with c-ares where the DNS servers are empty due
to a missing initialization by calling
[ares_library_init_android](https://c-ares.org/docs/ares_library_init_android.html).

Risk Level: low
Testing: test app
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: mobile (Android)

---------

Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw authored Sep 4, 2024
1 parent c198038 commit 7b67799
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.envoyproxy.envoymobile.engine;

import android.content.Context;
import android.net.ConnectivityManager;

import io.envoyproxy.envoymobile.engine.types.EnvoyEventTracker;
import io.envoyproxy.envoymobile.engine.types.EnvoyHTTPCallbacks;
import io.envoyproxy.envoymobile.engine.types.EnvoyLogger;
Expand All @@ -15,13 +17,15 @@
/* Android-specific implementation of the `EnvoyEngine` interface. */
public class AndroidEngineImpl implements EnvoyEngine {
private final EnvoyEngine envoyEngine;
private final Context context;

/**
* @param runningCallback Called when the engine finishes its async startup and begins running.
*/
public AndroidEngineImpl(Context context, EnvoyOnEngineRunning runningCallback,
EnvoyLogger logger, EnvoyEventTracker eventTracker,
Boolean enableProxying) {
this.context = context;
this.envoyEngine = new EnvoyEngineImpl(runningCallback, logger, eventTracker);
if (ContextUtils.getApplicationContext() == null) {
ContextUtils.initApplicationContext(context.getApplicationContext());
Expand All @@ -44,6 +48,10 @@ public void performRegistration(EnvoyConfiguration envoyConfiguration) {

@Override
public EnvoyStatus runWithConfig(EnvoyConfiguration envoyConfiguration, String logLevel) {
if (envoyConfiguration.useCares) {
JniLibrary.initCares(
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
}
return envoyEngine.runWithConfig(envoyConfiguration, logLevel);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.envoyproxy.envoymobile.engine;

import android.net.ConnectivityManager;

import io.envoyproxy.envoymobile.engine.types.EnvoyEventTracker;
import io.envoyproxy.envoymobile.engine.types.EnvoyHTTPCallbacks;
import io.envoyproxy.envoymobile.engine.types.EnvoyLogger;
Expand Down Expand Up @@ -305,4 +307,11 @@ public static native long createBootstrap(
long maxConnectionsPerHost, long streamIdleTimeoutSeconds, long perTryIdleTimeoutSeconds,
String appVersion, String appId, boolean trustChainVerification, byte[][] filterChain,
boolean enablePlatformCertificatesValidation, String upstreamTlsSni, byte[][] runtimeGuards);

/**
* Initializes c-ares.
* See <a
* href="https://c-ares.org/docs/ares_library_init_android.html">ares_library_init_android</a>.
*/
public static native void initCares(ConnectivityManager connectivityManager);
}
14 changes: 14 additions & 0 deletions mobile/library/jni/jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "source/common/protobuf/protobuf.h"

#include "ares.h"
#include "library/cc/engine_builder.h"
#include "library/common/api/c_types.h"
#include "library/common/bridge/utility.h"
Expand Down Expand Up @@ -201,6 +202,19 @@ extern "C" JNIEXPORT void JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibra
delete internal_engine;
}

extern "C" JNIEXPORT void JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_initCares(
JNIEnv* env, jclass, jobject connectivity_manager) {
#if defined(__ANDROID_API__)
Envoy::JNI::JniHelper jni_helper(env);
ares_library_init_jvm(jni_helper.getJavaVm());
ares_library_init_android(connectivity_manager);
#else
// For suppressing unused parameters.
(void)env;
(void)connectivity_manager;
#endif
}

extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_recordCounterInc(
JNIEnv* env,
jclass, // class
Expand Down
1 change: 1 addition & 0 deletions mobile/test/kotlin/apps/experimental/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class MainActivity : Activity() {
Log.d(TAG, "Event emitted: ${entry.key}, ${entry.value}")
}
})
.useCares(true)
.build()

recyclerView = findViewById<RecyclerView>(R.id.recycler_view)!!
Expand Down

0 comments on commit 7b67799

Please sign in to comment.