summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/async/client.go
blob: 8a7fbff6b1b0e9274e97b92562c7ed45e775468c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package async

import (
	"context"
	"fmt"
	"log"
	"net"
	"sync"
	"time"

	promise "github.com/fanliao/go-promise"
	"github.com/insomniacslk/dhcp/dhcpv6"
	"github.com/insomniacslk/dhcp/dhcpv6/client6"
)

// Client implements an asynchronous DHCPv6 client
type Client struct {
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	LocalAddr    net.Addr
	RemoteAddr   net.Addr
	IgnoreErrors bool

	connection   *net.UDPConn
	cancel       context.CancelFunc
	stopping     *sync.WaitGroup
	receiveQueue chan dhcpv6.DHCPv6
	sendQueue    chan dhcpv6.DHCPv6
	packetsLock  sync.Mutex
	packets      map[dhcpv6.TransactionID]*promise.Promise
	errors       chan error
}

// NewClient creates an asynchronous client
func NewClient() *Client {
	return &Client{
		ReadTimeout:  client6.DefaultReadTimeout,
		WriteTimeout: client6.DefaultWriteTimeout,
	}
}

// OpenForInterface starts the client on the specified interface, replacing
// client LocalAddr with a link-local address of the given interface and
// standard DHCP port (546).
func (c *Client) OpenForInterface(ifname string, bufferSize int) error {
	addr, err := dhcpv6.GetLinkLocalAddr(ifname)
	if err != nil {
		return err
	}
	c.LocalAddr = &net.UDPAddr{IP: addr, Port: dhcpv6.DefaultClientPort, Zone: ifname}
	return c.Open(bufferSize)
}

// Open starts the client
func (c *Client) Open(bufferSize int) error {
	var (
		addr *net.UDPAddr
		ok   bool
		err  error
	)

	if addr, ok = c.LocalAddr.(*net.UDPAddr); !ok {
		return fmt.Errorf("Invalid local address: %v not a net.UDPAddr", c.LocalAddr)
	}

	// prepare the socket to listen on for replies
	c.connection, err = net.ListenUDP("udp6", addr)
	if err != nil {
		return err
	}
	c.stopping = new(sync.WaitGroup)
	c.sendQueue = make(chan dhcpv6.DHCPv6, bufferSize)
	c.receiveQueue = make(chan dhcpv6.DHCPv6, bufferSize)
	c.packets = make(map[dhcpv6.TransactionID]*promise.Promise)
	c.packetsLock = sync.Mutex{}
	c.errors = make(chan error)

	var ctx context.Context
	ctx, c.cancel = context.WithCancel(context.Background())
	go c.receiverLoop(ctx)
	go c.senderLoop(ctx)

	return nil
}

// Close stops the client
func (c *Client) Close() {
	// Wait for sender and receiver loops
	c.stopping.Add(2)
	c.cancel()
	c.stopping.Wait()

	close(c.sendQueue)
	close(c.receiveQueue)
	close(c.errors)

	c.connection.Close()
}

// Errors returns a channel where runtime errors are posted
func (c *Client) Errors() <-chan error {
	return c.errors
}

func (c *Client) addError(err error) {
	if !c.IgnoreErrors {
		c.errors <- err
	}
}

func (c *Client) receiverLoop(ctx context.Context) {
	defer func() { c.stopping.Done() }()
	for {
		select {
		case <-ctx.Done():
			return
		case packet := <-c.receiveQueue:
			c.receive(packet)
		}
	}
}

func (c *Client) senderLoop(ctx context.Context) {
	defer func() { c.stopping.Done() }()
	for {
		select {
		case <-ctx.Done():
			return
		case packet := <-c.sendQueue:
			c.send(packet)
		}
	}
}

func (c *Client) send(packet dhcpv6.DHCPv6) {
	transactionID, err := dhcpv6.GetTransactionID(packet)
	if err != nil {
		c.addError(fmt.Errorf("Warning: This should never happen, there is no transaction ID on %s", packet))
		return
	}
	c.packetsLock.Lock()
	p := c.packets[transactionID]
	c.packetsLock.Unlock()

	raddr, err := c.remoteAddr()
	if err != nil {
		_ = p.Reject(err)
		log.Printf("Warning: cannot get remote address :%v", err)
		return
	}

	if err := c.connection.SetWriteDeadline(time.Now().Add(c.WriteTimeout)); err != nil {
		_ = p.Reject(err)
		log.Printf("Warning: cannot set write deadline :%v", err)
		return
	}
	_, err = c.connection.WriteTo(packet.ToBytes(), raddr)
	if err != nil {
		_ = p.Reject(err)
		log.Printf("Warning: cannot write to %s :%v", raddr, err)
		return
	}

	c.receiveQueue <- packet
}

func (c *Client) receive(_ dhcpv6.DHCPv6) {
	var (
		oobdata  = []byte{}
		received dhcpv6.DHCPv6
	)

	if err := c.connection.SetReadDeadline(time.Now().Add(c.ReadTimeout)); err != nil {
		log.Printf("Warning: cannot set read deadline :%v", err)
	}
	for {
		buffer := make([]byte, client6.MaxUDPReceivedPacketSize)
		n, _, _, _, err := c.connection.ReadMsgUDP(buffer, oobdata)
		if err != nil {
			if err, ok := err.(net.Error); !ok || !err.Timeout() {
				c.addError(fmt.Errorf("Error receiving the message: %s", err))
			}
			return
		}
		received, err = dhcpv6.FromBytes(buffer[:n])
		if err != nil {
			// skip non-DHCP packets
			continue
		}
		break
	}

	transactionID, err := dhcpv6.GetTransactionID(received)
	if err != nil {
		c.addError(fmt.Errorf("Unable to get a transactionID for %s: %s", received, err))
		return
	}

	c.packetsLock.Lock()
	if p, ok := c.packets[transactionID]; ok {
		delete(c.packets, transactionID)
		_ = p.Resolve(received)
	}
	c.packetsLock.Unlock()
}

func (c *Client) remoteAddr() (*net.UDPAddr, error) {
	if c.RemoteAddr == nil {
		return &net.UDPAddr{IP: dhcpv6.AllDHCPRelayAgentsAndServers, Port: dhcpv6.DefaultServerPort}, nil
	}

	if addr, ok := c.RemoteAddr.(*net.UDPAddr); ok {
		return addr, nil
	}
	return nil, fmt.Errorf("Invalid remote address: %v not a net.UDPAddr", c.RemoteAddr)
}

// Send inserts a message to the queue to be sent asynchronously.
// Returns a future which resolves to response and error.
func (c *Client) Send(message dhcpv6.DHCPv6, modifiers ...dhcpv6.Modifier) *promise.Future {
	for _, mod := range modifiers {
		mod(message)
	}

	transactionID, err := dhcpv6.GetTransactionID(message)
	if err != nil {
		return promise.Wrap(err)
	}

	p := promise.NewPromise()
	c.packetsLock.Lock()
	c.packets[transactionID] = p
	c.packetsLock.Unlock()
	c.sendQueue <- message
	return p.Future
}