Example #1
0
File: client.go Project: vmware/vic
// 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
}
Example #2
0
func (c *client) appendOptions(p *dhcp4.Packet, id ID) (*dhcp4.Packet, error) {
	p.AddOption(
		dhcp4.OptionParameterRequestList,
		[]byte{
			byte(dhcp4.OptionSubnetMask),
			byte(dhcp4.OptionRouter),
			byte(dhcp4.OptionDomainNameServer),
		},
	)
	b, err := id.MarshalBinary()
	if err != nil {
		return nil, err
	}

	p.AddOption(dhcp4.OptionClientIdentifier, b)
	return p, nil
}