2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2022-02-22 16:48:59 +00:00
|
|
|
"errors"
|
2018-03-14 09:36:59 +00:00
|
|
|
"net"
|
2020-05-22 09:17:17 +00:00
|
|
|
"sync"
|
2018-03-14 09:36:59 +00:00
|
|
|
"time"
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TCPTransport allows network communication over TCP.
|
|
|
|
type TCPTransport struct {
|
2019-12-30 07:43:05 +00:00
|
|
|
log *zap.Logger
|
2018-03-14 09:36:59 +00:00
|
|
|
server *Server
|
|
|
|
listener net.Listener
|
|
|
|
bindAddr string
|
2022-11-29 14:43:08 +00:00
|
|
|
hostPort hostPort
|
2020-05-22 09:17:17 +00:00
|
|
|
lock sync.RWMutex
|
2021-07-07 17:59:01 +00:00
|
|
|
quit bool
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 14:43:08 +00:00
|
|
|
type hostPort struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewTCPTransport returns a new TCPTransport that will listen for
|
2018-03-14 09:36:59 +00:00
|
|
|
// new incoming peer connections.
|
2019-12-30 07:43:05 +00:00
|
|
|
func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport {
|
2022-11-29 14:43:08 +00:00
|
|
|
host, port, err := net.SplitHostPort(bindAddr)
|
|
|
|
if err != nil {
|
|
|
|
// Only host can be provided, it's OK.
|
|
|
|
host = bindAddr
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
return &TCPTransport{
|
2019-12-30 07:43:05 +00:00
|
|
|
log: log,
|
2018-03-14 09:36:59 +00:00
|
|
|
server: s,
|
|
|
|
bindAddr: bindAddr,
|
2022-11-29 14:43:08 +00:00
|
|
|
hostPort: hostPort{
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
},
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial implements the Transporter interface.
|
2022-11-17 14:07:19 +00:00
|
|
|
func (t *TCPTransport) Dial(addr string, timeout time.Duration) (AddressablePeer, error) {
|
2018-03-14 09:36:59 +00:00
|
|
|
conn, err := net.DialTimeout("tcp", addr, timeout)
|
|
|
|
if err != nil {
|
2022-11-17 14:07:19 +00:00
|
|
|
return nil, err
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2022-11-17 14:07:19 +00:00
|
|
|
p := NewTCPPeer(conn, addr, t.server)
|
2020-01-15 14:03:42 +00:00
|
|
|
go p.handleConn()
|
2022-11-17 14:07:19 +00:00
|
|
|
return p, nil
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Accept() {
|
|
|
|
l, err := net.Listen("tcp", t.bindAddr)
|
|
|
|
if err != nil {
|
2019-12-30 07:43:05 +00:00
|
|
|
t.log.Panic("TCP listen error", zap.Error(err))
|
2018-03-14 09:36:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:17:17 +00:00
|
|
|
t.lock.Lock()
|
2021-07-07 17:59:01 +00:00
|
|
|
if t.quit {
|
|
|
|
t.lock.Unlock()
|
|
|
|
l.Close()
|
|
|
|
return
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
t.listener = l
|
2022-11-29 14:43:08 +00:00
|
|
|
t.bindAddr = l.Addr().String()
|
|
|
|
t.hostPort.Host, t.hostPort.Port, _ = net.SplitHostPort(t.bindAddr) // no error expected as l.Addr() is a valid address.
|
2020-05-22 09:17:17 +00:00
|
|
|
t.lock.Unlock()
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2021-07-07 17:59:01 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
quit := t.quit
|
|
|
|
t.lock.Unlock()
|
2022-02-22 16:48:59 +00:00
|
|
|
if errors.Is(err, net.ErrClosed) && quit {
|
2018-03-23 20:36:59 +00:00
|
|
|
break
|
|
|
|
}
|
2022-11-29 14:43:08 +00:00
|
|
|
t.log.Warn("TCP accept error", zap.String("address", l.Addr().String()), zap.Error(err))
|
2018-03-14 09:36:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-17 14:07:19 +00:00
|
|
|
p := NewTCPPeer(conn, "", t.server)
|
2020-01-15 14:03:42 +00:00
|
|
|
go p.handleConn()
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Close() {
|
2020-09-02 10:21:24 +00:00
|
|
|
t.lock.Lock()
|
network: add a nil check in (*TCPTransport).Close, prevent panic
You have to try to trigger that, but in the event of server shutdown before it
actually started listening Close will easily panic like this:
2020-03-05T11:48:08.660+0300 INFO node started {"blockHeight": 6458, "headerHeight": 15605}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x9d5f0a]
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/network.(*TCPTransport).Close(0xc000fd7050)
/home/rik/dev/neo-go/pkg/network/tcp_transport.go:78 +0x2a
github.com/nspcc-dev/neo-go/pkg/network.(*Server).Shutdown(0xc000182280)
/home/rik/dev/neo-go/pkg/network/server.go:190 +0x189
github.com/nspcc-dev/neo-go/cli/server.startServer(0xc000200160, 0x0, 0x0)
/home/rik/dev/neo-go/cli/server/server.go:367 +0x79e
github.com/urfave/cli.HandleAction(0xb84860, 0xccf240, 0xc000200160, 0xc0001c4500, 0x0)
/home/rik/dev/neo-go/vendor/github.com/urfave/cli/app.go:490 +0xc8
github.com/urfave/cli.Command.Run(0xc9a160, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0xca407f, 0x10, 0x0, ...)
/home/rik/dev/neo-go/vendor/github.com/urfave/cli/command.go:210 +0x996
github.com/urfave/cli.(*App).Run(0xc0001d64e0, 0xc0000ca000, 0x3, 0x3, 0x0, 0x0)
/home/rik/dev/neo-go/vendor/github.com/urfave/cli/app.go:255 +0x6af
main.main()
/home/rik/dev/neo-go/cli/main.go:25 +0x505
2020-03-05 08:53:26 +00:00
|
|
|
if t.listener != nil {
|
|
|
|
t.listener.Close()
|
|
|
|
}
|
2021-07-07 17:59:01 +00:00
|
|
|
t.quit = true
|
2020-09-02 10:21:24 +00:00
|
|
|
t.lock.Unlock()
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proto implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Proto() string {
|
|
|
|
return "tcp"
|
|
|
|
}
|
2020-05-22 09:17:17 +00:00
|
|
|
|
2022-11-29 14:43:08 +00:00
|
|
|
// HostPort implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) HostPort() (string, string) {
|
2020-05-22 09:17:17 +00:00
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
2022-11-29 14:43:08 +00:00
|
|
|
|
|
|
|
return t.hostPort.Host, t.hostPort.Port
|
2020-05-22 09:17:17 +00:00
|
|
|
}
|