-
Notifications
You must be signed in to change notification settings - Fork 8
/
packetconn.go
69 lines (60 loc) · 1.77 KB
/
packetconn.go
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
package arp
import (
"fmt"
"net"
"sync"
"time"
marp "github.com/mdlayher/arp"
)
// bufferReadFromPacketConn is a net.PacketConn which copies bytes from its
// embedded buffer into b when when its ReadFrom method is called.
type bufferedPacketConn struct {
channel chan []byte
closed bool
}
// NewTestHandler allow you to pass a PacketConn. Useful for testing
// if p is nil, auto create a bufferedPacketConn
func NewTestHandler(config Config, p net.PacketConn) (c *Handler, conn *marp.Client, err error) {
c = newHandler(config)
c.table = newARPTable() // we want an empty table for testing
ifi, err := net.InterfaceByName(config.NIC)
if err != nil {
return nil, nil, fmt.Errorf("InterfaceByName error: %w", err)
}
if p == nil {
p = &bufferedPacketConn{channel: make(chan []byte, 32)}
}
if c.client, err = marp.New(ifi, p); err != nil {
return nil, nil, err
}
return c, c.client, nil
}
var channelMutex sync.Mutex // avoid race in Close()
func (p *bufferedPacketConn) Close() error {
channelMutex.Lock()
defer channelMutex.Unlock()
if !p.closed {
close(p.channel)
p.closed = true
}
return nil
}
func (p *bufferedPacketConn) LocalAddr() net.Addr { return nil }
func (p *bufferedPacketConn) SetDeadline(t time.Time) error { return nil }
func (p *bufferedPacketConn) SetReadDeadline(t time.Time) error { return nil }
func (p *bufferedPacketConn) SetWriteDeadline(t time.Time) error { return nil }
func (p *bufferedPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
if p.closed {
return 0, nil, fmt.Errorf("closed")
}
data := <-p.channel
n := copy(b, data)
return n, nil, nil
}
func (p *bufferedPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
if p.closed {
return 0, fmt.Errorf("closed")
}
p.channel <- b
return len(b), nil
}