3 implementation of custom dht, based on pastry and kademlia.
4 keyspace is divided into buckets of limited capacity
5 node belongs to bucket, where at least 'depth' bits match 'prefix'
10 {used by: messages, fileshare}
14 type tPID
=Store1
.tFID
;
16 procedure NodeBootstrap(const contact
:tNetAddr
);
19 uses ServerLoop
,MemStream
,opcode
;
33 peer
: array [1..4] of tPeer
;
37 function MatchPrefix(const tp
:tFID
):boolean;
44 function PrefixLength(const a
,b
:tFID
):byte;
48 for result
:=0 to 20 do
49 if a
[result
]<>b
[result
]
53 if (a
[result
] and m
)<>(b
[result
] and m
)
54 then break
else m
:=m
shr 1;
59 function tBucket
.MatchPrefix(const tp
:tFID
):boolean;
61 result
:=(depth
=0)or(PrefixLength(prefix
,tp
)>=depth
);
64 function FindBucket(const prefix
:tFID
):tBucket_ptr
;
69 while (cur
<>nil) and (result
=nil) do begin
70 if cur
^.MatchPrefix(prefix
) {first matching is deepest}
76 operator
=(const a
,b
:tFID
):boolean;
78 result
:=CompareWord(a
,b
,10)=0;
81 procedure SplitBucket(ob
:tBucket_ptr
);
82 procedure Toggle(var prefix
:tPID
; bit
:byte);
84 prefix
[bit
div 8]:= prefix
[bit
div 8] xor (bit
mod 8);
89 writeln('DHT: SplitBucket ',string(ob
^.prefix
),'/',ob
^.depth
);
90 {find pref to old bucket, in order to unlink}
91 if ob
=Table
then table
:=table
^.next
else begin
93 while assigned(nb
) and (nb
^.next
<>ob
) do nb
:=nb
^.next
;
94 assert(assigned(nb
),'old bucket not in table');
96 nb
^.next
:=nb
^.next
^.next
; nb
:=nil;
98 {increase depth of this bucket}
100 ob
^.ModifyTime
:=mNow
;
101 {create new bucket with toggled bit}
104 Toggle(nb
^.Prefix
,nb
^.depth
);
106 {clear nodes that do not belong in bucket}
107 for i
:=1 to high(tBucket
.peer
) do begin
108 if ob
^.peer
[i
].addr
.isNil
then continue
;
109 if ob
^.MatchPrefix(ob
^.peer
[i
].id
)
110 then nb
^.peer
[i
].addr
.clear
111 else ob
^.peer
[i
].addr
.clear
;
113 writeln('-> ',string(ob
^.prefix
),'/',ob
^.depth
);
114 writeln('-> ',string(nb
^.prefix
),'/',nb
^.depth
);
115 if table
=nil then table
:=nb
else begin
117 while assigned(ob
^.next
)and (ob
^.next
^.depth
>nb
^.depth
) do ob
:=ob
^.next
;
119 writeln('-> after /',ob
^.depth
);
121 Shedule(2000,@nb
^.Refresh
);
124 procedure UpdateNode(const id
:tFID
; const addr
:tNetAddr
);
131 if not assigned(bkt
) then begin
136 bkt
^.ModifyTime
:=mNow
;
138 for i
:=1 to high(bkt
^.peer
) do bkt
^.peer
[i
].addr
.Clear
;
139 Shedule(2000,@bkt
^.Refresh
);
142 for i
:=1 to high(bkt
^.peer
)
143 do if (fr
=0)and bkt
^.peer
[i
].addr
.isNil
then fr
:=i
144 //else if bkt^.peer[i].addr=addr then fr:=i
145 else if bkt
^.peer
[i
].id
=id
then begin
146 if bkt
^.peer
[i
].addr
<>addr
then continue
;
147 {found node in the bucket}
148 writeln('DHT: UpadateNode ',string(id
));
149 // ?? bkt^.ModifyTime:=mNow;
150 bkt
^.peer
[i
].LastMsgFrom
:=mNow
;
151 exit
end else if (fr
=0) and (bkt
^.peer
[i
].ReqDelta
>=2)
152 then fr
:=i
{use non-responding as free};
154 if bkt
^.MatchPrefix(MyID
)
158 end; {the bucket is full!}
159 {drop new node and hope nodes in the bucket are good}
161 writeln('DHT: AddNode ',string(id
),' to /',bkt
^.depth
,'#',fr
);
162 bkt
^.ModifyTime
:=mNow
;
163 bkt
^.peer
[fr
].ID
:=ID
;
164 bkt
^.peer
[fr
].Addr
:=Addr
;
165 bkt
^.peer
[fr
].LastMsgFrom
:=mNow
;
166 bkt
^.peer
[fr
].LastResFrom
:=0;
167 bkt
^.peer
[fr
].ReqDelta
:=0;
171 procedure RecvRequest(msg
:tSMsg
);
172 var s
:tMemoryStream
absolute msg
.stream
;
184 writeln('DHT: ',string(msg
.source
^),' Request for ',string(rID
^));
185 UpdateNode(hID
^,msg
.source
^);
186 bkt
:=FindBucket(rID
^);
188 if assigned(bkt
) then begin
189 r
.WriteByte(opcode
.dhtSelect
);
191 r
.Write(msg
.Source
^,sizeof(tNetAddr
));
194 if (s
.RdBufLen
>0)and(s
.RdBufLen
<=8) then r
.Write(s
.RdBuf
^,s
.RdBufLen
);
195 for i
:=1 to high(tBucket
.peer
) do begin
196 if bkt
^.peer
[i
].addr
.isNil
then continue
;
197 if bkt
^.peer
[i
].addr
=msg
.source
^ then continue
;
198 writeln('-> Select to ',string(bkt
^.peer
[i
].addr
));
199 SendMessage(r
.base
^,r
.length
,bkt
^.peer
[i
].addr
);
204 else writeln('-> empty bucket');
205 r
.WriteByte(opcode
.dhtReqAck
);
207 writeln('-> ReqAck to ',string(msg
.Source
^));
208 SendMessage(r
.base
^,r
.length
,msg
.source
^);
209 FreeMem(r
.base
,r
.size
);
212 procedure SendRequest(const contact
:tNetAddr
; const forid
: tPID
; caps
:byte);
216 r
.WriteByte(opcode
.dhtRequest
);
217 r
.Write(MyID
,sizeof(tFID
));
218 r
.Write(ForID
,sizeof(tFID
));
220 writeln('DHT: Request to ',string(contact
));
221 SendMessage(r
.base
^,r
.length
,contact
);
222 FreeMem(r
.base
,r
.size
);
225 procedure RecvReqAck(msg
:tSMsg
);
226 var s
:tMemoryStream
absolute msg
.stream
;
231 writeln('DHT: ',string(msg
.source
^),' is ',string(hID
^),' (ReqAck)');
232 UpdateNode(hID
^,msg
.source
^);
235 procedure RecvWazzup(msg
:tSMsg
);
236 var s
:tMemoryStream
absolute msg
.stream
;
241 writeln('DHT: ',string(msg
.source
^),' is ',string(hID
^),' (Wazzup)');
242 UpdateNode(hID
^,msg
.source
^);
243 //UpdateSearch(hID^,msg.source^);
246 procedure NodeBootstrap(const contact
:tNetAddr
);
248 SendRequest(contact
,MyID
,0);
251 procedure RecvSelect(msg
:tSMsg
);
252 var s
:tMemoryStream
absolute msg
.stream
;
260 addr
:=s
.ReadPtr(sizeof(tNetAddr
));
262 writeln('DHT: ',string(msg
.source
^),' Select for ',string(addr
^));
263 if rID
^=MyID
then begin
264 writeln('-> self'); exit
end;
266 r
.WriteByte(opcode
.dhtWazzup
);
268 writeln('-> Wazzup to ',string(addr
^));
269 SendMessage(r
.base
^,r
.length
,addr
^);
270 FreeMem(r
.base
,r
.size
);
273 procedure tBucket
.Refresh
;
277 my
:=MatchPrefix(MyID
);
280 for i
:=1 to high(tBucket
.peer
) do
281 if (not peer
[i
].Addr
.isNil
) and (peer
[i
].ReqDelta
<3) then begin
282 if peer
[i
].ReqDelta
>1 then begin
283 {peer is not responding, but try once more}
284 if not rtr
then write('DHT: **Refresh (',peer
[i
].ReqDelta
,')** ');
285 SendRequest(peer
[i
].Addr
,prefix
,0);
286 inc(peer
[i
].ReqDelta
);
289 else if (ol
=0) or (peer
[i
].LastMsgFrom
<peer
[ol
].LastMsgFrom
)
292 if (ol
>0) and (not rtr
) then begin
293 write('DHT: **Refresh(T)** ');
294 SendRequest(peer
[ol
].Addr
,MyID
,0);
295 inc(peer
[ol
].ReqDelta
);
298 then Shedule(18000+(depth
*600),@Refresh
)
299 else Shedule(30000,@Refresh
);
303 {to bootstrap: ping address to get ID and insert to bucket/il
304 ping may get lost: separate bootstrap unit :)
305 now jut Ass-U-Me wont get lost}
307 procedure LoadIDFromArgs
;
313 assert(OptParamCount(oi
)=1,opt
+'(pid:sha1)');
314 writeln('DHT: set ID to '+paramstr(oi
+1));
315 MyID
:=tPID(paramstr(oi
+1));
320 SetMsgHandler(opcode
.dhtRequest
,@recvRequest
);
321 SetMsgHandler(opcode
.dhtSelect
,@recvSelect
);
322 SetMsgHandler(opcode
.dhtReqAck
,@recvReqAck
);
323 SetMsgHandler(opcode
.dhtWazzup
,@recvWazzup
);