Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #define LOG_TAG "VpnJni" |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 20 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 21 | #include <arpa/inet.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 24 | #include <linux/if.h> |
| 25 | #include <linux/if_tun.h> |
| 26 | #include <linux/route.h> |
| 27 | #include <linux/ipv6_route.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <netinet/in.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include <log/log.h> |
| 37 | |
| 38 | #include "netutils/ifc.h" |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 39 | |
| 40 | #include "jni.h" |
| 41 | #include "JNIHelp.h" |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android |
| 44 | { |
| 45 | |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 46 | static int inet4 = -1; |
| 47 | static int inet6 = -1; |
| 48 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 49 | static inline in_addr_t *as_in_addr(sockaddr *sa) { |
| 50 | return &((sockaddr_in *)sa)->sin_addr.s_addr; |
| 51 | } |
| 52 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 53 | //------------------------------------------------------------------------------ |
| 54 | |
Chih-Hung Hsieh | 6c89616 | 2016-05-19 15:29:38 -0700 | [diff] [blame] | 55 | #define SYSTEM_ERROR (-1) |
| 56 | #define BAD_ARGUMENT (-2) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 57 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 58 | static int create_interface(int mtu) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 59 | { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 60 | int tun = open("/dev/tun", O_RDWR | O_NONBLOCK); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 61 | |
| 62 | ifreq ifr4; |
| 63 | memset(&ifr4, 0, sizeof(ifr4)); |
| 64 | |
| 65 | // Allocate interface. |
Chia-chi Yeh | 6224b5e | 2011-06-13 15:26:51 -0700 | [diff] [blame] | 66 | ifr4.ifr_flags = IFF_TUN | IFF_NO_PI; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 67 | if (ioctl(tun, TUNSETIFF, &ifr4)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 68 | ALOGE("Cannot allocate TUN: %s", strerror(errno)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | // Activate interface. |
| 73 | ifr4.ifr_flags = IFF_UP; |
| 74 | if (ioctl(inet4, SIOCSIFFLAGS, &ifr4)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 75 | ALOGE("Cannot activate %s: %s", ifr4.ifr_name, strerror(errno)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 76 | goto error; |
| 77 | } |
| 78 | |
Chia-chi Yeh | 3663227 | 2011-06-13 15:05:37 -0700 | [diff] [blame] | 79 | // Set MTU if it is specified. |
| 80 | ifr4.ifr_mtu = mtu; |
| 81 | if (mtu > 0 && ioctl(inet4, SIOCSIFMTU, &ifr4)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 82 | ALOGE("Cannot set MTU on %s: %s", ifr4.ifr_name, strerror(errno)); |
Chia-chi Yeh | 3663227 | 2011-06-13 15:05:37 -0700 | [diff] [blame] | 83 | goto error; |
| 84 | } |
| 85 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 86 | return tun; |
| 87 | |
| 88 | error: |
| 89 | close(tun); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 90 | return SYSTEM_ERROR; |
| 91 | } |
| 92 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 93 | static int get_interface_name(char *name, int tun) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 94 | { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 95 | ifreq ifr4; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 96 | if (ioctl(tun, TUNGETIFF, &ifr4)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 97 | ALOGE("Cannot get interface name: %s", strerror(errno)); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 98 | return SYSTEM_ERROR; |
| 99 | } |
| 100 | strncpy(name, ifr4.ifr_name, IFNAMSIZ); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int get_interface_index(const char *name) |
| 105 | { |
| 106 | ifreq ifr4; |
| 107 | strncpy(ifr4.ifr_name, name, IFNAMSIZ); |
| 108 | if (ioctl(inet4, SIOGIFINDEX, &ifr4)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 109 | ALOGE("Cannot get index of %s: %s", name, strerror(errno)); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 110 | return SYSTEM_ERROR; |
| 111 | } |
| 112 | return ifr4.ifr_ifindex; |
| 113 | } |
| 114 | |
| 115 | static int set_addresses(const char *name, const char *addresses) |
| 116 | { |
| 117 | int index = get_interface_index(name); |
| 118 | if (index < 0) { |
| 119 | return index; |
| 120 | } |
| 121 | |
| 122 | ifreq ifr4; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 123 | memset(&ifr4, 0, sizeof(ifr4)); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 124 | strncpy(ifr4.ifr_name, name, IFNAMSIZ); |
Chia-chi Yeh | 3667369 | 2011-06-13 14:24:13 -0700 | [diff] [blame] | 125 | ifr4.ifr_addr.sa_family = AF_INET; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 126 | ifr4.ifr_netmask.sa_family = AF_INET; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 127 | |
| 128 | in6_ifreq ifr6; |
| 129 | memset(&ifr6, 0, sizeof(ifr6)); |
| 130 | ifr6.ifr6_ifindex = index; |
| 131 | |
| 132 | char address[65]; |
| 133 | int prefix; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 134 | int chars; |
| 135 | int count = 0; |
| 136 | |
| 137 | while (sscanf(addresses, " %64[^/]/%d %n", address, &prefix, &chars) == 2) { |
| 138 | addresses += chars; |
| 139 | |
| 140 | if (strchr(address, ':')) { |
| 141 | // Add an IPv6 address. |
| 142 | if (inet_pton(AF_INET6, address, &ifr6.ifr6_addr) != 1 || |
| 143 | prefix < 0 || prefix > 128) { |
| 144 | count = BAD_ARGUMENT; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | ifr6.ifr6_prefixlen = prefix; |
| 149 | if (ioctl(inet6, SIOCSIFADDR, &ifr6)) { |
| 150 | count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR; |
| 151 | break; |
| 152 | } |
| 153 | } else { |
| 154 | // Add an IPv4 address. |
| 155 | if (inet_pton(AF_INET, address, as_in_addr(&ifr4.ifr_addr)) != 1 || |
| 156 | prefix < 0 || prefix > 32) { |
| 157 | count = BAD_ARGUMENT; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | if (count) { |
George Burgess IV | a346f54 | 2016-03-02 13:34:44 -0800 | [diff] [blame] | 162 | snprintf(ifr4.ifr_name, sizeof(ifr4.ifr_name), "%s:%d", name, count); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 163 | } |
| 164 | if (ioctl(inet4, SIOCSIFADDR, &ifr4)) { |
| 165 | count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | in_addr_t mask = prefix ? (~0 << (32 - prefix)) : 0; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 170 | *as_in_addr(&ifr4.ifr_netmask) = htonl(mask); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 171 | if (ioctl(inet4, SIOCSIFNETMASK, &ifr4)) { |
| 172 | count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR; |
| 173 | break; |
| 174 | } |
| 175 | } |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 176 | ALOGD("Address added on %s: %s/%d", name, address, prefix); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 177 | ++count; |
| 178 | } |
| 179 | |
| 180 | if (count == BAD_ARGUMENT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 181 | ALOGE("Invalid address: %s/%d", address, prefix); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 182 | } else if (count == SYSTEM_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 183 | ALOGE("Cannot add address: %s/%d: %s", address, prefix, strerror(errno)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 184 | } else if (*addresses) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 185 | ALOGE("Invalid address: %s", addresses); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 186 | count = BAD_ARGUMENT; |
| 187 | } |
| 188 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 189 | return count; |
| 190 | } |
| 191 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 192 | static int reset_interface(const char *name) |
| 193 | { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 194 | ifreq ifr4; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 195 | strncpy(ifr4.ifr_name, name, IFNAMSIZ); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 196 | ifr4.ifr_flags = 0; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 197 | |
| 198 | if (ioctl(inet4, SIOCSIFFLAGS, &ifr4) && errno != ENODEV) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 199 | ALOGE("Cannot reset %s: %s", name, strerror(errno)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 200 | return SYSTEM_ERROR; |
| 201 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int check_interface(const char *name) |
| 206 | { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 207 | ifreq ifr4; |
| 208 | strncpy(ifr4.ifr_name, name, IFNAMSIZ); |
Chia-chi Yeh | 6ddd574 | 2011-06-13 16:08:01 -0700 | [diff] [blame] | 209 | ifr4.ifr_flags = 0; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 210 | |
| 211 | if (ioctl(inet4, SIOCGIFFLAGS, &ifr4) && errno != ENODEV) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 212 | ALOGE("Cannot check %s: %s", name, strerror(errno)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 213 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 214 | return ifr4.ifr_flags; |
| 215 | } |
| 216 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 217 | static bool modifyAddress(JNIEnv *env, jobject thiz, jstring jName, jstring jAddress, |
| 218 | jint jPrefixLength, bool add) |
| 219 | { |
| 220 | int error = SYSTEM_ERROR; |
| 221 | const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL; |
| 222 | const char *address = jAddress ? env->GetStringUTFChars(jAddress, NULL) : NULL; |
| 223 | |
| 224 | if (!name) { |
| 225 | jniThrowNullPointerException(env, "name"); |
| 226 | } else if (!address) { |
| 227 | jniThrowNullPointerException(env, "address"); |
| 228 | } else { |
| 229 | if (add) { |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 230 | if ((error = ifc_add_address(name, address, jPrefixLength)) != 0) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 231 | ALOGE("Cannot add address %s/%d on interface %s (%s)", address, jPrefixLength, name, |
| 232 | strerror(-error)); |
| 233 | } |
| 234 | } else { |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 235 | if ((error = ifc_del_address(name, address, jPrefixLength)) != 0) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 236 | ALOGE("Cannot del address %s/%d on interface %s (%s)", address, jPrefixLength, name, |
| 237 | strerror(-error)); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (name) { |
| 243 | env->ReleaseStringUTFChars(jName, name); |
| 244 | } |
| 245 | if (address) { |
| 246 | env->ReleaseStringUTFChars(jAddress, address); |
| 247 | } |
| 248 | return !error; |
| 249 | } |
| 250 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 251 | //------------------------------------------------------------------------------ |
| 252 | |
| 253 | static void throwException(JNIEnv *env, int error, const char *message) |
| 254 | { |
| 255 | if (error == SYSTEM_ERROR) { |
| 256 | jniThrowException(env, "java/lang/IllegalStateException", message); |
| 257 | } else { |
| 258 | jniThrowException(env, "java/lang/IllegalArgumentException", message); |
| 259 | } |
| 260 | } |
| 261 | |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 262 | static jint create(JNIEnv *env, jobject /* thiz */, jint mtu) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 263 | { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 264 | int tun = create_interface(mtu); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 265 | if (tun < 0) { |
| 266 | throwException(env, tun, "Cannot create interface"); |
Chia-chi Yeh | 3667369 | 2011-06-13 14:24:13 -0700 | [diff] [blame] | 267 | return -1; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 268 | } |
Chia-chi Yeh | 3667369 | 2011-06-13 14:24:13 -0700 | [diff] [blame] | 269 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 272 | static jstring getName(JNIEnv *env, jobject /* thiz */, jint tun) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 273 | { |
| 274 | char name[IFNAMSIZ]; |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 275 | if (get_interface_name(name, tun) < 0) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 276 | throwException(env, SYSTEM_ERROR, "Cannot get interface name"); |
| 277 | return NULL; |
| 278 | } |
| 279 | return env->NewStringUTF(name); |
| 280 | } |
| 281 | |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 282 | static jint setAddresses(JNIEnv *env, jobject /* thiz */, jstring jName, |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 283 | jstring jAddresses) |
| 284 | { |
| 285 | const char *name = NULL; |
| 286 | const char *addresses = NULL; |
| 287 | int count = -1; |
| 288 | |
| 289 | name = jName ? env->GetStringUTFChars(jName, NULL) : NULL; |
| 290 | if (!name) { |
| 291 | jniThrowNullPointerException(env, "name"); |
| 292 | goto error; |
| 293 | } |
| 294 | addresses = jAddresses ? env->GetStringUTFChars(jAddresses, NULL) : NULL; |
| 295 | if (!addresses) { |
| 296 | jniThrowNullPointerException(env, "addresses"); |
| 297 | goto error; |
| 298 | } |
| 299 | count = set_addresses(name, addresses); |
| 300 | if (count < 0) { |
| 301 | throwException(env, count, "Cannot set address"); |
| 302 | count = -1; |
| 303 | } |
| 304 | |
| 305 | error: |
| 306 | if (name) { |
| 307 | env->ReleaseStringUTFChars(jName, name); |
| 308 | } |
| 309 | if (addresses) { |
| 310 | env->ReleaseStringUTFChars(jAddresses, addresses); |
| 311 | } |
| 312 | return count; |
| 313 | } |
| 314 | |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 315 | static void reset(JNIEnv *env, jobject /* thiz */, jstring jName) |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 316 | { |
| 317 | const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 318 | if (!name) { |
| 319 | jniThrowNullPointerException(env, "name"); |
| 320 | return; |
| 321 | } |
| 322 | if (reset_interface(name) < 0) { |
| 323 | throwException(env, SYSTEM_ERROR, "Cannot reset interface"); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 324 | } |
| 325 | env->ReleaseStringUTFChars(jName, name); |
| 326 | } |
| 327 | |
Andreas Gampe | 8dcf593 | 2014-09-30 16:41:19 -0700 | [diff] [blame] | 328 | static jint check(JNIEnv *env, jobject /* thiz */, jstring jName) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 329 | { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 330 | const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 331 | if (!name) { |
| 332 | jniThrowNullPointerException(env, "name"); |
| 333 | return 0; |
| 334 | } |
| 335 | int flags = check_interface(name); |
| 336 | env->ReleaseStringUTFChars(jName, name); |
| 337 | return flags; |
| 338 | } |
| 339 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 340 | static bool addAddress(JNIEnv *env, jobject thiz, jstring jName, jstring jAddress, |
| 341 | jint jPrefixLength) |
| 342 | { |
| 343 | return modifyAddress(env, thiz, jName, jAddress, jPrefixLength, true); |
| 344 | } |
| 345 | |
| 346 | static bool delAddress(JNIEnv *env, jobject thiz, jstring jName, jstring jAddress, |
| 347 | jint jPrefixLength) |
| 348 | { |
| 349 | return modifyAddress(env, thiz, jName, jAddress, jPrefixLength, false); |
| 350 | } |
| 351 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 352 | //------------------------------------------------------------------------------ |
| 353 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 354 | static const JNINativeMethod gMethods[] = { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 355 | {"jniCreate", "(I)I", (void *)create}, |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 356 | {"jniGetName", "(I)Ljava/lang/String;", (void *)getName}, |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 357 | {"jniSetAddresses", "(Ljava/lang/String;Ljava/lang/String;)I", (void *)setAddresses}, |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 358 | {"jniReset", "(Ljava/lang/String;)V", (void *)reset}, |
| 359 | {"jniCheck", "(Ljava/lang/String;)I", (void *)check}, |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 360 | {"jniAddAddress", "(Ljava/lang/String;Ljava/lang/String;I)Z", (void *)addAddress}, |
| 361 | {"jniDelAddress", "(Ljava/lang/String;Ljava/lang/String;I)Z", (void *)delAddress}, |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | int register_android_server_connectivity_Vpn(JNIEnv *env) |
| 365 | { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 366 | if (inet4 == -1) { |
| 367 | inet4 = socket(AF_INET, SOCK_DGRAM, 0); |
| 368 | } |
| 369 | if (inet6 == -1) { |
| 370 | inet6 = socket(AF_INET6, SOCK_DGRAM, 0); |
| 371 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 372 | return jniRegisterNativeMethods(env, "com/android/server/connectivity/Vpn", |
| 373 | gMethods, NELEM(gMethods)); |
| 374 | } |
| 375 | |
| 376 | }; |