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.
12 "golang.org/x/net/internal/iana"
13 "golang.org/x/net/internal/socket"
21 func (c *rawOpt) set(f ControlFlags) { c.cflags |= f }
22 func (c *rawOpt) clear(f ControlFlags) { c.cflags &^= f }
23 func (c *rawOpt) isset(f ControlFlags) bool { return c.cflags&f != 0 }
25 type ControlFlags uint
28 FlagTTL ControlFlags = 1 << iota // pass the TTL on the received packet
29 FlagSrc // pass the source address on the received packet
30 FlagDst // pass the destination address on the received packet
31 FlagInterface // pass the interface index on the received packet
34 // A ControlMessage represents per packet basis IP-level socket options.
35 type ControlMessage struct {
36 // Receiving socket options: SetControlMessage allows to
37 // receive the options from the protocol stack using ReadFrom
38 // method of PacketConn or RawConn.
40 // Specifying socket options: ControlMessage for WriteTo
41 // method of PacketConn or RawConn allows to send the options
42 // to the protocol stack.
44 TTL int // time-to-live, receiving only
45 Src net.IP // source address, specifying only
46 Dst net.IP // destination address, receiving only
47 IfIndex int // interface index, must be 1 <= value when specifying
50 func (cm *ControlMessage) String() string {
54 return fmt.Sprintf("ttl=%d src=%v dst=%v ifindex=%d", cm.TTL, cm.Src, cm.Dst, cm.IfIndex)
57 // Marshal returns the binary encoding of cm.
58 func (cm *ControlMessage) Marshal() []byte {
62 var m socket.ControlMessage
63 if ctlOpts[ctlPacketInfo].name > 0 && (cm.Src.To4() != nil || cm.IfIndex > 0) {
64 m = socket.NewControlMessage([]int{ctlOpts[ctlPacketInfo].length})
67 ctlOpts[ctlPacketInfo].marshal(m, cm)
72 // Parse parses b as a control message and stores the result in cm.
73 func (cm *ControlMessage) Parse(b []byte) error {
74 ms, err := socket.ControlMessage(b).Parse()
78 for _, m := range ms {
79 lvl, typ, l, err := m.ParseHeader()
83 if lvl != iana.ProtocolIP {
87 case typ == ctlOpts[ctlTTL].name && l >= ctlOpts[ctlTTL].length:
88 ctlOpts[ctlTTL].parse(cm, m.Data(l))
89 case typ == ctlOpts[ctlDst].name && l >= ctlOpts[ctlDst].length:
90 ctlOpts[ctlDst].parse(cm, m.Data(l))
91 case typ == ctlOpts[ctlInterface].name && l >= ctlOpts[ctlInterface].length:
92 ctlOpts[ctlInterface].parse(cm, m.Data(l))
93 case typ == ctlOpts[ctlPacketInfo].name && l >= ctlOpts[ctlPacketInfo].length:
94 ctlOpts[ctlPacketInfo].parse(cm, m.Data(l))
100 // NewControlMessage returns a new control message.
102 // The returned message is large enough for options specified by cf.
103 func NewControlMessage(cf ControlFlags) []byte {
104 opt := rawOpt{cflags: cf}
106 if opt.isset(FlagTTL) && ctlOpts[ctlTTL].name > 0 {
107 l += socket.ControlMessageSpace(ctlOpts[ctlTTL].length)
109 if ctlOpts[ctlPacketInfo].name > 0 {
110 if opt.isset(FlagSrc | FlagDst | FlagInterface) {
111 l += socket.ControlMessageSpace(ctlOpts[ctlPacketInfo].length)
114 if opt.isset(FlagDst) && ctlOpts[ctlDst].name > 0 {
115 l += socket.ControlMessageSpace(ctlOpts[ctlDst].length)
117 if opt.isset(FlagInterface) && ctlOpts[ctlInterface].name > 0 {
118 l += socket.ControlMessageSpace(ctlOpts[ctlInterface].length)
128 // Ancillary data socket options
130 ctlTTL = iota // header field
131 ctlSrc // header field
132 ctlDst // header field
133 ctlInterface // inbound or outbound interface
134 ctlPacketInfo // inbound or outbound packet path
138 // A ctlOpt represents a binding for ancillary data socket option.
140 name int // option name, must be equal or greater than 1
141 length int // option length
142 marshal func([]byte, *ControlMessage) []byte
143 parse func(*ControlMessage, []byte)