~timkross/cjdns/cjdns

« back to all changes in this revision

Viewing changes to dht/core/juliusz/patch

  • Committer: cjdelisle
  • Date: 2011-02-16 23:03:00 UTC
  • Revision ID: git-v1:d475c9c10adc25590bea5e7dc5383b32f66d5eb8
First commit for cjdns.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--- dht.original        2011-02-15 20:31:54.000000000 -0500
 
2
+++ dht.c.new   2011-02-15 20:31:17.000000000 -0500
 
3
@@ -178,12 +178,14 @@
 
4
     struct search *next;
 
5
 };
 
6
 
 
7
+/* Implemented in cjdns modular dht.
 
8
 struct peer {
 
9
     time_t time;
 
10
     unsigned char ip[16];
 
11
     unsigned short len;
 
12
     unsigned short port;
 
13
 };
 
14
+*/
 
15
 
 
16
 /* The maximum number of peers we store for a given hash. */
 
17
 #ifndef DHT_MAX_PEERS
 
18
@@ -205,13 +207,15 @@
 
19
 #define DHT_SEARCH_EXPIRE_TIME (62 * 60)
 
20
 #endif
 
21
 
 
22
+/* Implemented in cjdns modular dht.
 
23
 struct storage {
 
24
     unsigned char id[20];
 
25
     int numpeers, maxpeers;
 
26
     struct peer *peers;
 
27
     struct storage *next;
 
28
 };
 
29
-
 
30
+*/
 
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,
 
35
@@ -225,11 +229,13 @@
 
36
                             const unsigned char *nodes6, int nodes6_len,
 
37
                             int af, struct storage *st,
 
38
                             const unsigned char *token, int token_len);
 
39
+/* #end# */
 
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);
 
49
@@ -242,6 +248,7 @@
 
50
 static int send_error(const struct sockaddr *sa, int salen,
 
51
                       unsigned char *tid, int tid_len,
 
52
                       int code, const char *message);
 
53
+/* #end# */
 
54
 
 
55
 #define ERROR 0
 
56
 #define REPLY 1
 
57
@@ -253,6 +260,7 @@
 
58
 #define WANT4 1
 
59
 #define WANT6 2
 
60
 
 
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,
 
65
@@ -265,6 +273,22 @@
 
66
                          unsigned char *values_return, int *values_len,
 
67
                          unsigned char *values6_return, int *values6_len,
 
68
                          int *want_return);
 
69
+*/
 
70
+/* Required for in cjdns modular dht. */
 
71
+static int
 
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,
 
83
+               int *want_return);
 
84
+/* #end# */
 
85
 
 
86
 static const unsigned char zeroes[20] = {0};
 
87
 static const unsigned char ones[20] = {
 
88
@@ -1842,17 +1866,26 @@
 
89
     return 0;
 
90
 }
 
91
 
 
92
+/* Altered to interface with cjdns modular dht.
 
93
 int
 
94
 dht_periodic(const void *buf, size_t buflen,
 
95
              const struct sockaddr *from, int fromlen,
 
96
              time_t *tosleep,
 
97
              dht_callback *callback, void *closure)
 
98
+*/
 
99
+int
 
100
+dht_periodic(int available, time_t *tosleep,
 
101
+             dht_callback *callback, void *closure,
 
102
+             struct DHTMessage* packet)
 
103
 {
 
104
     int i;
 
105
 
 
106
     gettimeofday(&now, NULL);
 
107
 
 
108
+/*
 
109
     if(buflen > 0) {
 
110
+*/
 
111
+    if (available) {
 
112
         int message;
 
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 @@
 
116
         int want;
 
117
         unsigned short ttid;
 
118
 
 
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);
 
124
+        /* #end# */
 
125
+
 
126
         if(is_martian(from))
 
127
             goto dontread;
 
128
 
 
129
@@ -1873,8 +1913,8 @@
 
130
                 goto dontread;
 
131
             }
 
132
         }
 
133
-
 
134
-        /* See parse_message. */
 
135
+        /* Replaced by cjdns modular dht.
 
136
+        * See parse_message. *
 
137
 
 
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,
 
143
                                 &want);
 
144
+        */
 
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,
 
149
+                                 &want);
 
150
+        /* #end# */
 
151
 
 
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);
 
156
             debugf("\n");
 
157
             goto dontread;
 
158
         }
 
159
@@ -1912,7 +1959,7 @@
 
160
         case REPLY:
 
161
             if(tid_len != 4) {
 
162
                 debugf("Broken node truncates transaction ids: ");
 
163
-                debug_printable(buf, buflen);
 
164
+                debug_printable(buf, rc);
 
165
                 debugf("\n");
 
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 @@
 
169
                 }
 
170
             } else {
 
171
                 debugf("Unexpected reply: ");
 
172
-                debug_printable(buf, buflen);
 
173
+                debug_printable(buf, rc);
 
174
                 debugf("\n");
 
175
             }
 
176
             break;
 
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. */
 
180
 
 
181
+/* Handled in cjdns modular dht engine
 
182
+
 
183
 #define CHECK(offset, delta, size)                      \
 
184
     if(delta < 0 || offset + delta > size) goto fail
 
185
 
 
186
@@ -2415,9 +2464,9 @@
 
187
     }
 
188
 
 
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. /
 
195
 
 
196
         len = af == AF_INET ? 4 : 16;
 
197
         j0 = random() % st->numpeers;
 
198
@@ -2451,6 +2500,7 @@
 
199
     errno = ENOSPC;
 
200
     return -1;
 
201
 }
 
202
+*/
 
203
 
 
204
 static int
 
205
 insert_closest_node(unsigned char *nodes, int numnodes,
 
206
@@ -2559,6 +2609,7 @@
 
207
                             af, st, token, token_len);
 
208
 }
 
209
 
 
210
+/* Handled in cjdns modular dht
 
211
 int
 
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 @@
 
215
 #undef INC
 
216
 #undef COPY
 
217
 #undef ADD_V
 
218
+*/
 
219
 
 
220
 #ifndef HAVE_MEMMEM
 
221
 static void *