Search in sources :

Example 1 with PRUDPReleasablePacketHandler

use of com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler in project BiglyBT by BiglySoftware.

the class VersionCheckClient method executeUDP.

private Map executeUDP(Map data_to_send, InetAddress bind_ip, int bind_port, boolean v6) throws Exception {
    if (COConfigurationManager.getBooleanParameter("update.anonymous")) {
        throw (new Exception("UDP disabled for anonymous updates"));
    }
    if (v6 && !enable_v6) {
        throw (new Exception("IPv6 is disabled"));
    }
    String host = getHost(v6, UDP_SERVER_ADDRESS_V6, UDP_SERVER_ADDRESS_V4);
    PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler(bind_port);
    PRUDPPacketHandler packet_handler = handler.getHandler();
    long timeout = 5;
    Random random = new Random();
    try {
        Exception last_error = null;
        packet_handler.setExplicitBindAddress(bind_ip);
        for (int i = 0; i < 3; i++) {
            try {
                // connection ids for requests must always have their msb set...
                // apart from the original darn udp tracker spec....
                long connection_id = 0x8000000000000000L | random.nextLong();
                VersionCheckClientUDPRequest request_packet = new VersionCheckClientUDPRequest(connection_id);
                request_packet.setPayload(data_to_send);
                VersionCheckClientUDPReply reply_packet = (VersionCheckClientUDPReply) packet_handler.sendAndReceive(null, request_packet, new InetSocketAddress(host, UDP_SERVER_PORT), timeout);
                Map reply = reply_packet.getPayload();
                preProcessReply(reply, v6);
                return (reply);
            } catch (Exception e) {
                last_error = e;
                timeout = timeout * 2;
            }
        }
        if (last_error != null) {
            throw (last_error);
        }
        throw (new Exception("Timeout"));
    } finally {
        packet_handler.setExplicitBindAddress(null);
        handler.release();
    }
}
Also used : PRUDPReleasablePacketHandler(com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler) PRUDPPacketHandler(com.biglybt.net.udp.uc.PRUDPPacketHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with PRUDPReleasablePacketHandler

use of com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler in project BiglyBT by BiglySoftware.

the class PRUDPPacketHandlerFactoryImpl method getReleasableHandler.

public static PRUDPReleasablePacketHandler getReleasableHandler(int port, PRUDPRequestHandler request_handler) {
    final Integer f_port = new Integer(port);
    try {
        class_mon.enter();
        PRUDPPacketHandlerImpl receiver = (PRUDPPacketHandlerImpl) receiver_map.get(f_port);
        if (receiver == null) {
            receiver = new PRUDPPacketHandlerImpl(port, null, null);
            receiver_map.put(f_port, receiver);
        }
        if (request_handler != null) {
            receiver.setRequestHandler(request_handler);
        }
        final PRUDPPacketHandlerImpl f_receiver = receiver;
        final PRUDPReleasablePacketHandler rel = new PRUDPReleasablePacketHandler() {

            @Override
            public PRUDPPacketHandler getHandler() {
                return (f_receiver);
            }

            @Override
            public void release() {
                try {
                    class_mon.enter();
                    List l = (List) releasable_map.get(f_port);
                    if (l == null) {
                        Debug.out("hmm");
                    } else {
                        if (!l.remove(this)) {
                            Debug.out("hmm");
                        } else {
                            if (l.size() == 0) {
                                if (!non_releasable_set.contains(f_port)) {
                                    f_receiver.destroy();
                                    receiver_map.remove(f_port);
                                }
                                releasable_map.remove(f_port);
                            }
                        }
                    }
                } finally {
                    class_mon.exit();
                }
            }
        };
        List l = (List) releasable_map.get(f_port);
        if (l == null) {
            l = new ArrayList();
            releasable_map.put(f_port, l);
        }
        l.add(rel);
        if (l.size() > 1024) {
            Debug.out("things going wrong here");
        }
        return (rel);
    } finally {
        class_mon.exit();
    }
}
Also used : PRUDPReleasablePacketHandler(com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler)

Example 3 with PRUDPReleasablePacketHandler

use of com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler in project BiglyBT by BiglySoftware.

the class NetworkAdminUDPTester method testInbound.

@Override
public InetAddress testInbound(InetAddress bind_ip, int bind_port) throws NetworkAdminException {
    PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler(bind_port);
    PRUDPPacketHandler packet_handler = handler.getHandler();
    HashMap data_to_send = new HashMap();
    PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
    String upnp_str = null;
    if (pi_upnp != null) {
        UPnPPlugin upnp = (UPnPPlugin) pi_upnp.getPlugin();
        /*
			UPnPMapping mapping = upnp.getMapping( true, port );

			if ( mapping == null ) {

				new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );

				// give UPnP a chance to work

				try {
					Thread.sleep( 500 );

				}
				catch (Throwable e) {

					Debug.printStackTrace( e );
				}
			}
			*/
        UPnPPluginService[] services = upnp.getServices();
        if (services.length > 0) {
            upnp_str = "";
            for (int i = 0; i < services.length; i++) {
                UPnPPluginService service = services[i];
                upnp_str += (i == 0 ? "" : ",") + service.getInfo();
            }
        }
    }
    if (upnp_str != null) {
        data_to_send.put("upnp", upnp_str);
    }
    NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();
    String as = net_asn.getAS();
    String asn = net_asn.getASName();
    if (as.length() > 0) {
        data_to_send.put("as", as);
    }
    if (asn.length() > 0) {
        data_to_send.put("asn", asn);
    }
    data_to_send.put("locale", MessageText.getCurrentLocale().toString());
    Random random = new Random();
    data_to_send.put("id", new Long(random.nextLong()));
    try {
        packet_handler.setExplicitBindAddress(bind_ip);
        Throwable last_error = null;
        long timeout = 5000;
        long timeout_inc = 5000;
        try {
            for (int i = 0; i < 3; i++) {
                data_to_send.put("seq", new Long(i));
                try {
                    // connection ids for requests must always have their msb set...
                    // apart from the original darn udp tracker spec....
                    long connection_id = 0x8000000000000000L | random.nextLong();
                    NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                    request_packet.setPayload(data_to_send);
                    if (listener != null) {
                        listener.reportProgress("Sending outbound packet and waiting for reply probe (timeout=" + timeout + ")");
                    }
                    NetworkAdminNATUDPReply reply_packet = (NetworkAdminNATUDPReply) packet_handler.sendAndReceive(null, request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT), timeout, PRUDPPacketHandler.PRIORITY_IMMEDIATE);
                    Map reply = reply_packet.getPayload();
                    byte[] ip_bytes = (byte[]) reply.get("ip_address");
                    if (ip_bytes == null) {
                        throw (new NetworkAdminException("IP address missing in reply"));
                    }
                    byte[] reason = (byte[]) reply.get("reason");
                    if (reason != null) {
                        throw (new NetworkAdminException(new String(reason, "UTF8")));
                    }
                    return (InetAddress.getByAddress(ip_bytes));
                } catch (Throwable e) {
                    last_error = e;
                    timeout += timeout_inc;
                }
            }
            if (last_error != null) {
                throw (last_error);
            }
            throw (new NetworkAdminException("Timeout"));
        } finally {
            try {
                data_to_send.put("seq", new Long(99));
                long connection_id = 0x8000000000000000L | random.nextLong();
                NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                request_packet.setPayload(data_to_send);
                if (listener != null) {
                    listener.reportProgress("Sending completion event");
                }
                packet_handler.send(request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT));
            } catch (Throwable e) {
            }
        }
    } catch (NetworkAdminException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new NetworkAdminException("Inbound test failed", e));
    } finally {
        packet_handler.setExplicitBindAddress(null);
        handler.release();
    }
}
Also used : NetworkAdminException(com.biglybt.core.networkmanager.admin.NetworkAdminException) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) PluginInterface(com.biglybt.pif.PluginInterface) NetworkAdminASN(com.biglybt.core.networkmanager.admin.NetworkAdminASN) UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) PRUDPReleasablePacketHandler(com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler) Random(java.util.Random) PRUDPPacketHandler(com.biglybt.net.udp.uc.PRUDPPacketHandler) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PRUDPReleasablePacketHandler (com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler)3 PRUDPPacketHandler (com.biglybt.net.udp.uc.PRUDPPacketHandler)2 NetworkAdminASN (com.biglybt.core.networkmanager.admin.NetworkAdminASN)1 NetworkAdminException (com.biglybt.core.networkmanager.admin.NetworkAdminException)1 PluginInterface (com.biglybt.pif.PluginInterface)1 UPnPPlugin (com.biglybt.plugin.upnp.UPnPPlugin)1 UPnPPluginService (com.biglybt.plugin.upnp.UPnPPluginService)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1