1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 import "encoding/binary"
9 // A MPLSLabel represents a MPLS label stack entry.
10 type MPLSLabel struct {
11 Label int // label value
12 TC int // traffic class; formerly experimental use
13 S bool // bottom of stack
14 TTL int // time to live
18 classMPLSLabelStack = 1
19 typeIncomingMPLSLabelStack = 1
22 // A MPLSLabelStack represents a MPLS label stack.
23 type MPLSLabelStack struct {
24 Class int // extension object class number
25 Type int // extension object sub-type
29 // Len implements the Len method of Extension interface.
30 func (ls *MPLSLabelStack) Len(proto int) int {
31 return 4 + (4 * len(ls.Labels))
34 // Marshal implements the Marshal method of Extension interface.
35 func (ls *MPLSLabelStack) Marshal(proto int) ([]byte, error) {
36 b := make([]byte, ls.Len(proto))
37 if err := ls.marshal(proto, b); err != nil {
43 func (ls *MPLSLabelStack) marshal(proto int, b []byte) error {
45 binary.BigEndian.PutUint16(b[:2], uint16(l))
46 b[2], b[3] = classMPLSLabelStack, typeIncomingMPLSLabelStack
48 for _, ll := range ls.Labels {
49 b[off], b[off+1], b[off+2] = byte(ll.Label>>12), byte(ll.Label>>4&0xff), byte(ll.Label<<4&0xf0)
50 b[off+2] |= byte(ll.TC << 1 & 0x0e)
54 b[off+3] = byte(ll.TTL)
60 func parseMPLSLabelStack(b []byte) (Extension, error) {
61 ls := &MPLSLabelStack{
65 for b = b[4:]; len(b) >= 4; b = b[4:] {
67 Label: int(b[0])<<12 | int(b[1])<<4 | int(b[2])>>4,
68 TC: int(b[2]&0x0e) >> 1,
74 ls.Labels = append(ls.Labels, ll)