75 lines
1.1 KiB
Go
75 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
`fmt`
|
|
`log`
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
/*
|
|
This is a nonsensical example.
|
|
The actual values are sort of scattered around a multitude of RFCs.
|
|
*/
|
|
deBits string = "01010101" // [0101 0101], or 85 (0x55)
|
|
)
|
|
|
|
var (
|
|
deLowerMask uint8 = 0x03 // 3
|
|
)
|
|
|
|
func ToDE(dscp, ecn uint8) (de byte) {
|
|
|
|
de = (dscp << 2) | (ecn & deLowerMask)
|
|
|
|
return
|
|
}
|
|
|
|
func FromDE(de byte) (dscp, ecn uint8) {
|
|
|
|
dscp = de >> 2
|
|
ecn = de & deLowerMask
|
|
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
var u64 uint64
|
|
var de byte
|
|
var dscp uint8
|
|
var ecn uint8
|
|
|
|
// Given a D/E of deBits (see const at top)...
|
|
if u64, err = strconv.ParseUint(deBits, 2, 8); err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
de = uint8(u64)
|
|
// Prints:
|
|
/*
|
|
D/E is: 85 (0x55)
|
|
*/
|
|
fmt.Printf("D/E is:\t\t%d (%#02x)\n", de, de)
|
|
|
|
dscp, ecn = FromDE(de)
|
|
// Prints:
|
|
/*
|
|
DSCP: 21 (0x15)
|
|
ECN: 1 (0x01)
|
|
*/
|
|
fmt.Printf(
|
|
"DSCP:\t\t%d (%#02x)\n"+
|
|
"ECN:\t\t%d (%#02x)\n",
|
|
dscp, dscp,
|
|
ecn, ecn,
|
|
)
|
|
|
|
de = ToDE(dscp, ecn)
|
|
// Prints:
|
|
/*
|
|
Confirmed D/E: 85 (0x55)
|
|
*/
|
|
fmt.Printf("Confirmed D/E:\t%d (%#02x)\n", de, de)
|
|
}
|