github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv6/header.go (about) 1 // Copyright 2014 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. 4 5 package ipv6 6 7 import ( 8 "fmt" 9 "net" 10 ) 11 12 const ( 13 Version = 6 // protocol version 14 HeaderLen = 40 // header length 15 ) 16 17 // A Header represents an IPv6 base header. 18 type Header struct { 19 Version int // protocol version 20 TrafficClass int // traffic class 21 FlowLabel int // flow label 22 PayloadLen int // payload length 23 NextHeader int // next header 24 HopLimit int // hop limit 25 Src net.IP // source address 26 Dst net.IP // destination address 27 } 28 29 func (h *Header) String() string { 30 if h == nil { 31 return "<nil>" 32 } 33 return fmt.Sprintf("ver=%d tclass=%#x flowlbl=%#x payloadlen=%d nxthdr=%d hoplim=%d src=%v dst=%v", h.Version, h.TrafficClass, h.FlowLabel, h.PayloadLen, h.NextHeader, h.HopLimit, h.Src, h.Dst) 34 } 35 36 // ParseHeader parses b as an IPv6 base header. 37 func ParseHeader(b []byte) (*Header, error) { 38 if len(b) < HeaderLen { 39 return nil, errHeaderTooShort 40 } 41 h := &Header{ 42 Version: int(b[0]) >> 4, 43 TrafficClass: int(b[0]&0x0f)<<4 | int(b[1])>>4, 44 FlowLabel: int(b[1]&0x0f)<<16 | int(b[2])<<8 | int(b[3]), 45 PayloadLen: int(b[4])<<8 | int(b[5]), 46 NextHeader: int(b[6]), 47 HopLimit: int(b[7]), 48 } 49 h.Src = make(net.IP, net.IPv6len) 50 copy(h.Src, b[8:24]) 51 h.Dst = make(net.IP, net.IPv6len) 52 copy(h.Dst, b[24:40]) 53 return h, nil 54 }