{-# LINE 1 "Network/Socket/Types.hsc" #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash, UnboxedTuples #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
#include "HsNetDef.h"
module Network.Socket.Types (
Socket
, withFdSocket
, unsafeFdSocket
, touchSocket
, socketToFd
, fdSocket
, mkSocket
, invalidateSocket
, close
, close'
, c_close
, SocketType(GeneralSocketType, UnsupportedSocketType, NoSocketType
, Stream, Datagram, Raw, RDM, SeqPacket)
, isSupportedSocketType
, packSocketType
, unpackSocketType
, Family(GeneralFamily, UnsupportedFamily
,AF_UNSPEC,AF_UNIX,AF_INET,AF_INET6,AF_IMPLINK,AF_PUP,AF_CHAOS
,AF_NS,AF_NBS,AF_ECMA,AF_DATAKIT,AF_CCITT,AF_SNA,AF_DECnet
,AF_DLI,AF_LAT,AF_HYLINK,AF_APPLETALK,AF_ROUTE,AF_NETBIOS
,AF_NIT,AF_802,AF_ISO,AF_OSI,AF_NETMAN,AF_X25,AF_AX25,AF_OSINET
,AF_GOSSIP,AF_IPX,Pseudo_AF_XTP,AF_CTF,AF_WAN,AF_SDL,AF_NETWARE
,AF_NDD,AF_INTF,AF_COIP,AF_CNT,Pseudo_AF_RTIP,Pseudo_AF_PIP
,AF_SIP,AF_ISDN,Pseudo_AF_KEY,AF_NATM,AF_ARP,Pseudo_AF_HDRCMPLT
,AF_ENCAP,AF_LINK,AF_RAW,AF_RIF,AF_NETROM,AF_BRIDGE,AF_ATMPVC
,AF_ROSE,AF_NETBEUI,AF_SECURITY,AF_PACKET,AF_ASH,AF_ECONET
,AF_ATMSVC,AF_IRDA,AF_PPPOX,AF_WANPIPE,AF_BLUETOOTH,AF_CAN)
, isSupportedFamily
, packFamily
, unpackFamily
, SocketAddress(..)
, withSocketAddress
, withNewSocketAddress
, SockAddr(..)
, isSupportedSockAddr
, HostAddress
, hostAddressToTuple
, tupleToHostAddress
, HostAddress6
, hostAddress6ToTuple
, tupleToHostAddress6
, FlowInfo
, ScopeID
, peekSockAddr
, pokeSockAddr
, withSockAddr
, NullSockAddr(..)
, ProtocolNumber
, defaultProtocol
, PortNumber
, defaultPort
, zeroMemory
, htonl
, ntohl
, In6Addr(..)
) where
import Data.IORef (IORef, newIORef, readIORef, atomicModifyIORef', mkWeakIORef)
import Foreign.C.Error (throwErrno)
import Foreign.Marshal.Alloc
import GHC.Conc (closeFdWith)
import System.Posix.Types (Fd)
import Control.DeepSeq (NFData (..))
import GHC.Exts (touch#)
import GHC.IORef (IORef (..))
import GHC.STRef (STRef (..))
import GHC.IO (IO (..))
import qualified Text.Read as P
import Foreign.Marshal.Array
import Network.Socket.Imports
import Network.Socket.ReadShow
data Socket = Socket (IORef CInt) CInt
instance Show Socket where
show (Socket _ ofd) = "<socket: " ++ show ofd ++ ">"
instance Eq Socket where
Socket ref1 _ == Socket ref2 _ = ref1 == ref2
{-# DEPRECATED fdSocket "Use withFdSocket or unsafeFdSocket instead" #-}
fdSocket :: Socket -> IO CInt
fdSocket = unsafeFdSocket
unsafeFdSocket :: Socket -> IO CInt
unsafeFdSocket (Socket ref _) = readIORef ref
touchSocket :: Socket -> IO ()
touchSocket (Socket ref _) = touch ref
touch :: IORef a -> IO ()
touch (IORef (STRef mutVar)) =
IO $ \s -> (# touch# mutVar s, () #)
withFdSocket :: Socket -> (CInt -> IO r) -> IO r
withFdSocket (Socket ref _) f = do
fd <- readIORef ref
r <- f fd
touch ref
return r
socketToFd :: Socket -> IO CInt
socketToFd s = do
{-# LINE 205 "Network/Socket/Types.hsc" #-}
fd <- unsafeFdSocket s
fd2 <- c_dup fd
close s
return fd2
foreign import ccall unsafe "dup"
c_dup :: CInt -> IO CInt
{-# LINE 214 "Network/Socket/Types.hsc" #-}
mkSocket :: CInt -> IO Socket
mkSocket fd = do
ref <- newIORef fd
let s = Socket ref fd
void $ mkWeakIORef ref $ close s
return s
invalidSocket :: CInt
{-# LINE 227 "Network/Socket/Types.hsc" #-}
invalidSocket = -1
{-# LINE 229 "Network/Socket/Types.hsc" #-}
invalidateSocket ::
Socket
-> (CInt -> IO a)
-> (CInt -> IO a)
-> IO a
invalidateSocket (Socket ref _) errorAction normalAction = do
oldfd <- atomicModifyIORef' ref $ \cur -> (invalidSocket, cur)
if oldfd == invalidSocket then errorAction oldfd else normalAction oldfd
close :: Socket -> IO ()
close s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
closeFdWith closeFd (toFd oldfd)
where
toFd :: CInt -> Fd
toFd = fromIntegral
closeFd :: Fd -> IO ()
closeFd = void . c_close . fromIntegral
close' :: Socket -> IO ()
close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
closeFdWith closeFd (toFd oldfd)
where
toFd :: CInt -> Fd
toFd = fromIntegral
closeFd :: Fd -> IO ()
closeFd fd = do
ret <- c_close $ fromIntegral fd
when (ret == -1) $ throwErrno "Network.Socket.close'"
{-# LINE 277 "Network/Socket/Types.hsc" #-}
foreign import ccall unsafe "close"
c_close :: CInt -> IO CInt
{-# LINE 280 "Network/Socket/Types.hsc" #-}
type ProtocolNumber = CInt
defaultProtocol :: ProtocolNumber
defaultProtocol = 0
newtype SocketType = SocketType { packSocketType :: CInt }
deriving (Eq, Ord)
unpackSocketType :: CInt -> SocketType
unpackSocketType = SocketType
{-# INLINE unpackSocketType #-}
isSupportedSocketType :: SocketType -> Bool
isSupportedSocketType = (/= UnsupportedSocketType)
pattern GeneralSocketType :: CInt -> SocketType
pattern GeneralSocketType n = SocketType n
{-# LINE 332 "Network/Socket/Types.hsc" #-}
{-# COMPLETE GeneralSocketType #-}
{-# LINE 334 "Network/Socket/Types.hsc" #-}
pattern UnsupportedSocketType :: SocketType
pattern UnsupportedSocketType = SocketType (-1)
pattern NoSocketType :: SocketType
pattern NoSocketType = SocketType 0
pattern Stream :: SocketType
{-# LINE 349 "Network/Socket/Types.hsc" #-}
pattern Stream = SocketType (1)
{-# LINE 350 "Network/Socket/Types.hsc" #-}
{-# LINE 353 "Network/Socket/Types.hsc" #-}
pattern Datagram :: SocketType
{-# LINE 356 "Network/Socket/Types.hsc" #-}
pattern Datagram = SocketType (2)
{-# LINE 357 "Network/Socket/Types.hsc" #-}
{-# LINE 360 "Network/Socket/Types.hsc" #-}
pattern Raw :: SocketType
{-# LINE 363 "Network/Socket/Types.hsc" #-}
pattern Raw = SocketType (3)
{-# LINE 364 "Network/Socket/Types.hsc" #-}
{-# LINE 367 "Network/Socket/Types.hsc" #-}
pattern RDM :: SocketType
{-# LINE 370 "Network/Socket/Types.hsc" #-}
pattern RDM = SocketType (4)
{-# LINE 371 "Network/Socket/Types.hsc" #-}
{-# LINE 374 "Network/Socket/Types.hsc" #-}
pattern SeqPacket :: SocketType
{-# LINE 377 "Network/Socket/Types.hsc" #-}
pattern SeqPacket = SocketType (5)
{-# LINE 378 "Network/Socket/Types.hsc" #-}
{-# LINE 381 "Network/Socket/Types.hsc" #-}
newtype Family = Family { packFamily :: CInt } deriving (Eq, Ord)
isSupportedFamily :: Family -> Bool
isSupportedFamily f = case f of
UnsupportedFamily -> False
GeneralFamily _ -> True
unpackFamily :: CInt -> Family
unpackFamily = Family
{-# INLINE unpackFamily #-}
pattern GeneralFamily :: CInt -> Family
pattern GeneralFamily n = Family n
{-# LINE 420 "Network/Socket/Types.hsc" #-}
{-# COMPLETE GeneralFamily #-}
{-# LINE 422 "Network/Socket/Types.hsc" #-}
pattern UnsupportedFamily :: Family
pattern UnsupportedFamily = Family (-1)
pattern AF_UNSPEC :: Family
pattern AF_UNSPEC = Family (0)
{-# LINE 436 "Network/Socket/Types.hsc" #-}
pattern AF_UNIX :: Family
{-# LINE 440 "Network/Socket/Types.hsc" #-}
pattern AF_UNIX = Family (1)
{-# LINE 441 "Network/Socket/Types.hsc" #-}
{-# LINE 444 "Network/Socket/Types.hsc" #-}
pattern AF_INET :: Family
{-# LINE 448 "Network/Socket/Types.hsc" #-}
pattern AF_INET = Family (2)
{-# LINE 449 "Network/Socket/Types.hsc" #-}
{-# LINE 452 "Network/Socket/Types.hsc" #-}
pattern AF_INET6 :: Family
{-# LINE 456 "Network/Socket/Types.hsc" #-}
pattern AF_INET6 = Family (10)
{-# LINE 457 "Network/Socket/Types.hsc" #-}
{-# LINE 460 "Network/Socket/Types.hsc" #-}
pattern AF_IMPLINK :: Family
{-# LINE 466 "Network/Socket/Types.hsc" #-}
pattern AF_IMPLINK = Family (-1)
{-# LINE 468 "Network/Socket/Types.hsc" #-}
pattern AF_PUP :: Family
{-# LINE 474 "Network/Socket/Types.hsc" #-}
pattern AF_PUP = Family (-1)
{-# LINE 476 "Network/Socket/Types.hsc" #-}
pattern AF_CHAOS :: Family
{-# LINE 482 "Network/Socket/Types.hsc" #-}
pattern AF_CHAOS = Family (-1)
{-# LINE 484 "Network/Socket/Types.hsc" #-}
pattern AF_NS :: Family
{-# LINE 490 "Network/Socket/Types.hsc" #-}
pattern AF_NS = Family (-1)
{-# LINE 492 "Network/Socket/Types.hsc" #-}
pattern AF_NBS :: Family
{-# LINE 498 "Network/Socket/Types.hsc" #-}
pattern AF_NBS = Family (-1)
{-# LINE 500 "Network/Socket/Types.hsc" #-}
pattern AF_ECMA :: Family
{-# LINE 506 "Network/Socket/Types.hsc" #-}
pattern AF_ECMA = Family (-1)
{-# LINE 508 "Network/Socket/Types.hsc" #-}
pattern AF_DATAKIT :: Family
{-# LINE 514 "Network/Socket/Types.hsc" #-}
pattern AF_DATAKIT = Family (-1)
{-# LINE 516 "Network/Socket/Types.hsc" #-}
pattern AF_CCITT :: Family
{-# LINE 522 "Network/Socket/Types.hsc" #-}
pattern AF_CCITT = Family (-1)
{-# LINE 524 "Network/Socket/Types.hsc" #-}
pattern AF_SNA :: Family
{-# LINE 528 "Network/Socket/Types.hsc" #-}
pattern AF_SNA = Family (22)
{-# LINE 529 "Network/Socket/Types.hsc" #-}
{-# LINE 532 "Network/Socket/Types.hsc" #-}
pattern AF_DECnet :: Family
{-# LINE 536 "Network/Socket/Types.hsc" #-}
pattern AF_DECnet = Family (12)
{-# LINE 537 "Network/Socket/Types.hsc" #-}
{-# LINE 540 "Network/Socket/Types.hsc" #-}
pattern AF_DLI :: Family
{-# LINE 546 "Network/Socket/Types.hsc" #-}
pattern AF_DLI = Family (-1)
{-# LINE 548 "Network/Socket/Types.hsc" #-}
pattern AF_LAT :: Family
{-# LINE 554 "Network/Socket/Types.hsc" #-}
pattern AF_LAT = Family (-1)
{-# LINE 556 "Network/Socket/Types.hsc" #-}
pattern AF_HYLINK :: Family
{-# LINE 562 "Network/Socket/Types.hsc" #-}
pattern AF_HYLINK = Family (-1)
{-# LINE 564 "Network/Socket/Types.hsc" #-}
pattern AF_APPLETALK :: Family
{-# LINE 568 "Network/Socket/Types.hsc" #-}
pattern AF_APPLETALK = Family (5)
{-# LINE 569 "Network/Socket/Types.hsc" #-}
{-# LINE 572 "Network/Socket/Types.hsc" #-}
pattern AF_ROUTE :: Family
{-# LINE 576 "Network/Socket/Types.hsc" #-}
pattern AF_ROUTE = Family (16)
{-# LINE 577 "Network/Socket/Types.hsc" #-}
{-# LINE 580 "Network/Socket/Types.hsc" #-}
pattern AF_NETBIOS :: Family
{-# LINE 586 "Network/Socket/Types.hsc" #-}
pattern AF_NETBIOS = Family (-1)
{-# LINE 588 "Network/Socket/Types.hsc" #-}
pattern AF_NIT :: Family
{-# LINE 594 "Network/Socket/Types.hsc" #-}
pattern AF_NIT = Family (-1)
{-# LINE 596 "Network/Socket/Types.hsc" #-}
pattern AF_802 :: Family
{-# LINE 602 "Network/Socket/Types.hsc" #-}
pattern AF_802 = Family (-1)
{-# LINE 604 "Network/Socket/Types.hsc" #-}
pattern AF_ISO :: Family
{-# LINE 610 "Network/Socket/Types.hsc" #-}
pattern AF_ISO = Family (-1)
{-# LINE 612 "Network/Socket/Types.hsc" #-}
pattern AF_OSI :: Family
{-# LINE 618 "Network/Socket/Types.hsc" #-}
pattern AF_OSI = Family (-1)
{-# LINE 620 "Network/Socket/Types.hsc" #-}
pattern AF_NETMAN :: Family
{-# LINE 626 "Network/Socket/Types.hsc" #-}
pattern AF_NETMAN = Family (-1)
{-# LINE 628 "Network/Socket/Types.hsc" #-}
pattern AF_X25 :: Family
{-# LINE 632 "Network/Socket/Types.hsc" #-}
pattern AF_X25 = Family (9)
{-# LINE 633 "Network/Socket/Types.hsc" #-}
{-# LINE 636 "Network/Socket/Types.hsc" #-}
pattern AF_AX25 :: Family
{-# LINE 640 "Network/Socket/Types.hsc" #-}
pattern AF_AX25 = Family (3)
{-# LINE 641 "Network/Socket/Types.hsc" #-}
{-# LINE 644 "Network/Socket/Types.hsc" #-}
pattern AF_OSINET :: Family
{-# LINE 650 "Network/Socket/Types.hsc" #-}
pattern AF_OSINET = Family (-1)
{-# LINE 652 "Network/Socket/Types.hsc" #-}
pattern AF_GOSSIP :: Family
{-# LINE 658 "Network/Socket/Types.hsc" #-}
pattern AF_GOSSIP = Family (-1)
{-# LINE 660 "Network/Socket/Types.hsc" #-}
pattern AF_IPX :: Family
{-# LINE 664 "Network/Socket/Types.hsc" #-}
pattern AF_IPX = Family (4)
{-# LINE 665 "Network/Socket/Types.hsc" #-}
{-# LINE 668 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_XTP :: Family
{-# LINE 674 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_XTP = Family (-1)
{-# LINE 676 "Network/Socket/Types.hsc" #-}
pattern AF_CTF :: Family
{-# LINE 682 "Network/Socket/Types.hsc" #-}
pattern AF_CTF = Family (-1)
{-# LINE 684 "Network/Socket/Types.hsc" #-}
pattern AF_WAN :: Family
{-# LINE 690 "Network/Socket/Types.hsc" #-}
pattern AF_WAN = Family (-1)
{-# LINE 692 "Network/Socket/Types.hsc" #-}
pattern AF_SDL :: Family
{-# LINE 698 "Network/Socket/Types.hsc" #-}
pattern AF_SDL = Family (-1)
{-# LINE 700 "Network/Socket/Types.hsc" #-}
pattern AF_NETWARE :: Family
{-# LINE 706 "Network/Socket/Types.hsc" #-}
pattern AF_NETWARE = Family (-1)
{-# LINE 708 "Network/Socket/Types.hsc" #-}
pattern AF_NDD :: Family
{-# LINE 714 "Network/Socket/Types.hsc" #-}
pattern AF_NDD = Family (-1)
{-# LINE 716 "Network/Socket/Types.hsc" #-}
pattern AF_INTF :: Family
{-# LINE 722 "Network/Socket/Types.hsc" #-}
pattern AF_INTF = Family (-1)
{-# LINE 724 "Network/Socket/Types.hsc" #-}
pattern AF_COIP :: Family
{-# LINE 730 "Network/Socket/Types.hsc" #-}
pattern AF_COIP = Family (-1)
{-# LINE 732 "Network/Socket/Types.hsc" #-}
pattern AF_CNT :: Family
{-# LINE 738 "Network/Socket/Types.hsc" #-}
pattern AF_CNT = Family (-1)
{-# LINE 740 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_RTIP :: Family
{-# LINE 746 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_RTIP = Family (-1)
{-# LINE 748 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_PIP :: Family
{-# LINE 754 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_PIP = Family (-1)
{-# LINE 756 "Network/Socket/Types.hsc" #-}
pattern AF_SIP :: Family
{-# LINE 762 "Network/Socket/Types.hsc" #-}
pattern AF_SIP = Family (-1)
{-# LINE 764 "Network/Socket/Types.hsc" #-}
pattern AF_ISDN :: Family
{-# LINE 768 "Network/Socket/Types.hsc" #-}
pattern AF_ISDN = Family (34)
{-# LINE 769 "Network/Socket/Types.hsc" #-}
{-# LINE 772 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_KEY :: Family
{-# LINE 778 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_KEY = Family (-1)
{-# LINE 780 "Network/Socket/Types.hsc" #-}
pattern AF_NATM :: Family
{-# LINE 786 "Network/Socket/Types.hsc" #-}
pattern AF_NATM = Family (-1)
{-# LINE 788 "Network/Socket/Types.hsc" #-}
pattern AF_ARP :: Family
{-# LINE 794 "Network/Socket/Types.hsc" #-}
pattern AF_ARP = Family (-1)
{-# LINE 796 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_HDRCMPLT :: Family
{-# LINE 802 "Network/Socket/Types.hsc" #-}
pattern Pseudo_AF_HDRCMPLT = Family (-1)
{-# LINE 804 "Network/Socket/Types.hsc" #-}
pattern AF_ENCAP :: Family
{-# LINE 810 "Network/Socket/Types.hsc" #-}
pattern AF_ENCAP = Family (-1)
{-# LINE 812 "Network/Socket/Types.hsc" #-}
pattern AF_LINK :: Family
{-# LINE 818 "Network/Socket/Types.hsc" #-}
pattern AF_LINK = Family (-1)
{-# LINE 820 "Network/Socket/Types.hsc" #-}
pattern AF_RAW :: Family
{-# LINE 826 "Network/Socket/Types.hsc" #-}
pattern AF_RAW = Family (-1)
{-# LINE 828 "Network/Socket/Types.hsc" #-}
pattern AF_RIF :: Family
{-# LINE 834 "Network/Socket/Types.hsc" #-}
pattern AF_RIF = Family (-1)
{-# LINE 836 "Network/Socket/Types.hsc" #-}
pattern AF_NETROM :: Family
{-# LINE 840 "Network/Socket/Types.hsc" #-}
pattern AF_NETROM = Family (6)
{-# LINE 841 "Network/Socket/Types.hsc" #-}
{-# LINE 844 "Network/Socket/Types.hsc" #-}
pattern AF_BRIDGE :: Family
{-# LINE 848 "Network/Socket/Types.hsc" #-}
pattern AF_BRIDGE = Family (7)
{-# LINE 849 "Network/Socket/Types.hsc" #-}
{-# LINE 852 "Network/Socket/Types.hsc" #-}
pattern AF_ATMPVC :: Family
{-# LINE 856 "Network/Socket/Types.hsc" #-}
pattern AF_ATMPVC = Family (8)
{-# LINE 857 "Network/Socket/Types.hsc" #-}
{-# LINE 860 "Network/Socket/Types.hsc" #-}
pattern AF_ROSE :: Family
{-# LINE 864 "Network/Socket/Types.hsc" #-}
pattern AF_ROSE = Family (11)
{-# LINE 865 "Network/Socket/Types.hsc" #-}
{-# LINE 868 "Network/Socket/Types.hsc" #-}
pattern AF_NETBEUI :: Family
{-# LINE 872 "Network/Socket/Types.hsc" #-}
pattern AF_NETBEUI = Family (13)
{-# LINE 873 "Network/Socket/Types.hsc" #-}
{-# LINE 876 "Network/Socket/Types.hsc" #-}
pattern AF_SECURITY :: Family
{-# LINE 880 "Network/Socket/Types.hsc" #-}
pattern AF_SECURITY = Family (14)
{-# LINE 881 "Network/Socket/Types.hsc" #-}
{-# LINE 884 "Network/Socket/Types.hsc" #-}
pattern AF_PACKET :: Family
{-# LINE 888 "Network/Socket/Types.hsc" #-}
pattern AF_PACKET = Family (17)
{-# LINE 889 "Network/Socket/Types.hsc" #-}
{-# LINE 892 "Network/Socket/Types.hsc" #-}
pattern AF_ASH :: Family
{-# LINE 896 "Network/Socket/Types.hsc" #-}
pattern AF_ASH = Family (18)
{-# LINE 897 "Network/Socket/Types.hsc" #-}
{-# LINE 900 "Network/Socket/Types.hsc" #-}
pattern AF_ECONET :: Family
{-# LINE 904 "Network/Socket/Types.hsc" #-}
pattern AF_ECONET = Family (19)
{-# LINE 905 "Network/Socket/Types.hsc" #-}
{-# LINE 908 "Network/Socket/Types.hsc" #-}
pattern AF_ATMSVC :: Family
{-# LINE 912 "Network/Socket/Types.hsc" #-}
pattern AF_ATMSVC = Family (20)
{-# LINE 913 "Network/Socket/Types.hsc" #-}
{-# LINE 916 "Network/Socket/Types.hsc" #-}
pattern AF_IRDA :: Family
{-# LINE 920 "Network/Socket/Types.hsc" #-}
pattern AF_IRDA = Family (23)
{-# LINE 921 "Network/Socket/Types.hsc" #-}
{-# LINE 924 "Network/Socket/Types.hsc" #-}
pattern AF_PPPOX :: Family
{-# LINE 928 "Network/Socket/Types.hsc" #-}
pattern AF_PPPOX = Family (24)
{-# LINE 929 "Network/Socket/Types.hsc" #-}
{-# LINE 932 "Network/Socket/Types.hsc" #-}
pattern AF_WANPIPE :: Family
{-# LINE 936 "Network/Socket/Types.hsc" #-}
pattern AF_WANPIPE = Family (25)
{-# LINE 937 "Network/Socket/Types.hsc" #-}
{-# LINE 940 "Network/Socket/Types.hsc" #-}
pattern AF_BLUETOOTH :: Family
{-# LINE 944 "Network/Socket/Types.hsc" #-}
pattern AF_BLUETOOTH = Family (31)
{-# LINE 945 "Network/Socket/Types.hsc" #-}
{-# LINE 948 "Network/Socket/Types.hsc" #-}
pattern AF_CAN :: Family
{-# LINE 952 "Network/Socket/Types.hsc" #-}
pattern AF_CAN = Family (29)
{-# LINE 953 "Network/Socket/Types.hsc" #-}
{-# LINE 956 "Network/Socket/Types.hsc" #-}
newtype PortNumber = PortNum Word16 deriving (Eq, Ord, Num, Enum, Bounded, Real, Integral)
foreign import CALLCONV unsafe "ntohs" ntohs :: Word16 -> Word16
foreign import CALLCONV unsafe "htons" htons :: Word16 -> Word16
foreign import CALLCONV unsafe "htonl" htonl :: Word32 -> Word32
foreign import CALLCONV unsafe "ntohl" ntohl :: Word32 -> Word32
{-# DEPRECATED htonl "Use getAddrInfo instead" #-}
{-# DEPRECATED ntohl "Use getAddrInfo instead" #-}
instance Storable PortNumber where
sizeOf ~_ = sizeOf (0 :: Word16)
alignment ~_ = alignment (0 :: Word16)
poke p (PortNum po) = poke (castPtr p) (htons po)
peek p = PortNum . ntohs <$> peek (castPtr p)
defaultPort :: PortNumber
defaultPort = 0
class SocketAddress sa where
sizeOfSocketAddress :: sa -> Int
peekSocketAddress :: Ptr sa -> IO sa
pokeSocketAddress :: Ptr a -> sa -> IO ()
sockaddrStorageLen :: Int
sockaddrStorageLen = 128
{-# NOINLINE withSocketAddress #-}
withSocketAddress :: SocketAddress sa => sa -> (Ptr sa -> Int -> IO a) -> IO a
withSocketAddress addr f = do
let sz = sizeOfSocketAddress addr
if sz == 0 then
f nullPtr 0
else
allocaBytes sz $ \p -> pokeSocketAddress p addr >> f (castPtr p) sz
withNewSocketAddress :: SocketAddress sa => (Ptr sa -> Int -> IO a) -> IO a
withNewSocketAddress f = allocaBytes sockaddrStorageLen $ \ptr -> do
zeroMemory ptr $ fromIntegral sockaddrStorageLen
f ptr sockaddrStorageLen
data NullSockAddr = NullSockAddr
instance SocketAddress NullSockAddr where
sizeOfSocketAddress _ = 0
peekSocketAddress _ = return NullSockAddr
pokeSocketAddress _ _ = return ()
type FlowInfo = Word32
type ScopeID = Word32
data SockAddr
= SockAddrInet
PortNumber
HostAddress
| SockAddrInet6
PortNumber
FlowInfo
HostAddress6
ScopeID
| SockAddrUnix
String
deriving (Eq, Ord)
instance NFData SockAddr where
rnf (SockAddrInet _ _) = ()
rnf (SockAddrInet6 _ _ _ _) = ()
rnf (SockAddrUnix str) = rnf str
isSupportedSockAddr :: SockAddr -> Bool
isSupportedSockAddr addr = case addr of
SockAddrInet{} -> True
SockAddrInet6{} -> True
SockAddrUnix{} -> True
instance SocketAddress SockAddr where
sizeOfSocketAddress = sizeOfSockAddr
peekSocketAddress = peekSockAddr
pokeSocketAddress = pokeSockAddr
{-# LINE 1103 "Network/Socket/Types.hsc" #-}
type CSaFamily = (Word16)
{-# LINE 1104 "Network/Socket/Types.hsc" #-}
{-# LINE 1105 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr :: SockAddr -> Int
{-# LINE 1111 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrUnix path) =
case path of
'\0':_ -> (2) + length path
{-# LINE 1125 "Network/Socket/Types.hsc" #-}
_ -> 110
{-# LINE 1126 "Network/Socket/Types.hsc" #-}
{-# LINE 1129 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr SockAddrInet{} = 16
{-# LINE 1130 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr SockAddrInet6{} = 28
{-# LINE 1131 "Network/Socket/Types.hsc" #-}
{-# NOINLINE withSockAddr #-}
withSockAddr :: SockAddr -> (Ptr SockAddr -> Int -> IO a) -> IO a
withSockAddr addr f = do
let sz = sizeOfSockAddr addr
allocaBytes sz $ \p -> pokeSockAddr p addr >> f (castPtr p) sz
unixPathMax :: Int
unixPathMax = 108
{-# LINE 1148 "Network/Socket/Types.hsc" #-}
pokeSockAddr :: Ptr a -> SockAddr -> IO ()
pokeSockAddr p sa@(SockAddrUnix path) = do
let pathC = map castCharToCChar path
len = length pathC
when (len >= unixPathMax) $ error
$ "pokeSockAddr: path is too long in SockAddrUnix " <> show path
<> ", length " <> show len <> ", unixPathMax " <> show unixPathMax
zeroMemory p $ fromIntegral $ sizeOfSockAddr sa
{-# LINE 1168 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((1) :: CSaFamily)
{-# LINE 1169 "Network/Socket/Types.hsc" #-}
pokeArray (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p) pathC
{-# LINE 1171 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet port addr) = do
zeroMemory p (16)
{-# LINE 1173 "Network/Socket/Types.hsc" #-}
{-# LINE 1176 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((2) :: CSaFamily)
{-# LINE 1177 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 1178 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p addr
{-# LINE 1179 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet6 port flow addr scope) = do
zeroMemory p (28)
{-# LINE 1181 "Network/Socket/Types.hsc" #-}
{-# LINE 1184 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((10) :: CSaFamily)
{-# LINE 1185 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 1186 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p flow
{-# LINE 1187 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p (In6Addr addr)
{-# LINE 1188 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 24)) p scope
{-# LINE 1189 "Network/Socket/Types.hsc" #-}
peekSockAddr :: Ptr SockAddr -> IO SockAddr
peekSockAddr p = do
family <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 1194 "Network/Socket/Types.hsc" #-}
case family :: CSaFamily of
(1) -> do
{-# LINE 1196 "Network/Socket/Types.hsc" #-}
str <- peekCAString (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p)
{-# LINE 1197 "Network/Socket/Types.hsc" #-}
return (SockAddrUnix str)
(2) -> do
{-# LINE 1199 "Network/Socket/Types.hsc" #-}
addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1200 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 1201 "Network/Socket/Types.hsc" #-}
return (SockAddrInet port addr)
(10) -> do
{-# LINE 1203 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 1204 "Network/Socket/Types.hsc" #-}
flow <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1205 "Network/Socket/Types.hsc" #-}
In6Addr addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 1206 "Network/Socket/Types.hsc" #-}
scope <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) p
{-# LINE 1207 "Network/Socket/Types.hsc" #-}
return (SockAddrInet6 port flow addr scope)
_ -> ioError $ userError $
"Network.Socket.Types.peekSockAddr: address family '" ++
show family ++ "' not supported."
type HostAddress = Word32
hostAddressToTuple :: HostAddress -> (Word8, Word8, Word8, Word8)
hostAddressToTuple ha' =
let ha = htonl ha'
byte i = fromIntegral (ha `shiftR` i) :: Word8
in (byte 24, byte 16, byte 8, byte 0)
tupleToHostAddress :: (Word8, Word8, Word8, Word8) -> HostAddress
tupleToHostAddress (b3, b2, b1, b0) =
let x `sl` i = fromIntegral x `shiftL` i :: Word32
in ntohl $ (b3 `sl` 24) .|. (b2 `sl` 16) .|. (b1 `sl` 8) .|. (b0 `sl` 0)
type HostAddress6 = (Word32, Word32, Word32, Word32)
hostAddress6ToTuple :: HostAddress6 -> (Word16, Word16, Word16, Word16,
Word16, Word16, Word16, Word16)
hostAddress6ToTuple (w3, w2, w1, w0) =
let high, low :: Word32 -> Word16
high w = fromIntegral (w `shiftR` 16)
low w = fromIntegral w
in (high w3, low w3, high w2, low w2, high w1, low w1, high w0, low w0)
tupleToHostAddress6 :: (Word16, Word16, Word16, Word16,
Word16, Word16, Word16, Word16) -> HostAddress6
tupleToHostAddress6 (w7, w6, w5, w4, w3, w2, w1, w0) =
let add :: Word16 -> Word16 -> Word32
high `add` low = (fromIntegral high `shiftL` 16) .|. (fromIntegral low)
in (w7 `add` w6, w5 `add` w4, w3 `add` w2, w1 `add` w0)
s6_addr_offset :: Int
s6_addr_offset = ((0))
{-# LINE 1271 "Network/Socket/Types.hsc" #-}
peek32 :: Ptr a -> Int -> IO Word32
peek32 p i0 = do
let i' = i0 * 4
peekByte n = peekByteOff p (s6_addr_offset + i' + n) :: IO Word8
a `sl` i = fromIntegral a `shiftL` i
a0 <- peekByte 0
a1 <- peekByte 1
a2 <- peekByte 2
a3 <- peekByte 3
return ((a0 `sl` 24) .|. (a1 `sl` 16) .|. (a2 `sl` 8) .|. (a3 `sl` 0))
poke32 :: Ptr a -> Int -> Word32 -> IO ()
poke32 p i0 a = do
let i' = i0 * 4
pokeByte n = pokeByteOff p (s6_addr_offset + i' + n)
x `sr` i = fromIntegral (x `shiftR` i) :: Word8
pokeByte 0 (a `sr` 24)
pokeByte 1 (a `sr` 16)
pokeByte 2 (a `sr` 8)
pokeByte 3 (a `sr` 0)
newtype In6Addr = In6Addr HostAddress6
{-# LINE 1299 "Network/Socket/Types.hsc" #-}
instance Storable In6Addr where
sizeOf ~_ = 16
{-# LINE 1302 "Network/Socket/Types.hsc" #-}
alignment ~_ = 4
{-# LINE 1303 "Network/Socket/Types.hsc" #-}
peek p = do
a <- peek32 p 0
b <- peek32 p 1
c <- peek32 p 2
d <- peek32 p 3
return $ In6Addr (a, b, c, d)
poke p (In6Addr (a, b, c, d)) = do
poke32 p 0 a
poke32 p 1 b
poke32 p 2 c
poke32 p 3 d
socktypeBijection :: Bijection SocketType String
socktypeBijection =
[ (UnsupportedSocketType, "UnsupportedSocketType")
, (Stream, "Stream")
, (Datagram, "Datagram")
, (Raw, "Raw")
, (RDM, "RDM")
, (SeqPacket, "SeqPacket")
, (NoSocketType, "NoSocketType")
]
instance Show SocketType where
showsPrec = bijectiveShow socktypeBijection def
where
gst = "GeneralSocketType"
def = defShow gst packSocketType _showInt
instance Read SocketType where
readPrec = bijectiveRead socktypeBijection def
where
gst = "GeneralSocketType"
def = defRead gst unpackSocketType _readInt
familyBijection :: Bijection Family String
familyBijection =
[ (UnsupportedFamily, "UnsupportedFamily")
, (AF_UNSPEC, "AF_UNSPEC")
, (AF_UNIX, "AF_UNIX")
, (AF_INET, "AF_INET")
, (AF_INET6, "AF_INET6")
, (AF_IMPLINK, "AF_IMPLINK")
, (AF_PUP, "AF_PUP")
, (AF_CHAOS, "AF_CHAOS")
, (AF_NS, "AF_NS")
, (AF_NBS, "AF_NBS")
, (AF_ECMA, "AF_ECMA")
, (AF_DATAKIT, "AF_DATAKIT")
, (AF_CCITT, "AF_CCITT")
, (AF_SNA, "AF_SNA")
, (AF_DECnet, "AF_DECnet")
, (AF_DLI, "AF_DLI")
, (AF_LAT, "AF_LAT")
, (AF_HYLINK, "AF_HYLINK")
, (AF_APPLETALK, "AF_APPLETALK")
, (AF_ROUTE, "AF_ROUTE")
, (AF_NETBIOS, "AF_NETBIOS")
, (AF_NIT, "AF_NIT")
, (AF_802, "AF_802")
, (AF_ISO, "AF_ISO")
, (AF_OSI, "AF_OSI")
, (AF_NETMAN, "AF_NETMAN")
, (AF_X25, "AF_X25")
, (AF_AX25, "AF_AX25")
, (AF_OSINET, "AF_OSINET")
, (AF_GOSSIP, "AF_GOSSIP")
, (AF_IPX, "AF_IPX")
, (Pseudo_AF_XTP, "Pseudo_AF_XTP")
, (AF_CTF, "AF_CTF")
, (AF_WAN, "AF_WAN")
, (AF_SDL, "AF_SDL")
, (AF_NETWARE, "AF_NETWARE")
, (AF_NDD, "AF_NDD")
, (AF_INTF, "AF_INTF")
, (AF_COIP, "AF_COIP")
, (AF_CNT, "AF_CNT")
, (Pseudo_AF_RTIP, "Pseudo_AF_RTIP")
, (Pseudo_AF_PIP, "Pseudo_AF_PIP")
, (AF_SIP, "AF_SIP")
, (AF_ISDN, "AF_ISDN")
, (Pseudo_AF_KEY, "Pseudo_AF_KEY")
, (AF_NATM, "AF_NATM")
, (AF_ARP, "AF_ARP")
, (Pseudo_AF_HDRCMPLT, "Pseudo_AF_HDRCMPLT")
, (AF_ENCAP, "AF_ENCAP")
, (AF_LINK, "AF_LINK")
, (AF_RAW, "AF_RAW")
, (AF_RIF, "AF_RIF")
, (AF_NETROM, "AF_NETROM")
, (AF_BRIDGE, "AF_BRIDGE")
, (AF_ATMPVC, "AF_ATMPVC")
, (AF_ROSE, "AF_ROSE")
, (AF_NETBEUI, "AF_NETBEUI")
, (AF_SECURITY, "AF_SECURITY")
, (AF_PACKET, "AF_PACKET")
, (AF_ASH, "AF_ASH")
, (AF_ECONET, "AF_ECONET")
, (AF_ATMSVC, "AF_ATMSVC")
, (AF_IRDA, "AF_IRDA")
, (AF_PPPOX, "AF_PPPOX")
, (AF_WANPIPE, "AF_WANPIPE")
, (AF_BLUETOOTH, "AF_BLUETOOTH")
, (AF_CAN, "AF_CAN")
]
instance Show Family where
showsPrec = bijectiveShow familyBijection def
where
gf = "GeneralFamily"
def = defShow gf packFamily _showInt
instance Read Family where
readPrec = bijectiveRead familyBijection def
where
gf = "GeneralFamily"
def = defRead gf unpackFamily _readInt
instance Show PortNumber where
showsPrec p (PortNum pn) = showsPrec p pn
instance Read PortNumber where
readPrec = safeInt
foreign import ccall unsafe "string.h" memset :: Ptr a -> CInt -> CSize -> IO ()
zeroMemory :: Ptr a -> CSize -> IO ()
zeroMemory dest nbytes = memset dest 0 (fromIntegral nbytes)