aboutsummaryrefslogtreecommitdiff
path: root/peer/messages.go
blob: 4760bb5166c9af13b672e643342e0eb6dfe49980 (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 (
	"crypto/rand"
	"fmt"
	"math/big"
)

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`
	ClientID = `+++ATH0-xxxxxxxxxxxx` // or may be `D13--xxxxxxxxxxxxxxx`
)

func NewID() (string, error) {
	limit := new(big.Int).Lsh(big.NewInt(1), 48)
	id, err := rand.Int(rand.Reader, limit)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("+++ATH0-%0.12x", id), nil
}