I am writing a VPN application, and the socket used for the VPN connection is processed in my own C code, not in java. How to use VpnService.protect() on this socket? I noticed that it has an overload of VpnService.protect(int) , can I return an int that socket returns from native code in Java and protects it this 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 found this question that describes how the protect function works, and it looks like it can have a fairly simple implementation in C, as it seems to it just by using the setsockopt call. But I'm also relatively new to C, so I can't completely repeat how to replicate it.
source share