我正在编写一个VPN应用程序,当我使用java时,就可以了。但是,当我使用c ++本机代码时,它可以发送到我的远程主机,也可以接收程序包。但是,似乎无法写入tun fd。我怎么了?
//has problem
public int onStartCommand(Intent intent, int flags, int startId)
{
Builder builder = new Builder();
builder.setSession("MyVPNService");
builder.addAddress("192.168.1.94", 24);
builder.addDnsServer("8.8.8.8");
builder.addRoute("0.0.0.0", 0);
mInterface = builder.establish();
final int fd = mInterface.getFd();
//final int fd = mInterface.detachFd(); //no use
int clientFd = getClientFd();
protect(clientFd);
new Thread(new Runnable() {
@Override
public void run() {
int re = sendFd(fd);
}
}).start();
return START_STICKY;
}
//my native code
if(events_in[i].data.fd == client_fd && events_in[i].events & EPOLLIN)
{
struct sockaddr_in src = { 0 };
socklen_t len = sizeof(struct sockaddr);;
memset(buf, 0, MAX_BKG_LEN);
int n = recvfrom(client_fd, buf, MAX_BKG_LEN, 0, (struct sockaddr*)&src, &len);
write(fd, buf, n); //Did I wrong?
}