-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
DHT.java
221 lines (176 loc) · 6.48 KB
/
DHT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.frostwire.jlibtorrent;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.DhtImmutableItemAlert;
import com.frostwire.jlibtorrent.swig.libtorrent;
import com.frostwire.jlibtorrent.swig.settings_pack;
import com.frostwire.jlibtorrent.swig.sha1_hash;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static com.frostwire.jlibtorrent.alerts.AlertType.DHT_GET_PEERS_REPLY;
import static com.frostwire.jlibtorrent.alerts.AlertType.DHT_IMMUTABLE_ITEM;
/**
* This class provides a lens only functionality.
*
* @author gubatron
* @author aldenml
*/
public final class DHT {
private static final int[] DHT_IMMUTABLE_ITEM_TYPES = {DHT_IMMUTABLE_ITEM.getSwig()};
private final Session s;
public DHT(Session s) {
this.s = s;
}
public void start() {
toggleDHT(true);
}
public void stop() {
toggleDHT(false);
}
public boolean isRunning() {
return s.isDHTRunning();
}
public void get(String sha1) {
s.dhtGetItem(new Sha1Hash(sha1));
}
public Entry get(String sha1, long timeout, TimeUnit unit) {
final Sha1Hash target = new Sha1Hash(sha1);
final Entry[] result = {null};
final CountDownLatch signal = new CountDownLatch(1);
AlertListener l = new AlertListener() {
@Override
public int[] types() {
return DHT_IMMUTABLE_ITEM_TYPES;
}
@Override
public void alert(Alert<?> alert) {
if (alert instanceof DhtImmutableItemAlert) {
DhtImmutableItemAlert itemAlert = (DhtImmutableItemAlert) alert;
if (target.equals(itemAlert.getTarget())) {
result[0] = itemAlert.getItem();
signal.countDown();
}
}
}
};
s.addListener(l);
s.dhtGetItem(target);
try {
signal.await(timeout, unit);
} catch (InterruptedException e) {
// ignore
}
s.removeListener(l);
return result[0];
}
public String put(Entry entry) {
return s.dhtPutItem(entry).toString();
}
public void getPeers(String sha1) {
s.dhtGetPeers(new Sha1Hash(sha1));
}
// commenting this method until a better API is created
/*
public ArrayList<TcpEndpoint> getPeers(String sha1, long timeout, TimeUnit unit) {
final Sha1Hash target = new Sha1Hash(sha1);
final Object[] result = {new ArrayList<TcpEndpoint>()};
final CountDownLatch signal = new CountDownLatch(1);
AlertListener l = new AlertListener() {
@Override
public int[] types() {
return DHT_GET_PEERS_REPLY_ALERT_TYPES;
}
@Override
public void alert(Alert<?> alert) {
if (alert instanceof DhtGetPeersReplyAlert) {
DhtGetPeersReplyAlert replyAlert = (DhtGetPeersReplyAlert) alert;
if (target.equals(replyAlert.infoHash())) {
result[0] = replyAlert.peers();
signal.countDown();
}
}
}
};
s.addListener(l);
s.dhtGetPeers(target);
try {
signal.await(timeout, unit);
} catch (InterruptedException e) {
// ignore
}
s.removeListener(l);
return (ArrayList<TcpEndpoint>) result[0];
}*/
public void announce(String sha1, int port, int flags) {
s.dhtAnnounce(new Sha1Hash(sha1), port, flags);
}
public void announce(String sha1) {
s.dhtAnnounce(new Sha1Hash(sha1));
}
private void toggleDHT(boolean on) {
SettingsPack pack = new SettingsPack();
pack.setBoolean(settings_pack.bool_types.enable_dht.swigValue(), on);
s.applySettings(pack);
}
/**
* calculate the target hash for an immutable item.
*
* @param e
* @return
*/
public static Sha1Hash itemTargetId(Entry e) {
return null;//new Sha1Hash(dht_item.item_target_id(e.getSwig().bencode()));
}
/**
* calculate the target hash for a mutable item.
*
* @param salt
* @param pk
* @return
*/
public static Sha1Hash itemTargetId(byte[] salt, byte[] pk) {
sha1_hash h = libtorrent.dht_item_target_id(Vectors.bytes2byte_vector(salt), Vectors.bytes2byte_vector(pk));
return new Sha1Hash(h);
}
/**
* Given a byte range ``v`` and an optional byte range ``salt``, a
* sequence number, public key ``pk`` (must be 32 bytes) and a secret key
* ``sk`` (must be 64 bytes), this function produces a signature which
* is written into a 64 byte buffer pointed to by ``sig``. The caller
* is responsible for allocating the destination buffer that's passed in
* as the ``sig`` argument. Typically it would be allocated on the stack.
*
* @param e
* @param salt
* @param seq
* @param pk
* @param sk
* @param sig
*/
public static void signMutableItem(Entry e, String salt, int seq, byte[] pk, byte[] sk, byte[] sig) {
/*
if (sig == null || sig.length != Ed25519.SIGNATURE_SIZE) {
throw new IllegalArgumentException("The signature array must be a valid one with length " + Ed25519.SIGNATURE_SIZE);
}
char_vector sig_v = Vectors.new_char_vector(Ed25519.SIGNATURE_SIZE);
dht_item.sign_mutable_item(e.getSwig().bencode(), salt, seq,
Vectors.bytes2char_vector(pk), Vectors.bytes2char_vector(sk), sig_v);
Vectors.char_vector2bytes(sig_v, sig);
*/
}
public static boolean verifyMutableItem(Entry e, String salt, int seq, byte[] pk, byte[] sig) {
return false;/*dht_item.verify_mutable_item(e.getSwig().bencode(), salt, seq,
Vectors.bytes2char_vector(pk),
Vectors.bytes2char_vector(sig));*/
}
public static int canonicalString(Entry e, int seq, String salt, byte[] out) {
/* if (out == null || out.length != 1200) {
throw new IllegalArgumentException("The out array must be a valid one with length 1200");
}
char_vector out_v = Vectors.new_char_vector(1200);
int r = dht_item.canonical_string(e.getSwig().bencode(),
seq, salt, out_v);
Vectors.char_vector2bytes(out_v, out);
return r;*/
return 0;
}
}