From 22c7e6c9d80b39ef21d24eb41cf7c40f6b6cb278 Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Wed, 1 Apr 2026 00:04:08 +0200 Subject: [PATCH] fix: StackBerkeley Bind/Listen/Connect and StackGo dial support (#64) * fix: StackBerkeley Bind/Listen/Connect and StackGo dial support - Bind now stores the bound address; Listen and Connect forward it to the gostack so the local port reaches SocketNetip. - Fix StackGo.Socket parsing laddr/raddr instead of zero-value locals. - Fix inverted isvalid() checks in StackBerkeley.Close causing nil deref. - Auto-assign ephemeral port for outbound dial connections in SocketNetip. - Configure tcp.Conn with TX/RX buffers before DialTCP; add DialConfig fields to StackGoConfig. Signed-off-by: deadprogram * use already existing poolconfig for new tcp config --------- Signed-off-by: deadprogram Co-authored-by: Patricio Whittingslow --- x/xnet/stack-go.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/x/xnet/stack-go.go b/x/xnet/stack-go.go index 49d5efd..e78e16e 100644 --- a/x/xnet/stack-go.go +++ b/x/xnet/stack-go.go @@ -25,10 +25,11 @@ func (s *StackAsync) StackGo(loopSleep time.Duration, cfg StackGoConfig) StackGo } func (s StackBlocking) StackGo(cfg StackGoConfig) StackGo { - return StackGo{ + sg := StackGo{ blk: s, plcfg: cfg.ListenerPoolConfig, } + return sg } type StackGo struct { @@ -65,8 +66,14 @@ func (s StackGo) SocketNetip(ctx context.Context, network string, family, sotype return nil, lneto.ErrUnsupported } if laddr.Port() == 0 { - return nil, lneto.ErrZeroSource - } else if laddr.Addr() == netip.IPv4Unspecified() { + if raddr.IsValid() && raddr.Addr() != netip.IPv4Unspecified() { + // Outbound (dial) connection: auto-assign ephemeral port. + laddr = netip.AddrPortFrom(laddr.Addr(), uint16(49152+s.blk.async.Prand32()%16384)) + } else { + return nil, lneto.ErrZeroSource + } + } + if laddr.Addr() == netip.IPv4Unspecified() { // Specify address. laddr = netip.AddrPortFrom(s.blk.async.ip.Addr(), laddr.Port()) } else if laddr.Addr().Is6() { @@ -83,6 +90,14 @@ func (s StackGo) SocketNetip(ctx context.Context, network string, family, sotype if raddr.IsValid() && raddr.Addr() != netip.IPv4Unspecified() { var conn tcp.Conn // DIAL TCP: active connection a.k.a TCP Client branch. + err = conn.Configure(tcp.ConnConfig{ + TxBuf: make([]byte, s.plcfg.TxBufSize), + RxBuf: make([]byte, s.plcfg.RxBufSize), + TxPacketQueueSize: s.plcfg.QueueSize, + }) + if err != nil { + return nil, err + } err = s.blk.async.DialTCP(&conn, laddr.Port(), raddr) if err != nil { return nil, err