-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_monmap.go
164 lines (142 loc) · 4.3 KB
/
message_monmap.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
// message_monmap.go - define the monmap message data structure
package gorados
import (
"bytes"
"encoding/binary"
"fmt"
"log"
)
type monType struct {
Name string // encode format: size(4 bytes) + value
Tag uint8
Pad uint8
Type uint8
Addr interface{} // SockAddr for ipv4 and SockAddr6 for ipv6
}
type monmapType struct {
EncodingVersion uint8
MinCompatible uint8
Size uint32
FSID [16]byte
Epoch uint32
Mons []monType // encode format: length(4 bytes) + value
LastChanged Time
Created Time
}
// MessageMonmap is sent by the remote monitor to client to know about all monitors.
type MessageMonmap struct {
message
monmapType
}
func (m *MessageMonmap) Name() string { return "Monmap" }
func (m *MessageMonmap) Decode(input []byte) (int, error) {
m.Payload = input[:]
var decoded int
buf := bytes.NewReader(input)
// First 4 bytes is the size of the monmap.
var size uint32
if err := binary.Read(buf, binary.LittleEndian, &size); err != nil {
return decoded, err
}
if len(input) < int(4+size) {
return decoded, fmt.Errorf("invalid monmap message")
}
decoded += SIZE_BYTES
// Decode the monmap fields.
if err := binary.Read(buf, binary.LittleEndian, &m.EncodingVersion); err != nil {
return decoded, err
}
decoded += binary.Size(m.EncodingVersion)
if err := binary.Read(buf, binary.LittleEndian, &m.MinCompatible); err != nil {
return decoded, err
}
decoded += binary.Size(m.MinCompatible)
if err := binary.Read(buf, binary.LittleEndian, &m.Size); err != nil {
return decoded, err
}
decoded += binary.Size(m.Size)
if err := binary.Read(buf, binary.LittleEndian, &m.FSID); err != nil {
return decoded, err
}
decoded += binary.Size(m.FSID)
if err := binary.Read(buf, binary.LittleEndian, &m.Epoch); err != nil {
return decoded, err
}
decoded += binary.Size(m.Epoch)
// Mon array: size(4 bytes) + monType{1...n}
if err := binary.Read(buf, binary.LittleEndian, &size); err != nil {
return decoded, err
}
decoded += SIZE_BYTES
input = input[decoded:]
m.Mons = make([]monType, size)
for i := range m.Mons {
n, data, err := decodeSizeValue(input, 1)
if err != nil {
return decoded, err
}
m.Mons[i].Name = string(data)
decoded += SIZE_BYTES + n
input = input[SIZE_BYTES+n:]
buf = bytes.NewReader(input)
if err := binary.Read(buf, binary.LittleEndian, &m.Mons[i].Tag); err != nil {
return decoded, err
}
if err := binary.Read(buf, binary.LittleEndian, &m.Mons[i].Pad); err != nil {
return decoded, err
}
if err := binary.Read(buf, binary.LittleEndian, &m.Mons[i].Type); err != nil {
return decoded, err
}
n = binary.Size(m.Mons[i].Tag) + binary.Size(m.Mons[i].Pad) + binary.Size(m.Mons[i].Type)
decoded += n
input = input[n:]
n, data, err = decodeSizeValue(input, 1)
if err != nil {
return decoded, err
}
// Parse the public network address of the monitor.
var typ, nonce uint32
buf.Reset(data)
if err := binary.Read(buf, binary.LittleEndian, &typ); err != nil {
return decoded, err
}
if err := binary.Read(buf, binary.LittleEndian, &nonce); err != nil {
return decoded, err
}
data = data[binary.Size(typ)+binary.Size(nonce):]
addrLen, addr, err := decodeSizeValue(data, 1)
if addrLen == 16 { // 16 bytes sockaddr_in for ipv4
realAddr := &SockAddr{}
if err = realAddr.UnmarshalBinary(addr); err != nil {
log.Printf("parse public network address for monitor %s failed", m.Mons[i].Name)
m.Mons[i].Addr = addr
} else {
m.Mons[i].Addr = realAddr
}
} else if addrLen == 28 { // 28 bytes sockaddr_in6 for ipv6
realAddr := &SockAddr6{}
if err = realAddr.UnmarshalBinary(addr); err != nil {
log.Printf("parse public network address for monitor %s failed", m.Mons[i].Name)
m.Mons[i].Addr = addr
} else {
m.Mons[i].Addr = realAddr
}
} else {
log.Printf("parse public network address for monitor %s failed", m.Mons[i].Name)
m.Mons[i].Addr = addr
}
decoded += SIZE_BYTES + n
input = input[SIZE_BYTES+n:]
}
buf.Reset(input)
if err := binary.Read(buf, binary.LittleEndian, &m.LastChanged); err != nil {
return decoded, err
}
decoded += binary.Size(m.LastChanged)
if err := binary.Read(buf, binary.LittleEndian, &m.Created); err != nil {
return decoded, err
}
decoded += binary.Size(m.Created)
return len(m.Payload), nil
}