-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
837 lines (714 loc) · 22.5 KB
/
event.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 ebpfevents
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net/netip"
"os"
"strings"
"time"
"github.com/elastic/ebpfevents/pkg/endian"
"github.com/elastic/ebpfevents/pkg/varlen"
)
//go:generate stringer -linecomment=true -type=EventType,Transport,Family,FileType,FileChangeType -output=event_string.go
type Record struct {
Event *Event
Error error
}
type EventUnmarshaler interface {
Unmarshal(*bytes.Reader) error
}
type EventType uint64
const (
EventTypeProcessFork EventType = 1 << (iota + 1) // ProcessFork
EventTypeProcessExec // ProcessExec
EventTypeProcessExit // ProcessExit
EventTypeProcessSetsid // ProcessSetsid
EventTypeProcessSetuid // ProcessSetuid
EventTypeProcessSetgid // ProcessSetgid
EventTypeProcessTTYWrite // ProcessTTYWrite
EventTypeFileDelete // FileDelete
EventTypeFileCreate // FileCreate
EventTypeFileRename // FileRename
EventTypeFileModify // FileModify
EventTypeNetworkConnectionAccepted // NetConnectionAccepted
EventTypeNetworkConnectionAttempted // NetConnectionAttempted
EventTypeNetworkConnectionClosed // NetConnectionClosed
)
func (et EventType) MarshalJSON() ([]byte, error) {
return json.Marshal(et.String())
}
type Header struct {
NsSinceBoot uint64 `json:"ns_since_boot"`
Time time.Time `json:"time"`
Type EventType `json:"type"`
}
type Event struct {
Header `json:",inline"`
Body any `json:"body"`
}
type PidInfo struct {
StartTimeNs uint64 `json:"start_time_ns"`
Tid uint32 `json:"tid"`
Tgid uint32 `json:"tgid"`
Ppid uint32 `json:"ppid"`
Pgid uint32 `json:"pgid"`
Sid uint32 `json:"sid"`
}
type CredInfo struct {
Ruid uint32 `json:"ruid"`
Rgid uint32 `json:"rgid"`
Euid uint32 `json:"euid"`
Egid uint32 `json:"egid"`
Suid uint32 `json:"suid"`
Sgid uint32 `json:"sgid"`
CapPermitted uint64 `json:"cap_permitted"`
CapEffective uint64 `json:"cap_effective"`
}
type TTYWinsize struct {
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
}
type TTYTermios struct {
Iflag uint32 `json:"iflag"`
Oflag uint32 `json:"oflag"`
Lflag uint32 `json:"lflag"`
Cflag uint32 `json:"cflag"`
}
type TTYDev struct {
Minor uint16 `json:"minor"`
Major uint16 `json:"major"`
Winsize TTYWinsize `json:"winsize"`
Termios TTYTermios `json:"-"`
}
type ProcessFork struct {
ParentPids PidInfo `json:"parent_pids"`
ChildPids PidInfo `json:"child_pids"`
Creds CredInfo `json:"creds"`
CgroupPath string `json:"cgroup_path"`
}
const TaskCommLen = 16
func (e *ProcessFork) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.ParentPids); err != nil {
return fmt.Errorf("read parent pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.ChildPids); err != nil {
return fmt.Errorf("read child pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type ProcessExec struct {
Pids PidInfo `json:"pids"`
Creds CredInfo `json:"creds"`
CTTY TTYDev `json:"ctty"`
Cwd string `json:"cwd"`
Argv []string `json:"argv"`
Env map[string]string `json:"env"`
Filename string `json:"filename"`
CgroupPath string `json:"cgroup_path"`
}
func (e *ProcessExec) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
if err := binary.Read(r, endian.Native, &e.CTTY); err != nil {
return fmt.Errorf("read ctty: %v", err)
}
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.Cwd]; ok {
e.Cwd = val.(string)
}
if val, ok := vlMap[varlen.Argv]; ok {
e.Argv = val.([]string)
}
if val, ok := vlMap[varlen.Env]; ok {
e.Env = val.(map[string]string)
}
if val, ok := vlMap[varlen.Filename]; ok {
e.Filename = val.(string)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type ProcessExit struct {
Pids PidInfo `json:"pids"`
ExitCode int32 `json:"exit_code"`
CgroupPath string `json:"cgroup_path"`
}
func (e *ProcessExit) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.ExitCode); err != nil {
return fmt.Errorf("read exit code: %v", err)
}
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type ProcessSetsid struct {
Pids PidInfo `json:"pids"`
}
func (e *ProcessSetsid) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
return nil
}
type ProcessSetuid struct {
Pids PidInfo `json:"pids"`
NewRuid uint32 `json:"new_ruid"`
NewEuid uint32 `json:"new_euid"`
NewRgid uint32 `json:"new_rgid"`
NewEgid uint32 `json:"new_egid"`
}
func (e *ProcessSetuid) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewRuid); err != nil {
return fmt.Errorf("read new ruid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewEuid); err != nil {
return fmt.Errorf("read new euid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewRgid); err != nil {
return fmt.Errorf("read new rgid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewEgid); err != nil {
return fmt.Errorf("read new egid: %v", err)
}
return nil
}
type ProcessSetgid struct {
Pids PidInfo `json:"pids"`
NewRgid uint32 `json:"new_rgid"`
NewEgid uint32 `json:"new_egid"`
NewRuid uint32 `json:"new_ruid"`
NewEuid uint32 `json:"new_euid"`
}
func (e *ProcessSetgid) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewRgid); err != nil {
return fmt.Errorf("read new rgid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewEgid); err != nil {
return fmt.Errorf("read new egid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewRuid); err != nil {
return fmt.Errorf("read new ruid: %v", err)
}
if err := binary.Read(r, endian.Native, &e.NewEuid); err != nil {
return fmt.Errorf("read new euid: %v", err)
}
return nil
}
type ProcessTTYWrite struct {
Pids PidInfo `json:"pids"`
Truncated uint64 `json:"truncated"`
CTTY TTYDev `json:"ctty"`
TTY TTYDev `json:"tty"`
Comm string `json:"comm"`
Output string `json:"output"`
}
func (e *ProcessTTYWrite) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Truncated); err != nil {
return fmt.Errorf("read truncated: %v", err)
}
if err := binary.Read(r, endian.Native, &e.CTTY); err != nil {
return fmt.Errorf("read ctty: %v", err)
}
if err := binary.Read(r, endian.Native, &e.TTY); err != nil {
return fmt.Errorf("read tty: %v", err)
}
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.TTYOutput]; ok {
e.Output = val.(string)
}
return nil
}
type FileType uint32
const (
FileTypeUnknown FileType = iota // Unknown
FileTypeFile // File
FileTypeDir // Dir
FileTypeSymlink // Symlink
FileTypeCharDevice // CharDevice
FileTypeBlockDevice // BlockDevice
FileTypeNamedPipe // NamedPipe
FileTypeSocket // Socket
)
func (ft FileType) MarshalJSON() ([]byte, error) {
return json.Marshal(ft.String())
}
type FileInfo struct {
Type FileType `json:"type"`
Inode uint64 `json:"inode"`
Mode os.FileMode `json:"mode"`
Size uint64 `json:"size"`
Uid uint32 `json:"uid"`
Gid uint32 `json:"gid"`
Atime time.Time `json:"atime"`
Mtime time.Time `json:"mtime"`
Ctime time.Time `json:"ctime"`
}
type FileCreate struct {
Pids PidInfo `json:"pids"`
Creds CredInfo `json:"creds"`
Finfo FileInfo `json:"file_info"`
MountNs uint32 `json:"mount_ns"`
Comm string `json:"comm"`
Path string `json:"path"`
SymlinkTargetPath string `json:"symlink_target_path"`
CgroupPath string `json:"cgroup_path"`
}
func (e *FileCreate) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
fi, err := readFileInfo(r)
if err != nil {
return fmt.Errorf("read file info: %v", err)
}
e.Finfo = fi
if err := binary.Read(r, endian.Native, &e.MountNs); err != nil {
return fmt.Errorf("read mount namespace: %v", err)
}
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.Path]; ok {
e.Path = val.(string)
}
if val, ok := vlMap[varlen.SymlinkTargetPath]; ok {
e.SymlinkTargetPath = val.(string)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type FileDelete struct {
Pids PidInfo `json:"pids"`
Creds CredInfo `json:"creds"`
Finfo FileInfo `json:"file_info"`
MountNs uint32 `json:"mount_ns"`
Comm string `json:"comm"`
Path string `json:"path"`
SymlinkTargetPath string `json:"symlink_target_path"`
CgroupPath string `json:"cgroup_path"`
}
func (e *FileDelete) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
fi, err := readFileInfo(r)
if err != nil {
return fmt.Errorf("read file info: %v", err)
}
e.Finfo = fi
if err := binary.Read(r, endian.Native, &e.MountNs); err != nil {
return fmt.Errorf("read mount namespace: %v", err)
}
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.Path]; ok {
e.Path = val.(string)
}
if val, ok := vlMap[varlen.SymlinkTargetPath]; ok {
e.SymlinkTargetPath = val.(string)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type FileRename struct {
Pids PidInfo `json:"pids"`
Creds CredInfo `json:"creds"`
Finfo FileInfo `json:"file_info"`
MountNs uint32 `json:"mount_ns"`
Comm string `json:"comm"`
OldPath string `json:"old_path"`
NewPath string `json:"new_path"`
SymlinkTargetPath string `json:"symlink_target_path"`
CgroupPath string `json:"cgroup_path"`
}
func (e *FileRename) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
fi, err := readFileInfo(r)
if err != nil {
return fmt.Errorf("read file info: %v", err)
}
e.Finfo = fi
if err := binary.Read(r, endian.Native, &e.MountNs); err != nil {
return fmt.Errorf("read mount namespace: %v", err)
}
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.OldPath]; ok {
e.OldPath = val.(string)
}
if val, ok := vlMap[varlen.NewPath]; ok {
e.NewPath = val.(string)
}
if val, ok := vlMap[varlen.SymlinkTargetPath]; ok {
e.SymlinkTargetPath = val.(string)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type FileChangeType uint32
const (
FileChangeTypeUnknown FileChangeType = iota // Unknown
FileChangeTypeContent // Content
FileChangeTypePermissions // Permissions
FileChangeTypeOwner // Owner
FileChangeTypeXattrs // Xattrs
)
func (ft FileChangeType) MarshalJSON() ([]byte, error) {
return json.Marshal(ft.String())
}
type FileModify struct {
Pids PidInfo `json:"pids"`
Creds CredInfo `json:"creds"`
Finfo FileInfo `json:"file_info"`
ChangeType FileChangeType `json:"change_type"`
MountNs uint32 `json:"mount_ns"`
Comm string `json:"comm"`
Path string `json:"path"`
SymlinkTargetPath string `json:"symlink_target_path"`
CgroupPath string `json:"cgroup_path"`
}
func (e *FileModify) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
if err := binary.Read(r, endian.Native, &e.Creds); err != nil {
return fmt.Errorf("read creds: %v", err)
}
fi, err := readFileInfo(r)
if err != nil {
return fmt.Errorf("read file info: %v", err)
}
e.Finfo = fi
if err := binary.Read(r, endian.Native, &e.ChangeType); err != nil {
return fmt.Errorf("read change type: %v", err)
}
if err := binary.Read(r, endian.Native, &e.MountNs); err != nil {
return fmt.Errorf("read mount namespace: %v", err)
}
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
vlMap, err := varlen.DeserializeVarlenFields(r)
if err != nil {
return fmt.Errorf("deserialize varlen fields: %v", err)
}
if val, ok := vlMap[varlen.Path]; ok {
e.Path = val.(string)
}
if val, ok := vlMap[varlen.SymlinkTargetPath]; ok {
e.SymlinkTargetPath = val.(string)
}
if val, ok := vlMap[varlen.CgroupPath]; ok {
e.CgroupPath = val.(string)
}
return nil
}
type Transport uint32
const (
TransportTCP Transport = iota + 1 // TCP
)
func (t Transport) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
type Family uint32
const (
AFInet Family = iota + 1 // Inet
AFInet6 // Inet6
)
func (f Family) MarshalJSON() ([]byte, error) {
return json.Marshal(f.String())
}
type NetInfo struct {
Transport Transport `json:"transport"`
Family Family `json:"family"`
SourceAddress netip.Addr `json:"source_address"`
DestinationAddress netip.Addr `json:"destination_address"`
SourcePort uint16 `json:"source_port"`
DestinationPort uint16 `json:"destination_port"`
NetNs uint32 `json:"net_ns"`
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
}
type NetEvent struct {
Pids PidInfo `json:"pids"`
Net NetInfo `json:"net"`
Comm string `json:"comm"`
}
func (e *NetEvent) Unmarshal(r *bytes.Reader) error {
if err := binary.Read(r, endian.Native, &e.Pids); err != nil {
return fmt.Errorf("read pids: %v", err)
}
ni, err := readNetInfo(r)
if err != nil {
return fmt.Errorf("read net info: %v", err)
}
e.Net = ni
comm, err := readTaskComm(r)
if err != nil {
return err
}
e.Comm = comm
return nil
}
func NewEvent(raw []byte) (*Event, error) {
var (
err error
ev Event
r = bytes.NewReader(raw)
)
if err := readHeader(r, &ev); err != nil {
return nil, fmt.Errorf("read event header: %v", err)
}
switch ev.Header.Type {
case EventTypeProcessFork:
err = readBody(r, &ProcessFork{}, &ev)
case EventTypeProcessExec:
err = readBody(r, &ProcessExec{}, &ev)
case EventTypeProcessExit:
err = readBody(r, &ProcessExit{}, &ev)
case EventTypeProcessSetsid:
err = readBody(r, &ProcessSetsid{}, &ev)
case EventTypeProcessSetuid:
err = readBody(r, &ProcessSetuid{}, &ev)
case EventTypeProcessSetgid:
err = readBody(r, &ProcessSetgid{}, &ev)
case EventTypeProcessTTYWrite:
err = readBody(r, &ProcessTTYWrite{}, &ev)
case EventTypeFileDelete:
err = readBody(r, &FileDelete{}, &ev)
case EventTypeFileCreate:
err = readBody(r, &FileCreate{}, &ev)
case EventTypeFileModify:
err = readBody(r, &FileModify{}, &ev)
case EventTypeFileRename:
err = readBody(r, &FileRename{}, &ev)
case EventTypeNetworkConnectionAccepted, EventTypeNetworkConnectionAttempted, EventTypeNetworkConnectionClosed:
err = readBody(r, &NetEvent{}, &ev)
default:
return nil, fmt.Errorf("unknown event type %d", ev.Header.Type)
}
if err != nil {
return nil, fmt.Errorf("read event body (%s): %v", ev.Type.String(), err)
}
return &ev, nil
}
func readHeader(r *bytes.Reader, ev *Event) error {
var h Header
if err := binary.Read(r, endian.Native, &h.NsSinceBoot); err != nil {
return fmt.Errorf("read ns since boot: %v", err)
}
if err := binary.Read(r, endian.Native, &h.Type); err != nil {
return fmt.Errorf("read type: %v", err)
}
h.Time = time.Now()
ev.Header = h
return nil
}
func readBody(r *bytes.Reader, e EventUnmarshaler, ev *Event) error {
if err := e.Unmarshal(r); err != nil {
return fmt.Errorf("unmarshal: %v", err)
}
ev.Body = e
return nil
}
func readTaskComm(r *bytes.Reader) (string, error) {
var s strings.Builder
for i := 0; i < TaskCommLen; i++ {
c, err := r.ReadByte()
if err != nil {
return "", fmt.Errorf("read comm: %v", err)
}
if c == 0 {
continue
}
if err := s.WriteByte(c); err != nil {
return "", fmt.Errorf("write comm: %v", err)
}
}
return s.String(), nil
}
func readNetInfo(r *bytes.Reader) (NetInfo, error) {
var ni NetInfo
if err := binary.Read(r, endian.Native, &ni.Transport); err != nil {
return ni, fmt.Errorf("read transport: %v", err)
}
if err := binary.Read(r, endian.Native, &ni.Family); err != nil {
return ni, fmt.Errorf("read family: %v", err)
}
switch ni.Family {
case AFInet:
var tmp [4]byte
if err := binary.Read(r, endian.Native, &tmp); err != nil {
return ni, fmt.Errorf("read saddr: %v", err)
}
ni.SourceAddress = netip.AddrFrom4(tmp)
if err := binary.Read(r, endian.Native, &tmp); err != nil {
return ni, fmt.Errorf("read daddr: %v", err)
}
ni.DestinationAddress = netip.AddrFrom4(tmp)
case AFInet6:
var tmp [16]byte
if err := binary.Read(r, endian.Native, &tmp); err != nil {
return ni, fmt.Errorf("read saddr6: %v", err)
}
ni.SourceAddress = netip.AddrFrom16(tmp)
if err := binary.Read(r, endian.Native, &tmp); err != nil {
return ni, fmt.Errorf("read daddr6: %v", err)
}
ni.DestinationAddress = netip.AddrFrom16(tmp)
}
if err := binary.Read(r, endian.Native, &ni.SourcePort); err != nil {
return ni, fmt.Errorf("read sport: %v", err)
}
if err := binary.Read(r, endian.Native, &ni.DestinationPort); err != nil {
return ni, fmt.Errorf("read dport: %v", err)
}
if err := binary.Read(r, endian.Native, &ni.NetNs); err != nil {
return ni, fmt.Errorf("read net ns: %v", err)
}
if err := binary.Read(r, endian.Native, &ni.BytesSent); err != nil {
return ni, fmt.Errorf("read bytes sent: %v", err)
}
if err := binary.Read(r, endian.Native, &ni.BytesReceived); err != nil {
return ni, fmt.Errorf("read bytes received: %v", err)
}
return ni, nil
}
func readFileInfo(r *bytes.Reader) (FileInfo, error) {
var fi FileInfo
if err := binary.Read(r, endian.Native, &fi.Type); err != nil {
return fi, fmt.Errorf("read type: %v", err)
}
if err := binary.Read(r, endian.Native, &fi.Inode); err != nil {
return fi, fmt.Errorf("read inode: %v", err)
}
var m uint16
if err := binary.Read(r, endian.Native, &m); err != nil {
return fi, fmt.Errorf("read mode: %v", err)
}
fi.Mode = os.FileMode(m)
if err := binary.Read(r, endian.Native, &fi.Size); err != nil {
return fi, fmt.Errorf("read size: %v", err)
}
if err := binary.Read(r, endian.Native, &fi.Uid); err != nil {
return fi, fmt.Errorf("read uid: %v", err)
}
if err := binary.Read(r, endian.Native, &fi.Gid); err != nil {
return fi, fmt.Errorf("read gid: %v", err)
}
var ts uint64
if err := binary.Read(r, endian.Native, &ts); err != nil {
return fi, fmt.Errorf("read atime: %v", err)
}
fi.Atime = time.Unix(0, int64(ts))
if err := binary.Read(r, endian.Native, &ts); err != nil {
return fi, fmt.Errorf("read mtime: %v", err)
}
fi.Mtime = time.Unix(0, int64(ts))
if err := binary.Read(r, endian.Native, &ts); err != nil {
return fi, fmt.Errorf("read ctime: %v", err)
}
fi.Ctime = time.Unix(0, int64(ts))
return fi, nil
}