From 87a884abdca4f8116b5ef9559d129f8a428cda5c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 22 Apr 2018 19:18:35 +0200 Subject: [PATCH] [client:client] implemented 'Receive(io.Reader)' for reading 'errorCode' then validating migration or rescuing key if failure --- src/git.xdrm.io/schastsp/client/client.go | 51 ++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/git.xdrm.io/schastsp/client/client.go b/src/git.xdrm.io/schastsp/client/client.go index b1a88d2..94ba94a 100644 --- a/src/git.xdrm.io/schastsp/client/client.go +++ b/src/git.xdrm.io/schastsp/client/client.go @@ -1,6 +1,9 @@ package client; import ( + "fmt" + "errors" + "git.xdrm.io/schastsp/lib/scha" "io" "git.xdrm.io/schastsp/context" "git.xdrm.io/schastsp/client/keyset" @@ -85,4 +88,50 @@ func (c *T) Send(w io.Writer) error { * @return err Error * ---------------------------------------------------------*/ -func (c *T) Receive(r io.Reader) error { return nil } \ No newline at end of file +func (c *T) Receive(r io.Reader) error { + + /* (1) Read error code + ---------------------------------------------------------*/ + errCode := make([]byte, 1) + read, err := r.Read(errCode) + if err != nil { return err } + if uint16(read) != 1 { return errors.New("Cannot read enough bytes") } + + + /* (2) Manage success + ---------------------------------------------------------*/ + if errCode[0] == 0 { + + /* (1) If pending migration -> migrate */ + if c.key.MigrationCode() == 2 { + c.key.MigrationCode(3) + if DEBUG { fmt.Printf("*** VALIDATED MIGRATION\n") } + } + + /* (2) No error anyway */ + return nil; + + } + + + /* (3) Manage rescue + ---------------------------------------------------------*/ + /* (1) Read y1 */ + y1 := make([]byte, scha.HSIZE); + read, err = r.Read(y1) + if err != nil { return err } + if uint16(read) != scha.HSIZE { return errors.New("Cannot read enough bytes") } + + /* (2) Read y2 */ + y2 := make([]byte, scha.HSIZE); + read, err = r.Read(y2) + if err != nil { return err } + if uint16(read) != scha.HSIZE { return errors.New("Cannot read enough bytes") } + + /* (3) Manage rescue mode */ + err = c.rescue(y1, y2); + + /* (4) Dispatch err */ + return err + +} \ No newline at end of file