Search in sources :

Example 1 with DHTTransportUDPContact

use of com.biglybt.core.dht.transport.udp.DHTTransportUDPContact in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method receivePunch.

protected void receivePunch(DHTTransportUDPContact originator, Map request, Map response) {
    trace("received punch request");
    boolean ok = false;
    String target_str = new String((byte[]) request.get("target"));
    BindingData entry;
    try {
        server_mon.enter();
        entry = rendezvous_bindings.get(target_str);
    } finally {
        server_mon.exit();
    }
    String extra_log = "";
    if (entry != null) {
        if (entry.isOKToConnect()) {
            DHTTransportUDPContact target = entry.getContact();
            Map target_client_data = sendConnect(target, originator, (Map) request.get("client_data"));
            if (target_client_data != null) {
                response.put("client_data", target_client_data);
                response.put("port", new Long(target.getTransportAddress().getPort()));
                ok = true;
                entry.connectOK();
            } else {
                entry.connectFailed();
                extra_log = " - consec=" + entry.getConsecutiveFailCount();
            }
        } else {
            extra_log = " - ignored due to consec fails";
        }
    } else {
        extra_log = " - invalid rendezvous";
    }
    log("Rendezvous punch request from " + originator.getString() + " to " + target_str + " " + (ok ? "initiated" : "failed") + extra_log);
    if (ok) {
        punch_recv_ok++;
    } else {
        punch_recv_fail++;
    }
    response.put("ok", new Long(ok ? 1 : 0));
}
Also used : DHTTransportUDPContact(com.biglybt.core.dht.transport.udp.DHTTransportUDPContact)

Example 2 with DHTTransportUDPContact

use of com.biglybt.core.dht.transport.udp.DHTTransportUDPContact in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method destroy.

@Override
public void destroy() {
    try {
        server_mon.enter();
        Iterator<BindingData> it = rendezvous_bindings.values().iterator();
        while (it.hasNext()) {
            BindingData entry = it.next();
            final DHTTransportUDPContact contact = entry.getContact();
            new AEThread2("DHTNATPuncher:destroy", true) {

                @Override
                public void run() {
                    sendClose(contact);
                }
            }.start();
        }
        byte[] lpk = last_publish_key;
        List<DHTTransportContact> lws = last_write_set;
        if (lpk != null && lws != null) {
            log("Removing publish on closedown");
            DHTTransportContact[] contacts = lws.toArray(new DHTTransportContact[lws.size()]);
            dht.remove(contacts, lpk, "NAT Puncher destroy", new DHTOperationAdapter());
        }
    } catch (Throwable e) {
        log(e);
    } finally {
        server_mon.exit();
    }
}
Also used : DHTTransportUDPContact(com.biglybt.core.dht.transport.udp.DHTTransportUDPContact) DHTOperationAdapter(com.biglybt.core.dht.DHTOperationAdapter)

Example 3 with DHTTransportUDPContact

use of com.biglybt.core.dht.transport.udp.DHTTransportUDPContact in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method receiveConnect.

protected void receiveConnect(DHTTransportContact rendezvous, Map request, Map response) {
    trace("received connect request");
    boolean ok = false;
    // ensure that we've received this from our current rendezvous node
    DHTTransportContact rt = rendezvous_target;
    if (rt != null && rt.getAddress().equals(rendezvous.getAddress())) {
        final DHTTransportUDPContact target = decodeContact((byte[]) request.get("origin"));
        if (target != null) {
            int transport_port = 0;
            Long indirect_port = (Long) request.get("port");
            if (indirect_port != null) {
                transport_port = indirect_port.intValue();
            }
            if (transport_port != 0) {
                InetSocketAddress existing_address = target.getTransportAddress();
                if (transport_port != existing_address.getPort()) {
                    target.setTransportAddress(new InetSocketAddress(existing_address.getAddress(), transport_port));
                }
            }
            Map originator_client_data = (Map) request.get("client_data");
            boolean no_tunnel = false;
            if (originator_client_data == null) {
                originator_client_data = new HashMap();
            } else {
                no_tunnel = originator_client_data.get("_notunnel") != null;
            }
            if (no_tunnel) {
                log("Received message from " + target.getString());
            } else {
                log("Received connect request from " + target.getString());
                // ping the origin a few times to try and establish a tunnel
                UTTimerEvent event = timer.addPeriodicEvent(3000, new UTTimerEventPerformer() {

                    private int pings = 1;

                    @Override
                    public void perform(UTTimerEvent ev) {
                        if (pings > 3) {
                            ev.cancel();
                            return;
                        }
                        pings++;
                        if (sendTunnelInbound(target)) {
                            ev.cancel();
                        }
                    }
                });
                if (sendTunnelInbound(target)) {
                    event.cancel();
                }
            }
            Map client_data = adapter.getClientData(target.getTransportAddress(), originator_client_data);
            if (client_data == null) {
                client_data = new HashMap();
            }
            response.put("client_data", client_data);
            ok = true;
        } else {
            log("Connect request: failed to decode target");
        }
    } else {
        log("Connect request from invalid rendezvous: " + rendezvous.getString());
    }
    response.put("ok", new Long(ok ? 1 : 0));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DHTTransportUDPContact(com.biglybt.core.dht.transport.udp.DHTTransportUDPContact)

Example 4 with DHTTransportUDPContact

use of com.biglybt.core.dht.transport.udp.DHTTransportUDPContact in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method punch.

@Override
public Map punch(String reason, InetSocketAddress[] target, DHTTransportContact[] rendezvous_used, Map originator_client_data) {
    try {
        DHTTransportUDP transport = (DHTTransportUDP) dht.getTransport();
        DHTTransportUDPContact contact = transport.importContact(target[0], transport.getMinimumProtocolVersion(), false);
        Map result = punch(reason, contact, rendezvous_used, originator_client_data);
        target[0] = contact.getTransportAddress();
        return (result);
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        return (null);
    }
}
Also used : DHTTransportUDP(com.biglybt.core.dht.transport.udp.DHTTransportUDP) DHTTransportUDPContact(com.biglybt.core.dht.transport.udp.DHTTransportUDPContact)

Example 5 with DHTTransportUDPContact

use of com.biglybt.core.dht.transport.udp.DHTTransportUDPContact in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method sendMessage.

@Override
public Map sendMessage(InetSocketAddress rendezvous, InetSocketAddress target, Map message) {
    try {
        DHTTransportUDP transport = (DHTTransportUDP) dht.getTransport();
        DHTTransportUDPContact rend_contact = transport.importContact(rendezvous, transport.getMinimumProtocolVersion(), false);
        DHTTransportUDPContact target_contact = transport.importContact(target, transport.getMinimumProtocolVersion(), false);
        Map result = sendPunch(rend_contact, target_contact, message, true);
        return (result);
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        return (null);
    }
}
Also used : DHTTransportUDP(com.biglybt.core.dht.transport.udp.DHTTransportUDP) DHTTransportUDPContact(com.biglybt.core.dht.transport.udp.DHTTransportUDPContact)

Aggregations

DHTTransportUDPContact (com.biglybt.core.dht.transport.udp.DHTTransportUDPContact)6 DHTTransportUDP (com.biglybt.core.dht.transport.udp.DHTTransportUDP)2 DHTOperationAdapter (com.biglybt.core.dht.DHTOperationAdapter)1 InetSocketAddress (java.net.InetSocketAddress)1