github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv4/control.go (about) 1 // Copyright 2012 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 ipv4 6 7 import ( 8 "fmt" 9 "net" 10 "sync" 11 ) 12 13 type rawOpt struct { 14 sync.RWMutex 15 cflags ControlFlags 16 } 17 18 func (c *rawOpt) set(f ControlFlags) { c.cflags |= f } 19 func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f } 20 func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 } 21 22 type ControlFlags uint 23 24 const ( 25 FlagTTL ControlFlags = 1 << iota // pass the TTL on the received packet 26 FlagSrc // pass the source address on the received packet 27 FlagDst // pass the destination address on the received packet 28 FlagInterface // pass the interface index on the received packet 29 ) 30 31 // A ControlMessage represents per packet basis IP-level socket options. 32 type ControlMessage struct { 33 // Receiving socket options: SetControlMessage allows to 34 // receive the options from the protocol stack using ReadFrom 35 // method of PacketConn or RawConn. 36 // 37 // Specifying socket options: ControlMessage for WriteTo 38 // method of PacketConn or RawConn allows to send the options 39 // to the protocol stack. 40 // 41 TTL int // time-to-live, receiving only 42 Src net.IP // source address, specifying only 43 Dst net.IP // destination address, receiving only 44 IfIndex int // interface index, must be 1 <= value when specifying 45 } 46 47 func (cm *ControlMessage) String() string { 48 if cm == nil { 49 return "<nil>" 50 } 51 return fmt.Sprintf("ttl=%d src=%v dst=%v ifindex=%d", cm.TTL, cm.Src, cm.Dst, cm.IfIndex) 52 } 53 54 // Ancillary data socket options 55 const ( 56 ctlTTL = iota // header field 57 ctlSrc // header field 58 ctlDst // header field 59 ctlInterface // inbound or outbound interface 60 ctlPacketInfo // inbound or outbound packet path 61 ctlMax 62 ) 63 64 // A ctlOpt represents a binding for ancillary data socket option. 65 type ctlOpt struct { 66 name int // option name, must be equal or greater than 1 67 length int // option length 68 marshal func([]byte, *ControlMessage) []byte 69 parse func(*ControlMessage, []byte) 70 }