// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ipv4

import (
	
	
	

	
	
)

type rawOpt struct {
	sync.RWMutex
	cflags ControlFlags
}

func ( *rawOpt) ( ControlFlags)        { .cflags |=  }
func ( *rawOpt) ( ControlFlags)      { .cflags &^=  }
func ( *rawOpt) ( ControlFlags) bool { return .cflags& != 0 }

type ControlFlags uint

const (
	FlagTTL       ControlFlags = 1 << iota // pass the TTL on the received packet
	FlagSrc                                // pass the source address on the received packet
	FlagDst                                // pass the destination address on the received packet
	FlagInterface                          // pass the interface index on the received packet
)

// A ControlMessage represents per packet basis IP-level socket options.
type ControlMessage struct {
	// Receiving socket options: SetControlMessage allows to
	// receive the options from the protocol stack using ReadFrom
	// method of PacketConn or RawConn.
	//
	// Specifying socket options: ControlMessage for WriteTo
	// method of PacketConn or RawConn allows to send the options
	// to the protocol stack.
	//
	TTL     int    // time-to-live, receiving only
	Src     net.IP // source address, specifying only
	Dst     net.IP // destination address, receiving only
	IfIndex int    // interface index, must be 1 <= value when specifying
}

func ( *ControlMessage) () string {
	if  == nil {
		return "<nil>"
	}
	return fmt.Sprintf("ttl=%d src=%v dst=%v ifindex=%d", .TTL, .Src, .Dst, .IfIndex)
}

// Marshal returns the binary encoding of cm.
func ( *ControlMessage) () []byte {
	if  == nil {
		return nil
	}
	var  socket.ControlMessage
	if ctlOpts[ctlPacketInfo].name > 0 && (.Src.To4() != nil || .IfIndex > 0) {
		 = socket.NewControlMessage([]int{ctlOpts[ctlPacketInfo].length})
	}
	if len() > 0 {
		ctlOpts[ctlPacketInfo].marshal(, )
	}
	return 
}

// Parse parses b as a control message and stores the result in cm.
func ( *ControlMessage) ( []byte) error {
	,  := socket.ControlMessage().Parse()
	if  != nil {
		return 
	}
	for ,  := range  {
		, , ,  := .ParseHeader()
		if  != nil {
			return 
		}
		if  != iana.ProtocolIP {
			continue
		}
		switch {
		case  == ctlOpts[ctlTTL].name &&  >= ctlOpts[ctlTTL].length:
			ctlOpts[ctlTTL].parse(, .Data())
		case  == ctlOpts[ctlDst].name &&  >= ctlOpts[ctlDst].length:
			ctlOpts[ctlDst].parse(, .Data())
		case  == ctlOpts[ctlInterface].name &&  >= ctlOpts[ctlInterface].length:
			ctlOpts[ctlInterface].parse(, .Data())
		case  == ctlOpts[ctlPacketInfo].name &&  >= ctlOpts[ctlPacketInfo].length:
			ctlOpts[ctlPacketInfo].parse(, .Data())
		}
	}
	return nil
}

// NewControlMessage returns a new control message.
//
// The returned message is large enough for options specified by cf.
func ( ControlFlags) []byte {
	 := rawOpt{cflags: }
	var  int
	if .isset(FlagTTL) && ctlOpts[ctlTTL].name > 0 {
		 += socket.ControlMessageSpace(ctlOpts[ctlTTL].length)
	}
	if ctlOpts[ctlPacketInfo].name > 0 {
		if .isset(FlagSrc | FlagDst | FlagInterface) {
			 += socket.ControlMessageSpace(ctlOpts[ctlPacketInfo].length)
		}
	} else {
		if .isset(FlagDst) && ctlOpts[ctlDst].name > 0 {
			 += socket.ControlMessageSpace(ctlOpts[ctlDst].length)
		}
		if .isset(FlagInterface) && ctlOpts[ctlInterface].name > 0 {
			 += socket.ControlMessageSpace(ctlOpts[ctlInterface].length)
		}
	}
	var  []byte
	if  > 0 {
		 = make([]byte, )
	}
	return 
}

// Ancillary data socket options
const (
	ctlTTL        = iota // header field
	ctlSrc               // header field
	ctlDst               // header field
	ctlInterface         // inbound or outbound interface
	ctlPacketInfo        // inbound or outbound packet path
	ctlMax
)

// A ctlOpt represents a binding for ancillary data socket option.
type ctlOpt struct {
	name    int // option name, must be equal or greater than 1
	length  int // option length
	marshal func([]byte, *ControlMessage) []byte
	parse   func(*ControlMessage, []byte)
}