List of usage examples for java.net DatagramSocket close
public void close()
From source file:com.vuze.plugin.azVPN_PIA.Checker.java
private int handleBound(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; String s;//from w ww . jav a 2 s . c om boolean isGoodExistingBind = matchesVPNIP(bindIP); if (isGoodExistingBind) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(bindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_GOOD, "pia.bound.good", new String[] { "" + bindIP, niName }); vpnIP = bindIP; } else { addReply(sReply, CHAR_BAD, "pia.bound.bad", new String[] { "" + bindIP }); newStatusID = STATUS_ID_BAD; } try { // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows (in some cases), but on Mac returns a wildcard // address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("pia.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); char replyChar = ' '; if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (localAddress.equals(bindIP)) { replyChar = isGoodExistingBind ? CHAR_GOOD : CHAR_WARN; s += " " + texts.getLocalisedMessageText("pia.same.as.vuze"); } else { // Vuze is bound, default routing goes somewhere else // This is ok, since Vuze will not accept incoming from "somewhere else" // We'll warn, but not update the status id replyChar = CHAR_WARN; s += " " + texts.getLocalisedMessageText("pia.not.same"); if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } } addLiteralReply(sReply, replyChar + " " + s); if (!isGoodExistingBind && rebindNetworkInterface) { rebindNetworkInterface(networkInterface, localAddress, sReply); // Should we redo test? } } else { // Vuze is bound, default routing goes to somewhere else. // Probably network splitting replyChar = isGoodExistingBind ? CHAR_WARN : CHAR_BAD; if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } addLiteralReply(sReply, replyChar + " " + s); } } } catch (Throwable t) { t.printStackTrace(); } return newStatusID; }
From source file:com.vuze.plugin.azVPN_PIA.Checker.java
private int handleUnboundOrLoopback(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; InetAddress newBindIP = null; NetworkInterface newBindNetworkInterface = null; String s;/*from www . ja v a2 s .co m*/ if (bindIP.isAnyLocalAddress()) { addReply(sReply, CHAR_WARN, "pia.vuze.unbound"); } else { addReply(sReply, CHAR_BAD, "pia.vuze.loopback"); } try { NetworkAdmin networkAdmin = NetworkAdmin.getSingleton(); // Find a bindable address that starts with 10. InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses(); for (InetAddress bindableAddress : bindableAddresses) { if (matchesVPNIP(bindableAddress)) { newBindIP = bindableAddress; newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); addReply(sReply, CHAR_GOOD, "pia.found.bindable.vpn", new String[] { "" + newBindIP }); break; } } // Find a Network Interface that has an address that starts with 10. NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces(); boolean foundNIF = false; for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) { NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses(); for (NetworkAdminNetworkInterfaceAddress a : addresses) { InetAddress address = a.getAddress(); if (address instanceof Inet4Address) { if (matchesVPNIP(address)) { s = texts.getLocalisedMessageText("pia.possible.vpn", new String[] { "" + address, networkAdminInterface.getName() + " (" + networkAdminInterface.getDisplayName() + ")" }); if (newBindIP == null) { foundNIF = true; newBindIP = address; // Either one should work //newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); newBindNetworkInterface = NetworkInterface .getByName(networkAdminInterface.getName()); s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("pia.assuming.vpn"); } else if (address.equals(newBindIP)) { s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("pia.same.address"); foundNIF = true; } else { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + ". " + texts.getLocalisedMessageText("pia.not.same.address"); } addLiteralReply(sReply, s); if (rebindNetworkInterface) { // stops message below from being added, we'll rebind later foundNIF = true; } } } } } if (!foundNIF) { addReply(sReply, CHAR_BAD, "pia.interface.not.found"); } // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows, but on Mac returns a wildcard address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("pia.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (newBindIP == null) { newBindIP = localAddress; newBindNetworkInterface = networkInterface; s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("pia.assuming.vpn"); } else if (localAddress.equals(newBindIP)) { s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("pia.same.address"); } else { // Vuze not bound. We already found a boundable address, but it's not this one /* Possibly good case: * - Vuze: unbound * - Found Bindable: 10.100.1.6 * - Default Routing: 10.255.1.1 * -> Split network */ if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("pia.not.same.future.address") + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " " + texts.getLocalisedMessageText( "default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } else { s = CHAR_WARN + " " + s; if (!bindIP.isLoopbackAddress()) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } if (newBindIP == null && foundNIF) { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s += " " + texts .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "pia.nat.error", new String[] { e.toString() }); } if (newBindIP == null) { addReply(sReply, CHAR_BAD, "pia.vpn.ip.detect.fail"); return STATUS_ID_BAD; } rebindNetworkInterface(newBindNetworkInterface, newBindIP, sReply); return newStatusID; }
From source file:vitro.vgw.wsiadapter.WSIAdapterCoapHAI.java
private int dtnObservationRequest(Node node, Resource resource) throws VitroGatewayException, IOException { int requestMsgId = UNDEFINED_COAP_MESSAGE_ID; // TODO: ??? we will use the packetID as a message ID to return. String proxyAddress = getProxyAddress(node); if (proxyAddress != null) { String moteUriResource = ""; if (MoteResource.containsValue(resource)) { //moteUriResource += MoteResource.getMoteUriResource(resource); String theResourceName = MoteResource.getMoteUriResource(resource); if (theResourceName == null) { logger.error("unsupported resource"); return UNDEFINED_COAP_MESSAGE_ID; }//from w ww .j a v a 2s . c o m // FOR TCS adapter, we prefer the TEMPERATURE_TCS // FOR WLAB and HAI we prefer the TEMPERATURE_ALT // we do this check because the getMoteUriResource is making a reverse lookup in the hashmap (where two keys point to the same resource) if (theResourceName.compareToIgnoreCase(MoteResource.TEMPERATURE_TCS) == 0) { theResourceName = MoteResource.TEMPERATURE_ALT; } moteUriResource += theResourceName; int packetID = UNDEFINED_COAP_MESSAGE_ID; do { //while loop to avoid a packetID that exists in the array that is to be ignored! packetID = random.nextInt(65535) + 1; } while (timedOut_DTN_CoapMessageIDsList.contains(Integer.valueOf(packetID)) || packetID == UNDEFINED_COAP_MESSAGE_ID); String msgString = packetID + "#" + node.getId() + "#" + RESOURCE_REQ + "#" + moteUriResource; byte[] msgBytes = new byte[Constants.DTN_MESSAGE_SIZE]; msgBytes = msgString.getBytes(); DatagramPacket sendPacket = new DatagramPacket(msgBytes, msgBytes.length, InetAddress.getByName(proxyAddress), Constants.PROXY_UDPFORWARDER_PORT); DatagramSocket clientSocket = new DatagramSocket(); clientSocket.send(sendPacket); clientSocket.close(); requestMsgId = packetID; logger.info("Sent Request: " + msgString); } else { logger.warn("No resource mapping for Node " + node.getId() + " and Resource " + resource.getName()); throw new WSIAdapterException( "No resource mapping for Node " + node.getId() + " and Resource " + resource.getName()); } } else { logger.warn("No available proxy for Node " + node.getId() + " is found"); throw new WSIAdapterException("No available proxy for Node " + node.getId() + " is found"); } return requestMsgId; }
From source file:com.mobiperf.measurements.RRCTask.java
/** * Send a single packet of the size indicated and wait for a response. * //from www. jav a 2 s . com * After 7000 ms, time out and return a value of -1 (meaning no response). Otherwise, return the * time from when the packet was sent to when a response was returned by the echo server. * * @param serverAddr Echo server to calculate round trip * @param size size of packet to send in bytes * @param rcvSize size of packets sent from the echo server * @param port where the echo server is listening * @return The round trip time for the packet * @throws IOException */ public static long sendPacket(InetAddress serverAddr, int size, int rcvSize, int port) throws IOException { long startTime = 0; byte[] buf = new byte[size]; byte[] rcvBuf = new byte[rcvSize]; long dataConsumedThisTask = 0; DatagramSocket socket = new DatagramSocket(); DatagramPacket packetRcv = new DatagramPacket(rcvBuf, rcvBuf.length); dataConsumedThisTask += (size + rcvSize); DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, port); try { socket.setSoTimeout(7000); startTime = System.currentTimeMillis(); Logger.d("Sending packet, waiting for response "); socket.send(packet); socket.receive(packetRcv); } catch (SocketTimeoutException e) { Logger.d("Timed out, trying again"); socket.close(); return -1; } long endTime = System.currentTimeMillis(); Logger.d("Sending complete: " + endTime); incrementData(dataConsumedThisTask); return endTime - startTime; }
From source file:com.webobjects.monitor.application.HostsPage.java
private boolean hostMeetsMinimumVersion(InetAddress anAddress) { byte[] versionRequest; try {// ww w. java 2 s . co m versionRequest = ("womp://queryVersion").getBytes(CharEncoding.UTF_8); } catch (UnsupportedEncodingException uee) { versionRequest = ("womp://queryVersion").getBytes(); } DatagramPacket outgoingPacket = new DatagramPacket(versionRequest, versionRequest.length, anAddress, WOApplication.application().lifebeatDestinationPort()); byte[] mbuffer = new byte[1000]; DatagramPacket incomingPacket = new DatagramPacket(mbuffer, mbuffer.length); DatagramSocket socket = null; try { socket = new DatagramSocket(); socket.send(outgoingPacket); incomingPacket.setLength(mbuffer.length); socket.setSoTimeout(2000); socket.receive(incomingPacket); String reply = new String(incomingPacket.getData()); if (reply.startsWith("womp://replyVersion/")) { int lastIndex = reply.lastIndexOf(":webObjects"); lastIndex += 11; String version = reply.substring(lastIndex); if (version.equals("4.5")) { return false; } } else { return false; } } catch (InterruptedIOException iioe) { return true; } catch (SocketException se) { return true; } catch (Throwable e) { return false; } finally { if (socket != null) { socket.close(); } } return true; }
From source file:com.mobilyzer.measurements.RRCTask.java
/** * Send a single packet of the size indicated and wait for a response. * /*ww w .j a v a2 s . c o m*/ * After 7000 ms, time out and return a value of -1 (meaning no response). Otherwise, return the * time from when the packet was sent to when a response was returned by the echo server. * * @param serverAddr Echo server to calculate round trip * @param size size of packet to send in bytes * @param rcvSize size of packets sent from the echo server * @param port where the echo server is listening * @return The round trip time for the packet * @throws IOException */ public static long sendPacket(InetAddress serverAddr, int size, int rcvSize, int port) throws IOException { long startTime = 0; byte[] buf = new byte[size]; byte[] rcvBuf = new byte[rcvSize]; DatagramSocket socket = new DatagramSocket(); DatagramPacket packetRcv = new DatagramPacket(rcvBuf, rcvBuf.length); DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, port); try { socket.setSoTimeout(7000); startTime = System.currentTimeMillis(); Logger.d("Sending packet, waiting for response "); socket.send(packet); socket.receive(packetRcv); } catch (SocketTimeoutException e) { Logger.d("Timed out, trying again"); socket.close(); return -1; } long endTime = System.currentTimeMillis(); Logger.d("Sending complete: " + endTime); return endTime - startTime; }
From source file:com.vuze.plugin.azVPN_Helper.CheckerCommon.java
private final int handleFindBindingAddress(InetAddress currentBindIP, StringBuilder sReply, int numLoops) { if (currentBindIP == null) { addReply(sReply, CHAR_BAD, "!Bind IP null!", new String[] { "" + currentBindIP }); return STATUS_ID_BAD; }//ww w . j a v a 2s .co m int newStatusID = STATUS_ID_OK; Map<String, BindableInterface> mapBindableInterfaces = new HashMap<String, BindableInterface>(); BindableInterface newBind = null; String s; // The "Any" field is equivalent to 0.0.0.0 in dotted-quad notation, which is unbound. // "Loopback" is 127.0.0.1, which is bound when Vuze can't bind to // user specified interface (ie. kill switched) if (currentBindIP.isAnyLocalAddress()) { addReply(sReply, CHAR_WARN, "vpnhelper.vuze.unbound"); } else if (currentBindIP.isLoopbackAddress()) { addReply(sReply, CHAR_BAD, "vpnhelper.vuze.loopback"); } else { // bound boolean isGoodExistingBind = matchesVPNIP(currentBindIP, null); if (isGoodExistingBind) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetUtils.getByInetAddress(currentBindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_GOOD, "vpnhelper.bound.good", new String[] { "" + currentBindIP, niName }); vpnIP = currentBindIP; } else { addReply(sReply, CHAR_BAD, "vpnhelper.bound.bad", new String[] { "" + currentBindIP }); } } try { boolean foundExistingVPNIP = false; NetworkAdmin networkAdmin = NetworkAdmin.getSingleton(); // Find a bindable address that starts with 10. InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses(); for (InetAddress bindableAddress : bindableAddresses) { if (matchesVPNIP(bindableAddress, null)) { String hostAddress = bindableAddress.getHostAddress(); BindableInterface bi = mapBindableInterfaces.get(hostAddress); if (bi == null) { bi = new BindableInterface(bindableAddress, NetUtils.getByInetAddress(bindableAddress)); mapBindableInterfaces.put(hostAddress, bi); if (!foundExistingVPNIP && bindableAddress.equals(vpnIP)) { foundExistingVPNIP = true; } } } } // Find a Network Interface that has an address that starts with 10. NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces(); /* Test reverse * for (int i = 0; i < interfaces.length / 2; i++) { NetworkAdminNetworkInterface temp = interfaces[i]; interfaces[i] = interfaces[interfaces.length - i - 1]; interfaces[interfaces.length - i - 1] = temp; } /**/ for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) { NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses(); for (NetworkAdminNetworkInterfaceAddress a : addresses) { InetAddress address = a.getAddress(); if (address instanceof Inet4Address) { if (matchesVPNIP(address, null)) { String hostAddress = address.getHostAddress(); BindableInterface bi = mapBindableInterfaces.get(hostAddress); if (bi == null) { bi = new BindableInterface(address, NetUtils.getByName(networkAdminInterface.getName())); mapBindableInterfaces.put(hostAddress, bi); if (!foundExistingVPNIP && address.equals(vpnIP)) { foundExistingVPNIP = true; } } } } } } if (vpnIP != null && !foundExistingVPNIP) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetUtils.getByInetAddress(currentBindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_WARN, "vpnhelper.existing.not.found", new String[] { "" + currentBindIP, niName }); if (numLoops == 0) { try { Field fldLastNICheck = NetUtils.class.getDeclaredField("last_ni_check"); fldLastNICheck.setAccessible(true); fldLastNICheck.set(null, Long.valueOf(-1)); return handleFindBindingAddress(currentBindIP, sReply, ++numLoops); } catch (Throwable t) { t.printStackTrace(); } } } BindableInterface[] array = mapBindableInterfaces.values().toArray(new BindableInterface[0]); Arrays.sort(array); for (BindableInterface bi : array) { if (!bi.isValidPrefixLength(minSubnetMaskBitCount)) { addReply(sReply, CHAR_WARN, "vpnhelper.submask.too.broad", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")", "" + bi.networkPrefixLength, "" + minSubnetMaskBitCount }); } else if (bi.canReach) { addReply(sReply, CHAR_GOOD, "vpnhelper.found.bindable.vpn", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")" }); } else { addReply(sReply, CHAR_WARN, "vpnhelper.not.reachable", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")" }); } PluginVPNHelper.log("subnet: " + bi.networkPrefixLength + "; Score: " + bi.score); } newBind = array.length > 0 && array[0].canReach && array[0].isValidPrefixLength(minSubnetMaskBitCount) ? array[0] : null; InetAddress localAddress = null; // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows, but on Mac returns a wildcard address DatagramSocket socket = new DatagramSocket(); try { socket.connect(testSocketAddress, 0); localAddress = socket.getLocalAddress(); } finally { socket.close(); } if (localAddress != null && !localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetUtils.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("vpnhelper.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress, networkInterface)) { if (newBind == null) { int networkPrefixLength = getNetworkPrefixLength(networkInterface, localAddress); if (networkPrefixLength >= 0 && networkPrefixLength < minSubnetMaskBitCount) { s = null; addReply(sReply, CHAR_WARN, "vpnhelper.nonvuze.submask.too.broad", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")", "" + networkPrefixLength, "" + minSubnetMaskBitCount }); } else if (!canReach(localAddress)) { addReply(sReply, CHAR_WARN, "vpnhelper.not.reachable", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); } else { newBind = new BindableInterface(localAddress, networkInterface); s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.assuming.vpn"); } } else if (localAddress.equals(newBind.address)) { s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.same.address"); } else { // Vuze not bound. We already found a boundable address, but it's not this one /* Possibly good case: * - Vuze: unbound * - Found Bindable: 10.100.1.6 * - Default Routing: 10.255.1.1 * -> Split network */ if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.not.same.future.address") + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " " + texts.getLocalisedMessageText( "default.routing.not.vpn.network.splitting.unbound"); } if (s != null) { addLiteralReply(sReply, s); } } else { s = CHAR_WARN + " " + s; if (!currentBindIP.isLoopbackAddress()) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } if (newBind == null) { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s += " " + texts .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "vpnhelper.nat.error", new String[] { e.toString() }); } if (newBind == null) { addReply(sReply, CHAR_BAD, "vpnhelper.vpn.ip.detect.fail"); String configBindIP = config.getCoreStringParameter(PluginConfig.CORE_PARAM_STRING_LOCAL_BIND_IP); if (configBindIP != null && configBindIP.length() > 0) { addReply(sReply, CHAR_WARN, "vpnhelper" + (currentBindIP.isLoopbackAddress() ? ".existing.bind.kept.loopback" : ".existing.bind.kept"), new String[] { configBindIP }); if (currentBindIP.isLoopbackAddress()) { if (numLoops == 0) { try { Field fldLastNICheck = NetUtils.class.getDeclaredField("last_ni_check"); fldLastNICheck.setAccessible(true); fldLastNICheck.set(null, Long.valueOf(-1)); return handleFindBindingAddress(currentBindIP, sReply, ++numLoops); } catch (Throwable t) { t.printStackTrace(); } } } } return STATUS_ID_BAD; } rebindNetworkInterface(newBind.networkInterface, newBind.address, sReply); return newStatusID; }
From source file:com.vuze.plugin.azVPN_Air.Checker.java
private int handleBound(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; String s;/*from w ww . j a v a 2 s .co m*/ boolean isGoodExistingBind = matchesVPNIP(bindIP); if (isGoodExistingBind) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(bindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_GOOD, "airvpn.bound.good", new String[] { "" + bindIP, niName }); vpnIP = bindIP; } else { addReply(sReply, CHAR_BAD, "airvpn.bound.bad", new String[] { "" + bindIP }); newStatusID = STATUS_ID_BAD; } try { // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows (in some cases), but on Mac returns a wildcard // address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("airvpn.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); char replyChar = ' '; if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (localAddress.equals(bindIP)) { replyChar = isGoodExistingBind ? CHAR_GOOD : CHAR_WARN; s += " " + texts.getLocalisedMessageText("airvpn.same.as.vuze"); } else { // Vuze is bound, default routing goes somewhere else // This is ok, since Vuze will not accept incoming from "somewhere else" // We'll warn, but not update the status id replyChar = CHAR_WARN; s += " " + texts.getLocalisedMessageText("airvpn.not.same"); if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } } addLiteralReply(sReply, replyChar + " " + s); if (!isGoodExistingBind && rebindNetworkInterface) { rebindNetworkInterface(networkInterface, localAddress, sReply); // Should we redo test? } } else { // Vuze is bound, default routing goes to somewhere else. // Probably network splitting replyChar = isGoodExistingBind ? CHAR_WARN : CHAR_BAD; if (isGoodExistingBind) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } addLiteralReply(sReply, replyChar + " " + s); } } } catch (Throwable t) { t.printStackTrace(); } return newStatusID; }
From source file:com.kecso.socket.ServerSocketControl.java
@Override public void run() { DatagramSocket sock = null; try {// www. j a v a 2 s .c om sock = new DatagramSocket(8888); sock.setSoTimeout(1000); byte[] buffer = new byte[65536]; DatagramPacket incoming = new DatagramPacket(buffer, buffer.length); while (!Thread.currentThread().isInterrupted()) { try { sock.receive(incoming); byte[] data = incoming.getData(); this.udpMessage = SerializationUtils.deserialize(data); byte[] response = SerializationUtils.serialize( this.output != null ? new UdpResponse((float) output.getSpeed(), (float) output.getVerticalSpeed(), (float) output.getAltitude(), (float) output.getRpm()) : null); DatagramPacket dp = new DatagramPacket(response, response.length, incoming.getAddress(), incoming.getPort()); sock.send(dp); } catch (SocketTimeoutException e) { } } } catch (Exception e) { System.err.println("IOException " + e); } finally { if (sock != null) { sock.close(); } } }
From source file:com.vuze.plugin.azVPN_Air.Checker.java
private int handleUnboundOrLoopback(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; InetAddress newBindIP = null; NetworkInterface newBindNetworkInterface = null; String s;/*ww w. ja va 2s . c o m*/ if (bindIP.isAnyLocalAddress()) { addReply(sReply, CHAR_WARN, "airvpn.vuze.unbound"); } else { addReply(sReply, CHAR_BAD, "airvpn.vuze.loopback"); } try { NetworkAdmin networkAdmin = NetworkAdmin.getSingleton(); // Find a bindable address that starts with 10. InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses(); for (InetAddress bindableAddress : bindableAddresses) { if (matchesVPNIP(bindableAddress)) { newBindIP = bindableAddress; newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); addReply(sReply, CHAR_GOOD, "airvpn.found.bindable.vpn", new String[] { "" + newBindIP }); break; } } // Find a Network Interface that has an address that starts with 10. NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces(); boolean foundNIF = false; for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) { NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses(); for (NetworkAdminNetworkInterfaceAddress a : addresses) { InetAddress address = a.getAddress(); if (address instanceof Inet4Address) { if (matchesVPNIP(address)) { s = texts.getLocalisedMessageText("airvpn.possible.vpn", new String[] { "" + address, networkAdminInterface.getName() + " (" + networkAdminInterface.getDisplayName() + ")" }); if (newBindIP == null) { foundNIF = true; newBindIP = address; // Either one should work //newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); newBindNetworkInterface = NetworkInterface .getByName(networkAdminInterface.getName()); s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("airvpn.assuming.vpn"); } else if (address.equals(newBindIP)) { s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("airvpn.same.address"); foundNIF = true; } else { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + ". " + texts.getLocalisedMessageText("airvpn.not.same.address"); } addLiteralReply(sReply, s); if (rebindNetworkInterface) { // stops message below from being added, we'll rebind later foundNIF = true; } } } } } if (!foundNIF) { addReply(sReply, CHAR_BAD, "airvpn.interface.not.found"); } // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows, but on Mac returns a wildcard address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("airvpn.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (newBindIP == null) { newBindIP = localAddress; newBindNetworkInterface = networkInterface; s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.assuming.vpn"); } else if (localAddress.equals(newBindIP)) { s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.same.address"); } else { // Vuze not bound. We already found a boundable address, but it's not this one /* Possibly good case: * - Vuze: unbound * - Found Bindable: 10.100.1.6 * - Default Routing: 10.255.1.1 * -> Split network */ if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("airvpn.not.same.future.address") + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " " + texts.getLocalisedMessageText( "default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } else { s = CHAR_WARN + " " + s; if (!bindIP.isLoopbackAddress()) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } if (newBindIP == null && foundNIF) { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s += " " + texts .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "airvpn.nat.error", new String[] { e.toString() }); } if (newBindIP == null) { addReply(sReply, CHAR_BAD, "airvpn.vpn.ip.detect.fail"); return STATUS_ID_BAD; } rebindNetworkInterface(newBindNetworkInterface, newBindIP, sReply); return newStatusID; }