forked from skiptomyliu/capsule8
/
chargen.go
111 lines (97 loc) · 2.76 KB
/
chargen.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
// Copyright 2017 Capsule8, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sensor
import (
"fmt"
"github.com/Happyholic1203/capsule8/pkg/expression"
)
// ChargenEventTypes defines the field types that can be used with filters on
// chargen telemetry events.
var ChargenEventTypes = expression.FieldTypeMap{
"index": expression.ValueTypeUnsignedInt64,
"characters": expression.ValueTypeString,
}
// ChargenTelemetryEvent is a telemetry event generated by the chargen event
// source.
type ChargenTelemetryEvent struct {
TelemetryEventData
Index uint64
Characters string
}
// CommonTelemetryEventData returns the telemtry event data common to all
// telemetry events for a chargen telemetry event.
func (e ChargenTelemetryEvent) CommonTelemetryEventData() TelemetryEventData {
return e.TelemetryEventData
}
func (s *Subscription) dispatchChargenEvent(
eventid uint64,
data expression.FieldValueMap,
) {
var e ChargenTelemetryEvent
e.Init(s.sensor)
e.Index = data["index"].(uint64)
e.Characters = data["characters"].(string)
s.DispatchEvent(eventid, e, data)
}
func generateCharacters(start, length uint64) string {
bytes := make([]byte, length)
for i := uint64(0); i < length; i++ {
bytes[i] = ' ' + byte((start+i)%95)
}
return string(bytes)
}
// RegisterChargenEventFilter registers a character generation event filter
// with a subscription.
func (s *Subscription) RegisterChargenEventFilter(
length uint64,
filter *expression.Expression,
) {
if length == 0 || length > 1<<16 {
s.logStatus(
fmt.Sprintf("Chargen length out of range (%d)", length))
return
}
monitor := s.sensor.Monitor()
eventID := monitor.ReserveEventID()
done := make(chan struct{})
es, err := s.addEventSink(eventID, filter)
if err != nil {
s.logStatus(
fmt.Sprintf("Invalid filter expression for chargen filter: %v", err))
return
}
es.unregister = func(es *eventSink) {
close(done)
}
es.enable = func() {
go func() {
index := uint64(0)
for {
select {
case <-done:
return
default:
characters := generateCharacters(index,
length)
data := expression.FieldValueMap{
"index": index,
"characters": characters,
}
index += length
s.dispatchChargenEvent(eventID, data)
}
}
}()
}
}