Skip to content

Commit ee6c5b4

Browse files
author
kix
committed
fix(control): improve error messages for UDP connection and packet send failures
1 parent 5740b94 commit ee6c5b4

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

control/anyfrom_pool.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
177177
anyfrom.RefreshTtl()
178178
return anyfrom, false, nil
179179
}
180-
180+
181181
// 使用更精确的双重检查锁定模式避免重复创建
182182
// 创建临时key用于创建锁
183183
createKey := lAddr + "_creating"
@@ -194,16 +194,16 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
194194
// 如果等待后仍未创建成功,返回错误而不是继续创建
195195
return nil, false, fmt.Errorf("timeout waiting for connection creation on %s", lAddr)
196196
}
197-
197+
198198
defer p.pool.Delete(createKey)
199-
199+
200200
// 再次检查是否已创建
201201
if af, ok := p.pool.Load(lAddr); ok {
202202
anyfrom := af.(*Anyfrom)
203203
anyfrom.RefreshTtl()
204204
return anyfrom, false, nil
205205
}
206-
206+
207207
// 创建新的Anyfrom
208208
d := net.ListenConfig{
209209
Control: func(network string, address string, c syscall.RawConn) error {
@@ -217,9 +217,9 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
217217
return nil
218218
})
219219
if err != nil {
220-
return nil, true, err
220+
return nil, true, fmt.Errorf("failed to create UDP connection for %s: %w", lAddr, err)
221221
}
222-
222+
223223
uConn := pc.(*net.UDPConn)
224224
af := &Anyfrom{
225225
UDPConn: uConn,
@@ -236,7 +236,7 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
236236
}
237237
})
238238
}
239-
239+
240240
p.pool.Store(lAddr, af)
241241
return af, true, nil
242242
}

control/udp.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func sendPkt(log *logrus.Logger, data []byte, from netip.AddrPort, realTo, to ne
6363
return err
6464
}
6565
_, err = uConn.WriteToUDPAddrPort(data, realTo)
66-
return err
66+
if err != nil {
67+
return fmt.Errorf("failed to write UDP packet from %s to %s: %w", from, realTo, err)
68+
}
69+
return nil
6770
}
6871

6972
func (c *ControlPlane) handlePkt(lConn *net.UDPConn, data []byte, src, pktDst, realDst netip.AddrPort, routingResult *bpfRoutingResult, skipSniffing bool) (err error) {

0 commit comments

Comments
 (0)