forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
252 lines (203 loc) · 5.22 KB
/
rpc.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Package rpc provides support for parsing RPC messages and reporting the
// results. This package supports the RPC v2 protocol as defined by RFC 5531
// (RFC 1831).
package nfs
import (
"encoding/binary"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"fmt"
"github.com/elastic/beats/packetbeat/protos"
"github.com/elastic/beats/packetbeat/protos/tcp"
"github.com/elastic/beats/packetbeat/publish"
)
var debugf = logp.MakeDebug("rpc")
const (
rpcLastFrag = 0x80000000
rpcSizeMask = 0x7fffffff
)
const (
rpcCall = 0
rpcReply = 1
)
type rpcStream struct {
tcpTuple *common.TCPTuple
rawData []byte
}
type rpcConnectionData struct {
streams [2]*rpcStream
}
type rpc struct {
// Configuration data.
ports []int
callsSeen *common.Cache
transactionTimeout time.Duration
results publish.Transactions // Channel where results are pushed.
}
func init() {
protos.Register("nfs", New)
}
func New(
testMode bool,
results publish.Transactions,
cfg *common.Config,
) (protos.Plugin, error) {
p := &rpc{}
config := defaultConfig
if !testMode {
if err := cfg.Unpack(&config); err != nil {
logp.Warn("failed to read config")
return nil, err
}
}
if err := p.init(results, &config); err != nil {
logp.Warn("failed to init")
return nil, err
}
return p, nil
}
func (r *rpc) init(results publish.Transactions, config *rpcConfig) error {
r.setFromConfig(config)
r.results = results
r.callsSeen = common.NewCacheWithRemovalListener(
r.transactionTimeout,
protos.DefaultTransactionHashSize,
func(k common.Key, v common.Value) {
nfs, ok := v.(*nfs)
if !ok {
logp.Err("Expired value is not a MapStr (%T).", v)
return
}
r.handleExpiredPacket(nfs)
})
r.callsSeen.StartJanitor(r.transactionTimeout)
return nil
}
func (r *rpc) setFromConfig(config *rpcConfig) error {
r.ports = config.Ports
r.transactionTimeout = config.TransactionTimeout
return nil
}
func (r *rpc) GetPorts() []int {
return r.ports
}
// Called when TCP payload data is available for parsing.
func (r *rpc) Parse(
pkt *protos.Packet,
tcptuple *common.TCPTuple,
dir uint8,
private protos.ProtocolData,
) protos.ProtocolData {
defer logp.Recover("ParseRPC exception")
conn := ensureRPCConnection(private)
conn = r.handleRPCFragment(conn, pkt, tcptuple, dir)
if conn == nil {
return nil
}
return conn
}
// Called when the FIN flag is seen in the TCP stream.
func (r *rpc) ReceivedFin(tcptuple *common.TCPTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
defer logp.Recover("ReceivedFinRpc exception")
// forced by TCP interface
return private
}
// Called when a packets are missing from the tcp
// stream.
func (r *rpc) GapInStream(tcptuple *common.TCPTuple, dir uint8,
nbytes int, private protos.ProtocolData) (priv protos.ProtocolData, drop bool) {
defer logp.Recover("GapInRpcStream exception")
// forced by TCP interface
return private, false
}
// ConnectionTimeout returns the per stream connection timeout.
// Return <=0 to set default tcp module transaction timeout.
func (r *rpc) ConnectionTimeout() time.Duration {
// forced by TCP interface
return r.transactionTimeout
}
func ensureRPCConnection(private protos.ProtocolData) *rpcConnectionData {
conn := getRPCConnection(private)
if conn == nil {
conn = &rpcConnectionData{}
}
return conn
}
func getRPCConnection(private protos.ProtocolData) *rpcConnectionData {
if private == nil {
return nil
}
priv, ok := private.(*rpcConnectionData)
if !ok {
logp.Warn("rpc connection data type error")
return nil
}
if priv == nil {
logp.Warn("Unexpected: rpc connection data not set")
return nil
}
return priv
}
// Parse function is used to process TCP payloads.
func (r *rpc) handleRPCFragment(
conn *rpcConnectionData,
pkt *protos.Packet,
tcptuple *common.TCPTuple,
dir uint8,
) *rpcConnectionData {
st := conn.streams[dir]
if st == nil {
st = newStream(pkt, tcptuple)
conn.streams[dir] = st
} else {
// concatenate bytes
st.rawData = append(st.rawData, pkt.Payload...)
if len(st.rawData) > tcp.TCPMaxDataInStream {
debugf("Stream data too large, dropping TCP stream")
conn.streams[dir] = nil
return conn
}
}
for len(st.rawData) > 0 {
if len(st.rawData) < 4 {
debugf("Wainting for more data")
break
}
marker := uint32(binary.BigEndian.Uint32(st.rawData[0:4]))
size := int(marker & rpcSizeMask)
islast := (marker & rpcLastFrag) != 0
if len(st.rawData)-4 < size {
debugf("Wainting for more data")
break
}
if !islast {
logp.Warn("multifragment rpc message")
break
}
xdr := newXDR(st.rawData[4 : 4+size])
// keep the rest of the next fragment
st.rawData = st.rawData[4+size:]
r.handleRPCPacket(xdr, pkt.Ts, tcptuple, dir)
}
return conn
}
func (r *rpc) handleRPCPacket(xdr *xdr, ts time.Time, tcptuple *common.TCPTuple, dir uint8) {
xid := fmt.Sprintf("%.8x", xdr.getUInt())
msgType := xdr.getUInt()
switch msgType {
case rpcCall:
r.handleCall(xid, xdr, ts, tcptuple, dir)
case rpcReply:
r.handleReply(xid, xdr, ts, tcptuple, dir)
default:
logp.Warn("Bad RPC message")
}
}
func newStream(pkt *protos.Packet, tcptuple *common.TCPTuple) *rpcStream {
return &rpcStream{
tcpTuple: tcptuple,
rawData: pkt.Payload,
}
}