github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv6/control.go (about)

     1  // Copyright 2013 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  	"sync"
    11  )
    12  
    13  // Note that RFC 3542 obsoletes RFC 2292 but OS X Snow Leopard and the
    14  // former still support RFC 2292 only.  Please be aware that almost
    15  // all protocol implementations prohibit using a combination of RFC
    16  // 2292 and RFC 3542 for some practical reasons.
    17  
    18  type rawOpt struct {
    19  	sync.RWMutex
    20  	cflags ControlFlags
    21  }
    22  
    23  func (c *rawOpt) set(f ControlFlags)        { c.cflags |= f }
    24  func (c *rawOpt) clear(f ControlFlags)      { c.cflags &^= f }
    25  func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
    26  
    27  // A ControlFlags represents per packet basis IP-level socket option
    28  // control flags.
    29  type ControlFlags uint
    30  
    31  const (
    32  	FlagTrafficClass ControlFlags = 1 << iota // pass the traffic class on the received packet
    33  	FlagHopLimit                              // pass the hop limit on the received packet
    34  	FlagSrc                                   // pass the source address on the received packet
    35  	FlagDst                                   // pass the destination address on the received packet
    36  	FlagInterface                             // pass the interface index on the received packet
    37  	FlagPathMTU                               // pass the path MTU on the received packet path
    38  )
    39  
    40  const flagPacketInfo = FlagDst | FlagInterface
    41  
    42  // A ControlMessage represents per packet basis IP-level socket
    43  // options.
    44  type ControlMessage struct {
    45  	// Receiving socket options: SetControlMessage allows to
    46  	// receive the options from the protocol stack using ReadFrom
    47  	// method of PacketConn.
    48  	//
    49  	// Specifying socket options: ControlMessage for WriteTo
    50  	// method of PacketConn allows to send the options to the
    51  	// protocol stack.
    52  	//
    53  	TrafficClass int    // traffic class, must be 1 <= value <= 255 when specifying
    54  	HopLimit     int    // hop limit, must be 1 <= value <= 255 when specifying
    55  	Src          net.IP // source address, specifying only
    56  	Dst          net.IP // destination address, receiving only
    57  	IfIndex      int    // interface index, must be 1 <= value when specifying
    58  	NextHop      net.IP // next hop address, specifying only
    59  	MTU          int    // path MTU, receiving only
    60  }
    61  
    62  func (cm *ControlMessage) String() string {
    63  	if cm == nil {
    64  		return "<nil>"
    65  	}
    66  	return fmt.Sprintf("tclass=%#x hoplim=%d src=%v dst=%v ifindex=%d nexthop=%v mtu=%d", cm.TrafficClass, cm.HopLimit, cm.Src, cm.Dst, cm.IfIndex, cm.NextHop, cm.MTU)
    67  }
    68  
    69  // Ancillary data socket options
    70  const (
    71  	ctlTrafficClass = iota // header field
    72  	ctlHopLimit            // header field
    73  	ctlPacketInfo          // inbound or outbound packet path
    74  	ctlNextHop             // nexthop
    75  	ctlPathMTU             // path mtu
    76  	ctlMax
    77  )
    78  
    79  // A ctlOpt represents a binding for ancillary data socket option.
    80  type ctlOpt struct {
    81  	name    int // option name, must be equal or greater than 1
    82  	length  int // option length
    83  	marshal func([]byte, *ControlMessage) []byte
    84  	parse   func(*ControlMessage, []byte)
    85  }