forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oracle.go
68 lines (55 loc) · 1.56 KB
/
oracle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package torrent
import (
"context"
"fmt"
"github.com/netsec-ethz/scion-apps/pkg/appnet"
"github.com/scionproto/scion/go/lib/snet"
)
func unique(pathSlice []snet.Path) []snet.Path {
keys := make(map[string]bool)
list := []snet.Path{}
for _, entry := range pathSlice {
if _, value := keys[fmt.Sprintf("%s", entry)]; !value {
keys[fmt.Sprintf("%s", entry)] = true
list = append(list, entry)
}
}
return list
}
func GetPathsFromAddr(lAddr, rAddr *snet.UDPAddr, shouldBeUnique bool) ([]snet.Path, error) {
if !lAddr.IA.Equal(rAddr.IA) {
// query paths from here to there:
pathSet, err := appnet.DefNetwork().PathQuerier.Query(context.Background(), rAddr.IA)
if err != nil {
return nil, err
}
if len(pathSet) == 0 {
return nil, fmt.Errorf("No Paths")
}
// print all paths. Also pick one path. Here we chose the path with least hops:
i := 0
fmt.Println("Available paths:")
for _, path := range pathSet {
fmt.Printf("[%2d] %s\n", i, path)
// fmt.Println(path.Metadata().Interfaces)
}
if !shouldBeUnique {
return pathSet, nil
}
uniquePaths := unique(pathSet)
fmt.Println("Unique paths:")
for _, path := range uniquePaths {
fmt.Printf("[%2d] %s\n", i, path)
}
return uniquePaths, nil
}
return []snet.Path{}, nil
}
func ChoosePath(rAddr *snet.UDPAddr, path snet.Path) *snet.UDPAddr {
// we need to copy the path to the destination (destination is the whole selected path)
newAddr := rAddr.Copy()
newAddr.Path = path.Path()
// newAddr.Path.InitOffsets()
newAddr.NextHop = path.UnderlayNextHop()
return newAddr
}