forked from kubeshark/tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_unix_socket.go
147 lines (130 loc) · 3.13 KB
/
packet_unix_socket.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
package main
import (
"errors"
"net"
"sync"
"syscall"
"time"
"github.com/kubeshark/gopacket"
"github.com/kubeshark/tracerproto/pkg/unixpacket"
"github.com/rs/zerolog/log"
)
const (
maxUnixPacketClients = 10
)
type SocketPcapConnection struct {
packetCounter uint64
writeChannel chan []byte
packetSent uint64
packetDropped uint64
}
type SocketPcap struct {
packetCounter uint64
clientsConnected int
connections map[*net.UnixConn]*SocketPcapConnection
sync.Mutex
maxPktSize int
}
func (s *SocketPcapConnection) Run(conn *net.UnixConn, sock *SocketPcap) {
for {
buf := <-s.writeChannel
_, err := conn.Write(buf)
if err != nil {
if errors.Is(err, syscall.EPIPE) {
log.Info().Str("Address", conn.RemoteAddr().String()).Msg("Unix socket connection closed:")
} else {
log.Error().Err(err).Str("Address", conn.RemoteAddr().String()).Msg("Unix socket connection error:")
}
sock.Disconnected(conn)
return
}
}
}
func (s *SocketPcap) WritePacket(pkt gopacket.SerializeBuffer) error {
s.Lock()
defer s.Unlock()
defer func() {
s.packetCounter++
}()
if len(s.connections) == 0 {
return nil
}
hdrBytes, err := pkt.PrependBytes(unixpacket.PacketHeaderSize)
if err != nil {
return err
}
p := unixpacket.PacketUnixSocket(hdrBytes)
hdr := p.GetHeader()
hdr.Timestamp = uint64(time.Now().UnixNano())
// clear buffer at the end as soon as it is prepended with specific data
defer func() {
_ = pkt.Clear()
}()
buf := pkt.Bytes()
if len(buf) > s.maxPktSize {
s.maxPktSize = len(buf)
// temorary logging
log.Info().Int("len", s.maxPktSize).Msg("Max packet size:")
}
for _, conn := range s.connections {
copyBuf := make([]byte, len(buf))
copy(copyBuf, buf)
p = unixpacket.PacketUnixSocket(copyBuf)
hdr = p.GetHeader()
hdr.PacketCounter = conn.packetCounter
conn.packetCounter++
select {
case conn.writeChannel <- copyBuf:
conn.packetSent++
default:
conn.packetDropped++
}
}
return nil
}
func (s *SocketPcap) Connected(conn *net.UnixConn) {
ch := make(chan []byte, 256)
s.Lock()
defer s.Unlock()
c := &SocketPcapConnection{
writeChannel: ch,
}
s.connections[conn] = c
go c.Run(conn, s)
}
func (s *SocketPcap) Disconnected(conn *net.UnixConn) {
s.Lock()
defer s.Unlock()
delete(s.connections, conn)
}
func NewSocketPcap(unixSocketFileName string) *SocketPcap {
l, err := net.ListenUnix("unixpacket", &net.UnixAddr{Name: unixSocketFileName, Net: "unixpacket"})
if err != nil {
panic(err)
}
sock := SocketPcap{
connections: make(map[*net.UnixConn]*SocketPcapConnection),
}
go sock.acceptClients(l)
return &sock
}
func (c *SocketPcap) acceptClients(l *net.UnixListener) {
for {
conn, err := l.AcceptUnix()
if err != nil {
log.Error().Err(err).Msg("Accept unix socket failed:")
time.Sleep(time.Second)
continue
}
log.Info().Str("Address", conn.RemoteAddr().String()).Msg("Accepted unix socket:")
c.Lock()
if c.clientsConnected == maxUnixPacketClients {
log.Info().Str("Address", conn.RemoteAddr().String()).Msg("Unix socket max connections exceeded, closing:")
conn.Close()
c.Unlock()
continue
}
c.Unlock()
c.Connected(conn)
}
}