android: Added NetworkManager class which allows to retrieve a local IP address
[strongswan.git] / src / frontends / android / jni / libandroidbridge / kernel / network_manager.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "network_manager.h"
16
17 #include "../android_jni.h"
18 #include <debug.h>
19
20 typedef struct private_network_manager_t private_network_manager_t;
21
22 struct private_network_manager_t {
23
24 /**
25 * Public interface
26 */
27 network_manager_t public;
28
29 /**
30 * Reference to NetworkManager object
31 */
32 jobject obj;
33
34 /**
35 * Java class for NetworkManager
36 */
37 jclass cls;
38 };
39
40 METHOD(network_manager_t, get_local_address, host_t*,
41 private_network_manager_t *this, bool ipv4)
42 {
43 JNIEnv *env;
44 jmethodID method_id;
45 jstring jaddr;
46 char *addr;
47 host_t *host;
48
49 androidjni_attach_thread(&env);
50 method_id = (*env)->GetMethodID(env, this->cls, "getLocalAddress",
51 "(Z)Ljava/lang/String;");
52 if (!method_id)
53 {
54 goto failed;
55 }
56 jaddr = (*env)->CallObjectMethod(env, this->obj, method_id, ipv4);
57 if (!jaddr)
58 {
59 goto failed;
60 }
61 addr = androidjni_convert_jstring(env, jaddr);
62 androidjni_detach_thread();
63 host = host_create_from_string(addr, 0);
64 free(addr);
65 return host;
66
67 failed:
68 androidjni_exception_occurred(env);
69 androidjni_detach_thread();
70 return NULL;
71 }
72
73 METHOD(network_manager_t, destroy, void,
74 private_network_manager_t *this)
75 {
76 JNIEnv *env;
77
78 androidjni_attach_thread(&env);
79 if (this->obj)
80 {
81 (*env)->DeleteGlobalRef(env, this->obj);
82 }
83 if (this->cls)
84 {
85 (*env)->DeleteGlobalRef(env, this->cls);
86 }
87 androidjni_detach_thread();
88 free(this);
89 }
90
91 /*
92 * Described in header.
93 */
94 network_manager_t *network_manager_create()
95 {
96 private_network_manager_t *this;
97 JNIEnv *env;
98 jmethodID method_id;
99 jobject obj;
100 jclass cls;
101
102 INIT(this,
103 .public = {
104 .get_local_address = _get_local_address,
105 .destroy = _destroy,
106 },
107 );
108
109 androidjni_attach_thread(&env);
110 cls = (*env)->FindClass(env, JNI_PACKAGE_STRING "/NetworkManager");
111 if (!cls)
112 {
113 goto failed;
114 }
115 this->cls = (*env)->NewGlobalRef(env, cls);
116 method_id = (*env)->GetMethodID(env, cls, "<init>",
117 "()V");
118 if (!method_id)
119 {
120 goto failed;
121 }
122 obj = (*env)->NewObject(env, cls, method_id);
123 if (!obj)
124 {
125 goto failed;
126 }
127 this->obj = (*env)->NewGlobalRef(env, obj);
128 androidjni_detach_thread();
129 return &this->public;
130
131 failed:
132 DBG1(DBG_KNL, "failed to build NetworkManager object");
133 androidjni_exception_occurred(env);
134 androidjni_detach_thread();
135 destroy(this);
136 return NULL;
137 };