public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: connect: implement resetting a connected DGRAM socket
@ 2021-04-26 19:37 Ken Brown
  2021-04-27 13:19 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Ken Brown @ 2021-04-26 19:37 UTC (permalink / raw)
  To: cygwin-patches

Following POSIX and Linux, allow a connected DGRAM socket's connection
to be reset (so that the socket becomes unconnected).  This is done by
calling connect and specifing an address whose family is AF_UNSPEC.
---
 winsup/cygwin/fhandler_socket_inet.cc  | 21 ++++++++++++++++--
 winsup/cygwin/fhandler_socket_local.cc | 30 +++++++++++++++++++++-----
 winsup/cygwin/fhandler_socket_unix.cc  |  7 ++++++
 winsup/cygwin/release/3.2.1            |  3 +++
 winsup/doc/new-features.xml            |  6 ++++++
 5 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler_socket_inet.cc b/winsup/cygwin/fhandler_socket_inet.cc
index f6bb8c503..30eab4099 100644
--- a/winsup/cygwin/fhandler_socket_inet.cc
+++ b/winsup/cygwin/fhandler_socket_inet.cc
@@ -781,8 +781,20 @@ int
 fhandler_socket_inet::connect (const struct sockaddr *name, int namelen)
 {
   struct sockaddr_storage sst;
+  bool reset = (name->sa_family == AF_UNSPEC
+		&& get_socket_type () == SOCK_DGRAM);
 
-  if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR)
+  if (reset)
+    {
+      if (connect_state () == unconnected)
+	return 0;
+      /* To reset a connected DGRAM socket, call Winsock's connect
+	 function with the address member of the sockaddr structure
+	 filled with zeroes. */
+      memset (&sst, 0, sizeof sst);
+      sst.ss_family = get_addr_family ();
+    }
+  else if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR)
     return SOCKET_ERROR;
 
   /* Initialize connect state to "connect_pending".  In the SOCK_STREAM
@@ -804,7 +816,12 @@ fhandler_socket_inet::connect (const struct sockaddr *name, int namelen)
 
   int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen);
   if (!res)
-    connect_state (connected);
+    {
+      if (reset)
+	connect_state (unconnected);
+      else
+	connect_state (connected);
+    }
   else if (!is_nonblocking ()
       && res == SOCKET_ERROR
       && WSAGetLastError () == WSAEWOULDBLOCK)
diff --git a/winsup/cygwin/fhandler_socket_local.cc b/winsup/cygwin/fhandler_socket_local.cc
index 1c8d48b58..bd4081622 100644
--- a/winsup/cygwin/fhandler_socket_local.cc
+++ b/winsup/cygwin/fhandler_socket_local.cc
@@ -894,19 +894,34 @@ fhandler_socket_local::connect (const struct sockaddr *name, int namelen)
 {
   struct sockaddr_storage sst;
   int type = 0;
+  bool reset = (name->sa_family == AF_UNSPEC
+		&& get_socket_type () == SOCK_DGRAM);
 
-  if (get_inet_addr_local (name, namelen, &sst, &namelen, &type, connect_secret)
-      == SOCKET_ERROR)
+  if (reset)
+    {
+      if (connect_state () == unconnected)
+	return 0;
+      /* To reset a connected DGRAM socket, call Winsock's connect
+	 function with the address member of the sockaddr structure
+	 filled with zeroes. */
+      memset (&sst, 0, sizeof sst);
+      sst.ss_family = get_addr_family ();
+    }
+  else if (get_inet_addr_local (name, namelen, &sst, &namelen, &type,
+				connect_secret) == SOCKET_ERROR)
     return SOCKET_ERROR;
 
-  if (get_socket_type () != type)
+  if (get_socket_type () != type && !reset)
     {
       WSASetLastError (WSAEPROTOTYPE);
       set_winsock_errno ();
       return SOCKET_ERROR;
     }
 
-  set_peer_sun_path (name->sa_data);
+  if (reset)
+    set_peer_sun_path (NULL);
+  else
+    set_peer_sun_path (name->sa_data);
 
   /* Don't move af_local_set_cred into af_local_connect which may be called
      via select, possibly running under another identity.  Call early here,
@@ -933,7 +948,12 @@ fhandler_socket_local::connect (const struct sockaddr *name, int namelen)
 
   int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen);
   if (!res)
-    connect_state (connected);
+    {
+      if (reset)
+	connect_state (unconnected);
+      else
+	connect_state (connected);
+    }
   else if (!is_nonblocking ()
       && res == SOCKET_ERROR
       && WSAGetLastError () == WSAEWOULDBLOCK)
diff --git a/winsup/cygwin/fhandler_socket_unix.cc b/winsup/cygwin/fhandler_socket_unix.cc
index 252bcd9a9..a2428e952 100644
--- a/winsup/cygwin/fhandler_socket_unix.cc
+++ b/winsup/cygwin/fhandler_socket_unix.cc
@@ -1696,6 +1696,13 @@ fhandler_socket_unix::connect (const struct sockaddr *name, int namelen)
       conn_unlock ();
       return -1;
     }
+  if (name->sa_family == AF_UNSPEC && get_socket_type () == SOCK_DGRAM)
+    {
+      connect_state (unconnected);
+      peer_sun_path (NULL);
+      conn_unlock ();
+      return 0;
+    }
   connect_state (connect_pending);
   conn_unlock ();
   /* Check validity of name */
