summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp/client.go
blob: 38de094e3b1418c198caf7632233b586dbf7381a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bsdp

import (
	"errors"
	"fmt"
	"net"

	"github.com/insomniacslk/dhcp/dhcpv4"
)

// Client represents a BSDP client that can perform BSDP exchanges via the
// broadcast address.
type Client dhcpv4.Client

// NewClient constructs a new client with default read and write timeouts from
// dhcpv4.Client.
func NewClient() *Client {
	c := dhcpv4.NewClient()
	return &Client{
		ReadTimeout:  c.ReadTimeout,
		WriteTimeout: c.WriteTimeout,
	}
}

func castVendorOpt(ack *dhcpv4.DHCPv4) {
	opts := ack.Options()
	for i := 0; i < len(opts); i++ {
		if opts[i].Code() == dhcpv4.OptionVendorSpecificInformation {
			vendorOpt, err := ParseOptVendorSpecificInformation(opts[i].ToBytes())
			// Oh well, we tried
			if err != nil {
				return
			}
			opts[i] = vendorOpt
		}
	}
}

// Exchange runs a full BSDP exchange (Inform[list], Ack, Inform[select],
// Ack). Returns a list of DHCPv4 structures representing the exchange.
func (c *Client) Exchange(ifname string, informList *dhcpv4.DHCPv4) ([]*dhcpv4.DHCPv4, error) {
	conversation := make([]*dhcpv4.DHCPv4, 1)
	var err error

	// Get our file descriptor for the broadcast socket.
	sendFd, err := dhcpv4.MakeBroadcastSocket(ifname)
	if err != nil {
		return conversation, err
	}
	recvFd, err := dhcpv4.MakeListeningSocket(ifname)
	if err != nil {
		return conversation, err
	}
	iface, err := net.InterfaceByName(ifname)
	if err != nil {
		return conversation, err
	}

	// Get currently configured IP.
	addrs, err := iface.Addrs()
	if err != nil {
		return conversation, err
	}
	localIPs, err := dhcpv4.GetExternalIPv4Addrs(addrs)
	if err != nil {
		return conversation, fmt.Errorf("could not get local IPv4 addr for %s: %v", iface.Name, err)
	}

	// INFORM[LIST]
	if informList == nil {
		informList, err = NewInformList(iface.HardwareAddr, localIPs[0], dhcpv4.ClientPort)
		if err != nil {
			return conversation, err
		}
	}
	conversation[0] = informList

	// ACK[LIST]
	ackForList, err := dhcpv4.BroadcastSendReceive(sendFd, recvFd, informList, c.ReadTimeout, c.WriteTimeout, dhcpv4.MessageTypeAck)
	if err != nil {
		return conversation, err
	}

	// Rewrite vendor-specific option for pretty printing.
	castVendorOpt(ackForList)
	conversation = append(conversation, ackForList)

	// Parse boot images sent back by server
	bootImages, err := ParseBootImageListFromAck(*ackForList)
	if err != nil {
		return conversation, err
	}
	if len(bootImages) == 0 {
		return conversation, errors.New("got no BootImages from server")
	}

	// INFORM[SELECT]
	informSelect, err := InformSelectForAck(*ackForList, dhcpv4.ClientPort, bootImages[0])
	if err != nil {
		return conversation, err
	}
	conversation = append(conversation, informSelect)

	// ACK[SELECT]
	ackForSelect, err := dhcpv4.BroadcastSendReceive(sendFd, recvFd, informSelect, c.ReadTimeout, c.WriteTimeout, dhcpv4.MessageTypeAck)
	castVendorOpt(ackForSelect)
	if err != nil {
		return conversation, err
	}
	return append(conversation, ackForSelect), nil
}