-
Notifications
You must be signed in to change notification settings - Fork 9
/
peersdb.go
142 lines (119 loc) · 4.2 KB
/
peersdb.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
package bitpeers
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net"
)
type PeersDB struct {
Path string `json:"-"`
MessageBytes []byte `json:"message_bytes"` // 0 : 4
Version uint8 `json:"version"` // 4 : 4
KeySize uint8 `json:"keysize"` // 5 : 5
NKey []byte `json:"nkey"` // 37 : 32
NNew uint32 `json:"nnew"` // 41 : 4
NTried uint32 `json:"ntried"` // 45 : 4
NewBuckets uint32 `json:"new_buckets"` // 49 : 4
NewAddrInfo []CAddrInfo `json:"new_addr_info"`
TriedAddrInfo []CAddrInfo `json:"tried_addr_info"`
}
type CAddrInfo struct {
Address CAddress `json:"address"`
Source net.IP `json:"source"`
LastSuccess uint64 `json:"last_success"`
Attempts uint32 `json:"attempts"`
}
type CAddress struct {
SerializationVersion []byte `json:"serialization_version"`
Time uint32 `json:"time"`
ServiceFlags []byte `json:"service_flags"`
PeerAddress CService `json:"ip"`
}
type CService struct {
IPAddress net.IP
Port uint16 // This is serialized as BigEndian
}
func NewPeersDB(path string) (PeersDB, error) {
peersDB := PeersDB{
Path: path,
}
dbbytes, err := readDBBytes(peersDB)
if err != nil {
return peersDB, fmt.Errorf("Couldn't read peer file %s", peersDB.Path)
}
dbreader := DBReader{
Bytes: dbbytes,
Cursor: 0,
}
peersDB.MessageBytes = dbreader.readBytes(4)
peersDB.Version = dbreader.readUint8()
peersDB.KeySize = dbreader.readUint8()
peersDB.NKey = dbreader.readBytes(32) // uint256 type
peersDB.NNew = dbreader.readUint32() // int type
peersDB.NTried = dbreader.readUint32() // int type
peersDB.NewBuckets = dbreader.readUint32() ^ (1 << 30) // int type
peersDB.NewAddrInfo = make([]CAddrInfo, peersDB.NNew)
peersDB.TriedAddrInfo = make([]CAddrInfo, peersDB.NTried)
var i uint32
for i = 0; i < peersDB.NNew; i++ {
peersDB.NewAddrInfo[i] = dbreader.readCAddrInfo()
}
for i = 0; i < peersDB.NTried; i++ {
peersDB.TriedAddrInfo[i] = dbreader.readCAddrInfo()
}
return peersDB, nil
}
func (dbreader *DBReader) readCAddrInfo() (cAddrInfo CAddrInfo) {
cAddrInfo.Address.SerializationVersion = dbreader.readBytes(4)
cAddrInfo.Address.Time = dbreader.readUint32()
cAddrInfo.Address.ServiceFlags = reverseBytes(dbreader.readBytes(8))
cAddrInfo.Address.PeerAddress.IPAddress = dbreader.readBytes(16)
cAddrInfo.Address.PeerAddress.Port = dbreader.readBigEndianUint16()
cAddrInfo.Source = dbreader.readBytes(16)
cAddrInfo.LastSuccess = dbreader.readUint64()
cAddrInfo.Attempts = dbreader.readUint32()
return
}
func readDBBytes(peersDB PeersDB) ([]byte, error) {
return ioutil.ReadFile(peersDB.Path)
}
func (cAddrInfo CAddrInfo) String() string {
return fmt.Sprintf("%s\nSource: %s\nLastSuccess: %d\nAttempts: %d\n\n", cAddrInfo.Address, cAddrInfo.Source, cAddrInfo.LastSuccess, cAddrInfo.Attempts)
}
func (cAddress CAddress) String() string {
return fmt.Sprintf("SerializationVersion: %s\nTime: %d\nServiceFlags: 0x%s\nIP: %s", hexstring(cAddress.SerializationVersion), cAddress.Time, hexstring(cAddress.ServiceFlags), cAddress.PeerAddress)
}
func (cService CService) String() string {
return fmt.Sprintf("%s:%d", cService.IPAddress, cService.Port)
}
func (cAddress *CAddress) MarshalJSON() ([]byte, error) {
type Alias CAddress
return json.Marshal(&struct {
IP string `json:"ip"`
SerializationVersion string `json:"serialization_version"`
ServiceFlags string `json:"service_flags"`
*Alias
}{
IP: cAddress.PeerAddress.String(),
SerializationVersion: hexstring(cAddress.SerializationVersion),
ServiceFlags: binaryString(cAddress.ServiceFlags),
Alias: (*Alias)(cAddress),
})
}
func hexstring(input []byte) string {
return hex.EncodeToString(input)
}
func reverseBytes(input []byte) []byte {
for i, j := 0, len(input)-1; i < j; i, j = i+1, j-1 {
input[i], input[j] = input[j], input[i]
}
return input
}
func binaryString(input []byte) string {
var binaryString string
for _, x := range input {
binaryString += fmt.Sprintf("%08b", x)
}
return binaryString
}