-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A portable and extensible sockets library. -- @package socket @version 0.5.3.0 module System.Socket.Protocol class Protocol p protocolNumber :: Protocol p => p -> CInt instance Protocol () module System.Socket.Protocol.UDP data UDP instance Protocol UDP module System.Socket.Protocol.TCP data TCP instance Protocol TCP module System.Socket.Type class Type t typeNumber :: Type t => t -> CInt module System.Socket.Type.Datagram data Datagram instance Type Datagram module System.Socket.Type.Raw data Raw instance Type Raw module System.Socket.Type.SequentialPacket data SequentialPacket instance Type SequentialPacket module System.Socket.Type.Stream data Stream instance Type Stream module System.Socket.Family class Storable (SocketAddress f) => Family f where type family SocketAddress f familyNumber :: Family f => f -> CInt module System.Socket.Unsafe unsafeSend :: Socket a t p -> Ptr a -> CSize -> MessageFlags -> IO CInt unsafeSendTo :: Socket f t p -> Ptr b -> CSize -> MessageFlags -> Ptr (SocketAddress f) -> CInt -> IO CInt unsafeReceive :: Socket a t p -> Ptr b -> CSize -> MessageFlags -> IO CInt unsafeReceiveFrom :: Socket f t p -> Ptr b -> CSize -> MessageFlags -> Ptr (SocketAddress f) -> Ptr CInt -> IO CInt unsafeGetSocketOption :: Storable a => Socket f t p -> CInt -> CInt -> IO a unsafeSetSocketOption :: Storable a => Socket f t p -> CInt -> CInt -> a -> IO () -- | Blocks until a socket should be tried for reading. -- --
-- safeSocketWaitRead = do -- wait <- withMVar msock $ \sock-> do -- -- Register while holding a lock on the socket descriptor. -- unsafeSocketWaitRead sock 0 -- -- Do the waiting without keeping the socket descriptor locked. -- wait --unsafeSocketWaitRead :: Fd -> Int -> IO (IO ()) unsafeSocketWaitWrite :: Fd -> Int -> IO (IO ()) tryWaitRetryLoop :: Socket f t p -> (Fd -> Int -> IO (IO ())) -> (Fd -> IO CInt) -> IO CInt module System.Socket.Family.Inet data Inet data SocketAddressInet SocketAddressInet :: Address -> Port -> SocketAddressInet address :: SocketAddressInet -> Address port :: SocketAddressInet -> Port -- | To avoid errors with endianess it was decided to keep this type -- abstract. -- -- Hint: Use the Storable instance if you really need to access. -- It exposes it exactly as found within an IP packet (big endian if you -- insist on interpreting it as a number). -- -- Another hint: Use getAddressInfo for parsing and suppress -- nameserver lookups: -- --
-- > getAddressInfo (Just "127.0.0.1") Nothing aiNumericHost :: IO [AddressInfo Inet Stream TCP]
-- [AddressInfo {addressInfoFlags = AddressInfoFlags 4, socketAddress = SocketAddressInet { address = 127.0.0.1, port = 0}, canonicalName = Nothing}]
--
data Address
newtype Port
Port :: Word16 -> Port
-- | -- 224.0.0.1 --allHostsGroup :: Address -- |
-- 0.0.0.0 --any :: Address -- |
-- 255.255.255.255 --broadcast :: Address -- |
-- 127.0.0.1 --loopback :: Address -- |
-- 224.0.0.255 --maxLocalGroup :: Address -- |
-- 255.255.255.255 --none :: Address -- |
-- 224.0.0.0 --unspecificGroup :: Address instance Eq Port instance Ord Port instance Num Port instance Eq Address instance Eq SocketAddressInet instance Show SocketAddressInet instance Storable SocketAddressInet instance Storable Address instance Show Address instance Show Port instance Family Inet module System.Socket.Family.Inet6 data Inet6 -- | Example: -- --
-- SocketAddressInet6 loopback 8080 mempty 0 --data SocketAddressInet6 SocketAddressInet6 :: Address -> Port -> FlowInfo -> ScopeId -> SocketAddressInet6 address :: SocketAddressInet6 -> Address port :: SocketAddressInet6 -> Port flowInfo :: SocketAddressInet6 -> FlowInfo scopeId :: SocketAddressInet6 -> ScopeId -- | To avoid errors with endianess it was decided to keep this type -- abstract. -- -- Hint: Use the Storable instance if you really need to access. -- It exposes it exactly as found within an IP packet (big endian if you -- insist on interpreting it as a number). -- -- Another hint: Use getAddressInfo for parsing and suppress -- nameserver lookups: -- --
-- > getAddressInfo (Just "::1") Nothing aiNumericHost :: IO [AddressInfo SocketAddressInet6 Stream TCP]
-- [AddressInfo {
-- addressInfoFlags = AddressInfoFlags 4,
-- socketAddress = SocketAddressInet6 {address = 0000:0000:0000:0000:0000:0000:0000:0001, port = 0, flowInfo = mempty, scopeId = 0},
-- canonicalName = Nothing }]
--
data Address
newtype Port
Port :: Word16 -> Port
newtype FlowInfo
FlowInfo :: Word32 -> FlowInfo
newtype ScopeId
ScopeId :: Word32 -> ScopeId
-- | -- :: --any :: Address -- |
-- ::1 --loopback :: Address -- |
-- IPV6_V6ONLY --data V6Only V6Only :: Bool -> V6Only instance Eq Port instance Ord Port instance Num Port instance Eq Address instance Eq FlowInfo instance Ord FlowInfo instance Bits FlowInfo instance Eq ScopeId instance Ord ScopeId instance Num ScopeId instance Eq SocketAddressInet6 instance Show SocketAddressInet6 instance Eq V6Only instance Ord V6Only instance Show V6Only instance SetSocketOption V6Only instance GetSocketOption V6Only instance Storable SocketAddressInet6 instance Storable Address instance Show Address instance Show ScopeId instance Monoid FlowInfo instance Show FlowInfo instance Show Port instance Family Inet6 -- | This starts a TCP server on localhost, sends "Hello world!" -- to connecting peers and closes the connection immediately. -- --
-- {-# LANGUAGE OverloadedStrings #-}
-- module Main where
--
-- import System.Socket
-- import System.Socket.Family.Inet as Inet
-- import Data.Monoid
-- import Data.ByteString
-- import Control.Monad
-- import Control.Concurrent
-- import Control.Exception
--
-- main :: IO ()
-- main = do
-- s <- socket :: IO (Socket Inet Stream TCP)
-- setSocketOption s (ReuseAddress True)
-- bind s addr
-- listen s 5
-- forever $ do
-- (peer,_) <- accept s
-- forkIO $ do
-- sendAll peer "Hello world!" msgNoSignal `finally` close peer
-- where
-- addr = SocketAddressInet Inet.loopback 8080
--
--
-- This downloads the Haskell website and prints it to stdout. Note the
-- use of IPv4-mapped Inet6 addresses: This will work even if you
-- don't have IPv6 connectivity yet and is the preferred method when
-- writing new applications.
--
--
-- {-# LANGUAGE OverloadedStrings #-}
-- module Main where
--
-- import Data.Monoid
-- import Data.ByteString.Lazy as B
-- import System.Socket
--
-- main :: IO ()
-- main = do
-- withConnectedSocket "www.haskell.org" "80" (aiAll `mappend` aiV4Mapped) $ \sock-> do
-- let _ = sock :: Socket Inet6 Stream TCP
-- sendAll sock "GET / HTTP/1.0\r\nHost: www.haskell.org\r\n\r\n" msgNoSignal
-- x <- receiveAll sock (1024*1024*1024) mempty
-- B.putStr x
--
module System.Socket
data AddressInfo f t p
AddressInfo :: AddressInfoFlags -> SocketAddress f -> Maybe ByteString -> AddressInfo f t p
addressInfoFlags :: AddressInfo f t p -> AddressInfoFlags
socketAddress :: AddressInfo f t p -> SocketAddress f
canonicalName :: AddressInfo f t p -> Maybe ByteString
class Family f => GetAddressInfo f
getAddressInfo :: (GetAddressInfo f, Type t, Protocol p) => Maybe ByteString -> Maybe ByteString -> AddressInfoFlags -> IO [AddressInfo f t p]
-- | Maps addresss to readable host- and service names.
--
-- The operation throws AddressInfoExceptions.
--
--
-- > getNameInfo (SocketAddressInet loopback 80) mempty
-- ("localhost.localdomain","http")
--
class Family f => GetNameInfo f
getNameInfo :: GetNameInfo f => SocketAddress f -> NameInfoFlags -> IO (ByteString, ByteString)
-- | Creates a new socket.
--
-- Whereas the underlying POSIX socket operation takes 3 parameters, this
-- library encodes this information in the type variables. This rules out
-- several kinds of errors and escpecially simplifies the handling of
-- addresses (by using associated type families). Examples:
--
-- -- -- create a IPv4-UDP-datagram socket -- sock <- socket :: IO (Socket Inet Datagram UDP) -- -- create a IPv6-TCP-streaming socket -- sock6 <- socket :: IO (Socket Inet6 Stream TCP) ---- --
result <- bracket -- (socket :: IO (Socket Inet6 Stream TCP)) close $ \sock-> do -- somethingWith sock -- your computation here return -- somethingelse
-- withConnectedSocket "wwww.haskell.org" "80" (aiAll `mappend` aiV4Mapped) $ \sock-> do -- let _ = sock :: Socket Inet6 Stream TCP -- doSomethingWithSocket sock --withConnectedSocket :: (GetAddressInfo f, Type t, Protocol p) => ByteString -> ByteString -> AddressInfoFlags -> (Socket f t p -> IO a) -> IO a -- | Like send, but operates on lazy ByteStrings and -- continues until all data has been sent or an exception occured. sendAll :: Socket f Stream p -> ByteString -> MessageFlags -> IO () -- | Like receive, but operates on lazy ByteStrings and -- continues until either an empty part has been received (peer closed -- the connection) or given buffer limit has been exceeded or an -- exception occured. -- --
-- AddressInfoException "Temporary failure in name resolution" --eaiAgain :: AddressInfoException -- |
-- AddressInfoException "Bad value for ai_flags" --eaiBadFlags :: AddressInfoException -- |
-- AddressInfoException "Non-recoverable failure in name resolution" --eaiFail :: AddressInfoException -- |
-- AddressInfoException "ai_family not supported" --eaiFamily :: AddressInfoException -- |
-- AddressInfoException "Memory allocation failure" --eaiMemory :: AddressInfoException -- |
-- AddressInfoException "No such host is known" --eaiNoName :: AddressInfoException -- |
-- AddressInfoException "ai_socktype not supported" --eaiSocketType :: AddressInfoException -- |
-- AddressInfoException "Servname not supported for ai_socktype" --eaiService :: AddressInfoException -- |
-- AddressInfoException "System error" --eaiSystem :: AddressInfoException class GetSocketOption o getSocketOption :: GetSocketOption o => Socket f t p -> IO o class SetSocketOption o setSocketOption :: SetSocketOption o => Socket f t p -> o -> IO () -- |
-- SO_ERROR --data Error Error :: SocketException -> Error -- |
-- SO_REUSEADDR --data ReuseAddress ReuseAddress :: Bool -> ReuseAddress -- | Use the Monoid instance to combine several flags: -- --
-- mconcat [msgNoSignal, msgWaitAll] ---- -- Use the Bits instance to check whether a flag is set: -- --
-- if flags .&. msgEndOfRecord /= mempty then ... --newtype MessageFlags MessageFlags :: CInt -> MessageFlags -- |
-- MSG_EOR --msgEndOfRecord :: MessageFlags -- |
-- MSG_NOSIGNAL ---- -- Suppresses the generation of PIPE signals when writing to a -- socket that is no longer connected. -- -- Although this flag is POSIX, it is not available on all platforms. Try -- --
-- msgNoSignal /= mempty ---- -- in order to check whether this flag is defined on a certain platform. -- It is safe to just use this constant even if it might not have effect -- on a certain target platform. The platform independence of this flag -- is therefore fulfilled to some extent. -- -- Some more explanation on the platform specific behaviour: -- --
-- MSG_OOB --msgOutOfBand :: MessageFlags -- |
-- MSG_WAITALL --msgWaitAll :: MessageFlags -- | Use the Monoid instance to combine several flags: -- --
-- mconcat [aiAddressConfig, aiV4Mapped] --newtype AddressInfoFlags AddressInfoFlags :: CInt -> AddressInfoFlags -- | AI_ADDRCONFIG: aiAddressConfig :: AddressInfoFlags -- | AI_ALL: Return both IPv4 (as mapped -- SocketAddressInet6) and IPv6 addresses when aiV4Mapped -- is set independent of whether IPv6 addresses exist for this name. aiAll :: AddressInfoFlags -- | AI_CANONNAME: aiCanonicalName :: AddressInfoFlags -- | AI_NUMERICHOST: aiNumericHost :: AddressInfoFlags -- | AI_NUMERICSERV: aiNumericService :: AddressInfoFlags -- | AI_PASSIVE: aiPassive :: AddressInfoFlags -- | AI_V4MAPPED: Return mapped IPv4 addresses if no IPv6 -- addresses could be found or if aiAll flag is set. aiV4Mapped :: AddressInfoFlags -- | Use the Monoid instance to combine several flags: -- --
-- mconcat [niNameRequired, niNoFullyQualifiedDomainName] --newtype NameInfoFlags NameInfoFlags :: CInt -> NameInfoFlags -- | NI_NAMEREQD: Throw an exception if the hostname cannot be -- determined. niNameRequired :: NameInfoFlags -- | NI_DGRAM: Service is datagram based (i.e. UDP) rather -- than stream based (i.e. TCP). niDatagram :: NameInfoFlags -- | NI_NOFQDN: Return only the hostname part of the fully -- qualified domain name for local hosts. niNoFullyQualifiedDomainName :: NameInfoFlags -- | NI_NUMERICHOST: Return the numeric form of the host address. niNumericHost :: NameInfoFlags -- | NI_NUMERICSERV: Return the numeric form of the service -- address. niNumericService :: NameInfoFlags