Skip to content

Commit

Permalink
correct netdever Accept() prototype
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
scottfeldman authored and deadprogram committed Dec 18, 2023
1 parent 4bc93e2 commit c134160
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion netdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type netdever interface {
Bind(sockfd int, ip netip.AddrPort) error
Connect(sockfd int, host string, ip netip.AddrPort) 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
//
Expand Down
17 changes: 14 additions & 3 deletions tcpsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
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
// connections.
type TCPConn struct {
Expand Down Expand Up @@ -277,7 +288,7 @@ type listener struct {
}

func (l *listener) Accept() (Conn, error) {
fd, err := netdev.Accept(l.fd, netip.AddrPort{})
fd, raddr, err := netdev.Accept(l.fd)
if err != nil {
return nil, err
}
Expand All @@ -286,6 +297,7 @@ func (l *listener) Accept() (Conn, error) {
fd: fd,
net: "tcp",
laddr: l.laddr,
raddr: TCPAddrFromAddrPort(raddr),
}, nil
}

Expand All @@ -303,8 +315,7 @@ func listenTCP(laddr *TCPAddr) (Listener, error) {
return nil, err
}

lip, _ := netip.AddrFromSlice(laddr.IP)
laddrport := netip.AddrPortFrom(lip, uint16(laddr.Port))
laddrport := laddr.AddrPort()
err = netdev.Bind(fd, laddrport)
if err != nil {
return nil, err
Expand Down

0 comments on commit c134160

Please sign in to comment.