aboutsummaryrefslogtreecommitdiff
path: root/peer/messages.go
blob: a6f28157518ec3271d79f7e30f5183c7fd9d7078 (plain)
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
package peer

import "io"

type Message int

const (
	Choke         Message = iota // choke: <len=0001><id=0>
	Unchoke                      // unchoke: <len=0001><id=1>
	Interested                   // interested: <len=0001><id=2>
	NotInterested                // not interested: <len=0001><id=3>
	Have                         // have: <len=0005><id=4><piece index>
	BitField                     // bitfield: <len=0001+X><id=5><bitfield>
	Request                      // request: <len=0013><id=6><index><begin><length>
	Piece                        // piece: <len=0009+X><id=7><index><begin><block>
	Cancel                       // cancel: <len=0013><id=8><index><begin><length>
	Port                         // port: <len=0003><id=9><listen-port>
	_                            // keep-alive: <len=0000>
)

const Proto = `BitTorrent protocol`

type Handshake struct {
	Proto    string
	Flags    [8]byte
	InfoHash [20]byte
	PeerID   [20]byte
}

func (h Handshake) Encode(w io.Writer) {
	l := len(h.Proto)
	w.Write([]byte{byte(l)})
	w.Write([]byte(h.Proto))
	w.Write(h.Flags[:])
	w.Write(h.InfoHash[:])
	w.Write(h.PeerID[:])
}