Skip to content

Commit

Permalink
Implemented dht get_peers operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Nov 14, 2014
1 parent fb057be commit bc3de69
Show file tree
Hide file tree
Showing 13 changed files with 753 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/com/frostwire/jlibtorrent/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,8 @@ private static Map<Integer, CastAlertFunction> buildCastAlertTable() {
CAST_ALERT_METHOD(dht_put_alert.class, map);
CAST_ALERT_METHOD(i2p_alert.class, map);

CAST_ALERT_METHOD(dht_get_peers_reply_alert.class, map);

return Collections.unmodifiableMap(map);
}

Expand Down
5 changes: 5 additions & 0 deletions src/com/frostwire/jlibtorrent/TcpEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public String getAddress() {
public int getPort() {
return endp.port();
}

@Override
public String toString() {
return "tcp:" + endp.address() + ":" + endp.port();
}
}
1 change: 1 addition & 0 deletions src/com/frostwire/jlibtorrent/alerts/AlertType.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public enum AlertType {
DHT_PUT(dht_put_alert.alert_type),
DHT_MUTABLE_ITEM(dht_mutable_item_alert.alert_type),
DHT_IMMUTABLE_ITEM(dht_immutable_item_alert.alert_type),
DHT_GET_PEERS_REPLY_ALERT(dht_get_peers_reply_alert.alert_type),
UNKNOWN(-1),
TORRENT_PRIORITIZE(-2);

Expand Down
35 changes: 35 additions & 0 deletions src/com/frostwire/jlibtorrent/alerts/DhtGetPeersReplyAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.frostwire.jlibtorrent.alerts;

import com.frostwire.jlibtorrent.Sha1Hash;
import com.frostwire.jlibtorrent.TcpEndpoint;
import com.frostwire.jlibtorrent.swig.dht_get_peers_reply_alert;
import com.frostwire.jlibtorrent.swig.tcp_endpoint_vector;

import java.util.ArrayList;

/**
* @author gubatron
* @author aldenml
*/
public final class DhtGetPeersReplyAlert extends AbstractAlert<dht_get_peers_reply_alert> {

public DhtGetPeersReplyAlert(dht_get_peers_reply_alert alert) {
super(alert);
}

public Sha1Hash getInfoHash() {
return new Sha1Hash(alert.getInfo_hash());
}

public ArrayList<TcpEndpoint> getPeers() {

This comment has been minimized.

Copy link
@gubatron

gubatron Nov 15, 2014

Collaborator

TcpEndpoint?

how is that possible? isn't the whole thing in UDP?

This comment has been minimized.

Copy link
@aldenml

aldenml Nov 15, 2014

Author Collaborator

I know that it's misleading, but at the end is just an ip and port and it is the way is reported by libtorrent. The nodes/peers report the outgoing port in the announce. Let's send upd packets to these addresses.

This comment has been minimized.

Copy link
@gubatron

gubatron Nov 15, 2014

Collaborator

that was what i was thinking.

So question, the IP:PORT that comes there, in bittorrent land is supposed to be the "Peer" (as in Torrent Peer for the torrent being announced on, not dht node), If I remember correctly, the api announcement entry point dht.announce(sha1String) only allows us to put the hash there, but we cannot enter there our IP:PORT, so I assume the IP:PORT being announced is the one the DHT is listening on.

This I believe brings a problem, as I think I cannot place a custom UDP handler if the DHT is listening and processing packets on that port, but then again I might be wrong. I wonder if another thread in the same process space (I doubt another process) can listen to the same UDP port for incoming packets.

At the same time, I think this is not good, because it'd force me to do at least all the handshaking through a port that's meant for DHT traffic, so it'd be good to be able to specify also the information about the Peer (not dht node) when announcing.

I'll open CLion and read.

tcp_endpoint_vector v = alert.getPeers();
int size = (int) v.size();
ArrayList<TcpEndpoint> peers = new ArrayList<TcpEndpoint>(size);

for (int i = 0; i < size; i++) {
peers.add(new TcpEndpoint(v.get(i)));
}

return peers;
}
}
10 changes: 10 additions & 0 deletions src/com/frostwire/jlibtorrent/demo/DhtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.frostwire.jlibtorrent.AlertListener;
import com.frostwire.jlibtorrent.Session;
import com.frostwire.jlibtorrent.Sha1Hash;
import com.frostwire.jlibtorrent.TcpEndpoint;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.DhtBootstrapAlert;
import com.frostwire.jlibtorrent.alerts.DhtGetPeersReplyAlert;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/**
Expand Down Expand Up @@ -34,6 +37,13 @@ public void alert(Alert<?> alert) {
if (alert instanceof DhtBootstrapAlert) {
signal.countDown();
}

if (alert instanceof DhtGetPeersReplyAlert) {
ArrayList<TcpEndpoint> peers = ((DhtGetPeersReplyAlert) alert).getPeers();
for (int i = 0; i < peers.size(); i++) {
System.out.println(peers.get(i));
}
}
}
});

Expand Down
5 changes: 5 additions & 0 deletions src/com/frostwire/jlibtorrent/swig/alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ public static i2p_alert cast_to_i2p_alert(alert alert) {
return (cPtr == 0) ? null : new i2p_alert(cPtr, false);
}

public static dht_get_peers_reply_alert cast_to_dht_get_peers_reply_alert(alert alert) {
long cPtr = libtorrent_jni.alert_cast_to_dht_get_peers_reply_alert(alert.getCPtr(alert), alert);
return (cPtr == 0) ? null : new dht_get_peers_reply_alert(cPtr, false);
}

public enum category_t {
error_notification(libtorrent_jni.alert_error_notification_get()),
peer_notification(libtorrent_jni.alert_peer_notification_get()),
Expand Down
78 changes: 78 additions & 0 deletions src/com/frostwire/jlibtorrent/swig/dht_get_peers_reply_alert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 3.0.2
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */

package com.frostwire.jlibtorrent.swig;

public class dht_get_peers_reply_alert extends alert {
private long swigCPtr;

protected dht_get_peers_reply_alert(long cPtr, boolean cMemoryOwn) {
super(libtorrent_jni.dht_get_peers_reply_alert_SWIGUpcast(cPtr), cMemoryOwn);
swigCPtr = cPtr;
}

protected static long getCPtr(dht_get_peers_reply_alert obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}

protected void finalize() {
delete();
}

public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
libtorrent_jni.delete_dht_get_peers_reply_alert(swigCPtr);
}
swigCPtr = 0;
}
super.delete();
}

