github.com/jlowellwofford/u-root@v1.0.0/pkg/dhclient/dhcp6.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dhclient
     6  
     7  import (
     8  	"net"
     9  	"net/url"
    10  	"strings"
    11  
    12  	"github.com/mdlayher/dhcp6"
    13  	"github.com/mdlayher/dhcp6/dhcp6opts"
    14  )
    15  
    16  // Packet6 implements Packet for IPv6 DHCP.
    17  type Packet6 struct {
    18  	p    *dhcp6.Packet
    19  	iana *dhcp6opts.IANA
    20  }
    21  
    22  // NewPacket6 wraps a DHCPv6 packet with some convenience methods.
    23  func NewPacket6(p *dhcp6.Packet, ianaLease *dhcp6opts.IANA) *Packet6 {
    24  	return &Packet6{
    25  		p:    p,
    26  		iana: ianaLease,
    27  	}
    28  }
    29  
    30  // Lease returns lease information assigned.
    31  func (p *Packet6) Lease() *dhcp6opts.IAAddr {
    32  	// TODO: Can a DHCPv6 server return multiple IAAddrs for one IANA?
    33  	// There certainly doesn't seem to be a way to request multiple other
    34  	// than requesting multiple IANAs.
    35  	iaAddrs, err := dhcp6opts.GetIAAddr(p.iana.Options)
    36  	if err != nil || len(iaAddrs) == 0 {
    37  		return nil
    38  	}
    39  
    40  	return iaAddrs[0]
    41  }
    42  
    43  // DNS returns DNS servers assigned.
    44  func (p *Packet6) DNS() []net.IP {
    45  	// TODO: Would the IANA contain this, or the packet?
    46  	ips, err := dhcp6opts.GetDNSServers(p.p.Options)
    47  	if err != nil {
    48  		return nil
    49  	}
    50  	return []net.IP(ips)
    51  }
    52  
    53  // Boot returns the boot file URL and parameters assigned.
    54  //
    55  // TODO: RFC 5970 is helpfully avoidant of where these options are used. Are
    56  // they added to the packet? Are they added to an IANA?  It *seems* like it's
    57  // in the packet.
    58  func (p *Packet6) Boot() (url.URL, string, error) {
    59  	uri, err := dhcp6opts.GetBootFileURL(p.p.Options)
    60  	if err != nil {
    61  		return url.URL{}, "", err
    62  	}
    63  
    64  	// Having this value is optional.
    65  	bfp, err := dhcp6opts.GetBootFileParam(p.p.Options)
    66  	if err != dhcp6.ErrOptionNotPresent {
    67  		return url.URL{}, "", err
    68  	}
    69  
    70  	var cmdline string
    71  	if bfp != nil {
    72  		cmdline = strings.Join(bfp, " ")
    73  	}
    74  	return url.URL(*uri), cmdline, nil
    75  }