forked from vmware-archive/gotftp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
199 lines (163 loc) · 4.02 KB
/
server.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
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tftp
import (
"bytes"
"net"
"sync"
"time"
"golang.org/x/net/ipv4"
)
type controlMessage struct {
*ipv4.ControlMessage
}
func (c controlMessage) LocalAddr() net.Addr {
return &net.IPAddr{IP: c.ControlMessage.Dst}
}
func (c controlMessage) RemoteAddr() net.Addr {
return &net.IPAddr{IP: c.ControlMessage.Src}
}
type zeroConn struct{}
func (c zeroConn) LocalAddr() net.Addr {
return &net.IPAddr{IP: net.IPv4zero}
}
func (c zeroConn) RemoteAddr() net.Addr {
return &net.IPAddr{IP: net.IPv4zero}
}
func newZeroConn() Conn {
return zeroConn{}
}
// ZeroConn can be used as a placeholder if otherwise not known.
var ZeroConn = newZeroConn()
type packetReaderImpl struct {
ch <-chan []byte
}
func (p *packetReaderImpl) read(timeout time.Duration) (packet, error) {
if timeout == 0 {
select {
case buf := <-p.ch:
return packetFromWire(bytes.NewBuffer(buf))
default:
return nil, ErrTimeout
}
}
select {
case buf := <-p.ch:
return packetFromWire(bytes.NewBuffer(buf))
case <-time.After(timeout):
return nil, ErrTimeout
}
}
type packetWriterImpl struct {
net.PacketConn
addr net.Addr
b bytes.Buffer
}
func (p *packetWriterImpl) write(x packet) error {
p.b.Reset()
err := packetToWire(x, &p.b)
if err != nil {
return err
}
_, err = p.PacketConn.WriteTo(p.b.Bytes(), p.addr)
return err
}
type syncPacketConn struct {
net.PacketConn
sync.Mutex
}
func (s *syncPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
s.Lock()
n, err := s.PacketConn.WriteTo(b, addr)
s.Unlock()
return n, err
}
func Serve(l net.PacketConn, h Handler) error {
ipv4pc := ipv4.NewPacketConn(l)
flags := ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
if err := ipv4pc.SetControlMessage(flags, true); err != nil {
return err
}
var (
mu sync.Mutex
table = make(map[string]chan []byte)
buf = make([]byte, 65536)
)
for {
n, cm, addr, err := ipv4pc.ReadFrom(buf)
if err != nil {
return err
}
// Ignore packet without control message.
if cm == nil {
continue
}
// Ownership of this buffer is transferred to the goroutine for the peer
// address, so we need to make a copy before handing it off.
b := make([]byte, n)
copy(b, buf[:n])
mu.Lock()
ch, ok := table[addr.String()]
if !ok {
ch = make(chan []byte, 10)
ch <- b
table[addr.String()] = ch
// Packet reader for client
r := &packetReaderImpl{
ch: ch,
}
// Packet writer for client
w := &packetWriterImpl{
PacketConn: &syncPacketConn{
PacketConn: l,
},
addr: addr,
}
// Kick off a serve loop for this peer address.
go func() {
// A client MAY reuse its socket for more than one request.
// Therefore, continue running the serve loop until there are no more
// inbound packets on the channel for this peer address.
for stop := false; !stop; {
serve(controlMessage{cm}, r, w, h)
mu.Lock()
if len(ch) == 0 {
delete(table, addr.String())
stop = true
}
mu.Unlock()
}
}()
} else {
select {
case ch <- b:
default:
// Drop packet on the floor if we can't handle it
}
}
// Unlock after sending buffer so that other routines can reliably check
// and use the length of a channel while holding the lock.
mu.Unlock()
}
}
func ListenAndServe(addr string, h Handler) error {
if addr == "" {
addr = ":69"
}
l, err := net.ListenPacket("udp4", addr)
if err != nil {
return err
}
defer l.Close() // Should I not do this?
return Serve(l, h)
}