Author: j16sdiz
Date: 2009-01-03 15:14:59 +0000 (Sat, 03 Jan 2009)
New Revision: 24902
Added:
trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest_.java
Removed:
trunk/plugins/JSTUN/de/javawi/jstun/test/BindingLifetimeTest.java
trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest.java
trunk/plugins/JSTUN/de/javawi/jstun/test/demo/BindingLifetimeTestDemo.java
trunk/plugins/JSTUN/de/javawi/jstun/test/demo/DiscoveryTestDemo.java
Modified:
trunk/plugins/JSTUN/JSTUN.java
Log:
workaround for JSTUN build. We should move the JUnit test to somewhere more
sensible.
Modified: trunk/plugins/JSTUN/JSTUN.java
===================================================================
--- trunk/plugins/JSTUN/JSTUN.java 2009-01-03 14:55:33 UTC (rev 24901)
+++ trunk/plugins/JSTUN/JSTUN.java 2009-01-03 15:14:59 UTC (rev 24902)
@@ -14,7 +14,7 @@
import org.tanukisoftware.wrapper.WrapperManager;
import plugins.JSTUN.de.javawi.jstun.test.DiscoveryInfo;
-import plugins.JSTUN.de.javawi.jstun.test.DiscoveryTest;
+import plugins.JSTUN.de.javawi.jstun.test.DiscoveryTest_;
import freenet.pluginmanager.DetectedIP;
import freenet.pluginmanager.FredPlugin;
import freenet.pluginmanager.FredPluginHTTP;
@@ -60,7 +60,7 @@
if(WrapperManager.hasShutdownHookBeenTriggered())
return null;
String stunServer = (String)
v.remove(r.nextInt(v.size()));
try {
- DiscoveryTest test = new
DiscoveryTest(iaddress, stunServer, 3478);
+ DiscoveryTest_ test = new
DiscoveryTest_(iaddress, stunServer, 3478);
// iphone-stun.freenet.de:3478
// larry.gloo.net:3478
// stun.xten.net:3478
Deleted: trunk/plugins/JSTUN/de/javawi/jstun/test/BindingLifetimeTest.java
===================================================================
--- trunk/plugins/JSTUN/de/javawi/jstun/test/BindingLifetimeTest.java
2009-01-03 14:55:33 UTC (rev 24901)
+++ trunk/plugins/JSTUN/de/javawi/jstun/test/BindingLifetimeTest.java
2009-01-03 15:14:59 UTC (rev 24902)
@@ -1,199 +0,0 @@
-/*
- * This file is part of JSTUN.
- *
- * Copyright (c) 2005 Thomas King <[email protected]>
- *
- * JSTUN is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * JSTUN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with JSTUN; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package plugins.JSTUN.de.javawi.jstun.test;
-
-import java.util.logging.*;
-import java.util.*;
-import java.io.*;
-import java.net.*;
-
-import plugins.JSTUN.de.javawi.jstun.attribute.*;
-import plugins.JSTUN.de.javawi.jstun.header.*;
-import plugins.JSTUN.de.javawi.jstun.util.UtilityException;
-
-public class BindingLifetimeTest {
- private static Logger logger =
Logger.getLogger("de.javawi.stun.test.BindingLifetimeTest");
- String stunServer;
- int port;
- int timeout = 300; //ms
- MappedAddress ma;
- Timer timer;
- DatagramSocket initialSocket;
-
- // start value for binary search - should be carefully choosen
- int upperBinarySearchLifetime = 345000; // ms
- int lowerBinarySearchLifetime = 0;
- int binarySearchLifetime = ( upperBinarySearchLifetime +
lowerBinarySearchLifetime ) / 2;
-
- // lifetime value
- int lifetime = -1; // -1 means undefined.
- boolean completed = false;
-
- public BindingLifetimeTest(String stunServer, int port) {
- super();
- this.stunServer = stunServer;
- this.port = port;
- timer = new Timer(true);
- }
-
- public void test() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException {
- initialSocket = new DatagramSocket();
- initialSocket.connect(InetAddress.getByName(stunServer), port);
- initialSocket.setSoTimeout(timeout);
-
- if (BindingCommunicationInitialSocket()) {
- return;
- }
- BindingLifetimeTask task = new BindingLifetimeTask();
- timer.schedule(task, binarySearchLifetime);
- logger.finer("Timer scheduled initially: " +
binarySearchLifetime + '.');
- }
-
- private boolean BindingCommunicationInitialSocket() throws
UtilityException, IOException, MessageHeaderParsingException,
MessageAttributeParsingException {
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
- ChangeRequest changeRequest = new ChangeRequest();
- sendMH.addMessageAttribute(changeRequest);
- byte[] data = sendMH.getBytes();
-
- DatagramPacket send = new DatagramPacket(data, data.length,
InetAddress.getByName(stunServer), port);
- initialSocket.send(send);
- logger.finer("Binding Request sent.");
-
- MessageHeader receiveMH = new MessageHeader();
- while (!(receiveMH.equalTransactionID(sendMH))) {
- DatagramPacket receive = new DatagramPacket(new
byte[200], 200);
- initialSocket.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
- ma = (MappedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- logger.config("Message header contains errorcode
message attribute.");
- return true;
- }
- if (ma == null) {
- logger.config("Response does not contain a mapped
address message attribute.");
- return true;
- }
- return false;
- }
-
- public int getLifetime() {
- return lifetime;
- }
-
- public boolean isCompleted() {
- return completed;
- }
-
- public void setUpperBinarySearchLifetime(int upperBinarySearchLifetime)
{
- this.upperBinarySearchLifetime = upperBinarySearchLifetime;
- binarySearchLifetime = ( upperBinarySearchLifetime +
lowerBinarySearchLifetime ) / 2;
- }
-
- class BindingLifetimeTask extends TimerTask {
-
- public BindingLifetimeTask() {
- super();
- }
-
- public void run() {
- try {
- lifetimeQuery();
- } catch (Exception e) {
- logger.config("Unhandled Exception.
BindLifetimeTasks stopped.");
- e.printStackTrace();
- }
- }
-
- public void lifetimeQuery() throws UtilityException,
MessageAttributeException, MessageHeaderParsingException,
MessageAttributeParsingException, IOException {
- try {
- DatagramSocket socket = new DatagramSocket();
-
socket.connect(InetAddress.getByName(stunServer), port);
- socket.setSoTimeout(timeout);
-
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
- ChangeRequest changeRequest = new
ChangeRequest();
- ResponseAddress responseAddress = new
ResponseAddress();
- responseAddress.setAddress(ma.getAddress());
- responseAddress.setPort(ma.getPort());
- sendMH.addMessageAttribute(changeRequest);
- sendMH.addMessageAttribute(responseAddress);
- byte[] data = sendMH.getBytes();
-
- DatagramPacket send = new DatagramPacket(data,
data.length, InetAddress.getByName(stunServer), port);
- socket.send(send);
- logger.finer("Binding Request sent.");
-
- MessageHeader receiveMH = new MessageHeader();
- while (!(receiveMH.equalTransactionID(sendMH)))
{
- DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
- initialSocket.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- logger.config("Message header contains
errorcode message attribute.");
- return;
- }
- logger.finer("Binding Response received.");
- if (upperBinarySearchLifetime ==
(lowerBinarySearchLifetime + 1)) {
- logger.config("BindingLifetimeTest
completed. UDP binding lifetime: " + binarySearchLifetime + '.');
- completed = true;
- return;
- }
- lifetime = binarySearchLifetime;
- logger.finer("Lifetime update: " + lifetime +
'.');
- lowerBinarySearchLifetime =
binarySearchLifetime;
- binarySearchLifetime = (
upperBinarySearchLifetime + lowerBinarySearchLifetime ) / 2;
- if (binarySearchLifetime > 0) {
- BindingLifetimeTask task = new
BindingLifetimeTask();
- timer.schedule(task,
binarySearchLifetime);
- logger.finer("Timer scheduled: " +
binarySearchLifetime + '.');
- } else {
- completed = true;
- }
- } catch (SocketTimeoutException ste) {
- logger.finest("Read operation at query socket
timeout.");
- if (upperBinarySearchLifetime ==
(lowerBinarySearchLifetime + 1)) {
- logger.config("BindingLifetimeTest
completed. UDP binding lifetime: " + binarySearchLifetime + '.');
- completed = true;
- return;
- }
- upperBinarySearchLifetime =
binarySearchLifetime;
- binarySearchLifetime = (
upperBinarySearchLifetime + lowerBinarySearchLifetime ) / 2;
- if (binarySearchLifetime > 0) {
- if
(BindingCommunicationInitialSocket()) {
- return;
- }
- BindingLifetimeTask task = new
BindingLifetimeTask();
- timer.schedule(task,
binarySearchLifetime);
- logger.finer("Timer scheduled: " +
binarySearchLifetime + '.');
- } else {
- completed = true;
- }
- }
- }
- }
-}
-
Deleted: trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest.java
===================================================================
--- trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest.java 2009-01-03
14:55:33 UTC (rev 24901)
+++ trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest.java 2009-01-03
15:14:59 UTC (rev 24902)
@@ -1,352 +0,0 @@
-/*
- * This file is part of JSTUN.
- *
- * Copyright (c) 2005 Thomas King <[email protected]>
- *
- * JSTUN is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * JSTUN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with JSTUN; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package plugins.JSTUN.de.javawi.jstun.test;
-
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.DatagramSocket;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.SocketException;
-import java.net.SocketTimeoutException;
-import java.net.UnknownHostException;
-import java.util.logging.Logger;
-
-import plugins.JSTUN.de.javawi.jstun.attribute.ChangeRequest;
-import plugins.JSTUN.de.javawi.jstun.attribute.ChangedAddress;
-import plugins.JSTUN.de.javawi.jstun.attribute.ErrorCode;
-import plugins.JSTUN.de.javawi.jstun.attribute.MappedAddress;
-import plugins.JSTUN.de.javawi.jstun.attribute.MessageAttribute;
-import plugins.JSTUN.de.javawi.jstun.attribute.MessageAttributeException;
-import
plugins.JSTUN.de.javawi.jstun.attribute.MessageAttributeParsingException;
-import plugins.JSTUN.de.javawi.jstun.header.MessageHeader;
-import plugins.JSTUN.de.javawi.jstun.header.MessageHeaderParsingException;
-import plugins.JSTUN.de.javawi.jstun.util.UtilityException;
-
-public class DiscoveryTest {
- private static Logger logger =
Logger.getLogger("de.javawi.stun.test.DiscoveryTest");
- InetAddress iaddress;
- String stunServer;
- int port;
- int timeoutInitValue = 300; //ms
- MappedAddress ma = null;
- ChangedAddress ca = null;
- boolean nodeNatted = true;
- DatagramSocket socketTest1 = null;
- DiscoveryInfo di = null;
-
- public DiscoveryTest(InetAddress iaddress , String stunServer, int
port) {
- super();
- this.iaddress = iaddress;
- this.stunServer = stunServer;
- this.port = port;
- }
-
- public DiscoveryInfo test() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException{
- ma = null;
- ca = null;
- nodeNatted = true;
- socketTest1 = null;
- di = new DiscoveryInfo(iaddress);
-
- if (test1()) {
- if (test2()) {
- if (test1Redo()) {
- test3();
- }
- }
- }
-
- socketTest1.close();
-
- return di;
- }
-
- private boolean test1() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageHeaderParsingException {
- int timeSinceFirstTransmission = 0;
- int timeout = timeoutInitValue;
- while (true) {
- try {
- // Test 1 including response
- socketTest1 = new DatagramSocket(new
InetSocketAddress(iaddress, 0));
- socketTest1.setReuseAddress(true);
-
socketTest1.connect(InetAddress.getByName(stunServer), port);
- socketTest1.setSoTimeout(timeout);
-
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
-
- ChangeRequest changeRequest = new
ChangeRequest();
- sendMH.addMessageAttribute(changeRequest);
-
- byte[] data = sendMH.getBytes();
- DatagramPacket send = new DatagramPacket(data,
data.length);
- socketTest1.send(send);
- logger.finer("Test 1: Binding Request sent.");
-
- MessageHeader receiveMH = new MessageHeader();
- while (!(receiveMH.equalTransactionID(sendMH)))
{
- DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
- socketTest1.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
-
- ma = (MappedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
- ca = (ChangedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- di.setError(ec.getResponseCode(),
ec.getReason());
- logger.config("Message header contains
errorcode message attribute.");
- return false;
- }
- if ((ma == null) || (ca == null)) {
- di.setError(700, "The server is sending
incomplete response (Mapped Address and Changed Address message attributes are
missing). The client should not retry.");
- logger.config("Response does not
contain a mapped address or changed address message attribute.");
- return false;
- } else {
-
di.setPublicIP(ma.getAddress().getInetAddress());
- if ((ma.getPort() ==
socketTest1.getLocalPort()) &&
(ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress()))) {
- logger.fine("Node is not
natted.");
- nodeNatted = false;
- } else {
- logger.fine("Node is natted.");
- }
- return true;
- }
- } catch (SocketTimeoutException ste) {
- if (timeSinceFirstTransmission < 7900) {
- logger.finer("Test 1: Socket timeout
while receiving the response.");
- timeSinceFirstTransmission += timeout;
- int timeoutAddValue =
(timeSinceFirstTransmission * 2);
- if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
- timeout = timeoutAddValue;
- } else {
- // node is not capable of udp
communication
- logger.finer("Test 1: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
- di.setBlockedUDP();
- logger.fine("Node is not capable of udp
communication.");
- return false;
- }
- }
- }
- }
-
- private boolean test2() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException {
- int timeSinceFirstTransmission = 0;
- int timeout = timeoutInitValue;
- while (true) {
- try {
- // Test 2 including response
- DatagramSocket sendSocket = new
DatagramSocket(new InetSocketAddress(iaddress, 0));
-
sendSocket.connect(InetAddress.getByName(stunServer), port);
- sendSocket.setSoTimeout(timeout);
-
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
-
- ChangeRequest changeRequest = new
ChangeRequest();
- changeRequest.setChangeIP();
- changeRequest.setChangePort();
- sendMH.addMessageAttribute(changeRequest);
-
- byte[] data = sendMH.getBytes();
- DatagramPacket send = new DatagramPacket(data,
data.length);
- sendSocket.send(send);
- logger.finer("Test 2: Binding Request sent.");
-
- int localPort = sendSocket.getLocalPort();
- InetAddress localAddress =
sendSocket.getLocalAddress();
-
- sendSocket.close();
-
- DatagramSocket receiveSocket = new
DatagramSocket(localPort, localAddress);
-
receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
- receiveSocket.setSoTimeout(timeout);
-
- MessageHeader receiveMH = new MessageHeader();
- while(!(receiveMH.equalTransactionID(sendMH))) {
- DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
- receiveSocket.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- di.setError(ec.getResponseCode(),
ec.getReason());
- logger.config("Message header contains
errorcode message attribute.");
- return false;
- }
- if (!nodeNatted) {
- di.setOpenAccess();
- logger.fine("Node has open access to
the internet (or, at least the node is a full-cone NAT without translation).");
- } else {
- di.setFullCone();
- logger.fine("Node is behind a full-cone
NAT.");
- }
- return false;
- } catch (SocketTimeoutException ste) {
- if (timeSinceFirstTransmission < 7900) {
- logger.finer("Test 2: Socket timeout
while receiving the response.");
- timeSinceFirstTransmission += timeout;
- int timeoutAddValue =
(timeSinceFirstTransmission * 2);
- if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
- timeout = timeoutAddValue;
- } else {
- logger.finer("Test 2: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
- if (!nodeNatted) {
- di.setSymmetricUDPFirewall();
- logger.fine("Node is behind a
symmetric udp firewall.");
- return false;
- } else {
- // not is natted
- // redo test 1 with address and
port as offered in the changed-address message attribute
- return true;
- }
- }
- }
- }
- }
-
- private boolean test1Redo() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageHeaderParsingException{
- int timeSinceFirstTransmission = 0;
- int timeout = timeoutInitValue;
- while (true) {
- // redo test 1 with address and port as offered in the
changed-address message attribute
- try {
- // Test 1 with changed port and address values
-
socketTest1.connect(ca.getAddress().getInetAddress(), ca.getPort());
- socketTest1.setSoTimeout(timeout);
-
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
-
- ChangeRequest changeRequest = new
ChangeRequest();
- sendMH.addMessageAttribute(changeRequest);
-
- byte[] data = sendMH.getBytes();
- DatagramPacket send = new DatagramPacket(data,
data.length);
- socketTest1.send(send);
- logger.finer("Test 1 redo with changed address:
Binding Request sent.");
-
- MessageHeader receiveMH = new MessageHeader();
- while (!(receiveMH.equalTransactionID(sendMH)))
{
- DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
- socketTest1.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
- MappedAddress ma2 = (MappedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- di.setError(ec.getResponseCode(),
ec.getReason());
- logger.config("Message header contains
errorcode message attribute.");
- return false;
- }
- if (ma2 == null) {
- di.setError(700, "The server is sending
incomplete response (Mapped Address message attribute is missing). The client
should not retry.");
- logger.config("Response does not
contain a mapped address message attribute.");
- return false;
- } else {
- if ((ma.getPort() != ma2.getPort()) ||
(!(ma.getAddress().getInetAddress().equals(ma2.getAddress().getInetAddress()))))
{
- di.setSymmetricCone();
- logger.fine("Node is behind a
symmetric NAT.");
- return false;
- }
- }
- return true;
- } catch (SocketTimeoutException ste2) {
- if (timeSinceFirstTransmission < 7900) {
- logger.config("Test 1 redo with changed
address: Socket timeout while receiving the response.");
- timeSinceFirstTransmission += timeout;
- int timeoutAddValue =
(timeSinceFirstTransmission * 2);
- if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
- timeout = timeoutAddValue;
- } else {
- //TODO: error handling here
- logger.config("Test 1 redo with changed
address: Socket timeout while receiving the response. Maximum retry limit
exceed. Give up.");
- return false;
- }
- }
- }
- }
-
- private void test3() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException {
- int timeSinceFirstTransmission = 0;
- int timeout = timeoutInitValue;
- while (true) {
- try {
- // Test 3 including response
- DatagramSocket sendSocket = new
DatagramSocket(new InetSocketAddress(iaddress, 0));
-
sendSocket.connect(InetAddress.getByName(stunServer), port);
- sendSocket.setSoTimeout(timeout);
-
- MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
- sendMH.generateTransactionID();
-
- ChangeRequest changeRequest = new
ChangeRequest();
- changeRequest.setChangePort();
- sendMH.addMessageAttribute(changeRequest);
-
- byte[] data = sendMH.getBytes();
- DatagramPacket send = new DatagramPacket(data,
data.length);
- sendSocket.send(send);
- logger.finer("Test 3: Binding Request sent.");
-
- int localPort = sendSocket.getLocalPort();
- InetAddress localAddress =
sendSocket.getLocalAddress();
-
- sendSocket.close();
-
- DatagramSocket receiveSocket = new
DatagramSocket(localPort, localAddress);
-
receiveSocket.connect(InetAddress.getByName(stunServer), ca.getPort());
- receiveSocket.setSoTimeout(timeout);
-
- MessageHeader receiveMH = new MessageHeader();
- while (!(receiveMH.equalTransactionID(sendMH)))
{
- DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
- receiveSocket.receive(receive);
- receiveMH =
MessageHeader.parseHeader(receive.getData());
- }
- ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
- if (ec != null) {
- di.setError(ec.getResponseCode(),
ec.getReason());
- logger.config("Message header contains
errorcode message attribute.");
- return;
- }
- if (nodeNatted) {
- di.setRestrictedCone();
- logger.fine("Node is behind a
restricted NAT.");
- }
- } catch (SocketTimeoutException ste) {
- if (timeSinceFirstTransmission < 7900) {
- logger.finer("Test 3: Socket timeout
while receiving the response.");
- timeSinceFirstTransmission += timeout;
- int timeoutAddValue =
(timeSinceFirstTransmission * 2);
- if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
- timeout = timeoutAddValue;
- } else {
- logger.finer("Test 3: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
- di.setPortRestrictedCone();
- logger.fine("Node is behind a port
restricted NAT.");
- return;
- }
- }
- }
- }
-}
Copied: trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest_.java (from rev
24901, trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest.java)
===================================================================
--- trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest_.java
(rev 0)
+++ trunk/plugins/JSTUN/de/javawi/jstun/test/DiscoveryTest_.java
2009-01-03 15:14:59 UTC (rev 24902)
@@ -0,0 +1,352 @@
+/*
+ * This file is part of JSTUN.
+ *
+ * Copyright (c) 2005 Thomas King <[email protected]>
+ *
+ * JSTUN is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * JSTUN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with JSTUN; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package plugins.JSTUN.de.javawi.jstun.test;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+import java.util.logging.Logger;
+
+import plugins.JSTUN.de.javawi.jstun.attribute.ChangeRequest;
+import plugins.JSTUN.de.javawi.jstun.attribute.ChangedAddress;
+import plugins.JSTUN.de.javawi.jstun.attribute.ErrorCode;
+import plugins.JSTUN.de.javawi.jstun.attribute.MappedAddress;
+import plugins.JSTUN.de.javawi.jstun.attribute.MessageAttribute;
+import plugins.JSTUN.de.javawi.jstun.attribute.MessageAttributeException;
+import
plugins.JSTUN.de.javawi.jstun.attribute.MessageAttributeParsingException;
+import plugins.JSTUN.de.javawi.jstun.header.MessageHeader;
+import plugins.JSTUN.de.javawi.jstun.header.MessageHeaderParsingException;
+import plugins.JSTUN.de.javawi.jstun.util.UtilityException;
+
+public class DiscoveryTest_ {
+ private static Logger logger =
Logger.getLogger("de.javawi.stun.test.DiscoveryTest");
+ InetAddress iaddress;
+ String stunServer;
+ int port;
+ int timeoutInitValue = 300; //ms
+ MappedAddress ma = null;
+ ChangedAddress ca = null;
+ boolean nodeNatted = true;
+ DatagramSocket socketTest1 = null;
+ DiscoveryInfo di = null;
+
+ public DiscoveryTest_(InetAddress iaddress , String stunServer, int
port) {
+ super();
+ this.iaddress = iaddress;
+ this.stunServer = stunServer;
+ this.port = port;
+ }
+
+ public DiscoveryInfo test() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException{
+ ma = null;
+ ca = null;
+ nodeNatted = true;
+ socketTest1 = null;
+ di = new DiscoveryInfo(iaddress);
+
+ if (test1()) {
+ if (test2()) {
+ if (test1Redo()) {
+ test3();
+ }
+ }
+ }
+
+ socketTest1.close();
+
+ return di;
+ }
+
+ private boolean test1() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageHeaderParsingException {
+ int timeSinceFirstTransmission = 0;
+ int timeout = timeoutInitValue;
+ while (true) {
+ try {
+ // Test 1 including response
+ socketTest1 = new DatagramSocket(new
InetSocketAddress(iaddress, 0));
+ socketTest1.setReuseAddress(true);
+
socketTest1.connect(InetAddress.getByName(stunServer), port);
+ socketTest1.setSoTimeout(timeout);
+
+ MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
+ sendMH.generateTransactionID();
+
+ ChangeRequest changeRequest = new
ChangeRequest();
+ sendMH.addMessageAttribute(changeRequest);
+
+ byte[] data = sendMH.getBytes();
+ DatagramPacket send = new DatagramPacket(data,
data.length);
+ socketTest1.send(send);
+ logger.finer("Test 1: Binding Request sent.");
+
+ MessageHeader receiveMH = new MessageHeader();
+ while (!(receiveMH.equalTransactionID(sendMH)))
{
+ DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
+ socketTest1.receive(receive);
+ receiveMH =
MessageHeader.parseHeader(receive.getData());
+ }
+
+ ma = (MappedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
+ ca = (ChangedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
+ ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
+ if (ec != null) {
+ di.setError(ec.getResponseCode(),
ec.getReason());
+ logger.config("Message header contains
errorcode message attribute.");
+ return false;
+ }
+ if ((ma == null) || (ca == null)) {
+ di.setError(700, "The server is sending
incomplete response (Mapped Address and Changed Address message attributes are
missing). The client should not retry.");
+ logger.config("Response does not
contain a mapped address or changed address message attribute.");
+ return false;
+ } else {
+
di.setPublicIP(ma.getAddress().getInetAddress());
+ if ((ma.getPort() ==
socketTest1.getLocalPort()) &&
(ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress()))) {
+ logger.fine("Node is not
natted.");
+ nodeNatted = false;
+ } else {
+ logger.fine("Node is natted.");
+ }
+ return true;
+ }
+ } catch (SocketTimeoutException ste) {
+ if (timeSinceFirstTransmission < 7900) {
+ logger.finer("Test 1: Socket timeout
while receiving the response.");
+ timeSinceFirstTransmission += timeout;
+ int timeoutAddValue =
(timeSinceFirstTransmission * 2);
+ if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
+ timeout = timeoutAddValue;
+ } else {
+ // node is not capable of udp
communication
+ logger.finer("Test 1: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
+ di.setBlockedUDP();
+ logger.fine("Node is not capable of udp
communication.");
+ return false;
+ }
+ }
+ }
+ }
+
+ private boolean test2() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException {
+ int timeSinceFirstTransmission = 0;
+ int timeout = timeoutInitValue;
+ while (true) {
+ try {
+ // Test 2 including response
+ DatagramSocket sendSocket = new
DatagramSocket(new InetSocketAddress(iaddress, 0));
+
sendSocket.connect(InetAddress.getByName(stunServer), port);
+ sendSocket.setSoTimeout(timeout);
+
+ MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
+ sendMH.generateTransactionID();
+
+ ChangeRequest changeRequest = new
ChangeRequest();
+ changeRequest.setChangeIP();
+ changeRequest.setChangePort();
+ sendMH.addMessageAttribute(changeRequest);
+
+ byte[] data = sendMH.getBytes();
+ DatagramPacket send = new DatagramPacket(data,
data.length);
+ sendSocket.send(send);
+ logger.finer("Test 2: Binding Request sent.");
+
+ int localPort = sendSocket.getLocalPort();
+ InetAddress localAddress =
sendSocket.getLocalAddress();
+
+ sendSocket.close();
+
+ DatagramSocket receiveSocket = new
DatagramSocket(localPort, localAddress);
+
receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
+ receiveSocket.setSoTimeout(timeout);
+
+ MessageHeader receiveMH = new MessageHeader();
+ while(!(receiveMH.equalTransactionID(sendMH))) {
+ DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
+ receiveSocket.receive(receive);
+ receiveMH =
MessageHeader.parseHeader(receive.getData());
+ }
+ ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
+ if (ec != null) {
+ di.setError(ec.getResponseCode(),
ec.getReason());
+ logger.config("Message header contains
errorcode message attribute.");
+ return false;
+ }
+ if (!nodeNatted) {
+ di.setOpenAccess();
+ logger.fine("Node has open access to
the internet (or, at least the node is a full-cone NAT without translation).");
+ } else {
+ di.setFullCone();
+ logger.fine("Node is behind a full-cone
NAT.");
+ }
+ return false;
+ } catch (SocketTimeoutException ste) {
+ if (timeSinceFirstTransmission < 7900) {
+ logger.finer("Test 2: Socket timeout
while receiving the response.");
+ timeSinceFirstTransmission += timeout;
+ int timeoutAddValue =
(timeSinceFirstTransmission * 2);
+ if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
+ timeout = timeoutAddValue;
+ } else {
+ logger.finer("Test 2: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
+ if (!nodeNatted) {
+ di.setSymmetricUDPFirewall();
+ logger.fine("Node is behind a
symmetric udp firewall.");
+ return false;
+ } else {
+ // not is natted
+ // redo test 1 with address and
port as offered in the changed-address message attribute
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ private boolean test1Redo() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageHeaderParsingException{
+ int timeSinceFirstTransmission = 0;
+ int timeout = timeoutInitValue;
+ while (true) {
+ // redo test 1 with address and port as offered in the
changed-address message attribute
+ try {
+ // Test 1 with changed port and address values
+
socketTest1.connect(ca.getAddress().getInetAddress(), ca.getPort());
+ socketTest1.setSoTimeout(timeout);
+
+ MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
+ sendMH.generateTransactionID();
+
+ ChangeRequest changeRequest = new
ChangeRequest();
+ sendMH.addMessageAttribute(changeRequest);
+
+ byte[] data = sendMH.getBytes();
+ DatagramPacket send = new DatagramPacket(data,
data.length);
+ socketTest1.send(send);
+ logger.finer("Test 1 redo with changed address:
Binding Request sent.");
+
+ MessageHeader receiveMH = new MessageHeader();
+ while (!(receiveMH.equalTransactionID(sendMH)))
{
+ DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
+ socketTest1.receive(receive);
+ receiveMH =
MessageHeader.parseHeader(receive.getData());
+ }
+ MappedAddress ma2 = (MappedAddress)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
+ ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
+ if (ec != null) {
+ di.setError(ec.getResponseCode(),
ec.getReason());
+ logger.config("Message header contains
errorcode message attribute.");
+ return false;
+ }
+ if (ma2 == null) {
+ di.setError(700, "The server is sending
incomplete response (Mapped Address message attribute is missing). The client
should not retry.");
+ logger.config("Response does not
contain a mapped address message attribute.");
+ return false;
+ } else {
+ if ((ma.getPort() != ma2.getPort()) ||
(!(ma.getAddress().getInetAddress().equals(ma2.getAddress().getInetAddress()))))
{
+ di.setSymmetricCone();
+ logger.fine("Node is behind a
symmetric NAT.");
+ return false;
+ }
+ }
+ return true;
+ } catch (SocketTimeoutException ste2) {
+ if (timeSinceFirstTransmission < 7900) {
+ logger.config("Test 1 redo with changed
address: Socket timeout while receiving the response.");
+ timeSinceFirstTransmission += timeout;
+ int timeoutAddValue =
(timeSinceFirstTransmission * 2);
+ if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
+ timeout = timeoutAddValue;
+ } else {
+ //TODO: error handling here
+ logger.config("Test 1 redo with changed
address: Socket timeout while receiving the response. Maximum retry limit
exceed. Give up.");
+ return false;
+ }
+ }
+ }
+ }
+
+ private void test3() throws UtilityException, SocketException,
UnknownHostException, IOException, MessageAttributeParsingException,
MessageAttributeException, MessageHeaderParsingException {
+ int timeSinceFirstTransmission = 0;
+ int timeout = timeoutInitValue;
+ while (true) {
+ try {
+ // Test 3 including response
+ DatagramSocket sendSocket = new
DatagramSocket(new InetSocketAddress(iaddress, 0));
+
sendSocket.connect(InetAddress.getByName(stunServer), port);
+ sendSocket.setSoTimeout(timeout);
+
+ MessageHeader sendMH = new
MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
+ sendMH.generateTransactionID();
+
+ ChangeRequest changeRequest = new
ChangeRequest();
+ changeRequest.setChangePort();
+ sendMH.addMessageAttribute(changeRequest);
+
+ byte[] data = sendMH.getBytes();
+ DatagramPacket send = new DatagramPacket(data,
data.length);
+ sendSocket.send(send);
+ logger.finer("Test 3: Binding Request sent.");
+
+ int localPort = sendSocket.getLocalPort();
+ InetAddress localAddress =
sendSocket.getLocalAddress();
+
+ sendSocket.close();
+
+ DatagramSocket receiveSocket = new
DatagramSocket(localPort, localAddress);
+
receiveSocket.connect(InetAddress.getByName(stunServer), ca.getPort());
+ receiveSocket.setSoTimeout(timeout);
+
+ MessageHeader receiveMH = new MessageHeader();
+ while (!(receiveMH.equalTransactionID(sendMH)))
{
+ DatagramPacket receive = new
DatagramPacket(new byte[200], 200);
+ receiveSocket.receive(receive);
+ receiveMH =
MessageHeader.parseHeader(receive.getData());
+ }
+ ErrorCode ec = (ErrorCode)
receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
+ if (ec != null) {
+ di.setError(ec.getResponseCode(),
ec.getReason());
+ logger.config("Message header contains
errorcode message attribute.");
+ return;
+ }
+ if (nodeNatted) {
+ di.setRestrictedCone();
+ logger.fine("Node is behind a
restricted NAT.");
+ }
+ } catch (SocketTimeoutException ste) {
+ if (timeSinceFirstTransmission < 7900) {
+ logger.finer("Test 3: Socket timeout
while receiving the response.");
+ timeSinceFirstTransmission += timeout;
+ int timeoutAddValue =
(timeSinceFirstTransmission * 2);
+ if (timeoutAddValue > 1600)
timeoutAddValue = 1600;
+ timeout = timeoutAddValue;
+ } else {
+ logger.finer("Test 3: Socket timeout
while receiving the response. Maximum retry limit exceed. Give up.");
+ di.setPortRestrictedCone();
+ logger.fine("Node is behind a port
restricted NAT.");
+ return;
+ }
+ }
+ }
+ }
+}
Deleted:
trunk/plugins/JSTUN/de/javawi/jstun/test/demo/BindingLifetimeTestDemo.java
===================================================================
--- trunk/plugins/JSTUN/de/javawi/jstun/test/demo/BindingLifetimeTestDemo.java
2009-01-03 14:55:33 UTC (rev 24901)
+++ trunk/plugins/JSTUN/de/javawi/jstun/test/demo/BindingLifetimeTestDemo.java
2009-01-03 15:14:59 UTC (rev 24902)
@@ -1,36 +0,0 @@
-package plugins.JSTUN.de.javawi.jstun.test.demo;
-
-import java.util.logging.FileHandler;
-import java.util.logging.Handler;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.logging.SimpleFormatter;
-
-import plugins.JSTUN.de.javawi.jstun.test.BindingLifetimeTest;
-
-public class BindingLifetimeTestDemo {
- public static void main(String[] args) {
- try {
- Handler fh = new FileHandler("logging.txt");
- fh.setFormatter(new SimpleFormatter());
- Logger.getLogger("de.javawi.stun").addHandler(fh);
- Logger.getLogger("de.javawi.stun").setLevel(Level.ALL);
- BindingLifetimeTest test = new
BindingLifetimeTest("iphone-stun.freenet.de", 3478);
- // iphone-stun.freenet.de:3478
- // larry.gloo.net:3478
- // stun.xten.net:3478
- test.test();
- boolean continueWhile = true;
- while(continueWhile) {
- Thread.sleep(5000);
- if (test.getLifetime() != -1) {
- System.out.println("Lifetime: " +
test.getLifetime() + " Finished: " + test.isCompleted());
- if (test.isCompleted()) continueWhile =
false;
- }
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
-}
Deleted: trunk/plugins/JSTUN/de/javawi/jstun/test/demo/DiscoveryTestDemo.java
===================================================================
--- trunk/plugins/JSTUN/de/javawi/jstun/test/demo/DiscoveryTestDemo.java
2009-01-03 14:55:33 UTC (rev 24901)
+++ trunk/plugins/JSTUN/de/javawi/jstun/test/demo/DiscoveryTestDemo.java
2009-01-03 15:14:59 UTC (rev 24902)
@@ -1,60 +0,0 @@
-package plugins.JSTUN.de.javawi.jstun.test.demo;
-
-import java.net.BindException;
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.util.Enumeration;
-import java.util.logging.FileHandler;
-import java.util.logging.Handler;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.logging.SimpleFormatter;
-
-import plugins.JSTUN.de.javawi.jstun.test.DiscoveryTest;
-
-public class DiscoveryTestDemo implements Runnable {
- InetAddress iaddress;
-
- public DiscoveryTestDemo(InetAddress iaddress) {
- this.iaddress = iaddress;
- }
-
- public void run() {
- try {
- DiscoveryTest test = new DiscoveryTest(iaddress,
"stun.xten.net", 3478);
- // iphone-stun.freenet.de:3478
- // larry.gloo.net:3478
- // stun.xten.net:3478
- System.out.println(test.test());
- } catch (BindException be) {
- System.out.println(iaddress.toString() + ": " +
be.getMessage());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- try {
- Handler fh = new FileHandler("logging.txt");
- fh.setFormatter(new SimpleFormatter());
- Logger.getLogger("de.javawi.stun").addHandler(fh);
- Logger.getLogger("de.javawi.stun").setLevel(Level.ALL);
-
- Enumeration<NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces();
- while (ifaces.hasMoreElements()) {
- NetworkInterface iface = ifaces.nextElement();
- Enumeration<InetAddress> iaddresses =
iface.getInetAddresses();
- while (iaddresses.hasMoreElements()) {
- InetAddress iaddress =
iaddresses.nextElement();
- if (!iaddress.isLoopbackAddress() &&
!iaddress.isLinkLocalAddress()) {
- Thread thread = new Thread(new
DiscoveryTestDemo(iaddress));
- thread.start();
- }
- }
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
-}
_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs