-
Notifications
You must be signed in to change notification settings - Fork 0
/
talk.go
135 lines (123 loc) · 2.72 KB
/
talk.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
package main
import (
"bytes"
bencode "code.google.com/p/bencode-go"
"crypto/sha1"
"fmt"
"net"
"strconv"
)
type UDPMessage struct {
addr *net.UDPAddr
data []byte
}
//map[address]conn
var peers = make(map[string]*net.UDPConn)
func listen(msg chan *UDPMessage) {
me, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(*port))
check(err)
mcast, err := net.ResolveUDPAddr("udp", "239.192.0.0:3838")
check(err)
lan, err := net.ListenMulticastUDP("udp", nil, mcast)
check(err)
sock, err := net.ListenUDP("udp", me)
check(err)
sock.SetReadBuffer(1 << 16) //64K
check(err)
l := func(c *net.UDPConn) {
for {
b := make([]byte, 65000)
_, addr, err := c.ReadFromUDP(b)
if err != nil {
continue
}
msg <- &UDPMessage{addr, b}
}
}
go l(lan)
go l(sock)
}
//hash is the hashed secret sent over the wire
func getShare(hash string) (share, error) {
for secret, s := range shares {
fmt.Printf("%s\n", hash)
fmt.Printf("%s\n", sha1.Sum([]byte(secret)))
if hash == fmt.Sprintf("%s", sha1.Sum([]byte(secret))) {
return s, nil
}
}
return share{}, fmt.Errorf("Not a common share")
}
//p == nil if not a ping
func parseHeader(b []byte) (Header, error) {
var h Header
if string(b[:4]) == "DBIT" {
err := bencode.Unmarshal(bytes.NewBuffer(b[4:]), &h)
if err != nil {
return h, fmt.Errorf("Invalid Bencoding")
}
return h, nil
}
return h, fmt.Errorf("Not a Dropbit message")
}
func handleMessage(m *UDPMessage, out chan *UDPMessage) {
addr, b := m.addr, m.data
h, err := parseHeader(b)
if err != nil {
return
}
s, err := getShare(h.Share)
if err != nil {
return
}
//above pretty much figures out for me or not
//below sees if new peer
//TODO consider seperate channel for peer discovery
//TODO also this can be done elsewhere
addr = changePort(addr, h.Port)
fmt.Println(addr)
_, known := s.peers[addr.String()]
if !known {
s.peers[addr.String()] = addr
out <- &UDPMessage{addr, s.createMetaShake()}
}
b = b[4:] //slice off DBIT
switch h.M {
case "ping":
fmt.Println("do ping")
case "meta":
//TODO eh this just feels wrong, unmute this shit
s.processMeta(&UDPMessage{addr, b}, out)
case "req":
m, err := s.processRequest(b)
if err != nil {
//if DB locks up or something
return
}
out <- &UDPMessage{addr, m}
case "have":
case "piece":
s.processPiece(&UDPMessage{addr, b}, out)
}
return
}
func sendMessage(m *UDPMessage) {
conn, ok := peers[m.addr.String()]
var err error
if !ok {
conn, err = net.DialUDP("udp", nil, m.addr)
conn.SetWriteBuffer(65000)
check(err)
if err != nil {
return
}
peers[m.addr.String()] = conn
}
_, err = conn.Write(m.data)
check(err)
return
}
func changePort(addr *net.UDPAddr, port int) *net.UDPAddr {
addr.Port = port
return addr
}