/
payload.go
61 lines (54 loc) · 1.59 KB
/
payload.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package testutils
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"github.com/google/gopacket"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/monitor"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
)
// CreateL3L4Payload assembles a L3/L4 payload for testing purposes
func CreateL3L4Payload(message interface{}, layers ...gopacket.SerializableLayer) ([]byte, error) {
buf := &bytes.Buffer{}
switch messageType := message.(type) {
case monitor.DebugCapture,
monitor.DropNotify,
monitor.PolicyVerdictNotify,
monitor.TraceNotify,
monitor.TraceNotifyV0,
monitor.TraceNotifyV1:
if err := binary.Write(buf, byteorder.Native, message); err != nil {
return nil, err
}
case monitorAPI.AgentNotify:
buf.WriteByte(byte(monitorAPI.MessageTypeAgent))
if err := gob.NewEncoder(buf).Encode(message); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported message type %T", messageType)
}
packet := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{
FixLengths: true,
}
if err := gopacket.SerializeLayers(packet, options, layers...); err != nil {
return nil, err
}
if _, err := buf.Write(packet.Bytes()); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MustCreateL3L4Payload wraps CreateL3L4Payload, but panics on error
func MustCreateL3L4Payload(message interface{}, layers ...gopacket.SerializableLayer) []byte {
payload, err := CreateL3L4Payload(message, layers...)
if err != nil {
panic(err)
}
return payload
}