-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
205 lines (185 loc) · 4.09 KB
/
conn.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
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
package tomp2p
import (
"errors"
"fmt"
"log"
"net"
"sync"
"time"
)
type peer struct {
port uint16
listening map[string]PeerConn
inbound chan Packet
outboundDefault chan Packet
open bool
mu *sync.Mutex
done chan bool
}
type PeerConn struct {
Address *net.Addr
Conn *net.UDPConn
outbound chan Packet
}
type Packet struct {
addr *net.UDPAddr
data []byte
outbound chan Packet
}
const (
maxUPDPacketSize = 1500
inboundChannelSize = 1000
outboundChannelSize = 1000
)
func (p *peer) Listen() {
p.ListenOnce()
go func() {
ticker := time.NewTicker(time.Second * 30)
defer ticker.Stop()
for {
select {
case <-ticker.C:
p.ListenOnce()
case <-p.done:
fmt.Println("done")
return
}
}
}()
}
func (p *peer) ListenOnce() error {
p.mu.Lock()
defer p.mu.Unlock()
if !p.open {
return errors.New("cannot listen on a closed peer connection")
}
ifaces, err := net.Interfaces()
if err != nil {
return err
}
if p.inbound == nil {
p.inbound = make(chan Packet, inboundChannelSize)
}
if p.listening == nil {
p.listening = make(map[string]PeerConn)
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
log.Printf("error in address %v, ignoring", addrs)
continue
}
for _, addr := range addrs {
if p.listening[addr.String()] == (PeerConn{}) {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
continue
}
inet := &net.UDPAddr{ip, int(p.port), ""}
udpConn, err := net.ListenUDP("udp", inet)
if err != nil {
log.Printf("error in listening /%v/ /%v/, ignoring", addr, err)
continue
}
outbound := make(chan Packet, outboundChannelSize)
if p.outboundDefault == nil {
p.outboundDefault = outbound
}
p.listening[addr.String()] = PeerConn{&addr, udpConn, outbound}
log.Printf("Debug: listening on: %v", addr)
p.setupOutbound(outbound, udpConn)
p.setupInbound(outbound, udpConn)
} else {
//find new default outbound channel
if p.outboundDefault == nil {
p.outboundDefault = p.listening[addr.String()].outbound
}
}
}
}
log.Printf("we are listening on %d addresses.", len(p.listening))
return nil
}
func (p *peer) setupOutbound(outbound chan Packet, udpConn *net.UDPConn) {
go func() {
for packet := range outbound {
_, err := udpConn.WriteToUDP(packet.data, packet.addr)
if err != nil {
log.Printf("Error out: UDP write error: %v", err)
break
}
}
p.mu.Lock()
defer p.mu.Unlock()
if p.open {
//either outbound channel is closed or connection error, mark default outbound as not usable
if p.outboundDefault == outbound {
p.outboundDefault = nil
}
p.Listen()
}
}()
}
func (p *peer) setupInbound(outbound chan Packet, udpConn *net.UDPConn) {
go func() {
for {
b := make([]byte, maxUPDPacketSize)
n, addr, err := udpConn.ReadFromUDP(b)
if err != nil {
p.mu.Lock()
defer p.mu.Unlock()
if p.open {
log.Printf("Error in: UDP read error: %v", err)
}
delete(p.listening, addr.String())
return
}
//idea: set the default outbound to the one that just received a packet
p.inbound <- Packet{addr, b[:n], outbound}
}
}()
}
func (p *peer) Close() {
p.mu.Lock()
defer p.mu.Unlock()
p.open = false
for _, v := range p.listening {
err := v.Conn.Close()
if err != nil {
log.Printf("closing connection failed %v, ignoring", v.Address)
continue
}
close(v.outbound)
}
close(p.inbound)
close(p.done)
log.Printf("closed peers")
}
func New() peer {
peer := peer{}
peer.port = 1135
peer.open = true
peer.mu = &sync.Mutex{}
peer.done = make(chan bool, 1)
return peer
}
func NewPort(port uint16) peer {
peer := peer{}
peer.port = port
peer.open = true
peer.mu = &sync.Mutex{}
peer.done = make(chan bool, 1)
return peer
}