classification
Title: unable to invoke socket.connect with AF_UNSPEC
Type: behavior Stage:
Components: IO Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Roman.Valov, giampaolo.rodola, marto1
Priority: normal Keywords:

Created on 2013-05-29 14:07 by Roman.Valov, last changed 2013-07-06 18:42 by giampaolo.rodola.

Messages (2)
msg190308 - (view) Author: Roman Valov (Roman.Valov) Date: 2013-05-29 14:07
There is a way to "disconnect" UDP socket
that was previously "connected" to specific
remote endpoint in C:

  struct sockaddr_in sin;        
  memset((char *)&sin, 0, sizeof(sin));
  sin.sin_family = AF_UNSPEC;
  connect(fd, (struct sockaddr *)&sin, sizeof(sin));

However in this is not available in python, since connect
accepts only (host, port) as a parameter for UDP socket.

It's possible to drop "port" connection with port=0,
however I can't find a way to drop "host" connection.
msg192479 - (view) Author: Martin Gergov (marto1) Date: 2013-07-06 18:34
--- a/Modules/socketmodule.c	Tue Jul 02 09:07:53 2013 -0400
+++ b/Modules/socketmodule.c	Sat Jul 06 21:08:40 2013 +0300
@@ -3673,6 +3673,15 @@
 {
     int how;
     int res;
+    struct sockaddr_in sin;
+    if (s->sock_type == SOCK_DGRAM){
+        memset((char *)&sin, 0, sizeof(sin));
+        sin.sin_family = AF_UNSPEC;
+        Py_BEGIN_ALLOW_THREADS
+        res = connect(s->sock_fd, (struct sockaddr *)&sin, sizeof(sin));
+        Py_END_ALLOW_THREADS
+        return Py_None;
+    }
 
     how = _PyLong_AsInt(arg);
     if (how == -1 && PyErr_Occurred())


The shutdown method seems a suitable candidate, but a disconnect() method(which is semantically more correct) could be introduced.
History
Date User Action Args
2013-07-06 18:42:10giampaolo.rodolasetnosy: + giampaolo.rodola
2013-07-06 18:34:03marto1setnosy: + marto1
messages: + msg192479
2013-05-29 14:07:24Roman.Valovcreate