Skip to content

Commit

Permalink
r1
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedirtwalker committed Sep 2, 2024
1 parent 65d7af3 commit 6aa466e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
23 changes: 18 additions & 5 deletions private/path/combinator/combinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package combinator
import (
"crypto/sha256"
"encoding/binary"
"hash"
"slices"

"github.com/scionproto/scion/pkg/addr"
Expand Down Expand Up @@ -61,9 +62,9 @@ func Combine(src, dst addr.IA, ups, cores, downs []*seg.PathSegment,

solutions := newDMG(ups, cores, downs).GetPaths(vertexFromIA(src), vertexFromIA(dst))
paths := make([]Path, len(solutions))
sumBuf := make([]byte, 0, sha256.Size)
st := newHashState()
for i, solution := range solutions {
paths[i] = solution.Path(sumBuf)
paths[i] = solution.Path(st)
}
paths = filterLongPaths(paths)
if !findAllIdentical {
Expand Down Expand Up @@ -136,8 +137,9 @@ func filterDuplicates(paths []Path) []Path {
// ASes and BRs, i.e. by its PathInterfaces.
// XXX(matzf): copied from snet.Fingerprint. Perhaps snet.Fingerprint could be adapted to
// take []snet.PathInterface directly.
func fingerprint(interfaces []snet.PathInterface, sumBuf []byte) snet.PathFingerprint {
h := sha256.New()
func fingerprint(interfaces []snet.PathInterface, st hashState) snet.PathFingerprint {
h := st.hash
h.Reset()
for _, intf := range interfaces {
if err := binary.Write(h, binary.BigEndian, intf.IA); err != nil {
panic(err)
Expand All @@ -146,5 +148,16 @@ func fingerprint(interfaces []snet.PathInterface, sumBuf []byte) snet.PathFinger
panic(err)
}
}
return snet.PathFingerprint(h.Sum(sumBuf[:0]))
// convert the snet.PathFingerprint, this is safe because the underlying
// type is string.
return snet.PathFingerprint(h.Sum(st.buf[:0]))
}

type hashState struct {
hash hash.Hash
buf []byte
}

func newHashState() hashState {
return hashState{hash: sha256.New(), buf: make([]byte, 0, sha256.Size)}
}
2 changes: 1 addition & 1 deletion private/path/combinator/combinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func TestFilterDuplicates(t *testing.T) {
Interfaces: interfaces,
Expiry: expiry,
},
Fingerprint: combinator.Fingerprint(interfaces, nil),
Fingerprint: combinator.Fingerprint(interfaces, combinator.NewHashState()),
}
}

Expand Down
1 change: 1 addition & 0 deletions private/path/combinator/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ package combinator
var (
FilterDuplicates = filterDuplicates
Fingerprint = fingerprint
NewHashState = newHashState
)
6 changes: 3 additions & 3 deletions private/path/combinator/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (g *dmg) AddEdge(src, dst vertex, segment *inputSegment, e *edge) {
// GetPaths returns all the paths from src to dst, sorted according to weight.
func (g *dmg) GetPaths(src, dst vertex) []*pathSolution {
var solutions []*pathSolution
queue := []*pathSolution{&pathSolution{currentVertex: src}}
queue := []*pathSolution{{currentVertex: src}}
for len(queue) > 0 {
currentPathSolution := queue[0]
queue = queue[1:]
Expand Down Expand Up @@ -341,7 +341,7 @@ type pathSolution struct {

// Path builds the forwarding path with metadata by extracting it from a path
// between source and destination in the DMG.
func (solution *pathSolution) Path(sumBuf []byte) Path {
func (solution *pathSolution) Path(hashState hashState) Path {
mtu := ^uint16(0)
var segments segmentList
var epicPathAuths [][]byte
Expand Down Expand Up @@ -459,7 +459,7 @@ func (solution *pathSolution) Path(sumBuf []byte) Path {
Notes: staticInfo.Notes,
},
Weight: solution.cost,
Fingerprint: fingerprint(interfaces, sumBuf),
Fingerprint: fingerprint(interfaces, hashState),
}

if authPHVF, authLHVF, ok := isEpicAvailable(epicPathAuths); ok {
Expand Down

0 comments on commit 6aa466e

Please sign in to comment.