Skip to content

Netcat's -k option doesn't work on vanilla openbsd netcat #3564

@stevenengler

Description

@stevenengler

This issue applies to the openbsd version of netcat, without any of the debian patches. Specifically, the version of netcat on fedora 40.

The -k option is documented as:

When a connection is completed, listen for another one. Requires -l. When used together with the -u option, the server socket is not connected and it can receive UDP datagrams from multiple hosts.

This is designed so that a netcat server can accept multiple clients in a row. But vanilla openbsd netcat actually closes and recreates the listening socket after each client connection. See the comment at #3558 (comment) for details.

Since netcat is closing the listening socket and immediately opening a new socket and trying to bind to the same listening address, the original listening socket won't have been disassociated from the network interface yet due to #3563. So when netcat tries to bind the new socket, it gets EADDRINUSE.

00:00:00.000000000 [tid 1000] socket(AF_INET, 1, 6) = 3
00:00:00.000000000 [tid 1000] setsockopt(3, 1, 2, 0x7ffc0bf2bb1c, 4) = 0
00:00:00.000000000 [tid 1000] setsockopt(3, 1, 15, 0x7ffc0bf2bb1c, 4) = 0
00:00:00.000000000 [tid 1000] bind(3, 0.0.0.0:8080, 16) = 0
00:00:00.000000000 [tid 1000] listen(3, 1) = 0
00:00:00.000000000 [tid 1000] accept4(3, 0x7ffc0bf2bc80, 128 (0x7ffc0bf2bb18), 2048) = <blocked>
00:00:02.003000000 [tid 1000] accept4(3, 0x7ffc0bf2bc80, 16 (0x7ffc0bf2bb18), 2048) = 4
[snip]
00:00:02.003000000 [tid 1000] close(4) = 0
00:00:02.003000000 [tid 1000] close(3) = 0
00:00:02.003000000 [tid 1000] socket(AF_INET, 1, 6) = 3
00:00:02.003000000 [tid 1000] setsockopt(3, 1, 2, 0x7ffc0bf2bb1c, 4) = 0
00:00:02.003000000 [tid 1000] setsockopt(3, 1, 15, 0x7ffc0bf2bb1c, 4) = 0
00:00:02.003000000 [tid 1000] bind(3, 0.0.0.0:8080, 16) = -98 (EADDRINUSE)
00:00:02.003000000 [tid 1000] close(3) = 0
00:00:02.003000000 [tid 1000] write(2, "nc: Address already in use\n", 27) = 27

This issue is the same as #3563, and should be closed when/if #3563 is fixed. But I opened this as a separate issue so that we track this as a known compatibility issue with netcat. And we can ensure that netcat works properly after #3563 is fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugError or flaw producing unexpected results

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions