Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build with network 3 #58

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages: ./libssh2
./libssh2-conduit
2 changes: 1 addition & 1 deletion libssh2/libssh2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Library
Includes: include/libssh2_local.h

Build-depends: base >= 4 && < 5,
network >= 2.3 && < 3.0,
network >= 2.3 && < 3.2,
syb >= 0.3.3, time >= 1.2,
bytestring >= 0.9,
unix
Expand Down
44 changes: 41 additions & 3 deletions libssh2/src/Network/SSH/Client/LibSSH2/Foreign.chs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ import Foreign hiding (void)
import Foreign.C.Types
import Foreign.C.String
import System.IO
#if MIN_VERSION_network(3,0,0)
import Network.Socket (Socket, withFdSocket)
#else
import Network.Socket (Socket(MkSocket), isReadable)
#endif
import qualified Data.ByteString as BSS
import qualified Data.ByteString.Unsafe as BSS

Expand Down Expand Up @@ -144,6 +148,24 @@ init_crypto :: Bool -> CInt
init_crypto False = 1
init_crypto True = 0

#if MIN_VERSION_network(3,0,0)
ssh2socket :: Socket
#ifdef mingw32_HOST_OS
#ifdef x86_64_HOST_ARCH
-> CULLong
#else
-> IO CUInt
#endif
#else
-> IO CInt
#endif
ssh2socket s =
#ifdef mingw32_HOST_OS
fromIntegral <$> withFdSocket s pure
#else
withFdSocket s pure
#endif
#else
ssh2socket :: Socket
#ifdef mingw32_HOST_OS
#ifdef x86_64_HOST_ARCH
Expand All @@ -153,14 +175,16 @@ ssh2socket :: Socket
#endif
#else
-> CInt
#endif
#endif /* mingw32_HOST_OS */
ssh2socket (MkSocket s _ _ _ _) =
#ifdef mingw32_HOST_OS
(fromIntegral s)
#else
s
#endif

#endif /* MIN_VERSION_network(3,0,0) */

{# fun init as initialize_
{ init_crypto `Bool' } -> `Int' #}

Expand Down Expand Up @@ -209,14 +233,24 @@ bool2int :: Bool -> CInt
bool2int True = 1
bool2int False = 0

#if MIN_VERSION_network(3,0,0)
{# fun session_handshake
{ `Ptr ()', `CInt' } -> `Int' #}

handshake_ :: Session -> Socket -> IO Int
handshake_ session socket = do
session_handshake (toPointer session) =<< ssh2socket socket
#else
{# fun session_handshake as handshake_
{ toPointer `Session', ssh2socket `Socket' } -> `Int' #}
#endif

-- | Run SSH handshake on network socket.
handshake :: Session -> Socket -> IO ()
handshake session socket = do
sessionSetSocket session (Just socket)
void . handleInt (Just session) $ handshake_ session socket
void $ handleInt (Just session)
$ handshake_ session socket

{# fun knownhost_init as initKnownHosts_
{ toPointer `Session' } -> `Ptr ()' id #}
Expand Down Expand Up @@ -543,7 +577,11 @@ pollChannelRead ch = do
mbSocket <- sessionGetSocket (channelSession ch)
case mbSocket of
Nothing -> error "pollChannelRead without socket present"
#if MIN_VERSION_network(3,0,0)
Just _ -> pure True
#else
Just socket -> isReadable socket
#endif

--
-- | Sftp support
Expand Down Expand Up @@ -893,4 +931,4 @@ agentAuthenticate login agent = do
isAuthenticationFailed _ = Nothing

withAgentPublicKeyVoidPtr :: AgentPublicKey -> (Ptr () -> IO a) -> IO a
withAgentPublicKeyVoidPtr p f = withAgentPublicKey p $ \pp -> f (castPtr pp)
withAgentPublicKeyVoidPtr p f = withAgentPublicKey p $ \pp -> f (castPtr pp)
16 changes: 15 additions & 1 deletion libssh2/src/Network/SSH/Client/LibSSH2/WaitSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ module Network.SSH.Client.LibSSH2.WaitSocket
, threadWaitWrite
) where

import Network.Socket(Socket,fdSocket)
import Network.Socket(Socket)
#if MIN_VERSION_network(3,0,0)
import Network.Socket(withFdSocket)
#else
import Network.Socket(fdSocket)
#endif

import System.Posix.Types(Fd(Fd))

#ifdef mingw32_HOST_OS
Expand All @@ -26,10 +32,18 @@ import qualified GHC.Conc (threadWaitRead, threadWaitWrite)
#endif

threadWaitRead :: Socket -> IO ()
#if MIN_VERSION_network(3,0,0)
threadWaitRead = flip withFdSocket (threadWaitRead_ . Fd)
#else
threadWaitRead = threadWaitRead_ . Fd . fdSocket
#endif

threadWaitWrite :: Socket -> IO ()
#if MIN_VERSION_network(3,0,0)
threadWaitWrite = flip withFdSocket (threadWaitWrite_ . Fd)
#else
threadWaitWrite = threadWaitWrite_ . Fd . fdSocket
#endif

-- | Block the current thread until data is available to read on the
-- given file descriptor (GHC only).
Expand Down