forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recordpath.go
145 lines (136 loc) · 4.1 KB
/
recordpath.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
// Copyright 2018 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.
package recordpath
import (
"fmt"
"os"
"time"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/hpkt"
"github.com/scionproto/scion/go/lib/layers"
"github.com/scionproto/scion/go/lib/sciond"
"github.com/scionproto/scion/go/lib/scmp"
"github.com/scionproto/scion/go/lib/spkt"
"github.com/scionproto/scion/go/tools/scmp/cmn"
)
var (
id uint64
)
func Run() {
var n, pktLen int
var ext common.Extension
cmn.SetupSignals(nil)
if cmn.PathEntry != nil {
n = len(cmn.PathEntry.Path.Interfaces)
ext = &layers.ExtnSCMP{Error: false, HopByHop: true}
}
entries := make([]*scmp.RecordPathEntry, 0, n)
id = cmn.Rand()
info := &scmp.InfoRecordPath{Id: id, Entries: entries}
pkt := cmn.NewSCMPPkt(scmp.T_G_RecordPathRequest, info, ext)
b := make(common.RawBytes, cmn.Mtu)
nhAddr := cmn.NextHopAddr()
ts := time.Now()
cmn.UpdatePktTS(pkt, ts)
// Serialize packet to internal buffer
pktLen, err := hpkt.WriteScnPkt(pkt, b)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to serialize SCION packet %v\n", err)
return
}
// Send packet
written, err := cmn.Conn.WriteTo(b[:pktLen], nhAddr)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Unable to write %v\n", err)
return
} else if written != pktLen {
fmt.Fprintf(os.Stderr, "ERROR: Wrote incomplete message. written=%d, expected=%d\n",
len(b), written)
return
}
cmn.Stats.Sent += 1
// Receive packet with timeout
cmn.Conn.SetReadDeadline(ts.Add(cmn.Timeout))
pktLen, err = cmn.Conn.Read(b)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
return
}
cmn.Stats.Recv += 1
now := time.Now()
// Parse packet
pktRecv := &spkt.ScnPkt{}
err = hpkt.ParseScnPkt(pktRecv, b[:pktLen])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: SCION packet parse error: %v\n", err)
return
}
// Validate packet
var scmpHdr *scmp.Hdr
scmpHdr, info, err = validate(pktRecv, cmn.PathEntry)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
return
}
// Calculate return time
rtt := now.Sub(scmpHdr.Time()).Round(time.Microsecond)
prettyPrint(pktRecv, pktLen, info, rtt)
}
func prettyPrint(pkt *spkt.ScnPkt, pktLen int, info *scmp.InfoRecordPath, rtt time.Duration) {
fmt.Printf("%d bytes from %s,[%s] time=%s Hops=%d\n",
pktLen, pkt.SrcIA, pkt.SrcHost, rtt, info.NumHops())
for i, e := range info.Entries {
fmt.Printf(" %2d. %s\n", i+1, e.String())
}
}
func validate(pkt *spkt.ScnPkt, pathEntry *sciond.PathReplyEntry) (*scmp.Hdr,
*scmp.InfoRecordPath, error) {
scmpHdr, scmpPld, err := cmn.Validate(pkt)
if err != nil {
return nil, nil, err
}
info, ok := scmpPld.Info.(*scmp.InfoRecordPath)
if !ok {
return nil, nil,
common.NewBasicError("Not an Info RecordPath", nil, "type", common.TypeOf(scmpPld.Info))
}
if info.Id != id {
return nil, nil,
common.NewBasicError("Wrong SCMP ID", nil, "expected", id, "actual", info.Id)
}
if pathEntry == nil {
return scmpHdr, info, nil
}
interfaces := pathEntry.Path.Interfaces
if len(info.Entries) != len(interfaces) {
return nil, nil,
common.NewBasicError("Invalid number of entries", nil,
"Expected", len(interfaces), "Actual", len(info.Entries))
}
for i, e := range info.Entries {
ia := interfaces[i].RawIsdas.IA()
if e.IA != ia {
return nil, nil,
common.NewBasicError("Invalid ISD-AS", nil, "entry", i,
"Expected", ia, "Actual", e.IA)
}
ifid := common.IFIDType(interfaces[i].IfID)
if e.IfID != ifid {
return nil, nil,
common.NewBasicError("Invalid IfID", nil, "entry", i,
"Expected", ifid, "Actual", e.IfID)
}
}
return scmpHdr, info, nil
}