我有以下方法(来自此repo)
private void runVpnConnection() {
configure();
FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());
// Allocate the buffer for a single packet.
ByteBuffer packet = ByteBuffer.allocate(32767);
Socket tcpSocket = new Socket();
boolean ok = true;
//forwarding packets
while (ok) {
// Assume no progress in this iteration.
try {
// Read the outgoing packet from the input stream.
int length = in.read(packet.array());
if (length > 0) {
Log.i(TAG, "packet received");
packet.limit(length);
String serverIP = "192.168.178.20";//getDestinationIP(packet);
int port = 5002; //getDestinationPort(packet, getHeaderLength(packet));
//Log.d(TAG, "destination IP: " + serverIP + " port: " + port);
InetAddress serverAddr = InetAddress.getByName(serverIP);
SocketAddress socketadd = new InetSocketAddress(serverAddr, port);
/*
tcpSocket.connect(socketadd);
OutputStream outBuffer = tcpSocket.getOutputStream();
outBuffer.write(packet.array());
outBuffer.flush();
outBuffer.close();
packet.clear();
*/
//ok = false;
ByteBuffer packet2 = packet.duplicate();
PaketData pdata = parsePacket(packet);
pdata.printValues();
//debugPacket(packet2);
//serverIP = pdata.destAddr//getDestinationIP(packet);
port = pdata.destPort; //getDestinationPort(packet, getHeaderLength(packet));
Log.d(TAG, "prot:" + pdata.prot);
if (pdata.prot == 17) {
Log.d(TAG, "udp");
//socketadd = new InetSocketAddress(pdata.destAddr, port);
DatagramSocket s = new DatagramSocket();
byte[] data = new byte[pdata.data.capacity()];
pdata.data.get(data);
if (shouldBeBlocked(pdata.destAddr)) {
sendResult("blocked: " + pdata.destAddr.toString() + ":" + pdata.destPort);
} else {
sendResult(pdata.destAddr.toString() + ":" + pdata.destPort);
if (protect(s)) {
DatagramPacket p = new DatagramPacket(data, data.length, pdata.destAddr, port);
Log.d("SendOutTest", "try sending");
try {
s.send(p);
Log.d("SendOutTest", "send out!");
} catch (IOException e) {
e.printStackTrace();
}
Log.d("SendOutTest", "message:");
} else {
Log.d("SendOutTest", "protection of socket failed!");
}
}
}
if (pdata.prot == 6) {
Log.d(TAG, "tcp");
byte[] data = new byte[pdata.data.capacity()];
pdata.data.get(data);
try {
Socket s = new Socket();
s.connect(new InetSocketAddress(pdata.destAddr, pdata.destPort), 10000);
if (shouldBeBlocked(pdata.destAddr)) {
sendResult("blocked: " + pdata.destAddr.toString() + ":" + pdata.destPort);
} else {
sendResult(pdata.destAddr.toString() + ":" + pdata.destPort);
if (protect(s)) {
Log.d(TAG, "try sending");
try {
OutputStream outStream = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(outStream);
int dataLength = data.length;
dos.writeInt(dataLength);
if (dataLength > 0) {
dos.write(data, 0, dataLength);
}
Log.d(TAG, "send out!");
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "message:");
} else {
Log.d(TAG, "protection of socket failed!");
}
}
} catch (ConnectException ce) {
System.out.println("Connection timed out to: " + pdata.destAddr + ":" + pdata.destPort);
ce.printStackTrace();
} catch (IOException ioe) {
System.out.println("Couldn't get data from: " + pdata.destAddr + ":" + pdata.destPort);
ioe.printStackTrace();
}
}
if (pdata.prot == 1) Log.d(TAG, "ICMP");
packet.clear();
}
/*
if (tcpSocket.isConnected()) {
InputStream inBuffer = tcpSocket.getInputStream();
byte[] bufferOutput = new byte[32767];
inBuffer.read(bufferOutput);
if (bufferOutput.length > 0) {
String recPacketString = new String(bufferOutput, 0, bufferOutput.length, "UTF-8");
Log.d(TAG, "recPacketString : " + recPacketString);
out.write(bufferOutput);
}
inBuffer.close();
}*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(TAG, "exception: " + e.toString());
ok = false;
}
}
}
它应该做的是截取数据包,检查数据包是否在黑名单上并将其路由到它的原始目的地。
问题是套接字s
保持超时,因为它无法连接到端口。有什么问题?
我知道它应该删除数据包的标题并将其放入一个新的数据包中再次发送,但我不知道它是否正在这样做(我没有这个级别的网络经验)
堆栈跟踪
java.net.SocketTimeoutException: failed to connect to /xx.xxx.xxx.xx (port xxx) after 10000ms
03-22 09:59:26.474 27330-28072/willem.com.vpn W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
03-22 09:59:26.474 27330-28072/willem.com.vpn W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
03-22 09:59:26.474 27330-28072/willem.com.vpn W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-22 09:59:26.474 27330-28072/willem.com.vpn W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
03-22 09:59:26.484 27330-28072/willem.com.vpn W/System.err﹕ at java.net.Socket.connect(Socket.java:833)
03-22 09:59:26.484 27330-28072/willem.com.vpn W/System.err﹕ at willem.com.vpn.VPN2.runVpnConnection(VPN2.java:199)
03-22 09:59:26.484 27330-28072/willem.com.vpn W/System.err﹕ at willem.com.vpn.VPN2.run(VPN2.java:93)
03-22 09:59:26.484 27330-28072/willem.com.vpn W/System.err﹕ at java.lang.Thread.run(Thread.java:841)