Skip to content

Instantly share code, notes, and snippets.

@geertj
Last active October 3, 2024 16:11
Show Gist options
  • Save geertj/4325783 to your computer and use it in GitHub Desktop.
Save geertj/4325783 to your computer and use it in GitHub Desktop.
Emulates the UNIX socketpair() system call on Windows. This function uses a trick with non-blocking sockets to prevent the need for a thread. A socketpair can be used as a full-duplex, select()able pipe on Windows.
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
"""Emulate the Unix socketpair() function on Windows."""
# We create a connected TCP socket. Note the trick with setblocking(0)
# that prevents us from having to create a thread.
lsock = socket.socket(family, type, proto)
lsock.bind(('localhost', 0))
lsock.listen(1)
addr, port = lsock.getsockname()
csock = socket.socket(family, type, proto)
csock.setblocking(0)
try:
csock.connect((addr, port))
except socket.error, e:
if e.errno != errno.WSAEWOULDBLOCK:
raise
ssock, addr = lsock.accept()
csock.setblocking(1)
lsock.close()
return (ssock, csock)
@ell1e
Copy link

ell1e commented Jul 30, 2024

Since the problem is public now, the above gist has a security issue in that with the right timing, an attacker can take over the connection: https://www.cve.org/CVERecord?id=CVE-2024-3219 Therefore, I would advise against using it. My apologies that it took so long for this to become public. The fix is to verify that the connection obtained is actually the corresponding one that was meant to connect, like done here: python/cpython#122134

@hrieke
Copy link

hrieke commented Jul 31, 2024

Thank you @ell1e for sending this information out.
@geertj License?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment