-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Low-level networking interface -- -- This package provides a low-level networking interface. -- --
-- library -- build-depends: network-uri-flag --@package network @version 3.0.0.0 -- | A module containing semi-public Socket internals. Modules which -- extend the Socket module will need to use this module while -- ideally most users will be able to make do with the public interface. module Network.Socket.Internal -- | Throw an IOError corresponding to the current socket error. throwSocketError :: String -> IO a -- | Like throwSocketError, but the error code is supplied as an -- argument. -- -- On Windows, do not use errno. Use a system error code instead. throwSocketErrorCode :: String -> CInt -> IO a -- | Throw an IOError corresponding to the current socket error if -- the IO action returns a result of -1. Discards the result of -- the IO action after error handling. throwSocketErrorIfMinus1_ :: (Eq a, Num a) => String -> IO a -> IO () -- | Throw an IOError corresponding to the current socket error if -- the IO action returns a result of -1, but retries in case of -- an interrupted operation. throwSocketErrorIfMinus1Retry :: (Eq a, Num a) => String -> IO a -> IO a -- | Throw an IOError corresponding to the current socket error if -- the IO action returns a result of -1, but retries in case of -- an interrupted operation. Discards the result of the IO action after -- error handling. throwSocketErrorIfMinus1Retry_ :: (Eq a, Num a) => String -> IO a -> IO () -- | Throw an IOError corresponding to the current socket error if -- the IO action returns a result of -1, but retries in case of -- an interrupted operation. Checks for operations that would block and -- executes an alternative action before retrying in that case. throwSocketErrorIfMinus1RetryMayBlock :: (Eq a, Num a) => String -> IO b -> IO a -> IO a -- | Like throwSocketErrorIfMinus1Retry, but if the action fails -- with EWOULDBLOCK or similar, wait for the socket to be -- read-ready, and try again. throwSocketErrorWaitRead :: (Eq a, Num a) => Socket -> String -> IO a -> IO a -- | Like throwSocketErrorIfMinus1Retry, but if the action fails -- with EWOULDBLOCK or similar, wait for the socket to be -- write-ready, and try again. throwSocketErrorWaitWrite :: (Eq a, Num a) => Socket -> String -> IO a -> IO a -- | With older versions of the network library (version 2.6.0.2 -- or earlier) on Windows operating systems, the networking subsystem -- must be initialised using withSocketsDo before any networking -- operations can be used. eg. -- --
-- main = withSocketsDo $ do {...}
--
--
-- It is fine to nest calls to withSocketsDo, and to perform
-- networking operations after withSocketsDo has returned.
--
-- withSocketsDo is not necessary for the current network library.
-- However, for compatibility with older versions on Windows, it is good
-- practice to always call withSocketsDo (it's very cheap).
withSocketsDo :: IO a -> IO a
-- | Zero a structure.
zeroMemory :: Ptr a -> CSize -> IO ()
-- | This module provides access to the BSD socket interface. This
-- module is generally more efficient than the String based
-- network functions in Socket. For detailed documentation,
-- consult your favorite POSIX socket reference. All functions
-- communicate failures by converting the error number to IOError.
--
-- This module is made to be imported with Socket like so:
--
-- -- import Network.Socket -- import Network.Socket.ByteString --module Network.Socket.ByteString -- | Send data to the socket. The socket must be connected to a remote -- socket. Returns the number of bytes sent. Applications are responsible -- for ensuring that all data has been sent. -- -- Sending data to closed socket may lead to undefined behaviour. send :: Socket -> ByteString -> IO Int -- | Send data to the socket. The socket must be connected to a remote -- socket. Unlike send, this function continues to send data until -- either all data has been sent or an error occurs. On error, an -- exception is raised, and there is no way to determine how much data, -- if any, was successfully sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendAll :: Socket -> ByteString -> IO () -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Returns the number of -- bytes sent. Applications are responsible for ensuring that all data -- has been sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendTo :: Socket -> ByteString -> SockAddr -> IO Int -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Unlike sendTo, -- this function continues to send data until either all data has been -- sent or an error occurs. On error, an exception is raised, and there -- is no way to determine how much data, if any, was successfully sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendAllTo :: Socket -> ByteString -> SockAddr -> IO () -- | Send data to the socket. The socket must be in a connected state. The -- data is sent as if the parts have been concatenated. This function -- continues to send data until either all data has been sent or an error -- occurs. On error, an exception is raised, and there is no way to -- determine how much data, if any, was successfully sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendMany :: Socket -> [ByteString] -> IO () -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. The data is sent as if -- the parts have been concatenated. This function continues to send data -- until either all data has been sent or an error occurs. On error, an -- exception is raised, and there is no way to determine how much data, -- if any, was successfully sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendManyTo :: Socket -> [ByteString] -> SockAddr -> IO () -- | Receive data from the socket. The socket must be in a connected state. -- This function may return fewer bytes than specified. If the message is -- longer than the specified length, it may be discarded depending on the -- type of socket. This function may block until a message arrives. -- -- Considering hardware and network realities, the maximum number of -- bytes to receive should be a small power of 2, e.g., 4096. -- -- For TCP sockets, a zero length return value means the peer has closed -- its half side of the connection. -- -- Receiving data from closed socket may lead to undefined behaviour. recv :: Socket -> Int -> IO ByteString -- | Receive data from the socket. The socket need not be in a connected -- state. Returns (bytes, address) where bytes is a -- ByteString representing the data received and address -- is a SockAddr representing the address of the sending socket. -- -- Receiving data from closed socket may lead to undefined behaviour. recvFrom :: Socket -> Int -> IO (ByteString, SockAddr) -- | This module provides extensible APIs for socket addresses. module Network.Socket.Address -- | The core typeclass to unify socket addresses. class SocketAddress sa sizeOfSocketAddress :: SocketAddress sa => sa -> Int peekSocketAddress :: SocketAddress sa => Ptr sa -> IO sa pokeSocketAddress :: SocketAddress sa => Ptr a -> sa -> IO () -- | Getting peer's socket address. getPeerName :: SocketAddress sa => Socket -> IO sa -- | Getting my socket address. getSocketName :: SocketAddress sa => Socket -> IO sa -- | Connect to a remote socket at address. connect :: SocketAddress sa => Socket -> sa -> IO () -- | Bind the socket to an address. The socket must not already be bound. -- The Family passed to bind must be the same as that -- passed to socket. If the special port number defaultPort -- is passed then the system assigns the next available use port. bind :: SocketAddress sa => Socket -> sa -> IO () -- | Accept a connection. The socket must be bound to an address and -- listening for connections. The return value is a pair (conn, -- address) where conn is a new socket object usable to -- send and receive data on the connection, and address is the -- address bound to the socket on the other end of the connection. On -- Unix, FD_CLOEXEC is set to the new Socket. accept :: SocketAddress sa => Socket -> IO (Socket, sa) -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Returns the number of -- bytes sent. Applications are responsible for ensuring that all data -- has been sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendTo :: SocketAddress sa => Socket -> ByteString -> sa -> IO Int -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Unlike sendTo, -- this function continues to send data until either all data has been -- sent or an error occurs. On error, an exception is raised, and there -- is no way to determine how much data, if any, was successfully sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendAllTo :: SocketAddress sa => Socket -> ByteString -> sa -> IO () -- | Receive data from the socket. The socket need not be in a connected -- state. Returns (bytes, address) where bytes is a -- ByteString representing the data received and address -- is a SockAddr representing the address of the sending socket. -- -- If the first return value is zero, it means EOF. -- -- Receiving data from closed socket may lead to undefined behaviour. recvFrom :: SocketAddress sa => Socket -> Int -> IO (ByteString, sa) -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Returns the number of -- bytes sent. Applications are responsible for ensuring that all data -- has been sent. sendBufTo :: SocketAddress sa => Socket -> Ptr a -> Int -> sa -> IO Int -- | Receive data from the socket, writing it into buffer instead of -- creating a new string. The socket need not be in a connected state. -- Returns (nbytes, address) where nbytes is the number -- of bytes received and address is a SockAddr -- representing the address of the sending socket. -- -- If the first return value is zero, it means EOF. -- -- For Stream sockets, the second return value would be invalid. -- -- NOTE: blocking on Windows unless you compile with -threaded (see GHC -- ticket #1129) recvBufFrom :: SocketAddress sa => Socket -> Ptr a -> Int -> IO (Int, sa) -- | This is the main module of the network package supposed to be used -- with either Network.Socket.ByteString or -- Network.Socket.ByteString.Lazy for sending/receiving. -- -- Here are two minimal example programs using the TCP/IP protocol: a -- server that echoes all data that it receives back (servicing only one -- client) and a client using it. -- --
-- -- Echo server program
-- module Main (main) where
--
-- import Control.Concurrent (forkFinally)
-- import qualified Control.Exception as E
-- import Control.Monad (unless, forever, void)
-- import qualified Data.ByteString as S
-- import Network.Socket
-- import Network.Socket.ByteString (recv, sendAll)
--
-- main :: IO ()
-- main = withSocketsDo $ do
-- addr <- resolve "3000"
-- E.bracket (open addr) close loop
-- where
-- resolve port = do
-- let hints = defaultHints {
-- addrFlags = [AI_PASSIVE]
-- , addrSocketType = Stream
-- }
-- addr:_ <- getAddrInfo (Just hints) Nothing (Just port)
-- return addr
-- open addr = do
-- sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
-- setSocketOption sock ReuseAddr 1
-- -- If the prefork technique is not used,
-- -- set CloseOnExec for the security reasons.
-- fd <- fdSocket sock
-- setCloseOnExecIfNeeded fd
-- bind sock (addrAddress addr)
-- listen sock 10
-- return sock
-- loop sock = forever $ do
-- (conn, peer) <- accept sock
-- putStrLn $ "Connection from " ++ show peer
-- void $ forkFinally (talk conn) (\_ -> close conn)
-- talk conn = do
-- msg <- recv conn 1024
-- unless (S.null msg) $ do
-- sendAll conn msg
-- talk conn
--
--
--
-- {-# LANGUAGE OverloadedStrings #-}
-- -- Echo client program
-- module Main (main) where
--
-- import qualified Control.Exception as E
-- import qualified Data.ByteString.Char8 as C
-- import Network.Socket
-- import Network.Socket.ByteString (recv, sendAll)
--
-- main :: IO ()
-- main = withSocketsDo $ do
-- addr <- resolve "127.0.0.1" "3000"
-- E.bracket (open addr) close talk
-- where
-- resolve host port = do
-- let hints = defaultHints { addrSocketType = Stream }
-- addr:_ <- getAddrInfo (Just hints) (Just host) (Just port)
-- return addr
-- open addr = do
-- sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
-- connect sock $ addrAddress addr
-- return sock
-- talk sock = do
-- sendAll sock "Hello, world!"
-- msg <- recv sock 1024
-- putStr "Received: "
-- C.putStrLn msg
--
--
-- The proper programming model is that one Socket is handled by a
-- single thread. If multiple threads use one Socket concurrently,
-- unexpected things would happen. There is one exception for multiple
-- threads vs a single Socket: one thread reads data from a
-- Socket only and the other thread writes data to the
-- Socket only.
module Network.Socket
-- | With older versions of the network library (version 2.6.0.2
-- or earlier) on Windows operating systems, the networking subsystem
-- must be initialised using withSocketsDo before any networking
-- operations can be used. eg.
--
--
-- main = withSocketsDo $ do {...}
--
--
-- It is fine to nest calls to withSocketsDo, and to perform
-- networking operations after withSocketsDo has returned.
--
-- withSocketsDo is not necessary for the current network library.
-- However, for compatibility with older versions on Windows, it is good
-- practice to always call withSocketsDo (it's very cheap).
withSocketsDo :: IO a -> IO a
-- | Resolve a host or service name to one or more addresses. The
-- AddrInfo values that this function returns contain
-- SockAddr values that you can pass directly to connect
-- or bind.
--
-- This function is protocol independent. It can return both IPv4 and
-- IPv6 address information.
--
-- The AddrInfo argument specifies the preferred query behaviour,
-- socket options, or protocol. You can override these conveniently using
-- Haskell's record update syntax on defaultHints, for example as
-- follows:
--
--
-- >>> let hints = defaultHints { addrFlags = [AI_NUMERICHOST], addrSocketType = Stream }
--
--
-- You must provide a Just value for at least one of the
-- HostName or ServiceName arguments. HostName can
-- be either a numeric network address (dotted quad for IPv4,
-- colon-separated hex for IPv6) or a hostname. In the latter case, its
-- addresses will be looked up unless AI_NUMERICHOST is specified
-- as a hint. If you do not provide a HostName value and do
-- not set AI_PASSIVE as a hint, network addresses in the result
-- will contain the address of the loopback interface.
--
-- If the query fails, this function throws an IO exception instead of
-- returning an empty list. Otherwise, it returns a non-empty list of
-- AddrInfo values.
--
-- There are several reasons why a query might result in several values.
-- For example, the queried-for host could be multihomed, or the service
-- might be available via several protocols.
--
-- Note: the order of arguments is slightly different to that defined for
-- getaddrinfo in RFC 2553. The AddrInfo parameter comes
-- first to make partial application easier.
--
-- -- >>> addr:_ <- getAddrInfo (Just hints) (Just "127.0.0.1") (Just "http") -- -- >>> addrAddress addr -- 127.0.0.1:80 --getAddrInfo :: Maybe AddrInfo -> Maybe HostName -> Maybe ServiceName -> IO [AddrInfo] -- | Either a host name e.g., "haskell.org" or a numeric host -- address string consisting of a dotted decimal IPv4 address or an IPv6 -- address e.g., "192.168.0.1". type HostName = String -- | Either a service name e.g., "http" or a numeric port number. type ServiceName = String data AddrInfo AddrInfo :: [AddrInfoFlag] -> Family -> SocketType -> ProtocolNumber -> SockAddr -> Maybe String -> AddrInfo [addrFlags] :: AddrInfo -> [AddrInfoFlag] [addrFamily] :: AddrInfo -> Family [addrSocketType] :: AddrInfo -> SocketType [addrProtocol] :: AddrInfo -> ProtocolNumber [addrAddress] :: AddrInfo -> SockAddr [addrCanonName] :: AddrInfo -> Maybe String -- | Default hints for address lookup with getAddrInfo. The values -- of the addrAddress and addrCanonName fields are -- undefined, and are never inspected by getAddrInfo. -- --
-- >>> addrFlags defaultHints -- [] -- -- >>> addrFamily defaultHints -- AF_UNSPEC -- -- >>> addrSocketType defaultHints -- NoSocketType -- -- >>> addrProtocol defaultHints -- 0 --defaultHints :: AddrInfo -- | Flags that control the querying behaviour of getAddrInfo. For -- more information, see -- https://tools.ietf.org/html/rfc3493#page-25 data AddrInfoFlag -- | The list of returned AddrInfo values will only contain IPv4 -- addresses if the local system has at least one IPv4 interface -- configured, and likewise for IPv6. (Only some platforms support this.) AI_ADDRCONFIG :: AddrInfoFlag -- | If AI_ALL is specified, return all matching IPv6 and IPv4 -- addresses. Otherwise, this flag has no effect. (Only some platforms -- support this.) AI_ALL :: AddrInfoFlag -- | The addrCanonName field of the first returned AddrInfo -- will contain the "canonical name" of the host. AI_CANONNAME :: AddrInfoFlag -- | The HostName argument must be a numeric address in -- string form, and network name lookups will not be attempted. AI_NUMERICHOST :: AddrInfoFlag -- | The ServiceName argument must be a port number in string -- form, and service name lookups will not be attempted. (Only some -- platforms support this.) AI_NUMERICSERV :: AddrInfoFlag -- | If no HostName value is provided, the network address in each -- SockAddr will be left as a "wild card". This is useful for -- server applications that will accept connections from any client. AI_PASSIVE :: AddrInfoFlag -- | If an IPv6 lookup is performed, and no IPv6 addresses are found, -- IPv6-mapped IPv4 addresses will be returned. (Only some platforms -- support this.) AI_V4MAPPED :: AddrInfoFlag -- | Indicate whether the given AddrInfoFlag will have any effect on -- this system. addrInfoFlagImplemented :: AddrInfoFlag -> Bool -- | Connect to a remote socket at address. connect :: Socket -> SockAddr -> IO () -- | Bind the socket to an address. The socket must not already be bound. -- The Family passed to bind must be the same as that -- passed to socket. If the special port number -- defaultPort is passed then the system assigns the next -- available use port. bind :: Socket -> SockAddr -> IO () -- | Listen for connections made to the socket. The second argument -- specifies the maximum number of queued connections and should be at -- least 1; the maximum value is system-dependent (usually 5). listen :: Socket -> Int -> IO () -- | Accept a connection. The socket must be bound to an address and -- listening for connections. The return value is a pair (conn, -- address) where conn is a new socket object usable to -- send and receive data on the connection, and address is the -- address bound to the socket on the other end of the connection. On -- Unix, FD_CLOEXEC is set to the new Socket. accept :: Socket -> IO (Socket, SockAddr) -- | Close the socket. This function does not throw exceptions even if the -- underlying system call returns errors. -- -- Sending data to or receiving data from closed socket may lead to -- undefined behaviour. -- -- If multiple threads use the same socket and one uses fdSocket -- and the other use close, unexpected behavior may happen. For -- more information, please refer to the documentation of -- fdSocket. close :: Socket -> IO () -- | Close the socket. This function throws exceptions if the underlying -- system call returns errors. -- -- Sending data to or receiving data from closed socket may lead to -- undefined behaviour. close' :: Socket -> IO () -- | Shut down one or both halves of the connection, depending on the -- second argument to the function. If the second argument is -- ShutdownReceive, further receives are disallowed. If it is -- ShutdownSend, further sends are disallowed. If it is -- ShutdownBoth, further sends and receives are disallowed. shutdown :: Socket -> ShutdownCmd -> IO () data ShutdownCmd ShutdownReceive :: ShutdownCmd ShutdownSend :: ShutdownCmd ShutdownBoth :: ShutdownCmd -- | Socket options for use with setSocketOption and -- getSocketOption. -- -- The existence of a constructor does not imply that the relevant option -- is supported on your system: see isSupportedSocketOption data SocketOption -- | SO_DEBUG Debug :: SocketOption -- | SO_REUSEADDR ReuseAddr :: SocketOption -- | SO_TYPE Type :: SocketOption -- | SO_ERROR SoError :: SocketOption -- | SO_DONTROUTE DontRoute :: SocketOption -- | SO_BROADCAST Broadcast :: SocketOption -- | SO_SNDBUF SendBuffer :: SocketOption -- | SO_RCVBUF RecvBuffer :: SocketOption -- | SO_KEEPALIVE KeepAlive :: SocketOption -- | SO_OOBINLINE OOBInline :: SocketOption -- | IP_TTL TimeToLive :: SocketOption -- | TCP_MAXSEG MaxSegment :: SocketOption -- | TCP_NODELAY NoDelay :: SocketOption -- | TCP_CORK Cork :: SocketOption -- | SO_LINGER: timeout in seconds, 0 means disabling/disabled. Linger :: SocketOption -- | SO_REUSEPORT ReusePort :: SocketOption -- | SO_RCVLOWAT RecvLowWater :: SocketOption -- | SO_SNDLOWAT SendLowWater :: SocketOption -- | SO_RCVTIMEO: this does not work at this moment. RecvTimeOut :: SocketOption -- | SO_SNDTIMEO: this does not work at this moment. SendTimeOut :: SocketOption -- | SO_USELOOPBACK UseLoopBack :: SocketOption -- | TCP_USER_TIMEOUT UserTimeout :: SocketOption -- | IPV6_V6ONLY: don't use this on OpenBSD. IPv6Only :: SocketOption CustomSockOpt :: (CInt, CInt) -> SocketOption -- | Does the SocketOption exist on this system? isSupportedSocketOption :: SocketOption -> Bool -- | Get a socket option that gives an Int value. There is currently no API -- to get e.g. the timeval socket options getSocketOption :: Socket -> SocketOption -> IO Int -- | Set a socket option that expects an Int value. There is currently no -- API to set e.g. the timeval socket options setSocketOption :: Socket -> SocketOption -> Int -> IO () -- | Basic type for a socket. data Socket -- | Create a new socket using the given address family, socket type and -- protocol number. The address family is usually AF_INET, -- AF_INET6, or AF_UNIX. The socket type is usually -- Stream or Datagram. The protocol number is usually -- defaultProtocol. If AF_INET6 is used and the socket type -- is Stream or Datagram, the IPv6Only socket option -- is set to 0 so that both IPv4 and IPv6 can be handled with one socket. -- --
-- >>> import Network.Socket
--
-- >>> let hints = defaultHints { addrFlags = [AI_NUMERICHOST, AI_NUMERICSERV], addrSocketType = Stream }
--
-- >>> addr:_ <- getAddrInfo (Just hints) (Just "127.0.0.1") (Just "5000")
--
-- >>> sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
--
-- >>> Network.Socket.bind sock (addrAddress addr)
--
-- >>> getSocketName sock
-- 127.0.0.1:5000
--
socket :: Family -> SocketType -> ProtocolNumber -> IO Socket
-- | Getting a file descriptor from a socket.
--
-- If a Socket is shared with multiple threads and one uses
-- fdSocket, unexpected issues may happen. Consider the following
-- scenario:
--
-- 1) Thread A acquires a Fd from Socket by
-- fdSocket.
--
-- 2) Thread B close the Socket.
--
-- 3) Thread C opens a new Socket. Unfortunately it gets the same
-- Fd number which thread A is holding.
--
-- In this case, it is safer for Thread A to clone Fd by
-- dup. But this would still suffer from a rase condition between
-- fdSocket and close.
fdSocket :: Socket -> IO CInt
-- | Creating a socket from a file descriptor.
mkSocket :: CInt -> IO Socket
-- | Turns a Socket into an Handle. By default, the new handle is
-- unbuffered. Use hSetBuffering to change the buffering.
--
-- Note that since a Handle is automatically closed by a finalizer
-- when it is no longer referenced, you should avoid doing any more
-- operations on the Socket after calling socketToHandle.
-- To close the Socket after socketToHandle, call
-- hClose on the Handle.
socketToHandle :: Socket -> IOMode -> IO Handle
-- | Socket Types.
--
-- The existence of a constructor does not necessarily imply that that
-- socket type is supported on your system: see
-- isSupportedSocketType.
data SocketType
-- | 0, used in getAddrInfo hints, for example
NoSocketType :: SocketType
-- | SOCK_STREAM
Stream :: SocketType
-- | SOCK_DGRAM
Datagram :: SocketType
-- | SOCK_RAW
Raw :: SocketType
-- | SOCK_RDM
RDM :: SocketType
-- | SOCK_SEQPACKET
SeqPacket :: SocketType
-- | Does the SOCK_ constant corresponding to the given SocketType exist on
-- this system?
isSupportedSocketType :: SocketType -> Bool
-- | Address families.
--
-- A constructor being present here does not mean it is supported by the
-- operating system: see isSupportedFamily.
data Family
-- | unspecified
AF_UNSPEC :: Family
-- | UNIX-domain
AF_UNIX :: Family
-- | Internet Protocol version 4
AF_INET :: Family
-- | Internet Protocol version 6
AF_INET6 :: Family
-- | Arpanet imp addresses
AF_IMPLINK :: Family
-- | pup protocols: e.g. BSP
AF_PUP :: Family
-- | mit CHAOS protocols
AF_CHAOS :: Family
-- | XEROX NS protocols
AF_NS :: Family
-- | nbs protocols
AF_NBS :: Family
-- | european computer manufacturers
AF_ECMA :: Family
-- | datakit protocols
AF_DATAKIT :: Family
-- | CCITT protocols, X.25 etc
AF_CCITT :: Family
-- | IBM SNA
AF_SNA :: Family
-- | DECnet
AF_DECnet :: Family
-- | Direct data link interface
AF_DLI :: Family
-- | LAT
AF_LAT :: Family
-- | NSC Hyperchannel
AF_HYLINK :: Family
-- | Apple Talk
AF_APPLETALK :: Family
-- | Internal Routing Protocol (aka AF_NETLINK)
AF_ROUTE :: Family
-- | NetBios-style addresses
AF_NETBIOS :: Family
-- | Network Interface Tap
AF_NIT :: Family
-- | IEEE 802.2, also ISO 8802
AF_802 :: Family
-- | ISO protocols
AF_ISO :: Family
-- | umbrella of all families used by OSI
AF_OSI :: Family
-- | DNA Network Management
AF_NETMAN :: Family
-- | CCITT X.25
AF_X25 :: Family
-- | AX25
AF_AX25 :: Family
-- | AFI
AF_OSINET :: Family
-- | US Government OSI
AF_GOSSIP :: Family
-- | Novell Internet Protocol
AF_IPX :: Family
-- | eXpress Transfer Protocol (no AF)
Pseudo_AF_XTP :: Family
-- | Common Trace Facility
AF_CTF :: Family
-- | Wide Area Network protocols
AF_WAN :: Family
-- | SGI Data Link for DLPI
AF_SDL :: Family
-- | Netware
AF_NETWARE :: Family
-- | NDD
AF_NDD :: Family
-- | Debugging use only
AF_INTF :: Family
-- | connection-oriented IP, aka ST II
AF_COIP :: Family
-- | Computer Network Technology
AF_CNT :: Family
-- | Help Identify RTIP packets
Pseudo_AF_RTIP :: Family
-- | Help Identify PIP packets
Pseudo_AF_PIP :: Family
-- | Simple Internet Protocol
AF_SIP :: Family
-- | Integrated Services Digital Network
AF_ISDN :: Family
-- | Internal key-management function
Pseudo_AF_KEY :: Family
-- | native ATM access
AF_NATM :: Family
-- | ARP (RFC 826)
AF_ARP :: Family
-- | Used by BPF to not rewrite hdrs in iface output
Pseudo_AF_HDRCMPLT :: Family
-- | ENCAP
AF_ENCAP :: Family
-- | Link layer interface
AF_LINK :: Family
-- | Link layer interface
AF_RAW :: Family
-- | raw interface
AF_RIF :: Family
-- | Amateur radio NetROM
AF_NETROM :: Family
-- | multiprotocol bridge
AF_BRIDGE :: Family
-- | ATM PVCs
AF_ATMPVC :: Family
-- | Amateur Radio X.25 PLP
AF_ROSE :: Family
-- | Netbeui 802.2LLC
AF_NETBEUI :: Family
-- | Security callback pseudo AF
AF_SECURITY :: Family
-- | Packet family
AF_PACKET :: Family
-- | Ash
AF_ASH :: Family
-- | Acorn Econet
AF_ECONET :: Family
-- | ATM SVCs
AF_ATMSVC :: Family
-- | IRDA sockets
AF_IRDA :: Family
-- | PPPoX sockets
AF_PPPOX :: Family
-- | Wanpipe API sockets
AF_WANPIPE :: Family
-- | bluetooth sockets
AF_BLUETOOTH :: Family
-- | Controller Area Network
AF_CAN :: Family
-- | Does the AF_ constant corresponding to the given family exist on this
-- system?
isSupportedFamily :: Family -> Bool
-- | Converting Family to CInt.
packFamily :: Family -> CInt
-- | Converting CInt to Family.
unpackFamily :: CInt -> Family
-- | Protocl number.
type ProtocolNumber = CInt
-- | This is the default protocol for a given service.
--
-- -- >>> defaultProtocol -- 0 --defaultProtocol :: ProtocolNumber -- | Socket addresses. The existence of a constructor does not necessarily -- imply that that socket address type is supported on your system: see -- isSupportedSockAddr. data SockAddr SockAddrInet :: !PortNumber -> !HostAddress -> SockAddr SockAddrInet6 :: !PortNumber -> !FlowInfo -> !HostAddress6 -> !ScopeID -> SockAddr -- | String must be a list of 0-255 values and its length should be -- less than 104. SockAddrUnix :: String -> SockAddr -- | Is the socket address type supported on this system? isSupportedSockAddr :: SockAddr -> Bool -- | Getting peer's SockAddr. getPeerName :: Socket -> IO SockAddr -- | Getting my SockAddr. getSocketName :: Socket -> IO SockAddr -- | The raw network byte order number is read using host byte order. -- Therefore on little-endian architectures the byte order is swapped. -- For example 127.0.0.1 is represented as 0x0100007f -- on little-endian hosts and as 0x7f000001 on big-endian hosts. -- -- For direct manipulation prefer hostAddressToTuple and -- tupleToHostAddress. type HostAddress = Word32 -- | Converts HostAddress to representation-independent IPv4 -- quadruple. For example for 127.0.0.1 the function will return -- (0x7f, 0, 0, 1) regardless of host endianness. hostAddressToTuple :: HostAddress -> (Word8, Word8, Word8, Word8) -- | Converts IPv4 quadruple to HostAddress. tupleToHostAddress :: (Word8, Word8, Word8, Word8) -> HostAddress -- | Independent of endianness. For example ::1 is stored as -- (0, 0, 0, 1). -- -- For direct manipulation prefer hostAddress6ToTuple and -- tupleToHostAddress6. type HostAddress6 = (Word32, Word32, Word32, Word32) -- | Converts HostAddress6 to representation-independent IPv6 -- octuple. hostAddress6ToTuple :: HostAddress6 -> (Word16, Word16, Word16, Word16, Word16, Word16, Word16, Word16) -- | Converts IPv6 octuple to HostAddress6. tupleToHostAddress6 :: (Word16, Word16, Word16, Word16, Word16, Word16, Word16, Word16) -> HostAddress6 -- | Flow information. type FlowInfo = Word32 -- | Scope identifier. type ScopeID = Word32 -- | Returns the index corresponding to the interface name. -- -- Since 2.7.0.0. ifNameToIndex :: String -> IO (Maybe Int) -- | Returns the interface name corresponding to the index. -- -- Since 2.7.0.0. ifIndexToName :: Int -> IO (Maybe String) -- | Port number. Use the Num instance (i.e. use a literal) to -- create a PortNumber value. -- --
-- >>> 1 :: PortNumber -- 1 -- -- >>> read "1" :: PortNumber -- 1 -- -- >>> show (12345 :: PortNumber) -- "12345" -- -- >>> 50000 < (51000 :: PortNumber) -- True -- -- >>> 50000 < (52000 :: PortNumber) -- True -- -- >>> 50000 + (10000 :: PortNumber) -- 60000 --data PortNumber -- | Default port number. -- --
-- >>> defaultPort -- 0 --defaultPort :: PortNumber -- | Getting the port of socket. socketPortSafe :: Socket -> IO (Maybe PortNumber) -- | Getting the port of socket. IOError is thrown if a port is not -- available. socketPort :: Socket -> IO PortNumber -- | Whether or not UNIX-domain sockets are available. -- -- Since 2.7.0.0. isUnixDomainSocketAvailable :: Bool -- | Build a pair of connected socket objects. For portability, use this -- function in the case where isUnixDomainSocketAvailable is -- True and specify AF_UNIX to the first argument. socketPair :: Family -> SocketType -> ProtocolNumber -> IO (Socket, Socket) -- | Send a file descriptor over a UNIX-domain socket. Use this function in -- the case where isUnixDomainSocketAvailable is True. sendFd :: Socket -> CInt -> IO () -- | Receive a file descriptor over a UNIX-domain socket. Note that the -- resulting file descriptor may have to be put into non-blocking mode in -- order to be used safely. See setNonBlockIfNeeded. Use this -- function in the case where isUnixDomainSocketAvailable is -- True. recvFd :: Socket -> IO CInt -- | Getting process ID, user ID and group ID for UNIX-domain sockets. -- -- This is implemented with SO_PEERCRED on Linux and getpeereid() on BSD -- variants. Unfortunately, on some BSD variants getpeereid() returns -- unexpected results, rather than an error, for AF_INET sockets. It is -- the user's responsibility to make sure that the socket is a -- UNIX-domain socket. Also, on some BSD variants, getpeereid() does not -- return credentials for sockets created via socketPair, only -- separately created and then explicitly connected UNIX-domain sockets -- work on such systems. -- -- Since 2.7.0.0. getPeerCredential :: Socket -> IO (Maybe CUInt, Maybe CUInt, Maybe CUInt) -- | Resolve an address to a host or service name. This function is -- protocol independent. The list of NameInfoFlag values controls -- query behaviour. -- -- If a host or service's name cannot be looked up, then the numeric form -- of the address or service will be returned. -- -- If the query fails, this function throws an IO exception. -- --
-- >>> addr:_ <- getAddrInfo (Just defaultHints) (Just "127.0.0.1") (Just "http") -- -- >>> getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True $ addrAddress addr -- (Just "127.0.0.1",Just "80") --getNameInfo :: [NameInfoFlag] -> Bool -> Bool -> SockAddr -> IO (Maybe HostName, Maybe ServiceName) -- | Flags that control the querying behaviour of getNameInfo. For -- more information, see -- https://tools.ietf.org/html/rfc3493#page-30 data NameInfoFlag -- | Resolve a datagram-based service name. This is required only for the -- few protocols that have different port numbers for their -- datagram-based versions than for their stream-based versions. NI_DGRAM :: NameInfoFlag -- | If the hostname cannot be looked up, an IO error is thrown. NI_NAMEREQD :: NameInfoFlag -- | If a host is local, return only the hostname part of the FQDN. NI_NOFQDN :: NameInfoFlag -- | The name of the host is not looked up. Instead, a numeric -- representation of the host's address is returned. For an IPv4 address, -- this will be a dotted-quad string. For IPv6, it will be -- colon-separated hexadecimal. NI_NUMERICHOST :: NameInfoFlag -- | The name of the service is not looked up. Instead, a numeric -- representation of the service is returned. NI_NUMERICSERV :: NameInfoFlag -- | Set the nonblocking flag on Unix. On Windows, nothing is done. -- -- Since 2.7.0.0. setCloseOnExecIfNeeded :: CInt -> IO () -- | Get the close_on_exec flag. On Windows, this function always returns -- False. -- -- Since 2.7.0.0. getCloseOnExec :: CInt -> IO Bool -- | Set the nonblocking flag on Unix. On Windows, nothing is done. setNonBlockIfNeeded :: CInt -> IO () -- | Get the close_on_exec flag. On Windows, this function always returns -- False. -- -- Since 2.7.0.0. getNonBlock :: CInt -> IO Bool -- | Send data to the socket. The socket must be connected to a remote -- socket. Returns the number of bytes sent. Applications are responsible -- for ensuring that all data has been sent. -- -- Sending data to closed socket may lead to undefined behaviour. sendBuf :: Socket -> Ptr Word8 -> Int -> IO Int -- | Receive data from the socket. The socket must be in a connected state. -- This function may return fewer bytes than specified. If the message is -- longer than the specified length, it may be discarded depending on the -- type of socket. This function may block until a message arrives. -- -- Considering hardware and network realities, the maximum number of -- bytes to receive should be a small power of 2, e.g., 4096. -- -- The return value is the length of received data. Zero means EOF. -- Historical note: Version 2.8.x.y or earlier, an EOF error was thrown. -- This was changed in version 3.0. -- -- Receiving data from closed socket may lead to undefined behaviour. recvBuf :: Socket -> Ptr Word8 -> Int -> IO Int -- | Send data to the socket. The recipient can be specified explicitly, so -- the socket need not be in a connected state. Returns the number of -- bytes sent. Applications are responsible for ensuring that all data -- has been sent. sendBufTo :: Socket -> Ptr a -> Int -> SockAddr -> IO Int -- | Receive data from the socket, writing it into buffer instead of -- creating a new string. The socket need not be in a connected state. -- Returns (nbytes, address) where nbytes is the number -- of bytes received and address is a SockAddr -- representing the address of the sending socket. -- -- If the first return value is zero, it means EOF. -- -- For Stream sockets, the second return value would be invalid. -- -- NOTE: blocking on Windows unless you compile with -threaded (see GHC -- ticket #1129) recvBufFrom :: Socket -> Ptr a -> Int -> IO (Int, SockAddr) -- | This is the value of SOMAXCONN, typically 128. 128 is good enough for -- normal network servers but is too small for high performance servers. maxListenQueue :: Int -- | This module provides access to the BSD socket interface. This -- module is generally more efficient than the String based -- network functions in Socket. For detailed documentation, -- consult your favorite POSIX socket reference. All functions -- communicate failures by converting the error number to IOError. -- -- This module is made to be imported with Socket like so: -- --
-- import Network.Socket -- import Network.Socket.ByteString.Lazy -- import Prelude hiding (getContents) --module Network.Socket.ByteString.Lazy send :: Socket -> ByteString -> IO Int64 sendAll :: Socket -> ByteString -> IO () -- | Receive data from the socket. The socket must be in a connected state. -- Data is received on demand, in chunks; each chunk will be sized to -- reflect the amount of data received by individual recv calls. -- -- All remaining data from the socket is consumed. When there is no more -- data to be received, the receiving side of the socket is shut down. If -- there is an error and an exception is thrown, the socket is not shut -- down. getContents :: Socket -> IO ByteString -- | Receive data from the socket. The socket must be in a connected state. -- This function may return fewer bytes than specified. If the received -- data is longer than the specified length, it may be discarded -- depending on the type of socket. This function may block until a -- message arrives. -- -- If there is no more data to be received, returns an empty -- ByteString. -- -- Receiving data from closed socket may lead to undefined behaviour. recv :: Socket -> Int64 -> IO ByteString