casaroli opened a new pull request, #22429:
URL: https://github.com/apache/nuttx/pull/22429

   ## Summary
   
   This PR makes the errors of TCP `connect()` agree with POSIX and Linux. Each 
commit is one case:
   
   - Commit 1: `inet_connect()` called `DEBUGPANIC()` for an address of an 
unknown family (for example `AF_UNIX` on an `AF_INET` socket). It now only 
returns `EAFNOSUPPORT`, as it already did without assertions.
   - Commit 2: a second `connect()` while the handshake of the first one goes 
on (a non-blocking socket in `SYN_SENT`) returned `EISCONN`. It now returns 
`EALREADY`, and the first connection goes on. A `connect()` of a listening 
socket started a connection from the listener; it now returns `EISCONN`.
   - Commit 3: a signal that interrupted a blocking `connect()` gave `EINTR`, 
but also tore the connection down without a monitor: the socket never became 
connected, and the next `connect()` gave `EISCONN`. POSIX: "the connection 
request shall not be aborted, and the connection shall be established 
asynchronously". Now the connection goes on, as after the `SO_SNDTIMEO` 
timeout: `poll()` gives `POLLOUT` when it completes, and a second `connect()` 
gives `EALREADY`.
   - Commit 4: a RST after the FIN of the peer (`CLOSE_WAIT`, `LAST_ACK`) left 
`SO_ERROR` at 0, or set it to `ECONNREFUSED` when `poll()` saw it. It now gives 
the error of Linux (`tcp_reset()`): `EPIPE` in `CLOSE_WAIT`, as a send after 
the FIN of the peer already does, and `ECONNRESET` in `LAST_ACK`. 
`ECONNREFUSED` stays for a refused connection. A receive and a send do not 
change.
   - Commit 5: the documentation of `connect()` and of a reset by the peer.
   
   This also does what the body of #22144 named as a follow-up ("Fix 4, not 
changed here"): `EALREADY` for the second `connect()`.
   
   ## Impact
   
   Return values of `connect()` and the `SO_ERROR` value in the cases above. A 
program that called `connect()` again after `EINPROGRESS` gets `EALREADY` 
instead of `EISCONN` while the handshake goes on (`EISCONN` once it is 
connected). A program that calls `connect()` again after `EINTR` also gets 
`EALREADY` while the handshake goes on (as on Linux); in the tree only 
`netutils/webclient` did this, and apache/nuttx-apps#MMMM fixes webclient for 
this (it waits with `poll()`; the fix is also right without this PR). No API, 
ABI or Kconfig change. Common network code: the same in flat, protected and 
kernel builds and on all architectures.
   
   ## Testing
   
   New cases (apache/nuttx-apps#NNNN): `test_tcp_connect_listener`, 
`test_tcp_connect_family`, `test_tcp_reset_closewait` in `cmocka_net_tcp`; 
`test_tun_tcp_connect_again`, `test_tun_tcp_connect_intr` in `cmocka_net_tun` 
(the program plays a peer that answers the SYN late).
   
   Each boot ran `cmocka_net_local`, `cmocka_net_tun` and `cmocka_net_tcp`, 
with the cases of this PR and of #NNNNN (the `accept()` PR) in the apps tree 
(CI image, gcc 12.3, 64 devif callbacks):
   
   | Config | master 453cafdc4dd | this PR (flat and x86_64: with #NNNNN too) |
   |---|---|---|
   | qemu-armv8a:knsh (kernel), QEMU TCG, 3 boots | `test_tcp_connect_listener` 
FAIL (`-111 != -106`: the listener starts a connection, `ECONNREFUSED`), 
`test_tun_tcp_connect_again` and `_intr` FAIL (`106 != 114`: `EISCONN`, not 
`EALREADY`), `test_tcp_reset_closewait` FAIL (`0 != 32`: `SO_ERROR` 0), 
`test_tcp_connect_family` stops the system (assertion at `inet_sockif.c:1293`), 
in 3 of 3 | all 5 pass in 3 of 3; `ostest` passes |
   | qemu-armv8a:nsh (flat), TCG, 1 boot | the same 4 FAIL, the same assertion 
| all 5 pass |
   | qemu-intel64:knsh_romfs (kernel, CMake), KVM, 1 boot | the same 4 FAIL, 
the same assertion | all 5 pass |
   
   The other cases give the same result with and without this PR. 
`test_tcp_close_timewait` fails on both (fixed by apache/nuttx-apps#4143). 
`test_tcp_reset_send` fails on both in the flat build and on x86_64 (`SIGPIPE` 
goes to another thread).
   
   After the rebase on master 82a1289a9e6 (commit 4 now gives `ECONNRESET` in 
`LAST_ACK`): qemu-armv8a:knsh TCG, 1 boot each: on master 
`test_tcp_reset_closewait` FAIL (`0 != 32`) and the other 4 cases as above; 
with this PR all of `cmocka_net_local`, `cmocka_net_tun` and `cmocka_net_tcp` 
pass (41 of 41). The `LAST_ACK` case is checked by reading only: over the 
loopback device the ACK of the FIN comes at once, so no RST can come in 
`LAST_ACK`.
   
   Builds with `-Werror` (CI image): qemu-armv8a:knsh and :nsh, rv-virt:knsh 
and :nsh (32-bit), qemu-intel64:knsh_romfs (CMake), with TCP, TUN, local and 
UDP sockets; the touched files also without `CONFIG_NET_SOCKOPTS` 
(qemu-armv8a:nsh: they compile with no warning). Protected build: not built 
(common network code, no build-mode test).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] net/tcp: Give the errors... casaroli (via GitHub) via commits

Reply via email to