Exemple #1
0
// setOptions sets dhcp options on a dhcp packet
func (c *client) setOptions(p dhcp4.Packet) (dhcp4.Packet, error) {
	defer trace.End(trace.Begin(""))

	dirty := false
	opts := p.ParseOptions()

	// the current parameter request list
	rl := opts[dhcp4.OptionParameterRequestList]
	// figure out if there are any new parameters
	for _, p := range c.params {
		if bytes.IndexByte(rl, p) == -1 {
			dirty = true
			rl = append(rl, p)
		}
	}

	opts[dhcp4.OptionParameterRequestList] = rl

	if _, ok := opts[dhcp4.OptionClientIdentifier]; !ok {
		b, err := c.id.MarshalBinary()
		if err != nil {
			return p, err
		}

		opts[dhcp4.OptionClientIdentifier] = b
		dirty = true
	}

	// finally reset the options on the packet, if necessary
	if dirty {
		// strip out all options, and add them back in with the new changed options;
		// this is the only way currently to delete/modify a packet option
		p.StripOptions()
		// have to copy since values in opts (of type []byte) are still pointing into p
		var newp dhcp4.Packet
		newp = make([]byte, len(p))
		copy(newp, p)
		log.Debugf("opts=%#v", opts)
		for o, v := range opts {
			newp.AddOption(o, v)
		}

		p = newp
	}

	return p, nil
}