diff --git a/winsup/cygwin/release/3.2.1 b/winsup/cygwin/release/3.2.1
index 7662c7114..9edf509bb 100644
--- a/winsup/cygwin/release/3.2.1
+++ b/winsup/cygwin/release/3.2.1
@@ -1,6 +1,9 @@
 What's new:
 -----------
 
+- A connected datagram socket can now have its connection reset.  As
+  specified by POSIX and Linux, this is done by calling connect(2)
+  with an address structure whose family is AF_UNSPEC.
 
 What changed:
 -------------
diff --git a/winsup/doc/new-features.xml b/winsup/doc/new-features.xml
index 5ec36e409..aa3594ce2 100644
--- a/winsup/doc/new-features.xml
+++ b/winsup/doc/new-features.xml
@@ -76,6 +76,12 @@ facl(2) now fails with EBADF on a file opened with O_PATH.
   as symlinks to the actual executables.
 </para></listitem>
 
+<listitem><para>
+- A connected datagram socket can now have its connection reset.  As
+  specified by POSIX and Linux, this is done by calling connect(2)
+  with an address structure whose family is AF_UNSPEC.
+</para></listitem>
+
 </itemizedlist>
 
 </sect2>
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Cygwin: connect: implement resetting a connected DGRAM socket
  2021-04-26 19:37 [PATCH] Cygwin: connect: implement resetting a connected DGRAM socket Ken Brown
@ 2021-04-27 13:19 ` Corinna Vinschen
  2021-04-27 13:48   ` Ken Brown
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2021-04-27 13:19 UTC (permalink / raw)
  To: cygwin-patches

On Apr 26 15:37, Ken Brown wrote:
> Following POSIX and Linux, allow a connected DGRAM socket's connection
> to be reset (so that the socket becomes unconnected).  This is done by
> calling connect and specifing an address whose family is AF_UNSPEC.
> ---
>  winsup/cygwin/fhandler_socket_inet.cc  | 21 ++++++++++++++++--
>  winsup/cygwin/fhandler_socket_local.cc | 30 +++++++++++++++++++++-----
>  winsup/cygwin/fhandler_socket_unix.cc  |  7 ++++++
>  winsup/cygwin/release/3.2.1            |  3 +++
>  winsup/doc/new-features.xml            |  6 ++++++
>  5 files changed, 60 insertions(+), 7 deletions(-)

LGTM.

> --- a/winsup/cygwin/release/3.2.1
> +++ b/winsup/cygwin/release/3.2.1
> @@ -1,6 +1,9 @@
>  What's new:
>  -----------
>  
> +- A connected datagram socket can now have its connection reset.  As
> +  specified by POSIX and Linux, this is done by calling connect(2)
> +  with an address structure whose family is AF_UNSPEC.

Isn't that just a bug, in theory?


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Cygwin: connect: implement resetting a connected DGRAM socket
  2021-04-27 13:19 ` Corinna Vinschen
@ 2021-04-27 13:48   ` Ken Brown
  0 siblings, 0 replies; 3+ messages in thread
From: Ken Brown @ 2021-04-27 13:48 UTC (permalink / raw)
  To: cygwin-patches

On 4/27/2021 9:19 AM, Corinna Vinschen wrote:
> On Apr 26 15:37, Ken Brown wrote:
>> Following POSIX and Linux, allow a connected DGRAM socket's connection
>> to be reset (so that the socket becomes unconnected).  This is done by
>> calling connect and specifing an address whose family is AF_UNSPEC.
>> ---
>>   winsup/cygwin/fhandler_socket_inet.cc  | 21 ++++++++++++++++--
>>   winsup/cygwin/fhandler_socket_local.cc | 30 +++++++++++++++++++++-----
>>   winsup/cygwin/fhandler_socket_unix.cc  |  7 ++++++
>>   winsup/cygwin/release/3.2.1            |  3 +++
>>   winsup/doc/new-features.xml            |  6 ++++++
>>   5 files changed, 60 insertions(+), 7 deletions(-)
> 
> LGTM.
> 
>> --- a/winsup/cygwin/release/3.2.1
>> +++ b/winsup/cygwin/release/3.2.1
>> @@ -1,6 +1,9 @@
>>   What's new:
>>   -----------
>>   
>> +- A connected datagram socket can now have its connection reset.  As
>> +  specified by POSIX and Linux, this is done by calling connect(2)
>> +  with an address structure whose family is AF_UNSPEC.
> 
> Isn't that just a bug, in theory?

I was thinking of it as a feature that hadn't been implemented yet.  But on 
second thought, I agree with you.  I'll change that.

Ken

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-27 13:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-26 19:37 [PATCH] Cygwin: connect: implement resetting a connected DGRAM socket Ken Brown
2021-04-27 13:19 ` Corinna Vinschen
2021-04-27 13:48   ` Ken Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).