2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1998
4 \section[SocketPrim]{Low-level socket bindings}
6 The @SocketPrim@ module is for when you want full control over the
7 sockets, exposing the C socket API.
10 {-# OPTIONS -#include "stgio.h" -#include "cbits/ghcSockets.h" #-}
24 socket, -- :: Family -> SocketType -> ProtocolNumber -> IO Socket
25 connect, -- :: Socket -> SockAddr -> IO ()
26 bindSocket, -- :: Socket -> SockAddr -> IO ()
27 listen, -- :: Socket -> Int -> IO ()
28 accept, -- :: Socket -> IO (Socket, SockAddr)
29 getPeerName, -- :: Socket -> IO SockAddr
30 getSocketName, -- :: Socket -> IO SockAddr
32 socketPort, -- :: Socket -> IO PortNumber
34 writeSocket, -- :: Socket -> String -> IO Int
35 readSocket, -- :: Socket -> Int -> IO (String, Int)
36 readSocketAll, -- :: Socket -> IO String
38 socketToHandle, -- :: Socket -> IO Handle
40 sendTo, -- :: Socket -> String -> SockAddr -> IO Int
41 recvFrom, -- :: Socket -> Int -> IO (String, Int, SockAddr)
42 -- sendmsg -- :: Socket -> Message -> MsgFlags -> IO Int
43 -- recvmsg -- :: Socket -> MsgFlags -> IO Message
45 shutdown, -- :: Socket -> ShutdownCmd -> IO ()
46 sClose, -- :: Socket -> IO ()
48 inet_addr, -- :: String -> IO HostAddress
49 inet_ntoa, -- :: HostAddress -> IO String
51 sIsConnected, -- :: Socket -> IO Bool
52 sIsBound, -- :: Socket -> IO Bool
53 sIsListening, -- :: Socket -> IO Bool
54 sIsReadable, -- :: Socket -> IO Bool
55 sIsWritable, -- :: Socket -> IO Bool
59 getSocketOption, -- :: Socket -> SocketOption -> IO Int
60 setSocketOption, -- :: Socket -> SocketOption -> Int -> IO ()
63 mkPortNumber, -- :: Int -> PortNumber
73 -- The following are exported ONLY for use in the BSD module and
74 -- should not be used else where.
76 packFamily, unpackFamily,
78 packSockAddr, unpackSockAddr
85 import PrelIOBase -- IOError, Handle representation
90 import IOExts ( IORef, newIORef, readIORef, writeIORef )
91 import PackedString ( unpackNBytesPS, byteArrayToPS,
92 unpackCString, unpackCStringIO,
98 %************************************************************************
100 \subsection[Socket-SocketTypes]{Socket Types}
102 %************************************************************************
105 There are a few possible ways to do this. The first is convert the
106 structs used in the C library into an equivalent Haskell type. An
107 other possible implementation is to keep all the internals in the C
108 code and use an Int\# and a status flag. The second method is used here
109 since a lot of the C structures are not required to be manipulated.
111 Originally the status was non-mutable so we had to return a new socket
112 each time we changed the status. This version now uses mutable
113 variables to avoid the need to do this. The result is a cleaner
114 interface and better security since the application programmer now
115 can't circumvent the status information to perform invalid operations
120 -- Returned Status Function called
121 = NotConnected -- socket
122 | Bound -- bindSocket
123 | Listening -- listen
124 | Connected -- connect/accept
125 | Error String -- Any
130 Int -- File Descriptor
133 Int -- Protocol Number
134 (IORef SocketStatus) -- Status Flag
137 The scheme used for addressing sockets is somewhat quirky. The
138 calls in the BSD socket API that need to know the socket address all
139 operate in terms of \tr{struct sockaddr}, a `virtual' type of socket address.
141 The Internet family of sockets are addressed as \tr{struct sockaddr\_in},
142 so when calling functions that operate on \tr{struct sockaddr}, we have
143 to type cast the Internet socket address into a \tr{struct sockaddr}. By luck(!),
144 the two structures are of the same size. Same casting is required of other
145 families of sockets such as Xerox NS. Similarly for Unix domain sockets.
147 To represent these socket addresses in Haskell-land, we do what BSD didn't do,
148 and use a union/algebraic type for the different families. Currently only
149 Unix domain sockets and the Internet family is supported.
153 -- NOTE: HostAddresses are represented in network byte order.
154 -- Functions that expect the address in machine byte order
155 -- will have to perform the necessary translation.
156 type HostAddress = Word
159 -- newtyped to prevent accidental use of sane-looking
160 -- port numbers that haven't actually been converted to
161 -- network-byte-order first.
163 newtype PortNumber = PNum Int -- 16-bit value stored in network byte order.
166 mkPortNumber :: Int -> PortNumber
167 mkPortNumber v = unsafePerformIO $ do
168 po <- _casm_ ``%r=(int)htons((int)%0); '' v
171 data SockAddr -- C Names
172 #ifndef cygwin32_TARGET_OS
173 = SockAddrUnix -- struct sockaddr_un
175 | SockAddrInet -- struct sockaddr_in
176 PortNumber -- sin_port (network byte order)
177 HostAddress -- sin_addr (ditto)
179 = SockAddrInet -- struct sockaddr_in
180 PortNumber -- sin_port (network byte order)
181 HostAddress -- sin_addr (ditto)
186 type ProtocolNumber = Int
191 %************************************************************************
193 \subsection[Socket-Connections]{Connection Functions}
195 %************************************************************************
197 In the following connection and binding primitives. The names of the
198 equivalent C functions have been preserved where possible. It should
199 be noted that some of these names used in the C library, \tr{bind} in
200 particular, have a different meaning to many Haskell programmers and
201 have thus been renamed by appending the prefix Socket.
203 Create an unconnected socket of the given family, type and protocol.
204 The most common invocation of $socket$ is the following:
208 my_socket <- socket AF_INET Stream 6
213 socket :: Family -- Family Name (usually AF_INET)
214 -> SocketType -- Socket Type (usually Stream)
215 -> ProtocolNumber -- Protocol Number (getProtocolByName to find value)
216 -> IO Socket -- Unconnected Socket
218 socket family stype protocol = do
219 status <- _ccall_ createSocket (packFamily family)
220 (packSocketType stype)
223 -1 -> constructErrorAndFail "socket"
225 socket_status <- newIORef NotConnected
226 return (MkSocket n family stype protocol socket_status)
229 Given a port number this {\em binds} the socket to that port. This
230 means that the programmer is only interested in data being sent to
231 that port number. The $Family$ passed to $bindSocket$ must
232 be the same as that passed to $socket$. If the special port
233 number $aNY\_PORT$ is passed then the system assigns the next
236 Port numbers for standard unix services can be found by calling
237 $getServiceEntry$. These are traditionally port numbers below
238 1000; although there are afew, namely NFS and IRC, which used higher
241 The port number allocated to a socket bound by using $aNY\_PORT$ can be
242 found by calling $port$
245 bindSocket :: Socket -- Unconnected Socket
246 -> SockAddr -- Address to Bind to
249 bindSocket (MkSocket s family stype protocol socketStatus) addr = do
250 #ifndef cygwin32_TARGET_OS
251 let isDomainSocket = if family == AF_UNIX then 1 else (0::Int)
253 let isDomainSocket = 0
255 currentStatus <- readIORef socketStatus
256 if currentStatus /= NotConnected
258 fail (userError ("bindSocket: can't peform bind on socket in status " ++
261 addr' <- packSockAddr addr
262 let (_,sz) = boundsOfByteArray addr'
263 status <- _ccall_ bindSocket s addr' sz isDomainSocket
265 -1 -> constructErrorAndFail "bindSocket"
266 0 -> writeIORef socketStatus (Bound)
270 Make a connection to an already opened socket on a given machine and port.
271 assumes that we have already called createSocket, othewise it will fail.
273 This is the dual to $bindSocket$. The {\em server} process will
274 usually bind to a port number, the {\em client} will then connect to
275 the same port number. Port numbers of user applications are normally
276 agreed in advance, otherwise we must rely on some meta protocol for telling
277 the other side what port number we have been allocated.
280 connect :: Socket -- Unconnected Socket
281 -> SockAddr -- Socket address stuff
284 connect (MkSocket s family stype protocol socketStatus) addr = do
285 #ifndef cygwin32_TARGET_OS
286 let isDomainSocket = if family == AF_UNIX then 1 else (0::Int)
288 let isDomainSocket = 0
290 currentStatus <- readIORef socketStatus
291 if currentStatus /= NotConnected
293 fail (userError ("connect: can't peform connect on socket in status " ++
296 addr' <- packSockAddr addr
297 let (_,sz) = boundsOfByteArray addr'
298 status <- _ccall_ connectSocket s addr' sz isDomainSocket
300 -1 -> constructErrorAndFail "connect"
301 0 -> writeIORef socketStatus Connected
304 The programmer must call $listen$ to tell the system software
305 that they are now interested in receiving data on this port. This
306 must be called on the bound socket before any calls to read or write
309 The programmer also gives a number which indicates the length of the
310 incoming queue of unread messages for this socket. On most systems the
311 maximum queue length is around 5. To remove a message from the queue
312 for processing a call to $accept$ should be made.
315 listen :: Socket -- Connected & Bound Socket
316 -> Int -- Queue Length
319 listen (MkSocket s family stype protocol socketStatus) backlog = do
320 currentStatus <- readIORef socketStatus
321 if currentStatus /= Bound
323 fail (userError ("listen: can't peform listen on socket in status " ++
326 status <- _ccall_ listenSocket s backlog
328 -1 -> constructErrorAndFail "listen"
329 0 -> writeIORef socketStatus Listening
332 A call to $accept$ only returns when data is available on the given
333 socket, unless the socket has been set to non-blocking. It will
334 return a new socket which should be used to read the incoming data and
335 should then be closed. Using the socket returned by $accept$ allows
336 incoming requests to be queued on the original socket.
339 accept :: Socket -- Queue Socket
340 -> IO (Socket, -- Readable Socket
341 SockAddr) -- Peer details
343 accept sock@(MkSocket s family stype protocol status) = do
344 currentStatus <- readIORef status
345 okay <- sIsAcceptable sock
348 fail (userError ("accept: can't peform accept on socket in status " ++
351 (ptr, sz) <- allocSockAddr family
352 int_star <- stToIO (newIntArray (0,1))
353 stToIO (writeIntArray int_star 0 sz)
354 sock <- _ccall_ acceptSocket s ptr int_star
356 -1 -> constructErrorAndFail "accept"
358 sz <- stToIO (readIntArray int_star 0)
359 addr <- unpackSockAddr ptr sz
360 status <- newIORef Connected
361 return ((MkSocket sock family stype protocol status), addr)
364 %************************************************************************
366 \subsection[Socket-DataPass]{Data Passing Primitives}
368 %************************************************************************
370 To allow Haskell to talk to C programs we need to be able to
371 communicate in terms of byte streams. @writeSocket@ and
372 @readSocket@ should only be used for this purpose and not for
373 communication between Haskell programs. Haskell programs should use
374 the 1.3 IO hPutStr and associated machinery for communicating with
379 writeSocket :: Socket -- Connected Socket
380 -> String -- Data to send
381 -> IO Int -- Number of Bytes sent
383 writeSocket (MkSocket s family stype protocol status) xs = do
384 currentStatus <- readIORef status
385 if not ((currentStatus == Connected) || (currentStatus == Listening))
387 fail (userError ("writeSocket: can't peform write on socket in status " ++
390 nbytes <- _ccall_ writeDescriptor s xs (length xs)
392 -1 -> constructErrorAndFail "writeSocket"
396 sendTo :: Socket -- Bound/Connected Socket
397 -> String -- Data to send
399 -> IO Int -- Number of Bytes sent
401 sendTo (MkSocket s family stype protocol status) xs addr = do
402 currentStatus <- readIORef status
403 if not ((currentStatus == Connected) || (currentStatus == Listening) || (currentStatus == Bound))
405 fail (userError ("sendTo: can't peform write on socket in status " ++
408 addr' <- packSockAddr addr
409 let (_,sz) = boundsOfByteArray addr'
410 nbytes <- _ccall_ sendTo__ s xs (length xs) addr' sz
412 -1 -> constructErrorAndFail "sendTo"
415 readSocket :: Socket -- Connected (or bound) Socket
416 -> Int -- Number of Bytes to Read
417 -> IO (String, Int) -- (Data Read, Number of Bytes)
419 readSocket (MkSocket s family stype protocol status) nbytes = do
420 currentStatus <- readIORef status
421 if not ((currentStatus == Connected) || (currentStatus == Listening))
423 fail (userError ("readSocket: can't perform read on socket in status " ++
426 ptr <- stToIO (newCharArray (0, nbytes))
427 nbytes <- _ccall_ readDescriptor s ptr nbytes
429 -1 -> constructErrorAndFail "readSocket"
431 barr <- stToIO (unsafeFreezeByteArray ptr)
432 return (unpackNBytesPS (byteArrayToPS barr) n, n)
434 readSocketAll :: Socket -> IO String
439 (readSocket s 4096 >>= \ (str, nbytes) ->
448 recvFrom :: Socket -> Int -> IO (String, Int, SockAddr)
449 recvFrom (MkSocket s family stype protocol status) nbytes = do
450 currentStatus <- readIORef status
451 if not ((currentStatus == Connected) || (currentStatus == Listening) || (currentStatus == Bound))
453 fail (userError ("recvFrom: can't perform read on socket in status " ++
456 ptr <- stToIO (newCharArray (0, nbytes))
457 (ptr_addr,_) <- allocSockAddr AF_INET
458 nbytes <- _ccall_ recvFrom__ s ptr nbytes ptr_addr
460 -1 -> constructErrorAndFail "recvFrom"
462 barr <- stToIO (unsafeFreezeByteArray ptr)
463 addr <- unpackSockAddrInet ptr_addr
464 return (unpackNBytesPS (byteArrayToPS barr) n, n, addr)
468 The port number the given socket is currently connected to can be
469 determined by calling $port$, is generally only useful when bind
470 was given $aNY\_PORT$.
473 socketPort :: Socket -- Connected & Bound Socket
474 -> IO PortNumber -- Port Number of Socket
475 socketPort sock@(MkSocket s AF_INET stype protocol status) =
476 getSocketName sock >>= \(SockAddrInet port _) ->
478 socketPort (MkSocket s family stype protocol status) =
479 fail (userError ("socketPort: not supported for Family " ++ show family))
482 Calling $getPeerName$ returns the address details of the machine,
483 other than the local one, which is connected to the socket. This is
484 used in programs such as FTP to determine where to send the returning
485 data. The corresponding call to get the details of the local machine
489 getPeerName :: Socket -> IO SockAddr
491 getPeerName (MkSocket s family stype protocol status) = do
492 (ptr, sz) <- allocSockAddr family
493 int_star <- stToIO (newIntArray (0,1))
494 stToIO (writeIntArray int_star 0 sz)
495 status <- _ccall_ getPeerName s ptr int_star
497 -1 -> constructErrorAndFail "getPeerName"
499 sz <- stToIO (readIntArray int_star 0)
500 unpackSockAddr ptr sz
502 getSocketName :: Socket -> IO SockAddr
504 getSocketName (MkSocket s family stype protocol status) = do
505 (ptr, sz) <- allocSockAddr family
506 int_star <- stToIO (newIntArray (0,1))
507 stToIO (writeIntArray int_star 0 sz)
508 status <- _ccall_ getSockName s ptr int_star
510 -1 -> constructErrorAndFail "getSocketName"
512 sz <- stToIO (readIntArray int_star 0)
513 unpackSockAddr ptr sz
519 %************************************************************************
521 \subsection[Socket-Properties]{Socket Properties}
523 %************************************************************************
527 = Broadcast {- SO_BROADCAST -}
528 | Debug {- SO_DEBUG -}
529 | DontRoute {- SO_DONTROUTE -}
530 | SoError {- SO_ERROR -}
531 | KeepAlive {- SO_KEEPALIVE -}
532 -- | Linger {- SO_LINGER -}
533 | OOBInline {- SO_OOBINLINE -}
534 | RecvBuffer {- SO_RCVBUF -}
535 | SendBuffer {- SO_SNDBUF -}
536 | RecvLowWater {- SO_RCVLOWAT -}
537 | SendLowWater {- SO_SNDLOWAT -}
538 | RecvTimeOut {- SO_RCVTIMEO -}
539 | SendTimeOut {- SO_SNDTIMEO -}
540 | ReuseAddr {- SO_REUSEADDR -}
542 | UseLoopBack {- SO_USELOOPBACK -} -- not used, I believe.
543 | MaxSegment {- TCP_MAXSEG -}
544 | NoDelay {- TCP_NODELAY -}
546 packSocketOption :: SocketOption -> Int
547 packSocketOption so =
549 Broadcast -> ``SO_BROADCAST''
550 Debug -> ``SO_DEBUG''
551 DontRoute -> ``SO_DONTROUTE''
552 SoError -> ``SO_ERROR''
553 KeepAlive -> ``SO_KEEPALIVE''
554 OOBInline -> ``SO_OOBINLINE''
555 RecvBuffer -> ``SO_RCVBUF''
556 SendBuffer -> ``SO_SNDBUF''
557 RecvLowWater -> ``SO_RCVLOWAT''
558 SendLowWater -> ``SO_SNDLOWAT''
559 RecvTimeOut -> ``SO_RCVTIMEO''
560 SendTimeOut -> ``SO_SNDTIMEO''
561 ReuseAddr -> ``SO_REUSEADDR''
563 UseLoopBack -> ``SO_USELOOPBACK''
564 MaxSegment -> ``TCP_MAXSEG''
565 NoDelay -> ``TCP_NODELAY''
567 setSocketOption :: Socket
568 -> SocketOption -- Option Name
569 -> Int -- Option Value
571 setSocketOption (MkSocket s family stype protocol status) so v = do
572 rc <- _ccall_ setSocketOption__ s (packSocketOption so) v
574 then constructErrorAndFail "setSocketOption"
577 getSocketOption :: Socket
578 -> SocketOption -- Option Name
579 -> IO Int -- Option Value
580 getSocketOption (MkSocket s family stype protocol status) so = do
581 rc <- _ccall_ getSocketOption__ s (packSocketOption so)
582 if rc == -1 -- let's just hope that value isn't taken..
583 then constructErrorAndFail "getSocketOption"
588 A calling sequence table for the main functions is shown in the table below.
592 \begin{tabular}{|l|c|c|c|c|c|c|c|}
594 {\bf A Call to} & socket & connect & bindSocket & listen & accept & read & write \\
596 {\bf Precedes} & & & & & & & \\
598 socket & & & & & & & \\
600 connect & + & & & & & & \\
602 bindSocket & + & & & & & & \\
604 listen & & & + & & & & \\
606 accept & & & & + & & & \\
608 read & & + & & + & + & + & + \\
610 write & & + & & + & + & + & + \\
613 \caption{Sequence Table for Major functions of Socket}
618 %************************************************************************
620 \subsection[Socket-OSDefs]{OS Dependent Definitions}
622 %************************************************************************
625 The following Family and Socket Type declarations were manually derived
626 from @<sys/socket.h>@ on the appropriate machines.
628 Maybe a configure script that could parse the socket.h file to produce
629 the following declaration is required to make it ``portable'' rather than
630 using the dreaded \#ifdefs.
632 Presently only the following machine/os combinations are supported:
644 unpackFamily :: Int -> Family
645 packFamily :: Family -> Int
647 packSocketType :: SocketType -> Int
650 #if sunos4_TARGET_OS || solaris2_TARGET_OS
653 AF_UNSPEC -- unspecified
654 | AF_UNIX -- local to host (pipes, portals
655 | AF_INET -- internetwork: UDP, TCP, etc
656 | AF_IMPLINK -- arpanet imp addresses
657 | AF_PUP -- pup protocols: e.g. BSP
658 | AF_CHAOS -- mit CHAOS protocols
659 | AF_NS -- XEROX NS protocols
660 | AF_NBS -- nbs protocols
661 | AF_ECMA -- european computer manufacturers
662 | AF_DATAKIT -- datakit protocols
663 | AF_CCITT -- CCITT protocols, X.25 etc
665 | AF_DECnet -- DECnet
666 | AF_DLI -- Direct data link interface
668 | AF_HYLINK -- NSC Hyperchannel
669 | AF_APPLETALK -- Apple Talk
670 | AF_NIT -- Network Interface Tap
671 | AF_802 -- IEEE 802.2, also ISO 8802
672 | AF_OSI -- umbrella of all families used by OSI
673 | AF_X25 -- CCITT X.25
675 | AF_GOSSIP -- US Government OSI
676 | AF_IPX -- Novell Internet Protocol
677 deriving (Eq, Ord, Ix, Show)
679 packFamily = index (AF_UNSPEC, AF_IPX)
680 unpackFamily family = (range (AF_UNSPEC, AF_IPX))!!family
684 #if cygwin32_TARGET_OS
687 AF_UNSPEC -- unspecified
688 | AF_UNIX -- local to host (pipes, portals)
689 | AF_INET -- internetwork: UDP, TCP, etc
690 | AF_IMPLINK -- arpanet imp addresses
691 | AF_PUP -- pup protocols: e.g. BSP
692 | AF_CHAOS -- mit CHAOS protocols
693 | AF_NS -- XEROX NS protocols
694 | AF_ISO -- ISO protocols
695 | AF_OSI -- OSI protocols
696 | AF_ECMA -- european computer manufacturers
697 | AF_DATAKIT -- datakit protocols
698 | AF_CCITT -- CCITT protocols, X.25 etc
700 | AF_DECnet -- DECnet
701 | AF_DLI -- Direct data link interface
703 | AF_HYLINK -- NSC Hyperchannel
704 | AF_APPLETALK -- Apple Talk
705 | AF_NETBIOS -- NetBios-style addresses
706 deriving (Eq, Ord, Ix, Show)
708 packFamily = index (AF_UNSPEC, AF_NETBIOS)
709 unpackFamily family = (range (AF_UNSPEC, AF_NETBIOS))!!family
717 AF_UNSPEC -- unspecified
718 | AF_UNIX -- local to host (pipes, portals
719 | AF_INET -- internetwork: UDP, TCP, etc
720 | AF_IMPLINK -- arpanet imp addresses
721 | AF_PUP -- pup protocols: e.g. BSP
722 | AF_CHAOS -- mit CHAOS protocols
723 | AF_NS -- XEROX NS protocols
724 | AF_NBS -- nbs protocols
725 | AF_ECMA -- european computer manufacturers
726 | AF_DATAKIT -- datakit protocols
727 | AF_CCITT -- CCITT protocols, X.25 etc
729 | AF_DECnet -- DECnet
730 | AF_DLI -- Direct data link interface
732 | AF_HYLINK -- NSC Hyperchannel
733 | AF_APPLETALK -- Apple Talk
734 | AF_NIT -- Network Interface Tap
735 deriving (Eq, Ord, Ix, Show)
737 packFamily = index (AF_UNSPEC, AF_NIT)
738 unpackFamily family = (range (AF_UNSPEC, AF_NIT))!!family
742 #if osf1_TARGET_OS || osf3_TARGET_OS
745 AF_UNSPEC -- unspecified
746 | AF_UNIX -- local to host (pipes, portals)
747 | AF_INET -- internetwork: UDP, TCP, etc.
748 | AF_IMPLINK -- arpanet imp addresses
749 | AF_PUP -- pup protocols: e.g. BSP
750 | AF_CHAOS -- mit CHAOS protocols
751 | AF_NS -- XEROX NS protocols
752 | AF_ISO -- ISO protocols
753 | AF_ECMA -- european computer manufacturers
754 | AF_DATAKIT -- datakit protocols
755 | AF_CCITT -- CCITT protocols, X.25 etc
757 | AF_DECnet -- DECnet
758 | AF_DLI -- DEC Direct data link interface
760 | AF_HYLINK -- NSC Hyperchannel
761 | AF_APPLETALK -- Apple Talk
762 | AF_ROUTE -- Internal Routing Protocol
763 | AF_LINK -- Link layer interface
764 | Pseudo_AF_XTP -- eXpress Transfer Protocol (no AF)
765 | AF_NETMAN -- DNA Network Management
766 | AF_X25 -- X25 protocol
767 | AF_CTF -- Common Trace Facility
768 | AF_WAN -- Wide Area Network protocols
769 deriving (Eq, Ord, Ix, Show)
771 packFamily = index (AF_UNSPEC, AF_WAN)
772 unpackFamily family = (range (AF_UNSPEC, AF_WAN))!!family
783 deriving (Eq, Ord, Ix, Show)
785 packFamily = index (AF_UNSPEC, AF_IPX)
786 unpackFamily family = (range (AF_UNSPEC, AF_IPX))!!family
793 AF_UNSPEC -- unspecified
794 | AF_UNIX -- backward compatibility
795 | AF_INET -- internetwork: UDP, TCP, etc.
796 | AF_IMPLINK -- arpanet imp addresses
797 | AF_PUP -- pup protocols: e.g. BSP
798 | AF_CHAOS -- mit CHAOS protocols
799 | AF_NS -- XEROX NS protocols
800 | AF_ISO -- ISO protocols
801 | AF_ECMA -- european computer manufacturers
802 | AF_DATAKIT -- datakit protocols
803 | AF_CCITT -- CCITT protocols, X.25 etc
805 | AF_DECnet -- DECnet
806 | AF_DLI -- DEC Direct data link interface
808 | AF_HYLINK -- NSC Hyperchannel
809 | AF_APPLETALK -- Apple Talk
810 | AF_ROUTE -- Internal Routing Protocol
811 | AF_RAW -- Link layer interface
813 -- these two overlap AF_ROUTE and AF_RAW
814 -- | AF_NIT -- Network Interface Tap
815 -- | AF_802 -- IEEE 802.2, also ISO 8802
817 | AF_OSI -- umbrella for all families used by OSI
818 | AF_X25 -- CCITT X.25
820 | AF_GOSIP -- US Government OSI
822 | AF_SDL -- SGI Data Link for DLPI
823 | AF_INET6 -- Internet Protocol version 6
824 | AF_LINK -- Link layer interface
825 deriving (Eq, Ord, Ix, Show)
827 packFamily = index (AF_UNSPEC, AF_LINK)
828 unpackFamily family = (range (AF_UNSPEC, AF_LINK))!!family
835 AF_UNSPEC -- unspecified
836 | AF_UNIX -- local to host (pipes, portals)
837 | AF_INET -- internetwork: UDP, TCP, etc.
838 | AF_IMPLINK -- arpanet imp addresses
839 | AF_PUP -- pup protocols: e.g. BSP
840 | AF_CHAOS -- mit CHAOS protocols
841 | AF_NS -- XEROX NS protocols
842 | AF_ISO -- ISO protocols
843 -- | AF_OSI is the same as AF_ISO on AIX
844 | AF_ECMA -- european computer manufacturers
845 | AF_DATAKIT -- datakit protocols
846 | AF_CCITT -- CCITT protocols, X.25 etc
848 | AF_DECnet -- DECnet
849 | AF_DLI -- DEC Direct data link interface
851 | AF_HYLINK -- NSC Hyperchannel
852 | AF_APPLETALK -- Apple Talk
853 | AF_ROUTE -- Internal Routing Protocol
854 | AF_LINK -- Link layer interface
855 | Pseudo_AF_XTP -- eXpress Transfer Protocol (no AF)
856 | AF_INTF -- Debugging use only
857 | AF_RIF -- raw interface
861 deriving (Eq, Ord, Ix, Show)
863 packFamily = index (AF_UNSPEC, AF_MAX)
864 unpackFamily family = (range (AF_UNSPEC, AF_MAX))!!family
868 #if freebsd_TARGET_OS
871 AF_UNSPEC -- unspecified
872 | AF_UNIX -- local to host (pipes, portals)
873 | AF_INET -- internetwork: UDP, TCP, etc.
874 | AF_IMPLINK -- arpanet imp addresses
875 | AF_PUP -- pup protocols: e.g. BSP
876 | AF_CHAOS -- mit CHAOS protocols
877 | AF_NS -- XEROX NS protocols
878 | AF_ISO -- ISO protocols
879 -- | AF_OSI is the same as AF_ISO
880 | AF_ECMA -- european computer manufacturers
881 | AF_DATAKIT -- datakit protocols
882 | AF_CCITT -- CCITT protocols, X.25 etc
884 | AF_DECnet -- DECnet
885 | AF_DLI -- DEC Direct data link interface
887 | AF_HYLINK -- NSC Hyperchannel
888 | AF_APPLETALK -- Apple Talk
889 | AF_ROUTE -- Internal Routing Protocol
890 | AF_LINK -- Link layer interface
891 | Pseudo_AF_XTP -- eXpress Transfer Protocol (no AF)
892 | AF_COIP -- connection-oriented IP, aka ST II
893 | AF_CNT -- Computer Network Technology
894 | Psuedo_AF_RTIP -- Help Identify RTIP packets
895 | AF_IPX -- Novell Internet Protocol
896 | AF_SIP -- Simple Internet Protocol
897 | Pseudo_AF_PIP -- Help Identify PIP packets
898 | AF_ISDN -- Integrated Services Digital Network
899 -- | AF_E164 is the same as AF_ISDN
900 | Pseudo_AF_KEY -- Internal key-management function
903 deriving (Eq, Ord, Ix, Show)
905 packFamily = index (AF_UNSPEC, AF_MAX)
906 unpackFamily family = (range (AF_UNSPEC, AF_MAX))!!family
910 -- Alpha running OSF or a SPARC with SunOS, rather than Solaris.
912 #if osf1_TARGET_OS || osf3_TARGET_OS || sunos4_TARGET_OS || hpux_TARGET_OS || \
913 aix_TARGET_OS || freebsd_TARGET_OS
920 deriving (Eq, Ord, Ix, Show)
922 packSocketType stype = 1 + (index (Stream, SeqPacket) stype)
925 -- This is for a box running cygwin32 toolchain.
927 #if defined(cygwin32_TARGET_OS)
932 | RDM -- reliably delivered msg
935 deriving (Eq, Ord, Ix, Show)
937 packSocketType stype =
939 Stream -> ``SOCK_STREAM''
940 Datagram -> ``SOCK_DGRAM''
943 SeqPacket -> ``SOCK_SEQPACKET''
944 Packet -> ``SOCK_PACKET''
948 -- This is a Sun running Solaris rather than SunOS or SGI running IRIX
950 #if defined(solaris2_TARGET_OS) || defined(irix_TARGET_OS)
958 deriving (Eq, Ord, Ix, Show)
960 packSocketType stype = 1 + (index (Datagram, SeqPacket) stype)
972 deriving (Eq, Ord, Ix, Show)
974 packSocketType stype = 1 + (index (Stream, Packet) stype)
978 %************************************************************************
980 \subsection[Socket-Util]{Utility Functions}
982 %************************************************************************
986 iNADDR_ANY :: HostAddress
987 iNADDR_ANY = unsafePerformIO (_casm_ `` %r = htonl(INADDR_ANY); '')
989 sOMAXCONN = ``SOMAXCONN''::Int
990 maxListenQueue = sOMAXCONN
992 -------------------------------------------------------------------------------
998 sdownCmdToInt :: ShutdownCmd -> Int
999 sdownCmdToInt ShutdownReceive = 0
1000 sdownCmdToInt ShutdownSend = 1
1001 sdownCmdToInt ShutdownBoth = 2
1003 shutdown :: Socket -> ShutdownCmd -> IO ()
1004 shutdown (MkSocket s _ _ _ _) stype = do
1005 let t = sdownCmdToInt stype
1006 status <- _ccall_ shutdownSocket s t
1008 -1 -> constructErrorAndFail "shutdown"
1011 -------------------------------------------------------------------------------
1013 sClose :: Socket -> IO ()
1014 sClose (MkSocket s family stype protocol status) = _ccall_ close s
1016 -------------------------------------------------------------------------------
1018 sIsConnected :: Socket -> IO Bool
1019 sIsConnected (MkSocket s family stype protocol status) = do
1020 value <- readIORef status
1021 return (value == Connected)
1023 -------------------------------------------------------------------------------
1025 sIsBound :: Socket -> IO Bool
1026 sIsBound (MkSocket s family stype protocol status) = do
1027 value <- readIORef status
1028 return (value == Bound)
1030 -------------------------------------------------------------------------------
1032 sIsListening :: Socket -> IO Bool
1033 sIsListening (MkSocket s family stype protocol status) = do
1034 value <- readIORef status
1035 return (value == Listening)
1037 -------------------------------------------------------------------------------
1039 sIsReadable :: Socket -> IO Bool
1040 sIsReadable (MkSocket s family stype protocol status) = do
1041 value <- readIORef status
1042 return (value == Listening || value == Connected)
1044 -------------------------------------------------------------------------------
1046 sIsWritable :: Socket -> IO Bool
1047 sIsWritable = sIsReadable
1049 -------------------------------------------------------------------------------
1051 sIsAcceptable :: Socket -> IO Bool
1052 #ifndef cygwin32_TARGET_OS
1053 sIsAcceptable (MkSocket s AF_UNIX Stream protocol status) = do
1054 value <- readIORef status
1055 return (value == Connected || value == Bound || value == Listening)
1056 sIsAcceptable (MkSocket s AF_UNIX _ protocol status) =
1059 sIsAcceptable (MkSocket s _ stype protocol status) = do
1060 value <- readIORef status
1061 return (value == Connected || value == Listening)
1063 -------------------------------------------------------------------------------
1066 sSetBlocking :: Socket -> Bool -> IO ()
1067 sIsBlocking :: Socket -> IO Bool
1072 Internet address manipulation routines:
1075 inet_addr :: String -> IO HostAddress
1076 inet_addr ipstr = do
1077 had <- _ccall_ inet_addr ipstr
1078 if had == (W# (int2Word# (negateInt# 1#))) -- hack to avoid depending on Int.intToWord here.
1079 then fail (userError ("inet_addr: Malformed address: " ++ ipstr))
1080 else return had -- network byte order
1082 inet_ntoa :: HostAddress -> IO String
1083 inet_ntoa haddr = do
1084 pstr <- _casm_ ``struct in_addr addr;
1086 %r = inet_ntoa (addr);'' haddr
1087 -- unpack straight away, since pstr points to static buffer.
1088 unpackCStringIO pstr
1092 Marshaling and allocation helper functions:
1095 -------------------------------------------------------------------------------
1097 allocSockAddr :: Family -> IO (MutableByteArray RealWorld Int, Int)
1099 #ifndef cygwin32_TARGET_OS
1100 allocSockAddr AF_UNIX = do
1101 ptr <- stToIO (newCharArray (0,``sizeof(struct sockaddr_un)''))
1102 let (_,sz) = boundsOfByteArray ptr
1106 allocSockAddr AF_INET = do
1107 ptr <- stToIO (newCharArray (0,``sizeof(struct sockaddr_in)''))
1108 let (_,sz) = boundsOfByteArray ptr
1111 -------------------------------------------------------------------------------
1113 unpackSockAddr :: MutableByteArray RealWorld Int -> Int -> IO SockAddr
1114 unpackSockAddr arr len = do
1115 fam <- _casm_ ``%r = ((struct sockaddr*)%0)->sa_family;'' arr
1116 case unpackFamily fam of
1117 #ifndef cygwin32_TARGET_OS
1118 AF_UNIX -> unpackSockAddrUnix arr (len - ``sizeof(short)'')
1120 AF_INET -> unpackSockAddrInet arr
1122 -------------------------------------------------------------------------------
1124 #ifndef cygwin32_TARGET_OS
1127 sun_path is *not* NULL terminated, hence we *do* need to know the
1130 unpackSockAddrUnix :: (MutableByteArray RealWorld Int) -> Int -> IO SockAddr
1131 unpackSockAddrUnix ptr len = do
1132 char_star <- _casm_ ``%r = ((struct sockaddr_un*)%0)->sun_path;'' ptr
1133 path <- unpackCStringLenIO char_star len
1134 return (SockAddrUnix path)
1138 -------------------------------------------------------------------------------
1140 unpackSockAddrInet :: (MutableByteArray RealWorld Int) -> IO SockAddr
1141 unpackSockAddrInet ptr = do
1142 port <- _casm_ ``%r = ((struct sockaddr_in*)%0)->sin_port;'' ptr
1143 addr <- _casm_ ``%r = ((struct sockaddr_in*)%0)->sin_addr.s_addr;'' ptr
1144 return (SockAddrInet (PNum port) addr)
1146 -------------------------------------------------------------------------------
1149 packSockAddr :: SockAddr -> IO (MutableByteArray RealWorld Int)
1150 #ifndef cygwin32_TARGET_OS
1151 packSockAddr (SockAddrUnix path) = do
1152 (ptr,_) <- allocSockAddr AF_UNIX
1153 _casm_ ``(((struct sockaddr_un *)%0)->sun_family) = AF_UNIX;'' ptr
1154 _casm_ ``strcpy ((((struct sockaddr_un *)%0)->sun_path),%1);'' ptr path
1157 packSockAddr (SockAddrInet (PNum port) address) = do
1158 (ptr,_) <- allocSockAddr AF_INET
1159 _casm_ ``(((struct sockaddr_in *)%0)->sin_family) = AF_INET;'' ptr
1160 _casm_ ``(((struct sockaddr_in *)%0)->sin_port) = (int)%1;'' ptr port
1161 _casm_ ``(((struct sockaddr_in *)%0)->sin_addr.s_addr) = %1;'' ptr address
1164 -------------------------------------------------------------------------------
1167 @socketHandle@ turns a @Socket@ into a Haskell IO @Handle@. By default, the new
1168 handle will not be buffered, use @hSetBuffering@ if you want to change
1172 #ifndef __PARALLEL_HASKELL__
1173 socketToHandle :: Socket -> IOMode -> IO Handle
1175 socketToHandle (MkSocket s family stype protocol status) m = do
1176 ptr <- _casm_ ``%r = fdopen (%0, (char *)%1);'' s m'
1177 fp <- makeForeignObj ptr (``&freeFile'' :: Addr)
1178 hndl <- newHandle (htype fp Nothing False)
1179 hSetBuffering hndl NoBuffering
1187 ReadWriteMode -> "r+"
1190 ReadMode -> ReadHandle
1191 WriteMode -> WriteHandle
1192 AppendMode -> AppendHandle
1193 ReadWriteMode -> ReadWriteHandle
1195 socketToHandle (MkSocket s family stype protocol status) m =
1196 error "socketToHandle not implemented in a parallel setup"