This commit is contained in:
iTrooz 2026-11-25 04:09:27 +00:00 • committed by GitHub
commit 08733df0c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 3 deletions

View file

@ -429,7 +429,7 @@ int ncat_send(struct fdinfo *fdn, const char *buf, size_t size)
{
int n;
if (o.recvonly)
if (o.recvonly || fdn->unnamed)
return size;
n = blocking_fdinfo_send(fdn, buf, size);
@ -456,6 +456,9 @@ int ncat_broadcast(fd_set *fds, const fd_list_t *fdlist, const char *msg, size_t
fdn = &fdlist->fds[i];
if (!checked_fd_isset(fdn->fd, fds))
continue;
/* An unnamed AF_UNIX peer cannot receive replies. */
if (fdn->unnamed)
continue;
if (blocking_fdinfo_send(fdn, msg, size) <= 0) {
if (o.debug > 1)
@ -604,7 +607,10 @@ void setup_environment(struct fdinfo *info)
char port[16];
socklen_t alen = sizeof(su);
if (getpeername(info->fd, &su.sockaddr, &alen) != 0) {
if (info->unnamed) {
/* The listener leaves the socket unconnected for unnamed datagram peers. */
su = info->remoteaddr;
} else if (getpeername(info->fd, &su.sockaddr, &alen) != 0) {
bye("getpeername failed: %s", socket_strerror(socket_errno()));
}
#ifdef HAVE_SYS_UN_H

View file

@ -623,7 +623,22 @@ static void handle_connection(int socket_accept, int type, fd_set *listen_fds)
* We're using connected udp. This has the down side of only
* being able to handle one udp client at a time
*/
Connect(socket_accept, &s.remoteaddr.sockaddr, s.ss_len);
#if HAVE_SYS_UN_H
if (o.af == AF_UNIX &&
(s.ss_len <= offsetof(struct sockaddr_un, sun_path)
#if defined(__APPLE__) || defined(__FreeBSD__)
/* These kernels return a full sockaddr with an empty path.
* On Linux, a leading NUL instead identifies an abstract address. */
|| s.remoteaddr.un.sun_path[0] == '\0'
#endif
)) {
/* Normalize the address. */
s.remoteaddr.un.sun_family = AF_UNIX;
s.ss_len = sizeof(sa_family_t);
s.unnamed = 1;
} else
#endif
Connect(socket_accept, &s.remoteaddr.sockaddr, s.ss_len);
s.fd = socket_accept;
/* If we expect new connections, we'll have to open a new listening
* socket to replace the one we just connected to a single client. */
@ -681,6 +696,10 @@ static void handle_connection(int socket_accept, int type, fd_set *listen_fds)
loguser_noprefix(".\n");
}
/* An unnamed peer has no address we can send back to. */
if (s.unnamed && !o.recvonly)
loguser("Cannot reply to an unnamed Unix datagram sender; discarding writes.\n");
/* Check conditions that might cause us to deny the connection. */
conn_count = get_conn_count();
if (conn_count >= o.conn_limit) {

View file

@ -797,6 +797,62 @@ kill_children;
unlink($UNIXSOCK);
}
# A UNIX datagram socket that never called bind()
{
local $xfail = 1 if !$HAVE_UNIXSOCK;
($s_pid, $s_out, $s_in) = ncat_server_noport("-U", "--udp", $UNIXSOCK);
test "Server UNIX socket listen receives from unnamed sender --udp (DGRAM)",
sub {
my $resp;
local *SOCK;
socket(SOCK, AF_UNIX, SOCK_DGRAM, 0) or die;
send(SOCK, "abc\n", 0, sockaddr_un($UNIXSOCK)) or die;
close(SOCK);
$resp = timeout_read($s_out) or die "Read timeout";
$resp eq "abc\n" or die "Server got \"$resp\", not \"abc\\n\" from unnamed sender";
};
# An unnamed peer never sends EOF, so the server has no reason to exit.
kill "TERM", $s_pid;
kill_children;
unlink($UNIXSOCK);
}
{
local $xfail = 1 if !$HAVE_UNIXSOCK;
my $report = 'print STDERR "$ENV{NCAT_REMOTE_ADDR}:$ENV{NCAT_REMOTE_PORT} $_"; exit';
my $command = "$PERL -ne " . plain_quote($report);
($s_pid, $s_out, $s_in, $s_err) = ncat_server_noport("-U", "--udp", "--exec", $command, $UNIXSOCK);
test "Server UNIX datagram socket passes data from an unnamed sender to --exec",
sub {
local *SOCK;
socket(SOCK, AF_UNIX, SOCK_DGRAM, 0) or die "socket: $!";
send(SOCK, "abc\n", 0, sockaddr_un($UNIXSOCK)) or die "send: $!";
my $resp = timeout_read($s_err) or die "No output from command";
$resp =~ /^localhost: abc$/m or die "Unexpected output: $resp";
};
kill_children;
unlink($UNIXSOCK);
}
if ($^O eq "linux") {
($s_pid, $s_out, $s_in) = ncat_server_noport("-U", "--udp", $UNIXSOCK);
test "Server UNIX datagram socket replies to a Linux abstract sender",
sub {
local *SOCK;
socket(SOCK, AF_UNIX, SOCK_DGRAM, 0) or die "socket: $!";
bind(SOCK, sockaddr_un("\0ncat-test-$$")) or die "bind: $!";
send(SOCK, "abc\n", 0, sockaddr_un($UNIXSOCK)) or die "send: $!";
my $resp = timeout_read($s_out) or die "Read timeout";
$resp eq "abc\n" or die "Server got \"$resp\", not \"abc\\n\"";
syswrite($s_in, "reply\n") or die "write: $!";
$resp = timeout_read(\*SOCK) or die "No reply to abstract sender";
$resp eq "reply\n" or die "Sender got \"$resp\", not \"reply\\n\"";
};
kill_children;
unlink($UNIXSOCK);
}
server_client_test "Connect success exit code",
[], ["--send-only"], sub {
my ($pid, $code);

View file

@ -154,6 +154,7 @@ struct fdinfo {
int lasterr;
union sockaddr_u remoteaddr;
socklen_t ss_len;
int unnamed;
#ifdef HAVE_OPENSSL
SSL *ssl;
#endif