vpnservice_builder.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2012-2014 Tobias Brunner
  3. * Copyright (C) 2012 Giuliano Grassi
  4. * Copyright (C) 2012 Ralf Sager
  5. * HSR Hochschule fuer Technik Rapperswil
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. #include "vpnservice_builder.h"
  18. #include "android_jni.h"
  19. #include <utils/debug.h>
  20. #include <library.h>
  21. typedef struct private_vpnservice_builder_t private_vpnservice_builder_t;
  22. /**
  23. * private data of vpnservice_builder
  24. */
  25. struct private_vpnservice_builder_t {
  26. /**
  27. * public interface
  28. */
  29. vpnservice_builder_t public;
  30. /**
  31. * Java object
  32. */
  33. jobject builder;
  34. };
  35. METHOD(vpnservice_builder_t, add_address, bool,
  36. private_vpnservice_builder_t *this, host_t *addr)
  37. {
  38. JNIEnv *env;
  39. jmethodID method_id;
  40. jstring str;
  41. char buf[INET6_ADDRSTRLEN];
  42. int prefix;
  43. androidjni_attach_thread(&env);
  44. DBG2(DBG_LIB, "builder: adding interface address %H", addr);
  45. prefix = addr->get_family(addr) == AF_INET ? 32 : 128;
  46. if (snprintf(buf, sizeof(buf), "%H", addr) >= sizeof(buf))
  47. {
  48. goto failed;
  49. }
  50. method_id = (*env)->GetMethodID(env, android_charonvpnservice_builder_class,
  51. "addAddress", "(Ljava/lang/String;I)Z");
  52. if (!method_id)
  53. {
  54. goto failed;
  55. }
  56. str = (*env)->NewStringUTF(env, buf);
  57. if (!str)
  58. {
  59. goto failed;
  60. }
  61. if (!(*env)->CallBooleanMethod(env, this->builder, method_id, str, prefix))
  62. {
  63. goto failed;
  64. }
  65. androidjni_detach_thread();
  66. return TRUE;
  67. failed:
  68. DBG1(DBG_LIB, "builder: failed to add address");
  69. androidjni_exception_occurred(env);
  70. androidjni_detach_thread();
  71. return FALSE;
  72. }
  73. METHOD(vpnservice_builder_t, set_mtu, bool,
  74. private_vpnservice_builder_t *this, int mtu)
  75. {
  76. JNIEnv *env;
  77. jmethodID method_id;
  78. androidjni_attach_thread(&env);
  79. DBG2(DBG_LIB, "builder: setting MTU to %d", mtu);
  80. method_id = (*env)->GetMethodID(env, android_charonvpnservice_builder_class,
  81. "setMtu", "(I)Z");
  82. if (!method_id)
  83. {
  84. goto failed;
  85. }
  86. if (!(*env)->CallBooleanMethod(env, this->builder, method_id, mtu))
  87. {
  88. goto failed;
  89. }
  90. androidjni_detach_thread();
  91. return TRUE;
  92. failed:
  93. DBG1(DBG_LIB, "builder: failed to set MTU");
  94. androidjni_exception_occurred(env);
  95. androidjni_detach_thread();
  96. return FALSE;
  97. }
  98. METHOD(vpnservice_builder_t, add_route, bool,
  99. private_vpnservice_builder_t *this, host_t *net, int prefix)
  100. {
  101. JNIEnv *env;
  102. jmethodID method_id;
  103. jstring str;
  104. char buf[INET6_ADDRSTRLEN];
  105. androidjni_attach_thread(&env);
  106. DBG2(DBG_LIB, "builder: adding route %+H/%d", net, prefix);
  107. if (snprintf(buf, sizeof(buf), "%+H", net) >= sizeof(buf))
  108. {
  109. goto failed;
  110. }
  111. method_id = (*env)->GetMethodID(env, android_charonvpnservice_builder_class,
  112. "addRoute", "(Ljava/lang/String;I)Z");
  113. if (!method_id)
  114. {
  115. goto failed;
  116. }
  117. str = (*env)->NewStringUTF(env, buf);
  118. if (!str)
  119. {
  120. goto failed;
  121. }
  122. if (!(*env)->CallBooleanMethod(env, this->builder, method_id, str, prefix))
  123. {
  124. goto failed;
  125. }
  126. androidjni_detach_thread();
  127. return TRUE;
  128. failed:
  129. DBG1(DBG_LIB, "builder: failed to add route");
  130. androidjni_exception_occurred(env);
  131. androidjni_detach_thread();
  132. return FALSE;
  133. }
  134. METHOD(vpnservice_builder_t, add_dns, bool,
  135. private_vpnservice_builder_t *this, host_t *dns)
  136. {
  137. JNIEnv *env;
  138. jmethodID method_id;
  139. jstring str;
  140. char buf[INET6_ADDRSTRLEN];
  141. androidjni_attach_thread(&env);
  142. DBG2(DBG_LIB, "builder: adding DNS server %H", dns);
  143. if (snprintf(buf, sizeof(buf), "%H", dns) >= sizeof(buf))
  144. {
  145. goto failed;
  146. }
  147. method_id = (*env)->GetMethodID(env, android_charonvpnservice_builder_class,
  148. "addDnsServer", "(Ljava/lang/String;)Z");
  149. if (!method_id)
  150. {
  151. goto failed;
  152. }
  153. str = (*env)->NewStringUTF(env, buf);
  154. if (!str)
  155. {
  156. goto failed;
  157. }
  158. if (!(*env)->CallBooleanMethod(env, this->builder, method_id, str))
  159. {
  160. goto failed;
  161. }
  162. androidjni_detach_thread();
  163. return TRUE;
  164. failed:
  165. DBG1(DBG_LIB, "builder: failed to add DNS server");
  166. androidjni_exception_occurred(env);
  167. androidjni_detach_thread();
  168. return FALSE;
  169. }
  170. /**
  171. * Establish or reestablish the TUN device
  172. */
  173. static int establish_internal(private_vpnservice_builder_t *this, char *method)
  174. {
  175. JNIEnv *env;
  176. jmethodID method_id;
  177. int fd;
  178. androidjni_attach_thread(&env);
  179. DBG2(DBG_LIB, "builder: building TUN device");
  180. method_id = (*env)->GetMethodID(env, android_charonvpnservice_builder_class,
  181. method, "()I");
  182. if (!method_id)
  183. {
  184. goto failed;
  185. }
  186. fd = (*env)->CallIntMethod(env, this->builder, method_id);
  187. if (fd == -1)
  188. {
  189. goto failed;
  190. }
  191. androidjni_detach_thread();
  192. return fd;
  193. failed:
  194. DBG1(DBG_LIB, "builder: failed to build TUN device");
  195. androidjni_exception_occurred(env);
  196. androidjni_detach_thread();
  197. return -1;
  198. }
  199. METHOD(vpnservice_builder_t, establish, int,
  200. private_vpnservice_builder_t *this)
  201. {
  202. return establish_internal(this, "establish");
  203. }
  204. METHOD(vpnservice_builder_t, establish_no_dns, int,
  205. private_vpnservice_builder_t *this)
  206. {
  207. return establish_internal(this, "establishNoDns");
  208. }
  209. METHOD(vpnservice_builder_t, destroy, void,
  210. private_vpnservice_builder_t *this)
  211. {
  212. JNIEnv *env;
  213. androidjni_attach_thread(&env);
  214. (*env)->DeleteGlobalRef(env, this->builder);
  215. androidjni_detach_thread();
  216. free(this);
  217. }
  218. vpnservice_builder_t *vpnservice_builder_create(jobject builder)
  219. {
  220. JNIEnv *env;
  221. private_vpnservice_builder_t *this;
  222. INIT(this,
  223. .public = {
  224. .add_address = _add_address,
  225. .add_route = _add_route,
  226. .add_dns = _add_dns,
  227. .set_mtu = _set_mtu,
  228. .establish = _establish,
  229. .establish_no_dns = _establish_no_dns,
  230. .destroy = _destroy,
  231. },
  232. );
  233. androidjni_attach_thread(&env);
  234. this->builder = (*env)->NewGlobalRef(env, builder);
  235. androidjni_detach_thread();
  236. return &this->public;
  237. }