@@ -30,6 +30,8 @@ static jmethodID android_cm_active_net_mid = NULL;
30
30
static jmethodID android_cm_link_props_mid = NULL ;
31
31
/* LinkProperties.getDnsServers */
32
32
static jmethodID android_lp_dns_servers_mid = NULL ;
33
+ /* LinkProperties.getDomains */
34
+ static jmethodID android_lp_domains_mid = NULL ;
33
35
/* List.size */
34
36
static jmethodID android_list_size_mid = NULL ;
35
37
/* List.get */
@@ -139,6 +141,12 @@ int ares_library_init_android(jobject connectivity_manager)
139
141
if (android_lp_dns_servers_mid == NULL )
140
142
goto cleanup ;
141
143
144
+ /* getDomains in API 21. */
145
+ android_lp_domains_mid = jni_get_method_id (env , obj_cls , "getDomains" ,
146
+ "()Ljava/lang/String;" );
147
+ if (android_lp_domains_mid == NULL )
148
+ goto cleanup ;
149
+
142
150
(* env )-> DeleteLocalRef (env , obj_cls );
143
151
obj_cls = jni_get_class (env , "java/util/List" );
144
152
if (obj_cls == NULL )
@@ -173,6 +181,7 @@ int ares_library_init_android(jobject connectivity_manager)
173
181
android_cm_active_net_mid = NULL ;
174
182
android_cm_link_props_mid = NULL ;
175
183
android_lp_dns_servers_mid = NULL ;
184
+ android_lp_domains_mid = NULL ;
176
185
android_list_size_mid = NULL ;
177
186
android_list_get_mid = NULL ;
178
187
android_ia_host_addr_mid = NULL ;
@@ -213,6 +222,7 @@ void ares_library_cleanup_android(void)
213
222
android_cm_active_net_mid = NULL ;
214
223
android_cm_link_props_mid = NULL ;
215
224
android_lp_dns_servers_mid = NULL ;
225
+ android_lp_domains_mid = NULL ;
216
226
android_list_size_mid = NULL ;
217
227
android_list_get_mid = NULL ;
218
228
android_ia_host_addr_mid = NULL ;
@@ -341,6 +351,95 @@ char **ares_get_android_server_list(size_t max_servers,
341
351
(* android_jvm )-> DetachCurrentThread (android_jvm );
342
352
return dns_list ;
343
353
}
354
+
355
+ char * ares_get_android_search_domains_list (void )
356
+ {
357
+ JNIEnv * env = NULL ;
358
+ jobject active_network = NULL ;
359
+ jobject link_properties = NULL ;
360
+ jstring domains = NULL ;
361
+ const char * domain ;
362
+ int res ;
363
+ size_t i ;
364
+ size_t cnt = 0 ;
365
+ char * domain_list = NULL ;
366
+ int need_detatch = 0 ;
367
+
368
+ if (android_jvm == NULL || android_connectivity_manager == NULL )
369
+ {
370
+ return NULL ;
371
+ }
372
+
373
+ if (android_cm_active_net_mid == NULL || android_cm_link_props_mid == NULL ||
374
+ android_lp_domains_mid == NULL )
375
+ {
376
+ return NULL ;
377
+ }
378
+
379
+ res = (* android_jvm )-> GetEnv (android_jvm , (void * * )& env , JNI_VERSION_1_6 );
380
+ if (res == JNI_EDETACHED )
381
+ {
382
+ env = NULL ;
383
+ res = (* android_jvm )-> AttachCurrentThread (android_jvm , & env , NULL );
384
+ need_detatch = 1 ;
385
+ }
386
+ if (res != JNI_OK || env == NULL )
387
+ goto done ;
388
+
389
+ /* JNI below is equivalent to this Java code.
390
+ import android.content.Context;
391
+ import android.net.ConnectivityManager;
392
+ import android.net.LinkProperties;
393
+
394
+ ConnectivityManager cm = (ConnectivityManager)this.getApplicationContext()
395
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
396
+ Network an = cm.getActiveNetwork();
397
+ LinkProperties lp = cm.getLinkProperties(an);
398
+ String domains = lp.getDomains();
399
+ for (String domain: domains.split(",")) {
400
+ String d = domain;
401
+ }
402
+
403
+ Note: The JNI ConnectivityManager object and all method IDs were previously
404
+ initialized in ares_library_init_android.
405
+ */
406
+
407
+ active_network = (* env )-> CallObjectMethod (env , android_connectivity_manager ,
408
+ android_cm_active_net_mid );
409
+ if (active_network == NULL )
410
+ goto done ;
411
+
412
+ link_properties =
413
+ (* env )-> CallObjectMethod (env , android_connectivity_manager ,
414
+ android_cm_link_props_mid , active_network );
415
+ if (link_properties == NULL )
416
+ goto done ;
417
+
418
+ /* Get the domains. It is a common separated list of domains to search. */
419
+ domains = (* env )-> CallObjectMethod (env , link_properties ,
420
+ android_lp_domains_mid );
421
+ if (domains == NULL )
422
+ goto done ;
423
+
424
+ /* Split on , */
425
+ domain = (* env )-> GetStringUTFChars (env , domains , 0 );
426
+ domain_list = ares_strdup (domain );
427
+ (* env )-> ReleaseStringUTFChars (env , domains , domain );
428
+ (* env )-> DeleteLocalRef (env , domains );
429
+
430
+ done :
431
+ if ((* env )-> ExceptionOccurred (env ))
432
+ (* env )-> ExceptionClear (env );
433
+
434
+ if (link_properties != NULL )
435
+ (* env )-> DeleteLocalRef (env , link_properties );
436
+ if (active_network != NULL )
437
+ (* env )-> DeleteLocalRef (env , active_network );
438
+
439
+ if (need_detatch )
440
+ (* android_jvm )-> DetachCurrentThread (android_jvm );
441
+ return domain_list ;
442
+ }
344
443
#else
345
444
/* warning: ISO C forbids an empty translation unit */
346
445
typedef int dummy_make_iso_compilers_happy ;
0 commit comments