forked from FactomProject/factomd
/
parcel.go
164 lines (139 loc) · 6.02 KB
/
parcel.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package p2p
import (
"bytes"
"fmt"
"hash/crc32"
log "github.com/sirupsen/logrus"
)
var parcelLogger = packageLogger.WithField("subpack", "connection")
// Parcel is the atomic level of communication for the p2p network. It contains within it the necessary info for
// the networking protocol, plus the message that the Application is sending.
type Parcel struct {
Header ParcelHeader
Payload []byte
}
// ParcelHeaderSize is the number of bytes in a parcel header
const ParcelHeaderSize = 32
type ParcelHeader struct {
Network NetworkID // 4 bytes - the network we are on (eg testnet, main net, etc.)
Version uint16 // 2 bytes - the version of the protocol we are running.
Type ParcelCommandType // 2 bytes - network level commands (eg: ping/pong)
Length uint32 // 4 bytes - length of the payload (that follows this header) in bytes
TargetPeer string // ? bytes - "" or nil for broadcast, otherwise the destination peer's hash.
Crc32 uint32 // 4 bytes - data integrity hash (of the payload itself.)
PartNo uint16 // 2 bytes - in case of multipart parcels, indicates which part this corresponds to, otherwise should be 0
PartsTotal uint16 // 2 bytes - in case of multipart parcels, indicates the total number of parts that the receiver should expect
NodeID uint64
PeerAddress string // address of the peer set by connection to know who sent message (for tracking source of other peers)
PeerPort string // port of the peer , or we are listening on
AppHash string // Application specific message hash, for tracing
AppType string // Application specific message type, for tracing
}
type ParcelCommandType uint16
// Parcel commands -- all new commands should be added to the *end* of the list!
const ( // iota is reset to 0
TypeHeartbeat ParcelCommandType = iota // "Note, I'm still alive"
TypePing // "Are you there?"
TypePong // "yes, I'm here"
TypePeerRequest // "Please share some peers"
TypePeerResponse // "Here's some peers I know about."
TypeAlert // network wide alerts (used in bitcoin to indicate criticalities)
TypeMessage // Application level message
TypeMessagePart // Application level message that was split into multiple parts
)
// CommandStrings is a Map of command ids to strings for easy printing of network comands
var CommandStrings = map[ParcelCommandType]string{
TypeHeartbeat: "Heartbeat", // "Note, I'm still alive"
TypePing: "Ping", // "Are you there?"
TypePong: "Pong", // "yes, I'm here"
TypePeerRequest: "Peer-Request", // "Please share some peers"
TypePeerResponse: "Peer-Response", // "Here's some peers I know about."
TypeAlert: "Alert", // network wide alerts (used in bitcoin to indicate criticalities)
TypeMessage: "Message", // Application level message
TypeMessagePart: "MessagePart", // Application level message that was split into multiple parts
}
// MaxPayloadSize is the maximum bytes a message can be at the networking level.
const MaxPayloadSize = 1000000000
func NewParcel(network NetworkID, payload []byte) *Parcel {
header := new(ParcelHeader).Init(network)
header.AppHash = "NetworkMessage"
header.AppType = "Network"
parcel := new(Parcel).Init(*header)
parcel.Payload = payload
parcel.UpdateHeader() // Updates the header with info about payload.
return parcel
}
func ParcelsForPayload(network NetworkID, payload []byte) []Parcel {
parcelCount := (len(payload) / MaxPayloadSize) + 1
parcels := make([]Parcel, parcelCount)
for i := 0; i < parcelCount; i++ {
start := i * MaxPayloadSize
next := (i + 1) * MaxPayloadSize
var end int
if next < len(payload) {
end = next
} else {
end = len(payload)
}
parcel := NewParcel(network, payload[start:end])
parcel.Header.Type = TypeMessagePart
parcel.Header.PartNo = uint16(i)
parcel.Header.PartsTotal = uint16(parcelCount)
parcels[i] = *parcel
}
return parcels
}
func ReassembleParcel(parcels []*Parcel) *Parcel {
var payload bytes.Buffer
for _, parcel := range parcels {
payload.Write(parcel.Payload)
}
// create a new message parcel from the reassembled payload, but
// copy all the relevant header fields from one of the original
// messages
origHeader := parcels[0].Header
assembledParcel := NewParcel(origHeader.Network, payload.Bytes())
assembledParcel.Header.NodeID = origHeader.NodeID
assembledParcel.Header.Type = TypeMessage
assembledParcel.Header.TargetPeer = origHeader.TargetPeer
assembledParcel.Header.PeerAddress = origHeader.PeerAddress
assembledParcel.Header.PeerPort = origHeader.PeerPort
return assembledParcel
}
func (p *ParcelHeader) Init(network NetworkID) *ParcelHeader {
p.Network = network
p.Version = ProtocolVersion
p.Type = TypeMessage
p.TargetPeer = "" // initially no target
p.PeerPort = NetworkListenPort // store our listening port
return p
}
func (p *Parcel) Init(header ParcelHeader) *Parcel {
p.Header = header
return p
}
func (p *Parcel) UpdateHeader() {
p.Header.Crc32 = crc32.Checksum(p.Payload, CRCKoopmanTable)
p.Header.Length = uint32(len(p.Payload))
}
func (p *Parcel) LogEntry() *log.Entry {
return parcelLogger.WithFields(log.Fields{
"network": p.Header.Network.String(),
"version": p.Header.Version,
"app_hash": p.Header.AppHash,
"app_type": p.Header.AppType,
"command": CommandStrings[p.Header.Type],
"length": p.Header.Length,
"target_peer": p.Header.TargetPeer,
"crc32": p.Header.Crc32,
"node_id": p.Header.NodeID,
"part_no": p.Header.PartNo + 1,
"parts_total": p.Header.PartsTotal,
})
}
func (p *Parcel) MessageType() string {
return (fmt.Sprintf("[%s]", CommandStrings[p.Header.Type]))
}