bpo-28134: Auto-detect socket values from file descriptor (#1349)

Fix socket(fileno=fd) by auto-detecting the socket's family, type,
and proto from the file descriptor. The auto-detection can be overruled
by passing in family, type, and proto explicitly.

Without the fix, all socket except for TCP/IP over IPv4 are basically broken:

>>> s = socket.create_connection(('www.python.org', 443))
>>> s
<socket.socket fd=3, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=6, laddr=('2003:58:bc4a:3b00:56ee:75ff:fe47:ca7b', 59730, 0, 0), raddr=('2a04:4e42:1b::223', 443, 0, 0)>
>>> socket.socket(fileno=s.fileno())
<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('2003:58:bc4a:3b00::%2550471192', 59730, 0, 2550471192), raddr=('2a04:4e42:1b:0:700c:e70b:ff7f:0%2550471192', 443, 0, 2550471192)>

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2018-01-29 22:37:58 +01:00 committed by GitHub
parent 72a0d218dc
commit b6e43af669
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 9 deletions

View file

@ -102,7 +102,8 @@ Local naming conventions:
/* Socket object documentation */
PyDoc_STRVAR(sock_doc,
"socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\
"socket(family=AF_INET, type=SOCK_STREAM, proto=0) -> socket object\n\
socket(family=-1, type=-1, proto=-1, fileno=None) -> socket object\n\
\n\
Open a socket of the given type. The family argument specifies the\n\
address family; it defaults to AF_INET. The type argument specifies\n\
@ -111,6 +112,9 @@ or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
specifying the default protocol. Keyword arguments are accepted.\n\
The socket is created as non-inheritable.\n\
\n\
When a fileno is passed in, family, type and proto are auto-detected,\n\
unless they are explicitly set.\n\
\n\
A socket object represents one endpoint of a network connection.\n\
\n\
Methods of socket objects (keyword arguments not allowed):\n\
@ -4792,7 +4796,7 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
PySocketSockObject *s = (PySocketSockObject *)self;
PyObject *fdobj = NULL;
SOCKET_T fd = INVALID_SOCKET;
int family = AF_INET, type = SOCK_STREAM, proto = 0;
int family = -1, type = -1, proto = -1;
static char *keywords[] = {"family", "type", "proto", "fileno", 0};
#ifndef MS_WINDOWS
#ifdef SOCK_CLOEXEC
@ -4842,9 +4846,72 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
"can't use invalid socket value");
return -1;
}
if (family == -1) {
sock_addr_t addrbuf;
socklen_t addrlen = sizeof(sock_addr_t);
memset(&addrbuf, 0, addrlen);
if (getsockname(fd, SAS2SA(&addrbuf), &addrlen) == 0) {
family = SAS2SA(&addrbuf)->sa_family;
} else {
#ifdef MS_WINDOWS
PyErr_SetFromWindowsErrWithFilename(0, "family");
#else
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "family");
#endif
return -1;
}
}
#ifdef SO_TYPE
if (type == -1) {
int tmp;
socklen_t slen = sizeof(tmp);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &tmp, &slen) == 0) {
type = tmp;
} else {
#ifdef MS_WINDOWS
PyErr_SetFromWindowsErrWithFilename(0, "type");
#else
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "type");
#endif
return -1;
}
}
#else
type = SOCK_STREAM;
#endif
#ifdef SO_PROTOCOL
if (proto == -1) {
int tmp;
socklen_t slen = sizeof(tmp);
if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &tmp, &slen) == 0) {
proto = tmp;
} else {
#ifdef MS_WINDOWS
PyErr_SetFromWindowsErrWithFilename(0, "protocol");
#else
PyErr_SetFromErrnoWithFilename(PyExc_OSError, "protocol");
#endif
return -1;
}
}
#else
proto = 0;
#endif
}
}
else {
/* No fd, default to AF_INET and SOCK_STREAM */
if (family == -1) {
family = AF_INET;
}
if (type == -1) {
type = SOCK_STREAM;
}
if (proto == -1) {
proto = 0;
}
#ifdef MS_WINDOWS
/* Windows implementation */
#ifndef WSA_FLAG_NO_HANDLE_INHERIT