简体   繁体   中英

Android VpnService protect socket that's stored in native code?

I'm writing a VPN application and the socket used for the VPN Connection is handled in my native C code, not in java. How do I use VpnService.protect() on that socket? I noticed that it has a VpnService.protect(int) overload, could I return the int that socket returns from the native code to Java and protect it that way?

Example

// Native Code
int socket;

JNIEXPORT jint JNICALL
Java_com_my_package_Class_initializeSocket
(
    JNIEnv *env,
    jobject jobj
) {
    socket = socket(AF_INET, SOCK_DGRAM, 0);

    // . . . Handler other socket preparations 

    return (jint)socket;
}

// Java Code
public native int initializeSocket();

. . . 

int socket = initializeSocket();
this.protect(socket);

Edit

I did find this question that describes how the protect function works, and it looks like it might have a pretty simple implementation in C since it appears it's just using a setsockopt call. But I'm also relatively new to C so I can't quite follow how to replicate it.

I simply wanted verification that my processes was valid, after completing more testing I've verified that it works.

Example

// Native Code
int socket;

JNIEXPORT jint JNICALL
Java_com_my_package_Class_initializeSocket
(
    JNIEnv *env,
    jobject jobj
) {
    socket = socket(AF_INET, SOCK_DGRAM, 0);

    // . . . Handler other socket preparations 

    return (jint)socket;
}

// Java Code
public native int initializeSocket();

. . . 

int socket = initializeSocket();
this.protect(socket);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM