mirror of
https://github.com/soypat/lneto.git
synced 2026-08-27 09:59:04 +00:00
do not crash if no tcp connections are desired
This commit is contained in:
@@ -42,7 +42,7 @@ type node struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errZeroMaxNodes = errors.New("zero max ports")
|
errZeroMaxNodesArg = errors.New("zero max nodes arg")
|
||||||
errZeroPort = errors.New("port must be greater than zero")
|
errZeroPort = errors.New("port must be greater than zero")
|
||||||
errInvalidProto = errors.New("invalid protocol")
|
errInvalidProto = errors.New("invalid protocol")
|
||||||
errProtoRegistered = errors.New("protocol already registered")
|
errProtoRegistered = errors.New("protocol already registered")
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func (ls *StackEthernet) Reset6(mac, gateway [6]byte, mtu, maxNodes int) error {
|
|||||||
if mtu > math.MaxUint16 || mtu < 256 {
|
if mtu > math.MaxUint16 || mtu < 256 {
|
||||||
return errors.New("invalid MTU")
|
return errors.New("invalid MTU")
|
||||||
} else if maxNodes <= 0 {
|
} else if maxNodes <= 0 {
|
||||||
return errZeroMaxNodes
|
return errZeroMaxNodesArg
|
||||||
}
|
}
|
||||||
ls.handlers = slices.Grow(ls.handlers[:0], maxNodes)
|
ls.handlers = slices.Grow(ls.handlers[:0], maxNodes)
|
||||||
*ls = StackEthernet{
|
*ls = StackEthernet{
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ type StackIP struct {
|
|||||||
|
|
||||||
func (sb *StackIP) Reset(addr netip.Addr, maxNodes int) error {
|
func (sb *StackIP) Reset(addr netip.Addr, maxNodes int) error {
|
||||||
if maxNodes <= 0 {
|
if maxNodes <= 0 {
|
||||||
return errZeroMaxNodes
|
return errZeroMaxNodesArg
|
||||||
}
|
}
|
||||||
err := sb.SetAddr(addr)
|
err := sb.SetAddr(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func (ps *StackPorts) Reset(protocol uint64, dstPortOffset uint16, maxNodes int)
|
|||||||
if protocol > math.MaxUint16 {
|
if protocol > math.MaxUint16 {
|
||||||
return errInvalidProto
|
return errInvalidProto
|
||||||
} else if maxNodes <= 0 {
|
} else if maxNodes <= 0 {
|
||||||
return errZeroMaxNodes
|
return errZeroMaxNodesArg
|
||||||
}
|
}
|
||||||
ps.handlers = slices.Grow(ps.handlers[:0], maxNodes)
|
ps.handlers = slices.Grow(ps.handlers[:0], maxNodes)
|
||||||
*ps = StackPorts{
|
*ps = StackPorts{
|
||||||
|
|||||||
@@ -123,14 +123,15 @@ func (s *StackAsync) Reset(cfg StackConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Enable TCP if connections present.
|
// Enable TCP if connections present.
|
||||||
|
if cfg.MaxTCPConns > 0 {
|
||||||
err = s.tcps.ResetTCP(cfg.MaxTCPConns)
|
err = s.tcps.ResetTCP(cfg.MaxTCPConns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = s.ip.Register(&s.tcps)
|
err = s.ip.Register(&s.tcps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now setup stacks.
|
// Now setup stacks.
|
||||||
|
|||||||
@@ -85,13 +85,19 @@ func (s StackRetrying) DoResolveHardwareAddress6(addr netip.Addr, timeout time.D
|
|||||||
|
|
||||||
func (s StackRetrying) DoDialTCP(conn *tcp.Conn, localPort uint16, addrp netip.AddrPort, timeout time.Duration, retries int) (err error) {
|
func (s StackRetrying) DoDialTCP(conn *tcp.Conn, localPort uint16, addrp netip.AddrPort, timeout time.Duration, retries int) (err error) {
|
||||||
expectEnd := time.Now().Add(timeout * time.Duration(retries))
|
expectEnd := time.Now().Add(timeout * time.Duration(retries))
|
||||||
|
var firstErr error
|
||||||
for i := 0; i < retries; i++ {
|
for i := 0; i < retries; i++ {
|
||||||
err = s.block.DoDialTCP(conn, localPort, addrp, timeout)
|
err = s.block.DoDialTCP(conn, localPort, addrp, timeout)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
|
} else if firstErr == nil {
|
||||||
|
firstErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if time.Now().Before(expectEnd) {
|
if time.Now().Before(expectEnd) {
|
||||||
|
if err != firstErr {
|
||||||
|
return errors.Join(firstErr, err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return errRetriesExceeded
|
return errRetriesExceeded
|
||||||
|
|||||||
Reference in New Issue
Block a user