]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0170: channel: some issues in ch_listen() v9.2.0170
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Sun, 15 Mar 2026 09:22:29 +0000 (09:22 +0000)
committerChristian Brabandt <cb@256bit.org>
Sun, 15 Mar 2026 09:32:19 +0000 (09:32 +0000)
Problem:  channel: some issues in ch_listen()
          (char101, after v9.2.0153)
Solution: On MS-Windows, initialize using channel_init_winsock() and use
          SO_EXCLUSIVEADDRUSE instead of SO_REUSEADDR, allow to use port
          0 to have the OS assign a port (Yasuhiro Matsumoto).

related: #19231
closes:  #19690

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Yasuhiro Matsumoto <mattn.jp@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/channel.c
src/testdir/test_channel.vim
src/version.c

index f35cbf8eadbb6ac40438243b6dceb4250fbef370..6f06517fd765b9aad79832011cc3028eeecede9d 100644 (file)
@@ -1440,7 +1440,7 @@ channel_listen_func(typval_T *argvars)
            return NULL;
        }
        port = strtol((char *)(p + 1), &rest, 10);
-       if (port <= 0 || port >= 65536 || *rest != NUL)
+       if (port < 0 || port >= 65536 || *rest != NUL)
        {
            semsg(_(e_invalid_argument_str), address);
            return NULL;
@@ -1459,7 +1459,7 @@ channel_listen_func(typval_T *argvars)
            return NULL;
        }
        port = strtol((char *)(p + 1), &rest, 10);
-       if (port <= 0 || port >= 65536 || *rest != NUL)
+       if (port < 0 || port >= 65536 || *rest != NUL)
        {
            semsg(_(e_invalid_argument_str), address);
            return NULL;
@@ -1511,6 +1511,10 @@ channel_listen(
     int                        val = 1;
     channel_T          *channel;
 
+#ifdef MSWIN
+    channel_init_winsock();
+#endif
+
     channel = add_channel();
     if (channel == NULL)
     {
@@ -1555,7 +1559,7 @@ channel_listen(
     }
 
 #ifdef MSWIN
-    if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
+    if (setsockopt(sd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
                                    (const char *)&val, sizeof(val)) < 0)
 #else
     if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
@@ -1591,6 +1595,16 @@ channel_listen(
        return NULL;
     }
 
+    // When port 0 was specified, retrieve the actual port assigned by the OS.
+    if (port_in == 0)
+    {
+       struct sockaddr_in      addr;
+       socklen_t               addr_len = sizeof(addr);
+
+       if (getsockname(sd, (struct sockaddr *)&addr, &addr_len) == 0)
+           port_in = ntohs(addr.sin_port);
+    }
+
     channel->ch_listen = TRUE;
     channel->CH_SOCK_FD = (sock_T)sd;
     channel->ch_nb_close_cb = nb_close_cb;
index 431eaab26106e46fa3520b4dee9564ec920797ef..94dec0d973199385ee8c0c2ca47223d5280856a8 100644 (file)
@@ -2798,8 +2798,11 @@ func Test_listen_invalid_address()
     " port number too large
     call assert_fails("call ch_listen('localhost:99999')", 'E475:')
 
-    " port number zero
-    call assert_fails("call ch_listen('localhost:0')", 'E475:')
+    " port number zero should let the OS assign an available port
+    let ch = ch_listen('localhost:0')
+    call assert_equal('open', ch_status(ch))
+    call assert_notequal(0, ch_info(ch).port)
+    call ch_close(ch)
 
     " port number negative
     call assert_fails("call ch_listen('localhost:-1')", 'E475:')
index 4f9c944a621d92729019a0c7c7b20e78c9158e85..38e48f4a11be7be8380fa7402fa7817948603318 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    170,
 /**/
     169,
 /**/