-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
252 lines (221 loc) · 6.08 KB
/
share.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
package main
import (
"bytes"
bencode "code.google.com/p/bencode-go"
"crypto/sha1"
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io"
"math"
"net"
"os"
//"os"
)
type share struct {
Secret string
Path string
Db *sql.DB
peers map[string]net.Addr //map[port:ip]addr
}
//TODO this could be useful for maps and DB
func newShare(secret, path string) share {
return share{
secret,
path,
nil,
make(map[string]net.Addr),
}
}
//return map[path]hash(torrent)
//eh maybe err, maybe not the hash, do that later
func (s *share) getFileHashes() map[string]string {
rows, err := s.Db.Query("SELECT path, data FROM files")
check(err)
files := make(map[string]string)
for rows.Next() {
var path string
var data []byte
rows.Scan(&path, &data)
files[path] = fmt.Sprintf("%s", sha1.Sum(data))
}
rows.Close()
return files
}
func (s *share) processMeta(u *UDPMessage, out chan *UDPMessage) {
msg, sender := u.data, u.addr
fmt.Println("process meta")
mfiles := s.getFileHashes()
var shake Shake
err := bencode.Unmarshal(bytes.NewBuffer(msg), &shake)
check(err)
yfiles := shake.Files
for yf, yhash := range yfiles {
mhash, ok := mfiles[yf]
if !ok || mhash != yhash {
b := s.createRequest(yf, -1, -1, -1)
out <- &UDPMessage{sender, b}
}
}
}
func (s *share) processRequest(msg []byte) ([]byte, error) {
fmt.Println("process request")
var r Request
err := bencode.Unmarshal(bytes.NewBuffer(msg), &r)
if err != nil {
return []byte{}, err
}
mdata, err := s.getFileMeta(r.File)
check(err)
if r.Index == -1 && r.Begin == -1 && r.Length == -1 {
fmt.Println("want some meta")
var data bytes.Buffer
err = bencode.Marshal(&data, *mdata)
fmt.Println(mdata)
check(err)
return s.createPiece(r.File, -1, -1, data.Bytes()), nil
}
buf := make([]byte, r.Length)
f, err := os.Open(s.Path + "/" + r.File)
check(err)
_, err = f.ReadAt(buf, int64(r.Index*mdata.Piece_length+r.Begin))
f.Close()
check(err)
fmt.Println("sending piece for", r.File, r.Index, r.Begin)
return s.createPiece(r.File, r.Index, r.Begin, buf), nil
}
func (s *share) processPiece(u *UDPMessage, out chan *UDPMessage) {
msg, sender := u.data, u.addr
fmt.Println("Process piece")
var p Piece
err := bencode.Unmarshal(bytes.NewBuffer(msg), &p)
check(err)
var data []byte
err = s.Db.QueryRow("SELECT data FROM files WHERE path = ?", p.File).Scan(&data)
check(err)
//if meta and file we're not aware of, just take their meta
if err == sql.ErrNoRows && p.Index == -1 && p.Begin == -1 {
fmt.Println("mine", len(p.Piece))
_, err := s.Db.Exec("INSERT INTO files(path, data) values(?, ?)", p.File, p.Piece)
//TODO modify bitset HAVES to all 0
check(err)
} else if err != nil {
return
}
var mdata bt_file
err = bencode.Unmarshal(bytes.NewBuffer(data), &mdata)
//thanks to hashes, EOF characters in string (but don't break anything)
if err != io.ErrUnexpectedEOF {
check(err)
}
fmt.Println("time", mdata.Time)
fmt.Println("Length", mdata.Length)
fmt.Println("piece_len", mdata.Piece_length)
//fmt.Println("pieces", mdata.Pieces)
if p.Index == -1 && p.Begin == -1 {
fmt.Println("got meta")
var ydata bt_file
err = bencode.Unmarshal(bytes.NewBuffer([]byte(p.Piece)), &ydata)
check(err)
if mdata.Time > ydata.Time {
return //I don't need this
}
//process meta; at this point, I either didn't have it or theirs is newer
rlength := int(math.Min(float64(BLOCK_SIZE), float64(ydata.Piece_length)))
for i := 0; i < len(ydata.Pieces)/20; i++ { //256k chunks
pb := i * 20 //piece hash actual index
if i >= len(mdata.Pieces)/20 ||
mdata.Pieces[pb:pb+20] != ydata.Pieces[pb:pb+20] {
for j := 0; j < ydata.Piece_length; j += rlength { //16K chunks
length := int(math.Min(float64(ydata.Length-int64((i*ydata.Piece_length)+j)), float64(rlength)))
out <- &UDPMessage{sender, s.createRequest(p.File, i, j, length)}
if length < rlength {
break
}
}
}
}
} else {
fmt.Printf("opening %s to write at %d, %d\n", p.File, p.Index, p.Begin)
//TODO eh, flags are weird
//TODO also need to see behavior on files where WriteAt() will be OOB
f, err := os.OpenFile(s.Path+"/"+p.File, os.O_RDWR|os.O_CREATE, 0666)
check(err)
_, err = f.WriteAt([]byte(p.Piece), int64(p.Index*mdata.Piece_length+p.Begin))
f.Close()
check(err)
}
}
func (s *share) createPing(secret string) []byte {
buf := bytes.NewBuffer([]byte("DBIT"))
err := bencode.Marshal(buf, Header{
"ping",
*port,
fmt.Sprintf("%s", sha1.Sum([]byte(secret))),
fmt.Sprintf("%s", sha1.Sum([]byte("192.168.1.64:6667"))),
})
check(err)
return buf.Bytes()
}
//analagous to getTracker
//TODO do this recursively
//gotta find peers first
func (s *share) createMetaShake() []byte {
files := s.getFileHashes()
peers := make([]string, 0)
for p, _ := range s.peers {
peers = append(peers, p)
}
b := bytes.NewBuffer([]byte("DBIT"))
err := bencode.Marshal(b, Shake{
"meta",
*port,
fmt.Sprintf("%s", sha1.Sum([]byte(s.Secret))),
fmt.Sprintf("%s", sha1.Sum([]byte("192.168.1.64:6667"))), //FIXME config?
files,
peers,
})
check(err)
return b.Bytes()
}
func (s *share) createPiece(path string, index, begin int, piece []byte) []byte {
b := bytes.NewBuffer([]byte("DBIT"))
err := bencode.Marshal(b, Piece{
"piece",
*port,
fmt.Sprintf("%s", sha1.Sum([]byte(s.Secret))),
fmt.Sprintf("%s", sha1.Sum([]byte("192.168.1.64:6667"))),
path,
index,
begin,
fmt.Sprintf("%s", piece),
})
check(err)
return b.Bytes()
}
func (s *share) createRequest(path string, index, begin, length int) []byte {
b := bytes.NewBuffer([]byte("DBIT"))
err := bencode.Marshal(b, Request{
"req",
*port,
fmt.Sprintf("%s", sha1.Sum([]byte(s.Secret))),
fmt.Sprintf("%s", sha1.Sum([]byte("192.168.1.64:6667"))),
path,
index,
begin,
length,
})
check(err)
return b.Bytes()
}
func (s *share) getFileMeta(path string) (btf *bt_file, err error) {
var data []byte
err = s.Db.QueryRow("SELECT data FROM files WHERE path = ?", path).Scan(&data)
if err != nil {
return btf, err
}
//TODO THIS IS WEIRD, WE NEED TO GO DEEPER
var bt bt_file
err = bencode.Unmarshal(bytes.NewBuffer(data), &bt)
return &bt, err
}