v4 mpls实装显示

This commit is contained in:
tsosunchia
2023-10-05 06:42:04 +08:00
parent 13aaa54067
commit db1e9e2b0c
4 changed files with 31 additions and 3 deletions

View File

@@ -108,4 +108,8 @@ func applyLangSetting(h *trace.Hop) {
}
}
}
if len(h.MPLS) > 1 {
h.Hostname += " [MPLS: " + h.MPLS + "]"
}
}

View File

@@ -47,6 +47,10 @@ func HopPrinter(h trace.Hop, info HopInfo) {
if h.Geo != nil {
txt += " " + formatIpGeoData(h.Address.String(), h.Geo)
}
if len(h.MPLS) > 1 {
txt += " [MPLS: " + h.MPLS + "]"
}
switch info {
case IXP:
fmt.Print(CYAN_PREFIX)

View File

@@ -175,6 +175,9 @@ func (t *ICMPTracer) listenICMP() {
func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data []byte, ttl int) {
t.inflightRequestRWLock.RLock()
defer t.inflightRequestRWLock.RUnlock()
label, err := int64(0), error(nil)
tc, s, ttlMpls := int64(0), "", int64(0)
mpls := ""
extensionOffset := 20 + 8 + 52
@@ -185,12 +188,27 @@ func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data
//log.Println("ICMP Multi-Part Extensions detected for TTL:", ttl)
//这里感觉会有问题,能力所限先这样吧,等有缘人来改
tmp := fmt.Sprintf("%x", msg.Msg[:*msg.N])
substring := tmp[len(tmp)-7 : len(tmp)-3]
label, err := strconv.ParseInt(substring, 16, 32)
substring := tmp[len(tmp)-8 : len(tmp)-3]
label, err = strconv.ParseInt(substring, 16, 32)
if err != nil {
return
}
log.Printf("MPLS for TTL %d: Lbl %d\n", ttl, label)
strSlice := []byte(tmp[len(tmp)-3 : len(tmp)-2])
charValue := int(strSlice[0] - '0')
binaryStr := fmt.Sprintf("%04b", charValue)
tc, err = strconv.ParseInt(binaryStr[:3], 2, 32)
if err != nil {
return
}
s = binaryStr[3:]
substring = tmp[len(tmp)-2:]
ttlMpls, err = strconv.ParseInt(substring, 16, 32)
if err != nil {
return
}
//fmt.Printf("MPLS for TTL %d: Lbl %d, TC %d, S %s, TTL %d\n", ttl, label, tc, s, ttlMpls)
mpls = fmt.Sprintf("Lbl %d, TC %d, S %s, TTL %d", label, tc, s, ttlMpls)
}
}
@@ -198,6 +216,7 @@ func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data
t.inflightRequest[ttl] <- Hop{
Success: true,
Address: msg.Peer,
MPLS: mpls,
}
}
}

View File

@@ -124,6 +124,7 @@ type Hop struct {
Error error
Geo *ipgeo.IPGeoData
Lang string
MPLS string
}
func (h *Hop) fetchIPData(c Config) (err error) {