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
112
113
114
115
116
117
|
package bsdp
import (
"fmt"
"net"
"syscall"
"github.com/insomniacslk/dhcp/dhcpv4"
)
// Client is a BSDP-specific client suitable for performing BSDP exchanges.
type Client dhcpv4.Client
// 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
// INFORM[LIST]
if informList == nil {
informList, err = NewInformListForInterface(ifname, dhcpv4.ClientPort)
if err != nil {
return conversation, err
}
}
conversation[0] = *informList
// TODO: deduplicate with code in dhcpv4/client.go
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
if err != nil {
return conversation, err
}
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if err != nil {
return conversation, err
}
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
if err != nil {
return conversation, err
}
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
if err != nil {
return conversation, err
}
err = dhcpv4.BindToInterface(fd, ifname)
if err != nil {
return conversation, err
}
bcast := [4]byte{}
copy(bcast[:], net.IPv4bcast)
daddr := syscall.SockaddrInet4{Port: dhcpv4.ClientPort, Addr: bcast}
packet, err := dhcpv4.MakeRawBroadcastPacket(informList.ToBytes())
if err != nil {
return conversation, err
}
err = syscall.Sendto(fd, packet, 0, &daddr)
if err != nil {
return conversation, err
}
// ACK 1
conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: dhcpv4.ClientPort})
if err != nil {
return conversation, err
}
defer conn.Close()
buf := make([]byte, dhcpv4.MaxUDPReceivedPacketSize)
oobdata := []byte{} // ignoring oob data
n, _, _, _, err := conn.ReadMsgUDP(buf, oobdata)
ack1, err := dhcpv4.FromBytes(buf[:n])
if err != nil {
return conversation, err
}
// TODO match the packet content
// TODO check that the peer address matches the declared server IP and port
conversation = append(conversation, *ack1)
// Parse boot images sent back by server
bootImages, err := ParseBootImageListFromAck(*ack1)
if err != nil {
return conversation, err
}
if len(bootImages) == 0 {
return conversation, fmt.Errorf("Got no BootImages from server")
}
// INFORM[SELECT]
informSelect, err := InformSelectForAck(*ack1, dhcpv4.ClientPort, bootImages[0])
if err != nil {
return conversation, err
}
conversation = append(conversation, *informSelect)
packet, err = dhcpv4.MakeRawBroadcastPacket(informSelect.ToBytes())
if err != nil {
return conversation, err
}
err = syscall.Sendto(fd, packet, 0, &daddr)
if err != nil {
return conversation, err
}
// ACK 2
buf = make([]byte, dhcpv4.MaxUDPReceivedPacketSize)
n, _, _, _, err = conn.ReadMsgUDP(buf, oobdata)
ack2, err := dhcpv4.FromBytes(buf[:n])
if err != nil {
return conversation, err
}
// TODO match the packet content
// TODO check that the peer address matches the declared server IP and port
conversation = append(conversation, *ack2)
return conversation, nil
}
|