0

I am trying to build a TCP/IP sniffer for android using VpnService.I modified ToyVpn example I am correctly getting the output IP packet from the descriptor and for the moment I am just trying to send it to the destination socket without IP and TCP headers and show in Log the response from the destination server. Actualy, what I have to do is to deliver the packet in the network and when I have a response write it in the OutputStream coresponding to the ParcelFileDescriptor.

I am using this code:

while (vpnInterface != null && vpnInterface.getFileDescriptor() != null
            && vpnInterface.getFileDescriptor().valid()) {

        packet.clear();

        // Read the outgoing packet from the input stream.
        final byte[] data = packet.array();

        int length = in.read(data);

        //use this to get the unsigned byte 
        int[] d = new int[data.length];

        if (length > 0) {
            packet.limit(length);
            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < data.length; i++) {
                d[i] = data[i] & 0xFF;
                sb.append(d[i] + " ");
            }
            Log.i("packet", sb.toString());
            Socket socket = SocketChannel.open().socket();
            if ((null != socket) && (null != this)) {
                this.protect(socket);
            }

            //connect to dest ip and port
            socket.connect(new InetSocketAddress(d[16] + "." + d[17] + "."
                    + d[18] + "." + d[19], (d[22] * 256) + d[23]));

            DataOutputStream dOut = new DataOutputStream(
                    socket.getOutputStream());
            DataInputStream dIn = new DataInputStream(
                    socket.getInputStream());

            dOut.write(data, 40, data.length - 40);
            dOut.flush();
            dOut.close();

            length = dIn.read(data);

            if (length > 0) {
                sb = new StringBuilder("");
                for (int i = 0; i < data.length; i++) {
                    d[i] = data[i] & 0xFF;
                    sb.append(d[i] + " ");
                }
                Log.i("response", sb.toString());
                dIn.close();
            }
        }

        Thread.sleep(10); 
}

The problem is that I get ClosedChannelException when trying to read the InputStream from the socket. Do you have any ideea why is this happening? The ideea is that I don't know how to manage the input packets from the dest socket.

Sorry if I made any mistake but I am beginner in JAVA.

1
  • The socket cannot possibly be null at the point you are testing it. If the socket couldn't be created an exception would have been thrown. And this can never be null at all. Don't waste time and space on pointless tests. There are more than enough real failure conditions to worry about.
    – user207421
    Dec 3, 2013 at 22:57

1 Answer 1

2

You're closing the InputStream that you've got from the Socket. JavaDoc says:

Closing the returned InputStream will close the associated socket.

Usually, you should never close a stream that you do not own!

The same is true for the OutputStream you get with socket.getOutputStream(). If you close it, the socket will be closed too!

5
  • I tested the code with the if commented and still get the exception. It is thrown on length = dIn.read(data). Maybe is because I close the OutputStream. I will test tomorow to see. Dec 3, 2013 at 23:52
  • You're closing the outputstream with dOut.close(). This closes the Socket too!
    – isnot2bad
    Dec 4, 2013 at 7:08
  • Do I have to add any header in the data that i send through the socket or it is ok like this. I ask this because I get a unintelligible response(an array of 1068 bytes) that are not an Ip packet or a tcp one. Thx! Dec 4, 2013 at 18:18
  • @user3041210 Sorry, can't help in this case; I don't know the VPN protocol stack.
    – isnot2bad
    Dec 4, 2013 at 22:33
  • Ok. But is not about the VPN protocol. I write the payload of a packet in the google.com socket and I receive a bunch of bytes. stackoverflow.com/questions/20379320/… Dec 5, 2013 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.