forked from RobWC/nitro-tftp
/
tftp_client_mgr.go
209 lines (196 loc) · 5.58 KB
/
tftp_client_mgr.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
206
207
208
209
package main
import (
"bufio"
"bytes"
"encoding/binary"
"net"
"os"
"path"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
//Start starta new TFTP client session
func (c *TFTPServer) Start(addr *net.UDPAddr, msg interface{}) {
//add connection
if tftpMsg, ok := msg.(*TFTPReadWritePkt); ok {
nc := &TFTPConn{Type: tftpMsg.Opcode, remote: addr, blockSize: DefaultBlockSize, filename: msg.(*TFTPReadWritePkt).Filename}
c.Connections[addr.String()] = nc
if tftpMsg.Opcode == OpcodeRead {
//Setting block to min of 1
log.Printf("Sending file %s to client %s", nc.filename, addr.String())
c.Connections[addr.String()].block = 1
c.sendData(addr.String())
} else if tftpMsg.Opcode == OpcodeWrite {
//Setting block to min of 0
log.Printf("Receiving file %s from client %s", nc.filename, addr.String())
c.Connections[addr.String()].block = 0
c.recieveData(addr.String())
}
}
//send error message
}
//ACK handle ack packet
func (c *TFTPServer) ACK(addr *net.UDPAddr, msg interface{}) {
if tftpMsg, ok := msg.(*TFTPAckPkt); ok {
log.Printf("%#v", tftpMsg)
}
}
func (c *TFTPServer) sendAck(conn *net.UDPConn, tid string) {
pkt := &TFTPAckPkt{Opcode: OpcodeACK, Block: c.Connections[tid].block}
conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
if _, err := conn.Write(pkt.Pack()); err != nil {
log.Println(err)
}
}
func (c *TFTPServer) sendError(opcode int, tid string) {
}
func (c *TFTPServer) sendData(tid string) {
//TODO: Implement reverse of recieve data
//read from file send to destination, update block
if r, err := net.DialUDP(ServerNet, nil, c.Connections[tid].remote); err != nil {
log.Println(err)
} else {
c.clientwg.Add(1)
go func() {
defer c.clientwg.Done()
buffer := make([]byte, c.Connections[tid].blockSize)
bb := make([]byte, 1024000)
fileName := strings.Join([]string{c.outgoingDir, c.Connections[tid].filename}, "/")
inputFile, err := os.OpenFile(path.Clean(fileName), os.O_RDWR|os.O_CREATE, 0660)
defer inputFile.Close()
if err != nil {
//Unable to open file, send error to client
log.Println(err)
}
inputReader := bufio.NewReader(inputFile)
for {
dLen, err := inputReader.Read(buffer)
if err != nil {
//unable to read from file
log.Println(err)
}
pkt := &TFTPDataPkt{Opcode: OpcodeData, Block: c.Connections[tid].block, Data: bytes.Trim(buffer, "\x00")}
r.SetWriteDeadline(time.Now().Add(1 * time.Second))
if _, err := r.Write(pkt.Pack()); err != nil {
log.Println(err)
}
c.Connections[tid].BytesSent = c.Connections[tid].BytesSent + len(pkt.Data)
buffer = buffer[:cap(buffer)]
//TODO: send next packet once block is sent
msgLen, _, _, _, err := r.ReadMsgUDP(bb, nil)
if err != nil {
switch err := err.(type) {
case net.Error:
if err.Timeout() {
log.Error(err)
} else if err.Temporary() {
log.Error(err)
}
}
return
}
//pull message from buffer
msg := bb[:msgLen]
//clear buffer
bb = bb[:cap(bb)]
//TODO: Process ACK
if uint16(msg[1]) == OpcodeACK {
pkt := &TFTPAckPkt{}
pkt.Unpack(msg)
//Write Data
c.Connections[tid].block = c.Connections[tid].block + 1
} else {
//TODO: send error
}
if c.Connections[tid].blockSize > dLen {
log.Printf("Sending file %s to client %s complete, total size %d", c.Connections[tid].filename, tid, c.Connections[tid].BytesSent)
return
}
}
}()
}
}
func (c *TFTPServer) recieveData(tid string) {
if r, err := net.DialUDP(ServerNet, nil, c.Connections[tid].remote); err != nil {
log.Println(err)
} else {
c.sendAck(r, tid)
c.clientwg.Add(1)
go func() {
defer c.clientwg.Done()
bb := make([]byte, 1024000)
fileName := strings.Join([]string{c.outgoingDir, c.Connections[tid].filename}, "/")
outputFile, err := os.OpenFile(path.Clean(fileName), os.O_RDWR|os.O_CREATE, 0660)
if err != nil {
//Unable to open file, send error to client
log.Println(err)
}
outputWriter := bufio.NewWriter(outputFile)
for {
//handle each packet in a seperate go routine
msgLen, _, _, _, err := r.ReadMsgUDP(bb, nil)
if err != nil {
switch err := err.(type) {
case net.Error:
if err.Timeout() {
log.Error(err)
} else if err.Temporary() {
log.Error(err)
}
}
return
}
//pull message from buffer
msg := bb[:msgLen]
//clear buffer
bb = bb[:cap(bb)]
opCode := binary.BigEndian.Uint16(msg[:2])
if opCode == OpcodeData {
pkt := &TFTPDataPkt{}
pkt.Unpack(msg)
//Write Data
ofb, err := outputWriter.Write(pkt.Data)
if err != nil {
//Unable to write to file
log.Println(err)
}
//add bytes received
c.Connections[tid].BytesRecv = c.Connections[tid].BytesRecv + ofb
if c.Debug {
log.Debug("Wrote %d bytes to file %s", ofb, c.Connections[tid].filename)
}
c.Connections[tid].block = pkt.Block
if len(pkt.Data) < DefaultBlockSize {
//last packet
c.sendAck(r, tid)
err := r.Close()
if err != nil {
panic(err)
}
err = outputWriter.Flush()
if err != nil {
log.Println(err)
}
log.Printf("Writing file %s from client %s complete, total size %d", c.Connections[tid].filename, tid, c.Connections[tid].BytesRecv)
return
}
//continue to read data
c.sendAck(r, tid)
} else {
//TODO: send error
}
}
}()
}
}
//TFTPConn TFTP connection
type TFTPConn struct {
Type uint16
remote *net.UDPAddr
block uint16
blockSize int
filename string
BytesSent int
BytesRecv int
}