Skip to content

Commit a945ad8

Browse files
committed
core/connmap: m debug logs
1 parent 5849f3d commit a945ad8

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

intra/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func undoAlg(r dnsx.Resolver, algip netip.Addr) (realips, domains, probableDomai
195195

196196
func hasActiveConn(cm core.ConnMapper, ipp, ips, port string) bool {
197197
if cm == nil {
198+
log.W("intra: hasActiveConn: unexpected nil cm")
198199
return false
199200
}
200201
// TODO: filter by protocol (tcp/udp) when finding conns
@@ -242,9 +243,11 @@ func hasSelfUid(t []core.ConnTuple, d bool) bool {
242243
}
243244
for _, x := range t {
244245
if x.UID == protect.UidSelf {
246+
log.D("intra: hasSelfUid(%v): true", x)
245247
return true
246248
}
247249
}
250+
log.V("intra: hasSelfUid(%d): false; %v", len(t), t)
248251
return false // regardless of d
249252
}
250253

intra/core/connmap.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import (
1010
"net"
1111
"strings"
1212
"sync"
13+
14+
"github.com/celzero/firestack/intra/log"
1315
)
1416

1517
type ConnTuple struct {
1618
CID string // conn id
1719
UID string // proc id
1820
}
1921

22+
func (t ConnTuple) String() string {
23+
return t.CID + ":" + t.UID
24+
}
25+
2026
type ConnMapper interface {
2127
Clear() []string
2228
Track(t ConnTuple, x ...net.Conn) int
@@ -57,10 +63,11 @@ func (h *cm) Track(t ConnTuple, conns ...net.Conn) (n int) {
5763
}
5864
h.trackDstLocked(t, conns)
5965

66+
log.D("connmap: track: %d conns for %s", n, cid)
6067
return
6168
}
6269

63-
func (h *cm) trackDstLocked(t ConnTuple, conns []net.Conn) {
70+
func (h *cm) trackDstLocked(t ConnTuple, conns []net.Conn) (n int) {
6471
for _, c := range conns {
6572
if c == nil {
6673
continue
@@ -71,13 +78,18 @@ func (h *cm) trackDstLocked(t ConnTuple, conns []net.Conn) {
7178
continue
7279
}
7380
dst := raddr.String()
81+
log.V("connmap: track: %s -> %s", t.CID, dst)
7482
if tups, ok := h.dsttracker[dst]; ok {
7583
// TODO: do not add dup tuples (cid)
7684
h.dsttracker[dst] = append(tups, t)
85+
n += len(tups) + 1
7786
} else {
7887
h.dsttracker[dst] = []ConnTuple{t}
88+
n += 1
7989
}
8090
}
91+
log.D("connmap: track: %d dst for %s", n, t.CID)
92+
return
8193
}
8294

8395
func (h *cm) Untrack(cid string) (n int) {
@@ -86,34 +98,42 @@ func (h *cm) Untrack(cid string) (n int) {
8698

8799
for _, c := range h.conntracker[cid] {
88100
if c != nil {
89-
n++
90101
h.untrackDstLocked(cid, c)
91102
go c.Close()
103+
n += 1
92104
}
93105
}
94106
delete(h.conntracker, cid)
107+
log.D("connmap: untrack: %d conns for %s", n, cid)
95108
return
96109
}
97110

98-
func (h *cm) untrackDstLocked(cid string, c net.Conn) {
111+
func (h *cm) untrackDstLocked(cid string, c net.Conn) (ok bool) {
99112
raddr := c.RemoteAddr()
100-
if raddr == nil {
113+
if raddr == nil { // should not happen?
114+
log.W("connmap: untrack: no remote addr for %s", cid)
101115
return
102116
}
103117
dst := raddr.String()
104118
if tups, ok := h.dsttracker[dst]; ok {
105119
for i, t := range tups {
106120
if t.CID == cid {
121+
log.D("connmap: untrack: dst %s -> %s", cid, dst)
107122
// ids[i+1:] does not panic if i+1 is out of range
108123
// go.dev/play/p/troeQ5djf9h
109124
h.dsttracker[dst] = append(tups[:i], tups[i+1:]...)
125+
ok = true
110126
break
111127
}
112128
}
113-
if len(h.dsttracker[dst]) == 0 {
129+
if len(tups) == 0 {
114130
delete(h.dsttracker, dst)
115131
}
132+
log.D("connmap: untrack: %d dst for %s; ok? %t", len(tups), cid, ok)
133+
} else {
134+
log.D("connmap: untrack: no dst for %s", cid)
116135
}
136+
return
117137
}
118138

119139
func (h *cm) UntrackBatch(cids []string) (out []string) {
@@ -131,6 +151,7 @@ func (h *cm) UntrackBatch(cids []string) (out []string) {
131151
delete(h.conntracker, id)
132152
out = append(out, id)
133153
}
154+
log.D("connmap: untrack: batch %d conns", len(out))
134155
return
135156
}
136157

@@ -146,23 +167,23 @@ func (h *cm) Get(cid string) (conns []net.Conn) {
146167

147168
func (h *cm) Find(dst string) (tups []ConnTuple) {
148169
if len(dst) == 0 {
170+
log.D("connmap: find: empty dst")
149171
return
150172
}
151173

152174
h.RLock()
153175
defer h.RUnlock()
154-
155176
// TODO: handle unconnected udp sockets
156-
if tups, ok := h.dsttracker[dst]; ok {
157-
return tups
158-
}
177+
tups = h.dsttracker[dst]
178+
log.V("connmap: find: %d tuples for %s", len(tups), dst)
159179
return
160180
}
161181

162182
func (h *cm) FindAll(csvips, port string) (out []ConnTuple) {
163183
out = make([]ConnTuple, 0)
164184

165185
if len(csvips) == 0 {
186+
log.D("connmap: findAll: empty csvips")
166187
return
167188
}
168189

@@ -176,6 +197,7 @@ func (h *cm) FindAll(csvips, port string) (out []ConnTuple) {
176197
out = append(out, tups...)
177198
}
178199
}
200+
log.V("connmap: findAll: %d tuples for %s", len(out), csvips)
179201
return
180202
}
181203

@@ -194,5 +216,6 @@ func (h *cm) Clear() (cids []string) {
194216
}
195217
clear(h.conntracker)
196218
clear(h.dsttracker)
219+
log.D("connmap: clear: %d conns", len(cids))
197220
return
198221
}

0 commit comments

Comments
 (0)