External.hs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {-# LANGUAGE ForeignFunctionInterface #-}
  2. {-# LANGUAGE InterruptibleFFI #-}
  3. {-# LANGUAGE EmptyDataDecls #-}
  4. module System.IO.Uniform.External where
  5. import Foreign
  6. import Foreign.C.Types
  7. import Foreign.C.String
  8. import System.Posix.Types (Fd(..))
  9. data Nethandler
  10. -- | A bounded IP port from where to accept SocketIO connections.
  11. newtype BoundedPort = BoundedPort {lis :: (Ptr Nethandler)}
  12. data Ds
  13. data TlsDs
  14. data SocketIO = SocketIO {sock :: (Ptr Ds)} | TlsSocketIO {bio :: (Ptr TlsDs)}
  15. newtype FileIO = FileIO {fd :: (Ptr Ds)}
  16. data StdIO
  17. closeFd :: Fd -> IO ()
  18. closeFd (Fd f) = c_closeFd f
  19. -- | Closes a BoundedPort, and releases any resource used by it.
  20. closePort :: BoundedPort -> IO ()
  21. closePort p = c_closePort (lis p)
  22. foreign import ccall interruptible "getPort" c_getPort :: CInt -> IO (Ptr Nethandler)
  23. foreign import ccall interruptible "createFromHandler" c_accept :: Ptr Nethandler -> IO (Ptr Ds)
  24. foreign import ccall safe "createFromFileName" c_createFile :: CString -> IO (Ptr Ds)
  25. foreign import ccall interruptible "createToIPv4Host" c_connect4 :: CUInt -> CInt -> IO (Ptr Ds)
  26. foreign import ccall interruptible "createToIPv6Host" c_connect6 :: Ptr CUChar -> CInt -> IO (Ptr Ds)
  27. foreign import ccall interruptible "startSockTls" c_startSockTls :: Ptr Ds -> CString -> CString -> CString -> IO (Ptr TlsDs)
  28. foreign import ccall safe "getPeer" c_getPeer :: Ptr Ds -> Ptr CUInt -> Ptr CUChar -> Ptr CInt -> IO (CInt)
  29. --foreign import ccall safe "getFd" c_getFd :: Ptr Ds -> IO CInt
  30. --foreign import ccall safe "getTlsFd" c_getTlsFd :: Ptr TlsDs -> IO CInt
  31. foreign import ccall safe "closeFd" c_closeFd :: CInt -> IO ()
  32. foreign import ccall safe "prepareToClose" c_prepareToClose :: Ptr Ds -> IO CInt
  33. foreign import ccall safe "closeHandler" c_closePort :: Ptr Nethandler -> IO ()
  34. foreign import ccall safe "closeTls" c_closeTls :: Ptr TlsDs -> IO (Ptr Ds)
  35. foreign import ccall interruptible "sendDs" c_send :: Ptr Ds -> Ptr CChar -> CInt -> IO CInt
  36. foreign import ccall interruptible "stdDsSend" c_sendStd :: Ptr CChar -> CInt -> IO CInt
  37. foreign import ccall interruptible "tlsDsSend" c_sendTls :: Ptr TlsDs -> Ptr CChar -> CInt -> IO CInt
  38. foreign import ccall interruptible "recvDs" c_recv :: Ptr Ds -> Ptr CChar -> CInt -> IO CInt
  39. foreign import ccall interruptible "stdDsRecv" c_recvStd :: Ptr CChar -> CInt -> IO CInt
  40. foreign import ccall interruptible "tlsDsRecv" c_recvTls :: Ptr TlsDs -> Ptr CChar -> CInt -> IO CInt