From a18914d3ae42943022a684e6f4661bac7d8abb52 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tom=C3=A1=C5=A1=20Brada?= Date: Fri, 25 Dec 2015 23:52:43 +0100 Subject: [PATCH] Discover and fix bugs in auth and new chat. Chat timeouts still need simplification. forgot to check verify pub match node id Do not ban node when verification temporary fails. --- CRAuth.pas | 15 ++++++++------- Chat.pas | 2 ++ dht.pas | 14 +++++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CRAuth.pas b/CRAuth.pas index cf34be5..039e022 100644 --- a/CRAuth.pas +++ b/CRAuth.pas @@ -48,17 +48,16 @@ end; procedure tAuth.ReplyRes(msg:tSMsg; data:boolean); var r:tMemoryStream absolute msg.Stream; var status:byte; - var RPub:^tEccKey; var resp:^tEccKey; var vresp:tSha1Digest; begin if not data then exit; status:=r.readbyte; {todo, set error (eg: unsuported meth)} - RPub:=r.readptr(sizeof(tEccKey)); + r.Read(RemotePub,sizeof(tEccKey)); resp:=r.readptr(sizeof(tEccKey)); - ECC.CreateResponse(Challenge,vresp,RPub^); + ECC.CreateResponse(Challenge,vresp,RemotePub); Valid:=CompareByte(resp^,vresp,sizeof(vresp))=0; - if (status and 128)=1 then begin + if (status and 128)>0 then begin {expecting pow} Ch.Callback:=@ReplyPow; Ch.Ack; @@ -138,6 +137,7 @@ procedure tServer.SendRep(msg:tSMsg; data:boolean); ms.WriteByte(128); ms.Write(PublicKey,sizeof(PublicKey)); ms.Write(resp,sizeof(resp)); + ch^.Callback:=@SendPoW; ch^.SetTimeout(8000,0);{no reply expected} ch^.send(ms); end; @@ -150,6 +150,7 @@ procedure tServer.SendPow(msg:tSMsg; data:boolean); ms.WriteByte(1); ms.Write(PublicPoW,sizeof(PublicPoW)); ms.Write(PublicPoWTS,2); + ch^.Callback:=@Last; ch^.SetTimeout(8000,2000); ch^.send(ms); end; @@ -161,9 +162,9 @@ procedure tServer.Last(msg:tSMsg; data:boolean); if not data then exit; {unlikely} Valid:=r.ReadByte; ValidPoW:=r.ReadByte; - if (Valid>0)or(ValidPoW>0) then begin - writeln('CRAuth: Our auth failed on remote, reason pub=',Valid,' pow=',ValidPoW); - Writeln('CRAuth: remote ',string(ch^.remote),' ',string(pub)); + if (Valid<>1)or(ValidPoW<>1) then begin + write('CRAuth: Our auth failed on remote, reason pub=',Valid,' pow=',ValidPoW); + Writeln(' remote ',string(ch^.remote),' ',string(pub)); end; Close; end; diff --git a/Chat.pas b/Chat.pas index a8370bd..a8e4df4 100644 --- a/Chat.pas +++ b/Chat.pas @@ -135,6 +135,7 @@ procedure tChat.Ack; ServerLoop.SendMessage(s.base^,s.length,remote); FreeMem(s.base,s.length); rxAcked:=true; + if assigned(OnTimeout) and (tmReply>0) then Shedule(tmReply,@ReplyTimeout); end; end; @@ -145,6 +146,7 @@ procedure tChat.Close; closed:=true; callback:=nil; {avoid calling} ontimeout:=nil; + UnShedule(@ReplyTimeout); {fuck it} //writeln('Chat: closing'); if txLen=0 {no packets in flight} then begin Shedule(5000{todo},@Done); {wait for something lost} diff --git a/dht.pas b/dht.pas index 0ad9004..61b8e00 100644 --- a/dht.pas +++ b/dht.pas @@ -235,7 +235,7 @@ procedure GetNextNode(var ibkt:tBucket_ptr; var ix:byte; const id:tPID; maxrd:wo end; until (not bkt^.peer[ix].Addr.isNil) and(bkt^.peer[ix].ReqDelta0 then begin + writeln('DHT: Verificator error ',string(Addr),Verify^.error); + ReqDelta:=3; + end else + if Verify^.Valid and Verify^.PowValid and (CompareWord(ID,Verify^.RemotePub,10)=0) then + Ban:=false + else begin Ban:=true; - end else Ban:=false; {just in case} + writeln('DHT: Verificator failed for ',string(Addr),Verify^.Valid,Verify^.PoWValid,Verify^.error); + end; Verify:=nil; {it will free itelf} end; -- 2.11.4.GIT