I'm coding a VPN in android for watch the local network traffic (packet) and let the packets go after inspection. my app is base on ToyVPN
, so far i can receive the packets but i can't send them Through the tunnel. i see the tunnel connects to ('127.0.0.1',9040). My question is, is needed a server to bind on ('127.0.0.1',9040) to respond to other tunnel side? if not? where does the tunnel connect to? Basically how this tunnel works?
see a part of the code:
public void run() {
try {
//a. Configure the TUN and get the interface.
mInterface = builder.setSession("MyVPNService")
.addAddress("192.168.1.0", 24)
.addDnsServer("8.8.8.8")
.addRoute("0.0.0.0", 0).establish();
//b. Packets to be sent are queued in this input stream.
FileInputStream in = new FileInputStream(
mInterface.getFileDescriptor());
//b. Packets received need to be written to this output stream.
FileOutputStream out = new FileOutputStream(
mInterface.getFileDescriptor());
//c. The UDP channel can be used to pass/get ip package to/from server
DatagramChannel tunnel = DatagramChannel.open();
// Connect to the server, localhost is used for demonstration only.
tunnel.connect(new InetSocketAddress("127.0.0.1", 9040));
//d. Protect this socket, so package send by it will not be feedback to the vpn service.
protect(tunnel.socket());
tunnel.configureBlocking(false);
ByteBuffer packet = ByteBuffer.allocate(MAX_PACKET_SIZE);
Log.d("hixnal","tunnel open:" + tunnel.isOpen() + " connected:" + tunnel.isConnected());
//e. Use a loop to pass packets.
int timer = 0;
int p=0;
while (true) {
boolean idle = true;
int length= in.read(packet.array());
if (length > 0) {
p++;
Log.d("hixnal",p+"");
packet.limit(length);
//debugPacket(packet);
//tunnel.write(packet);