func (dp *DhcpPacket) ReplaceOption(optionToReplace option.SpecificOption) (err error) { done := false for i := len(dp.Options) - 2; 0 <= i; i-- { if dp.Options[i].GetType() == optionToReplace.GetNum() { rawOpt := dp.Options[i] // test whether this option is included into raw packet's data idx, err := rawOpt.GetIndexInto(dp.Raw) if err != nil { // this option is not included ??? continue } if done { // remove old data option from rawData. // It is used when several time same option in same packet // For example RFC 3396 : Encoding Long Options in the Dynamic Host Configuration Protocol (DHCPv4) dp.Raw = append(dp.Raw[:idx], dp.Raw[idx+int(rawOpt.GetRawLen()):]...) } else { // insert option into rawData dp.Raw = append(dp.Raw[:idx], append(optionToReplace.GetRawOption().GetRawValue(), dp.Raw[idx+int(rawOpt.GetRawLen()):]...)...) done = true } } } if done { dp.Options, err = option.ParseOptions(dp.Raw[240:]) //240th byte is where options are } else { err = dherrors.OptNotFound } return }
func (dp *DhcpPacket) AddOptionBefore(optionToAdd, beforeOption option.SpecificOption) (err error) { if !optionToAdd.GetRawOption().IsValid() { return dherrors.MalformedOption } done := false for i := len(dp.Options) - 2; 0 <= i; i-- { if dp.Options[i].GetType() != beforeOption.GetNum() { continue } if 0 < i && dp.Options[i-1].GetType() == beforeOption.GetNum() { // we don't insert here as this option use RFC 3396 : Encoding Long Options in the Dynamic Host Configuration Protocol (DHCPv4) continue } done = true rawOpt := dp.Options[i] // test whether this option is included into raw packet's data idx, err := rawOpt.GetIndexInto(dp.Raw) if err != nil { // this option is not included ??? continue } // insert data before option into rawData dp.Raw = append(dp.Raw[:idx], append(optionToAdd.GetRawOption().GetRawValue(), dp.Raw[idx:]...)...) break } if done { dp.Options, err = option.ParseOptions(dp.Raw[240:]) //240th byte is where options are } else { err = dherrors.OptNotFound } return }
func (dp *DhcpPacket) ContainOption(optionToCheck option.SpecificOption) bool { for _, cOption := range dp.Options { if cOption.GetType() == optionToCheck.GetNum() { return true } } return false }
func (dp DhcpPacket) GetOption(specOpt option.SpecificOption) (bool, error) { genOpt, found := option.GetRawOption(dp.Options, specOpt.GetNum()) if found { err := specOpt.Parse(genOpt) if err != nil { return false, err } return true, nil } return false, nil }