1
--- dht.original 2011-02-15 20:31:54.000000000 -0500
2
+++ dht.c.new 2011-02-15 20:31:17.000000000 -0500
7
+/* Implemented in cjdns modular dht.
16
/* The maximum number of peers we store for a given hash. */
19
#define DHT_SEARCH_EXPIRE_TIME (62 * 60)
22
+/* Implemented in cjdns modular dht.
25
int numpeers, maxpeers;
31
+/* Implemented in cjdns modular dht. */
32
static int send_ping(const struct sockaddr *sa, int salen,
33
const unsigned char *tid, int tid_len);
34
static int send_pong(const struct sockaddr *sa, int salen,
36
const unsigned char *nodes6, int nodes6_len,
37
int af, struct storage *st,
38
const unsigned char *token, int token_len);
40
static int send_closest_nodes(const struct sockaddr *sa, int salen,
41
const unsigned char *tid, int tid_len,
42
const unsigned char *id, int want,
43
int af, struct storage *st,
44
const unsigned char *token, int token_len);
45
+/* Implemented in cjdns modular dht. */
46
static int send_get_peers(const struct sockaddr *sa, int salen,
47
unsigned char *tid, int tid_len,
48
unsigned char *infohash, int want, int confirm);
50
static int send_error(const struct sockaddr *sa, int salen,
51
unsigned char *tid, int tid_len,
52
int code, const char *message);
61
+/* Implemented in cjdns modular dht.
62
static int parse_message(const unsigned char *buf, int buflen,
63
unsigned char *tid_return, int *tid_len,
64
unsigned char *id_return,
66
unsigned char *values_return, int *values_len,
67
unsigned char *values6_return, int *values6_len,
70
+/* Required for in cjdns modular dht. */
72
+parse_message2(bobj_t* bencodedMessage,
73
+ unsigned char *tid_return, int *tid_len,
74
+ unsigned char *id_return,
75
+ unsigned char *info_hash_return,
76
+ unsigned char *target_return,
77
+ unsigned short *port_return,
78
+ unsigned char *token_return, int *token_len,
79
+ unsigned char *nodes_return, int *nodes_len,
80
+ unsigned char *nodes6_return, int *nodes6_len,
81
+ unsigned char *values_return, int *values_len,
82
+ unsigned char *values6_return, int *values6_len,
86
static const unsigned char zeroes[20] = {0};
87
static const unsigned char ones[20] = {
88
@@ -1842,17 +1866,26 @@
92
+/* Altered to interface with cjdns modular dht.
94
dht_periodic(const void *buf, size_t buflen,
95
const struct sockaddr *from, int fromlen,
97
dht_callback *callback, void *closure)
100
+dht_periodic(int available, time_t *tosleep,
101
+ dht_callback *callback, void *closure,
102
+ struct DHTMessage* packet)
106
gettimeofday(&now, NULL);
113
unsigned char tid[16], id[20], info_hash[20], target[20];
114
unsigned char nodes[256], nodes6[1024], token[128];
115
@@ -1864,6 +1897,13 @@
119
+ /* For cjdns compatability */
120
+ struct sockaddr_storage source_storage;
121
+ struct sockaddr *from = (struct sockaddr*)&source_storage;
122
+ socklen_t fromlen = sizeof(source_storage);
123
+ NetowrkTools_getPeerAddress(packet->peerAddress, packet->addressLength, &source_storage);
129
@@ -1873,8 +1913,8 @@
134
- /* See parse_message. */
135
+ /* Replaced by cjdns modular dht.
136
+ * See parse_message. *
138
if(((char*)buf)[buflen] != '\0') {
139
debugf("Unterminated message.\n");
140
@@ -1887,10 +1927,17 @@
141
nodes, &nodes_len, nodes6, &nodes6_len,
142
values, &values_len, values6, &values6_len,
145
+ message = parse_message2(packet->bencoded, tid, &tid_len, id, info_hash,
146
+ target, &port, token, &token_len,
147
+ nodes, &nodes_len, nodes6, &nodes6_len,
148
+ values, &values_len, values6, &values6_len,
152
if(message < 0 || message == ERROR || id_cmp(id, zeroes) == 0) {
153
debugf("Unparseable message: ");
154
- debug_printable(buf, buflen);
155
+ debug_printable(buf, rc);
159
@@ -1912,7 +1959,7 @@
162
debugf("Broken node truncates transaction ids: ");
163
- debug_printable(buf, buflen);
164
+ debug_printable(buf, rc);
166
/* This is really annoying, as it means that we will
167
time-out all our searches that go through this node.
168
@@ -2022,7 +2069,7 @@
171
debugf("Unexpected reply: ");
172
- debug_printable(buf, buflen);
173
+ debug_printable(buf, rc);
177
@@ -2276,6 +2323,8 @@
178
/* We could use a proper bencoding printer and parser, but the format of
179
DHT messages is fairly stylised, so this seemed simpler. */
181
+/* Handled in cjdns modular dht engine
183
#define CHECK(offset, delta, size) \
184
if(delta < 0 || offset + delta > size) goto fail
186
@@ -2415,9 +2464,9 @@
189
if(st && st->numpeers > 0) {
190
- /* We treat the storage as a circular list, and serve a randomly
191
+ / We treat the storage as a circular list, and serve a randomly
192
chosen slice. In order to make sure we fit within 1024 octets,
193
- we limit ourselves to 50 peers. */
194
+ we limit ourselves to 50 peers. /
196
len = af == AF_INET ? 4 : 16;
197
j0 = random() % st->numpeers;
198
@@ -2451,6 +2500,7 @@
205
insert_closest_node(unsigned char *nodes, int numnodes,
206
@@ -2559,6 +2609,7 @@
207
af, st, token, token_len);
210
+/* Handled in cjdns modular dht
212
send_get_peers(const struct sockaddr *sa, int salen,
213
unsigned char *tid, int tid_len, unsigned char *infohash,
214
@@ -2667,6 +2718,7 @@