diff --git a/ncat/docs/ncat.xml b/ncat/docs/ncat.xml
index fd8cd8301..7ecb0d659 100644
--- a/ncat/docs/ncat.xml
+++ b/ncat/docs/ncat.xml
@@ -767,8 +767,11 @@
Both stream and datagram domain sockets are supported. Use
on its own for stream sockets, or
combine it with for datagram sockets.
- Datagram sockets require the use of the
- option to specify a source socket to connect from.
+ Datagram sockets require a source socket to connect from. By
+ default, a source socket with a random filename will be created as
+ needed, and deleted when the program ends. Use the
+ with a path to use a source socket with a
+ specific name.
diff --git a/ncat/ncat_connect.c b/ncat/ncat_connect.c
index 82cbf8d1a..23cd3ede6 100644
--- a/ncat/ncat_connect.c
+++ b/ncat/ncat_connect.c
@@ -519,10 +519,19 @@ int ncat_connect(void)
if (o.af == AF_UNIX && o.udp)
{
if (srcaddr.storage.ss_family != AF_UNIX) {
- bye("A source socket filename (--source) is required when using\n"
- "Unix domain sockets in --udp mode.");
+ char *tmp_name = NULL;
+ /* If no source socket was specified, we have to create temporary one. */
+ if ((tmp_name = tempnam(NULL, "ncat.")) == NULL)
+ bye("Failed to create name for temporary DGRAM source Unix domain socket (tempnam).");
+
+ srcaddr.un.sun_family = AF_UNIX;
+ strncpy(srcaddr.un.sun_path, tmp_name, sizeof(srcaddr.un.sun_path));
+ free (tmp_name);
}
nsi_set_localaddr(cs.sock_nsi, &srcaddr.storage, SUN_LEN((struct sockaddr_un *)&srcaddr.storage));
+
+ if (o.verbose)
+ loguser("[%s] used as source DGRAM Unix domain socket.\n", srcaddr.un.sun_path);
}
else
#endif
@@ -675,6 +684,14 @@ int ncat_connect(void)
nsi_get_read_count(cs.sock_nsi), time);
}
+#if HAVE_SYS_UN_H
+ if (o.af == AF_UNIX && o.udp) {
+ if (o.verbose)
+ loguser("Deleting source DGRAM Unix domain socket. [%s]\n", srcaddr.un.sun_path);
+ unlink(srcaddr.un.sun_path);
+ }
+#endif
+
nsp_delete(mypool);
return rc == NSOCK_LOOP_ERROR ? 1 : 0;
diff --git a/ncat/test/ncat-test.pl b/ncat/test/ncat-test.pl
index 25f1e7481..26970417a 100755
--- a/ncat/test/ncat-test.pl
+++ b/ncat/test/ncat-test.pl
@@ -548,6 +548,19 @@ sub {
kill_children;
unlink($UNIXSOCK);
+($s_pid, $s_out, $s_in) = ncat("-l", "-U", "--udp", $UNIXSOCK);
+test "Server UNIX socket listen on $UNIXSOCK --udp (DGRAM)",
+sub {
+ my $resp;
+
+ unlink($UNIXSOCK);
+ my ($c_pid, $c_out, $c_in) = ncat("-U", "--udp", $UNIXSOCK);
+ syswrite($c_in, "abc\n");
+ $resp = timeout_read($s_out);
+ $resp eq "abc\n" or die "Server got \"$resp\", not \"abc\\n\" from client";
+};
+kill_children;
+unlink($UNIXSOCK);
server_client_test "Connect success exit code",
[], ["--send-only"], sub {