haskell / network

Low-level networking interface
http://hackage.haskell.org/package/network
Other
325 stars 187 forks source link

Emulating socketPair on Windows #550

Closed kazu-yamamoto closed 1 year ago

kazu-yamamoto commented 1 year ago

@Misuke socketPair call error since Windows does not provide socketpair(2). The following code could emulate socketPair on Windows when we support AF_UNIX. What do you think?

sockpair :: IO (Socket, Socket)
sockpair = withSystemTempFile "temp-for-pair" $ \file hdl -> do
    hClose hdl
    removeFile file
    listenSock <- socket AF_UNIX Stream defaultProtocol
    bind listenSock $ SockAddrUnix file
    listen listenSock 10
    clientSock <- socket AF_UNIX Stream defaultProtocol
    connect clientSock $ SockAddrUnix file
    (serverSock, _) <- accept listenSock
    close listenSock
    return (clientSock, serverSock)
kazu-yamamoto commented 1 year ago

@Mistuke If you agree, I will implement socketPair based on AF_UNIX for Windows.

Mistuke commented 1 year ago

@kazu-yamamoto yeah that looks like a good workaround. Sorry I had missed that you had assigned this to me.

mpilgrem commented 1 year ago

Can I clarify, is network-3.1.3.0 broken on Windows? I tried moving the Stack project from lts-20.21 (network-3.1.2.9) to lts-20.22 (network-3.1.3.0) and it now it fails to build with:

persistent-sqlite> ghc-9.2.7.exe:  | C:\sr\snapshots\1cf8fbfe\lib\x86_64-windows-ghc-9.2.7\network-3.1.3.0-whnRyKC8v07Jg7Mne1eCa\HSnetwork-3.1.3.0-whnRyKC8v07Jg7Mne1eCa.o: unknown symbol `socketpair'
persistent-sqlite> ghc-9.2.7.exe: Could not load Object Code C:\sr\snapshots\1cf8fbfe\lib\x86_64-windows-ghc-9.2.7\network-3.1.3.0-whnRyKC8v07Jg7Mne1eCa\HSnetwork-3.1.3.0-whnRyKC8v07Jg7Mne1eCa.o.
persistent-sqlite>
persistent-sqlite> ghc-9.2.7.exe: unable to load unit `network-3.1.3.0'

Searching, I came across this issue.

kazu-yamamoto commented 1 year ago

@mpilgrem What about network-3.1.4.0?

mpilgrem commented 1 year ago

@kazu-yamamoto, thanks - all good with network-3.1.4.0. I'll wait for Stackage's LTS-20 series to catch up.

Mistuke commented 1 year ago

How did it happen though? c_socketpair is only defined on !windows. I'm also confused how the CI didn't catch it. Looks like the error is from the runtime linker, so I guess we didn't have a socketpair test?

kazu-yamamoto commented 1 year ago

@mpilgrem Nice! Sorry for your inconvenience. @Mistuke I guess #556 fixes that.