{-# LINE 1 "Network/Socket/Types.hsc" #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Network.Socket.Types
(
Socket(..)
, sockFd
, sockFamily
, sockType
, sockProtocol
, sockStatus
, SocketStatus(..)
, SocketType(..)
, isSupportedSocketType
, packSocketType
, packSocketType'
, packSocketTypeOrThrow
, unpackSocketType
, unpackSocketType'
, Family(..)
, isSupportedFamily
, packFamily
, unpackFamily
, SockAddr(..)
, isSupportedSockAddr
, HostAddress
, hostAddressToTuple
, tupleToHostAddress
{-# LINE 40 "Network/Socket/Types.hsc" #-}
, HostAddress6
, hostAddress6ToTuple
, tupleToHostAddress6
, FlowInfo
, ScopeID
{-# LINE 46 "Network/Socket/Types.hsc" #-}
, peekSockAddr
, pokeSockAddr
, sizeOfSockAddr
, sizeOfSockAddrByFamily
, withSockAddr
, withNewSockAddr
, ProtocolNumber
, PortNumber(..)
, zeroMemory
) where
import Control.Concurrent.MVar
import Control.Monad
import Data.Bits
import Data.Maybe
import Data.Ratio
import Data.Typeable
import Data.Word
import Data.Int
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
data Socket
= MkSocket
CInt
Family
SocketType
ProtocolNumber
(MVar SocketStatus)
deriving Typeable
sockFd :: Socket -> CInt
sockFd (MkSocket n _ _ _ _) = n
sockFamily :: Socket -> Family
sockFamily (MkSocket _ f _ _ _) = f
sockType :: Socket -> SocketType
sockType (MkSocket _ _ t _ _) = t
sockProtocol :: Socket -> ProtocolNumber
sockProtocol (MkSocket _ _ _ p _) = p
sockStatus :: Socket -> MVar SocketStatus
sockStatus (MkSocket _ _ _ _ s) = s
instance Eq Socket where
(MkSocket _ _ _ _ m1) == (MkSocket _ _ _ _ m2) = m1 == m2
instance Show Socket where
showsPrec _n (MkSocket fd _ _ _ _) =
showString "<socket: " . shows fd . showString ">"
type ProtocolNumber = CInt
data SocketStatus
= NotConnected
| Bound
| Listening
| Connected
| ConvertedToHandle
| Closed
deriving (Eq, Show, Typeable)
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 178 "Network/Socket/Types.hsc" #-}
Just Stream -> Just 1
{-# LINE 179 "Network/Socket/Types.hsc" #-}
{-# LINE 180 "Network/Socket/Types.hsc" #-}
{-# LINE 181 "Network/Socket/Types.hsc" #-}
Just Datagram -> Just 2
{-# LINE 182 "Network/Socket/Types.hsc" #-}
{-# LINE 183 "Network/Socket/Types.hsc" #-}
{-# LINE 184 "Network/Socket/Types.hsc" #-}
Just Raw -> Just 3
{-# LINE 185 "Network/Socket/Types.hsc" #-}
{-# LINE 186 "Network/Socket/Types.hsc" #-}
{-# LINE 187 "Network/Socket/Types.hsc" #-}
Just RDM -> Just 4
{-# LINE 188 "Network/Socket/Types.hsc" #-}
{-# LINE 189 "Network/Socket/Types.hsc" #-}
{-# LINE 190 "Network/Socket/Types.hsc" #-}
Just SeqPacket -> Just 5
{-# LINE 191 "Network/Socket/Types.hsc" #-}
{-# LINE 192 "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 213 "Network/Socket/Types.hsc" #-}
(1) -> Just Stream
{-# LINE 214 "Network/Socket/Types.hsc" #-}
{-# LINE 215 "Network/Socket/Types.hsc" #-}
{-# LINE 216 "Network/Socket/Types.hsc" #-}
(2) -> Just Datagram
{-# LINE 217 "Network/Socket/Types.hsc" #-}
{-# LINE 218 "Network/Socket/Types.hsc" #-}
{-# LINE 219 "Network/Socket/Types.hsc" #-}
(3) -> Just Raw
{-# LINE 220 "Network/Socket/Types.hsc" #-}
{-# LINE 221 "Network/Socket/Types.hsc" #-}
{-# LINE 222 "Network/Socket/Types.hsc" #-}
(4) -> Just RDM
{-# LINE 223 "Network/Socket/Types.hsc" #-}
{-# LINE 224 "Network/Socket/Types.hsc" #-}
{-# LINE 225 "Network/Socket/Types.hsc" #-}
(5) -> Just SeqPacket
{-# LINE 226 "Network/Socket/Types.hsc" #-}
{-# LINE 227 "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 330 "Network/Socket/Types.hsc" #-}
{-# LINE 331 "Network/Socket/Types.hsc" #-}
Just AF_UNIX -> Just 1
{-# LINE 332 "Network/Socket/Types.hsc" #-}
{-# LINE 333 "Network/Socket/Types.hsc" #-}
{-# LINE 334 "Network/Socket/Types.hsc" #-}
Just AF_INET -> Just 2
{-# LINE 335 "Network/Socket/Types.hsc" #-}
{-# LINE 336 "Network/Socket/Types.hsc" #-}
{-# LINE 337 "Network/Socket/Types.hsc" #-}
Just AF_INET6 -> Just 10
{-# LINE 338 "Network/Socket/Types.hsc" #-}
{-# LINE 339 "Network/Socket/Types.hsc" #-}
{-# LINE 342 "Network/Socket/Types.hsc" #-}
{-# LINE 345 "Network/Socket/Types.hsc" #-}
{-# LINE 348 "Network/Socket/Types.hsc" #-}
{-# LINE 351 "Network/Socket/Types.hsc" #-}
{-# LINE 354 "Network/Socket/Types.hsc" #-}
{-# LINE 357 "Network/Socket/Types.hsc" #-}
{-# LINE 360 "Network/Socket/Types.hsc" #-}
{-# LINE 363 "Network/Socket/Types.hsc" #-}
{-# LINE 364 "Network/Socket/Types.hsc" #-}
Just AF_SNA -> Just 22
{-# LINE 365 "Network/Socket/Types.hsc" #-}
{-# LINE 366 "Network/Socket/Types.hsc" #-}
{-# LINE 367 "Network/Socket/Types.hsc" #-}
Just AF_DECnet -> Just 12
{-# LINE 368 "Network/Socket/Types.hsc" #-}
{-# LINE 369 "Network/Socket/Types.hsc" #-}
{-# LINE 372 "Network/Socket/Types.hsc" #-}
{-# LINE 375 "Network/Socket/Types.hsc" #-}
{-# LINE 378 "Network/Socket/Types.hsc" #-}
{-# LINE 379 "Network/Socket/Types.hsc" #-}
Just AF_APPLETALK -> Just 5
{-# LINE 380 "Network/Socket/Types.hsc" #-}
{-# LINE 381 "Network/Socket/Types.hsc" #-}
{-# LINE 382 "Network/Socket/Types.hsc" #-}
Just AF_ROUTE -> Just 16
{-# LINE 383 "Network/Socket/Types.hsc" #-}
{-# LINE 384 "Network/Socket/Types.hsc" #-}
{-# LINE 387 "Network/Socket/Types.hsc" #-}
{-# LINE 390 "Network/Socket/Types.hsc" #-}
{-# LINE 393 "Network/Socket/Types.hsc" #-}
{-# LINE 396 "Network/Socket/Types.hsc" #-}
{-# LINE 399 "Network/Socket/Types.hsc" #-}
{-# LINE 402 "Network/Socket/Types.hsc" #-}
{-# LINE 403 "Network/Socket/Types.hsc" #-}
Just AF_X25 -> Just 9
{-# LINE 404 "Network/Socket/Types.hsc" #-}
{-# LINE 405 "Network/Socket/Types.hsc" #-}
{-# LINE 406 "Network/Socket/Types.hsc" #-}
Just AF_AX25 -> Just 3
{-# LINE 407 "Network/Socket/Types.hsc" #-}
{-# LINE 408 "Network/Socket/Types.hsc" #-}
{-# LINE 411 "Network/Socket/Types.hsc" #-}
{-# LINE 414 "Network/Socket/Types.hsc" #-}
{-# LINE 415 "Network/Socket/Types.hsc" #-}
Just AF_IPX -> Just 4
{-# LINE 416 "Network/Socket/Types.hsc" #-}
{-# LINE 417 "Network/Socket/Types.hsc" #-}
{-# LINE 420 "Network/Socket/Types.hsc" #-}
{-# LINE 423 "Network/Socket/Types.hsc" #-}
{-# LINE 426 "Network/Socket/Types.hsc" #-}
{-# LINE 429 "Network/Socket/Types.hsc" #-}
{-# LINE 432 "Network/Socket/Types.hsc" #-}
{-# LINE 435 "Network/Socket/Types.hsc" #-}
{-# LINE 438 "Network/Socket/Types.hsc" #-}
{-# LINE 441 "Network/Socket/Types.hsc" #-}
{-# LINE 444 "Network/Socket/Types.hsc" #-}
{-# LINE 447 "Network/Socket/Types.hsc" #-}
{-# LINE 450 "Network/Socket/Types.hsc" #-}
{-# LINE 453 "Network/Socket/Types.hsc" #-}
{-# LINE 454 "Network/Socket/Types.hsc" #-}
Just AF_ISDN -> Just 34
{-# LINE 455 "Network/Socket/Types.hsc" #-}
{-# LINE 456 "Network/Socket/Types.hsc" #-}
{-# LINE 459 "Network/Socket/Types.hsc" #-}
{-# LINE 462 "Network/Socket/Types.hsc" #-}
{-# LINE 465 "Network/Socket/Types.hsc" #-}
{-# LINE 468 "Network/Socket/Types.hsc" #-}
{-# LINE 471 "Network/Socket/Types.hsc" #-}
{-# LINE 474 "Network/Socket/Types.hsc" #-}
{-# LINE 477 "Network/Socket/Types.hsc" #-}
{-# LINE 480 "Network/Socket/Types.hsc" #-}
{-# LINE 481 "Network/Socket/Types.hsc" #-}
Just AF_NETROM -> Just 6
{-# LINE 482 "Network/Socket/Types.hsc" #-}
{-# LINE 483 "Network/Socket/Types.hsc" #-}
{-# LINE 484 "Network/Socket/Types.hsc" #-}
Just AF_BRIDGE -> Just 7
{-# LINE 485 "Network/Socket/Types.hsc" #-}
{-# LINE 486 "Network/Socket/Types.hsc" #-}
{-# LINE 487 "Network/Socket/Types.hsc" #-}
Just AF_ATMPVC -> Just 8
{-# LINE 488 "Network/Socket/Types.hsc" #-}
{-# LINE 489 "Network/Socket/Types.hsc" #-}
{-# LINE 490 "Network/Socket/Types.hsc" #-}
Just AF_ROSE -> Just 11
{-# LINE 491 "Network/Socket/Types.hsc" #-}
{-# LINE 492 "Network/Socket/Types.hsc" #-}
{-# LINE 493 "Network/Socket/Types.hsc" #-}
Just AF_NETBEUI -> Just 13
{-# LINE 494 "Network/Socket/Types.hsc" #-}
{-# LINE 495 "Network/Socket/Types.hsc" #-}
{-# LINE 496 "Network/Socket/Types.hsc" #-}
Just AF_SECURITY -> Just 14
{-# LINE 497 "Network/Socket/Types.hsc" #-}
{-# LINE 498 "Network/Socket/Types.hsc" #-}
{-# LINE 499 "Network/Socket/Types.hsc" #-}
Just AF_PACKET -> Just 17
{-# LINE 500 "Network/Socket/Types.hsc" #-}
{-# LINE 501 "Network/Socket/Types.hsc" #-}
{-# LINE 502 "Network/Socket/Types.hsc" #-}
Just AF_ASH -> Just 18
{-# LINE 503 "Network/Socket/Types.hsc" #-}
{-# LINE 504 "Network/Socket/Types.hsc" #-}
{-# LINE 505 "Network/Socket/Types.hsc" #-}
Just AF_ECONET -> Just 19
{-# LINE 506 "Network/Socket/Types.hsc" #-}
{-# LINE 507 "Network/Socket/Types.hsc" #-}
{-# LINE 508 "Network/Socket/Types.hsc" #-}
Just AF_ATMSVC -> Just 20
{-# LINE 509 "Network/Socket/Types.hsc" #-}
{-# LINE 510 "Network/Socket/Types.hsc" #-}
{-# LINE 511 "Network/Socket/Types.hsc" #-}
Just AF_IRDA -> Just 23
{-# LINE 512 "Network/Socket/Types.hsc" #-}
{-# LINE 513 "Network/Socket/Types.hsc" #-}
{-# LINE 514 "Network/Socket/Types.hsc" #-}
Just AF_PPPOX -> Just 24
{-# LINE 515 "Network/Socket/Types.hsc" #-}
{-# LINE 516 "Network/Socket/Types.hsc" #-}
{-# LINE 517 "Network/Socket/Types.hsc" #-}
Just AF_WANPIPE -> Just 25
{-# LINE 518 "Network/Socket/Types.hsc" #-}
{-# LINE 519 "Network/Socket/Types.hsc" #-}
{-# LINE 520 "Network/Socket/Types.hsc" #-}
Just AF_BLUETOOTH -> Just 31
{-# LINE 521 "Network/Socket/Types.hsc" #-}
{-# LINE 522 "Network/Socket/Types.hsc" #-}
{-# LINE 523 "Network/Socket/Types.hsc" #-}
Just AF_CAN -> Just 29
{-# LINE 524 "Network/Socket/Types.hsc" #-}
{-# LINE 525 "Network/Socket/Types.hsc" #-}
_ -> Nothing
unpackFamily :: CInt -> Family
unpackFamily f = case f of
(0) -> AF_UNSPEC
{-# LINE 532 "Network/Socket/Types.hsc" #-}
{-# LINE 533 "Network/Socket/Types.hsc" #-}
(1) -> AF_UNIX
{-# LINE 534 "Network/Socket/Types.hsc" #-}
{-# LINE 535 "Network/Socket/Types.hsc" #-}
{-# LINE 536 "Network/Socket/Types.hsc" #-}
(2) -> AF_INET
{-# LINE 537 "Network/Socket/Types.hsc" #-}
{-# LINE 538 "Network/Socket/Types.hsc" #-}
{-# LINE 539 "Network/Socket/Types.hsc" #-}
(10) -> AF_INET6
{-# LINE 540 "Network/Socket/Types.hsc" #-}
{-# LINE 541 "Network/Socket/Types.hsc" #-}
{-# LINE 544 "Network/Socket/Types.hsc" #-}
{-# LINE 547 "Network/Socket/Types.hsc" #-}
{-# LINE 550 "Network/Socket/Types.hsc" #-}
{-# LINE 553 "Network/Socket/Types.hsc" #-}
{-# LINE 556 "Network/Socket/Types.hsc" #-}
{-# LINE 559 "Network/Socket/Types.hsc" #-}
{-# LINE 562 "Network/Socket/Types.hsc" #-}
{-# LINE 565 "Network/Socket/Types.hsc" #-}
{-# LINE 566 "Network/Socket/Types.hsc" #-}
(22) -> AF_SNA
{-# LINE 567 "Network/Socket/Types.hsc" #-}
{-# LINE 568 "Network/Socket/Types.hsc" #-}
{-# LINE 569 "Network/Socket/Types.hsc" #-}
(12) -> AF_DECnet
{-# LINE 570 "Network/Socket/Types.hsc" #-}
{-# LINE 571 "Network/Socket/Types.hsc" #-}
{-# LINE 574 "Network/Socket/Types.hsc" #-}
{-# LINE 577 "Network/Socket/Types.hsc" #-}
{-# LINE 580 "Network/Socket/Types.hsc" #-}
{-# LINE 581 "Network/Socket/Types.hsc" #-}
(5) -> AF_APPLETALK
{-# LINE 582 "Network/Socket/Types.hsc" #-}
{-# LINE 583 "Network/Socket/Types.hsc" #-}
{-# LINE 584 "Network/Socket/Types.hsc" #-}
(16) -> AF_ROUTE
{-# LINE 585 "Network/Socket/Types.hsc" #-}
{-# LINE 586 "Network/Socket/Types.hsc" #-}
{-# LINE 589 "Network/Socket/Types.hsc" #-}
{-# LINE 592 "Network/Socket/Types.hsc" #-}
{-# LINE 595 "Network/Socket/Types.hsc" #-}
{-# LINE 598 "Network/Socket/Types.hsc" #-}
{-# LINE 603 "Network/Socket/Types.hsc" #-}
{-# LINE 606 "Network/Socket/Types.hsc" #-}
{-# LINE 607 "Network/Socket/Types.hsc" #-}
(9) -> AF_X25
{-# LINE 608 "Network/Socket/Types.hsc" #-}
{-# LINE 609 "Network/Socket/Types.hsc" #-}
{-# LINE 610 "Network/Socket/Types.hsc" #-}
(3) -> AF_AX25
{-# LINE 611 "Network/Socket/Types.hsc" #-}
{-# LINE 612 "Network/Socket/Types.hsc" #-}
{-# LINE 615 "Network/Socket/Types.hsc" #-}
{-# LINE 618 "Network/Socket/Types.hsc" #-}
{-# LINE 619 "Network/Socket/Types.hsc" #-}
(4) -> AF_IPX
{-# LINE 620 "Network/Socket/Types.hsc" #-}
{-# LINE 621 "Network/Socket/Types.hsc" #-}
{-# LINE 624 "Network/Socket/Types.hsc" #-}
{-# LINE 627 "Network/Socket/Types.hsc" #-}
{-# LINE 630 "Network/Socket/Types.hsc" #-}
{-# LINE 633 "Network/Socket/Types.hsc" #-}
{-# LINE 636 "Network/Socket/Types.hsc" #-}
{-# LINE 639 "Network/Socket/Types.hsc" #-}
{-# LINE 642 "Network/Socket/Types.hsc" #-}
{-# LINE 645 "Network/Socket/Types.hsc" #-}
{-# LINE 648 "Network/Socket/Types.hsc" #-}
{-# LINE 651 "Network/Socket/Types.hsc" #-}
{-# LINE 654 "Network/Socket/Types.hsc" #-}
{-# LINE 657 "Network/Socket/Types.hsc" #-}
{-# LINE 658 "Network/Socket/Types.hsc" #-}
(34) -> AF_ISDN
{-# LINE 659 "Network/Socket/Types.hsc" #-}
{-# LINE 660 "Network/Socket/Types.hsc" #-}
{-# LINE 663 "Network/Socket/Types.hsc" #-}
{-# LINE 666 "Network/Socket/Types.hsc" #-}
{-# LINE 669 "Network/Socket/Types.hsc" #-}
{-# LINE 672 "Network/Socket/Types.hsc" #-}
{-# LINE 675 "Network/Socket/Types.hsc" #-}
{-# LINE 678 "Network/Socket/Types.hsc" #-}
{-# LINE 681 "Network/Socket/Types.hsc" #-}
{-# LINE 684 "Network/Socket/Types.hsc" #-}
{-# LINE 685 "Network/Socket/Types.hsc" #-}
(6) -> AF_NETROM
{-# LINE 686 "Network/Socket/Types.hsc" #-}
{-# LINE 687 "Network/Socket/Types.hsc" #-}
{-# LINE 688 "Network/Socket/Types.hsc" #-}
(7) -> AF_BRIDGE
{-# LINE 689 "Network/Socket/Types.hsc" #-}
{-# LINE 690 "Network/Socket/Types.hsc" #-}
{-# LINE 691 "Network/Socket/Types.hsc" #-}
(8) -> AF_ATMPVC
{-# LINE 692 "Network/Socket/Types.hsc" #-}
{-# LINE 693 "Network/Socket/Types.hsc" #-}
{-# LINE 694 "Network/Socket/Types.hsc" #-}
(11) -> AF_ROSE
{-# LINE 695 "Network/Socket/Types.hsc" #-}
{-# LINE 696 "Network/Socket/Types.hsc" #-}
{-# LINE 697 "Network/Socket/Types.hsc" #-}
(13) -> AF_NETBEUI
{-# LINE 698 "Network/Socket/Types.hsc" #-}
{-# LINE 699 "Network/Socket/Types.hsc" #-}
{-# LINE 700 "Network/Socket/Types.hsc" #-}
(14) -> AF_SECURITY
{-# LINE 701 "Network/Socket/Types.hsc" #-}
{-# LINE 702 "Network/Socket/Types.hsc" #-}
{-# LINE 703 "Network/Socket/Types.hsc" #-}
(17) -> AF_PACKET
{-# LINE 704 "Network/Socket/Types.hsc" #-}
{-# LINE 705 "Network/Socket/Types.hsc" #-}
{-# LINE 706 "Network/Socket/Types.hsc" #-}
(18) -> AF_ASH
{-# LINE 707 "Network/Socket/Types.hsc" #-}
{-# LINE 708 "Network/Socket/Types.hsc" #-}
{-# LINE 709 "Network/Socket/Types.hsc" #-}
(19) -> AF_ECONET
{-# LINE 710 "Network/Socket/Types.hsc" #-}
{-# LINE 711 "Network/Socket/Types.hsc" #-}
{-# LINE 712 "Network/Socket/Types.hsc" #-}
(20) -> AF_ATMSVC
{-# LINE 713 "Network/Socket/Types.hsc" #-}
{-# LINE 714 "Network/Socket/Types.hsc" #-}
{-# LINE 715 "Network/Socket/Types.hsc" #-}
(23) -> AF_IRDA
{-# LINE 716 "Network/Socket/Types.hsc" #-}
{-# LINE 717 "Network/Socket/Types.hsc" #-}
{-# LINE 718 "Network/Socket/Types.hsc" #-}
(24) -> AF_PPPOX
{-# LINE 719 "Network/Socket/Types.hsc" #-}
{-# LINE 720 "Network/Socket/Types.hsc" #-}
{-# LINE 721 "Network/Socket/Types.hsc" #-}
(25) -> AF_WANPIPE
{-# LINE 722 "Network/Socket/Types.hsc" #-}
{-# LINE 723 "Network/Socket/Types.hsc" #-}
{-# LINE 724 "Network/Socket/Types.hsc" #-}
(31) -> AF_BLUETOOTH
{-# LINE 725 "Network/Socket/Types.hsc" #-}
{-# LINE 726 "Network/Socket/Types.hsc" #-}
{-# LINE 727 "Network/Socket/Types.hsc" #-}
(29) -> AF_CAN
{-# LINE 728 "Network/Socket/Types.hsc" #-}
{-# LINE 729 "Network/Socket/Types.hsc" #-}
unknown -> error $
"Network.Socket.Types.unpackFamily: unknown address family: " ++
show unknown
newtype PortNumber = PortNum Word16 deriving (Eq, Ord, Typeable)
{-# DEPRECATED PortNum "Do not use the PortNum constructor. Use the Num instance. PortNum will be removed in the next release." #-}
instance Show PortNumber where
showsPrec p pn = showsPrec p (portNumberToInt pn)
instance Read PortNumber where
readsPrec n = map (\(x,y) -> (intToPortNumber x, y)) . readsPrec n
intToPortNumber :: Int -> PortNumber
intToPortNumber v = PortNum (htons (fromIntegral v))
portNumberToInt :: PortNumber -> Int
portNumberToInt (PortNum po) = fromIntegral (ntohs po)
foreign import CALLCONV unsafe "ntohs" ntohs :: Word16 -> Word16
foreign import CALLCONV unsafe "htons" htons :: Word16 -> Word16
foreign import CALLCONV unsafe "ntohl" ntohl :: Word32 -> Word32
foreign import CALLCONV unsafe "htonl" htonl :: Word32 -> Word32
instance Enum PortNumber where
toEnum = intToPortNumber
fromEnum = portNumberToInt
instance Num PortNumber where
fromInteger i = intToPortNumber (fromInteger i)
(+) x y = intToPortNumber (portNumberToInt x + portNumberToInt y)
(-) x y = intToPortNumber (portNumberToInt x - portNumberToInt y)
negate x = intToPortNumber (-portNumberToInt x)
(*) x y = intToPortNumber (portNumberToInt x * portNumberToInt y)
abs n = intToPortNumber (abs (portNumberToInt n))
signum n = intToPortNumber (signum (portNumberToInt n))
instance Real PortNumber where
toRational x = toInteger x % 1
instance Integral PortNumber where
quotRem a b = let (c,d) = quotRem (portNumberToInt a) (portNumberToInt b) in
(intToPortNumber c, intToPortNumber d)
toInteger a = toInteger (portNumberToInt a)
instance Storable PortNumber where
sizeOf _ = sizeOf (undefined :: Word16)
alignment _ = alignment (undefined :: Word16)
poke p (PortNum po) = poke (castPtr p) po
peek p = PortNum `liftM` peek (castPtr p)
{-# LINE 818 "Network/Socket/Types.hsc" #-}
type FlowInfo = Word32
type ScopeID = Word32
{-# LINE 821 "Network/Socket/Types.hsc" #-}
data SockAddr
= SockAddrInet
PortNumber
HostAddress
| SockAddrInet6
PortNumber
FlowInfo
HostAddress6
ScopeID
| SockAddrUnix
String
| SockAddrCan
Int32
deriving (Eq, Ord, Typeable)
isSupportedSockAddr :: SockAddr -> Bool
isSupportedSockAddr addr = case addr of
SockAddrInet {} -> True
{-# LINE 846 "Network/Socket/Types.hsc" #-}
SockAddrInet6 {} -> True
{-# LINE 848 "Network/Socket/Types.hsc" #-}
{-# LINE 849 "Network/Socket/Types.hsc" #-}
SockAddrUnix{} -> True
{-# LINE 851 "Network/Socket/Types.hsc" #-}
{-# LINE 852 "Network/Socket/Types.hsc" #-}
SockAddrCan{} -> True
{-# LINE 854 "Network/Socket/Types.hsc" #-}
{-# LINE 858 "Network/Socket/Types.hsc" #-}
{-# DEPRECATED SockAddrCan "This will be removed in 3.0" #-}
{-# LINE 866 "Network/Socket/Types.hsc" #-}
type CSaFamily = (Word16)
{-# LINE 867 "Network/Socket/Types.hsc" #-}
{-# LINE 868 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr :: SockAddr -> Int
{-# LINE 874 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrUnix path) =
case path of
'\0':_ -> (2) + length path
{-# LINE 877 "Network/Socket/Types.hsc" #-}
_ -> 110
{-# LINE 878 "Network/Socket/Types.hsc" #-}
{-# LINE 879 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrInet _ _) = 16
{-# LINE 880 "Network/Socket/Types.hsc" #-}
{-# LINE 881 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrInet6 _ _ _ _) = 28
{-# LINE 882 "Network/Socket/Types.hsc" #-}
{-# LINE 883 "Network/Socket/Types.hsc" #-}
{-# LINE 884 "Network/Socket/Types.hsc" #-}
sizeOfSockAddr (SockAddrCan _) = 16
{-# LINE 885 "Network/Socket/Types.hsc" #-}
{-# LINE 886 "Network/Socket/Types.hsc" #-}
{-# LINE 890 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily :: Family -> Int
{-# LINE 895 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily AF_UNIX = 110
{-# LINE 896 "Network/Socket/Types.hsc" #-}
{-# LINE 897 "Network/Socket/Types.hsc" #-}
{-# LINE 898 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily AF_INET6 = 28
{-# LINE 899 "Network/Socket/Types.hsc" #-}
{-# LINE 900 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily AF_INET = 16
{-# LINE 901 "Network/Socket/Types.hsc" #-}
{-# LINE 902 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily AF_CAN = 16
{-# LINE 903 "Network/Socket/Types.hsc" #-}
{-# LINE 904 "Network/Socket/Types.hsc" #-}
sizeOfSockAddrByFamily family = error $
"Network.Socket.Types.sizeOfSockAddrByFamily: address family '" ++
show family ++ "' not supported."
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
withNewSockAddr :: Family -> (Ptr SockAddr -> Int -> IO a) -> IO a
withNewSockAddr family f = do
let sz = sizeOfSockAddrByFamily family
allocaBytes sz $ \ptr -> f ptr sz
pokeSockAddr :: Ptr a -> SockAddr -> IO ()
{-# LINE 932 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrUnix path) = do
{-# LINE 936 "Network/Socket/Types.hsc" #-}
case path of
('\0':_) -> zeroMemory p (110)
{-# LINE 938 "Network/Socket/Types.hsc" #-}
_ -> return ()
{-# LINE 940 "Network/Socket/Types.hsc" #-}
{-# LINE 943 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((1) :: CSaFamily)
{-# LINE 944 "Network/Socket/Types.hsc" #-}
let pathC = map castCharToCChar path
poker = case path of ('\0':_) -> pokeArray; _ -> pokeArray0 0
poker (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p) pathC
{-# LINE 947 "Network/Socket/Types.hsc" #-}
{-# LINE 948 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet (PortNum port) addr) = do
{-# LINE 952 "Network/Socket/Types.hsc" #-}
{-# LINE 955 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((2) :: CSaFamily)
{-# LINE 956 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 957 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p addr
{-# LINE 958 "Network/Socket/Types.hsc" #-}
{-# LINE 959 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrInet6 (PortNum port) flow addr scope) = do
{-# LINE 963 "Network/Socket/Types.hsc" #-}
{-# LINE 966 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ((10) :: CSaFamily)
{-# LINE 967 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 2)) p port
{-# LINE 968 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p flow
{-# LINE 969 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p (In6Addr addr)
{-# LINE 970 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 24)) p scope
{-# LINE 971 "Network/Socket/Types.hsc" #-}
{-# LINE 972 "Network/Socket/Types.hsc" #-}
{-# LINE 973 "Network/Socket/Types.hsc" #-}
pokeSockAddr p (SockAddrCan ifIndex) = do
{-# LINE 977 "Network/Socket/Types.hsc" #-}
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) p ifIndex
{-# LINE 978 "Network/Socket/Types.hsc" #-}
{-# LINE 979 "Network/Socket/Types.hsc" #-}
{-# LINE 983 "Network/Socket/Types.hsc" #-}
peekSockAddr :: Ptr SockAddr -> IO SockAddr
peekSockAddr p = do
family <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 988 "Network/Socket/Types.hsc" #-}
case family :: CSaFamily of
{-# LINE 990 "Network/Socket/Types.hsc" #-}
(1) -> do
{-# LINE 991 "Network/Socket/Types.hsc" #-}
str <- peekCString (((\hsc_ptr -> hsc_ptr `plusPtr` 2)) p)
{-# LINE 992 "Network/Socket/Types.hsc" #-}
return (SockAddrUnix str)
{-# LINE 994 "Network/Socket/Types.hsc" #-}
(2) -> do
{-# LINE 995 "Network/Socket/Types.hsc" #-}
addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 996 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 997 "Network/Socket/Types.hsc" #-}
return (SockAddrInet (PortNum port) addr)
{-# LINE 999 "Network/Socket/Types.hsc" #-}
(10) -> do
{-# LINE 1000 "Network/Socket/Types.hsc" #-}
port <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) p
{-# LINE 1001 "Network/Socket/Types.hsc" #-}
flow <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1002 "Network/Socket/Types.hsc" #-}
In6Addr addr <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 1003 "Network/Socket/Types.hsc" #-}
scope <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) p
{-# LINE 1004 "Network/Socket/Types.hsc" #-}
return (SockAddrInet6 (PortNum port) flow addr scope)
{-# LINE 1006 "Network/Socket/Types.hsc" #-}
{-# LINE 1007 "Network/Socket/Types.hsc" #-}
(29) -> do
{-# LINE 1008 "Network/Socket/Types.hsc" #-}
ifidx <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) p
{-# LINE 1009 "Network/Socket/Types.hsc" #-}
return (SockAddrCan ifidx)
{-# LINE 1011 "Network/Socket/Types.hsc" #-}
_ -> 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)
{-# LINE 1042 "Network/Socket/Types.hsc" #-}
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 1069 "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 1097 "Network/Socket/Types.hsc" #-}
instance Storable In6Addr where
sizeOf _ = 16
{-# LINE 1100 "Network/Socket/Types.hsc" #-}
alignment _ = 4
{-# LINE 1101 "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
{-# LINE 1115 "Network/Socket/Types.hsc" #-}
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)