diff options
author | Andrea Barberio <insomniac@slackware.it> | 2017-12-11 09:08:55 +0000 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2017-12-11 09:08:55 +0000 |
commit | 5db858ce8450757dd7dff589c7d258213c3b7baa (patch) | |
tree | a7453167ff5466032aca36e762f55a15c7052891 | |
parent | 4877803635767fc1481e747921563cfeff6a1212 (diff) |
Refactored REQUEST code into NewRequestFromAdvertise
-rw-r--r-- | dhcpv6/client.go | 71 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message.go | 71 |
2 files changed, 75 insertions, 67 deletions
diff --git a/dhcpv6/client.go b/dhcpv6/client.go index 7d53334..e261688 100644 --- a/dhcpv6/client.go +++ b/dhcpv6/client.go @@ -141,74 +141,11 @@ func (c *Client) Solicit(ifname string, solicit DHCPv6) (DHCPv6, DHCPv6, error) // the request, a reply if not nil, and an error if any func (c *Client) Request(ifname string, advertise, request DHCPv6) (DHCPv6, DHCPv6, error) { if request == nil { - if advertise == nil { - return nil, nil, fmt.Errorf("ADVERTISE and REQUEST cannot be both nil") - } - if advertise.Type() != ADVERTISE { - return nil, nil, fmt.Errorf("The passed ADVERTISE must have ADVERTISE type set") - } - adv, ok := advertise.(*DHCPv6Message) - if !ok { - return nil, nil, fmt.Errorf("The passed ADVERTISE must be of DHCPv6Message type") - } - // build REQUEST from ADVERTISE - req := DHCPv6Message{} - req.SetMessage(REQUEST) - req.SetTransactionID(adv.TransactionID()) - // add Client ID - cid := adv.GetOneOption(OPTION_CLIENTID) - if cid == nil { - return nil, nil, fmt.Errorf("Client ID cannot be nil in ADVERTISE when building REQUEST") - } - req.AddOption(cid) - // add Server ID - sid := adv.GetOneOption(OPTION_SERVERID) - if sid == nil { - return nil, nil, fmt.Errorf("Server ID cannot be nil in ADVERTISE when building REQUEST") - } - req.AddOption(sid) - // add Elapsed Time - req.AddOption(&OptElapsedTime{}) - // add IA_NA - iaNa := adv.GetOneOption(OPTION_IA_NA) - if iaNa == nil { - return nil, nil, fmt.Errorf("IA_NA cannot be nil in ADVERTISE when building REQUEST") - } - req.AddOption(iaNa) - // add OptRequestedOption - oro := OptRequestedOption{} - oro.SetRequestedOptions([]OptionCode{ - OPT_BOOTFILE_URL, - OPT_BOOTFILE_PARAM, - }) - req.AddOption(&oro) - // add OPTION_NII - // TODO implement OptionNetworkInterfaceIdentifier - nii := OptionGeneric{ - OptionCode: OPTION_NII, - OptionData: []byte{ - 1, // UNDI - Universal Network Device Interface - 3, 2, // UNDI rev. 3.2 - second generation EFI runtime driver support, see rfc4578 - }, - } - req.AddOption(&nii) - // add OPTION_CLIENT_ARCH_TYPE - // TODO implement OptionClientArchType - cat := OptionGeneric{ - OptionCode: OPTION_CLIENT_ARCH_TYPE, - OptionData: []byte{ - 0, // Intel - see rfc4578 - 7, // EFI BC - }, - } - req.AddOption(&cat) - // add OPTION_VENDOR_CLASS, only if present in the original request - // TODO implement OptionVendorClass - vClass := adv.GetOneOption(OPTION_VENDOR_CLASS) - if vClass != nil { - req.AddOption(vClass) + var err error + request, err = NewRequestFromAdvertise(advertise) + if err != nil { + return nil, nil, err } - request = &req } reply, err := c.sendReceive(ifname, request) return request, reply, err diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 7151096..8368d6c 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -100,6 +100,77 @@ func NewSolicitForInterface(ifname string) (*DHCPv6Message, error) { return d, nil } +func NewRequestFromAdvertise(advertise DHCPv6) (DHCPv6, error) { + if advertise == nil { + return nil, fmt.Errorf("ADVERTISE cannot be nil") + } + if advertise.Type() != ADVERTISE { + return nil, fmt.Errorf("The passed ADVERTISE must have ADVERTISE type set") + } + adv, ok := advertise.(*DHCPv6Message) + if !ok { + return nil, fmt.Errorf("The passed ADVERTISE must be of DHCPv6Message type") + } + // build REQUEST from ADVERTISE + req := DHCPv6Message{} + req.SetMessage(REQUEST) + req.SetTransactionID(adv.TransactionID()) + // add Client ID + cid := adv.GetOneOption(OPTION_CLIENTID) + if cid == nil { + return nil, fmt.Errorf("Client ID cannot be nil in ADVERTISE when building REQUEST") + } + req.AddOption(cid) + // add Server ID + sid := adv.GetOneOption(OPTION_SERVERID) + if sid == nil { + return nil, fmt.Errorf("Server ID cannot be nil in ADVERTISE when building REQUEST") + } + req.AddOption(sid) + // add Elapsed Time + req.AddOption(&OptElapsedTime{}) + // add IA_NA + iaNa := adv.GetOneOption(OPTION_IA_NA) + if iaNa == nil { + return nil, fmt.Errorf("IA_NA cannot be nil in ADVERTISE when building REQUEST") + } + req.AddOption(iaNa) + // add OptRequestedOption + oro := OptRequestedOption{} + oro.SetRequestedOptions([]OptionCode{ + OPT_BOOTFILE_URL, + OPT_BOOTFILE_PARAM, + }) + req.AddOption(&oro) + // add OPTION_NII + // TODO implement OptionNetworkInterfaceIdentifier + nii := OptionGeneric{ + OptionCode: OPTION_NII, + OptionData: []byte{ + 1, // UNDI - Universal Network Device Interface + 3, 2, // UNDI rev. 3.2 - second generation EFI runtime driver support, see rfc4578 + }, + } + req.AddOption(&nii) + // add OPTION_CLIENT_ARCH_TYPE + // TODO implement OptionClientArchType + cat := OptionGeneric{ + OptionCode: OPTION_CLIENT_ARCH_TYPE, + OptionData: []byte{ + 0, // Intel - see rfc4578 + 7, // EFI BC + }, + } + req.AddOption(&cat) + // add OPTION_VENDOR_CLASS, only if present in the original request + // TODO implement OptionVendorClass + vClass := adv.GetOneOption(OPTION_VENDOR_CLASS) + if vClass != nil { + req.AddOption(vClass) + } + return &req, nil +} + func (d *DHCPv6Message) Type() MessageType { return d.messageType } |