public dht_get_peers_reply_alert(sha1_hash ih, tcp_endpoint_vector v) {
this(libtorrent_jni.new_dht_get_peers_reply_alert(sha1_hash.getCPtr(ih), ih, tcp_endpoint_vector.getCPtr(v), v), true);
}

public int type() {
return libtorrent_jni.dht_get_peers_reply_alert_type(swigCPtr, this);
}

public int category() {
return libtorrent_jni.dht_get_peers_reply_alert_category(swigCPtr, this);
}

public String what() {
return libtorrent_jni.dht_get_peers_reply_alert_what(swigCPtr, this);
}

public String message() {
return libtorrent_jni.dht_get_peers_reply_alert_message(swigCPtr, this);
}

public void setInfo_hash(sha1_hash value) {
libtorrent_jni.dht_get_peers_reply_alert_info_hash_set(swigCPtr, this, sha1_hash.getCPtr(value), value);
}

public sha1_hash getInfo_hash() {
long cPtr = libtorrent_jni.dht_get_peers_reply_alert_info_hash_get(swigCPtr, this);
return (cPtr == 0) ? null : new sha1_hash(cPtr, false);
}

public void setPeers(tcp_endpoint_vector value) {
libtorrent_jni.dht_get_peers_reply_alert_peers_set(swigCPtr, this, tcp_endpoint_vector.getCPtr(value), value);
}

public tcp_endpoint_vector getPeers() {
long cPtr = libtorrent_jni.dht_get_peers_reply_alert_peers_get(swigCPtr, this);
return (cPtr == 0) ? null : new tcp_endpoint_vector(cPtr, false);
}

