forked from ishbir/bmutil
/
protocol.go
85 lines (70 loc) · 2.12 KB
/
protocol.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
// Originally derived from: btcsuite/btcd/wire/protocol.go
// Copyright (c) 2013-2015 Conformal Systems LLC.
// Copyright (c) 2015 Monetas
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"fmt"
"strconv"
"strings"
)
const (
// ProtocolVersion is the latest protocol version this package supports.
ProtocolVersion uint32 = 3
)
// ServiceFlag identifies services supported by a bitmessage peer.
type ServiceFlag uint64
const (
// SFNodeNetwork is a flag used to indicate a peer is a full node.
SFNodeNetwork ServiceFlag = 1 << iota
)
// Map of service flags back to their constant names for pretty printing.
var sfStrings = map[ServiceFlag]string{
SFNodeNetwork: "SFNodeNetwork",
}
// String returns the ServiceFlag in human-readable form.
func (f ServiceFlag) String() string {
// No flags are set.
if f == 0 {
return "0x0"
}
// Add individual bit flags.
s := ""
for flag, name := range sfStrings {
if f&flag == flag {
s += name + "|"
f -= flag
}
}
// Add any remaining flags which aren't accounted for as hex.
s = strings.TrimRight(s, "|")
if f != 0 {
s += "|0x" + strconv.FormatUint(uint64(f), 16)
}
s = strings.TrimLeft(s, "|")
return s
}
// BitmessageNet represents which bitmessage network a message belongs to.
type BitmessageNet uint32
// Constants used to indicate the message bitmessage network. They can also be
// used to seek to the next message when a stream's state is unknown, but
// this package does not provide that functionality since it's generally a
// better idea to simply disconnect clients that are misbehaving over TCP.
const (
// MainNet represents the main bitmessage network.
MainNet BitmessageNet = 0xe9beb4d9
)
// bnStrings is a map of bitmessage networks back to their constant names for
// pretty printing.
var bnStrings = map[BitmessageNet]string{
MainNet: "MainNet",
}
// String returns the BitmessageNet in human-readable form.
func (n BitmessageNet) String() string {
if s, ok := bnStrings[n]; ok {
return s
}
return fmt.Sprintf("Unknown BitmessageNet (%d)", uint32(n))
}