mirror of
https://github.com/danderson/netboot.git
synced 2025-08-10 08:37:10 +02:00
gofmt -s all the things again.
This commit is contained in:
parent
bcaa633b65
commit
7de0228ec0
@ -1,9 +1,9 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"net"
|
||||
"golang.org/x/net/ipv6"
|
||||
"fmt"
|
||||
"golang.org/x/net/ipv6"
|
||||
"net"
|
||||
)
|
||||
|
||||
// Conn is dhcpv6-specific socket
|
||||
@ -113,7 +113,8 @@ func (c *Conn) SendDHCP(dst net.IP, p []byte) error {
|
||||
IP: dst,
|
||||
Port: 546,
|
||||
}
|
||||
_, err := c.conn.WriteTo(p, nil, dstAddr); if err != nil {
|
||||
_, err := c.conn.WriteTo(p, nil, dstAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error sending a reply to %s: %s", dst.String(), err)
|
||||
}
|
||||
return nil
|
||||
|
@ -1,9 +1,9 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"net"
|
||||
)
|
||||
|
||||
@ -110,8 +110,8 @@ func UnmarshalOption(bs []byte) (*Option, error) {
|
||||
// HumanReadable presents DHCPv6 options in a human-readable form
|
||||
func (o Options) HumanReadable() []string {
|
||||
ret := make([]string, 0, len(o))
|
||||
for _, multipleOptions := range(o) {
|
||||
for _, option := range(multipleOptions) {
|
||||
for _, multipleOptions := range o {
|
||||
for _, option := range multipleOptions {
|
||||
switch option.ID {
|
||||
case 3:
|
||||
ret = append(ret, o.humanReadableIaNa(*option)...)
|
||||
@ -137,7 +137,6 @@ func (o Options) humanReadableIaNa(opt Option) []string {
|
||||
l := uint16(binary.BigEndian.Uint16(iaOptions[2:4]))
|
||||
id := uint16(binary.BigEndian.Uint16(iaOptions[0:2]))
|
||||
|
||||
|
||||
switch id {
|
||||
case OptIaAddr:
|
||||
ip := make(net.IP, 16)
|
||||
@ -157,7 +156,8 @@ func (o Options) humanReadableIaNa(opt Option) []string {
|
||||
|
||||
// Add adds an option to Options
|
||||
func (o Options) Add(option *Option) {
|
||||
_, present := o[option.ID]; if !present {
|
||||
_, present := o[option.ID]
|
||||
if !present {
|
||||
o[option.ID] = make([]*Option, 0)
|
||||
}
|
||||
o[option.ID] = append(o[option.ID], option)
|
||||
@ -206,8 +206,8 @@ func MakeDNSServersOption(addresses []net.IP) *Option {
|
||||
// Marshal serializes Options
|
||||
func (o Options) Marshal() ([]byte, error) {
|
||||
buffer := bytes.NewBuffer(make([]byte, 0, 1446))
|
||||
for _, multipleOptions := range(o) {
|
||||
for _, o := range (multipleOptions) {
|
||||
for _, multipleOptions := range o {
|
||||
for _, o := range multipleOptions {
|
||||
serialized, err := o.Marshal()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error serializing option value: %s", err)
|
||||
@ -243,7 +243,8 @@ func (o *Option) Marshal() ([]byte, error) {
|
||||
func (o Options) UnmarshalOptionRequestOption() map[uint16]bool {
|
||||
ret := make(map[uint16]bool)
|
||||
|
||||
_, present := o[OptOro]; if !present {
|
||||
_, present := o[OptOro]
|
||||
if !present {
|
||||
return ret
|
||||
}
|
||||
|
||||
@ -315,7 +316,7 @@ func (o Options) IaNaIDs() [][]byte {
|
||||
options, exists := o[OptIaNa]
|
||||
ret := make([][]byte, 0)
|
||||
if exists {
|
||||
for _, option := range(options) {
|
||||
for _, option := range options {
|
||||
ret = append(ret, option.Value[0:4])
|
||||
}
|
||||
return ret
|
||||
@ -340,4 +341,3 @@ func (o Options) BootFileURL() []byte {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMarshalOption(t *testing.T) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// MessageType contains ID identifying DHCP message type. See RFC 3315
|
||||
|
@ -1,8 +1,8 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"encoding/binary"
|
||||
"hash/fnv"
|
||||
"net"
|
||||
)
|
||||
|
||||
@ -59,7 +59,7 @@ func (b *PacketBuilder) makeMsgAdvertise(transactionID [3]byte, serverDUID, clie
|
||||
associations []*IdentityAssociation, bootFileURL, preference []byte, dnsServers []net.IP) *Packet {
|
||||
retOptions := make(Options)
|
||||
retOptions.Add(MakeOption(OptClientID, clientID))
|
||||
for _, association := range(associations) {
|
||||
for _, association := range associations {
|
||||
retOptions.Add(MakeIaNaOption(association.InterfaceID, b.calculateT1(), b.calculateT2(),
|
||||
MakeIaAddrOption(association.IPAddress, b.PreferredLifetime, b.ValidLifetime)))
|
||||
}
|
||||
@ -69,8 +69,11 @@ func (b *PacketBuilder) makeMsgAdvertise(transactionID [3]byte, serverDUID, clie
|
||||
}
|
||||
retOptions.Add(MakeOption(OptBootfileURL, bootFileURL))
|
||||
if preference != nil {
|
||||
retOptions.Add(MakeOption(OptPreference, preference))}
|
||||
if len(dnsServers) > 0 { retOptions.Add(MakeDNSServersOption(dnsServers)) }
|
||||
retOptions.Add(MakeOption(OptPreference, preference))
|
||||
}
|
||||
if len(dnsServers) > 0 {
|
||||
retOptions.Add(MakeDNSServersOption(dnsServers))
|
||||
}
|
||||
|
||||
return &Packet{Type: MsgAdvertise, TransactionID: transactionID, Options: retOptions}
|
||||
}
|
||||
@ -79,11 +82,11 @@ func (b *PacketBuilder) makeMsgReply(transactionID [3]byte, serverDUID, clientID
|
||||
associations []*IdentityAssociation, iasWithoutAddresses [][]byte, bootFileURL []byte, dnsServers []net.IP, err error) *Packet {
|
||||
retOptions := make(Options)
|
||||
retOptions.Add(MakeOption(OptClientID, clientID))
|
||||
for _, association := range(associations) {
|
||||
for _, association := range associations {
|
||||
retOptions.Add(MakeIaNaOption(association.InterfaceID, b.calculateT1(), b.calculateT2(),
|
||||
MakeIaAddrOption(association.IPAddress, b.PreferredLifetime, b.ValidLifetime)))
|
||||
}
|
||||
for _, ia := range(iasWithoutAddresses) {
|
||||
for _, ia := range iasWithoutAddresses {
|
||||
retOptions.Add(MakeIaNaOption(ia, b.calculateT1(), b.calculateT2(),
|
||||
MakeStatusOption(2, err.Error())))
|
||||
}
|
||||
@ -92,7 +95,9 @@ func (b *PacketBuilder) makeMsgReply(transactionID [3]byte, serverDUID, clientID
|
||||
retOptions.Add(MakeOption(OptVendorClass, []byte{0, 0, 0, 0, 0, 10, 72, 84, 84, 80, 67, 108, 105, 101, 110, 116})) // HTTPClient
|
||||
}
|
||||
retOptions.Add(MakeOption(OptBootfileURL, bootFileURL))
|
||||
if len(dnsServers) > 0 { retOptions.Add(MakeDNSServersOption(dnsServers)) }
|
||||
if len(dnsServers) > 0 {
|
||||
retOptions.Add(MakeDNSServersOption(dnsServers))
|
||||
}
|
||||
|
||||
return &Packet{Type: MsgReply, TransactionID: transactionID, Options: retOptions}
|
||||
}
|
||||
@ -106,7 +111,9 @@ func (b *PacketBuilder) makeMsgInformationRequestReply(transactionID [3]byte, se
|
||||
retOptions.Add(MakeOption(OptVendorClass, []byte{0, 0, 0, 0, 0, 10, 72, 84, 84, 80, 67, 108, 105, 101, 110, 116})) // HTTPClient
|
||||
}
|
||||
retOptions.Add(MakeOption(OptBootfileURL, bootFileURL))
|
||||
if len(dnsServers) > 0 { retOptions.Add(MakeDNSServersOption(dnsServers)) }
|
||||
if len(dnsServers) > 0 {
|
||||
retOptions.Add(MakeDNSServersOption(dnsServers))
|
||||
}
|
||||
|
||||
return &Packet{Type: MsgReply, TransactionID: transactionID, Options: retOptions}
|
||||
}
|
||||
@ -155,12 +162,13 @@ func iasWithoutAddesses(availableAssociations []*IdentityAssociation, allIAs [][
|
||||
ret := make([][]byte, 0)
|
||||
iasWithAddresses := make(map[uint64]bool)
|
||||
|
||||
for _, association := range(availableAssociations) {
|
||||
for _, association := range availableAssociations {
|
||||
iasWithAddresses[calculateIAIDHash(association.InterfaceID)] = true
|
||||
}
|
||||
|
||||
for _, ia := range(allIAs) {
|
||||
_, exists := iasWithAddresses[calculateIAIDHash(ia)]; if !exists {
|
||||
for _, ia := range allIAs {
|
||||
_, exists := iasWithAddresses[calculateIAIDHash(ia)]
|
||||
if !exists {
|
||||
ret = append(ret, ia)
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMakeMsgAdvertise(t *testing.T) {
|
||||
@ -86,7 +86,8 @@ func TestMakeMsgAdvertiseShouldSkipDnsServersIfNoneConfigured(t *testing.T) {
|
||||
msg := builder.makeMsgAdvertise(transactionID, expectedServerID, expectedClientID, 0x11,
|
||||
[]*IdentityAssociation{identityAssociation}, expectedBootFileURL, nil, []net.IP{})
|
||||
|
||||
_, exists := msg.Options[OptRecursiveDNS]; if exists {
|
||||
_, exists := msg.Options[OptRecursiveDNS]
|
||||
if exists {
|
||||
t.Fatalf("DNS servers option should not be set")
|
||||
}
|
||||
}
|
||||
@ -168,7 +169,8 @@ func TestMakeNoAddrsAvailable(t *testing.T) {
|
||||
t.Fatalf("Expected server id %v, got %v", expectedClientID, serverIDOption)
|
||||
}
|
||||
|
||||
_, exists := msg.Options[OptStatusCode]; if !exists {
|
||||
_, exists := msg.Options[OptStatusCode]
|
||||
if !exists {
|
||||
t.Fatalf("Expected status code option to be present")
|
||||
}
|
||||
statusCodeOption := msg.Options[OptStatusCode][0].Value
|
||||
@ -258,7 +260,8 @@ func TestMakeMsgReplyShouldSkipDnsServersIfNoneWereConfigured(t *testing.T) {
|
||||
msg := builder.makeMsgReply(transactionID, expectedServerID, expectedClientID, 0x11,
|
||||
[]*IdentityAssociation{identityAssociation}, make([][]byte, 0), expectedBootFileURL, []net.IP{}, nil)
|
||||
|
||||
_, exists := msg.Options[OptRecursiveDNS]; if exists {
|
||||
_, exists := msg.Options[OptRecursiveDNS]
|
||||
if exists {
|
||||
t.Fatalf("Dns servers option shouldn't be present")
|
||||
}
|
||||
}
|
||||
@ -413,7 +416,8 @@ func TestMakeMsgInformationRequestReplyShouldSkipDnsServersIfNoneWereConfigured(
|
||||
msg := builder.makeMsgInformationRequestReply(transactionID, expectedServerID, expectedClientID, 0x11,
|
||||
expectedBootFileURL, []net.IP{})
|
||||
|
||||
_, exists := msg.Options[OptRecursiveDNS]; if exists {
|
||||
_, exists := msg.Options[OptRecursiveDNS]
|
||||
if exists {
|
||||
t.Fatalf("Dns servers option shouldn't be present")
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package dhcp6
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShouldDiscardSolicitWithoutBootfileUrlOption(t *testing.T) {
|
||||
@ -93,10 +93,9 @@ func TestShouldDiscardRequestWithWrongServerId(t *testing.T) {
|
||||
|
||||
func MakeOptionRequestOptions(options []uint16) *Option {
|
||||
value := make([]byte, len(options)*2)
|
||||
for i, option := range(options) {
|
||||
for i, option := range options {
|
||||
binary.BigEndian.PutUint16(value[i*2:], option)
|
||||
}
|
||||
|
||||
return &Option{ID: OptOro, Length: uint16(len(options) * 2), Value: value}
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"net"
|
||||
"math/rand"
|
||||
"time"
|
||||
"math/big"
|
||||
"hash/fnv"
|
||||
"sync"
|
||||
"fmt"
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"hash/fnv"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type associationExpiration struct {
|
||||
@ -80,9 +80,9 @@ func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]b
|
||||
ret := make([]*dhcp6.IdentityAssociation, 0, len(interfaceIDs))
|
||||
rng := rand.New(rand.NewSource(p.timeNow().UnixNano()))
|
||||
|
||||
for _, interfaceID := range (interfaceIDs) {
|
||||
for _, interfaceID := range interfaceIDs {
|
||||
clientIDHash := p.calculateIAIDHash(clientID, interfaceID)
|
||||
association, exists := p.identityAssociations[clientIDHash];
|
||||
association, exists := p.identityAssociations[clientIDHash]
|
||||
|
||||
if exists {
|
||||
ret = append(ret, association)
|
||||
@ -96,7 +96,7 @@ func (p *RandomAddressPool) ReserveAddresses(clientID []byte, interfaceIDs [][]b
|
||||
// we assume that ip addresses adhere to high 64 bits for net and subnet ids, low 64 bits are for host id rule
|
||||
hostOffset := rng.Uint64() % p.poolSize
|
||||
newIP := big.NewInt(0).Add(p.poolStartAddress, big.NewInt(0).SetUint64(hostOffset))
|
||||
_, exists := p.usedIps[newIP.Uint64()];
|
||||
_, exists := p.usedIps[newIP.Uint64()]
|
||||
if !exists {
|
||||
timeNow := p.timeNow()
|
||||
association := &dhcp6.IdentityAssociation{ClientID: clientID,
|
||||
@ -120,7 +120,7 @@ func (p *RandomAddressPool) ReleaseAddresses(clientID []byte, interfaceIDs [][]
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
for _, interfaceID := range(interfaceIDs) {
|
||||
for _, interfaceID := range interfaceIDs {
|
||||
association, exists := p.identityAssociations[p.calculateIAIDHash(clientID, interfaceID)]
|
||||
if !exists {
|
||||
continue
|
||||
@ -134,9 +134,13 @@ func (p *RandomAddressPool) ReleaseAddresses(clientID []byte, interfaceIDs [][]
|
||||
// back into the address pool. Note it should be called from under the RandomAddressPool.lock.
|
||||
func (p *RandomAddressPool) expireIdentityAssociations() {
|
||||
for {
|
||||
if p.identityAssociationExpirations.Size() < 1 { break }
|
||||
if p.identityAssociationExpirations.Size() < 1 {
|
||||
break
|
||||
}
|
||||
expiration := p.identityAssociationExpirations.Peek().(*associationExpiration)
|
||||
if p.timeNow().Before(expiration.expiresAt) { break }
|
||||
if p.timeNow().Before(expiration.expiresAt) {
|
||||
break
|
||||
}
|
||||
p.identityAssociationExpirations.Shift()
|
||||
delete(p.identityAssociations, p.calculateIAIDHash(expiration.ia.ClientID, expiration.ia.InterfaceID))
|
||||
delete(p.usedIps, big.NewInt(0).SetBytes(expiration.ia.IPAddress).Uint64())
|
||||
|
@ -1,8 +1,8 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -80,7 +80,8 @@ func TestReserveAddressKeepsTrackOfUsedAddresses(t *testing.T) {
|
||||
pool.timeNow = func() time.Time { return expectedTime }
|
||||
pool.ReserveAddresses(expectedClientID, [][]byte{expectedIAID})
|
||||
|
||||
_, exists := pool.usedIps[0x01]; if !exists {
|
||||
_, exists := pool.usedIps[0x01]
|
||||
if !exists {
|
||||
t.Fatal("'2001:db8:f00f:cafe::1' should be marked as in use")
|
||||
}
|
||||
}
|
||||
@ -139,7 +140,8 @@ func TestReleaseAddress(t *testing.T) {
|
||||
|
||||
pool.ReleaseAddresses(expectedClientID, [][]byte{expectedIAID})
|
||||
|
||||
_, exists := pool.identityAssociations[pool.calculateIAIDHash(expectedClientID, expectedIAID)]; if exists {
|
||||
_, exists := pool.identityAssociations[pool.calculateIAIDHash(expectedClientID, expectedIAID)]
|
||||
if exists {
|
||||
t.Fatalf("identity association for %v should've been removed, but is still available", a[0].IPAddress)
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package pixiecore
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
"strings"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const X86_HTTP_CLIENT = 0x10
|
||||
|
@ -1,13 +1,13 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"go.universe.tf/netboot/dhcp6/pool"
|
||||
"go.universe.tf/netboot/pixiecore"
|
||||
"net"
|
||||
"strings"
|
||||
"go.universe.tf/netboot/pixiecore"
|
||||
)
|
||||
|
||||
// pixiecore bootipv6 --listen-addr=2001:db8:f00f:cafe::4/64 --httpboot-url=http://[2001:db8:f00f:cafe::4]/bootx64.efi --ipxe-url=http://[2001:db8:f00f:cafe::4]/script.ipxe
|
||||
@ -35,7 +35,9 @@ var bootIPv6Cmd = &cobra.Command{
|
||||
if err != nil {
|
||||
s.Debug = logWithStdFmt
|
||||
}
|
||||
if debug { s.Debug = logWithStdFmt }
|
||||
if debug {
|
||||
s.Debug = logWithStdFmt
|
||||
}
|
||||
|
||||
if addr == "" {
|
||||
fatalf("Please specify address to bind to")
|
||||
|
@ -1,14 +1,14 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"go.universe.tf/netboot/dhcp6/pool"
|
||||
"time"
|
||||
"go.universe.tf/netboot/pixiecore"
|
||||
"net"
|
||||
"strings"
|
||||
"go.universe.tf/netboot/pixiecore"
|
||||
"time"
|
||||
)
|
||||
|
||||
// pixiecore ipv6api --listen-addr=2001:db8:f00f:cafe::4 --api-request-url=http://[2001:db8:f00f:cafe::4]:8888
|
||||
@ -36,7 +36,9 @@ var ipv6ApiCmd = &cobra.Command{
|
||||
if err != nil {
|
||||
s.Debug = logWithStdFmt
|
||||
}
|
||||
if debug { s.Debug = logWithStdFmt }
|
||||
if debug {
|
||||
s.Debug = logWithStdFmt
|
||||
}
|
||||
|
||||
if addr == "" {
|
||||
fatalf("Please specify address to bind to")
|
||||
@ -98,4 +100,3 @@ func init() {
|
||||
rootCmd.AddCommand(ipv6ApiCmd)
|
||||
serverv6APIConfigFlags(ipv6ApiCmd)
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package pixiecore
|
||||
|
||||
import (
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"fmt"
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
)
|
||||
|
||||
func (s *ServerV6) serveDHCP(conn *dhcp6.Conn) error {
|
||||
|
@ -1,11 +1,11 @@
|
||||
package pixiecore
|
||||
|
||||
import (
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"fmt"
|
||||
"time"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"go.universe.tf/netboot/dhcp6"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServerV6 struct {
|
||||
|
Loading…
Reference in New Issue
Block a user