public final static int alert_type = libtorrent_jni.dht_get_peers_reply_alert_alert_type_get();
public final static int static_category = libtorrent_jni.dht_get_peers_reply_alert_static_category_get();
}
24 changes: 24 additions & 0 deletions src/com/frostwire/jlibtorrent/swig/libtorrent_jni.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ public class libtorrent_jni {
public final static native long peer_list_entry_vector_get(long jarg1, peer_list_entry_vector jarg1_, int jarg2);
public final static native void peer_list_entry_vector_set(long jarg1, peer_list_entry_vector jarg1_, int jarg2, long jarg3, peer_list_entry jarg3_);
public final static native void delete_peer_list_entry_vector(long jarg1);
public final static native long new_tcp_endpoint_vector();
public final static native long tcp_endpoint_vector_size(long jarg1, tcp_endpoint_vector jarg1_);
public final static native long tcp_endpoint_vector_capacity(long jarg1, tcp_endpoint_vector jarg1_);
public final static native void tcp_endpoint_vector_reserve(long jarg1, tcp_endpoint_vector jarg1_, long jarg2);
public final static native boolean tcp_endpoint_vector_isEmpty(long jarg1, tcp_endpoint_vector jarg1_);
public final static native void tcp_endpoint_vector_clear(long jarg1, tcp_endpoint_vector jarg1_);
public final static native void tcp_endpoint_vector_add(long jarg1, tcp_endpoint_vector jarg1_, long jarg2, tcp_endpoint jarg2_);
public final static native long tcp_endpoint_vector_get(long jarg1, tcp_endpoint_vector jarg1_, int jarg2);
public final static native void tcp_endpoint_vector_set(long jarg1, tcp_endpoint_vector jarg1_, int jarg2, long jarg3, tcp_endpoint jarg3_);
public final static native void delete_tcp_endpoint_vector(long jarg1);
public final static native long new_entry_list();
public final static native boolean entry_list_isEmpty(long jarg1, entry_list jarg1_);
public final static native long entry_list_size(long jarg1, entry_list jarg1_);
Expand Down Expand Up @@ -1577,6 +1587,7 @@ public class libtorrent_jni {
public final static native long alert_cast_to_dht_mutable_item_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_dht_put_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_i2p_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_dht_get_peers_reply_alert(long jarg1, alert jarg1_);
public final static native int torrent_alert_alert_type_get();
public final static native String torrent_alert_message(long jarg1, torrent_alert jarg1_);
public final static native void torrent_alert_handle_set(long jarg1, torrent_alert jarg1_, long jarg2, torrent_handle jarg2_);
Expand Down Expand Up @@ -2401,6 +2412,18 @@ public class libtorrent_jni {
public final static native void i2p_alert_error_set(long jarg1, i2p_alert jarg1_, long jarg2, error_code jarg2_);
public final static native long i2p_alert_error_get(long jarg1, i2p_alert jarg1_);
public final static native void delete_i2p_alert(long jarg1);
public final static native long new_dht_get_peers_reply_alert(long jarg1, sha1_hash jarg1_, long jarg2, tcp_endpoint_vector jarg2_);
public final static native int dht_get_peers_reply_alert_alert_type_get();
public final static native int dht_get_peers_reply_alert_type(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native int dht_get_peers_reply_alert_category(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native String dht_get_peers_reply_alert_what(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native int dht_get_peers_reply_alert_static_category_get();
public final static native String dht_get_peers_reply_alert_message(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native void dht_get_peers_reply_alert_info_hash_set(long jarg1, dht_get_peers_reply_alert jarg1_, long jarg2, sha1_hash jarg2_);
public final static native long dht_get_peers_reply_alert_info_hash_get(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native void dht_get_peers_reply_alert_peers_set(long jarg1, dht_get_peers_reply_alert jarg1_, long jarg2, tcp_endpoint_vector jarg2_);
public final static native long dht_get_peers_reply_alert_peers_get(long jarg1, dht_get_peers_reply_alert jarg1_);
public final static native void delete_dht_get_peers_reply_alert(long jarg1);
public final static native void cached_piece_info_piece_set(long jarg1, cached_piece_info jarg1_, int jarg2);
public final static native int cached_piece_info_piece_get(long jarg1, cached_piece_info jarg1_);
public final static native void cached_piece_info_blocks_set(long jarg1, cached_piece_info jarg1_, long jarg2, bool_vector jarg2_);
Expand Down Expand Up @@ -3677,5 +3700,6 @@ public class libtorrent_jni {
public final static native long dht_mutable_item_alert_SWIGUpcast(long jarg1);
public final static native long dht_put_alert_SWIGUpcast(long jarg1);
public final static native long i2p_alert_SWIGUpcast(long jarg1);
public final static native long dht_get_peers_reply_alert_SWIGUpcast(long jarg1);
public final static native long peer_connection_SWIGSmartPtrUpcast(long jarg1);
}
74 changes: 74 additions & 0 deletions src/com/frostwire/jlibtorrent/swig/tcp_endpoint_vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 3.0.2
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */

package com.frostwire.jlibtorrent.swig;

public class tcp_endpoint_vector {
private long swigCPtr;
protected boolean swigCMemOwn;

protected tcp_endpoint_vector(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}

protected static long getCPtr(tcp_endpoint_vector obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}

protected void finalize() {
delete();
}

public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
libtorrent_jni.delete_tcp_endpoint_vector(swigCPtr);
}
swigCPtr = 0;
}
}

public tcp_endpoint_vector() {
this(libtorrent_jni.new_tcp_endpoint_vector(), true);
}

public long size() {
return libtorrent_jni.tcp_endpoint_vector_size(swigCPtr, this);
}

public long capacity() {
return libtorrent_jni.tcp_endpoint_vector_capacity(swigCPtr, this);
}

public void reserve(long n) {
libtorrent_jni.tcp_endpoint_vector_reserve(swigCPtr, this, n);
}

public boolean isEmpty() {
return libtorrent_jni.tcp_endpoint_vector_isEmpty(swigCPtr, this);
}

public void clear() {
libtorrent_jni.tcp_endpoint_vector_clear(swigCPtr, this);
}

public void add(tcp_endpoint x) {
libtorrent_jni.tcp_endpoint_vector_add(swigCPtr, this, tcp_endpoint.getCPtr(x), x);
}

public tcp_endpoint get(int i) {
return new tcp_endpoint(libtorrent_jni.tcp_endpoint_vector_get(swigCPtr, this, i), false);
}

public void set(int i, tcp_endpoint val) {
libtorrent_jni.tcp_endpoint_vector_set(swigCPtr, this, i, tcp_endpoint.getCPtr(val), val);
}

}
16 changes: 16 additions & 0 deletions swig/alert_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,22 @@ namespace libtorrent
error_code error;
};

struct TORRENT_EXPORT dht_get_peers_reply_alert: alert
{
// internal
dht_get_peers_reply_alert(libtorrent::sha1_hash const& ih, std::vector<tcp::endpoint> const& v)
: info_hash(ih), peers(v) {
}

TORRENT_DEFINE_ALERT(dht_get_peers_reply_alert);

static const int static_category = alert::dht_notification;
virtual std::string message() const;

sha1_hash info_hash;
std::vector<tcp::endpoint> peers;
};

#undef TORRENT_DEFINE_ALERT

}
Expand Down
4 changes: 4 additions & 0 deletions swig/dht_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ template class rob<dht_tracker_m_dht, &dht_tracker::m_dht>;
virtual int category() const { return static_category; } \
virtual char const* what() const { return #name; }

namespace libtorrent {

struct dht_get_peers_reply_alert: alert {

dht_get_peers_reply_alert(sha1_hash const& ih, std::vector<tcp::endpoint> const& v)
Expand All @@ -69,6 +71,8 @@ struct dht_get_peers_reply_alert: alert {
std::vector<tcp::endpoint> peers;
};

}

void dht_put_item_cb(entry& e, boost::array<char, 64>& sig, boost::uint64_t& seq,
std::string const& salt, char const* public_key, char const* private_key,
entry& data)
Expand Down
4 changes: 4 additions & 0 deletions swig/libtorrent.i
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ namespace std {
%template(peer_entry_vector) vector<libtorrent::peer_entry>;
%template(announce_entry_vector) vector<libtorrent::announce_entry>;
%template(peer_list_entry_vector) vector<libtorrent::peer_list_entry>;
%template(tcp_endpoint_vector) vector<tcp::endpoint>;

%template(entry_list) list<libtorrent::entry>;

Expand Down Expand Up @@ -729,6 +730,8 @@ namespace libtorrent {
CAST_ALERT_METHOD(dht_mutable_item_alert)
CAST_ALERT_METHOD(dht_put_alert)
CAST_ALERT_METHOD(i2p_alert)

CAST_ALERT_METHOD(dht_get_peers_reply_alert)
};

%extend entry {
Expand Down Expand Up @@ -812,6 +815,7 @@ namespace libtorrent {
return std::vector<int>($self->transferred, $self->transferred + stats_alert::stats_channel::num_channels);
}
};

}

class ed25519 {
Expand Down

1 comment on commit bc3de69

@gubatron
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not worthy

You are a monster @aldenml

Please sign in to comment.