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(localAddress, 0)); sendSocket.connect(InetAddress.getByName(stunServer), stunPort); 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.debug("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()); receiveMH.parseAttributes(receive.getData()); } ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode); if (ec != null) { di.setError(ec.getResponseCode(), ec.getReason()); LOGGER.debug("Message header contains an Errorcode message attribute."); return; } if (nodeNatted) { di.setRestrictedCone(); LOGGER.debug("Node is behind a restricted NAT."); return; } } catch (SocketTimeoutException ste) { if (timeSinceFirstTransmission < timeoutTest) { LOGGER.debug("Test 3: Socket timeout while receiving the response."); timeSinceFirstTransmission += timeout; int timeoutAddValue = (timeSinceFirstTransmission * 2); if (timeoutAddValue > timeoutSecond) // 1600) timeoutAddValue = timeoutSecond; timeout = timeoutAddValue; } else { LOGGER.debug( "Test 3: Socket timeout while receiving the response. Maximum retry limit exceed. Give up."); di.setPortRestrictedCone(); LOGGER.debug("Node is behind a port restricted NAT."); return; } } } }
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(localAddress, 0)); sendSocket.connect(InetAddress.getByName(stunServer), stunPort); 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.debug("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()); receiveMH.parseAttributes(receive.getData()); } ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode); if (ec != null) { di.setError(ec.getResponseCode(), ec.getReason()); LOGGER.debug("Message header contains an Errorcode message attribute."); return false; } if (!nodeNatted) { di.setOpenAccess(); LOGGER.debug( "Node has open access to the Internet (or, at least the node is behind a full-cone NAT without translation)."); } else { di.setFullCone(); LOGGER.debug("Node is behind a full-cone NAT."); } return false; } catch (SocketTimeoutException ste) { if (timeSinceFirstTransmission < timeoutTest) { LOGGER.debug("Test 2: Socket timeout while receiving the response."); timeSinceFirstTransmission += timeout; int timeoutAddValue = (timeSinceFirstTransmission * 2); if (timeoutAddValue > timeoutSecond) // 1600) timeoutAddValue = timeoutSecond; timeout = timeoutAddValue; } else { LOGGER.debug( "Test 2: Socket timeout while receiving the response. Maximum retry limit exceed. Give up."); if (!nodeNatted) { di.setSymmetricUDPFirewall(); LOGGER.debug("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; } } } } }