This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tftp_server.go
191 lines (172 loc) · 4.81 KB
/
tftp_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
package main
import (
"bytes"
"encoding/binary"
"net"
"os"
"strconv"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/robwc/tftp"
)
const (
//ServerNet default network to listen on
ServerNet = "udp4"
//DefaultPort default port to listen on
DefaultPort = 69
//DefaultIP default IP to listen on
DefaultIP = "0.0.0.0"
//DefaultStats enable stats collection by default
DefaultStats = true
)
//TFTPServer A server to listen for UDP messages
type TFTPServer struct {
ctrlChan chan int //ctrlChan control channel for managing the server
listenAddr *net.UDPAddr //listenAddr the address to listen on
sock *net.UDPConn //sock UDP connection socket
incomingDir string //incomingDir incoming directory for files
outgoingDir string //outgoingDir outgoingDir for files
protocol string //protocol protocol to listen on: udp, udp4, or udp6
wg sync.WaitGroup //wg wait group for syncing data
Connections map[string]*TFTPConn //Connections active TFTP connection
clientwg sync.WaitGroup //clientwg wait group to manage client connection
Debug bool //Debug enable debuging
StatsMgr *StatsMgr //StatsMgr stats collection manager
Config *Config //Config complete config passed to server
}
//LoadConfig load a config from disk
func (s *TFTPServer) LoadConfig(c *Config) error {
var err error
if c.IncomingDir == "" && c.OutgoingDir == "" {
//load default
c = NewConfig()
} else {
s.Config = c
s.incomingDir = c.IncomingDir
s.outgoingDir = c.OutgoingDir
}
//check inc dir exists and can be written to
iStat, err := os.Stat(c.IncomingDir)
if err != nil {
//Directory does not exist, create dir
err := os.Mkdir(c.IncomingDir, 0777)
if err != nil {
}
iStat, err = os.Stat(c.IncomingDir)
if err != nil {
}
}
if !iStat.IsDir() {
//is not a dir
}
//check for rw
//check outgoing dir exists and can be written to
oStat, err := os.Stat(c.OutgoingDir)
if err != nil {
//Directory does not exist, create dir
err := os.Mkdir(c.OutgoingDir, 0777)
if err != nil {
}
oStat, err = os.Stat(c.OutgoingDir)
if err != nil {
}
}
if !oStat.IsDir() {
//is not a dir
}
//check for rw
//listen addr
s.listenAddr, err = net.ResolveUDPAddr(c.Protocol, strings.Join([]string{c.IP.String(), strconv.Itoa(c.Port)}, ":"))
if err != nil {
return err
}
s.protocol = c.Protocol
return nil
}
//Listen Listen for connections
func (s *TFTPServer) Listen() chan int {
s.Connections = make(map[string]*TFTPConn)
s.ctrlChan = make(chan int)
var err error
bb := make([]byte, 1024000)
//Listen on stats API
if s.Config.Stats {
s.StatsMgr = NewStatsMgr()
s.StatsMgr.StatsListener(s.Config.StatsIP, s.Config.StatsPort, s.ctrlChan)
}
s.sock, err = net.ListenUDP(s.protocol, s.listenAddr)
if err != nil {
log.Println(err)
return s.ctrlChan
}
s.sock.SetReadBuffer(2048000)
s.wg.Add(1)
go func(msg <-chan int) {
for item := range msg {
if item == -1 {
break
}
}
err := s.sock.Close()
if err != nil {
log.Println(err)
}
s.wg.Done()
}(s.ctrlChan)
s.wg.Add(1)
go func() {
for {
//handle each packet in a seperate go routine
msgLen, _, _, addr, err := s.sock.ReadMsgUDP(bb, nil)
if err != nil {
switch err := err.(type) {
case net.Error:
if err.Timeout() {
log.Println(err)
} else if err.Temporary() {
log.Println(err)
}
}
s.wg.Done()
return
}
msg := bb[:msgLen]
//clear buffer by emptying slice but not reallocating memory
bb = bb[:cap(bb)]
log.Println("New Connection from", addr.String())
nullBytes := bytes.Count(msg, []byte{'\x00'})
//Normal TFTP connection
if nullBytes == 3 {
if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeRead {
pkt := &tftp.TFTPReadWritePkt{}
pkt.Unpack(msg)
s.Start(addr, pkt)
} else if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeWrite {
pkt := &tftp.TFTPReadWritePkt{}
pkt.Unpack(msg)
s.Start(addr, pkt)
} else if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeErr {
pkt := &tftp.TFTPErrPkt{}
pkt.Unpack(msg)
}
//Option packet sent
} else if nullBytes > 3 {
if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeRead {
pkt := &tftp.TFTPOptionPkt{Options: make(map[string]string)}
pkt.Unpack(msg)
s.StartOptions(addr, pkt)
} else if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeWrite {
pkt := &tftp.TFTPOptionPkt{Options: make(map[string]string)}
pkt.Unpack(msg)
s.StartOptions(addr, pkt)
} else if binary.BigEndian.Uint16(msg[:2]) == tftp.OpcodeErr {
pkt := &tftp.TFTPErrPkt{}
pkt.Unpack(msg)
}
}
}
}()
log.Printf("%s now listening on %s for incoming connections", AppName, s.listenAddr.String())
return s.ctrlChan
}