-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
265 lines (216 loc) · 4.88 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Based on https://sourceforge.net/p/nbd/code/ci/master/tree/doc/proto.md
*/
package main
import (
"encoding/binary"
"flag"
"fmt"
"github.com/pkg/errors"
"io"
"math/rand"
"net"
"os"
)
var (
host = flag.String("host", "localhost", "Hostname")
port = flag.Uint("port", 10809, "Port")
data = flag.String("data", "", "Data to write")
offset = flag.Uint64("offset", 0, "Offset")
)
const (
NBD_SERVER_MAGIC = 0x4e42444d41474943
NBD_CLIENTSERV_MAGIC = 0x00420281861253
NBD_HANDSHAKE_MSG_SIZE = 0x98
)
const (
NBD_CMD_READ = 0
NBD_CMD_WRITE = 1
)
func main() {
flag.Parse()
if *data == "" {
fmt.Fprintf(os.Stderr, "Data to write ccentrinannot be empty")
os.Exit(1)
}
nbd := NewNBD(*host, uint16(*port))
if err := nbd.Connect(); err != nil {
panic(err)
}
defer nbd.Close()
if err := nbd.Handshake(); err != nil {
panic(err)
}
/*
data, err := nbd.Read(0, 1024)
if err != nil {
panic(err)
}
fmt.Println(string(data))
*/
if err := nbd.Write(*offset, []byte(*data)); err != nil {
panic(err)
}
}
type NBD struct {
host string
port uint16
conn net.Conn
exportSize uint64
flags uint32
}
func NewNBD(host string, port uint16) *NBD {
return &NBD{
host,
port,
nil,
0,
0,
}
}
func (n *NBD) Connect() error {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", n.host, n.port))
if err != nil {
return err
}
n.conn = conn
return nil
}
func (n *NBD) Close() {
n.conn.Close()
}
func (n *NBD) Handshake() error {
data := make([]byte, NBD_HANDSHAKE_MSG_SIZE)
num, err := io.ReadFull(n.conn, data)
if err != nil {
return err
}
if num < NBD_HANDSHAKE_MSG_SIZE {
return errors.New("handshake message is too short")
}
magic := binary.BigEndian.Uint64(data[0:8])
if magic != NBD_SERVER_MAGIC {
return fmt.Errorf("bad magic value: %d", magic)
}
clientServMagic := binary.BigEndian.Uint64(data[8:16])
if clientServMagic != NBD_CLIENTSERV_MAGIC {
return fmt.Errorf("bad cliserv_magic value: %d", clientServMagic)
}
exportSize := binary.BigEndian.Uint64(data[16:24])
n.exportSize = exportSize
flags := binary.BigEndian.Uint32(data[24:28])
n.flags = flags
reserved := data[28:]
for i := range reserved {
if reserved[i] != 0 {
return fmt.Errorf("invalid data in reserved bytes %d", reserved[i])
}
}
return nil
}
const (
NBD_REQUEST_MAGIC = 0x25609513
NBD_REPLY_MAGIC = 0x67446698
)
type NBDRequest struct {
Magic uint32
Type uint16
Command uint16
Handle uint64
Offset uint64
Length uint32
Data []byte
}
func (r *NBDRequest) Pack() []byte {
result := make([]byte, 28, 28)
binary.BigEndian.PutUint32(result[0:4], r.Magic)
binary.BigEndian.PutUint16(result[4:6], r.Type)
binary.BigEndian.PutUint16(result[6:8], r.Command)
binary.BigEndian.PutUint64(result[8:16], r.Handle)
binary.BigEndian.PutUint64(result[16:24], r.Offset)
binary.BigEndian.PutUint32(result[24:28], r.Length)
return append(result, r.Data...)
}
const (
NBD_REPLY_SIZE = 16
)
type NBDReply struct {
Magic uint32
Error uint32
Handle uint64
Data []byte
}
func UnpackReplyFromData(data []byte) (*NBDReply, error) {
if len(data) < 16 {
return nil, errors.New("data is too short")
}
return &NBDReply{
binary.BigEndian.Uint32(data[0:4]),
binary.BigEndian.Uint32(data[4:8]),
binary.BigEndian.Uint64(data[8:16]),
data[16:],
}, nil
}
func (n *NBD) sendRequest(request *NBDRequest) (*NBDReply, error) {
data := request.Pack()
num, err := n.conn.Write(data)
if err != nil {
return nil, err
}
if num != len(data) {
return nil, errors.New("couldn't send all data")
}
rspLen := uint32(0)
switch request.Command {
case NBD_CMD_READ:
rspLen = NBD_REPLY_SIZE + request.Length
case NBD_CMD_WRITE:
rspLen = NBD_REPLY_SIZE
}
buf := make([]byte, rspLen)
readNum, err := n.conn.Read(buf)
if err != nil {
return nil, err
}
if readNum != int(rspLen) {
return nil, fmt.Errorf("cannot read all reply data: expected %d, read %d", request.Length, readNum)
}
reply, err := UnpackReplyFromData(buf)
if err != nil {
return nil, err
}
return reply, nil
}
func (n *NBD) sendCommand(command uint16, offset uint64, length uint32, data []byte) ([]byte, error) {
handle := rand.Uint64()
request := &NBDRequest{
NBD_REQUEST_MAGIC,
0,
command,
handle,
offset,
length,
data,
}
reply, err := n.sendRequest(request)
if err != nil {
return nil, err
}
if reply.Magic != NBD_REPLY_MAGIC {
return nil, errors.New("invalid reply magic")
}
if reply.Handle != handle {
return nil, errors.New("invalid reply handle")
}
if reply.Error != 0 {
return nil, fmt.Errorf("unknown reply error: code %x", reply.Error)
}
return reply.Data, nil
}
func (n *NBD) Read(offset uint64, length uint32) ([]byte, error) {
return n.sendCommand(NBD_CMD_READ, offset, length, nil)
}
func (n *NBD) Write(offset uint64, data []byte) error {
_, err := n.sendCommand(NBD_CMD_WRITE, offset, uint32(len(data)), data)
return err
}