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.

Tested with examples/net/tcpecho on wioterminal and nano-rp2040.  Here's
a run with wioterminal:

SERVER
============
sfeldma@nuc:~/work/drivers$ tinygo flash -monitor -target wioterminal -size short -stack-size=8kb ./examples/net/tcpecho
code    data     bss |   flash     ram
110876    2552   11212 |  113428   13764
Connected to /dev/ttyACM2. Press Ctrl-C to exit.

Realtek rtl8720dn Wifi network device driver (rtl8720dn)

Driver version           : 0.0.1
RTL8720 firmware version : 2.1.2
MAC address              : 2c:f7:f1:1c:9b:2f

Connecting to Wifi SSID 'test'...CONNECTED

DHCP-assigned IP         : 10.0.0.140
DHCP-assigned subnet     : 255.255.255.0
DHCP-assigned gateway    : 10.0.0.1

Starting TCP server listening on :8080
Client 10.0.0.190:50000 connected
Client 10.0.0.190:50000 closed

CLIENT
=============
nc -p 50000 10.0.0.140 8080
This commit is contained in:
Scott Feldman
2023-12-18 00:41:14 -08:00
committed by Ron Evans
parent 1a96fc4547
commit 8642886f73
5 changed files with 52 additions and 16 deletions
+20 -6
View File
@@ -437,6 +437,12 @@ func ipToName(ip netip.AddrPort) []byte {
return name
}
func nameToIp(name []byte) netip.AddrPort {
port := uint16(name[2])<<8 | uint16(name[3])
addr, _ := netip.AddrFromSlice(name[4:8])
return netip.AddrPortFrom(addr, port)
}
func (r *rtl8720dn) Bind(sockfd int, ip netip.AddrPort) error {
if debugging(debugNetdev) {
@@ -534,10 +540,10 @@ func (r *rtl8720dn) Listen(sockfd int, backlog int) error {
return nil
}
func (r *rtl8720dn) Accept(sockfd int, ip netip.AddrPort) (int, error) {
func (r *rtl8720dn) Accept(sockfd int) (int, netip.AddrPort, error) {
if debugging(debugNetdev) {
fmt.Printf("[Accept] sockfd: %d, peer: %s\r\n", sockfd, ip)
fmt.Printf("[Accept] sockfd: %d\r\n", sockfd)
}
r.mu.Lock()
@@ -546,12 +552,12 @@ func (r *rtl8720dn) Accept(sockfd int, ip netip.AddrPort) (int, error) {
var newSock int32
var lsock = sock(sockfd)
var socket = r.sockets[lsock]
var name = ipToName(ip)
var name = ipToName(netip.AddrPort{})
switch socket.protocol {
case netdev.IPPROTO_TCP:
default:
return -1, netdev.ErrProtocolNotSupported
return -1, netip.AddrPort{}, netdev.ErrProtocolNotSupported
}
for {
@@ -570,6 +576,14 @@ func (r *rtl8720dn) Accept(sockfd int, ip netip.AddrPort) (int, error) {
continue
}
// Get remote peer ip:port
namelen = uint32(len(name))
result := r.rpc_lwip_getpeername(int32(newSock), name, &namelen)
if result == -1 {
return -1, netip.AddrPort{}, fmt.Errorf("Getpeername failed")
}
raddr := nameToIp(name)
// If we've already seen this socket, we can re-use
// the socket and return it. But, only if the socket
// is closed. If it's not closed, we'll just come back
@@ -582,12 +596,12 @@ func (r *rtl8720dn) Accept(sockfd int, ip netip.AddrPort) (int, error) {
continue
}
// Reuse client socket
return int(newSock), nil
return int(newSock), raddr, nil
}
// Create new socket for client and return fd
r.sockets[sock(newSock)] = newSocket(socket.protocol)
return int(newSock), nil
return int(newSock), raddr, nil
}
}