-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.go
170 lines (139 loc) · 3.87 KB
/
parsers.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
package main
import (
"bytes"
"encoding/binary"
"io/ioutil"
"math"
"os"
)
func identifyRequest(buf []byte, length int) string {
switch int(buf[4]) {
case 0xc2, 0x6c:
return "BOOTP"
case 0x56:
return "ARP"
case 0x5f + length, 0x76 + length:
return "TFTP"
case 0x5a:
return "TFTP_Data"
default:
return "notIdentified"
}
}
func processBOOTP(data []byte, filename string) []byte {
var req struct {
_ rndisMessage
Ether etherHeader
Ipv4 ipv4Datagram
Udp udpHeader
Bootp incompleteBootpMessage
}
inbuf := bytes.NewReader(data)
err := binary.Read(inbuf, binary.BigEndian, &req)
check(err)
rndisResp := makeRndis(fullSize - rndisSize).bytes()
etherResp := etherHeader{req.Ether.Source, serverHwaddr, 0x800}
ipResp := makeIpv4Packet(serverIP, bbIP, ipSize+udpSize+bootpSize, 0, ipUDP)
udpResp := makeUdpHeader(req.Udp.Dest, req.Udp.Source, bootpSize)
bootpResp := makeBootpPacket("BEAGLEBOOT", req.Bootp.Xid,
req.Ether.Source, bbIP, serverIP, filename)
buf := new(bytes.Buffer)
err = writeMulti(buf, binary.BigEndian, []interface{}{
rndisResp,
etherResp,
ipResp,
udpResp,
bootpResp})
check(err)
return buf.Bytes()
}
func processARP(data []byte) []byte {
var req struct {
_ rndisMessage
Ether etherHeader
Arp arpMessage
}
inbuf := bytes.NewReader(data)
err := binary.Read(inbuf, binary.BigEndian, &req)
check(err)
arp := makeARPMessage(2, serverHwaddr, req.Arp.TargetProtocolAddr,
req.Arp.SenderHardwareAddr, req.Arp.SenderProtocolAddr)
rndisResp := makeRndis(etherSize + arpSize).bytes()
etherResp := etherHeader{req.Ether.Source, serverHwaddr, 0x806}
buf := new(bytes.Buffer)
err = writeMulti(buf, binary.BigEndian, []interface{}{
rndisResp,
etherResp,
arp})
check(err)
return buf.Bytes()
}
func processTFTP(data []byte, filename string) []byte {
var req struct {
_ rndisMessage
Ether etherHeader
_ ipv4Datagram
Udp udpHeader
}
var blocksize uint16 = 512
inbuf := bytes.NewReader(data)
err := binary.Read(inbuf, binary.BigEndian, &req)
check(err)
dat, err := ioutil.ReadFile(binPath + string(os.PathSeparator) + filename)
check(err)
rndis := makeRndis(etherSize + ipSize + udpSize + tftpSize + uint32(blocksize)).bytes()
etherResp := etherHeader{req.Ether.Source, serverHwaddr, 0x800}
ip := makeIpv4Packet(serverIP, bbIP, ipSize+udpSize+tftpSize+blocksize, 0, ipUDP)
udpResp := makeUdpHeader(req.Udp.Dest, req.Udp.Source, tftpSize+blocksize)
tftp := tftpData{3, 1}
filedata := dat[:blocksize]
buf := new(bytes.Buffer)
err = writeMulti(buf, binary.BigEndian, []interface{}{
rndis,
etherResp,
ip,
udpResp,
tftp,
filedata})
check(err)
return buf.Bytes()
}
func processTFTPData(data []byte, filename string) []byte {
var req struct {
_ rndisMessage
Ether etherHeader
_ ipv4Datagram
Udp udpHeader
Tftp tftpData
}
var blocksize uint16 = 512
inbuf := bytes.NewReader(data)
err := binary.Read(inbuf, binary.BigEndian, &req)
check(err)
dat, err := ioutil.ReadFile(binPath + string(os.PathSeparator) + filename)
check(err)
blocks := uint16(math.Ceil(float64(len(dat)) / float64(blocksize)))
bn := req.Tftp.BlockNumber + 1
if bn == blocks { // Last block
blocksize = uint16(len(dat[(bn-1)*blocksize:]))
} else if bn == blocks+1 { //Finished
return []byte{}
}
rndis := makeRndis(etherSize + ipSize + udpSize + tftpSize + uint32(blocksize)).bytes()
etherResp := etherHeader{req.Ether.Source, serverHwaddr, 0x800}
ip := makeIpv4Packet(serverIP, bbIP, ipSize+udpSize+tftpSize+blocksize, 0, ipUDP)
udpResp := makeUdpHeader(req.Udp.Dest, req.Udp.Source, tftpSize+blocksize)
tftp := tftpData{3, bn}
start := (uint64(bn) - 1) * uint64(512)
filedata := dat[start : start+uint64(blocksize)]
buf := new(bytes.Buffer)
err = writeMulti(buf, binary.BigEndian, []interface{}{
rndis,
etherResp,
ip,
udpResp,
tftp,
filedata})
check(err)
return buf.Bytes()
}