Skip to content

Commit c134160

Browse files
scottfeldmandeadprogram
authored andcommitted
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.
1 parent 4bc93e2 commit c134160

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

netdev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type netdever interface {
9595
Bind(sockfd int, ip netip.AddrPort) error
9696
Connect(sockfd int, host string, ip netip.AddrPort) error
9797
Listen(sockfd int, backlog int) error
98-
Accept(sockfd int, ip netip.AddrPort) (int, error)
98+
Accept(sockfd int) (int, netip.AddrPort, error)
9999

100100
// # Flags argument on Send and Recv
101101
//

tcpsock.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
119119
return &TCPAddr{IP: ip.AsSlice(), Port: port}, nil
120120
}
121121

122+
// TCPAddrFromAddrPort returns addr as a TCPAddr. If addr.IsValid() is false,
123+
// then the returned TCPAddr will contain a nil IP field, indicating an
124+
// address family-agnostic unspecified address.
125+
func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr {
126+
return &TCPAddr{
127+
IP: addr.Addr().AsSlice(),
128+
Zone: addr.Addr().Zone(),
129+
Port: int(addr.Port()),
130+
}
131+
}
132+
122133
// TCPConn is an implementation of the Conn interface for TCP network
123134
// connections.
124135
type TCPConn struct {
@@ -277,7 +288,7 @@ type listener struct {
277288
}
278289

279290
func (l *listener) Accept() (Conn, error) {
280-
fd, err := netdev.Accept(l.fd, netip.AddrPort{})
291+
fd, raddr, err := netdev.Accept(l.fd)
281292
if err != nil {
282293
return nil, err
283294
}
@@ -286,6 +297,7 @@ func (l *listener) Accept() (Conn, error) {
286297
fd: fd,
287298
net: "tcp",
288299
laddr: l.laddr,
300+
raddr: TCPAddrFromAddrPort(raddr),
289301
}, nil
290302
}
291303

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

306-
lip, _ := netip.AddrFromSlice(laddr.IP)
307-
laddrport := netip.AddrPortFrom(lip, uint16(laddr.Port))
318+
laddrport := laddr.AddrPort()
308319
err = netdev.Bind(fd, laddrport)
309320
if err != nil {
310321
return nil, err

0 commit comments

Comments
 (0)