{-# LINE 1 "Network/Socket/Types.hsc" #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash, UnboxedTuples #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
#include "HsNetDef.h"
module Network.Socket.Types (
Socket
, withFdSocket
, unsafeFdSocket
, touchSocket
, socketToFd
, fdSocket
, mkSocket
, invalidateSocket
, close
, close'
, c_close
, SocketType(..)
, isSupportedSocketType
, packSocketType
, packSocketType'
, packSocketTypeOrThrow
, unpackSocketType
, unpackSocketType'
, Family(..)
, isSupportedFamily
, packFamily
, unpackFamily
, SocketAddress(..)
, withSocketAddress
, withNewSocketAddress
, SockAddr(..)
, isSupportedSockAddr
, HostAddress
, hostAddressToTuple
, tupleToHostAddress
, HostAddress6
, hostAddress6ToTuple
, tupleToHostAddress6
, FlowInfo
, ScopeID
, peekSockAddr
, pokeSockAddr
, withSockAddr
, ProtocolNumber
, defaultProtocol
, PortNumber
, defaultPort
, zeroMemory
, htonl
, ntohl
) where
import Control.Monad (when)
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 (..))
{-# LINE 84 "Network/Socket/Types.hsc" #-}
import Foreign.Marshal.Array
{-# LINE 86 "Network/Socket/Types.hsc" #-}
import Network.Socket.Imports
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 187 "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 196 "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 209 "Network/Socket/Types.hsc" #-}
invalidSocket = -1
{-# LINE 211 "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 259 "Network/Socket/Types.hsc" #-}
foreign import ccall unsafe "close"
c_close :: CInt -> IO CInt
{-# LINE 262 "Network/Socket/Types.hsc" #-}
type ProtocolNumber = CInt
defaultProtocol :: ProtocolNumber
defaultProtocol = 0
data SocketType
= NoSocketType
| Stream
| Datagram
| Raw
| RDM
| SeqPacket
deriving (Eq, Ord, Read, Show, Typeable)
isSupportedSocketType :: SocketType -> Bool
isSupportedSocketType = isJust . packSocketType'
packSocketType' :: SocketType -> Maybe CInt
packSocketType' stype = case Just stype of
Just NoSocketType -> Just 0
{-# LINE 317 "Network/Socket/Types.hsc" #-}
Just Stream -> Just 1
{-# LINE 318 "Network/Socket/Types.hsc" #-}
{-# LINE 319 "Network/Socket/Types.hsc" #-}
{-# LINE 320 "Network/Socket/Types.hsc" #-}
Just Datagram -> Just 2
{-# LINE 321 "Network/Socket/Types.hsc" #-}
{-# LINE 322 "Network/Socket/Types.hsc" #-}
{-# LINE 323 "Network/Socket/Types.hsc" #-}
Just Raw -> Just 3
{-# LINE 324 "Network/Socket/Types.hsc" #-}
{-# LINE 325 "Network/Socket/Types.hsc" #-}
{-# LINE 326 "Network/Socket/Types.hsc" #-}
Just RDM -> Just 4
{-# LINE 327 "Network/Socket/Types.hsc" #-}
{-# LINE 328 "Network/Socket/Types.hsc" #-}
{-# LINE 329 "Network/Socket/Types.hsc" #-}
Just SeqPacket -> Just 5
{-# LINE 330 "Network/Socket/Types.hsc" #-}
{-# LINE 331 "Network/Socket/Types.hsc" #-}
_ -> Nothing
packSocketType :: SocketType -> CInt
packSocketType stype = fromMaybe (error errMsg) (packSocketType' stype)
where
errMsg = concat ["Network.Socket.packSocketType: ",
"socket type ", show stype, " unsupported on this system"]
packSocketTypeOrThrow :: String -> SocketType -> IO CInt
packSocketTypeOrThrow caller stype = maybe err return (packSocketType' stype)
where
err = ioError . userError . concat $ ["Network.Socket.", caller, ": ",
"socket type ", show stype, " unsupported on this system"]
unpackSocketType:: CInt -> Maybe SocketType
unpackSocketType t = case t of
0 -> Just NoSocketType
{-# LINE 352 "Network/Socket/Types.hsc" #-}
(1) -> Just Stream
{-# LINE 353 "Network/Socket/Types.hsc" #-}
{-# LINE 354 "Network/Socket/Types.hsc" #-}
{-# LINE 355 "Network/Socket/Types.hsc" #-}
(2) -> Just Datagram
{-# LINE 356 "Network/Socket/Types.hsc" #-}
{-# LINE 357 "Network/Socket/Types.hsc" #-}
{-# LINE 358 "Network/Socket/Types.hsc" #-}
(3) -> Just Raw
{-# LINE 359 "Network/Socket/Types.hsc" #-}
{-# LINE 360 "Network/Socket/Types.hsc" #-}
{-# LINE 361 "Network/Socket/Types.hsc" #-}
(4) -> Just RDM
{-# LINE 362 "Network/Socket/Types.hsc" #-}
{-# LINE 363 "Network/Socket/Types.hsc" #-}
{-# LINE 364 "Network/Socket/Types.hsc" #-}
(5) -> Just SeqPacket
{-# LINE 365 "Network/Socket/Types.hsc" #-}
{-# LINE 366 "Network/Socket/Types.hsc" #-}
_ -> Nothing
unpackSocketType' :: String -> CInt -> IO SocketType
unpackSocketType' caller ty = maybe err return (unpackSocketType ty)
where
err = ioError . userError . concat $ ["Network.Socket.", caller, ": ",
"socket type ", show ty, " unsupported on this system"]
data Family
= 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
deriving (Eq, Ord, Read, Show)
packFamily :: Family -> CInt
packFamily f = case packFamily' f of
Just fam -> fam
Nothing -> error $
"Network.Socket.packFamily: unsupported address family: " ++
show f
isSupportedFamily :: Family -> Bool
isSupportedFamily = isJust . packFamily'
packFamily' :: Family -> Maybe CInt
packFamily' f = case Just f of
Just AF_UNSPEC -> Just 0
{-# LINE 470 "Network/Socket/Types.hsc" #-}
{-# LINE 471 "Network/Socket/Types.hsc" #-}
Just AF_UNIX -> Just 1
{-# LINE 472 "Network/Socket/Types.hsc" #-}
{-# LINE 473 "Network/Socket/Types.hsc" #-}
{-# LINE 474 "Network/Socket/Types.hsc" #-}
Just AF_INET -> Just 2
{-# LINE 475 "Network/Socket/Types.hsc" #-}
{-# LINE 476 "Network/Socket/Types.hsc" #-}
{-# LINE 477 "Network/Socket/Types.hsc" #-}
Just AF_INET6 -> Just 10
{-# LINE 478 "Network/Socket/Types.hsc" #-}
{-# LINE 479 "Network/Socket/Types.hsc" #-}
{-# LINE 482 "Network/Socket/Types.hsc" #-}
{-# LINE 485 "Network/Socket/Types.hsc" #-}
{-# LINE 488 "Network/Socket/Types.hsc" #-}
{-# LINE 491 "Network/Socket/Types.hsc" #-}
{-# LINE 494 "Network/Socket/Types.hsc" #-}
{-# LINE 497 "Network/Socket/Types.hsc" #-}
{-# LINE 500 "Network/Socket/Types.hsc" #-}
{-# LINE 503 "Network/Socket/Types.hsc" #-}
{-# LINE 504 "Network/Socket/Types.hsc" #-}
Just AF_SNA -> Just 22
{-# LINE 505 "Network/Socket/Types.hsc" #-}
{-# LINE 506 "Network/Socket/Types.hsc" #-}
{-# LINE 507 "Network/Socket/Types.hsc" #-}
Just AF_DECnet -> Just 12
{-# LINE 508 "Network/Socket/Types.hsc" #-}
{-# LINE 509 "Network/Socket/Types.hsc" #-}
{-# LINE 512 "Network/Socket/Types.hsc" #-}
{-# LINE 515 "Network/Socket/Types.hsc" #-}
{-# LINE 518 "Network/Socket/Types.hsc" #-}
{-# LINE 519 "Network/Socket/Types.hsc" #-}
Just AF_APPLETALK -> Just 5
{-# LINE 520 "Network/Socket/Types.hsc" #-}
{-# LINE 521 "Network/Socket/Types.hsc" #-}
{-# LINE 522 "Network/Socket/Types.hsc" #-}
Just AF_ROUTE -> Just 16
{-# LINE 523 "Network/Socket/Types.hsc" #-}
{-# LINE 524 "Network/Socket/Types.hsc" #-}
{-# LINE 527 "Network/Socket/Types.hsc" #-}
{-# LINE 530 "Network/Socket/Types.hsc" #-}
{-# LINE 533 "Network/Socket/Types.hsc" #-}
{-# LINE 536 "Network/Socket/Types.hsc" #-}
{-# LINE 539 "Network/Socket/Types.hsc" #-}
{-# LINE 542 "Network/Socket/Types.hsc" #-}
{-# LINE 543 "Network/Socket/Types.hsc" #-}
Just AF_X25 -> Just 9
{-# LINE 544 "Network/Socket/Types.hsc" #-}
{-# LINE 545 "Network/Socket/Types.hsc" #-}
{-# LINE 546 "Network/Socket/Types.hsc" #-}
Just AF_AX25 -> Just 3
{-# LINE 547 "Network/Socket/Types.hsc" #-}
{-# LINE 548 "Network/Socket/Types.hsc" #-}
{-# LINE 551 "Network/Socket/Types.hsc" #-}
{-# LINE 554 "Network/Socket/Types.hsc" #-}
{-# LINE 555 "Network/Socket/Types.hsc" #-}
Just AF_IPX -> Just 4
{-# LINE 556 "Network/Socket/Types.hsc" #-}
{-# LINE 557 "Network/Socket/Types.hsc" #-}
{-# LINE 560 "Network/Socket/Types.hsc" #-}
{-# LINE 563 "Network/Socket/Types.hsc" #-}
{-# LINE 566 "Network/Socket/Types.hsc" #-}
{-# LINE 569 "Network/Socket/Types.hsc" #-}
{-# LINE 572 "Network/Socket/Types.hsc" #-}
{-# LINE 575 "Network/Socket/Types.hsc" #-}
{-# LINE 578 "Network/Socket/Types.hsc" #-}
{-# LINE 581 "Network/Socket/Types.hsc" #-}
{-# LINE 584 "Network/Socket/Types.hsc" #-}
{-# LINE 587 "Network/Socket/Types.hsc" #-}
{-# LINE 590 "Network/Socket/Types.hsc" #-}
{-# LINE 593 "Network/Socket/Types.hsc" #-}
{-# LINE 594 "Network/Socket/Types.hsc" #-}
Just AF_ISDN -> Just 34
{-# LINE 595 "Network/Socket/Types.hsc" #-}
{-# LINE 596 "Network/Socket/Types.hsc" #-}
{-# LINE 599 "Network/Socket/Types.hsc" #-}
{-# LINE 602 "Network/Socket/Types.hsc" #-}
{-# LINE 605 "Network/Socket/Types.hsc" #-}
{-# LINE 608 "Network/Socket/Types.hsc" #-}
{-# LINE 611 "Network/Socket/Types.hsc" #-}
{-# LINE 614 "Network/Socket/Types.hsc" #-}
{-# LINE 617 "Network/Socket/Types.hsc" #-}
{-# LINE 620 "Network/Socket/Types.hsc" #-}
{-# LINE 621 "Network/Socket/Types.hsc" #-}
Just AF_NETROM -> Just 6
{-# LINE 622 "Network/Socket/Types.hsc" #-}
{-# LINE 623 "Network/Socket/Types.hsc" #-}
{-# LINE 624 "Network/Socket/Types.hsc" #-}
Just AF_BRIDGE -> Just 7
{-# LINE 625 "Network/Socket/Types.hsc" #-}
{-# LINE 626 "Network/Socket/Types.hsc" #-}
{-# LINE 627 "Network/Socket/Types.hsc" #-}
Just AF_ATMPVC -> Just 8
{-# LINE 628 "Network/Socket/Types.hsc" #-}
{-# LINE 629 "Network/Socket/Types.hsc" #-}
{-# LINE 630 "Network/Socket/Types.hsc" #-}
Just AF_ROSE -> Just 11
{-# LINE 631 "Network/Socket/Types.hsc" #-}
{-# LINE 632 "Network/Socket/Types.hsc" #-}
{-# LINE 633 "Network/Socket/Types.hsc" #-}
Just AF_NETBEUI -> Just 13
{-# LINE 634 "Network/Socket/Types.hsc" #-}
{-# LINE 635 "Network/Socket/Types.hsc" #-}
{-# LINE 636 "Network/Socket/Types.hsc" #-}
Just AF_SECURITY -> Just 14
{-# LINE 637 "Network/Socket/Types.hsc" #-}
{-# LINE 638 "Network/Socket/Types.hsc" #-}
{-# LINE 639 "Network/Socket/Types.hsc" #-}
Just AF_PACKET -> Just 17
{-# LINE 640 "Network/Socket/Types.hsc" #-}
{-# LINE 641 "Network/Socket/Types.hsc" #-}
{-# LINE 642 "Network/Socket/Types.hsc" #-}
Just AF_ASH -> Just 18
{-# LINE 643 "Network/Socket/Types.hsc" #-}
{-# LINE 644 "Network/Socket/Types.hsc" #-}
{-# LINE 645 "Network/Socket/Types.hsc" #-}
Just AF_ECONET -> Just 19
{-# LINE 646 "Network/Socket/Types.hsc" #-}
{-# LINE 647 "Network/Socket/Types.hsc" #-}
{-# LINE 648 "Network/Socket/Types.hsc" #-}
Just AF_ATMSVC -> Just 20
{-# LINE 649 "Network/Socket/Types.hsc" #-}
{-# LINE 650 "Network/Socket/Types.hsc" #-}
{-# LINE 651 "Network/Socket/Types.hsc" #-}
Just AF_IRDA -> Just 23
{-# LINE 652 "Network/Socket/Types.hsc" #-}
{-# LINE 653 "Network/Socket/Types.hsc" #-}
{-# LINE 654 "Network/Socket/Types.hsc" #-}
Just AF_PPPOX -> Just 24
{-# LINE 655 "Network/Socket/Types.hsc" #-}
{-# LINE 656 "Network/Socket/Types.hsc" #-}
{-# LINE 657 "Network/Socket/Types.hsc" #-}
Just AF_WANPIPE -> Just 25
{-# LINE 658 "Network/Socket/Types.hsc" #-}
{-# LINE 659 "Network/Socket/Types.hsc" #-}
{-# LINE 660 "Network/Socket/Types.hsc" #-}
Just AF_BLUETOOTH -> Just 31
{-# LINE 661 "Network/Socket/Types.hsc" #-}
{-# LINE 662 "Network/Socket/Types.hsc" #-}
{-# LINE 663 "Network/Socket/Types.hsc" #-}
Just AF_CAN -> Just 29
{-# LINE 664 "Network/Socket/Types.hsc" #-}
{-# LINE 665 "Network/Socket/Types.hsc" #-}
_ -> Nothing
unpackFamily :: CInt -> Family
unpackFamily f = case f of
(0) -> AF_UNSPEC
{-# LINE 673 "Network/Socket/Types.hsc" #-}
{-# LINE 674 "Network/Socket/Types.hsc" #-}
(1) -> AF_UNIX
{-# LINE 675 "Network/Socket/Types.hsc" #-}
{-# LINE 676 "Network/Socket/Types.hsc" #-}
{-# LINE 677 "Network/Socket/Types.hsc" #-}
(2) -> AF_INET
{-# LINE 678 "Network/Socket/Types.hsc" #-}
{-# LINE 679 "Network/Socket/Types.hsc" #-}
{-# LINE 680 "Network/Socket/Types.hsc" #-}
(10) -> AF_INET6
{-# LINE 681 "Network/Socket/Types.hsc" #-}
{-# LINE 682 "Network/Socket/Types.hsc" #-}
{-# LINE 685 "Network/Socket/Types.hsc" #-}
{-# LINE 688 "Network/Socket/Types.hsc" #-}
{-# LINE 691 "Network/Socket/Types.hsc" #-}
{-# LINE 694 "Network/Socket/Types.hsc" #-}
{-# LINE 697 "Network/Socket/Types.hsc" #-}
{-# LINE 700 "Network/Socket/Types.hsc" #-}
{-# LINE 703 "Network/Socket/Types.hsc" #-}
{-# LINE 706 "Network/Socket/Types.hsc" #-}
{-# LINE 707 "Network/Socket/Types.hsc" #-}
(22) -> AF_SNA
{-# LINE 708 "Network/Socket/Types.hsc" #-}
{-# LINE 709 "Network/Socket/Types.hsc" #-}
{-# LINE 710 "Network/Socket/Types.hsc" #-}
(12) -> AF_DECnet
{-# LINE 711 "Network/Socket/Types.hsc" #-}
{-# LINE 712 "Network/Socket/Types.hsc" #-}
{-# LINE 715 "Network/Socket/Types.hsc" #-}
{-# LINE 718 "Network/Socket/Types.hsc" #-}
{-# LINE 721 "Network/Socket/Types.hsc" #-}
{-# LINE 722 "Network/Socket/Types.hsc" #-}
(5) -> AF_APPLETALK
{-# LINE 723 "Network/Socket/Types.hsc" #-}
{-# LINE 724 "Network/Socket/Types.hsc" #-}
{-# LINE 725 "Network/Socket/Types.hsc" #-}
(16) -> AF_ROUTE
{-# LINE 726 "Network/Socket/Types.hsc" #-}
{-# LINE 727 "Network/Socket/Types.hsc" #-}
{-# LINE 730 "Network/Socket/Types.hsc" #-}
{-# LINE 733 "Network/Socket/Types.hsc" #-}
{-# LINE 736 "Network/Socket/Types.hsc" #-}
{-# LINE 739 "Network/Socket/Types.hsc" #-}
{-# LINE 744 "Network/Socket/Types.hsc" #-}
{-# LINE 747 "Network/Socket/Types.hsc" #-}
{-# LINE 748 "Network/Socket/Types.hsc" #-}
(9) -> AF_X25
{-# LINE 749 "Network/Socket/Types.hsc" #-}
{-# LINE 750 "Network/Socket/Types.hsc" #-}
{-# LINE 751 "Network/Socket/Types.hsc" #-}
(3) -> AF_AX25
{-# LINE 752 "Network/Socket/Types.hsc" #-}
{-# LINE 753 "Network/Socket/Types.hsc" #-}
{-# LINE 756 "Network/Socket/Types.hsc" #-}
{-# LINE 759 "Network/Socket/Types.hsc" #-}
{-# LINE 760 "Network/Socket/Types.hsc" #-}
(4) -> AF_IPX
{-# LINE 761 "Network/Socket/Types.hsc" #-}
{-# LINE 762 "Network/Socket/Types.hsc" #-}
{-# LINE 765 "Network/Socket/Types.hsc" #-}
{-# LINE 768 "Network/Socket/Types.hsc" #-}
{-# LINE 771 "Network/Socket/Types.hsc" #-}
{-# LINE 774 "Network/Socket/Types.hsc" #-}
{-# LINE 777 "Network/Socket/Types.hsc" #-}
{-# LINE 780 "Network/Socket/Types.hsc" #-}
{-# LINE 783 "Network/Socket/Types.hsc" #-}
{-# LINE 786 "Network/Socket/Types.hsc" #-}
{-# LINE 789 "Network/Socket/Types.hsc" #-}
{-# LINE 792 "Network/Socket/Types.hsc" #-}
{-# LINE 795 "Network/Socket/Types.hsc" #-}
{-# LINE 798 "Network/Socket/Types.hsc" #-}
{-# LINE 799 "Network/Socket/Types.hsc" #-}
(34) -> AF_ISDN
{-# LINE 800 "Network/Socket/Types.hsc" #-}
{-# LINE 801 "Network/Socket/Types.hsc" #-}
{-# LINE 804 "Network/Socket/Types.hsc" #-}
{-# LINE 807 "Network/Socket/Types.hsc" #-}
{-# LINE 810 "Network/Socket/Types.hsc" #-}
{-# LINE 813 "Network/Socket/Types.hsc" #-}
{-# LINE 816 "Network/Socket/Types.hsc" #-}
{-# LINE 819 "Network/Socket/Types.hsc" #-}
{-# LINE 822 "Network/Socket/Types.hsc" #-}
{-# LINE 825 "Network/Socket/Types.hsc" #-}
{-# LINE 826 "Network/Socket/Types.hsc" #-}
(6) -> AF_NETROM
{-# LINE 827 "Network/Socket/Types.hsc" #-}
{-# LINE 828 "Network/Socket/Types.hsc" #-}
{-# LINE 829 "Network/Socket/Types.hsc" #-}
(7) -> AF_BRIDGE
{-# LINE 830 "Network/Socket/Types.hsc" #-}
{-# LINE 831 "Network/Socket/Types.hsc" #-}
{-# LINE 832 "Network/Socket/Types.hsc" #-}
(8) -> AF_ATMPVC
{-# LINE 833 "Network/Socket/Types.hsc" #-}
{-# LINE 834 "Network/Socket/Types.hsc" #-}
{-# LINE 835 "Network/Socket/Types.hsc" #-}
(11) -> AF_ROSE
{-# LINE 836 "Network/Socket/Types.hsc" #-}
{-# LINE 837 "Network/Socket/Types.hsc" #-}
{-# LINE 838 "Network/Socket/Types.hsc" #-}
(13) -> AF_NETBEUI
{-# LINE 839 "Network/Socket/Types.hsc" #-}
{-# LINE 840 "Network/Socket/Types.hsc" #-}
{-# LINE 841 "Network/Socket/Types.hsc" #-}
(14) -> AF_SECURITY
{-# LINE 842 "Network/Socket/Types.hsc" #-}
{-# LINE 843 "Network/Socket/Types.hsc" #-}
{-# LINE 844 "Network/Socket/Types.hsc" #-}
(17) -> AF_PACKET
{-# LINE 845 "Network/Socket/Types.hsc" #-}
{-# LINE 846 "Network/Socket/Types.hsc" #-}
{-# LINE 847 "Network/Socket/Types.hsc" #-}
(18) -> AF_ASH
{-# LINE 848 "Network/Socket/Types.hsc" #-}
{-# LINE 849 "Network/Socket/Types.hsc" #-}
{-# LINE 850 "Network/Socket/Types.hsc" #-}
(19) -> AF_ECONET
{-# LINE 851 "Network/Socket/Types.hsc" #-}
{-# LINE 852 "Network/Socket/Types.hsc" #-}
{-# LINE 853 "Network/Socket/Types.hsc" #-}
(20) -> AF_ATMSVC
{-# LINE 854 "Network/Socket/Types.hsc" #-}
{-# LINE 855 "Network/Socket/Types.hsc" #-}
{-# LINE 856 "Network/Socket/Types.hsc" #-}
(23) -> AF_IRDA
{-# LINE 857 "Network/Socket/Types.hsc" #-}
{-# LINE 858 "Network/Socket/Types.hsc" #-}
{-# LINE 859 "Network/Socket/Types.hsc" #-}
(24) -> AF_PPPOX
{-# LINE 860 "Network/Socket/Types.hsc" #-}
{-# LINE 861 "Network/Socket/Types.hsc" #-}
{-# LINE 862 "Network/Socket/Types.hsc" #-}
(25) -> AF_WANPIPE
{-# LINE 863 "Network/Socket/Types.hsc" #-}
{-# LINE 864 "Network/Socket/Types.hsc" #-}
{-# LINE 865 "Network/Socket/Types.hsc" #-}
(31) -> AF_BLUETOOTH
{-# LINE 866 "Network/Socket/Types.hsc" #-}
{-# LINE 867 "Network/Socket/Types.hsc" #-}
{-# LINE 868 "Network/Socket/Types.hsc" #-}
(29) -> AF_CAN
{-# LINE 869 "Network/Socket/Types.hsc" #-}
{-# LINE 870 "Network/Socket/Types.hsc" #-}
unknown -> error $
"Network.Socket.Types.unpackFamily: unknown address family: " ++
show unknown
newtype PortNumber = PortNum Word16 deriving (Eq, Ord, Typeable, Num, Enum, Real, Integral)
instance Show PortNumber where
showsPrec p (PortNum pn) = showsPrec p (fromIntegral pn :: Int)
instance Read PortNumber where
readsPrec n = map (\(x,y) -> (fromIntegral (x :: Int), y)) . readsPrec n
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 (undefined :: Word16)
alignment _ = alignment (undefined :: 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
withSocketAddress :: SocketAddress sa => sa -> (Ptr sa -> Int -> IO a) -> IO a
withSocketAddress addr f = do
let sz = sizeOfSocketAddress addr
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
type FlowInfo = Word32
type ScopeID = Word32
data SockAddr
= SockAddrInet
!PortNumber
!HostAddress
| SockAddrInet6
!PortNumber
!FlowInfo
!HostAddress6
!ScopeID
| SockAddrUnix
String
deriving (Eq, Ord, Typeable)
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
{-# LINE 1002 "Network/Socket/Types.hsc" #-}
SockAddrUnix{} -> True
{-# LINE 1006 "Network/Socket/Types.hsc" #-}
instance SocketAddress SockAddr where
sizeOfSocketAddress = sizeOfSockAddr
peekSocketAddress = peekSockAddr
pokeSocketAddress = pokeSockAddr
{-# LINE 1017 "Network/Socket/Types.hsc" #-}
type CSaFamily = (Word16)
{-# LINE 1018 "Network/Socket/Types.hsc" #-}
{-# LINE 1019 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr :: SockAddr -> Int
{-# LINE 1025 "Network/Socket/Types.hsc" #-}
{-# LINE 1026 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrUnix path) =
case path of
'\0':_ -> (2) + length path
{-# LINE 1040 "Network/Socket/Types.hsc" #-}
_ -> 110
{-# LINE 1041 "Network/Socket/Types.hsc" #-}
{-# LINE 1044 "Network/Socket/Types.hsc" #-}
{-# LINE 1047 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr SockAddrInet{} = 16
{-# LINE 1048 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr SockAddrInet6{} = 28
{-# LINE 1049 "Network/Socket/Types.hsc" #-}
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
{-# LINE 1062 "Network/Socket/Types.hsc" #-}
unixPathMax :: Int
unixPathMax = 108
{-# LINE 1064 "Network/Socket/Types.hsc" #-}
{-# LINE 1065 "Network/Socket/Types.hsc" #-}
pokeSockAddr :: Ptr a -> SockAddr -> IO ()
{-# LINE 1076 "Network/Socket/Types.hsc" #-}
pokeSockAddr p sa@(SockAddrUnix path) = do
when (length path > unixPathMax) $ error "pokeSockAddr: path is too long"
zeroMemory p $ fromIntegral $ sizeOfSockAddr sa
{-# LINE 1082 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((1) :: CSaFamily)
{-# LINE 1083 "Network/Socket/Types.hsc" #-}
let pathC = map castCharToCChar path
pokeArray (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p) pathC
{-# LINE 1086 "Network/Socket/Types.hsc" #-}
{-# LINE 1089 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet port addr) = do
zeroMemory p (16)
{-# LINE 1091 "Network/Socket/Types.hsc" #-}
{-# LINE 1094 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((2) :: CSaFamily)
{-# LINE 1095 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 1096 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p addr
{-# LINE 1097 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet6 port flow addr scope) = do
zeroMemory p (28)
{-# LINE 1099 "Network/Socket/Types.hsc" #-}
{-# LINE 1102 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((10) :: CSaFamily)
{-# LINE 1103 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 1104 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p flow
{-# LINE 1105 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p (In6Addr addr)
{-# LINE 1106 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 24)) p scope
{-# LINE 1107 "Network/Socket/Types.hsc" #-}
peekSockAddr :: Ptr SockAddr -> IO SockAddr
peekSockAddr p = do
family <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 1112 "Network/Socket/Types.hsc" #-}
case family :: CSaFamily of
{-# LINE 1114 "Network/Socket/Types.hsc" #-}
(1) -> do
{-# LINE 1115 "Network/Socket/Types.hsc" #-}
str <- peekCAString (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p)
{-# LINE 1116 "Network/Socket/Types.hsc" #-}
return (SockAddrUnix str)
{-# LINE 1118 "Network/Socket/Types.hsc" #-}
(2) -> do
{-# LINE 1119 "Network/Socket/Types.hsc" #-}
addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1120 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 1121 "Network/Socket/Types.hsc" #-}
return (SockAddrInet port addr)
(10) -> do
{-# LINE 1123 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 1124 "Network/Socket/Types.hsc" #-}
flow <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1125 "Network/Socket/Types.hsc" #-}
In6Addr addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 1126 "Network/Socket/Types.hsc" #-}
scope <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) p
{-# LINE 1127 "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 1191 "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 1219 "Network/Socket/Types.hsc" #-}
instance Storable In6Addr where
sizeOf _ = 16
{-# LINE 1222 "Network/Socket/Types.hsc" #-}
alignment _ = 4
{-# LINE 1223 "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
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)