mirror of
https://github.com/tinygo-org/net.git
synced 2026-08-21 12:59:00 +00:00
correct netdever Accept() prototype
According to man page accept(2), accept returns new client sockfd and remote peer ip:port. This patch corrects the Accept() prototype in the netdever interface to not take in an ip:port arg, but rather return an ip:port for remote peer.
This commit is contained in:
@@ -95,7 +95,7 @@ type netdever interface {
|
|||||||
Bind(sockfd int, ip netip.AddrPort) error
|
Bind(sockfd int, ip netip.AddrPort) error
|
||||||
Connect(sockfd int, host string, ip netip.AddrPort) error
|
Connect(sockfd int, host string, ip netip.AddrPort) error
|
||||||
Listen(sockfd int, backlog int) error
|
Listen(sockfd int, backlog int) error
|
||||||
Accept(sockfd int, ip netip.AddrPort) (int, error)
|
Accept(sockfd int) (int, netip.AddrPort, error)
|
||||||
|
|
||||||
// # Flags argument on Send and Recv
|
// # Flags argument on Send and Recv
|
||||||
//
|
//
|
||||||
|
|||||||
+14
-3
@@ -119,6 +119,17 @@ func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
|
|||||||
return &TCPAddr{IP: ip.AsSlice(), Port: port}, nil
|
return &TCPAddr{IP: ip.AsSlice(), Port: port}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TCPAddrFromAddrPort returns addr as a TCPAddr. If addr.IsValid() is false,
|
||||||
|
// then the returned TCPAddr will contain a nil IP field, indicating an
|
||||||
|
// address family-agnostic unspecified address.
|
||||||
|
func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr {
|
||||||
|
return &TCPAddr{
|
||||||
|
IP: addr.Addr().AsSlice(),
|
||||||
|
Zone: addr.Addr().Zone(),
|
||||||
|
Port: int(addr.Port()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TCPConn is an implementation of the Conn interface for TCP network
|
// TCPConn is an implementation of the Conn interface for TCP network
|
||||||
// connections.
|
// connections.
|
||||||
type TCPConn struct {
|
type TCPConn struct {
|
||||||
@@ -277,7 +288,7 @@ type listener struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *listener) Accept() (Conn, error) {
|
func (l *listener) Accept() (Conn, error) {
|
||||||
fd, err := netdev.Accept(l.fd, netip.AddrPort{})
|
fd, raddr, err := netdev.Accept(l.fd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -286,6 +297,7 @@ func (l *listener) Accept() (Conn, error) {
|
|||||||
fd: fd,
|
fd: fd,
|
||||||
net: "tcp",
|
net: "tcp",
|
||||||
laddr: l.laddr,
|
laddr: l.laddr,
|
||||||
|
raddr: TCPAddrFromAddrPort(raddr),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,8 +315,7 @@ func listenTCP(laddr *TCPAddr) (Listener, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lip, _ := netip.AddrFromSlice(laddr.IP)
|
laddrport := laddr.AddrPort()
|
||||||
laddrport := netip.AddrPortFrom(lip, uint16(laddr.Port))
|
|
||||||
err = netdev.Bind(fd, laddrport)
|
err = netdev.Bind(fd, laddrport)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user