-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathpathdb_dump.go
193 lines (179 loc) · 4.88 KB
/
pathdb_dump.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2020 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// debug tool to dump the contents of a sqlite path DB.
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/pathdb/query"
"github.com/scionproto/scion/go/lib/pathdb/sqlite"
"github.com/scionproto/scion/go/proto"
)
func main() {
if err := realMain(); err != nil {
fmt.Fprintf(os.Stderr, "Error while executing: %v\n", err)
os.Exit(1)
}
}
func realMain() error {
var filename string
var showTimestamps bool
flag.StringVar(&filename, "db", "", "Sqlite DB file (optional)")
flag.BoolVar(&showTimestamps, "t", false, "Show update and expiration times")
flag.Parse()
var err error
if filename == "" {
filename, err = defaultDBfilename()
if err != nil {
return err
}
}
db, err := sqlite.New(filename)
if err != nil {
return err
}
defer db.Close()
ch, err := db.GetAll(context.Background())
if err != nil {
return err
}
var segments []segment
for res := range ch {
if res.Err != nil {
return err
}
seg, err := newSegment(res.Result)
if err != nil {
return err
}
segments = append(segments, seg)
}
sort.Slice(segments, func(i, j int) bool {
return segments[i].lessThan(&segments[j])
})
for _, seg := range segments {
fmt.Println(seg.toString(showTimestamps))
}
return nil
}
type asIface struct {
IA addr.IA
ifNum common.IFIDType
}
func ifsArrayToString(ifs []asIface) string {
if len(ifs) == 0 {
return ""
}
strs := []string{fmt.Sprintf("%s %d", ifs[0].IA, ifs[0].ifNum)}
for i := 1; i < len(ifs)-1; i += 2 {
strs = append(strs, fmt.Sprintf("%d %s %d", ifs[i].ifNum, ifs[i].IA, ifs[i+1].ifNum))
}
strs = append(strs, fmt.Sprintf("%d %s", ifs[len(ifs)-1].ifNum, ifs[len(ifs)-1].IA))
return strings.Join(strs, ">")
}
type segment struct {
LoggingID string
SegType proto.PathSegType
interfaces []asIface
Updated time.Time
Expiry time.Time
}
func newSegment(res *query.Result) (segment, error) {
ifs := make([]asIface, 0, len(res.Seg.ASEntries))
for _, ase := range res.Seg.ASEntries {
hop, err := ase.HopEntries[0].HopField()
if err != nil {
return segment{}, err
}
if hop.ConsIngress > 0 {
iface := asIface{
IA: ase.IA(),
ifNum: hop.ConsIngress,
}
ifs = append(ifs, iface)
}
if hop.ConsEgress > 0 {
iface := asIface{
IA: ase.IA(),
ifNum: hop.ConsEgress,
}
ifs = append(ifs, iface)
}
}
return segment{
LoggingID: res.Seg.GetLoggingID(),
SegType: res.Type,
Updated: res.LastUpdate,
Expiry: res.Seg.MinExpiry(),
interfaces: ifs,
}, nil
}
func (s segment) toString(showTimestamps bool) string {
str := fmt.Sprintf("%s\t%s\t%s", s.LoggingID, s.SegType, ifsArrayToString(s.interfaces))
if showTimestamps {
now := time.Now()
updatedStr := now.Sub(s.Updated).String()
expiryStr := s.Expiry.Sub(now).String()
str += fmt.Sprintf("\tUpdated: %s\tExpires in: %s", updatedStr, expiryStr)
}
return str
}
// lessThan returns if this segment is < the other segment. It uses the segment type,
// then the number of interfaces and then finally the ID of the interfaces to sort.
func (s *segment) lessThan(o *segment) bool {
segsLessThan := func(lhs, rhs *segment) bool {
for i := 0; i < len(lhs.interfaces); i++ {
if lhs.interfaces[i].IA != rhs.interfaces[i].IA {
return lhs.interfaces[i].IA.IAInt() < rhs.interfaces[i].IA.IAInt()
} else if lhs.interfaces[i].ifNum != rhs.interfaces[i].ifNum {
return lhs.interfaces[i].ifNum < rhs.interfaces[i].ifNum
}
}
return false
}
switch {
case s.SegType != o.SegType:
// reversed Type comparison so core < down < up
return s.SegType > o.SegType
case len(s.interfaces) != len(o.interfaces):
return len(s.interfaces) < len(o.interfaces)
default:
return segsLessThan(s, o)
}
}
func defaultDBfilename() (string, error) {
searchPath := "/etc/scion/gen-cache/"
glob := filepath.Join(searchPath, "ps*path.db")
filenames, err := filepath.Glob(glob)
if err != nil {
return "", fmt.Errorf("Error while listing files: %v", err)
}
if len(filenames) == 1 {
return filenames[0], nil
}
reason := "no"
if len(filenames) > 1 {
reason = "more than one"
}
return "", fmt.Errorf("Found %s files matching '%s'. "+
"Please specify the path to a DB file using the -db flag.", reason, glob)
}