From 21d7292dbd062ff11194fdc235a3d54830d7ba57 Mon Sep 17 00:00:00 2001 From: Bernd Fix Date: Sun, 21 Aug 2022 16:40:12 +0200 Subject: Integration test: bug fixes 1st round. --- src/gnunet/service/dht/path/handling.go | 146 +++++++++++++++----------------- 1 file changed, 70 insertions(+), 76 deletions(-) (limited to 'src/gnunet/service/dht/path/handling.go') diff --git a/src/gnunet/service/dht/path/handling.go b/src/gnunet/service/dht/path/handling.go index 4040511..7ad91df 100644 --- a/src/gnunet/service/dht/path/handling.go +++ b/src/gnunet/service/dht/path/handling.go @@ -19,11 +19,10 @@ package path import ( - "bytes" - "encoding/hex" - "fmt" "gnunet/crypto" + "gnunet/enums" "gnunet/util" + "strings" "github.com/bfix/gospel/data" "github.com/bfix/gospel/logger" @@ -33,17 +32,11 @@ import ( // Path handling //---------------------------------------------------------------------- -// path flags -const ( - PathTruncated = 1 - PathLastHop = 2 -) - // Path is the complete list of verified hops a message travelled. // It also keeps the associated block hash and expiration time of // the request for signature verification purposes. type Path struct { - Flags uint32 `order:"big"` // flags + Flags uint16 `order:"big"` // flags BlkHash *crypto.HashCode `` // block hash value Expire util.AbsoluteTime `` // expiration time TruncOrigin *util.PeerID `opt:"(IsUsed)"` // truncated origin (optional) @@ -58,9 +51,9 @@ type Path struct { func (p *Path) IsUsed(field string) bool { switch field { case "TruncOrigin": - return p.Flags&PathTruncated != 0 + return p.Flags&enums.DHT_RO_TRUNCATED != 0 case "LastSig", "LastHop": - return p.Flags&PathLastHop != 0 + return p.Flags&enums.DHT_RO_RECORD_ROUTE != 0 } return false } @@ -128,7 +121,7 @@ func (p *Path) Clone() *Path { // NewElement creates a new path element from data func (p *Path) NewElement(pred, signer, succ *util.PeerID) *Element { return &Element{ - elementData: elementData{ + _ElementData: _ElementData{ Expire: p.Expire, BlockHash: p.BlkHash, PeerPredecessor: pred, @@ -155,7 +148,7 @@ func (p *Path) Add(elem *Element) { // update last hop signature p.LastSig = elem.Signature p.LastHop = elem.Signer - p.Flags |= PathLastHop + p.Flags |= enums.DHT_RO_RECORD_ROUTE } // Verify path: process list entries from right to left (decreasing index). @@ -181,6 +174,7 @@ func (p *Path) Verify(local *util.PeerID) { pe := p.NewElement(pred, p.LastHop, local) ok, err := pe.Verify(p.LastSig) if err != nil || !ok { + logger.Println(logger.WARN, "[path] Dropping path (invalid last signature)") // remove last hop signature and truncated origin; reset flags p.LastSig = nil p.LastHop = nil @@ -188,79 +182,79 @@ func (p *Path) Verify(local *util.PeerID) { p.Flags = 0 } return - } else { - // yes: process list of path elements - signer := p.LastHop - sig := p.LastSig - succ := local - num := len(p.List) - var pred *util.PeerID - for i := num - 1; i >= 0; i-- { - if i == -1 { - if p.TruncOrigin != nil { - pred = p.TruncOrigin - } else { - pred = util.NewPeerID(nil) - } + } + // yes: process list of path elements + signer := p.LastHop + sig := p.LastSig + succ := local + num := len(p.List) + var pred *util.PeerID + for i := num - 1; i >= 0; i-- { + if i == -1 { + if p.TruncOrigin != nil { + pred = p.TruncOrigin } else { - pred = p.List[i].Signer + pred = util.NewPeerID(nil) } - pe := p.NewElement(pred, signer, succ) - ok, err := pe.Verify(sig) - if err != nil || !ok { - // we need to truncate: - logger.Printf(logger.WARN, "[path] Truncating path (invalid signature at hop %d)", i) + } else { + pred = p.List[i].Signer + } + pe := p.NewElement(pred, signer, succ) + ok, err := pe.Verify(sig) + if err != nil || !ok { + // we need to truncate: + logger.Printf(logger.WARN, "[path] Truncating path (invalid signature at hop %d)", i) - // are we at the end of the list? - if i == num-1 { - // yes: the last hop signature failed -> reset path - p.LastSig = nil - p.LastHop = nil - p.TruncOrigin = nil - p.Flags = 0 - p.List = make([]*Entry, 0) - return - } - // trim list - p.Flags |= PathTruncated - p.TruncOrigin = signer - size := num - 2 - i - list := make([]*Entry, size) - if size > 0 { - copy(list, p.List[i+2:]) - } - p.List = list + // are we at the end of the list? + if i == num-1 { + // yes: the last hop signature failed -> reset path + p.LastSig = nil + p.LastHop = nil + p.TruncOrigin = nil + p.Flags = 0 + p.List = make([]*Entry, 0) return } - // check next path element - succ = signer - signer = pred - if i != -1 { - sig = p.List[i].Signature + // trim list + p.Flags |= enums.DHT_RO_TRUNCATED + p.TruncOrigin = signer + size := num - 2 - i + list := make([]*Entry, size) + if size > 0 { + copy(list, p.List[i+2:]) } + p.List = list + return + } + // check next path element + succ = signer + signer = pred + if i != -1 { + sig = p.List[i].Signature } } } -// String returs a uman-readbale representation +// String returns a human-readable representation func (p *Path) String() string { - buf := new(bytes.Buffer) - s := "0" - if p.TruncOrigin != nil { - s = p.TruncOrigin.String() - } - buf.WriteString(fmt.Sprintf("{to=%s, (%d)[", s, len(p.List))) - for _, e := range p.List { - buf.WriteString(e.String()) - } - s = "0" - if p.LastSig != nil { - s = hex.EncodeToString(p.LastSig.Bytes()) + var hops []string + if p != nil { + if p.TruncOrigin != nil { + hops = append(hops, p.TruncOrigin.Short()) + } + for _, e := range p.List { + hops = append(hops, e.Signer.Short()) + } + if p.LastHop != nil { + hops = append(hops, p.LastHop.Short()) + } } - num := len(s) - if num > 16 { - s = s[:8] + ".." + s[num-8:] + // trim to sensible length for display + if num := len(hops); num > 8 { + trim := make([]string, 9) + copy(trim[:4], hops[:4]) + trim[4] = "..." + copy(trim[5:], hops[num-5:]) } - buf.WriteString(fmt.Sprintf("], ls=%s}", s)) - return buf.String() + return "[" + strings.Join(hops, "-") + "]" } -- cgit v1.2.3